source: trunk/tools/dbginfo/dbgLXDumper.c@ 3314

Last change on this file since 3314 was 3286, checked in by bird, 26 years ago

IBMSRC record somewhat dumpable.

File size: 21.3 KB
Line 
1/* $Id: dbgLXDumper.c,v 1.6 2000-03-31 15:35:08 bird Exp $
2 *
3 * dbgLXDumper - reads and interprets the debuginfo found in an LX executable.
4 *
5 * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11/*******************************************************************************
12* Defined Constants And Macros *
13*******************************************************************************/
14#define INCL_DOSERRORS
15#define FOR_EXEHDR 1 /* exe386.h flag */
16#define DWORD ULONG /* Used by exe386.h / newexe.h */
17#define WORD USHORT /* Used by exe386.h / newexe.h */
18
19#define HLLVERSION100 0x0100
20#define HLLVERSION300 0x0300
21#define HLLVERSION400 0x0400
22#define HLLVERSION500 0x0500
23
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#include <os2.h>
29#include <newexe.h>
30#include <exe386.h>
31
32#include <malloc.h>
33#include <string.h>
34#include <stdio.h>
35#include <stddef.h>
36#include <ctype.h>
37
38#include "hll.h"
39
40
41
42/*******************************************************************************
43* Internal Functions *
44*******************************************************************************/
45int dbgLXDump(void *pvFile);
46int dumpHLL(FILE *phOut, PBYTE pb, int cb);
47void dumpHex(FILE *phOut, PBYTE pb, int cb);
48void * readfile(const char *pszFilename);
49signed long fsize(FILE *phFile);
50
51
52
53/*******************************************************************************
54* Global Variables *
55*******************************************************************************/
56static char achBufferDummy64[0x10000] = {0};
57char achBufferDummy128[0x20000] = {0};
58
59int main(int argc, char **argv)
60{
61 void * pvFile;
62
63 if (argc != 2)
64 {
65 fprintf(stderr, "error parameters!\n");
66 return -1;
67 }
68
69 pvFile = readfile(argv[1]);
70 if (pvFile == NULL)
71 {
72 fprintf(stderr, "error reading file %s\n", argv[1]);
73 return -2;
74 }
75
76 return dbgLXDump(pvFile);
77}
78
79
80
81/**
82 * Dumps the internals of LX dubug info.
83 * @returns error code. (0 is ok)
84 * @param pvFile Pointer to filemapping.
85 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
86 */
87int dbgLXDump(void *pvFile)
88{
89 struct exe_hdr * pehdr = (struct exe_hdr *) pvFile;
90 struct e32_exe * pe32;
91 unsigned offe32;
92 PBYTE pbFile = (PBYTE)pvFile;
93
94 /*
95 * Find LX header.
96 */
97 if (pehdr->e_magic == EMAGIC)
98 offe32 = pehdr->e_lfanew;
99 else
100 offe32 = 0;
101 pe32 = (struct e32_exe *)((unsigned)pvFile + offe32);
102 if (pe32->e32_magic[0] != E32MAGIC1 || pe32->e32_magic[1] != E32MAGIC2)
103 {
104 fprintf(stderr, "not LX executable\n");
105 return ERROR_INVALID_EXE_SIGNATURE;
106 }
107
108 /*
109 * Check if there is any debuginfo in this executable.
110 */
111 printf("e32_debuginfo 0x%08x (%d)\n"
112 "e32_debuglen 0x%08x (%d)\n",
113 pe32->e32_debuginfo, pe32->e32_debuglen);
114
115 if (pe32->e32_debuginfo != 0 && pe32->e32_debuglen > 0)
116 {
117 PBYTE pbDbg = pbFile + pe32->e32_debuginfo;
118 /*
119 * Check signature. 'NB0'
120 */
121 printf("Debug signature: %c%c%c%c\n",
122 pbDbg[0], pbDbg[1], pbDbg[2], pbDbg[3]);
123 if (pbDbg[0] == 'N' && pbDbg[1] == 'B' && pbDbg[2] == '0')
124 {
125 int i;
126
127 /*
128 * Switch on debug datatype.
129 */
130 switch (pbDbg[3])
131 {
132 case '0':
133 printf("Found 32-bit Codeview format\n");
134 break;
135
136 case '1':
137 printf("Found AIX Debugger format - unsupported\n");
138 break;
139
140 case '2':
141 printf("Found 16-bit Codeview format\n");
142 break;
143
144 case '4':
145 printf("Found 32-bit OS/2 PM Debugger format (HLL)\n");
146 return dumpHLL(stdout, pbDbg, pe32->e32_debuglen);
147
148 default:
149 printf("Invalid debug type, %c (%d)\n", pbDbg[3], pbDbg[3]);
150 return ERROR_INVALID_DATA;
151 }
152
153 /*
154 * Dump debug data
155 */
156 printf("\ndumps debug data\n");
157 dumpHex(stdout, pbDbg + 4, pe32->e32_debuglen - 4);
158 }
159 else
160 {
161 printf("Invalid debug signature\n");
162 return ERROR_INVALID_DATA;
163 }
164 }
165 else
166 printf(" - no debug info -\n");
167
168 return NO_ERROR;
169}
170
171
172/**
173 * Dumps binary data to file handle.
174 * @param phOut Output file handle.
175 * @param pb Pointer to debug data. (Starts with signature ('NB04').)
176 * @param cb Size of debug data.
177 *
178 * HLL:
179 * Starts with a 4 byte word with the offset (from start of HLL data) to
180 * the number of entries. (what entries is yet to come)
181 *
182 *
183 */
184int dumpHLL(FILE *phOut, PBYTE pb, int cb)
185{
186 int i, j, k; /* loop variables! */
187 unsigned long ulHLLVersion = 0; /* HLL version of the last module. */
188 PHLLDIR pDir;
189 PHLLHDR pHdr = (PHLLHDR)pb;
190
191 /*
192 * Dump header.
193 */
194 fprintf(phOut,
195 "- HLL header -\n"
196 " Signature %.4s\n"
197 " Directory offset 0x%08x (%d)\n"
198 "\n",
199 pHdr->achSignature,
200 pHdr->offDirectory,
201 pHdr->offDirectory);
202
203
204 /*
205 * Get and Dump directory
206 */
207 if (pHdr->offDirectory + sizeof(HLLDIR) > cb)
208 {
209 fprintf(phOut, "error: offDirectory is incorrect! (cb=%d, off=%d)\n",
210 cb, pHdr->offDirectory);
211 return ERROR_INVALID_DATA;
212 }
213 pDir = (PHLLDIR)(pb + pHdr->offDirectory);
214 fprintf(phOut,
215 "- HLL Directory -\n"
216 " Size of this struct 0x%02x (%d)\n"
217 " Size directory entry 0x%02x (%d)\n"
218 " Number of entries 0x%08x (%d)\n",
219 pDir->cb,
220 pDir->cb,
221 pDir->cbEntry,
222 pDir->cbEntry,
223 pDir->cEntries,
224 pDir->cEntries);
225
226
227 /*
228 * Directory sanity check - check that it's not too big
229 */
230 if ((PBYTE)&pDir->aEntries[pDir->cEntries] - pb > cb)
231 {
232 fprintf(phOut, "Error: Directory is to big!\n");
233 return ERROR_INVALID_DATA;
234 }
235
236
237
238 /*
239 * Loop thru the directory.
240 */
241 for (i = 0; i < pDir->cEntries; i++)
242 {
243 /*
244 * Directory entry type descriptions.
245 */
246 static const char * apsz[] =
247 {
248 "HLL_DE_MODULES",
249 "HLL_DE_PUBLICS",
250 "HLL_DE_TYPES",
251 "HLL_DE_SYMBOLS",
252 "HLL_DE_SRCLINES",
253 "HLL_DE_LIBRARIES",
254 "unknown",
255 "unknown",
256 "HLL_DE_SRCLNSEG",
257 "unknown",
258 "HLL_DE_IBMSRC"
259 };
260 const char *pszType = pDir->aEntries[i].usType >= HLL_DE_MODULES
261 && pDir->aEntries[i].usType <= HLL_DE_IBMSRC
262 ? apsz[pDir->aEntries[i].usType - HLL_DE_MODULES]
263 : "unknown";
264
265 /*
266 * Dump directroy info.
267 */
268 fprintf(phOut, "\n"
269 "- HLL Directory Entry %d (0x%x): -\n", i, i);
270 fprintf(phOut, " usType 0x%08x (%d) %s\n"
271 " iMod 0x%08x (%d)\n"
272 " off 0x%08x (%d)\n"
273 " cb 0x%08x (%d)\n",
274 pDir->aEntries[i].usType,
275 pDir->aEntries[i].usType,
276 pszType,
277 pDir->aEntries[i].iMod,
278 pDir->aEntries[i].iMod,
279 pDir->aEntries[i].off,
280 pDir->aEntries[i].off,
281 pDir->aEntries[i].cb,
282 pDir->aEntries[i].cb
283 );
284
285
286
287 /*
288 * Switch between the different entry types to do individual
289 * processing.
290 */
291 switch (pDir->aEntries[i].usType)
292 {
293 /*
294 * Module - debuginfo on an object/source module.
295 */
296 case HLL_DE_MODULES:
297 {
298 PHLLMODULE pModule = (PHLLMODULE)(pDir->aEntries[i].off + pb);
299 PHLLSEGINFO paSegInfo;
300 int c;
301
302 /*
303 * Dump module entry data.
304 */
305 fprintf(phOut,
306 " Modulename: %.*s\n"
307 " overlay %d\n"
308 " ilib %d\n"
309 " pad %d\n"
310 " cSegInfo %d\n"
311 " usDebugStyle %#04x %c%c\n"
312 " HLL Version %d.%d\n"
313 " cchName %d\n"
314 ,
315 pModule->cchName,
316 &pModule->achName[0],
317 pModule->overlay,
318 pModule->iLib,
319 pModule->pad,
320 pModule->cSegInfo,
321 pModule->usDebugStyle,
322 pModule->usDebugStyle & 0xFF,
323 pModule->usDebugStyle >> 8,
324 pModule->chVerMajor,
325 pModule->chVerMinor,
326 pModule->cchName
327 );
328
329 ulHLLVersion = pModule->chVerMajor*0x100 + pModule->chVerMinor;
330
331
332 /*
333 * Dump Segment info
334 */
335 fprintf(phOut,
336 " SegmentInfo %d\n"
337 " iObject %#x\n"
338 " off %#x\n"
339 " cb %#x\n",
340 0,
341 pModule->SegInfo0.iObject,
342 pModule->SegInfo0.off,
343 pModule->SegInfo0.cb);
344
345 c = pModule->cSegInfo > 0 ? pModule->cSegInfo : 0;
346 paSegInfo = (PHLLSEGINFO)((void*)&pModule->achName[pModule->cchName]);
347 for (j = 0; j + 1 < c; j++)
348 {
349 fprintf(phOut,
350 " SegmentInfo %d\n"
351 " iObject %#x\n"
352 " off %#x\n"
353 " cb %#x\n",
354 j + 1,
355 paSegInfo[j].iObject,
356 paSegInfo[j].off,
357 paSegInfo[j].cb);
358 }
359 break;
360 }
361
362
363 case HLL_DE_PUBLICS: /* Public symbols */
364 {
365 PHLLPUBLICSYM pPubSym = (PHLLPUBLICSYM)(pDir->aEntries[i].off + pb);
366
367 while ((char *)pPubSym - pb - pDir->aEntries[i].off < pDir->aEntries[i].cb)
368 {
369 fprintf(phOut,
370 " %#03x:%#08x iType=%#2x name=%.*s\n",
371 pPubSym->iObject,
372 pPubSym->off,
373 pPubSym->iType,
374 pPubSym->cchName,
375 pPubSym->achName);
376
377 /* next */
378 pPubSym = (PHLLPUBLICSYM)&pPubSym->achName[pPubSym->cchName];
379 }
380 break;
381 }
382
383
384 case HLL_DE_TYPES: /* Types */
385 break;
386
387 case HLL_DE_SYMBOLS: /* Symbols */
388 break;
389
390 case HLL_DE_LIBRARIES: /* Libraries */
391 break;
392
393 case HLL_DE_SRCLINES: /* Line numbers - (IBM C/2 1.1) */
394 break;
395
396 case HLL_DE_SRCLNSEG: /* Line numbers - (MSC 6.00) */
397 break;
398
399 /*
400 * Line numbers - (IBM HLL)
401 *
402 * HLL 04 have a FirstEntry before each table.
403 * HLL 03 don't seem to have... This is not implemented yet.
404 */
405 case HLL_DE_IBMSRC:
406 {
407 PHLLFIRSTENTRY pFirstEntry = (PHLLFIRSTENTRY)(pb + pDir->aEntries[i].off);
408 int cbFirstEntry;
409 int cb;
410
411
412 /*
413 * Set the size of an first entry struct based on the HLL version.
414 */
415 if (ulHLLVersion == HLLVERSION100)
416 cbFirstEntry = sizeof(pFirstEntry->hll01);
417 else
418 cbFirstEntry = sizeof(pFirstEntry->hll04);
419
420
421 /*
422 * Loop thru all the arrays in this data directory.
423 * Each array starts with an HLLFIRSTENTRY structure.
424 */
425 cb = pDir->aEntries[i].cb;
426 while (cb >= cbFirstEntry)
427 {
428 int cbEntries;
429
430 /*
431 * Dump the special first entry.
432 */
433 fprintf(phOut,
434 " First entry:\n"
435 " uchType 0x%02x\n"
436 " uchReserved 0x%02x\n"
437 " cEntries 0x%04x\n"
438 " iSeg 0x%04x\n"
439 " offBase/cb 0x%08x\n",
440 pFirstEntry->hll04.uchType,
441 pFirstEntry->hll04.uchReserved,
442 pFirstEntry->hll04.cEntries,
443 pFirstEntry->hll04.iSeg,
444 pFirstEntry->hll04.u1.offBase
445 );
446
447 switch (pFirstEntry->hll03.uchType)
448 {
449 /*
450 * Source File information and offset only.
451 */
452 case 0:
453 {
454 int cbLine;
455 PHLLLINENUMBERENTRY pLines =
456 (PHLLLINENUMBERENTRY)((char*)pFirstEntry + cbFirstEntry);
457
458 /*
459 * Determin size of a line entry.
460 */
461 if (ulHLLVersion == HLLVERSION100)
462 cbLine = sizeof(pLines->hll01);
463 else
464 cbLine = sizeof(pLines->hll03);
465
466 /*
467 * Loop thru all the line info and dump it.
468 */
469 fprintf(phOut, " Lineinfo:\n");
470 for (k = 0; k < pFirstEntry->hll01.cEntries; k++) /* cEntries is similar for all formats. */
471 {
472 fprintf(phOut,
473 " usLine=%4d (0x%02x) iusSourceFile=0x%04x off=0x%08x\n",
474 pLines->hll04.usLine,
475 pLines->hll04.usLine,
476 pLines->hll04.iusSourceFile,
477 pLines->hll04.off
478 );
479 /* next */
480 pLines = (PHLLLINENUMBERENTRY)((char*)pLines + cbLine);
481 }
482
483 cbEntries = cbLine * pFirstEntry->hll01.cEntries; /* cEntries is similar for all formats. */
484 break;
485 }
486
487
488 /*
489 * Filenames.
490 */
491 case 3:
492 {
493 PCHAR pch;
494 PHLLFILENAMEENTRY pFilenameEntry =
495 (PHLLFILENAMEENTRY)((char*)pFirstEntry + cbFirstEntry);
496
497 fprintf(phOut,
498 " FilenameEntry:\n"
499 " offSource 0x%08x\n"
500 " cSourceRecords 0x%08x\n"
501 " cSourceFiles 0x%08x\n"
502 " cchName 0x%02x\n",
503 pFilenameEntry->offSource,
504 pFilenameEntry->cSourceRecords,
505 pFilenameEntry->cSourceFiles,
506 pFilenameEntry->cchName
507 );
508
509 fprintf(phOut,
510 " Filenames:\n");
511 pch = &pFilenameEntry->cchName;
512 for (k = 0; k < pFilenameEntry->cSourceFiles; k++)
513 {
514 fprintf(phOut,
515 " %.*s\n", *pch, pch+1);
516 /* next */
517 cbEntries += 1 + *pch;
518 pch += 1 + *pch;
519 }
520
521 if (ulHLLVersion == HLLVERSION100)
522 cbEntries = pFirstEntry->hll01.u1.cbFileNameTable;
523 else
524 cbEntries = pFirstEntry->hll03.u1.cbFileNameTable;
525 break;
526 }
527
528
529 default:
530 fprintf(phOut, "warning: unsupported entry type, %d\n", pFirstEntry->hll03.uchType);
531 cbEntries = cb = 0;
532 }
533
534
535 /*
536 * Next
537 */
538 cb -= cbEntries + sizeof(HLLFIRSTENTRY);
539 pFirstEntry = (PHLLFIRSTENTRY)((char*)pFirstEntry + cbEntries + sizeof(HLLFIRSTENTRY));
540 }
541
542 dumpHex(phOut,
543 pDir->aEntries[i].off + pb,
544 pDir->aEntries[i].cb);
545 break;
546 }
547
548 default:
549 fprintf(phOut, " Error - unknown entry type. (%x)\n", pDir->aEntries[i].usType);
550 }
551
552 }
553
554
555 /* - temporary - */
556 printf("\ndumps debug data\n");
557 dumpHex(phOut, pb, cb);
558
559 return NO_ERROR;
560}
561
562
563
564/**
565 * Dumps binary data to file handle.
566 * @param phOut Output file handle.
567 * @param pb Pointer to data.
568 * @param cb Count of bytes to dump.
569 */
570void dumpHex(FILE *phOut, PBYTE pb, int cb)
571{
572 int i;
573
574 for (i = 0; i < cb; i += 16)
575 {
576 int j;
577 /* write offset */
578 fprintf(phOut, "%08x ", i);
579
580 /* write data (hex value) */
581 for (j = 0; j < 16; j++)
582 {
583 int f = i + j < cb;
584 unsigned char uch = f ? pb[i + j] : 0;
585 if (j == 3 || j == 11)
586 fprintf(phOut, f ? "%02x-" : " -", uch);
587 else if (j == 7)
588 fprintf(phOut, f ? "%02x - " : " - ", uch);
589 else
590 fprintf(phOut, f ? "%02x " : " ", uch);
591 }
592 fprintf(phOut, " ");
593
594 /* write ASCII */
595 for (j = 0; j < 16; j++)
596 {
597 if (i + j < cb)
598 {
599 if (isprint(pb[i + j]))
600 fprintf(phOut, "%c", pb[i + j]);
601 else
602 fprintf(phOut, ".");
603 }
604 else
605 fprintf(phOut, " ");
606 }
607 fprintf(phOut, "\n");
608 }
609}
610
611
612
613/**
614 * Creates a memory buffer for a binary file.
615 * @returns Pointer to file memoryblock. NULL on error.
616 * @param pszFilename Pointer to filename string.
617 * @remark This function is the one using most of the execution
618 * time (DosRead + DosOpen) - about 70% of the execution time!
619 */
620void *readfile(const char *pszFilename)
621{
622 void *pvFile = NULL;
623 FILE *phFile;
624
625 phFile = fopen(pszFilename, "rb");
626 if (phFile != NULL)
627 {
628 signed long cbFile = fsize(phFile);
629 if (cbFile > 0)
630 {
631 pvFile = malloc(cbFile + 1);
632 if (pvFile != NULL)
633 {
634 memset(pvFile, 0, cbFile + 1);
635 if (fread(pvFile, 1, cbFile, phFile) == 0)
636 { /* failed! */
637 free(pvFile);
638 pvFile = NULL;
639 }
640 }
641 else
642 fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
643 }
644 fclose(phFile);
645 }
646 return pvFile;
647}
648
649
650
651/**
652 * Find the size of a file.
653 * @returns Size of file. -1 on error.
654 * @param phFile File handle.
655 */
656signed long fsize(FILE *phFile)
657{
658 int ipos;
659 signed long cb;
660
661 if ((ipos = ftell(phFile)) < 0
662 ||
663 fseek(phFile, 0, SEEK_END) != 0
664 ||
665 (cb = ftell(phFile)) < 0
666 ||
667 fseek(phFile, ipos, SEEK_SET) != 0
668 )
669 cb = -1;
670 return cb;
671}
672
Note: See TracBrowser for help on using the repository browser.