kernel32/tests: Add extra tests for SetCurrentDirectory.
[wine/multimedia.git] / tools / winedump / pdb.c
blob1d9fce024e4a039ecc9823213205cab9f463f00d
1 /*
2 * PDB dumping utility
4 * Copyright 2006 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include <fcntl.h>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winedump.h"
47 #include "wine/mscvpdb.h"
49 struct pdb_reader
51 union
53 struct
55 const struct PDB_JG_HEADER* header;
56 const struct PDB_JG_TOC* toc;
57 } jg;
58 struct
60 const struct PDB_DS_HEADER* header;
61 const struct PDB_DS_TOC* toc;
62 } ds;
63 } u;
64 void* (*read_file)(struct pdb_reader*, DWORD);
65 DWORD file_used[1024];
68 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list, int size)
70 int i, nBlocks;
71 BYTE* buffer;
73 if (!size) return NULL;
75 nBlocks = (size + pdb->block_size - 1) / pdb->block_size;
76 buffer = malloc(nBlocks * pdb->block_size);
78 for (i = 0; i < nBlocks; i++)
79 memcpy(buffer + i * pdb->block_size,
80 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
82 return buffer;
85 static void* pdb_jg_read_file(struct pdb_reader* reader, DWORD file_nr)
87 const WORD* block_list;
88 DWORD i;
90 if (!reader->u.jg.toc || file_nr >= reader->u.jg.toc->num_files) return NULL;
92 reader->file_used[file_nr / 32] |= 1 << (file_nr % 32);
93 if (reader->u.jg.toc->file[file_nr].size == 0 ||
94 reader->u.jg.toc->file[file_nr].size == 0xFFFFFFFF)
95 return NULL;
96 block_list = (const WORD*) &reader->u.jg.toc->file[reader->u.jg.toc->num_files];
97 for (i = 0; i < file_nr; i++)
98 block_list += (reader->u.jg.toc->file[i].size +
99 reader->u.jg.header->block_size - 1) / reader->u.jg.header->block_size;
101 return pdb_jg_read(reader->u.jg.header, block_list,
102 reader->u.jg.toc->file[file_nr].size);
105 static void pdb_jg_init(struct pdb_reader* reader)
107 reader->u.jg.header = PRD(0, sizeof(struct PDB_JG_HEADER));
108 reader->read_file = pdb_jg_read_file;
109 reader->u.jg.toc = pdb_jg_read(reader->u.jg.header,
110 reader->u.jg.header->toc_block,
111 reader->u.jg.header->toc.size);
112 memset(reader->file_used, 0, sizeof(reader->file_used));
115 static DWORD pdb_get_num_files(const struct pdb_reader* reader)
117 if (reader->read_file == pdb_jg_read_file)
118 return reader->u.jg.toc->num_files;
119 else
120 return reader->u.ds.toc->num_files;
123 static DWORD pdb_get_file_size(const struct pdb_reader* reader, unsigned idx)
125 if (reader->read_file == pdb_jg_read_file)
126 return reader->u.jg.toc->file[idx].size;
127 else
128 return reader->u.ds.toc->file_size[idx];
131 static void pdb_exit(struct pdb_reader* reader)
133 #if 1
134 unsigned i;
135 unsigned char* file;
136 DWORD size;
138 for (i = 0; i < pdb_get_num_files(reader); i++)
140 if (reader->file_used[i / 32] & (1 << (i % 32))) continue;
142 file = reader->read_file(reader, i);
143 if (!file) continue;
145 size = pdb_get_file_size(reader, i);
147 printf("File --unused-- #%d (%x)\n", i, size);
148 dump_data(file, size, " ");
149 free(file);
151 #endif
152 if (reader->read_file == pdb_jg_read_file)
153 free((char*)reader->u.jg.toc);
154 else
155 free((char*)reader->u.ds.toc);
158 static void pdb_dump_symbols(struct pdb_reader* reader)
160 PDB_SYMBOLS* symbols;
161 unsigned char* modimage;
162 const char* file;
164 symbols = reader->read_file(reader, 3);
166 if (!symbols) return;
168 switch (symbols->version)
170 case 0: /* VC 4.0 */
171 case 19960307: /* VC 5.0 */
172 case 19970606: /* VC 6.0 */
173 case 19990903: /* VC 7.0 */
174 break;
175 default:
176 printf("-Unknown symbol info version %d\n", symbols->version);
178 printf("Symbols:\n"
179 "\tsignature: %08x\n"
180 "\tversion: %u\n"
181 "\tunknown: %08x\n"
182 "\thash1_file: %08x\n"
183 "\thash2_file: %08x\n"
184 "\tgsym_file: %04x\n"
185 "\tunknown1: %04x\n"
186 "\tmodule_size: %08x\n"
187 "\toffset_size: %08x\n"
188 "\thash_size: %08x\n"
189 "\tsrc_module_size %08x\n"
190 "\tpdbimport_size %08x\n",
191 symbols->signature,
192 symbols->version,
193 symbols->unknown,
194 symbols->hash1_file,
195 symbols->hash2_file,
196 symbols->gsym_file,
197 symbols->unknown1,
198 symbols->module_size,
199 symbols->offset_size,
200 symbols->hash_size,
201 symbols->srcmodule_size,
202 symbols->pdbimport_size);
204 if (symbols->offset_size)
206 const BYTE* src;
208 printf("\t----------offsets------------\n");
209 src = (const BYTE*)((const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size);
210 dump_data(src, symbols->offset_size, " ");
213 if (symbols->srcmodule_size)
215 const PDB_SYMBOL_SOURCE*src;
216 int i, j, cfile;
217 const WORD* indx;
218 const DWORD* offset;
219 const char* start_cstr;
220 const char* cstr;
222 printf("\t----------src module------------\n");
223 src = (const PDB_SYMBOL_SOURCE*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
224 symbols->module_size + symbols->offset_size + symbols->hash_size);
225 printf("\tSource Modules\n"
226 "\t\tnModules: %u\n"
227 "\t\tnSrcFiles: %u\n",
228 src->nModules, src->nSrcFiles);
230 /* usage of table seems to be as follows:
231 * two arrays of WORD (src->nModules as size)
232 * - first array contains index into files for "module" compilation
233 * (module = compilation unit ??)
234 * - second array contains the number of source files in module
235 * an array of DWORD (src->nSrcFiles as size)
236 * - contains offset (in following string table) of the source file name
237 * a string table
238 * - each string is a pascal string (ie. with its length as first BYTE) or
239 * 0-terminated string (depending on version)
241 indx = &src->table[src->nModules];
242 offset = (const DWORD*)&src->table[2 * src->nModules];
243 cstr = (const char*)&src->table[2 * (src->nModules + src->nSrcFiles)];
244 start_cstr = cstr;
246 for (i = cfile = 0; i < src->nModules; i++)
248 printf("\t\tModule[%2d]:\n", i);
249 for (j = 0; j < indx[i]; j++, cfile++)
251 /* FIXME: in some cases, it's a p_string but WHEN ? */
252 if (src->table[cfile] < src->nSrcFiles &&
253 cstr + offset[src->table[cfile]] >= (const char*)start_cstr /* wrap around */ &&
254 cstr + offset[src->table[cfile]] < (const char*)src + symbols->srcmodule_size)
255 printf("\t\t\tSource file: %s\n", cstr + offset[src->table[cfile]]);
256 else
257 printf("\t\t\tSource file: <<out of bounds>>\n");
261 if (symbols->pdbimport_size)
263 const PDB_SYMBOL_IMPORT* imp;
264 const char* first;
265 const char* last;
266 const char* ptr;
268 printf("\t------------import--------------\n");
269 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
270 symbols->module_size + symbols->offset_size +
271 symbols->hash_size + symbols->srcmodule_size);
272 first = (const char*)imp;
273 last = (const char*)imp + symbols->pdbimport_size;
274 while (imp < (const PDB_SYMBOL_IMPORT*)last)
276 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
277 printf("\tImport: %lx\n"
278 "\t\tUnknown1: %08x\n"
279 "\t\tUnknown2: %08x\n"
280 "\t\tTimeDateStamp: %08x\n"
281 "\t\tAge: %08u\n"
282 "\t\tfile1: %s\n"
283 "\t\tfile2: %s\n",
284 (ULONG_PTR)((const char*)imp - (const char*)first),
285 imp->unknown1,
286 imp->unknown2,
287 imp->TimeDateStamp,
288 imp->Age,
289 imp->filename,
290 ptr);
291 imp = (const PDB_SYMBOL_IMPORT*)(first + ((ptr - first + strlen(ptr) + 1 + 3) & ~3));
295 /* Read global symbol table */
296 modimage = reader->read_file(reader, symbols->gsym_file);
297 if (modimage)
299 printf("\t------------globals-------------\n");
300 codeview_dump_symbols(modimage, pdb_get_file_size(reader, symbols->gsym_file));
301 free(modimage);
304 /* Read per-module symbol / linenumber tables */
305 file = (const char*)symbols + sizeof(PDB_SYMBOLS);
306 while (file - (const char*)symbols < sizeof(PDB_SYMBOLS) + symbols->module_size)
308 int file_nr, symbol_size, lineno_size;
309 const char* file_name;
311 if (symbols->version < 19970000)
313 const PDB_SYMBOL_FILE* sym_file = (const PDB_SYMBOL_FILE*) file;
314 file_nr = sym_file->file;
315 file_name = sym_file->filename;
316 symbol_size = sym_file->symbol_size;
317 lineno_size = sym_file->lineno_size;
318 printf("\t--------symbol file----------- %s\n", file_name);
319 printf("\tgot symbol_file\n"
320 "\t\tunknown1: %08x \n"
321 "\t\trange\n"
322 "\t\t\tsegment: %04x\n"
323 "\t\t\tpad1: %04x\n"
324 "\t\t\toffset: %08x\n"
325 "\t\t\tsize: %08x\n"
326 "\t\t\tcharacteristics: %08x\n"
327 "\t\t\tindex: %04x\n"
328 "\t\t\tpad2: %04x\n"
329 "\t\tflag: %04x\n"
330 "\t\tfile: %04x\n"
331 "\t\tsymb size: %08x\n"
332 "\t\tline size: %08x\n"
333 "\t\tunknown2: %08x\n"
334 "\t\tnSrcFiles: %08x\n"
335 "\t\tattribute: %08x\n",
336 sym_file->unknown1,
337 sym_file->range.segment,
338 sym_file->range.pad1,
339 sym_file->range.offset,
340 sym_file->range.size,
341 sym_file->range.characteristics,
342 sym_file->range.index,
343 sym_file->range.pad2,
344 sym_file->flag,
345 sym_file->file,
346 sym_file->symbol_size,
347 sym_file->lineno_size,
348 sym_file->unknown2,
349 sym_file->nSrcFiles,
350 sym_file->attribute);
352 else
354 const PDB_SYMBOL_FILE_EX* sym_file = (const PDB_SYMBOL_FILE_EX*) file;
355 file_nr = sym_file->file;
356 file_name = sym_file->filename;
357 symbol_size = sym_file->symbol_size;
358 lineno_size = sym_file->lineno_size;
359 printf("\t--------symbol file----------- %s\n", file_name);
360 printf("\t\tunknown1: %08x \n"
361 "\t\trange\n"
362 "\t\t\tsegment: %04x\n"
363 "\t\t\tpad1: %04x\n"
364 "\t\t\toffset: %08x\n"
365 "\t\t\tsize: %08x\n"
366 "\t\t\tcharacteristics: %08x\n"
367 "\t\t\tindex: %04x\n"
368 "\t\t\tpad2: %04x\n"
369 "\t\t\ttimestamp: %08x\n"
370 "\t\t\tunknown: %08x\n"
371 "\t\tflag: %04x\n"
372 "\t\tfile: %04x\n"
373 "\t\tsymb size: %08x\n"
374 "\t\tline size: %08x\n"
375 "\t\tunknown2: %08x\n"
376 "\t\tnSrcFiles: %08x\n"
377 "\t\tattribute: %08x\n"
378 "\t\treserved/0: %08x\n"
379 "\t\treserved/1: %08x\n",
380 sym_file->unknown1,
381 sym_file->range.segment,
382 sym_file->range.pad1,
383 sym_file->range.offset,
384 sym_file->range.size,
385 sym_file->range.characteristics,
386 sym_file->range.index,
387 sym_file->range.pad2,
388 sym_file->range.timestamp,
389 sym_file->range.unknown,
390 sym_file->flag,
391 sym_file->file,
392 sym_file->symbol_size,
393 sym_file->lineno_size,
394 sym_file->unknown2,
395 sym_file->nSrcFiles,
396 sym_file->attribute,
397 sym_file->reserved[0],
398 sym_file->reserved[1]);
400 modimage = reader->read_file(reader, file_nr);
401 if (modimage)
403 int total_size = pdb_get_file_size(reader, file_nr);
405 if (symbol_size)
406 codeview_dump_symbols((const char*)modimage + sizeof(DWORD), symbol_size);
408 /* what's that part ??? */
409 if (0)
410 dump_data(modimage + symbol_size + lineno_size, total_size - (symbol_size + lineno_size), " ");
411 free(modimage);
414 file_name += strlen(file_name) + 1;
415 file = (char*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
417 free(symbols);
420 static void pdb_dump_types(struct pdb_reader* reader)
422 PDB_TYPES* types = NULL;
424 types = reader->read_file(reader, 2);
426 switch (types->version)
428 case 19950410: /* VC 4.0 */
429 case 19951122:
430 case 19961031: /* VC 5.0 / 6.0 */
431 case 19990903: /* VC 7.0 */
432 break;
433 default:
434 printf("-Unknown type info version %d\n", types->version);
437 /* Read type table */
438 printf("Types:\n"
439 "\tversion: %u\n"
440 "\ttype_offset: %08x\n"
441 "\tfirst_index: %x\n"
442 "\tlast_index: %x\n"
443 "\ttype_size: %x\n"
444 "\tfile: %x\n"
445 "\tpad: %x\n"
446 "\thash_size: %x\n"
447 "\thash_base: %x\n"
448 "\thash_offset: %x\n"
449 "\thash_len: %x\n"
450 "\tsearch_offset: %x\n"
451 "\tsearch_len: %x\n"
452 "\tunknown_offset: %x\n"
453 "\tunknown_len: %x\n",
454 types->version,
455 types->type_offset,
456 types->first_index,
457 types->last_index,
458 types->type_size,
459 types->file,
460 types->pad,
461 types->hash_size,
462 types->hash_base,
463 types->hash_offset,
464 types->hash_len,
465 types->search_offset,
466 types->search_len,
467 types->unknown_offset,
468 types->unknown_len);
469 codeview_dump_types_from_block((const char*)types + types->type_offset, types->type_size);
470 free(types);
473 static const char pdb2[] = "Microsoft C/C++ program database 2.00";
475 static void pdb_jg_dump(void)
477 struct pdb_reader reader;
478 struct PDB_JG_ROOT* root = NULL;
481 * Read in TOC and well-known files
483 pdb_jg_init(&reader);
484 printf("Header (JG):\n"
485 "\tident: %.*s\n"
486 "\tsignature: %08x\n"
487 "\tblock_size: %08x\n"
488 "\tfree_list: %04x\n"
489 "\ttotal_alloc:%04x\n",
490 (int)sizeof(pdb2) - 1, reader.u.jg.header->ident,
491 reader.u.jg.header->signature,
492 reader.u.jg.header->block_size,
493 reader.u.jg.header->free_list,
494 reader.u.jg.header->total_alloc);
496 root = reader.read_file(&reader, 1);
498 if (root)
500 printf("Root:\n"
501 "\tVersion: %u\n"
502 "\tTimeDateStamp: %08x\n"
503 "\tAge: %08x\n"
504 "\tnames: %.*s\n",
505 root->Version,
506 root->TimeDateStamp,
507 root->Age,
508 (unsigned)root->cbNames,
509 root->names);
511 /* Check for unknown versions */
512 switch (root->Version)
514 case 19950623: /* VC 4.0 */
515 case 19950814:
516 case 19960307: /* VC 5.0 */
517 case 19970604: /* VC 6.0 */
518 break;
519 default:
520 printf("-Unknown root block version %d\n", root->Version);
522 free(root);
524 else printf("-Unable to get root\n");
526 pdb_dump_types(&reader);
527 #if 0
528 /* segments info, index is unknown */
530 const void* segs = pdb_read_file(pdb, toc, 8); /* FIXME which index ??? */
531 const void* ptr = segs;
533 if (segs) while (ptr < segs + toc->file[8].size)
535 printf("Segment %s\n", (const char*)ptr);
536 ptr += (strlen(ptr) + 1 + 3) & ~3;
537 printf("\tdword[0]: %08lx\n", *(DWORD*)ptr); ptr += 4;
538 printf("\tdword[1]: %08lx\n", *(DWORD*)ptr); ptr += 4;
539 printf("\tdword[2]: %08lx\n", *(DWORD*)ptr); ptr += 4;
540 printf("\tdword[3]: %08lx\n", *(DWORD*)ptr); ptr += 4;
541 printf("\tdword[4]: %08lx\n", *(DWORD*)ptr); ptr += 4;
542 printf("\tdword[5]: %08lx\n", *(DWORD*)ptr); ptr += 4;
543 printf("\tdword[6]: %08lx\n", *(DWORD*)ptr); ptr += 4;
544 printf("\tdword[7]: %08lx\n", *(DWORD*)ptr); ptr += 4;
546 free(segs);
548 #endif
550 pdb_dump_symbols(&reader);
551 pdb_exit(&reader);
554 static void* pdb_ds_read(const struct PDB_DS_HEADER* header, const DWORD* block_list, int size)
556 int i, nBlocks;
557 BYTE* buffer;
559 if (!size) return NULL;
561 nBlocks = (size + header->block_size - 1) / header->block_size;
562 buffer = malloc(nBlocks * header->block_size);
564 for (i = 0; i < nBlocks; i++)
565 memcpy(buffer + i * header->block_size,
566 (const char*)header + block_list[i] * header->block_size, header->block_size);
568 return buffer;
571 static void* pdb_ds_read_file(struct pdb_reader* reader, DWORD file_number)
573 const DWORD* block_list;
574 DWORD i;
576 if (!reader->u.ds.toc || file_number >= reader->u.ds.toc->num_files) return NULL;
578 reader->file_used[file_number / 32] |= 1 << (file_number % 32);
579 if (reader->u.ds.toc->file_size[file_number] == 0 ||
580 reader->u.ds.toc->file_size[file_number] == 0xFFFFFFFF)
581 return NULL;
582 block_list = reader->u.ds.toc->file_size + reader->u.ds.toc->num_files;
583 for (i = 0; i < file_number; i++)
584 block_list += (reader->u.ds.toc->file_size[i] + reader->u.ds.header->block_size - 1) /
585 reader->u.ds.header->block_size;
587 return pdb_ds_read(reader->u.ds.header, block_list, reader->u.ds.toc->file_size[file_number]);
590 static BOOL pdb_ds_init(struct pdb_reader* reader)
592 reader->u.ds.header = PRD(0, sizeof(*reader->u.ds.header));
593 if (!reader->u.ds.header) return FALSE;
594 reader->read_file = pdb_ds_read_file;
595 reader->u.ds.toc = pdb_ds_read(reader->u.ds.header,
596 (const DWORD*)((const char*)reader->u.ds.header + reader->u.ds.header->toc_page * reader->u.ds.header->block_size),
597 reader->u.ds.header->toc_size);
598 memset(reader->file_used, 0, sizeof(reader->file_used));
599 return TRUE;
602 static const char pdb7[] = "Microsoft C/C++ MSF 7.00";
604 static void pdb_ds_dump(void)
606 struct pdb_reader reader;
607 struct PDB_DS_ROOT* root;
609 pdb_ds_init(&reader);
610 printf("Header (DS)\n"
611 "\tsignature: %.*s\n"
612 "\tblock_size: %08x\n"
613 "\tunknown1: %08x\n"
614 "\tnum_pages: %08x\n"
615 "\ttoc_size: %08x\n"
616 "\tunknown2: %08x\n"
617 "\ttoc_page: %08x\n",
618 (int)sizeof(pdb7) - 1, reader.u.ds.header->signature,
619 reader.u.ds.header->block_size,
620 reader.u.ds.header->unknown1,
621 reader.u.ds.header->num_pages,
622 reader.u.ds.header->toc_size,
623 reader.u.ds.header->unknown2,
624 reader.u.ds.header->toc_page);
626 /* files:
627 * 0: JG says old toc pages, I'd say free pages (tbc, low prio)
628 * 1: root structure
629 * 2: types
630 * 3: modules
632 root = reader.read_file(&reader, 1);
633 if (root)
635 const char* ptr;
637 printf("Root:\n"
638 "\tVersion: %u\n"
639 "\tTimeDateStamp: %08x\n"
640 "\tAge: %08x\n"
641 "\tguid %s\n"
642 "\tcbNames: %08x\n",
643 root->Version,
644 root->TimeDateStamp,
645 root->Age,
646 get_guid_str(&root->guid),
647 root->cbNames);
648 for (ptr = &root->names[0]; ptr < &root->names[0] + root->cbNames; ptr += strlen(ptr) + 1)
649 printf("\tString: %s\n", ptr);
650 /* follows an unknown list of DWORDs */
651 free(root);
653 else printf("-Unable to get root\n");
655 pdb_dump_types(&reader);
656 pdb_dump_symbols(&reader);
658 pdb_exit(&reader);
661 enum FileSig get_kind_pdb(void)
663 const char* head;
665 head = PRD(0, sizeof(pdb2) - 1);
666 if (head && !memcmp(head, pdb2, sizeof(pdb2) - 1))
667 return SIG_PDB;
668 head = PRD(0, sizeof(pdb7) - 1);
669 if (head && !memcmp(head, pdb7, sizeof(pdb7) - 1))
670 return SIG_PDB;
671 return SIG_UNKNOWN;
674 void pdb_dump(void)
676 const char* head;
678 /* init_types(); */
679 head = PRD(0, sizeof(pdb2) - 1);
680 if (head && !memcmp(head, pdb2, sizeof(pdb2) - 1))
682 pdb_jg_dump();
683 return;
685 head = PRD(0, sizeof(pdb7) - 1);
686 if (head && !memcmp(head, pdb7, sizeof(pdb7) - 1))
688 pdb_ds_dump();
689 return;
691 printf("Unrecognized header %s\n", head);