1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
|
#include "pgreplay.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
/* long enough to contain the beginning of a log line */
#define BUFLEN 1024
/* separates log line entries */
#define SEPCHAR '|'
/* types of log entries */
typedef enum {
log_debug5,
log_debug4,
log_debug3,
log_debug2,
log_debug1,
log_info,
log_notice,
log_warning,
log_error,
log_log,
log_fatal,
log_panic,
log_unknown
} log_type;
/* type for functions parsing the next log entry */
typedef int (parse_log_entry_func)(struct timeval *, char *, char *, uint64_t *, log_type *, char **, char **);
/* functions for parsing stderr and CSV log entries */
static parse_log_entry_func parse_errlog_entry;
static parse_log_entry_func parse_csvlog_entry;
static parse_log_entry_func * const parse_log_entry[2] = {
&parse_errlog_entry,
&parse_csvlog_entry
};
/* used to remember prepared statements */
struct prep_stmt {
char *name;
struct prep_stmt *next;
};
/* used to remember "open" connections */
struct connection {
uint64_t session_id;
struct connection *next;
struct prep_stmt *statements;
};
/* hash structure for "open" connections */
static struct connection * open_conn[256] = { NULL };
/* indicates whether we are parsing a CSV file */
static int csv;
/* start and end timestamp for parsing log entries */
static const char *start_time, *end_time;
/* database and username filters for parsing log entries */
static const char *database_only, *username_only;
/* file which we parse */
static int infile;
/* line number for error messages */
static unsigned long lineno = 0;
/* offset for time values (what mktime(3) makes of 2000-01-01 00:00:00)
used to make timestamps independent of local time and broken mktime */
static time_t epoch;
/* time of the first and last statement that we parse */
static struct timeval first_stmt_time, last_stmt_time;
/* statistics */
static unsigned long stat_simple = 0; /* simple statements */
static unsigned long stat_copy = 0; /* copy statements */
static unsigned long stat_param = 0; /* parametrized statements */
static unsigned long stat_named = 0; /* different named statements */
static unsigned long stat_execnamed = 0; /* named statement executions */
static unsigned long stat_fastpath = 0; /* fast-path function calls */
static unsigned long stat_cancel = 0; /* cancel requests */
/* a version of strcpy that handles overlapping strings well */
static char *overlap_strcpy(char *dest, const char *src) {
register char c;
while((c = *(src++))) {
*(dest++) = c;
}
*dest = '\0';
return dest;
}
/* convert a string to a log_type */
static log_type to_log_type(const char* s) {
/* compare in order of expected likelyhood for performance */
if (! strcmp(s, "LOG")) {
return log_log;
} else if (! strcmp(s, "ERROR")) {
return log_error;
} else if (! strcmp(s, "STATEMENT")) {
return log_unknown;
} else if (! strcmp(s, "DETAIL")) {
return log_unknown;
} else if (! strcmp(s, "HINT")) {
return log_unknown;
} else if (! strcmp(s, "FATAL")) {
return log_fatal;
} else if (! strcmp(s, "WARNING")) {
return log_warning;
} else if (! strcmp(s, "NOTICE")) {
return log_notice;
} else if (! strcmp(s, "INFO")) {
return log_info;
} else if (! strcmp(s, "PANIC")) {
return log_panic;
} else if (! strcmp(s, "DEBUG1")) {
return log_debug1;
} else if (! strcmp(s, "DEBUG2")) {
return log_debug2;
} else if (! strcmp(s, "DEBUG3")) {
return log_debug3;
} else if (! strcmp(s, "DEBUG4")) {
return log_debug4;
} else if (! strcmp(s, "DEBUG5")) {
return log_debug5;
} else {
return log_unknown;
}
}
/* Parses a timestamp (ignoring the time zone part).
If "dest" is not null, the parsed time will be returned there.
Return value is NULL on success, else an error message */
const char * parse_time(const char *source, struct timeval *dest) {
int i;
static struct tm tm; /* initialize with zeros */
char s[24] = { '\0' }; /* modifiable copy of source */
static char errmsg[BUFLEN];
/* format of timestamp part */
static const char format[]="nnnn-nn-nn nn:nn:nn.nnn";
/* check timestamp for validity */
if (!source) {
strcpy(errmsg, "NULL passed as timestamp string");
return errmsg;
}
if (strlen(source) < strlen(format)) {
sprintf(errmsg, "timestamp string is less than %lu characters long", (unsigned long)strlen(format));
return errmsg;
}
if (strlen(source) >= BUFLEN) {
sprintf(errmsg, "timestamp string is more than %d characters long", BUFLEN-1);
return errmsg;
}
for (i=0; i<strlen(format); ++i) {
switch (format[i]) {
case 'n':
if ((source[i] < '0') || (source[i] > '9')) {
sprintf(errmsg, "character %d in timestamp string is '%c', expected digit", i+1, source[i]);
return errmsg;
} else
s[i] = source[i];
break;
default:
if (source[i] != format[i]) {
sprintf(errmsg, "character %d in timestamp string is '%c', expected '%c'", i+1, source[i], format[i]);
return errmsg;
} else
s[i] = '\0'; /* tokenize parts */
}
}
/* parse time into 'tm' */
tm.tm_year = atoi(s) - 1900;
tm.tm_mon = atoi(s + 5) - 1;
tm.tm_mday = atoi(s + 8);
tm.tm_hour = atoi(s + 11);
tm.tm_min = atoi(s + 14);
tm.tm_sec = atoi(s + 17);
tm.tm_isdst = 0; /* ignore daylight savings time */
if (dest) {
dest->tv_sec = mktime(&tm) - epoch;
dest->tv_usec = atoi(s + 20) * 1000;
}
return NULL;
}
static char * parse_session(const char *source, uint64_t *dest) {
char s[BUFLEN]; /* modifiable copy of source */
static char errmsg[BUFLEN];
char *s1 = NULL, c;
uint32_t part1, part2;
int i;
/* check input for validity */
if (!source) {
strcpy(errmsg, "NULL passed as session id string");
return errmsg;
}
if (strlen(source) > BUFLEN -1) {
sprintf(errmsg, "session id string is more than %d characters long", BUFLEN);
return errmsg;
}
for (i=0; i<=strlen(source); ++i) {
c = source[i];
if (('.' == c) && (! s1)) {
s[i] = '\0';
s1 = s + i + 1;
} else if (((c < '0') || (c > '9')) && ((c < 'a') || (c > 'f')) && ('\0' != c)) {
sprintf(errmsg, "character %d in session id string is '%c', expected hex digit", i+1, c);
return errmsg;
} else
s[i] = c;
}
if (! s1) {
strcpy(errmsg, "Missing \".\" in session id string");
return errmsg;
}
if ((strlen(s) > 8) || (strlen(s1) > 8)) {
strcpy(errmsg, "none of the parts of a session id string may be longer than 8 hex digits");
return errmsg;
}
/* convert both parts */
sscanf(s, UINT32_FORMAT, &part1);
sscanf(s1, UINT32_FORMAT, &part2);
*dest = (((uint64_t)part1) << 32) + part2;
return NULL;
}
/* reads one log entry from the input file
the result is a malloc'ed string that must be freed
a return value of NULL means that there was an error */
static char * read_log_line() {
char *line, buf[BUFLEN] = { '\0' }, *p;
int len, escaped = 0, nl_found = 0, line_size = 0, i, l;
ssize_t bytes_read;
/* this will contain stuff we have read from the file but not used yet */
static char peekbuf[BUFLEN] = { '\0' };
static int peeklen = 0;
debug(3, "Entering read_log_line, current line number %lu\n", lineno+1);
/* pre-allocate the result to length 1 */
if (NULL == (line = malloc(1))) {
fprintf(stderr, "Cannot allocate 1 byte of memory\n");
return NULL;
}
*line = '\0';
while (! nl_found) {
/* if there were any chars left from the last invokation, use them first */
len = peeklen;
if (len) {
strcpy(buf, peekbuf);
peekbuf[0] = '\0';
}
peeklen = 0;
/* read from file until buf is full (at most) */
if (len < BUFLEN - 1) {
if (-1 == (bytes_read = read(infile, buf + len, BUFLEN - 1 - len))) {
perror("Error reading from input file");
return NULL;
}
len += bytes_read;
buf[len] = '\0';
}
/* if there is still nothing, we're done */
if (0 == len) {
debug(2, "Encountered EOF%s\n", "");
debug(3, "Leaving read_log_line%s\n", "");
return line;
}
/* search the string for unescaped newlines */
for (p=buf; *p!='\0'; ++p) {
if (csv && ('"' == *p)) {
escaped = !escaped;
}
/* keep up with line count */
lineno += ('\n' == *p);
/* look for unescaped newline */
if (!escaped && ('\n' == *p)) {
/* if a newline is found, truncate the string
and prepend the rest to peekbuf */
l = len - (++p - buf);
/* right shift peekbuf by l */
for (i=peeklen; i>=0; --i) {
peekbuf[l+i] = peekbuf[i];
}
strncpy(peekbuf, p, l);
*p = '\0';
peeklen += len - (p - buf);
len = p - buf;
if (csv) {
/* for a CSV file, this must be the end of the log entry */
nl_found = 1;
break; /* out from the for loop */
} else {
/* in a stderr log file, we must check for a
continuation line (newline + tab) */
/* first, make sure there is something to peek at */
if (0 == peeklen) {
/* try to read one more byte from the file */
if (-1 == (bytes_read = read(infile, peekbuf, 1))) {
perror("Error reading from input file");
return NULL;
}
if (0 == bytes_read) {
/* EOF means end of log entry */
nl_found = 1;
break; /* out from the for loop */
} else {
peeklen = bytes_read;
peekbuf[peeklen] = '\0';
}
}
/* then check for a continuation tab */
if ('\t' == *peekbuf) {
/* continuation line, remove tab
and copy peekbuf back to buf */
strncpy(p--, peekbuf + 1, BUFLEN - 1 - len);
if (peeklen > BUFLEN - len) {
overlap_strcpy(peekbuf, peekbuf + (BUFLEN - len));
peeklen = peeklen - BUFLEN + len;
len = BUFLEN - 1;
} else {
*peekbuf = '\0';
len += peeklen - 1;
peeklen = 0;
}
buf[len] = '\0';
} else {
/* end of log entry reached */
nl_found = 1;
break; /* out from the for loop */
}
}
}
}
/* extend result line and append buf */
line_size += len;
if (NULL == (p = realloc(line, line_size+1))) {
fprintf(stderr, "Cannot allocate %d bytes of memory\n", line_size);
free(line); line = NULL;
return NULL;
}
line = p;
strcat(line, buf);
*buf = '\0';
len = 0;
}
/* remove trailing newline in result if present */
if ('\n' == line[line_size - 1]) {
line[line_size - 1] = '\0';
}
debug(3, "Leaving read_log_line%s\n", "");
return line;
}
/* parses the next stderr log entry (and maybe a detail message after that)
timestamp, user, database, session ID, log message type, log message
and detail message are returned in the respective parameters
"message" and "detail" are malloc'ed if they are not NULL
return values: -1 (error), 0 (end-of-file), or 1 (success) */
static int parse_errlog_entry(struct timeval *time, char *user, char *database, uint64_t *session_id, log_type *type, char **message, char **detail) {
char *line = NULL, *part2, *part3, *part4, *part5, *part6;
const char *errmsg;
int i, skip_line = 0;
static int dump_found = 0;
/* if not NULL, contains the next log entry to parse */
static char* keepline = NULL;
debug(3, "Entering parse_errlog_entry%s\n", "");
/* initialize message and detail with NULL */
*message = NULL;
*detail = NULL;
/* use cached line or read next line from log file */
if (keepline) {
line = keepline;
keepline = NULL;
} else {
/* read lines until we are between start_time and end_time */
do {
if (line) {
free(line);
}
if (NULL == (line = read_log_line())) {
return -1;
}
/* is it the start of a memory dump? */
if (0 == strncmp(line, "TopMemoryContext: ", 18)) {
fprintf(stderr, "Found memory dump in line %lu\n", lineno);
dump_found = 1;
skip_line = 1;
} else {
/* if there is a dump and the line starts blank,
assume the line is part of the dump
*/
if (dump_found && (' ' == *line)) {
skip_line = 1;
} else {
skip_line = 0;
}
}
} while (('\0' != *line)
&& (skip_line
|| (start_time && (strncmp(line, start_time, 23) < 0))));
}
/* check for EOF */
if (('\0' == *line) || (end_time && (strncmp(line, end_time, 23) > 0))) {
free(line);
debug(3, "Leaving parse_errlog_entry%s\n", "");
return 0;
}
/* split line on | in six pieces: time, user, database, session ID, log entry type, rest */
if (NULL == (part2 = strchr(line, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: no \"%c\" found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part2++) = '\0';
}
if (NULL == (part3 = strchr(part2, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: second \"%c\" not found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part3++) = '\0';
}
if (NULL == (part4 = strchr(part3, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: third \"%c\" not found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part4++) = '\0';
}
if (NULL == (part5 = strchr(part4, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: fourth \"%c\" not found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part5++) = '\0';
}
if (NULL == (part6 = strstr(part5, ": "))) {
fprintf(stderr, "Error parsing line %lu: log message does not begin with a log type\n", lineno);
free(line);
return -1;
} else {
*part6 = '\0';
part6 += 3;
}
/* first part is the time, parse it into parameter */
if ((errmsg = parse_time(line, time))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* second part is the username, copy to parameter */
if (NAMELEN < strlen(part2)) {
fprintf(stderr, "Error parsing line %lu: username exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(user, part2);
}
/* third part is the database, copy to parameter */
if (NAMELEN < strlen(part3)) {
fprintf(stderr, "Error parsing line %lu: database name exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(database, part3);
}
/* fourth part is the session ID, copy to parameter */
if ((errmsg = parse_session(part4, session_id))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* fifth part is the log type, copy to parameter */
*type = to_log_type(part5);
/* sixth part is the log message */
overlap_strcpy(line, part6);
*message = line;
/* read the next log entry so that we can peek at it */
line = NULL;
do {
if (NULL != line) {
free(line);
}
if (NULL == (line = read_log_line())) {
free(*message);
*message = NULL;
return -1;
}
/* is it the start of a memory dump? */
if (0 == strncmp(line, "TopMemoryContext: ", 18)) {
fprintf(stderr, "Found memory dump in line %lu\n", lineno);
dump_found = 1;
skip_line = 1;
} else {
/* if there is a dump and the line starts blank,
assume the line is part of the dump
*/
if (dump_found && (' ' == *line)) {
skip_line = 1;
} else {
skip_line = 0;
}
}
} while (('\0' != *line) && skip_line);
if ('\0' == *line) {
/* EOF, that's ok */
keepline = line;
} else {
/* skip four | to the fifth part */
part2 = line;
for (i=0; i<4; ++i) {
if (NULL == (part2 = strchr(part2, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: only %d \"%c\" found - log_line_prefix may be wrong\n", lineno, i, SEPCHAR);
free(*message);
free(line);
*message = NULL;
return -1;
} else {
++part2;
}
}
/* check if it is a DETAIL */
if (strncmp(part2, "DETAIL: ", 9)) {
/* if not, remember the line for the next pass */
keepline = line;
} else {
debug(2, "Found a DETAIL message%s\n", "");
/* set the return parameter to the detail message */
overlap_strcpy(line, part2 + 9);
*detail = line;
}
}
debug(3, "Leaving parse_errlog_entry%s\n", "");
return 1;
}
/* parses the next CSV log entry
timestamp, user, database, session ID, log message type, log message
and detail message are returned in the respective parameters
"message" is malloc'ed, "detail" not
return values: -1 (error), 0 (end-of-file), or 1 (success) */
static int parse_csvlog_entry(struct timeval *time, char *user, char *database, uint64_t *session_id, log_type *type, char **message, char **detail) {
char *line = NULL, *part[16], *p1, *p2;
const char *errmsg;
int i, escaped = 0;
debug(3, "Entering parse_csvlog_entry%s\n", "");
/* initialize message and detail with NULL */
*message = NULL;
*detail = NULL;
/* read next line after start timestamp from log file */
do {
if (line) {
free(line);
}
if (NULL == (line = read_log_line())) {
return -1;
}
} while (('\0' != *line)
&& (start_time && (strncmp(line, start_time, 23) < 0)));
/* check for EOF */
if (('\0' == *line) || (end_time && (strncmp(line, end_time, 23) > 0))) {
free(line);
debug(3, "Leaving parse_errlog_entry%s\n", "");
return 0;
}
/* parse first 15 parts from the CSV record */
part[0] = p1 = line;
for (i=1; i<16; ++i) {
p2 = p1;
/* copy p1 to p2 until we hit an unescaped comma,
remove escaping double quotes */
while (escaped || (',' != *p1)) {
switch (*p1) {
case '\0':
fprintf(stderr, "Error parsing line %lu: comma number %d not found (or unmatched quotes)\n", lineno, i);
free(line);
return -1;
case '"':
/* don't copy the first double quote */
if (!escaped && (p1 != part[i-1])) {
*(p2++) = '"';
}
++p1;
escaped = !escaped;
break;
default:
*(p2++) = *(p1++);
}
}
*p2 = '\0';
part[i] = ++p1;
}
/* first part is the time, parse it into parameter */
if ((errmsg = parse_time(part[0], time))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* second part is the username, copy to parameter */
if (NAMELEN < strlen(part[1])) {
fprintf(stderr, "Error parsing line %lu: username exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(user, part[1]);
}
/* third part is the database, copy to parameter */
if (NAMELEN < strlen(part[2])) {
fprintf(stderr, "Error parsing line %lu: database name exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(database, part[2]);
}
/* sixth part is the session ID, copy to parameter */
if ((errmsg = parse_session(part[5], session_id))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* twelfth part is the log type, copy to parameter */
*type = to_log_type(part[11]);
/* fourteenth part is the message, assign to output parameter */
overlap_strcpy(line, part[13]);
*message = line;
/* detail is the fifteenth part of the line, if not empty */
*detail = part[14];
if ('\0' == **detail) {
*detail = NULL;
}
debug(3, "Leaving parse_csvlog_entry%s\n", "");
return 1;
}
/* add (malloc) the prepared statement name to the list of
prepared statements for the connection
returns 0 if the statement already existed, 1 if it was added and -1 if there was an error */
static int add_pstmt(struct connection * conn, char const *name) {
struct prep_stmt *pstmt = conn->statements;
int rc;
debug(3, "Entering add_pstmt for statement \"%s\"\n", name);
if ('\0' == *name) {
/* the empty statement will never be stored, but should be prepared */
rc = 1;
/* count for statistics */
++stat_param;
} else {
while (pstmt && strcmp(pstmt->name, name)) {
pstmt = pstmt->next;
}
if (pstmt) {
/* statement already prepared */
debug(2, "Prepared statement is already in list%s\n", "");
rc = 0;
} else {
debug(2, "Adding prepared statement to list%s\n", "");
/* add statement name to linked list */
if (NULL == (pstmt = malloc(sizeof(struct prep_stmt)))) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", (unsigned long)sizeof(struct prep_stmt));
return -1;
}
if (NULL == (pstmt->name = malloc(strlen(name) + 1))) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", (unsigned long)strlen(name) + 1);
free(pstmt);
return -1;
}
strcpy(pstmt->name, name);
pstmt->next = conn->statements;
conn->statements = pstmt;
rc = 1;
/* count for statistics */
++stat_named;
}
/* count for statistics */
++stat_execnamed;
}
debug(3, "Leaving add_pstmt%s\n", "");
return rc;
}
/* remove (free) the prepared statement name to the list of
prepared statements for the connection */
static void remove_pstmt(struct connection * conn, char const *name) {
struct prep_stmt *pstmt = conn->statements, *pstmt2 = NULL;
debug(3, "Entering remove_pstmt for statement \"%s\"\n", name);
while (pstmt && strcmp(pstmt->name, name)) {
pstmt2 = pstmt; /*remember previous */
pstmt = pstmt->next;
}
if (pstmt) {
if (pstmt2) {
pstmt2->next = pstmt->next;
} else {
conn->statements = pstmt->next;
}
free(pstmt->name);
free(pstmt);
} else {
debug(2, "Prepared statement not found%s\n", "");
}
debug(3, "Leaving remove_pstmt%s\n", "");
return;
}
static void remove_all_pstmts(struct connection * conn) {
struct prep_stmt *pstmt = conn->statements, *pstmt2 = NULL;
debug(3, "Entering remove_all_pstmts%s\n", "");
while(pstmt) {
pstmt2 = pstmt;
pstmt = pstmt->next;
free(pstmt2->name);
free(pstmt2);
}
conn->statements = NULL;
debug(3, "Leaving remove_all_pstmts%s\n", "");
return;
}
/* remove all "COPY" and "SET client_encoding" statements;
for DEALLOCATE statements, try to remove prepared statement */
/* maximum number of tokens we need to analyze a statement */
#define MAX_TOKENS 3
static int filter_bad_statements(char *line, struct connection *conn) {
char *statement = line, *p = line, token[MAX_TOKENS][NAMELEN + 1],
*q = NULL, *quote, *h;
int comment_depth, tokens = 0, ok = 1, i, nameindex, quotelen;
debug(3, "Entering filter_bad_statements%s\n", "");
for (i=0; i<MAX_TOKENS; ++i) {
token[i][0] = '\0';
}
while (ok) {
if (('\0' == *p) || (';' == *p)) {
/* end of a statement found */
if (tokens > 0) {
/* count parsed simple statements */
++stat_simple;
/* remove statements that won't work */
if (! strcmp("copy", token[0])) {
fprintf(stderr, "Warning: COPY statement ignored in line %lu\n", lineno);
/* replace statement with blanks */
while (statement < p) {
*(statement++) = ' ';
}
/* count for statistics */
++stat_copy;
} else if ((tokens > 1) && (! strcmp("set", token[0])) && (! strcmp("client_encoding", token[1]))) {
fprintf(stderr, "Warning: \"SET client_encoding\" statement ignored in line %lu\n", lineno);
/* replace statement with blanks */
while (statement < p) {
*(statement++) = ' ';
}
} else if (! strcmp("deallocate", token[0])) {
/* there coule be a "prepare" in the second token, should be ignored */
if (strcmp("prepare", token[1])) {
nameindex = 1;
} else {
nameindex = 2;
}
if (strcmp("all", token[nameindex])) {
/* deallocate single statement */
debug(2, "Deallocating prepared statement \"%s\"\n", token[nameindex]);
remove_pstmt(conn, token[nameindex]);
} else {
/* deallocate all prepared statements */
debug(2, "Deallocating all prepared statements%s\n", "");
remove_all_pstmts(conn);
}
}
}
/* break out of loop if end-of-line is reached */
if ('\0' == *p) {
break;
}
/* else prepare for next statement */
statement = ++p;
for (i=0; i<MAX_TOKENS; ++i) {
token[i][0] = '\0';
}
tokens = 0;
} else if ((('E' == *p) || ('e' == *p)) && ('\'' == p[1])) {
/* special string constant; skip to end */
++p;
while ('\0' != *(++p)) {
if ('\'' == *p) {
if ('\'' == p[1]) {
/* regular escaped apostrophe */
++p;
} else {
break;
}
}
if (('\\' == *p) && (('\'' == p[1]) || ('\\' == p[1]))) {
/* backslash escaped apostrophe or backslash */
++p;
}
}
if ('\0' == *p) {
fprintf(stderr, "Error: string literal not closed near line %lu\n"
"Hint: retry with%s the -q option\n",
lineno, (backslash_quote ? "out" : ""));
ok = 0;
} else {
++p;
}
} else if ('\'' == *p) {
/* simple string constant; skip to end */
while ('\0' != *(++p)) {
if ('\'' == *p) {
if ('\'' == p[1]) {
/* regular escaped apostrophe */
++p;
} else {
break;
}
}
if (backslash_quote && ('\\' == *p) && (('\'' == p[1]) || ('\\' == p[1]))) {
/* backslash escaped apostrophe or backslash */
++p;
}
}
if ('\0' == *p) {
fprintf(stderr, "Error: string literal not closed near line %lu\n"
"Hint: retry with%s the -q option\n",
lineno, (backslash_quote ? "out" : ""));
ok = 0;
} else {
++p;
}
} else if (('$' == *p) && (('0' > *(p+1)) || (('9' < *(p+1))))) {
/* dollar quoted string constant; skip to end */
quote = p++;
while (('$' != *p) && ('\0' != *p)) {
++p;
}
if ('\0' == *p) {
fprintf(stderr, "Error: end of dollar quote not found in line %lu\n", lineno);
ok = 0;
} else {
quotelen = p - quote;
*p = '\0';
h = p;
do {
h = strstr(++h, quote);
} while ((NULL != h) && ('$' != *(h + quotelen)));
*p = '$';
if (NULL == h) {
fprintf(stderr, "Error: end of dollar quoted string found in line %lu\n", lineno);
ok = 0;
} else {
p = h + (quotelen + 1);
}
}
} else if (('-' == *p) && ('-' == p[1])) {
/* comment; skip to end of line or statement */
while (('\n' != *p) && ('\0' != *p)) {
++p;
}
} else if (('/' == *p) && ('*' == p[1])) {
/* comment, skip to matching end */
p += 2;
comment_depth = 1; /* comments can be nested */
while (0 != comment_depth) {
if ('\0' == *p) {
fprintf(stderr, "Error: comment not closed in line %lu\n", lineno);
ok = 0;
break;
} else if (('*' == *p) && ('/' == p[1])) {
--comment_depth;
p += 2;
} else if (('/' == *p) && ('*' == p[1])) {
++comment_depth;
p += 2;
} else {
++p;
}
}
} else if ('"' == *p) {
/* quoted identifier, copy to token if necessary */
if (tokens < MAX_TOKENS) {
q = token[tokens];
}
while (1) {
++p;
if ('\0' == *p) {
fprintf(stderr, "Error: quoted identifier not closed in line %lu\n", lineno);
ok = 0;
break;
} else if ('"' == *p) {
if ('"' == p[1]) {
/* double " means a single " in a quoted identifier */
if ((tokens < MAX_TOKENS) && (q - token[tokens] < NAMELEN)) {
*(q++) = '"';
}
++p;
} else {
/* end of token */
if (tokens < MAX_TOKENS) {
*q = '\0';
++tokens;
}
++p;
break;
}
} else {
/* normal character, copy to token */
if ((tokens < MAX_TOKENS) && (q - token[tokens] < NAMELEN)) {
*(q++) = *p;
}
}
}
} else if ((('A' <= *p) && ('Z' >= *p))
|| (('a' <= *p) && ('z' >= *p))
|| (127 < (unsigned char)(*p)) /* consider > 127 as letter */
|| ('_' == *p)) {
/* normal identifier, copy to token if necessary */
if (tokens < MAX_TOKENS) {
q = token[tokens];
}
while ((('A' <= *p) && ('Z' >= *p))
|| (('a' <= *p) && ('z' >= *p))
|| (('0' <= *p) && ('9' >= *p))
|| (127 < (unsigned char)(*p)) /* consider > 127 as letter */
|| ('_' == *p) || ('$' == *p)) {
if ((tokens < MAX_TOKENS) && (q - token[tokens] < NAMELEN)) {
/* convert to lowercase */
*(q++) = *p + ('a' - 'A') * ((*p >= 'A') && (*p <= 'Z'));
}
++p;
}
*q = '\0';
++tokens;
} else {
/* everything else is considered unimportant */
++p;
}
}
debug(3, "Leaving filter_bad_statements%s\n", "");
return ok;
}
/* check if there is a connection for this session_id
if not, a new connection replay_item is generated and returned in r
return values: found or created hash entry for success, NULL for failure */
static struct connection *add_connection(replay_item **r, struct timeval *time, const char *user, const char *database, uint64_t session_id) {
unsigned char hash = hash_session(session_id);
struct connection *conn = open_conn[hash];
debug(3, "Entering add_connection for session 0x" UINT64_FORMAT "\n", session_id);
while (conn && (conn->session_id != session_id)) {
conn = conn->next;
}
if (conn) {
/* session already exists */
*r = NULL;
} else {
/* session doesn't exist yet; create it and add it to hash table */
if (NULL == (*r = replay_create_connect(time, session_id, user, database))) {
/* something went wrong */
return NULL;
} else {
if ((conn = malloc(sizeof(struct connection)))) {
conn->next = open_conn[hash];
conn->statements = NULL;
conn->session_id = session_id;
open_conn[hash] = conn;
} else {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", (unsigned long)sizeof(struct connection));
replay_free(*r);
return NULL;
}
}
}
debug(3, "Leaving add_connection%s\n", "");
return conn;
}
/* remove a connection from the hash structure
returns 1 for success, 0 for failure */
static int remove_connection(uint64_t session_id) {
unsigned char hash = hash_session(session_id);
struct connection *conn = open_conn[hash], *conn2 = NULL;
debug(3, "Entering remove_connection for session 0x" UINT64_FORMAT "\n", session_id);
while (conn && (conn->session_id != session_id)) {
conn2 = conn; /* remember previous */
conn = conn->next;
}
if (conn) {
remove_all_pstmts(conn);
if (conn2) {
conn2->next = conn->next;
} else {
open_conn[hash] = conn->next;
}
free(conn);
}
debug(3, "Leaving remove_connection%s\n", "");
return (NULL != conn);
}
/* tokenizes the "line" into single arguments, pointers to which
are stored in "result" (which is malloc'ed and must be freed)
returns the number of arguments and -1 if there is an error
Note that the strings that "result" points to are *not* malloc'ed,
but part of "line" */
static int parse_bind_args(char *** const result, char *line) {
int count = 0;
char *p, *p1;
*result = NULL;
debug(3, "Entering parse_bind_args%s\n", "");
/* count the dollar signs in the line = upper limit for number of arguments */
for (p=line; '\0'!=*p; ++p) {
if ('$' == *p) {
++count;
}
}
/* if there is none, we are done */
if (! count) {
return 0;
}
/* allocate result */
if (NULL == (*result = calloc(count, sizeof(char **)))) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", (unsigned long)sizeof(char **));
return -1;
}
/* reset count - now we will really count */
count = 0;
/* loop until end of the line */
p = line;
while ('\0' != *p) {
/* expect "$<number> = " */
if ('$' != *(p++)) {
fprintf(stderr, "Error: no dollar sign found in argument %d in line %lu\n", count, lineno);
free(*result);
*result = NULL;
return -1;
}
while (('0' <= *p) && ('9' >= *p)) {
++p;
}
if ((' ' != p[0]) || ('=' != p[1]) || (' ' != p[2])) {
fprintf(stderr, "Error: bad format in argument %d in line %lu\n", count, lineno);
free(*result);
*result = NULL;
return -1;
}
p += 3;
if ('\'' == *p) {
/* normal argument in single quotes */
(*result)[count] = p1 = ++p;
/* eliminate double single quotes and search for ending single quote */
while (*p1) {
if ('\0' == *p) {
fprintf(stderr, "Error: unexpected end of line in argument %d in line %lu\n", count, lineno);
free(*result);
*result = NULL;
return -1;
}
if ('\'' == *p) {
/* single quote encountered */
if ('\'' == p[1]) {
/* double single quote, skip one */
*(p1++) = '\'';
++p;
} else {
/* end of argument */
*p1 = '\0';
}
} else {
/* normal character */
*(p1++) = *p;
}
++p;
}
} else {
/* must me NULL argument */
if (('N' == p[0]) && ('U' == p[1]) && ('L' == p[2]) && ('L' == p[3])) {
(*result)[count] = NULL;
p += 4;
} else {
fprintf(stderr, "Error: expected NULL in argument %d in line %lu\n", count, lineno);
free(*result);
*result = NULL;
return -1;
}
}
/* skip ", " if present */
if ((',' == p[0]) && (' ' == p[1])) {
p += 2;
}
++count;
}
debug(2, "Parsed %d arguments\n", count);
debug(3, "Leaving parse_bind_args%s\n", "");
return count;
}
static void print_parse_statistics() {
int hours, minutes;
double seconds;
struct timeval delta;
fprintf(sf, "\nParse statistics\n");
fprintf(sf, "================\n\n");
fprintf(sf, "Log lines read: %lu\n", lineno);
fprintf(sf, "Total SQL statements processed: %lu\n", stat_simple + stat_param + stat_execnamed);
fprintf(sf, "Simple SQL statements processed: %lu\n", stat_simple);
if (stat_copy) {
fprintf(sf, "(includes %lu ignored copy statements)\n", stat_copy);
}
fprintf(sf, "Parametrized SQL statements processed: %lu\n", stat_param);
fprintf(sf, "Named prepared SQL statements executions processed: %lu\n", stat_execnamed);
if (stat_named) {
fprintf(sf, "Different named prepared SQL statements processed: %lu\n", stat_named);
fprintf(sf, "(average reuse count %.3f)\n", (double)(stat_execnamed - stat_named) / stat_named);
}
fprintf(sf, "Cancel requests processed: %lu\n", stat_cancel);
fprintf(sf, "Fast-path function calls ignored: %lu\n", stat_fastpath);
/* calculate lengh of the recorded workload */
timersub(&last_stmt_time, &first_stmt_time, &delta);
hours = delta.tv_sec / 3600;
delta.tv_sec -= hours * 3600;
minutes = delta.tv_sec / 60;
delta.tv_sec -= minutes * 60;
seconds = delta.tv_usec / 1000000.0 + delta.tv_sec;
fprintf(sf, "Duration of recorded workload:");
if (hours > 0) {
fprintf(sf, " %d hours", hours);
}
if (minutes > 0) {
fprintf(sf, " %d minutes", minutes);
}
fprintf(sf, " %.3f seconds\n", seconds);
}
int parse_provider_init(const char *in, int parse_csv, const char *begin, const char *end, const char *db_only, const char *usr_only) {
static struct tm tm; /* initialize with zeros */
int rc = 1;
debug(3, "Entering parse_provider_init%s\n", "");
if (NULL == in) {
infile = 0;
} else {
if (-1 == (infile = open(in, O_RDONLY))) {
perror("Error opening input file");
rc = 0;
}
}
csv = parse_csv;
start_time = begin;
end_time = end;
database_only = db_only;
username_only = usr_only;
/* initialize epoch with 2000-01-01 00:00:00 */
tm.tm_year = 2000 - 1900;
tm.tm_mon = 1 - 1;
tm.tm_mday = 1;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = 0; /* ignore daylight savings time */
epoch = mktime(&tm);
debug(3, "Leaving parse_provider_init%s\n", "");
return rc;
}
void parse_provider_finish() {
debug(3, "Entering parse_provider_finish%s\n", "");
if (0 != infile) {
if (close(infile)) {
perror("Error closing input file:");
}
}
if (sf) {
print_parse_statistics();
}
debug(3, "Leaving parse_provider_finish%s\n", "");
}
/* the replay_item is malloc'ed and must be freed with replay_free()
will return NULL if an error occurred */
replay_item * parse_provider() {
replay_item *r = NULL;
char *message = NULL, *detail = NULL, *statement = NULL, *namep = NULL, name[NAMELEN + 1], **args, user[NAMELEN + 1], database[NAMELEN + 1], quote_name[NAMELEN + 3];
log_type logtype;
uint64_t session_id;
int count, i;
/* possible stati: -1 = error, 0 = looking for log line, 1 = interesting line found
2 = using cached value, 3 = EOF */
int status = 0;
replay_type type = -1; /* -1 is an impossible value */
struct timeval time;
/* remember time from last parsed line */
static struct timeval oldtime = { 0, 0 };
struct connection *conn = NULL;
/* queue of up to two replay items */
static replay_item *queue[2] = { NULL, NULL };
static int first_stmt_time_set = 0;
debug(3, "Entering parse_provider%s\n", "");
if (queue[0]) {
/* if there is something in the queue, return it */
debug(2, "Queue is not empty, returning top element%s\n", "");
r = queue[0];
queue[0] = queue[1];
queue[1] = NULL;
status = 2;
}
/* read a log entry until we find an interesting one */
while (0 == status) {
switch ((*parse_log_entry[csv])(&time, user, database, &session_id, &logtype, &message, &detail)) {
case 0:
/* EOF encountered */
status = 3;
break;
case 1:
/* Do not process line that does not match any of the specified users and databases */
memset(quote_name, '\0', NAMELEN + 3);
if ('\0' != *database) {
quote_name[0] = '\\';
strcat(quote_name, database);
strcat(quote_name, "\\");
}
if ((NULL != database_only) && (NULL == strstr(database_only, quote_name))) {
debug(2, "Database \"%s\" does not match filter, skipped log entry\n", database);
free(message);
if (! csv && detail) {
free(detail);
}
break;
}
memset(quote_name, '\0', NAMELEN + 3);
if ('\0' != *user) {
quote_name[0] = '\\';
strcat(quote_name, user);
strcat(quote_name, "\\");
}
if ((NULL != username_only) && (NULL == strstr(username_only, quote_name))) {
debug(2, "User \"%s\" does not match filter, skipped log entry\n", user);
free(message);
if (! csv && detail) {
free(detail);
}
break;
}
/* check line prefix to determine type */
if ((log_log == logtype) && (! strncmp(message, "connection authorized: ", 23))) {
debug(2, "Connection 0x" UINT64_FORMAT " found\n", session_id);
type = pg_connect;
statement = message + 23;
status = 1;
} else if ((log_log == logtype) && (! strncmp(message, "disconnection: ", 15))) {
debug(2, "Disconnection found%s\n", "");
type = pg_disconnect;
statement = message + 15;
status = 1;
} else if ((log_log == logtype) && (! strncmp(message, "statement: ", 11))) {
debug(2, "Simple statement found%s\n", "");
type = pg_execute;
statement = message + 11;
status = 1;
} else if ((log_log == logtype) && (! strncmp(message, "execute ", 8))) {
debug(2, "Prepared statement execution found%s\n", "");
type = pg_exec_prepared;
namep = message + 8;
if ((statement = strchr(namep, ':'))) {
/* split in name and statement */
*(statement++) = '\0';
++statement;
/* check for unnamed statement, change name to empty string */
if (! strcmp(namep, "<unnamed>")) {
*namep = '\0';
}
status = 1;
} else {
fprintf(stderr, "Error: missing statement name in line %lu\n", lineno);
status = -1;
}
} else if ((log_error == logtype) && (! strncmp(message, "canceling statement due to user request", 39))) {
debug(2, "Cancel request found%s\n", "");
type = pg_cancel;
status = 1;
} else if ((log_log == logtype) && (! strncmp(message, "fastpath function call: ", 24))) {
free(message);
if (! csv && detail) {
free(detail);
}
fprintf(stderr, "Warning: fast-path function call ignored in line %lu\n", lineno);
/* count for statistics */
++stat_fastpath;
} else {
free(message);
if (! csv && detail) {
free(detail);
}
debug(2, "Skipped log entry%s\n", "");
}
break;
default:
/* something went wrong */
status = -1;
}
}
/* if everything is ok so far, search for a connection in our list
add one if there is none so far, store the "connect" replay_item in r */
if (1 == status) {
/* we need a connection in any case */
if ((conn = add_connection(&r, &time, user, database, session_id))) {
if (r && (pg_connect != type)) {
debug(2, "Automatically generated connection%s\n", "");
}
if ((!r) && (pg_connect == type)) {
/* if the connection already existed and we read
a connect line, that is an error */
fprintf(stderr, "Error: duplicate session ID 0x" UINT64_FORMAT "\n", session_id);
status = -1;
}
} else {
/* error occurred */
status = -1;
}
}
/* if everything is ok so far, process line according to type
result will be stored in queue[0] and queue[1] */
if (1 == status) {
switch (type) {
case pg_connect:
/* we are status! */
break;
case pg_disconnect:
if (NULL == (queue[0] = replay_create_disconnect(&time, session_id))) {
status = -1;
}
/* remove connection from hash structure */
if (! remove_connection(session_id)) {
/* can't happen */
fprintf(stderr, "Error: cannot remove connection " UINT64_FORMAT " from hash table\n", session_id);
status = -1;
}
break;
case pg_execute:
if (filter_bad_statements(statement, conn)) {
if (NULL == (queue[0] = replay_create_execute(&time, session_id, statement))) {
status = -1;
}
} else {
status = -1;
}
break;
case pg_exec_prepared:
/* we don't need to filter, since it can only be a single statement
and neither COPY nor SET can be prepared statements */
/* make a persistent copy of the statement name */
strcpy(name, namep);
/* see if this is a new statement */
switch (add_pstmt(conn, name)) {
case 1: /* new */
if (NULL == (queue[0] = replay_create_prepare(&time, session_id, name, statement))) {
status = -1;
}
/* the detail message is read later */
break;
case 0: /* statement already exists */
/* the detail message is read later */
break;
default: /* error */
status = -1;
}
break;
case pg_cancel:
if (NULL == (queue[0] = replay_create_cancel(&time, session_id))) {
status = -1;
}
/* count for statistics */
++stat_cancel;
break;
default:
/* can't happen */
fprintf(stderr, "Error: impossible type parsing line %lu\n", lineno);
status = -1;
}
}
/* read and process the DETAIL message for a prepared statement */
if ((1 == status) && (pg_exec_prepared == type)) {
if (! detail) {
/* no DETAIL message --> statement has no parameters */
debug(2, "Prepared statement \"%s\" has no bind arguments\n", name);
if (NULL == (queue[1] = replay_create_exec_prepared(&time, session_id, name, 0, NULL))) {
status = -1;
}
} else if (strncmp(detail, "parameters: ", 12)) {
fprintf(stderr, "Error: no parameters for prepared statement at line %lu\n", lineno);
status = -1;
} else {
debug(2, "Reading bind arguments for prepared statement \"%s\"\n", name);
statement = detail + 12;
if (-1 == (count = parse_bind_args(&args, statement))) {
status = -1;
} else {
if (NULL == (queue[1] = replay_create_exec_prepared(&time, session_id, name, count, args))) {
status = -1;
}
free(args);
}
}
}
if (! csv && detail) {
free(detail);
}
if (message) {
free(message);
}
/* if EOF, close all connections that are still open */
if (3 == status) {
/* search for entry in connection hash table */
for (i=0; i<256; ++i) {
if (open_conn[i]) {
/* entry found, create disconnect item */
debug(2, "End-of-file encountered, creating disconnect item for session 0x" UINT64_FORMAT "\n", open_conn[i]->session_id);
if (NULL == (r = replay_create_disconnect(&oldtime, open_conn[i]->session_id))) {
status = -1;
}
remove_connection(open_conn[i]->session_id);
break;
}
}
if ((! r) && (3 == status)) {
debug(2, "End-of-file encountered, signal end%s\n", "");
r = end_item;
}
}
if (1 == status) {
/* condense queue */
if (! queue[0]) {
queue[0] = queue[1];
queue[1] = NULL;
}
if (! r) {
r = queue[0];
queue[0] = queue[1];
queue[1] = NULL;
}
if (! queue[0]) {
queue[0] = queue[1];
queue[1] = NULL;
}
/* remember time */
oldtime.tv_sec = replay_get_time(r)->tv_sec;
oldtime.tv_usec = replay_get_time(r)->tv_usec;
last_stmt_time.tv_sec = replay_get_time(r)->tv_sec;
last_stmt_time.tv_usec = replay_get_time(r)->tv_usec;
if (! first_stmt_time_set) {
first_stmt_time.tv_sec = replay_get_time(r)->tv_sec;
first_stmt_time.tv_usec = replay_get_time(r)->tv_usec;
first_stmt_time_set = 1;
}
}
if (-1 == status) {
/* free items in queue */
if (r) {
replay_free(r);
r = NULL;
}
if (queue[0]) {
replay_free(queue[0]);
queue[0] = NULL;
}
if (queue[1]) {
replay_free(queue[1]);
queue[1] = NULL;
}
}
if (r && (1 <= debug_level) && (end_item != r)) {
replay_print_debug(r);
}
debug(3, "Leaving parse_provider%s\n", "");
return r;
}
|