PostgreSQL Source Code  git master
fe-trace.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include "libpq-fe.h"
#include "libpq-int.h"
#include "port/pg_bswap.h"
Include dependency graph for fe-trace.c:

Go to the source code of this file.

Functions

void PQtrace (PGconn *conn, FILE *debug_port)
 
void PQuntrace (PGconn *conn)
 
void PQtraceSetFlags (PGconn *conn, int flags)
 
static void pqTraceFormatTimestamp (char *timestr, size_t ts_len)
 
static void pqTraceOutputByte1 (FILE *pfdebug, const char *data, int *cursor)
 
static int pqTraceOutputInt16 (FILE *pfdebug, const char *data, int *cursor)
 
static int pqTraceOutputInt32 (FILE *pfdebug, const char *data, int *cursor, bool suppress)
 
static void pqTraceOutputString (FILE *pfdebug, const char *data, int *cursor, bool suppress)
 
static void pqTraceOutputNchar (FILE *pfdebug, int len, const char *data, int *cursor)
 
static void pqTraceOutputA (FILE *f, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputB (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputC (FILE *f, bool toServer, const char *message, int *cursor)
 
static void pqTraceOutputD (FILE *f, bool toServer, const char *message, int *cursor)
 
static void pqTraceOutputNR (FILE *f, const char *type, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputE (FILE *f, bool toServer, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputf (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputF (FILE *f, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputG (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputH (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputK (FILE *f, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputP (FILE *f, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputQ (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputR (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputS (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputt (FILE *f, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputT (FILE *f, const char *message, int *cursor, bool regress)
 
static void pqTraceOutputv (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputV (FILE *f, const char *message, int *cursor)
 
static void pqTraceOutputW (FILE *f, const char *message, int *cursor, int length)
 
static void pqTraceOutputZ (FILE *f, const char *message, int *cursor)
 
void pqTraceOutputMessage (PGconn *conn, const char *message, bool toServer)
 
void pqTraceOutputNoTypeByteMessage (PGconn *conn, const char *message)
 

Function Documentation

◆ PQtrace()

void PQtrace ( PGconn conn,
FILE *  debug_port 
)

Definition at line 35 of file fe-trace.c.

References pg_conn::Pfdebug, PQuntrace(), and pg_conn::traceFlags.

Referenced by main().

36 {
37  if (conn == NULL)
38  return;
39  PQuntrace(conn);
40  if (debug_port == NULL)
41  return;
42 
43  conn->Pfdebug = debug_port;
44  conn->traceFlags = 0;
45 }
int traceFlags
Definition: libpq-int.h:398
FILE * Pfdebug
Definition: libpq-int.h:397
void PQuntrace(PGconn *conn)
Definition: fe-trace.c:49

◆ pqTraceFormatTimestamp()

static void pqTraceFormatTimestamp ( char *  timestr,
size_t  ts_len 
)
static

Definition at line 80 of file fe-trace.c.

References gettimeofday(), now(), and snprintf.

Referenced by pqTraceOutputMessage(), and pqTraceOutputNoTypeByteMessage().

81 {
82  struct timeval tval;
83  time_t now;
84 
85  gettimeofday(&tval, NULL);
86 
87  /*
88  * MSVC's implementation of timeval uses a long for tv_sec, however,
89  * localtime() expects a time_t pointer. Here we'll assign tv_sec to a
90  * local time_t variable so that we pass localtime() the correct pointer
91  * type.
92  */
93  now = tval.tv_sec;
94  strftime(timestr, ts_len,
95  "%Y-%m-%d %H:%M:%S",
96  localtime(&now));
97  /* append microseconds */
98  snprintf(timestr + strlen(timestr), ts_len - strlen(timestr),
99  ".%06u", (unsigned int) (tval.tv_usec));
100 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104
#define snprintf
Definition: port.h:216
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ pqTraceOutputA()

static void pqTraceOutputA ( FILE *  f,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 220 of file fe-trace.c.

References fprintf, pqTraceOutputInt32(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

221 {
222  fprintf(f, "NotificationResponse\t");
223  pqTraceOutputInt32(f, message, cursor, regress);
224  pqTraceOutputString(f, message, cursor, false);
225  pqTraceOutputString(f, message, cursor, false);
226 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144

◆ pqTraceOutputB()

static void pqTraceOutputB ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 230 of file fe-trace.c.

References fprintf, i, pqTraceOutputInt16(), pqTraceOutputInt32(), pqTraceOutputNchar(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

231 {
232  int nparams;
233 
234  fprintf(f, "Bind\t");
235  pqTraceOutputString(f, message, cursor, false);
236  pqTraceOutputString(f, message, cursor, false);
237  nparams = pqTraceOutputInt16(f, message, cursor);
238 
239  for (int i = 0; i < nparams; i++)
240  pqTraceOutputInt16(f, message, cursor);
241 
242  nparams = pqTraceOutputInt16(f, message, cursor);
243 
244  for (int i = 0; i < nparams; i++)
245  {
246  int nbytes;
247 
248  nbytes = pqTraceOutputInt32(f, message, cursor, false);
249  if (nbytes == -1)
250  continue;
251  pqTraceOutputNchar(f, nbytes, message, cursor);
252  }
253 
254  nparams = pqTraceOutputInt16(f, message, cursor);
255  for (int i = 0; i < nparams; i++)
256  pqTraceOutputInt16(f, message, cursor);
257 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
int i
static void pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
Definition: fe-trace.c:188

◆ pqTraceOutputByte1()

static void pqTraceOutputByte1 ( FILE *  pfdebug,
const char *  data,
int *  cursor 
)
static

Definition at line 106 of file fe-trace.c.

References fprintf.

Referenced by pqTraceOutputC(), pqTraceOutputD(), pqTraceOutputG(), pqTraceOutputH(), pqTraceOutputNR(), pqTraceOutputW(), and pqTraceOutputZ().

107 {
108  const char *v = data + *cursor;
109 
110  /*
111  * Show non-printable data in hex format, including the terminating \0
112  * that completes ErrorResponse and NoticeResponse messages.
113  */
114  if (!isprint((unsigned char) *v))
115  fprintf(pfdebug, " \\x%02x", *v);
116  else
117  fprintf(pfdebug, " %c", *v);
118  *cursor += 1;
119 }
#define fprintf
Definition: port.h:220
Definition: type.h:130

◆ pqTraceOutputC()

static void pqTraceOutputC ( FILE *  f,
bool  toServer,
const char *  message,
int *  cursor 
)
static

Definition at line 261 of file fe-trace.c.

References fprintf, pqTraceOutputByte1(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

262 {
263  if (toServer)
264  {
265  fprintf(f, "Close\t");
266  pqTraceOutputByte1(f, message, cursor);
267  pqTraceOutputString(f, message, cursor, false);
268  }
269  else
270  {
271  fprintf(f, "CommandComplete\t");
272  pqTraceOutputString(f, message, cursor, false);
273  }
274 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106

◆ pqTraceOutputD()

static void pqTraceOutputD ( FILE *  f,
bool  toServer,
const char *  message,
int *  cursor 
)
static

Definition at line 278 of file fe-trace.c.

References fprintf, i, pqTraceOutputByte1(), pqTraceOutputInt16(), pqTraceOutputInt32(), pqTraceOutputNchar(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

279 {
280  if (toServer)
281  {
282  fprintf(f, "Describe\t");
283  pqTraceOutputByte1(f, message, cursor);
284  pqTraceOutputString(f, message, cursor, false);
285  }
286  else
287  {
288  int nfields;
289  int len;
290  int i;
291 
292  fprintf(f, "DataRow\t");
293  nfields = pqTraceOutputInt16(f, message, cursor);
294  for (i = 0; i < nfields; i++)
295  {
296  len = pqTraceOutputInt32(f, message, cursor, false);
297  if (len == -1)
298  continue;
299  pqTraceOutputNchar(f, len, message, cursor);
300  }
301  }
302 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
int i
static void pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
Definition: fe-trace.c:188

◆ pqTraceOutputE()

static void pqTraceOutputE ( FILE *  f,
bool  toServer,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 327 of file fe-trace.c.

References fprintf, pqTraceOutputInt32(), pqTraceOutputNR(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

328 {
329  if (toServer)
330  {
331  fprintf(f, "Execute\t");
332  pqTraceOutputString(f, message, cursor, false);
333  pqTraceOutputInt32(f, message, cursor, false);
334  }
335  else
336  pqTraceOutputNR(f, "ErrorResponse", message, cursor, regress);
337 }
static void pqTraceOutputNR(FILE *f, const char *type, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:306
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144

◆ pqTraceOutputf()

static void pqTraceOutputf ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 341 of file fe-trace.c.

References fprintf, and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

342 {
343  fprintf(f, "CopyFail\t");
344  pqTraceOutputString(f, message, cursor, false);
345 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130

◆ pqTraceOutputF()

static void pqTraceOutputF ( FILE *  f,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 349 of file fe-trace.c.

References fprintf, i, pqTraceOutputInt16(), pqTraceOutputInt32(), and pqTraceOutputNchar().

Referenced by pqTraceOutputMessage().

350 {
351  int nfields;
352  int nbytes;
353 
354  fprintf(f, "FunctionCall\t");
355  pqTraceOutputInt32(f, message, cursor, regress);
356  nfields = pqTraceOutputInt16(f, message, cursor);
357 
358  for (int i = 0; i < nfields; i++)
359  pqTraceOutputInt16(f, message, cursor);
360 
361  nfields = pqTraceOutputInt16(f, message, cursor);
362 
363  for (int i = 0; i < nfields; i++)
364  {
365  nbytes = pqTraceOutputInt32(f, message, cursor, false);
366  if (nbytes == -1)
367  continue;
368  pqTraceOutputNchar(f, nbytes, message, cursor);
369  }
370 
371  pqTraceOutputInt16(f, message, cursor);
372 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
int i
static void pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
Definition: fe-trace.c:188

◆ pqTraceOutputG()

static void pqTraceOutputG ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 376 of file fe-trace.c.

References fprintf, i, pqTraceOutputByte1(), and pqTraceOutputInt16().

Referenced by pqTraceOutputMessage().

377 {
378  int nfields;
379 
380  fprintf(f, "CopyInResponse\t");
381  pqTraceOutputByte1(f, message, cursor);
382  nfields = pqTraceOutputInt16(f, message, cursor);
383 
384  for (int i = 0; i < nfields; i++)
385  pqTraceOutputInt16(f, message, cursor);
386 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
int i

◆ pqTraceOutputH()

static void pqTraceOutputH ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 390 of file fe-trace.c.

References fprintf, i, pqTraceOutputByte1(), and pqTraceOutputInt16().

Referenced by pqTraceOutputMessage().

391 {
392  int nfields;
393 
394  fprintf(f, "CopyOutResponse\t");
395  pqTraceOutputByte1(f, message, cursor);
396  nfields = pqTraceOutputInt16(f, message, cursor);
397 
398  for (int i = 0; i < nfields; i++)
399  pqTraceOutputInt16(f, message, cursor);
400 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
int i

◆ pqTraceOutputInt16()

static int pqTraceOutputInt16 ( FILE *  pfdebug,
const char *  data,
int *  cursor 
)
static

Definition at line 125 of file fe-trace.c.

References fprintf, and pg_ntoh16.

Referenced by pqTraceOutputB(), pqTraceOutputD(), pqTraceOutputF(), pqTraceOutputG(), pqTraceOutputH(), pqTraceOutputP(), pqTraceOutputt(), pqTraceOutputT(), and pqTraceOutputW().

126 {
127  uint16 tmp;
128  int result;
129 
130  memcpy(&tmp, data + *cursor, 2);
131  *cursor += 2;
132  result = (int) pg_ntoh16(tmp);
133  fprintf(pfdebug, " %d", result);
134 
135  return result;
136 }
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
#define fprintf
Definition: port.h:220
unsigned short uint16
Definition: c.h:440
Definition: type.h:130

◆ pqTraceOutputInt32()

static int pqTraceOutputInt32 ( FILE *  pfdebug,
const char *  data,
int *  cursor,
bool  suppress 
)
static

Definition at line 144 of file fe-trace.c.

References fprintf, and pg_ntoh32.

Referenced by pqTraceOutputA(), pqTraceOutputB(), pqTraceOutputD(), pqTraceOutputE(), pqTraceOutputF(), pqTraceOutputK(), pqTraceOutputNoTypeByteMessage(), pqTraceOutputP(), pqTraceOutputR(), pqTraceOutputt(), pqTraceOutputT(), pqTraceOutputv(), and pqTraceOutputV().

145 {
146  int result;
147 
148  memcpy(&result, data + *cursor, 4);
149  *cursor += 4;
150  result = (int) pg_ntoh32(result);
151  if (suppress)
152  fprintf(pfdebug, " NNNN");
153  else
154  fprintf(pfdebug, " %d", result);
155 
156  return result;
157 }
#define fprintf
Definition: port.h:220
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
Definition: type.h:130

◆ pqTraceOutputK()

static void pqTraceOutputK ( FILE *  f,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 404 of file fe-trace.c.

References fprintf, and pqTraceOutputInt32().

Referenced by pqTraceOutputMessage().

405 {
406  fprintf(f, "BackendKeyData\t");
407  pqTraceOutputInt32(f, message, cursor, regress);
408  pqTraceOutputInt32(f, message, cursor, regress);
409 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144

◆ pqTraceOutputMessage()

void pqTraceOutputMessage ( PGconn conn,
const char *  message,
bool  toServer 
)

Definition at line 529 of file fe-trace.c.

References fprintf, pg_conn::Pfdebug, pg_ntoh32, PQTRACE_REGRESS_MODE, PQTRACE_SUPPRESS_TIMESTAMPS, pqTraceFormatTimestamp(), pqTraceOutputA(), pqTraceOutputB(), pqTraceOutputC(), pqTraceOutputD(), pqTraceOutputE(), pqTraceOutputf(), pqTraceOutputF(), pqTraceOutputG(), pqTraceOutputH(), pqTraceOutputK(), pqTraceOutputNR(), pqTraceOutputP(), pqTraceOutputQ(), pqTraceOutputR(), pqTraceOutputS(), pqTraceOutputt(), pqTraceOutputT(), pqTraceOutputv(), pqTraceOutputV(), pqTraceOutputW(), pqTraceOutputZ(), and pg_conn::traceFlags.

Referenced by getCopyDataMessage(), pqFunctionCall3(), pqParseInput3(), and pqPutMsgEnd().

530 {
531  char id;
532  int length;
533  char *prefix = toServer ? "F" : "B";
534  int logCursor = 0;
535  bool regress;
536 
537  if ((conn->traceFlags & PQTRACE_SUPPRESS_TIMESTAMPS) == 0)
538  {
539  char timestr[128];
540 
541  pqTraceFormatTimestamp(timestr, sizeof(timestr));
542  fprintf(conn->Pfdebug, "%s\t", timestr);
543  }
544  regress = (conn->traceFlags & PQTRACE_REGRESS_MODE) != 0;
545 
546  id = message[logCursor++];
547 
548  memcpy(&length, message + logCursor, 4);
549  length = (int) pg_ntoh32(length);
550  logCursor += 4;
551 
552  /*
553  * In regress mode, suppress the length of ErrorResponse and
554  * NoticeResponse. The F (file name), L (line number) and R (routine
555  * name) fields can change as server code is modified, and if their
556  * lengths differ from the originals, that would break tests.
557  */
558  if (regress && !toServer && (id == 'E' || id == 'N'))
559  fprintf(conn->Pfdebug, "%s\tNN\t", prefix);
560  else
561  fprintf(conn->Pfdebug, "%s\t%d\t", prefix, length);
562 
563  switch (id)
564  {
565  case '1':
566  fprintf(conn->Pfdebug, "ParseComplete");
567  /* No message content */
568  break;
569  case '2':
570  fprintf(conn->Pfdebug, "BindComplete");
571  /* No message content */
572  break;
573  case '3':
574  fprintf(conn->Pfdebug, "CloseComplete");
575  /* No message content */
576  break;
577  case 'A': /* Notification Response */
578  pqTraceOutputA(conn->Pfdebug, message, &logCursor, regress);
579  break;
580  case 'B': /* Bind */
581  pqTraceOutputB(conn->Pfdebug, message, &logCursor);
582  break;
583  case 'c':
584  fprintf(conn->Pfdebug, "CopyDone");
585  /* No message content */
586  break;
587  case 'C': /* Close(F) or Command Complete(B) */
588  pqTraceOutputC(conn->Pfdebug, toServer, message, &logCursor);
589  break;
590  case 'd': /* Copy Data */
591  /* Drop COPY data to reduce the overhead of logging. */
592  break;
593  case 'D': /* Describe(F) or Data Row(B) */
594  pqTraceOutputD(conn->Pfdebug, toServer, message, &logCursor);
595  break;
596  case 'E': /* Execute(F) or Error Response(B) */
597  pqTraceOutputE(conn->Pfdebug, toServer, message, &logCursor,
598  regress);
599  break;
600  case 'f': /* Copy Fail */
601  pqTraceOutputf(conn->Pfdebug, message, &logCursor);
602  break;
603  case 'F': /* Function Call */
604  pqTraceOutputF(conn->Pfdebug, message, &logCursor, regress);
605  break;
606  case 'G': /* Start Copy In */
607  pqTraceOutputG(conn->Pfdebug, message, &logCursor);
608  break;
609  case 'H': /* Flush(F) or Start Copy Out(B) */
610  if (!toServer)
611  pqTraceOutputH(conn->Pfdebug, message, &logCursor);
612  else
613  fprintf(conn->Pfdebug, "Flush"); /* no message content */
614  break;
615  case 'I':
616  fprintf(conn->Pfdebug, "EmptyQueryResponse");
617  /* No message content */
618  break;
619  case 'K': /* secret key data from the backend */
620  pqTraceOutputK(conn->Pfdebug, message, &logCursor, regress);
621  break;
622  case 'n':
623  fprintf(conn->Pfdebug, "NoData");
624  /* No message content */
625  break;
626  case 'N':
627  pqTraceOutputNR(conn->Pfdebug, "NoticeResponse", message,
628  &logCursor, regress);
629  break;
630  case 'P': /* Parse */
631  pqTraceOutputP(conn->Pfdebug, message, &logCursor, regress);
632  break;
633  case 'Q': /* Query */
634  pqTraceOutputQ(conn->Pfdebug, message, &logCursor);
635  break;
636  case 'R': /* Authentication */
637  pqTraceOutputR(conn->Pfdebug, message, &logCursor);
638  break;
639  case 's':
640  fprintf(conn->Pfdebug, "PortalSuspended");
641  /* No message content */
642  break;
643  case 'S': /* Parameter Status(B) or Sync(F) */
644  if (!toServer)
645  pqTraceOutputS(conn->Pfdebug, message, &logCursor);
646  else
647  fprintf(conn->Pfdebug, "Sync"); /* no message content */
648  break;
649  case 't': /* Parameter Description */
650  pqTraceOutputt(conn->Pfdebug, message, &logCursor, regress);
651  break;
652  case 'T': /* Row Description */
653  pqTraceOutputT(conn->Pfdebug, message, &logCursor, regress);
654  break;
655  case 'v': /* Negotiate Protocol Version */
656  pqTraceOutputv(conn->Pfdebug, message, &logCursor);
657  break;
658  case 'V': /* Function Call response */
659  pqTraceOutputV(conn->Pfdebug, message, &logCursor);
660  break;
661  case 'W': /* Start Copy Both */
662  pqTraceOutputW(conn->Pfdebug, message, &logCursor, length);
663  break;
664  case 'X':
665  fprintf(conn->Pfdebug, "Terminate");
666  /* No message content */
667  break;
668  case 'Z': /* Ready For Query */
669  pqTraceOutputZ(conn->Pfdebug, message, &logCursor);
670  break;
671  default:
672  fprintf(conn->Pfdebug, "Unknown message: %02x", id);
673  break;
674  }
675 
676  fputc('\n', conn->Pfdebug);
677 
678  /*
679  * Verify the printing routine did it right. Note that the one-byte
680  * message identifier is not included in the length, but our cursor does
681  * include it.
682  */
683  if (logCursor - 1 != length)
684  fprintf(conn->Pfdebug,
685  "mismatched message length: consumed %d, expected %d\n",
686  logCursor - 1, length);
687 }
static void pqTraceOutputK(FILE *f, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:404
static void pqTraceOutputC(FILE *f, bool toServer, const char *message, int *cursor)
Definition: fe-trace.c:261
static void pqTraceOutputNR(FILE *f, const char *type, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:306
static void pqTraceOutputG(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:376
static void pqTraceOutputT(FILE *f, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:466
static void pqTraceOutputQ(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:428
static void pqTraceOutputA(FILE *f, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:220
static void pqTraceOutputR(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:436
static void pqTraceOutputV(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:496
static void pqTraceOutputH(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:390
static void pqTraceOutputf(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:341
static void pqTraceOutputW(FILE *f, const char *message, int *cursor, int length)
Definition: fe-trace.c:508
int traceFlags
Definition: libpq-int.h:398
#define fprintf
Definition: port.h:220
FILE * Pfdebug
Definition: libpq-int.h:397
static void pqTraceOutputB(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:230
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
static void pqTraceOutputF(FILE *f, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:349
static void pqTraceOutputD(FILE *f, bool toServer, const char *message, int *cursor)
Definition: fe-trace.c:278
static void pqTraceOutputt(FILE *f, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:453
static void pqTraceOutputZ(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:519
static void pqTraceOutputE(FILE *f, bool toServer, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:327
static void pqTraceOutputP(FILE *f, const char *message, int *cursor, bool regress)
Definition: fe-trace.c:413
#define PQTRACE_REGRESS_MODE
Definition: libpq-fe.h:405
static void pqTraceOutputv(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:487
#define PQTRACE_SUPPRESS_TIMESTAMPS
Definition: libpq-fe.h:403
static void pqTraceFormatTimestamp(char *timestr, size_t ts_len)
Definition: fe-trace.c:80
static void pqTraceOutputS(FILE *f, const char *message, int *cursor)
Definition: fe-trace.c:444

◆ pqTraceOutputNchar()

static void pqTraceOutputNchar ( FILE *  pfdebug,
int  len,
const char *  data,
int *  cursor 
)
static

Definition at line 188 of file fe-trace.c.

References fprintf, i, and next.

Referenced by pqTraceOutputB(), pqTraceOutputD(), pqTraceOutputF(), and pqTraceOutputV().

189 {
190  int i,
191  next; /* first char not yet printed */
192  const char *v = data + *cursor;
193 
194  fprintf(pfdebug, " \'");
195 
196  for (next = i = 0; i < len; ++i)
197  {
198  if (isprint((unsigned char) v[i]))
199  continue;
200  else
201  {
202  fwrite(v + next, 1, i - next, pfdebug);
203  fprintf(pfdebug, "\\x%02x", v[i]);
204  next = i + 1;
205  }
206  }
207  if (next < len)
208  fwrite(v + next, 1, len - next, pfdebug);
209 
210  fprintf(pfdebug, "\'");
211  *cursor += len;
212 }
static int32 next
Definition: blutils.c:219
#define fprintf
Definition: port.h:220
Definition: type.h:130
int i

◆ pqTraceOutputNoTypeByteMessage()

void pqTraceOutputNoTypeByteMessage ( PGconn conn,
const char *  message 
)

Definition at line 694 of file fe-trace.c.

References fprintf, pg_conn::Pfdebug, pg_ntoh32, PQTRACE_SUPPRESS_TIMESTAMPS, pqTraceFormatTimestamp(), pqTraceOutputInt32(), and pg_conn::traceFlags.

Referenced by pqPutMsgEnd().

695 {
696  int length;
697  int logCursor = 0;
698 
699  if ((conn->traceFlags & PQTRACE_SUPPRESS_TIMESTAMPS) == 0)
700  {
701  char timestr[128];
702 
703  pqTraceFormatTimestamp(timestr, sizeof(timestr));
704  fprintf(conn->Pfdebug, "%s\t", timestr);
705  }
706 
707  memcpy(&length, message + logCursor, 4);
708  length = (int) pg_ntoh32(length);
709  logCursor += 4;
710 
711  fprintf(conn->Pfdebug, "F\t%d\t", length);
712 
713  switch (length)
714  {
715  case 16: /* CancelRequest */
716  fprintf(conn->Pfdebug, "CancelRequest\t");
717  pqTraceOutputInt32(conn->Pfdebug, message, &logCursor, false);
718  pqTraceOutputInt32(conn->Pfdebug, message, &logCursor, false);
719  pqTraceOutputInt32(conn->Pfdebug, message, &logCursor, false);
720  break;
721  case 8: /* GSSENCRequest or SSLRequest */
722  /* These messages do not reach here. */
723  default:
724  fprintf(conn->Pfdebug, "Unknown message: length is %d", length);
725  break;
726  }
727 
728  fputc('\n', conn->Pfdebug);
729 }
int traceFlags
Definition: libpq-int.h:398
#define fprintf
Definition: port.h:220
FILE * Pfdebug
Definition: libpq-int.h:397
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define PQTRACE_SUPPRESS_TIMESTAMPS
Definition: libpq-fe.h:403
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
static void pqTraceFormatTimestamp(char *timestr, size_t ts_len)
Definition: fe-trace.c:80

◆ pqTraceOutputNR()

static void pqTraceOutputNR ( FILE *  f,
const char *  type,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 306 of file fe-trace.c.

References fprintf, pqTraceOutputByte1(), and pqTraceOutputString().

Referenced by pqTraceOutputE(), and pqTraceOutputMessage().

308 {
309  fprintf(f, "%s\t", type);
310  for (;;)
311  {
312  char field;
313  bool suppress;
314 
315  pqTraceOutputByte1(f, message, cursor);
316  field = message[*cursor - 1];
317  if (field == '\0')
318  break;
319 
320  suppress = regress && (field == 'L' || field == 'F' || field == 'R');
321  pqTraceOutputString(f, message, cursor, suppress);
322  }
323 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106

◆ pqTraceOutputP()

static void pqTraceOutputP ( FILE *  f,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 413 of file fe-trace.c.

References fprintf, i, pqTraceOutputInt16(), pqTraceOutputInt32(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

414 {
415  int nparams;
416 
417  fprintf(f, "Parse\t");
418  pqTraceOutputString(f, message, cursor, false);
419  pqTraceOutputString(f, message, cursor, false);
420  nparams = pqTraceOutputInt16(f, message, cursor);
421 
422  for (int i = 0; i < nparams; i++)
423  pqTraceOutputInt32(f, message, cursor, regress);
424 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
int i

◆ pqTraceOutputQ()

static void pqTraceOutputQ ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 428 of file fe-trace.c.

References fprintf, and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

429 {
430  fprintf(f, "Query\t");
431  pqTraceOutputString(f, message, cursor, false);
432 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130

◆ pqTraceOutputR()

static void pqTraceOutputR ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 436 of file fe-trace.c.

References fprintf, and pqTraceOutputInt32().

Referenced by pqTraceOutputMessage().

437 {
438  fprintf(f, "Authentication\t");
439  pqTraceOutputInt32(f, message, cursor, false);
440 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144

◆ pqTraceOutputS()

static void pqTraceOutputS ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 444 of file fe-trace.c.

References fprintf, and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

445 {
446  fprintf(f, "ParameterStatus\t");
447  pqTraceOutputString(f, message, cursor, false);
448  pqTraceOutputString(f, message, cursor, false);
449 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130

◆ pqTraceOutputString()

static void pqTraceOutputString ( FILE *  pfdebug,
const char *  data,
int *  cursor,
bool  suppress 
)
static

Definition at line 163 of file fe-trace.c.

References fprintf.

Referenced by pqTraceOutputA(), pqTraceOutputB(), pqTraceOutputC(), pqTraceOutputD(), pqTraceOutputE(), pqTraceOutputf(), pqTraceOutputNR(), pqTraceOutputP(), pqTraceOutputQ(), pqTraceOutputS(), and pqTraceOutputT().

164 {
165  int len;
166 
167  if (suppress)
168  {
169  fprintf(pfdebug, " \"SSSS\"");
170  *cursor += strlen(data + *cursor) + 1;
171  }
172  else
173  {
174  len = fprintf(pfdebug, " \"%s\"", data + *cursor);
175 
176  /*
177  * This is a null-terminated string. So add 1 after subtracting 3
178  * which is the double quotes and space length from len.
179  */
180  *cursor += (len - 3 + 1);
181  }
182 }
#define fprintf
Definition: port.h:220
Definition: type.h:130

◆ pqTraceOutputt()

static void pqTraceOutputt ( FILE *  f,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 453 of file fe-trace.c.

References fprintf, i, pqTraceOutputInt16(), and pqTraceOutputInt32().

Referenced by pqTraceOutputMessage().

454 {
455  int nfields;
456 
457  fprintf(f, "ParameterDescription\t");
458  nfields = pqTraceOutputInt16(f, message, cursor);
459 
460  for (int i = 0; i < nfields; i++)
461  pqTraceOutputInt32(f, message, cursor, regress);
462 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
int i

◆ pqTraceOutputT()

static void pqTraceOutputT ( FILE *  f,
const char *  message,
int *  cursor,
bool  regress 
)
static

Definition at line 466 of file fe-trace.c.

References fprintf, i, pqTraceOutputInt16(), pqTraceOutputInt32(), and pqTraceOutputString().

Referenced by pqTraceOutputMessage().

467 {
468  int nfields;
469 
470  fprintf(f, "RowDescription\t");
471  nfields = pqTraceOutputInt16(f, message, cursor);
472 
473  for (int i = 0; i < nfields; i++)
474  {
475  pqTraceOutputString(f, message, cursor, false);
476  pqTraceOutputInt32(f, message, cursor, regress);
477  pqTraceOutputInt16(f, message, cursor);
478  pqTraceOutputInt32(f, message, cursor, regress);
479  pqTraceOutputInt16(f, message, cursor);
480  pqTraceOutputInt32(f, message, cursor, false);
481  pqTraceOutputInt16(f, message, cursor);
482  }
483 }
static void pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:163
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
int i

◆ pqTraceOutputv()

static void pqTraceOutputv ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 487 of file fe-trace.c.

References fprintf, and pqTraceOutputInt32().

Referenced by pqTraceOutputMessage().

488 {
489  fprintf(f, "NegotiateProtocolVersion\t");
490  pqTraceOutputInt32(f, message, cursor, false);
491  pqTraceOutputInt32(f, message, cursor, false);
492 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144

◆ pqTraceOutputV()

static void pqTraceOutputV ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 496 of file fe-trace.c.

References fprintf, pqTraceOutputInt32(), and pqTraceOutputNchar().

Referenced by pqTraceOutputMessage().

497 {
498  int len;
499 
500  fprintf(f, "FunctionCallResponse\t");
501  len = pqTraceOutputInt32(f, message, cursor, false);
502  if (len != -1)
503  pqTraceOutputNchar(f, len, message, cursor);
504 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static int pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
Definition: fe-trace.c:144
static void pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
Definition: fe-trace.c:188

◆ pqTraceOutputW()

static void pqTraceOutputW ( FILE *  f,
const char *  message,
int *  cursor,
int  length 
)
static

Definition at line 508 of file fe-trace.c.

References fprintf, pqTraceOutputByte1(), and pqTraceOutputInt16().

Referenced by pqTraceOutputMessage().

509 {
510  fprintf(f, "CopyBothResponse\t");
511  pqTraceOutputByte1(f, message, cursor);
512 
513  while (length > *cursor)
514  pqTraceOutputInt16(f, message, cursor);
515 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106
static int pqTraceOutputInt16(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:125

◆ pqTraceOutputZ()

static void pqTraceOutputZ ( FILE *  f,
const char *  message,
int *  cursor 
)
static

Definition at line 519 of file fe-trace.c.

References fprintf, and pqTraceOutputByte1().

Referenced by pqTraceOutputMessage().

520 {
521  fprintf(f, "ReadyForQuery\t");
522  pqTraceOutputByte1(f, message, cursor);
523 }
#define fprintf
Definition: port.h:220
Definition: type.h:130
static void pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
Definition: fe-trace.c:106

◆ PQtraceSetFlags()

void PQtraceSetFlags ( PGconn conn,
int  flags 
)

Definition at line 64 of file fe-trace.c.

References pg_conn::Pfdebug, and pg_conn::traceFlags.

Referenced by main().

65 {
66  if (conn == NULL)
67  return;
68  /* If PQtrace() failed, do nothing. */
69  if (conn->Pfdebug == NULL)
70  return;
71  conn->traceFlags = flags;
72 }
int traceFlags
Definition: libpq-int.h:398
FILE * Pfdebug
Definition: libpq-int.h:397

◆ PQuntrace()

void PQuntrace ( PGconn conn)

Definition at line 49 of file fe-trace.c.

References pg_conn::Pfdebug, and pg_conn::traceFlags.

Referenced by PQtrace().

50 {
51  if (conn == NULL)
52  return;
53  if (conn->Pfdebug)
54  {
55  fflush(conn->Pfdebug);
56  conn->Pfdebug = NULL;
57  }
58 
59  conn->traceFlags = 0;
60 }
int traceFlags
Definition: libpq-int.h:398
FILE * Pfdebug
Definition: libpq-int.h:397