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
|
/*
* pg_filedump.c - PostgreSQL file dump utility for dumping and
* formatting heap(data), index and control files.
* Version 8.1.1 for PostgreSQL 8.1
*
* Copyright (c) 2002, 2003, 2005 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Patrick Macdonald <patrickm@redhat.com>
*
* Component of: PostgreSQL - Red Hat Edition - Utilities / Tools
*
*/
#include "pg_filedump.h"
// Global variables for ease of use mostly
static FILE *fp = NULL; // File to dump or format
static char *fileName = NULL; // File name for display
static char *buffer = NULL; // Cache for current block
static unsigned int blockSize = 0; // Current block size
static unsigned int currentBlock = 0; // Current block in file
static unsigned int pageOffset = 0; // Offset of current block
static unsigned int bytesToFormat = 0; // Number of bytes to format
static unsigned int blockVersion = 0; // Block version number
// Function Prototypes
static void DisplayOptions (unsigned int validOptions);
static unsigned int ConsumeOptions (int numOptions, char **options);
static int GetOptionValue (char *optionString);
static void FormatBlock ();
static unsigned int GetBlockSize ();
static unsigned int GetSpecialSectionType (Page page);
static void CreateDumpFileHeader (int numOptions, char **options);
static int FormatHeader (Page page);
static void FormatItemBlock (Page page);
static void FormatItem (unsigned int numBytes, unsigned int startIndex,
unsigned int formatAs);
static void FormatSpecial ();
static void FormatControl ();
static void FormatBinary (unsigned int numBytes, unsigned int startIndex);
static void DumpBinaryBlock ();
static void DumpFileContents ();
// Send properly formed usage information to the user.
static void
DisplayOptions (unsigned int validOptions)
{
if (validOptions == OPT_RC_COPYRIGHT)
printf
("\nVersion 8.1.1 (PostgreSQL 8.1) Copyright (c) 2002, 2003, 2005 Red Hat, Inc.\n");
printf
("\nUsage: pg_filedump [-abcdfhixy] [-R startblock [endblock]] [-S blocksize] file\n\n"
"Display formatted contents of a PostgreSQL heap/index/control file\n"
" Defaults are: relative addressing, range of the entire file, block\n"
" size as listed on block 0 in the file\n\n"
"The following options are valid for heap and index files:\n"
" -a Display absolute addresses when formatting (Block header\n"
" information is always block relative)\n"
" -b Display binary block images within a range (Option will turn\n"
" off all formatting options)\n"
" -d Display formatted block content dump (Option will turn off\n"
" all other formatting options)\n"
" -f Display formatted block content dump along with interpretation\n"
" -h Display this information\n"
" -i Display interpreted item details\n"
" -R Display specific block ranges within the file (Blocks are\n"
" indexed from 0)\n" " [startblock]: block to start at\n"
" [endblock]: block to end at\n"
" A startblock without an endblock will format the single block\n"
" -S Force block size to [blocksize]\n"
" -x Force interpreted formatting of block items as index items\n"
" -y Force interpreted formatting of block items as heap items\n\n"
"The following options are valid for control files:\n"
" -c Interpret the file listed as a control file\n"
" -f Display formatted content dump along with interpretation\n"
" -S Force block size to [blocksize]\n"
"\nReport bugs to <rhdb@sources.redhat.com>\n");
}
// Iterate through the provided options and set the option flags.
// An error will result in a positive rc and will force a display
// of the usage information. This routine returns enum
// optionReturnCode values.
static unsigned int
ConsumeOptions (int numOptions, char **options)
{
unsigned int rc = OPT_RC_VALID;
unsigned int x;
unsigned int optionStringLength;
char *optionString;
char duplicateSwitch = 0x00;
for (x = 1; x < numOptions; x++)
{
optionString = options[x];
optionStringLength = strlen (optionString);
// Range is a special case where we have to consume the next 1 or 2
// parameters to mark the range start and end
if ((optionStringLength == 2) && (strcmp (optionString, "-R") == 0))
{
int range = 0;
SET_OPTION (blockOptions, BLOCK_RANGE, 'R');
// Only accept the range option once
if (rc == OPT_RC_DUPLICATE)
break;
// Make sure there are options after the range identifier
if (x >= (numOptions - 2))
{
rc = OPT_RC_INVALID;
printf ("Error: Missing range start identifier.\n");
break;
}
// Mark that we have the range and advance the option to what should
// be the range start. Check the value of the next parameter
optionString = options[++x];
if ((range = GetOptionValue (optionString)) < 0)
{
rc = OPT_RC_INVALID;
printf ("Error: Invalid range start identifier <%s>.\n",
optionString);
break;
}
// The default is to dump only one block
blockStart = blockEnd = (unsigned int) range;
// We have our range start marker, check if there is an end
// marker on the option line. Assume that the last option
// is the file we are dumping, so check if there are options
// range start marker and the file
if (x <= (numOptions - 3))
{
if ((range = GetOptionValue (options[x + 1])) >= 0)
{
// End range must be => start range
if (blockStart <= range)
{
blockEnd = (unsigned int) range;
x++;
}
else
{
rc = OPT_RC_INVALID;
printf ("Error: Requested block range start <%d> is "
"greater than end <%d>.\n", blockStart, range);
break;
}
}
}
}
// Check for the special case where the user forces a block size
// instead of having the tool determine it. This is useful if
// the header of block 0 is corrupt and gives a garbage block size
else if ((optionStringLength == 2)
&& (strcmp (optionString, "-S") == 0))
{
int localBlockSize;
SET_OPTION (blockOptions, BLOCK_FORCED, 'S');
// Only accept the forced size option once
if (rc == OPT_RC_DUPLICATE)
break;
// The token immediately following -S is the block size
if (x >= (numOptions - 2))
{
rc = OPT_RC_INVALID;
printf ("Error: Missing block size identifier.\n");
break;
}
// Next option encountered must be forced block size
optionString = options[++x];
if ((localBlockSize = GetOptionValue (optionString)) > 0)
blockSize = (unsigned int) localBlockSize;
else
{
rc = OPT_RC_INVALID;
printf ("Error: Invalid block size requested <%s>.\n",
optionString);
break;
}
}
// The last option MUST be the file name
else if (x == (numOptions - 1))
{
// Check to see if this looks like an option string before opening
if (optionString[0] != '-')
{
fp = fopen (optionString, "rb");
if (fp)
fileName = options[x];
else
{
rc = OPT_RC_FILE;
printf ("Error: Could not open file <%s>.\n", optionString);
break;
}
}
else
{
// Could be the case where the help flag is used without a
// filename. Otherwise, the last option isn't a file
if (strcmp (optionString, "-h") == 0)
rc = OPT_RC_COPYRIGHT;
else
{
rc = OPT_RC_FILE;
printf ("Error: Missing file name to dump.\n");
}
break;
}
}
else
{
unsigned int y;
// Option strings must start with '-' and contain switches
if (optionString[0] != '-')
{
rc = OPT_RC_INVALID;
printf ("Error: Invalid option string <%s>.\n", optionString);
break;
}
// Iterate through the singular option string, throw out
// garbage, duplicates and set flags to be used in formatting
for (y = 1; y < optionStringLength; y++)
{
switch (optionString[y])
{
// Use absolute addressing
case 'a':
SET_OPTION (blockOptions, BLOCK_ABSOLUTE, 'a');
break;
// Dump the binary contents of the page
case 'b':
SET_OPTION (blockOptions, BLOCK_BINARY, 'b');
break;
// Dump the listed file as a control file
case 'c':
SET_OPTION (controlOptions, CONTROL_DUMP, 'c');
break;
// Do not interpret the data. Format to hex and ascii.
case 'd':
SET_OPTION (blockOptions, BLOCK_NO_INTR, 'd');
break;
// Format the contents of the block with interpretation
// of the headers
case 'f':
SET_OPTION (blockOptions, BLOCK_FORMAT, 'f');
break;
// Display the usage screen
case 'h':
rc = OPT_RC_COPYRIGHT;
break;
// Format the items in detail
case 'i':
SET_OPTION (itemOptions, ITEM_DETAIL, 'i');
break;
// Interpret items as index values
case 'x':
SET_OPTION (itemOptions, ITEM_INDEX, 'x');
if (itemOptions & ITEM_HEAP)
{
rc = OPT_RC_INVALID;
printf ("Error: Options <y> and <x> are "
"mutually exclusive.\n");
}
break;
// Interpret items as heap values
case 'y':
SET_OPTION (itemOptions, ITEM_HEAP, 'y');
if (itemOptions & ITEM_INDEX)
{
rc = OPT_RC_INVALID;
printf ("Error: Options <x> and <y> are "
"mutually exclusive.\n");
}
break;
default:
rc = OPT_RC_INVALID;
printf ("Error: Unknown option <%c>.\n", optionString[y]);
break;
}
if (rc)
break;
}
}
}
if (rc == OPT_RC_DUPLICATE)
printf ("Error: Duplicate option listed <%c>.\n", duplicateSwitch);
// If the user requested a control file dump, a pure binary
// block dump or a non-interpreted formatted dump, mask off
// all other block level options (with a few exceptions)
if (rc == OPT_RC_VALID)
{
// The user has requested a control file dump, only -f and
// -S are valid... turn off all other formatting
if (controlOptions & CONTROL_DUMP)
{
if ((blockOptions & ~(BLOCK_FORMAT | BLOCK_FORCED))
|| (itemOptions))
{
rc = OPT_RC_INVALID;
printf ("Error: Invalid options used for Control File dump.\n"
" Only options <Sf> may be used with <c>.\n");
}
else
{
controlOptions |=
(blockOptions & (BLOCK_FORMAT | BLOCK_FORCED));
blockOptions = itemOptions = 0;
}
}
// The user has request a binary block dump... only -R and
// -f are honoured
else if (blockOptions & BLOCK_BINARY)
{
blockOptions &= (BLOCK_BINARY | BLOCK_RANGE | BLOCK_FORCED);
itemOptions = 0;
}
// The user has requested a non-intepreted dump... only -a,
// -R and -f are honoured
else if (blockOptions & BLOCK_NO_INTR)
{
blockOptions &=
(BLOCK_NO_INTR | BLOCK_ABSOLUTE | BLOCK_RANGE | BLOCK_FORCED);
itemOptions = 0;
}
}
return (rc);
}
// Given the index into the parameter list, convert and return the
// current string to a number if possible
static int
GetOptionValue (char *optionString)
{
unsigned int x;
int value = -1;
int optionStringLength = strlen (optionString);
// Verify the next option looks like a number
for (x = 0; x < optionStringLength; x++)
if (!isdigit ((int) optionString[x]))
break;
// Convert the string to a number if it looks good
if (x == optionStringLength)
value = atoi (optionString);
return (value);
}
// Read the page header off of block 0 to determine the block size
// used in this file. Can be overridden using the -S option. The
// returned value is the block size of block 0 on disk
static unsigned int
GetBlockSize ()
{
unsigned int pageHeaderSize = sizeof (PageHeaderData);
unsigned int localSize = 0;
int bytesRead = 0;
char localCache[pageHeaderSize];
// Read the first header off of block 0 to determine the block size
bytesRead = fread (&localCache, 1, pageHeaderSize, fp);
rewind (fp);
if (bytesRead == pageHeaderSize)
localSize = (unsigned int) PageGetPageSize (&localCache);
else
printf ("Error: Unable to read full page header from block 0.\n"
" ===> Read %u bytes", bytesRead);
return (localSize);
}
// Determine the contents of the special section on the block and
// return this enum value
static unsigned int
GetSpecialSectionType (Page page)
{
unsigned int rc = 0;
unsigned int specialOffset;
unsigned int specialSize;
unsigned int specialValue;
PageHeader pageHeader = (PageHeader) page;
// If this is not a partial header, check the validity of the
// special section offset and contents
if (bytesToFormat > sizeof (PageHeaderData))
{
specialOffset = (unsigned int) pageHeader->pd_special;
// Check that the special offset can remain on the block or
// the partial block
if ((specialOffset == 0) ||
(specialOffset > blockSize) || (specialOffset > bytesToFormat))
rc = SPEC_SECT_ERROR_BOUNDARY;
else
{
specialSize = blockSize - specialOffset;
// If there is a special section, use its size to guess its
// contents
if (specialSize == 0)
rc = SPEC_SECT_NONE;
else if (specialSize == MAXALIGN (sizeof (uint32)))
{
if (bytesToFormat == blockSize)
{
specialValue = *((int *) (buffer + specialOffset));
if (specialValue == SEQUENCE_MAGIC)
rc = SPEC_SECT_SEQUENCE;
else
rc = SPEC_SECT_INDEX_RTREE;
}
else
rc = SPEC_SECT_ERROR_UNKNOWN;
}
else if (specialSize == MAXALIGN (sizeof (HashPageOpaqueData)))
{
HashPageOpaque hpo = (HashPageOpaque) (buffer + specialOffset);
if (hpo->hasho_filler == HASHO_FILL)
rc = SPEC_SECT_INDEX_HASH;
else
rc = SPEC_SECT_INDEX_BTREE;
}
else
rc = SPEC_SECT_ERROR_UNKNOWN;
}
}
else
rc = SPEC_SECT_ERROR_UNKNOWN;
return (rc);
}
// Display a header for the dump so we know the file name, the options
// used and the time the dump was taken
static void
CreateDumpFileHeader (int numOptions, char **options)
{
unsigned int x;
char optionBuffer[52] = "\0";
time_t rightNow = time (NULL);
// Iterate through the options and cache them.
// The maximum we can display is 50 option characters + spaces.
for (x = 1; x < (numOptions - 1); x++)
{
if ((strlen (optionBuffer) + strlen (options[x])) > 50)
break;
strcat (optionBuffer, options[x]);
strcat (optionBuffer, " ");
}
printf
("\n*******************************************************************\n"
"* PostgreSQL File/Block Formatted Dump Utility - Version 8.1.1\n*\n"
"* File: %s\n"
"* Options used: %s\n*\n"
"* Dump created on: %s"
"*******************************************************************\n",
fileName, (strlen (optionBuffer)) ? optionBuffer : "None",
ctime (&rightNow));
}
// Dump out a formatted block header for the requested block
static int
FormatHeader (Page page)
{
int rc = 0;
unsigned int headerBytes;
PageHeader pageHeader = (PageHeader) page;
printf ("<Header> -----\n");
// Only attempt to format the header if the entire header (minus the item
// array) is available
if (bytesToFormat < sizeof (PageHeaderData))
{
headerBytes = bytesToFormat;
rc = EOF_ENCOUNTERED;
}
else
{
XLogRecPtr pageLSN = PageGetLSN (page);
int maxOffset = PageGetMaxOffsetNumber (page);
headerBytes = sizeof (PageHeaderData);
blockVersion = (unsigned int) PageGetPageLayoutVersion (page);
// The full header exists but we have to check that the item array
// is available or how far we can index into it
if (maxOffset > 0)
{
unsigned int itemLength = maxOffset * sizeof (ItemIdData);
if (bytesToFormat < (headerBytes + itemLength))
{
headerBytes = bytesToFormat;
rc = EOF_ENCOUNTERED;
}
else
headerBytes += itemLength;
}
// Interpret the content of the header
printf
(" Block Offset: 0x%08x Offsets: Lower %4u (0x%04hx)\n"
" Block: Size %4d Version %4u Upper %4u (0x%04hx)\n"
" LSN: logid %6d recoff 0x%08x Special %4u (0x%04hx)\n"
" Items: %4d Free Space: %4u\n"
" Length (including item array): %u\n\n",
pageOffset, pageHeader->pd_lower, pageHeader->pd_lower,
PageGetPageSize (page), blockVersion, pageHeader->pd_upper,
pageHeader->pd_upper,
pageLSN.xlogid, pageLSN.xrecoff, pageHeader->pd_special,
pageHeader->pd_special, maxOffset,
pageHeader->pd_upper - pageHeader->pd_lower, headerBytes);
// Check for the case where this is a BTree page. The meta data stored
// in the header is not recorded in the item array.
if ((PageGetSpecialSize (page) ==
(MAXALIGN (sizeof (BTPageOpaqueData))))
&& (bytesToFormat == blockSize))
{
// As of 7.4, BTree and Hash pages have the same size special
// section. Only look at the meta data if it's a BTree...
BTPageOpaque btpo =
(BTPageOpaque) ((char *) page + pageHeader->pd_special);
if ((((HashPageOpaque) (btpo))->hasho_filler != HASHO_FILL) &&
(btpo->btpo_flags & BTP_META))
{
BTMetaPageData *btpMeta = BTPageGetMeta (buffer);
printf (" BTree Meta Data: Magic (0x%08x) Version (%u)\n"
" Root: Block (%u) Level (%u)\n"
" FastRoot: Block (%u) Level (%u)\n\n",
btpMeta->btm_magic, btpMeta->btm_version,
btpMeta->btm_root, btpMeta->btm_level,
btpMeta->btm_fastroot, btpMeta->btm_fastlevel);
headerBytes += (sizeof (BTMetaPageData) - sizeof (ItemIdData));
}
}
// Eye the contents of the header and alert the user to possible
// problems.
if ((maxOffset < 0) ||
(maxOffset > blockSize) ||
(blockVersion != PG_PAGE_LAYOUT_VERSION) || /* only one we support */
(pageHeader->pd_upper > blockSize) ||
(pageHeader->pd_upper > pageHeader->pd_special) ||
(pageHeader->pd_lower <
(sizeof (PageHeaderData) - sizeof (ItemIdData)))
|| (pageHeader->pd_lower > blockSize)
|| (pageHeader->pd_upper < pageHeader->pd_lower)
|| (pageHeader->pd_special > blockSize))
printf (" Error: Invalid header information.\n\n");
}
// If we have reached the end of file while interpreting the header, let
// the user know about it
if (rc == EOF_ENCOUNTERED)
printf
(" Error: End of block encountered within the header."
" Bytes read: %4u.\n\n", bytesToFormat);
// A request to dump the formatted binary of the block (header,
// items and special section). It's best to dump even on an error
// so the user can see the raw image.
if (blockOptions & BLOCK_FORMAT)
FormatBinary (headerBytes, 0);
return (rc);
}
// Dump out formatted items that reside on this block
static void
FormatItemBlock (Page page)
{
unsigned int x;
unsigned int itemSize;
unsigned int itemOffset;
unsigned int itemFlags;
ItemId itemId;
int maxOffset = PageGetMaxOffsetNumber (page);
printf ("<Data> ------ \n");
// Loop through the items on the block. Check if the block is
// empty and has a sensible item array listed before running
// through each item
if (maxOffset == 0)
printf (" Empty block - no items listed \n\n");
else if ((maxOffset < 0) || (maxOffset > blockSize))
printf (" Error: Item index corrupt on block. Offset: <%d>.\n\n",
maxOffset);
else
{
int formatAs;
char textFlags[8];
// First, honour requests to format items a special way, then
// use the special section to determine the format style
if (itemOptions & ITEM_INDEX)
formatAs = ITEM_INDEX;
else if (itemOptions & ITEM_HEAP)
formatAs = ITEM_HEAP;
else if (specialType != SPEC_SECT_NONE)
formatAs = ITEM_INDEX;
else
formatAs = ITEM_HEAP;
for (x = 1; x < (maxOffset + 1); x++)
{
itemId = PageGetItemId (page, x);
itemFlags = (unsigned int) ItemIdGetFlags (itemId);
itemSize = (unsigned int) ItemIdGetLength (itemId);
itemOffset = (unsigned int) ItemIdGetOffset (itemId);
if (itemFlags)
strcpy (textFlags, (LP_USED & itemFlags) ? "USED" : "DELETE");
else
sprintf (textFlags, "0x%02x", itemFlags);
printf (" Item %3u -- Length: %4u Offset: %4u (0x%04x)"
" Flags: %s\n", x, itemSize, itemOffset, itemOffset,
textFlags);
// Make sure the item can physically fit on this block before
// formatting
if ((itemOffset + itemSize > blockSize) ||
(itemOffset + itemSize > bytesToFormat))
printf (" Error: Item contents extend beyond block.\n"
" BlockSize<%d> Bytes Read<%d> Item Start<%d>.\n",
blockSize, bytesToFormat, itemOffset + itemSize);
else
{
// If the user requests that the items be interpreted as
// heap or index items...
if (itemOptions & ITEM_DETAIL)
FormatItem (itemSize, itemOffset, formatAs);
// Dump the items contents in hex and ascii
if (blockOptions & BLOCK_FORMAT)
FormatBinary (itemSize, itemOffset);
if (x == maxOffset)
printf ("\n");
}
}
}
}
// Interpret the contents of the item based on whether it has a special
// section and/or the user has hinted
static void
FormatItem (unsigned int numBytes, unsigned int startIndex,
unsigned int formatAs)
{
// It is an index item, so dump the index header
if (formatAs == ITEM_INDEX)
{
if (numBytes < SizeOfIptrData)
{
if (numBytes)
printf (" Error: This item does not look like an index item.\n");
}
else
{
IndexTuple itup = (IndexTuple) (&(buffer[startIndex]));
printf (" Block Id: %u linp Index: %u Size: %d\n"
" Has Nulls: %u Has Varwidths: %u\n\n",
((uint32) ((itup->t_tid.ip_blkid.bi_hi << 16) |
(uint16) itup->t_tid.ip_blkid.bi_lo)),
itup->t_tid.ip_posid, IndexTupleSize (itup),
IndexTupleHasNulls (itup), IndexTupleHasVarwidths (itup));
if (numBytes != IndexTupleSize (itup))
printf (" Error: Item size difference. Given <%u>, "
"Internal <%d>.\n", numBytes, IndexTupleSize (itup));
}
}
else
{
// It is a heap item, so dump the heap header
int alignedSize = MAXALIGN (sizeof (HeapTupleHeaderData));
if (numBytes < alignedSize)
{
if (numBytes)
printf (" Error: This item does not look like a heap item.\n");
}
else
{
char flagString[256];
unsigned int x;
unsigned int bitmapLength = 0;
unsigned int oidLength = 0;
unsigned int computedLength;
unsigned int infoMask;
int localNatts;
unsigned int localHoff;
bits8 *localBits;
unsigned int localBitOffset;
HeapTupleHeader htup = (HeapTupleHeader) (&buffer[startIndex]);
infoMask = htup->t_infomask;
localBits = &(htup->t_bits[0]);
localNatts = htup->t_natts;
localHoff = htup->t_hoff;
localBitOffset = offsetof (HeapTupleHeaderData, t_bits);
printf (" XMIN: %u CMIN: %u XMAX: %u CMAX|XVAC: %u",
HeapTupleHeaderGetXmin(htup),
HeapTupleHeaderGetCmin(htup),
HeapTupleHeaderGetXmax(htup),
HeapTupleHeaderGetCmax(htup));
if (infoMask & HEAP_HASOID)
printf (" OID: %u",
HeapTupleHeaderGetOid(htup));
printf ("\n"
" Block Id: %u linp Index: %u Attributes: %d Size: %d\n",
((uint32)
((htup->t_ctid.ip_blkid.bi_hi << 16) | (uint16) htup->
t_ctid.ip_blkid.bi_lo)), htup->t_ctid.ip_posid,
htup->t_natts, htup->t_hoff);
// Place readable versions of the tuple info mask into a buffer.
// Assume that the string can not expand beyond 256.
flagString[0] = '\0';
if (infoMask & HEAP_HASNULL)
strcat (flagString, "HASNULL|");
if (infoMask & HEAP_HASVARWIDTH)
strcat (flagString, "HASVARWIDTH|");
if (infoMask & HEAP_HASEXTERNAL)
strcat (flagString, "HASEXTERNAL|");
if (infoMask & HEAP_HASCOMPRESSED)
strcat (flagString, "HASCOMPRESSED|");
if (infoMask & HEAP_HASOID)
strcat (flagString, "HASOID|");
if (infoMask & HEAP_XMAX_EXCL_LOCK)
strcat (flagString, "XMAX_EXCL_LOCK|");
if (infoMask & HEAP_XMAX_SHARED_LOCK)
strcat (flagString, "XMAX_SHARED_LOCK|");
if (infoMask & HEAP_XMIN_COMMITTED)
strcat (flagString, "XMIN_COMMITTED|");
if (infoMask & HEAP_XMIN_INVALID)
strcat (flagString, "XMIN_INVALID|");
if (infoMask & HEAP_XMAX_COMMITTED)
strcat (flagString, "XMAX_COMMITTED|");
if (infoMask & HEAP_XMAX_INVALID)
strcat (flagString, "XMAX_INVALID|");
if (infoMask & HEAP_XMAX_IS_MULTI)
strcat (flagString, "XMAX_IS_MULTI|");
if (infoMask & HEAP_UPDATED)
strcat (flagString, "UPDATED|");
if (infoMask & HEAP_MOVED_OFF)
strcat (flagString, "MOVED_OFF|");
if (infoMask & HEAP_MOVED_IN)
strcat (flagString, "MOVED_IN|");
if (strlen (flagString))
flagString[strlen (flagString) - 1] = '\0';
printf (" infomask: 0x%04x (%s) \n", infoMask, flagString);
// As t_bits is a variable length array, determine the length of
// the header proper
if (infoMask & HEAP_HASNULL)
bitmapLength = BITMAPLEN (localNatts);
else
bitmapLength = 0;
if (infoMask & HEAP_HASOID)
oidLength += sizeof (Oid);
computedLength =
MAXALIGN (localBitOffset + bitmapLength + oidLength);
// Inform the user of a header size mismatch or dump the t_bits array
if (computedLength != localHoff)
printf
(" Error: Computed header length not equal to header size.\n"
" Computed <%u> Header: <%d>\n", computedLength,
localHoff);
else if ((infoMask & HEAP_HASNULL) && bitmapLength)
{
printf (" t_bits: ");
for (x = 0; x < bitmapLength; x++)
{
printf ("[%u]: 0x%02x ", x, localBits[x]);
if (((x & 0x03) == 0x03) && (x < bitmapLength - 1))
printf ("\n ");
}
printf ("\n");
}
printf ("\n");
}
}
}
// On blocks that have special sections, we have to interpret the
// contents based on size of the special section (since there is
// no other way)
static void
FormatSpecial ()
{
PageHeader pageHeader = (PageHeader) buffer;
char flagString[50] = "\0";
unsigned int specialOffset = pageHeader->pd_special;
unsigned int specialSize =
(blockSize >= specialOffset) ? (blockSize - specialOffset) : 0;
printf ("<Special Section> -----\n");
switch (specialType)
{
case SPEC_SECT_ERROR_UNKNOWN:
case SPEC_SECT_ERROR_BOUNDARY:
printf (" Error: Invalid special section encountered.\n");
break;
case SPEC_SECT_SEQUENCE:
printf (" Sequence: 0x%08x\n", SEQUENCE_MAGIC);
break;
// GIST/RTree index section
case SPEC_SECT_INDEX_RTREE:
{
unsigned int specialValue =
*((unsigned int *) (buffer + specialOffset));
printf (" RTree/GIST Index Section:\n" " Flags: 0x%08x (%s)\n\n",
specialValue,
(specialValue & F_LEAF) ? "LEAF" : "UNKNOWN FLAGS");
}
break;
// Btree index section
case SPEC_SECT_INDEX_BTREE:
{
BTPageOpaque btreeSection = (BTPageOpaque) (buffer + specialOffset);
if (btreeSection->btpo_flags & BTP_LEAF)
strcat (flagString, "LEAF|");
if (btreeSection->btpo_flags & BTP_ROOT)
strcat (flagString, "ROOT|");
if (btreeSection->btpo_flags & BTP_DELETED)
strcat (flagString, "DELETED|");
if (btreeSection->btpo_flags & BTP_META)
strcat (flagString, "META|");
if (btreeSection->btpo_flags & BTP_HALF_DEAD)
strcat (flagString, "HALFDEAD|");
if (strlen (flagString))
flagString[strlen (flagString) - 1] = '\0';
printf (" BTree Index Section:\n"
" Flags: 0x%04x (%s)\n"
" Blocks: Previous (%d) Next (%d) %s (%d)\n\n",
btreeSection->btpo_flags, flagString,
btreeSection->btpo_prev, btreeSection->btpo_next,
(btreeSection->
btpo_flags & BTP_DELETED) ? "Next XID" : "Level",
btreeSection->btpo.level);
}
break;
// Hash index section
case SPEC_SECT_INDEX_HASH:
{
HashPageOpaque hashSection =
(HashPageOpaque) (buffer + specialOffset);
if (hashSection->hasho_flag & LH_UNUSED_PAGE)
strcat (flagString, "UNUSED|");
if (hashSection->hasho_flag & LH_OVERFLOW_PAGE)
strcat (flagString, "OVERFLOW|");
if (hashSection->hasho_flag & LH_BUCKET_PAGE)
strcat (flagString, "BUCKET|");
if (hashSection->hasho_flag & LH_BITMAP_PAGE)
strcat (flagString, "BITMAP|");
if (hashSection->hasho_flag & LH_META_PAGE)
strcat (flagString, "META|");
if (strlen (flagString))
flagString[strlen (flagString) - 1] = '\0';
printf (" Hash Index Section:\n"
" Flags: 0x%04x (%s)\n"
" Bucket Number: 0x%04x\n"
" Blocks: Previous (%d) Next (%d)\n\n",
hashSection->hasho_flag, flagString,
hashSection->hasho_bucket,
hashSection->hasho_prevblkno, hashSection->hasho_nextblkno);
}
break;
// No idea what type of special section this is
default:
printf (" Unknown special section type. Type: <%u>.\n", specialType);
break;
}
// Dump the formatted contents of the special section
if (blockOptions & BLOCK_FORMAT)
{
if (specialType == SPEC_SECT_ERROR_BOUNDARY)
printf (" Error: Special section points off page."
" Unable to dump contents.\n");
else
FormatBinary (specialSize, specialOffset);
}
}
// For each block, dump out formatted header and content information
static void
FormatBlock ()
{
Page page = (Page) buffer;
pageOffset = blockSize * currentBlock;
specialType = GetSpecialSectionType (page);
printf ("\nBlock %4u **%s***************************************\n",
currentBlock,
(bytesToFormat ==
blockSize) ? "***************" : " PARTIAL BLOCK ");
// Either dump out the entire block in hex+acsii fashion or
// interpret the data based on block structure
if (blockOptions & BLOCK_NO_INTR)
FormatBinary (bytesToFormat, 0);
else
{
int rc;
// Every block contains a header, items and possibly a special
// section. Beware of partial block reads though
rc = FormatHeader (page);
// If we didn't encounter a partial read in the header, carry on...
if (rc != EOF_ENCOUNTERED)
{
FormatItemBlock (page);
if (specialType != SPEC_SECT_NONE)
FormatSpecial ();
}
}
}
// Dump out the content of the PG control file
static void
FormatControl ()
{
unsigned int localPgVersion = 0;
unsigned int controlFileSize = 0;
printf
("\n<pg_control Contents> *********************************************\n\n");
// Check the version
if (bytesToFormat >= offsetof (ControlFileData, catalog_version_no))
localPgVersion = ((ControlFileData *) buffer)->pg_control_version;
if (localPgVersion >= 72)
controlFileSize = sizeof (ControlFileData);
else
{
printf ("pg_filedump: PostgreSQL %u not supported.\n", localPgVersion);
return;
}
// Interpret the control file if it's all there
if (bytesToFormat >= controlFileSize)
{
ControlFileData *controlData = (ControlFileData *) buffer;
CheckPoint *checkPoint = &(controlData->checkPointCopy);
pg_crc32 crcLocal;
char *dbState;
// Compute a local copy of the CRC to verify the one on disk
INIT_CRC32 (crcLocal);
COMP_CRC32 (crcLocal, buffer, offsetof(ControlFileData, crc));
FIN_CRC32 (crcLocal);
// Grab a readable version of the database state
switch (controlData->state)
{
case DB_STARTUP:
dbState = "STARTUP";
break;
case DB_SHUTDOWNED:
dbState = "SHUTDOWNED";
break;
case DB_SHUTDOWNING:
dbState = "SHUTDOWNING";
break;
case DB_IN_RECOVERY:
dbState = "IN RECOVERY";
break;
case DB_IN_PRODUCTION:
dbState = "IN PRODUCTION";
break;
default:
dbState = "UNKNOWN";
break;
}
printf (" CRC: %s\n"
" pg_control Version: %u%s\n"
" Catalog Version: %u\n"
" System Identifier: " UINT64_FORMAT "\n"
" State: %s\n"
" Last Checkpoint: %s"
" Current Log File: %u\n"
" Next Log Segment: %u\n"
" Last Checkpoint Record: Log File (%u) Offset (0x%08x)\n"
" Previous Checkpoint Record: Log File (%u) Offset (0x%08x)\n"
" Last Checkpoint Record Redo: Log File (%u) Offset (0x%08x)\n"
" |- Undo: Log File (%u) Offset (0x%08x)\n"
" |- TimeLineID: %u\n"
" |- Next XID: %u\n"
" |- Next OID: %u\n"
" |- Next Multi: %u\n"
" |- Next MultiOff: %u\n"
" |- Time: %s"
" Maximum Data Alignment: %u\n"
" Floating-Point Sample: %.7g%s\n"
" Database Block Size: %u\n"
" Blocks Per Segment: %u\n"
" XLOG Segment Size: %u\n"
" Maximum Identifier Length: %u\n"
" Maximum Index Keys: %u\n"
" Date and Time Type Storage: %s\n"
" Locale Buffer Length: %u\n"
" lc_collate: %s\n"
" lc_ctype: %s\n\n",
EQ_CRC32 (crcLocal,
controlData->crc) ? "Correct" : "Not Correct",
controlData->pg_control_version,
(controlData->pg_control_version == PG_CONTROL_VERSION ?
"" : " (Not Correct!)"),
controlData->catalog_version_no,
controlData->system_identifier,
dbState,
ctime (&(controlData->time)), controlData->logId,
controlData->logSeg, controlData->checkPoint.xlogid,
controlData->checkPoint.xrecoff,
controlData->prevCheckPoint.xlogid,
controlData->prevCheckPoint.xrecoff, checkPoint->redo.xlogid,
checkPoint->redo.xrecoff, checkPoint->undo.xlogid,
checkPoint->undo.xrecoff, checkPoint->ThisTimeLineID,
checkPoint->nextXid, checkPoint->nextOid,
checkPoint->nextMulti, checkPoint->nextMultiOffset,
ctime (&checkPoint->time),
controlData->maxAlign,
controlData->floatFormat,
(controlData->floatFormat == FLOATFORMAT_VALUE ?
"" : " (Not Correct!)"),
controlData->blcksz,
controlData->relseg_size,
controlData->xlog_seg_size,
controlData->nameDataLen,
controlData->indexMaxKeys,
(controlData->enableIntTimes ?
"64 bit Integers" : "Floating Point"),
controlData->localeBuflen, controlData->lc_collate,
controlData->lc_ctype);
}
else
{
printf (" Error: pg_control file size incorrect.\n"
" Size: Correct <%u> Received <%u>.\n\n",
controlFileSize, bytesToFormat);
// If we have an error, force a formatted dump so we can see
// where things are going wrong
controlOptions |= CONTROL_FORMAT;
}
// Dump hex and ascii representation of data
if (controlOptions & CONTROL_FORMAT)
{
printf ("<pg_control Formatted Dump> *****************"
"**********************\n\n");
FormatBinary (bytesToFormat, 0);
}
}
// Dump out the contents of the block in hex and ascii.
// BYTES_PER_LINE bytes are formatted in each line.
static void
FormatBinary (unsigned int numBytes, unsigned int startIndex)
{
unsigned int index = 0;
unsigned int stopIndex = 0;
unsigned int x = 0;
unsigned int lastByte = startIndex + numBytes;
if (numBytes)
{
// Iterate through a printable row detailing the current
// address, the hex and ascii values
for (index = startIndex; index < lastByte; index += BYTES_PER_LINE)
{
stopIndex = index + BYTES_PER_LINE;
// Print out the address
if (blockOptions & BLOCK_ABSOLUTE)
printf (" %08x: ", (unsigned int) (pageOffset + index));
else
printf (" %04x: ", (unsigned int) index);
// Print out the hex version of the data
for (x = index; x < stopIndex; x++)
{
if (x < lastByte)
printf ("%02x", 0xff & ((unsigned) buffer[x]));
else
printf (" ");
if ((x & 0x03) == 0x03)
printf (" ");
}
printf (" ");
// Print out the ascii version of the data
for (x = index; x < stopIndex; x++)
{
if (x < lastByte)
printf ("%c", isprint (buffer[x]) ? buffer[x] : '.');
else
printf (" ");
}
printf ("\n");
}
printf ("\n");
}
}
// Dump the binary image of the block
static void
DumpBinaryBlock ()
{
unsigned int x;
for (x = 0; x < bytesToFormat; x++)
putchar (buffer[x]);
}
// Control the dumping of the blocks within the file
static void
DumpFileContents ()
{
unsigned int initialRead = 1;
unsigned int contentsToDump = 1;
// If the user requested a block range, seek to the correct position
// within the file for the start block.
if (blockOptions & BLOCK_RANGE)
{
unsigned int position = blockSize * blockStart;
if (fseek (fp, position, SEEK_SET) != 0)
{
printf ("Error: Seek error encountered before requested "
"start block <%d>.\n", blockStart);
contentsToDump = 0;
}
else
currentBlock = blockStart;
}
// Iterate through the blocks in the file until you reach the end or
// the requested range end
while (contentsToDump)
{
bytesToFormat = fread (buffer, 1, blockSize, fp);
if (bytesToFormat == 0)
{
// fseek() won't pop an error if you seek passed eof. The next
// subsequent read gets the error.
if (initialRead)
printf ("Error: Premature end of file encountered.\n");
else if (!(blockOptions & BLOCK_BINARY))
printf ("\n*** End of File Encountered. Last Block "
"Read: %d ***\n", currentBlock - 1);
contentsToDump = 0;
}
else
{
if (blockOptions & BLOCK_BINARY)
DumpBinaryBlock ();
else
{
if (controlOptions & CONTROL_DUMP)
{
FormatControl ();
contentsToDump = false;
}
else
FormatBlock ();
}
}
// Check to see if we are at the end of the requested range.
if ((blockOptions & BLOCK_RANGE) &&
(currentBlock >= blockEnd) && (contentsToDump))
{
//Don't print out message if we're doing a binary dump
if (!(blockOptions & BLOCK_BINARY))
printf ("\n*** End of Requested Range Encountered. "
"Last Block Read: %d ***\n", currentBlock);
contentsToDump = 0;
}
else
currentBlock++;
initialRead = 0;
}
}
// Consume the options and iterate through the given file, formatting as
// requested.
int
main (int argv, char **argc)
{
// If there is a parameter list, validate the options
unsigned int validOptions;
validOptions = (argv < 2) ? OPT_RC_COPYRIGHT : ConsumeOptions (argv, argc);
// Display valid options if no parameters are received or invalid options
// where encountered
if (validOptions != OPT_RC_VALID)
DisplayOptions (validOptions);
else
{
// Don't dump the header if we're dumping binary pages
if (!(blockOptions & BLOCK_BINARY))
CreateDumpFileHeader (argv, argc);
// If the user has not forced a block size, use the size of the
// control file data or the information from the block 0 header
if (controlOptions)
{
if (!(controlOptions & CONTROL_FORCED))
blockSize = sizeof (ControlFileData);
}
else if (!(blockOptions & BLOCK_FORCED))
blockSize = GetBlockSize ();
// On a positive block size, allocate a local buffer to store
// the subsequent blocks
if (blockSize > 0)
{
buffer = (char *) malloc (blockSize);
if (buffer)
DumpFileContents ();
else
printf ("\nError: Unable to create buffer of size <%d>.\n",
blockSize);
}
}
// Close out the file and get rid of the allocated block buffer
if (fp)
fclose (fp);
if (buffer)
free (buffer);
exit (0);
}
|