Include protection for <unistd.h>, <sys/types.h> and <sys/stat.h>.
[wine/hacks.git] / tools / winedump / debug.c
blobbd9164e307f84cb7a3178c39b284034dfce296aa
1 /*
2 * Made after:
3 * CVDump - Parses through a Visual Studio .DBG file in CodeView 4 format
4 * and dumps the info to STDOUT in a human-readable format
6 * Copyright 2000 John R. Sheets
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <time.h>
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #ifdef HAVE_SYS_MMAN_H
39 #include <sys/mman.h>
40 #endif
41 #include <fcntl.h>
43 #include "winnt.h"
44 #include "winedump.h"
45 #include "pe.h"
46 #include "cvinclude.h"
49 * .DBG File Layout:
51 * IMAGE_SEPARATE_DEBUG_HEADER
52 * IMAGE_SECTION_HEADER[]
53 * IMAGE_DEBUG_DIRECTORY[]
54 * OMFSignature
55 * debug data (typical example)
56 * - IMAGE_DEBUG_TYPE_MISC
57 * - IMAGE_DEBUG_TYPE_FPO
58 * - IMAGE_DEBUG_TYPE_CODEVIEW
59 * OMFDirHeader
60 * OMFDirEntry[]
64 * Descriptions:
66 * (hdr) IMAGE_SEPARATE_DEBUG_HEADER - .DBG-specific file header; holds info that
67 * applies to the file as a whole, including # of COFF sections, file offsets, etc.
68 * (hdr) IMAGE_SECTION_HEADER - list of COFF sections copied verbatim from .EXE;
69 * although this directory contains file offsets, these offsets are meaningless
70 * in the context of the .DBG file, because only the section headers are copied
71 * to the .DBG file...not the binary data it points to.
72 * (hdr) IMAGE_DEBUG_DIRECTORY - list of different formats of debug info contained in file
73 * (see IMAGE_DEBUG_TYPE_* descriptions below); tells where each section starts
74 * (hdr) OMFSignature (CV) - Contains "NBxx" signature, plus file offset telling how far
75 * into the IMAGE_DEBUG_TYPE_CODEVIEW section the OMFDirHeader and OMFDirEntry's sit
76 * (data) IMAGE_DEBUG_TYPE_MISC - usually holds name of original .EXE file
77 * (data) IMAGE_DEBUG_TYPE_FPO - Frame Pointer Optimization data; used for dealing with
78 * optimized stack frames (optional)
79 * (data) IMAGE_DEBUG_TYPE_CODEVIEW - *** THE GOOD STUFF ***
80 * This block of data contains all the symbol tables, line number info, etc.,
81 * that the Visual C++ debugger needs.
82 * (hdr) OMFDirHeader (CV) -
83 * (hdr) OMFDirEntry (CV) - list of subsections within CodeView debug data section
87 * The .DBG file typically has three arrays of directory entries, which tell
88 * the OS or debugger where in the file to look for the actual data
90 * IMAGE_SECTION_HEADER - number of entries determined by:
91 * (IMAGE_SEPARATE_DEBUG_HEADER.NumberOfSections)
93 * IMAGE_DEBUG_DIRECTORY - number of entries determined by:
94 * (IMAGE_SEPARATE_DEBUG_HEADER.DebugDirectorySize / sizeof (IMAGE_DEBUG_DIRECTORY))
96 * OMFDirEntry - number of entries determined by:
97 * (OMFDirHeader.cDir)
100 static void* cv_base /* = 0 */;
102 static int dump_cv_sst_module(OMFDirEntry* omfde)
104 OMFModule* module;
105 OMFSegDesc* segDesc;
106 int i;
108 module = PRD(Offset(cv_base) + omfde->lfo, sizeof(OMFModule));
109 if (!module) {printf("Can't get the OMF-Module, aborting\n"); return FALSE;}
111 printf(" olvNumber: %u\n", module->ovlNumber);
112 printf(" iLib: %u\n", module->iLib);
113 printf(" cSeg: %u\n", module->cSeg);
114 printf(" Style: %c%c\n", module->Style[0], module->Style[1]);
115 printf(" Name: %.*s\n",
116 *(BYTE*)((char*)(module + 1) + sizeof(OMFSegDesc) * module->cSeg),
117 (char*)(module + 1) + sizeof(OMFSegDesc) * module->cSeg + 1);
119 segDesc = PRD(Offset(module + 1), sizeof(OMFSegDesc) * module->cSeg);
120 if (!segDesc) {printf("Can't get the OMF-SegDesc, aborting\n"); return FALSE;}
122 for (i = 0; i < module->cSeg; i++)
124 printf (" segment #%2d: offset = [0x%8lx], size = [0x%8lx]\n",
125 segDesc->Seg, segDesc->Off, segDesc->cbSeg);
126 segDesc++;
128 return TRUE;
131 static int dump_cv_sst_global_pub(OMFDirEntry* omfde)
133 long fileoffset;
134 OMFSymHash* header;
135 BYTE* symbols;
136 BYTE* curpos;
137 PUBSYM32* sym;
138 unsigned symlen;
139 int recordlen;
140 char nametmp[256];
142 fileoffset = Offset(cv_base) + omfde->lfo;
143 printf (" GlobalPub section starts at file offset 0x%lx\n", fileoffset);
144 printf (" Symbol table starts at 0x%lx\n", fileoffset + sizeof (OMFSymHash));
146 printf ("\n ----- Begin Symbol Table -----\n");
147 printf (" (type) (symbol name) (offset) (len) (seg) (ind)\n");
149 header = PRD(fileoffset, sizeof(OMFSymHash));
150 if (!header) {printf("Can't get OMF-SymHash, aborting\n");return FALSE;}
152 symbols = PRD(fileoffset + sizeof(OMFSymHash), header->cbSymbol);
153 if (!symbols) {printf("Can't OMF-SymHash details, aborting\n"); return FALSE;}
155 /* We don't know how many symbols are in this block of memory...only what
156 * the total size of the block is. Because the symbol's name is tacked
157 * on to the end of the PUBSYM32 struct, each symbol may take up a different
158 * # of bytes. This makes it harder to parse through the symbol table,
159 * since we won't know the exact location of the following symbol until we've
160 * already parsed the current one.
162 for (curpos = symbols; curpos < symbols + header->cbSymbol; curpos += recordlen)
164 /* Point to the next PUBSYM32 in the table.
166 sym = (PUBSYM32*)curpos;
168 if (sym->reclen < sizeof(PUBSYM32)) break;
170 symlen = sym->reclen - sizeof(PUBSYM32) + 1;
171 if (symlen > sizeof(nametmp)) {printf("\nsqueeze%d\n", symlen);symlen = sizeof(nametmp) - 1;}
173 memcpy(nametmp, curpos + sizeof (PUBSYM32) + 1, symlen);
174 nametmp[symlen] = '\0';
176 printf (" 0x%04x %-30.30s [0x%8lx] [0x%4x] %d %ld\n",
177 sym->rectyp, nametmp, sym->off, sym->reclen, sym->seg, sym->typind);
179 /* The entire record is null-padded to the nearest 4-byte
180 * boundary, so we must do a little extra math to keep things straight.
182 recordlen = (sym->reclen + 3) & ~3;
185 return TRUE;
188 static int dump_cv_sst_global_sym(OMFDirEntry* omfde)
190 /*** NOT YET IMPLEMENTED ***/
191 return TRUE;
194 static int dump_cv_sst_static_sym(OMFDirEntry* omfde)
196 /*** NOT YET IMPLEMENTED ***/
197 return TRUE;
200 static int dump_cv_sst_libraries(OMFDirEntry* omfde)
202 /*** NOT YET IMPLEMENTED ***/
203 return TRUE;
206 static int dump_cv_sst_global_types(OMFDirEntry* omfde)
208 /*** NOT YET IMPLEMENTED ***/
209 return TRUE;
212 static int dump_cv_sst_seg_map(OMFDirEntry* omfde)
214 OMFSegMap* segMap;
215 OMFSegMapDesc* segMapDesc;
216 int i;
218 segMap = PRD(Offset(cv_base) + omfde->lfo, sizeof(OMFSegMap));
219 if (!segMap) {printf("Can't get SegMap, aborting\n");return FALSE;}
221 printf(" cSeg: %u\n", segMap->cSeg);
222 printf(" cSegLog: %u\n", segMap->cSegLog);
224 segMapDesc = PRD(Offset(segMap + 1), segMap->cSeg * sizeof(OMFSegDesc));
225 if (!segMapDesc) {printf("Can't get SegDescr array, aborting\n");return FALSE;}
227 for (i = 0; i < segMap->cSeg; i++)
229 printf(" SegDescr #%2d\n", i + 1);
230 printf(" flags: %04X\n", segMapDesc[i].flags);
231 printf(" ovl: %u\n", segMapDesc[i].ovl);
232 printf(" group: %u\n", segMapDesc[i].group);
233 printf(" frame: %u\n", segMapDesc[i].frame);
234 printf(" iSegName: %u\n", segMapDesc[i].iSegName);
235 printf(" iClassName: %u\n", segMapDesc[i].iClassName);
236 printf(" offset: %lu\n", segMapDesc[i].offset);
237 printf(" cbSeg: %lu\n", segMapDesc[i].cbSeg);
240 return TRUE;
243 static int dump_cv_sst_file_index(OMFDirEntry* omfde)
245 /*** NOT YET IMPLEMENTED ***/
246 return TRUE;
249 static int dump_cv_sst_src_module(OMFDirEntry* omfde)
251 int i, j;
252 BYTE* rawdata;
253 unsigned long* seg_info_dw;
254 unsigned short* seg_info_w;
255 unsigned ofs;
256 OMFSourceModule* sourceModule;
257 OMFSourceFile* sourceFile;
258 OMFSourceLine* sourceLine;
260 rawdata = PRD(Offset(cv_base) + omfde->lfo, omfde->cb);
261 if (!rawdata) {printf("Can't get srcModule subsection details, aborting\n");return FALSE;}
263 /* FIXME: check ptr validity */
264 sourceModule = (void*)rawdata;
265 printf (" Module table: Found %d file(s) and %d segment(s)\n",
266 sourceModule->cFile, sourceModule->cSeg);
267 for (i = 0; i < sourceModule->cFile; i++)
269 printf (" File #%2d begins at an offset of 0x%lx in this section\n",
270 i + 1, sourceModule->baseSrcFile[i]);
273 /* FIXME: check ptr validity */
274 seg_info_dw = (void*)((char*)(sourceModule + 1) +
275 sizeof(unsigned long) * (sourceModule->cFile - 1));
276 seg_info_w = (unsigned short*)(&seg_info_dw[sourceModule->cSeg * 2]);
277 for (i = 0; i < sourceModule->cSeg; i++)
279 printf (" Segment #%2d start = 0x%lx, end = 0x%lx, seg index = %u\n",
280 i + 1, seg_info_dw[i * 2], seg_info_dw[(i * 2) + 1],
281 seg_info_w[i]);
283 ofs = sizeof(OMFSourceModule) + sizeof(unsigned long) * (sourceModule->cFile - 1) +
284 sourceModule->cSeg * (2 * sizeof(unsigned long) + sizeof(unsigned short));
285 ofs = (ofs + 3) & ~3;
287 /* the OMFSourceFile is quite unpleasant to use:
288 * we have first:
289 * unsigned short number of segments
290 * unsigned short reservered
291 * unsigned long baseSrcLn[# segments]
292 * unsigned long offset[2 * #segments]
293 * odd indices are start offsets
294 * even indices are end offsets
295 * unsigned char string length for file name
296 * char file name (length is previous field)
298 /* FIXME: check ptr validity */
299 sourceFile = (void*)(rawdata + ofs);
300 seg_info_dw = (void*)((char*)sourceFile + 2 * sizeof(unsigned short) +
301 sourceFile->cSeg * sizeof(unsigned long));
303 ofs += 2 * sizeof(unsigned short) + 3 * sourceFile->cSeg * sizeof(unsigned long);
305 printf(" File table: %.*s\n",
306 *(BYTE*)((char*)sourceModule + ofs), (char*)sourceModule + ofs + 1);
308 for (i = 0; i < sourceFile->cSeg; i++)
310 printf (" Segment #%2d start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
311 i + 1, seg_info_dw[i * 2], seg_info_dw[(i * 2) + 1], sourceFile->baseSrcLn[i]);
313 /* add file name length */
314 ofs += *(BYTE*)((char*)sourceModule + ofs) + 1;
315 ofs = (ofs + 3) & ~3;
317 for (i = 0; i < sourceModule->cSeg; i++)
319 sourceLine = (void*)(rawdata + ofs);
320 seg_info_dw = (void*)((char*)sourceLine + 2 * sizeof(unsigned short));
321 seg_info_w = (void*)(&seg_info_dw[sourceLine->cLnOff]);
323 printf (" Line table #%2d: Found %d line numbers for segment index %d\n",
324 i, sourceLine->cLnOff, sourceLine->Seg);
326 for (j = 0; j < sourceLine->cLnOff; j++)
328 printf (" Pair #%2d: offset = [0x%8lx], linenumber = %d\n",
329 j + 1, seg_info_dw[j], seg_info_w[j]);
331 ofs += 2 * sizeof(unsigned short) +
332 sourceLine->cLnOff * (sizeof(unsigned long) + sizeof(unsigned short));
333 ofs = (ofs + 3) & ~3;
336 return TRUE;
339 static int dump_cv_sst_align_sym(OMFDirEntry* omfde)
341 /*** NOT YET IMPLEMENTED ***/
343 return TRUE;
346 static void dump_codeview_all_modules(OMFDirHeader *omfdh)
348 unsigned i;
349 OMFDirEntry *dirEntry;
350 const char* str;
352 if (!omfdh || !omfdh->cDir) return;
354 dirEntry = PRD(Offset(omfdh + 1), omfdh->cDir * sizeof(OMFDirEntry));
355 if (!dirEntry) {printf("Can't read DirEntry array, aborting\n"); return;}
357 for (i = 0; i < omfdh->cDir; i++)
359 switch (dirEntry[i].SubSection)
361 case sstModule: str = "sstModule"; break;
362 case sstAlignSym: str = "sstAlignSym"; break;
363 case sstSrcModule: str = "sstSrcModule"; break;
364 case sstLibraries: str = "sstLibraries"; break;
365 case sstGlobalSym: str = "sstGlobalSym"; break;
366 case sstGlobalPub: str = "sstGlobalPub"; break;
367 case sstGlobalTypes: str = "sstGlobalTypes"; break;
368 case sstSegMap: str = "sstSegMap"; break;
369 case sstFileIndex: str = "sstFileIndex"; break;
370 case sstStaticSym: str = "sstStaticSym"; break;
371 default: str = "<undefined>"; break;
373 printf("Module #%2d (%p)\n", i + 1, &dirEntry[i]);
374 printf(" SubSection: %04X (%s)\n", dirEntry[i].SubSection, str);
375 printf(" iMod: %d\n", dirEntry[i].iMod);
376 printf(" lfo: %ld\n", dirEntry[i].lfo);
377 printf(" cb: %lu\n", dirEntry[i].cb);
379 switch (dirEntry[i].SubSection)
381 case sstModule: dump_cv_sst_module(&dirEntry[i]); break;
382 case sstAlignSym: dump_cv_sst_align_sym(&dirEntry[i]); break;
383 case sstSrcModule: dump_cv_sst_src_module(&dirEntry[i]); break;
384 case sstLibraries: dump_cv_sst_libraries(&dirEntry[i]); break;
385 case sstGlobalSym: dump_cv_sst_global_sym(&dirEntry[i]); break;
386 case sstGlobalPub: dump_cv_sst_global_pub(&dirEntry[i]); break;
387 case sstGlobalTypes: dump_cv_sst_global_types(&dirEntry[i]); break;
388 case sstSegMap: dump_cv_sst_seg_map(&dirEntry[i]); break;
389 case sstFileIndex: dump_cv_sst_file_index(&dirEntry[i]); break;
390 case sstStaticSym: dump_cv_sst_static_sym(&dirEntry[i]); break;
391 default: printf("unsupported type %x\n", dirEntry[i].SubSection); break;
393 printf("\n");
396 return;
399 static void dump_codeview_headers(unsigned long base, unsigned long len)
401 OMFDirHeader *dirHeader;
402 OMFSignature *signature;
403 OMFDirEntry *dirEntry;
404 unsigned i;
405 int modulecount = 0, alignsymcount = 0, srcmodulecount = 0, librariescount = 0;
406 int globalsymcount = 0, globalpubcount = 0, globaltypescount = 0;
407 int segmapcount = 0, fileindexcount = 0, staticsymcount = 0;
409 cv_base = PRD(base, len);
410 if (!cv_base) {printf("Can't get full debug content, aborting\n");return;}
412 signature = cv_base;
414 printf(" CodeView Data\n");
416 printf(" Signature: %.4s\n", signature->Signature);
417 printf(" Filepos: 0x%08lX\n", signature->filepos);
419 if (memcmp(signature->Signature, "NB10", 4) == 0)
421 struct {DWORD TimeStamp; DWORD Dunno; char Name[1];}* pdb_data;
422 pdb_data = (void*)(signature + 1);
424 printf(" TimeStamp: %08lX (%s)\n",
425 pdb_data->TimeStamp, get_time_str(pdb_data->TimeStamp));
426 printf(" Dunno: %08lX\n", pdb_data->Dunno);
427 printf(" Filename: %s\n", pdb_data->Name);
428 return;
431 if (memcmp(signature->Signature, "NB09", 4) != 0 && memcmp(signature->Signature, "NB11", 4) != 0)
433 printf("Unsupported signature, aborting\n");
434 return;
437 dirHeader = PRD(Offset(cv_base) + signature->filepos, sizeof(OMFDirHeader));
438 if (!dirHeader) {printf("Can't get debug header, aborting\n"); return;}
440 printf(" Size of header: 0x%4X\n", dirHeader->cbDirHeader);
441 printf(" Size per entry: 0x%4X\n", dirHeader->cbDirEntry);
442 printf(" # of entries: 0x%8lX (%ld)\n", dirHeader->cDir, dirHeader->cDir);
443 printf(" Offset to NextDir: 0x%8lX\n", dirHeader->lfoNextDir);
444 printf(" Flags: 0x%8lX\n", dirHeader->flags);
446 if (!dirHeader->cDir) return;
448 dirEntry = PRD(Offset(dirHeader + 1), sizeof(OMFDirEntry) * dirHeader->cDir);
449 if (!dirEntry) {printf("Can't get DirEntry array, aborting\n");return;}
451 for (i = 0; i < dirHeader->cDir; i++)
453 switch (dirEntry[i].SubSection)
455 case sstModule: modulecount++; break;
456 case sstAlignSym: alignsymcount++; break;
457 case sstSrcModule: srcmodulecount++; break;
458 case sstLibraries: librariescount++; break;
459 case sstGlobalSym: globalsymcount++; break;
460 case sstGlobalPub: globalpubcount++; break;
461 case sstGlobalTypes: globaltypescount++; break;
462 case sstSegMap: segmapcount++; break;
463 case sstFileIndex: fileindexcount++; break;
464 case sstStaticSym: staticsymcount++; break;
468 /* This one has to be > 0
470 printf ("\nFound: %d sstModule subsections\n", modulecount);
472 if (alignsymcount > 0) printf (" %d sstAlignSym subsections\n", alignsymcount);
473 if (srcmodulecount > 0) printf (" %d sstSrcModule subsections\n", srcmodulecount);
474 if (librariescount > 0) printf (" %d sstLibraries subsections\n", librariescount);
475 if (globalsymcount > 0) printf (" %d sstGlobalSym subsections\n", globalsymcount);
476 if (globalpubcount > 0) printf (" %d sstGlobalPub subsections\n", globalpubcount);
477 if (globaltypescount > 0) printf (" %d sstGlobalTypes subsections\n", globaltypescount);
478 if (segmapcount > 0) printf (" %d sstSegMap subsections\n", segmapcount);
479 if (fileindexcount > 0) printf (" %d sstFileIndex subsections\n", fileindexcount);
480 if (staticsymcount > 0) printf (" %d sstStaticSym subsections\n", staticsymcount);
482 dump_codeview_all_modules(dirHeader);
485 void dump_codeview(unsigned long base, unsigned long len)
487 dump_codeview_headers(base, len);