Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
12 KiB

  1. /*
  2. * This code implements the old MapDebugInformation API.
  3. *
  4. * For source to MapDebugInformation, check out mdi.c
  5. */
  6. #include <pch.h>
  7. BOOL fVerbose;
  8. BOOL fRemote;
  9. UCHAR CurrentImageName[ MAX_PATH + 1 ];
  10. UCHAR SymbolPath[ MAX_PATH + 1 ];
  11. void
  12. Usage( void )
  13. {
  14. fputs("usage: DBGDUMP [-?] [-v] [-r] image-names...\n"
  15. " [-?] display this message\n"
  16. " [-v] verbose output\n"
  17. " [-r symbol path] assume image names are from remote system.\n",
  18. stderr);
  19. exit( 1 );
  20. }
  21. #ifndef _WIN64
  22. void
  23. ShowDebugInfo(
  24. PIMAGE_DEBUG_INFORMATION DebugInfo
  25. );
  26. #endif
  27. VOID
  28. DumpSectionHeader(
  29. IN ULONG i,
  30. IN PIMAGE_SECTION_HEADER Sh
  31. );
  32. int __cdecl
  33. main(
  34. int argc,
  35. char *argv[],
  36. char *envp[]
  37. )
  38. {
  39. #ifdef _WIN64
  40. printf("MapDebugInformation() is not supported on 64 bit platforms.\n");
  41. #else
  42. char c, *s;
  43. LPSTR FilePart;
  44. HANDLE FileHandle;
  45. PIMAGE_DEBUG_INFORMATION DebugInfo;
  46. if (argc <= 1) {
  47. Usage();
  48. }
  49. while (--argc) {
  50. s = *++argv;
  51. if (*s == '/' || *s == '-') {
  52. while (c = *++s)
  53. switch (toupper( c )) {
  54. case '?':
  55. Usage();
  56. break;
  57. case 'V':
  58. fVerbose = TRUE;
  59. break;
  60. case 'R':
  61. if (--argc) {
  62. StringCchCopy( (PCHAR) SymbolPath, MAX_PATH, *++argv );
  63. fRemote = TRUE;
  64. }
  65. else {
  66. fprintf( stderr, "DBGDUMP: Argument to /%c switch missing\n", c );
  67. Usage();
  68. }
  69. break;
  70. default:
  71. fprintf( stderr, "DBGDUMP: Invalid switch - /%c\n", c );
  72. Usage();
  73. break;
  74. }
  75. }
  76. else {
  77. DebugInfo = NULL;
  78. if (!fRemote) {
  79. if (!GetFullPathNameA( s, sizeof( CurrentImageName ), (PCHAR) CurrentImageName, &FilePart )) {
  80. fprintf( stderr, "DBGDUMP: invalid file name - %s (%u)\n", s, GetLastError() );
  81. }
  82. else {
  83. FileHandle = CreateFileA( (PCHAR)CurrentImageName,
  84. GENERIC_READ,
  85. FILE_SHARE_READ | FILE_SHARE_WRITE,
  86. NULL,
  87. OPEN_EXISTING,
  88. 0,
  89. NULL
  90. );
  91. if (FileHandle == INVALID_HANDLE_VALUE) {
  92. fprintf( stderr, "DBGDUMP: unable to open - %s (%u)\n", CurrentImageName, GetLastError() );
  93. }
  94. else {
  95. DebugInfo = MapDebugInformation( FileHandle, NULL, NULL, 0 );
  96. }
  97. }
  98. }
  99. else {
  100. StringCchCopy( (PCHAR) CurrentImageName, MAX_PATH, s );
  101. DebugInfo = MapDebugInformation( NULL, (PCHAR) CurrentImageName, (PCHAR) SymbolPath, 0 );
  102. }
  103. if (DebugInfo != NULL) {
  104. ShowDebugInfo( DebugInfo );
  105. UnmapDebugInformation( DebugInfo );
  106. }
  107. }
  108. }
  109. exit( 0 );
  110. #endif // #if defined(_WIN64)
  111. return 0;
  112. }
  113. #ifndef _WIN64
  114. VOID
  115. DumpSectionHeader(
  116. IN ULONG i,
  117. IN PIMAGE_SECTION_HEADER Sh
  118. )
  119. {
  120. PCHAR name;
  121. ULONG li, lj;
  122. USHORT memFlags, alignFlags;
  123. printf( "\nSECTION HEADER #%hX\n% 8.8s name\n", i, Sh->Name );
  124. printf( "% 8lX physical address\n% 8lX virtual address\n% 8lX size of raw data\n% 8lX file pointer to raw data\n% 8lX file pointer to relocation table\n",
  125. Sh->Misc.PhysicalAddress,
  126. Sh->VirtualAddress,
  127. Sh->SizeOfRawData,
  128. Sh->PointerToRawData,
  129. Sh->PointerToRelocations );
  130. printf( "% 8lX file pointer to line numbers\n% 8hX number of relocations\n% 8hX number of line numbers\n% 8lX flags\n",
  131. Sh->PointerToLinenumbers,
  132. Sh->NumberOfRelocations,
  133. Sh->NumberOfLinenumbers,
  134. Sh->Characteristics );
  135. memFlags = alignFlags = 0;
  136. for (li=Sh->Characteristics, lj=0L; li; li=li>>1, lj++) {
  137. if (li & 1) {
  138. switch((li & 1) << lj) {
  139. case IMAGE_SCN_TYPE_NO_PAD : name = (PCHAR) "No Pad"; break;
  140. case IMAGE_SCN_CNT_CODE : name = (PCHAR) "Code"; break;
  141. case IMAGE_SCN_CNT_INITIALIZED_DATA : name = (PCHAR) "Initialized Data"; break;
  142. case IMAGE_SCN_CNT_UNINITIALIZED_DATA : name = (PCHAR) "Uninitialized Data"; break;
  143. case IMAGE_SCN_LNK_OTHER : name = (PCHAR) "Other"; break;
  144. case IMAGE_SCN_LNK_INFO : name = (PCHAR) "Info"; break;
  145. case IMAGE_SCN_LNK_REMOVE : name = (PCHAR) "Remove"; break;
  146. case IMAGE_SCN_LNK_COMDAT : name = (PCHAR) "Communal"; break;
  147. case IMAGE_SCN_ALIGN_1BYTES :
  148. case IMAGE_SCN_ALIGN_2BYTES :
  149. case IMAGE_SCN_ALIGN_4BYTES :
  150. case IMAGE_SCN_ALIGN_8BYTES :
  151. case IMAGE_SCN_ALIGN_16BYTES :
  152. case IMAGE_SCN_ALIGN_32BYTES :
  153. case IMAGE_SCN_ALIGN_64BYTES : name = (PCHAR) ""; break;
  154. case IMAGE_SCN_MEM_DISCARDABLE: name = (PCHAR) "Discardable"; break;
  155. case IMAGE_SCN_MEM_NOT_CACHED : name = (PCHAR) "Not Cached"; break;
  156. case IMAGE_SCN_MEM_NOT_PAGED : name = (PCHAR) "Not Paged"; break;
  157. case IMAGE_SCN_MEM_SHARED : name = (PCHAR) "Shared"; break;
  158. case IMAGE_SCN_MEM_EXECUTE : name = (PCHAR) ""; memFlags |= 1; break;
  159. case IMAGE_SCN_MEM_READ : name = (PCHAR) ""; memFlags |= 2; break;
  160. case IMAGE_SCN_MEM_WRITE : name = (PCHAR) ""; memFlags |= 4; break;
  161. default : name = (PCHAR) "RESERVED - UNKNOWN";
  162. }
  163. if (*name) {
  164. printf( " %s\n", name );
  165. }
  166. }
  167. }
  168. if (Sh->Characteristics & IMAGE_SCN_ALIGN_64BYTES) {
  169. switch(Sh->Characteristics & IMAGE_SCN_ALIGN_64BYTES) {
  170. case IMAGE_SCN_ALIGN_1BYTES : name = (PCHAR) "Align1"; break;
  171. case IMAGE_SCN_ALIGN_2BYTES : name = (PCHAR) "Align2"; break;
  172. case IMAGE_SCN_ALIGN_4BYTES : name = (PCHAR) "Align4"; break;
  173. case IMAGE_SCN_ALIGN_8BYTES : name = (PCHAR) "Align8"; break;
  174. case IMAGE_SCN_ALIGN_16BYTES : name = (PCHAR) "Align16"; break;
  175. case IMAGE_SCN_ALIGN_32BYTES : name = (PCHAR) "Align32"; break;
  176. case IMAGE_SCN_ALIGN_64BYTES : name = (PCHAR) "Align64"; break;
  177. }
  178. printf( " %s\n", name );
  179. }
  180. if (memFlags) {
  181. switch(memFlags) {
  182. case 1 : name = (PCHAR) "Execute Only"; break;
  183. case 2 : name = (PCHAR) "Read Only"; break;
  184. case 3 : name = (PCHAR) "Execute Read"; break;
  185. case 4 : name = (PCHAR) "Write Only"; break;
  186. case 5 : name = (PCHAR) "Execute Write"; break;
  187. case 6 : name = (PCHAR) "Read Write"; break;
  188. case 7 : name = (PCHAR) "Execute Read Write"; break;
  189. default : name = (PCHAR) "Unknown Memory Flags"; break;
  190. }
  191. printf( " %s\n", name );
  192. }
  193. }
  194. char *FrameType[] = {
  195. "FRAME_FPO",
  196. "FRAME_TRAP",
  197. "FRAME_TSS",
  198. "FRAME_NONFPO",
  199. "FRAME_UNKNOWN"
  200. };
  201. void
  202. ShowDebugInfo(
  203. PIMAGE_DEBUG_INFORMATION DebugInfo
  204. )
  205. {
  206. ULONG i;
  207. PIMAGE_SECTION_HEADER Section;
  208. PIMAGE_FUNCTION_ENTRY FunctionEntry;
  209. PFPO_DATA FpoEntry;
  210. LPSTR s;
  211. printf( "Debug information at % p\n", DebugInfo );
  212. printf( " Size % 8lx\n", DebugInfo->ReservedSize );
  213. printf( " Mapped at % p\n", DebugInfo->ReservedMappedBase );
  214. printf( " Machine % 8hx\n", DebugInfo->ReservedMachine );
  215. printf( " Characteristics % 8hx\n", DebugInfo->ReservedCharacteristics );
  216. printf( " Time/Date stamp % 8lx", DebugInfo->ReservedTimeDateStamp );
  217. if (DebugInfo->ReservedTimeDateStamp && (s = ctime( (time_t *)&DebugInfo->ReservedTimeDateStamp ))) {
  218. printf( " %s", s );
  219. }
  220. else {
  221. putchar( '\n' );
  222. }
  223. printf( " CheckSum % 8lx\n", DebugInfo->ReservedCheckSum );
  224. printf( " ImageBase % 8lx\n", DebugInfo->ImageBase );
  225. printf( " SizeOfImage % 8lx\n", DebugInfo->SizeOfImage );
  226. printf( " NumberOfSections % 8lx\n", DebugInfo->ReservedNumberOfSections );
  227. printf( " ExportedNamesSize% 8lx\n", DebugInfo->ReservedExportedNamesSize );
  228. printf( " #Function Entries% 8lx\n", DebugInfo->ReservedNumberOfFunctionTableEntries );
  229. printf( " #FPO Entries % 8lx\n", DebugInfo->ReservedNumberOfFpoTableEntries );
  230. printf( " Coff Symbol Size % 8lx\n", DebugInfo->SizeOfCoffSymbols );
  231. printf( " CV Symbol Size % 8lx\n", DebugInfo->ReservedSizeOfCodeViewSymbols );
  232. printf( " Image Path %s\n", DebugInfo->ImageFilePath );
  233. printf( " Image Name %s\n", DebugInfo->ImageFileName );
  234. printf( " Debug Path %s\n", DebugInfo->ReservedDebugFilePath );
  235. printf( "\n" );
  236. if (DebugInfo->ReservedNumberOfSections != 0) {
  237. printf( "Section Headers:\n" );
  238. Section = DebugInfo->ReservedSections;
  239. for (i=0; i<DebugInfo->ReservedNumberOfSections; i++) {
  240. DumpSectionHeader( i, Section++ );
  241. }
  242. printf( "\n" );
  243. }
  244. if (DebugInfo->ReservedExportedNamesSize != 0) {
  245. printf( "Exported Names:\n" );
  246. s = DebugInfo->ReservedExportedNames;
  247. while (*s) {
  248. printf( " %s\n", s );
  249. while (*s++) {
  250. }
  251. }
  252. printf( "\n" );
  253. }
  254. if (DebugInfo->ReservedNumberOfFunctionTableEntries != 0) {
  255. printf( "Function Table:\n" );
  256. FunctionEntry = DebugInfo->ReservedFunctionTableEntries;
  257. for (i=0; i<DebugInfo->ReservedNumberOfFunctionTableEntries; i++) {
  258. printf( " % 4x: % 8x % 8x % 8x\n",
  259. i,
  260. FunctionEntry->StartingAddress,
  261. FunctionEntry->EndingAddress,
  262. FunctionEntry->EndOfPrologue
  263. );
  264. FunctionEntry += 1;
  265. }
  266. printf( "\n" );
  267. }
  268. if (DebugInfo->ReservedNumberOfFpoTableEntries != 0) {
  269. printf( "FPO Table:\n" );
  270. FpoEntry = DebugInfo->ReservedFpoTableEntries;
  271. for (i=0; i<DebugInfo->ReservedNumberOfFpoTableEntries; i++) {
  272. printf( " % 4x: % 8x % 8x % 8x [%02x %1x%s%s %s]\n",
  273. i,
  274. FpoEntry->ulOffStart,
  275. FpoEntry->cbProcSize,
  276. FpoEntry->cdwParams,
  277. FpoEntry->cbProlog,
  278. FpoEntry->cbRegs,
  279. FpoEntry->fHasSEH ? " SEH" : "",
  280. FpoEntry->fUseBP ? " EBP" : "",
  281. FrameType[ FpoEntry->cbFrame ]
  282. );
  283. FpoEntry += 1;
  284. }
  285. printf( "\n" );
  286. }
  287. return;
  288. }
  289. #endif // #ifndef _WIN64