Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / tools / winedump / debug.c
blobab7bc947441a4921a44ce67f5425f808cda81d50
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 <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <fcntl.h>
32 #include "winnt.h"
33 #include "winedump.h"
34 #include "pe.h"
35 #include "cvinclude.h"
38 * .DBG File Layout:
40 * IMAGE_SEPARATE_DEBUG_HEADER
41 * IMAGE_SECTION_HEADER[]
42 * IMAGE_DEBUG_DIRECTORY[]
43 * OMFSignature
44 * debug data (typical example)
45 * - IMAGE_DEBUG_TYPE_MISC
46 * - IMAGE_DEBUG_TYPE_FPO
47 * - IMAGE_DEBUG_TYPE_CODEVIEW
48 * OMFDirHeader
49 * OMFDirEntry[]
53 * Descriptions:
55 * (hdr) IMAGE_SEPARATE_DEBUG_HEADER - .DBG-specific file header; holds info that
56 * applies to the file as a whole, including # of COFF sections, file offsets, etc.
57 * (hdr) IMAGE_SECTION_HEADER - list of COFF sections copied verbatim from .EXE;
58 * although this directory contains file offsets, these offsets are meaningless
59 * in the context of the .DBG file, because only the section headers are copied
60 * to the .DBG file...not the binary data it points to.
61 * (hdr) IMAGE_DEBUG_DIRECTORY - list of different formats of debug info contained in file
62 * (see IMAGE_DEBUG_TYPE_* descriptions below); tells where each section starts
63 * (hdr) OMFSignature (CV) - Contains "NBxx" signature, plus file offset telling how far
64 * into the IMAGE_DEBUG_TYPE_CODEVIEW section the OMFDirHeader and OMFDirEntry's sit
65 * (data) IMAGE_DEBUG_TYPE_MISC - usually holds name of original .EXE file
66 * (data) IMAGE_DEBUG_TYPE_FPO - Frame Pointer Optimization data; used for dealing with
67 * optimized stack frames (optional)
68 * (data) IMAGE_DEBUG_TYPE_CODEVIEW - *** THE GOOD STUFF ***
69 * This block of data contains all the symbol tables, line number info, etc.,
70 * that the Visual C++ debugger needs.
71 * (hdr) OMFDirHeader (CV) -
72 * (hdr) OMFDirEntry (CV) - list of subsections within CodeView debug data section
76 * The .DBG file typically has three arrays of directory entries, which tell
77 * the OS or debugger where in the file to look for the actual data
79 * IMAGE_SECTION_HEADER - number of entries determined by:
80 * (IMAGE_SEPARATE_DEBUG_HEADER.NumberOfSections)
82 * IMAGE_DEBUG_DIRECTORY - number of entries determined by:
83 * (IMAGE_SEPARATE_DEBUG_HEADER.DebugDirectorySize / sizeof (IMAGE_DEBUG_DIRECTORY))
85 * OMFDirEntry - number of entries determined by:
86 * (OMFDirHeader.cDir)
89 static void* cv_base /* = 0 */;
91 static int dump_cv_sst_module(OMFDirEntry* omfde)
93 OMFModule* module;
94 OMFSegDesc* segDesc;
95 int i;
97 module = PRD(Offset(cv_base) + omfde->lfo, sizeof(OMFModule));
98 if (!module) {printf("Can't get the OMF-Module, aborting\n"); return FALSE;}
100 printf(" olvNumber: %u\n", module->ovlNumber);
101 printf(" iLib: %u\n", module->iLib);
102 printf(" cSeg: %u\n", module->cSeg);
103 printf(" Style: %c%c\n", module->Style[0], module->Style[1]);
104 printf(" Name: %.*s\n",
105 *(BYTE*)((char*)(module + 1) + sizeof(OMFSegDesc) * module->cSeg),
106 (char*)(module + 1) + sizeof(OMFSegDesc) * module->cSeg + 1);
108 segDesc = PRD(Offset(module + 1), sizeof(OMFSegDesc) * module->cSeg);
109 if (!segDesc) {printf("Can't get the OMF-SegDesc, aborting\n"); return FALSE;}
111 for (i = 0; i < module->cSeg; i++)
113 printf (" segment #%2d: offset = [0x%8lx], size = [0x%8lx]\n",
114 segDesc->Seg, segDesc->Off, segDesc->cbSeg);
115 segDesc++;
117 return TRUE;
120 static int dump_cv_sst_global_pub(OMFDirEntry* omfde)
122 long fileoffset;
123 OMFSymHash* header;
124 BYTE* symbols;
125 BYTE* curpos;
126 PUBSYM32* sym;
127 unsigned symlen;
128 int recordlen;
129 char nametmp[256];
131 fileoffset = Offset(cv_base) + omfde->lfo;
132 printf (" GlobalPub section starts at file offset 0x%lx\n", fileoffset);
133 printf (" Symbol table starts at 0x%lx\n", fileoffset + sizeof (OMFSymHash));
135 printf ("\n ----- Begin Symbol Table -----\n");
136 printf (" (type) (symbol name) (offset) (len) (seg) (ind)\n");
138 header = PRD(fileoffset, sizeof(OMFSymHash));
139 if (!header) {printf("Can't get OMF-SymHash, aborting\n");return FALSE;}
141 symbols = PRD(fileoffset + sizeof(OMFSymHash), header->cbSymbol);
142 if (!symbols) {printf("Can't OMF-SymHash details, aborting\n"); return FALSE;}
144 /* We don't know how many symbols are in this block of memory...only what
145 * the total size of the block is. Because the symbol's name is tacked
146 * on to the end of the PUBSYM32 struct, each symbol may take up a different
147 * # of bytes. This makes it harder to parse through the symbol table,
148 * since we won't know the exact location of the following symbol until we've
149 * already parsed the current one.
151 for (curpos = symbols; curpos < symbols + header->cbSymbol; curpos += recordlen)
153 /* Point to the next PUBSYM32 in the table.
155 sym = (PUBSYM32*)curpos;
157 if (sym->reclen < sizeof(PUBSYM32)) break;
159 symlen = sym->reclen - sizeof(PUBSYM32) + 1;
160 if (symlen > sizeof(nametmp)) {printf("\nsqueeze%d\n", symlen);symlen = sizeof(nametmp) - 1;}
162 memcpy(nametmp, curpos + sizeof (PUBSYM32) + 1, symlen);
163 nametmp[symlen] = '\0';
165 printf (" 0x%04x %-30.30s [0x%8lx] [0x%4x] %d %ld\n",
166 sym->rectyp, nametmp, sym->off, sym->reclen, sym->seg, sym->typind);
168 /* The entire record is null-padded to the nearest 4-byte
169 * boundary, so we must do a little extra math to keep things straight.
171 recordlen = (sym->reclen + 3) & ~3;
174 return TRUE;
177 static int dump_cv_sst_global_sym(OMFDirEntry* omfde)
179 /*** NOT YET IMPLEMENTED ***/
180 return TRUE;
183 static int dump_cv_sst_static_sym(OMFDirEntry* omfde)
185 /*** NOT YET IMPLEMENTED ***/
186 return TRUE;
189 static int dump_cv_sst_libraries(OMFDirEntry* omfde)
191 /*** NOT YET IMPLEMENTED ***/
192 return TRUE;
195 static int dump_cv_sst_global_types(OMFDirEntry* omfde)
197 /*** NOT YET IMPLEMENTED ***/
198 return TRUE;
201 static int dump_cv_sst_seg_map(OMFDirEntry* omfde)
203 OMFSegMap* segMap;
204 OMFSegMapDesc* segMapDesc;
205 int i;
207 segMap = PRD(Offset(cv_base) + omfde->lfo, sizeof(OMFSegMap));
208 if (!segMap) {printf("Can't get SegMap, aborting\n");return FALSE;}
210 printf(" cSeg: %u\n", segMap->cSeg);
211 printf(" cSegLog: %u\n", segMap->cSegLog);
213 segMapDesc = PRD(Offset(segMap + 1), segMap->cSeg * sizeof(OMFSegDesc));
214 if (!segMapDesc) {printf("Can't get SegDescr array, aborting\n");return FALSE;}
216 for (i = 0; i < segMap->cSeg; i++)
218 printf(" SegDescr #%2d\n", i + 1);
219 printf(" flags: %04X\n", segMapDesc[i].flags);
220 printf(" ovl: %u\n", segMapDesc[i].ovl);
221 printf(" group: %u\n", segMapDesc[i].group);
222 printf(" frame: %u\n", segMapDesc[i].frame);
223 printf(" iSegName: %u\n", segMapDesc[i].iSegName);
224 printf(" iClassName: %u\n", segMapDesc[i].iClassName);
225 printf(" offset: %lu\n", segMapDesc[i].offset);
226 printf(" cbSeg: %lu\n", segMapDesc[i].cbSeg);
229 return TRUE;
232 static int dump_cv_sst_file_index(OMFDirEntry* omfde)
234 /*** NOT YET IMPLEMENTED ***/
235 return TRUE;
238 static int dump_cv_sst_src_module(OMFDirEntry* omfde)
240 int i, j;
241 BYTE* rawdata;
242 unsigned long* seg_info_dw;
243 unsigned short* seg_info_w;
244 unsigned ofs;
245 OMFSourceModule* sourceModule;
246 OMFSourceFile* sourceFile;
247 OMFSourceLine* sourceLine;
249 rawdata = PRD(Offset(cv_base) + omfde->lfo, omfde->cb);
250 if (!rawdata) {printf("Can't get srcModule subsection details, aborting\n");return FALSE;}
252 /* FIXME: check ptr validity */
253 sourceModule = (void*)rawdata;
254 printf (" Module table: Found %d file(s) and %d segment(s)\n",
255 sourceModule->cFile, sourceModule->cSeg);
256 for (i = 0; i < sourceModule->cFile; i++)
258 printf (" File #%2d begins at an offset of 0x%lx in this section\n",
259 i + 1, sourceModule->baseSrcFile[i]);
262 /* FIXME: check ptr validity */
263 seg_info_dw = (void*)((char*)(sourceModule + 1) +
264 sizeof(unsigned long) * (sourceModule->cFile - 1));
265 seg_info_w = (unsigned short*)(&seg_info_dw[sourceModule->cSeg * 2]);
266 for (i = 0; i < sourceModule->cSeg; i++)
268 printf (" Segment #%2d start = 0x%lx, end = 0x%lx, seg index = %u\n",
269 i + 1, seg_info_dw[i * 2], seg_info_dw[(i * 2) + 1],
270 seg_info_w[i]);
272 ofs = sizeof(OMFSourceModule) + sizeof(unsigned long) * (sourceModule->cFile - 1) +
273 sourceModule->cSeg * (2 * sizeof(unsigned long) + sizeof(unsigned short));
274 ofs = (ofs + 3) & ~3;
276 /* the OMFSourceFile is quite unpleasant to use:
277 * we have first:
278 * unsigned short number of segments
279 * unsigned short reservered
280 * unsigned long baseSrcLn[# segments]
281 * unsigned long offset[2 * #segments]
282 * odd indices are start offsets
283 * even indices are end offsets
284 * unsigned char string length for file name
285 * char file name (length is previous field)
287 /* FIXME: check ptr validity */
288 sourceFile = (void*)(rawdata + ofs);
289 seg_info_dw = (void*)((char*)sourceFile + 2 * sizeof(unsigned short) +
290 sourceFile->cSeg * sizeof(unsigned long));
292 ofs += 2 * sizeof(unsigned short) + 3 * sourceFile->cSeg * sizeof(unsigned long);
294 printf(" File table: %.*s\n",
295 *(BYTE*)((char*)sourceModule + ofs), (char*)sourceModule + ofs + 1);
297 for (i = 0; i < sourceFile->cSeg; i++)
299 printf (" Segment #%2d start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
300 i + 1, seg_info_dw[i * 2], seg_info_dw[(i * 2) + 1], sourceFile->baseSrcLn[i]);
302 /* add file name length */
303 ofs += *(BYTE*)((char*)sourceModule + ofs) + 1;
304 ofs = (ofs + 3) & ~3;
306 for (i = 0; i < sourceModule->cSeg; i++)
308 sourceLine = (void*)(rawdata + ofs);
309 seg_info_dw = (void*)((char*)sourceLine + 2 * sizeof(unsigned short));
310 seg_info_w = (void*)(&seg_info_dw[sourceLine->cLnOff]);
312 printf (" Line table #%2d: Found %d line numbers for segment index %d\n",
313 i, sourceLine->cLnOff, sourceLine->Seg);
315 for (j = 0; j < sourceLine->cLnOff; j++)
317 printf (" Pair #%2d: offset = [0x%8lx], linenumber = %d\n",
318 j + 1, seg_info_dw[j], seg_info_w[j]);
320 ofs += 2 * sizeof(unsigned short) +
321 sourceLine->cLnOff * (sizeof(unsigned long) + sizeof(unsigned short));
322 ofs = (ofs + 3) & ~3;
325 return TRUE;
328 static int dump_cv_sst_align_sym(OMFDirEntry* omfde)
330 /*** NOT YET IMPLEMENTED ***/
332 return TRUE;
335 static void dump_codeview_all_modules(OMFDirHeader *omfdh)
337 unsigned i;
338 OMFDirEntry *dirEntry;
339 const char* str;
341 if (!omfdh || !omfdh->cDir) return;
343 dirEntry = PRD(Offset(omfdh + 1), omfdh->cDir * sizeof(OMFDirEntry));
344 if (!dirEntry) {printf("Can't read DirEntry array, aborting\n"); return;}
346 for (i = 0; i < omfdh->cDir; i++)
348 switch (dirEntry[i].SubSection)
350 case sstModule: str = "sstModule"; break;
351 case sstAlignSym: str = "sstAlignSym"; break;
352 case sstSrcModule: str = "sstSrcModule"; break;
353 case sstLibraries: str = "sstLibraries"; break;
354 case sstGlobalSym: str = "sstGlobalSym"; break;
355 case sstGlobalPub: str = "sstGlobalPub"; break;
356 case sstGlobalTypes: str = "sstGlobalTypes"; break;
357 case sstSegMap: str = "sstSegMap"; break;
358 case sstFileIndex: str = "sstFileIndex"; break;
359 case sstStaticSym: str = "sstStaticSym"; break;
360 default: str = "<undefined>"; break;
362 printf("Module #%2d (%p)\n", i + 1, &dirEntry[i]);
363 printf(" SubSection: %04X (%s)\n", dirEntry[i].SubSection, str);
364 printf(" iMod: %d\n", dirEntry[i].iMod);
365 printf(" lfo: %ld\n", dirEntry[i].lfo);
366 printf(" cb: %lu\n", dirEntry[i].cb);
368 switch (dirEntry[i].SubSection)
370 case sstModule: dump_cv_sst_module(&dirEntry[i]); break;
371 case sstAlignSym: dump_cv_sst_align_sym(&dirEntry[i]); break;
372 case sstSrcModule: dump_cv_sst_src_module(&dirEntry[i]); break;
373 case sstLibraries: dump_cv_sst_libraries(&dirEntry[i]); break;
374 case sstGlobalSym: dump_cv_sst_global_sym(&dirEntry[i]); break;
375 case sstGlobalPub: dump_cv_sst_global_pub(&dirEntry[i]); break;
376 case sstGlobalTypes: dump_cv_sst_global_types(&dirEntry[i]); break;
377 case sstSegMap: dump_cv_sst_seg_map(&dirEntry[i]); break;
378 case sstFileIndex: dump_cv_sst_file_index(&dirEntry[i]); break;
379 case sstStaticSym: dump_cv_sst_static_sym(&dirEntry[i]); break;
380 default: printf("unsupported type %x\n", dirEntry[i].SubSection); break;
382 printf("\n");
385 return;
388 static void dump_codeview_headers(unsigned long base, unsigned long len)
390 OMFDirHeader *dirHeader;
391 OMFSignature *signature;
392 OMFDirEntry *dirEntry;
393 unsigned i;
394 int modulecount = 0, alignsymcount = 0, srcmodulecount = 0, librariescount = 0;
395 int globalsymcount = 0, globalpubcount = 0, globaltypescount = 0;
396 int segmapcount = 0, fileindexcount = 0, staticsymcount = 0;
398 cv_base = PRD(base, len);
399 if (!cv_base) {printf("Can't get full debug content, aborting\n");return;}
401 signature = cv_base;
403 printf(" CodeView Data\n");
405 printf(" Signature: %.4s\n", signature->Signature);
406 printf(" Filepos: 0x%08lX\n", signature->filepos);
408 if (memcmp(signature->Signature, "NB10", 4) == 0)
410 struct {DWORD TimeStamp; DWORD Dunno; char Name[1];}* pdb_data;
411 pdb_data = (void*)(signature + 1);
413 printf(" TimeStamp: %08lX (%s)\n",
414 pdb_data->TimeStamp, get_time_str(pdb_data->TimeStamp));
415 printf(" Dunno: %08lX\n", pdb_data->Dunno);
416 printf(" Filename: %s\n", pdb_data->Name);
417 return;
420 if (memcmp(signature->Signature, "NB09", 4) != 0 && memcmp(signature->Signature, "NB11", 4) != 0)
422 printf("Unsupported signature, aborting\n");
423 return;
426 dirHeader = PRD(Offset(cv_base) + signature->filepos, sizeof(OMFDirHeader));
427 if (!dirHeader) {printf("Can't get debug header, aborting\n"); return;}
429 printf(" Size of header: 0x%4X\n", dirHeader->cbDirHeader);
430 printf(" Size per entry: 0x%4X\n", dirHeader->cbDirEntry);
431 printf(" # of entries: 0x%8lX (%ld)\n", dirHeader->cDir, dirHeader->cDir);
432 printf(" Offset to NextDir: 0x%8lX\n", dirHeader->lfoNextDir);
433 printf(" Flags: 0x%8lX\n", dirHeader->flags);
435 if (!dirHeader->cDir) return;
437 dirEntry = PRD(Offset(dirHeader + 1), sizeof(OMFDirEntry) * dirHeader->cDir);
438 if (!dirEntry) {printf("Can't get DirEntry array, aborting\n");return;}
440 for (i = 0; i < dirHeader->cDir; i++)
442 switch (dirEntry[i].SubSection)
444 case sstModule: modulecount++; break;
445 case sstAlignSym: alignsymcount++; break;
446 case sstSrcModule: srcmodulecount++; break;
447 case sstLibraries: librariescount++; break;
448 case sstGlobalSym: globalsymcount++; break;
449 case sstGlobalPub: globalpubcount++; break;
450 case sstGlobalTypes: globaltypescount++; break;
451 case sstSegMap: segmapcount++; break;
452 case sstFileIndex: fileindexcount++; break;
453 case sstStaticSym: staticsymcount++; break;
457 /* This one has to be > 0
459 printf ("\nFound: %d sstModule subsections\n", modulecount);
461 if (alignsymcount > 0) printf (" %d sstAlignSym subsections\n", alignsymcount);
462 if (srcmodulecount > 0) printf (" %d sstSrcModule subsections\n", srcmodulecount);
463 if (librariescount > 0) printf (" %d sstLibraries subsections\n", librariescount);
464 if (globalsymcount > 0) printf (" %d sstGlobalSym subsections\n", globalsymcount);
465 if (globalpubcount > 0) printf (" %d sstGlobalPub subsections\n", globalpubcount);
466 if (globaltypescount > 0) printf (" %d sstGlobalTypes subsections\n", globaltypescount);
467 if (segmapcount > 0) printf (" %d sstSegMap subsections\n", segmapcount);
468 if (fileindexcount > 0) printf (" %d sstFileIndex subsections\n", fileindexcount);
469 if (staticsymcount > 0) printf (" %d sstStaticSym subsections\n", staticsymcount);
471 dump_codeview_all_modules(dirHeader);
474 void dump_codeview(unsigned long base, unsigned long len)
476 dump_codeview_headers(base, len);