Debian Codegraph @

src/bin/dwarfexample/simplereader.c

raw
1 /*
2 Copyright (c) 2009-2023 David Anderson. All rights reserved.
3
4 This example program is hereby
5 placed into the public domain to be copied or
6 used by anyone for any purpose without restriction.
7
8 */
9 /* simplereader.c
10 This is an example of code reading dwarf .debug_info.
11 It is kept simple to expose essential features.
12 Though it now has a bunch of special options to enable
13 testing of specific libdwarf features so it's no longer
14 all that simple...
15
16 It does not do all possible error reporting or error handling.
17 It does to a bit of error checking as a help in ensuring
18 that some code works properly... for error checks.
19
20 The --names
21 option adds some extra printing.
22
23 The --check
24 option does some interface and error checking.
25
26 Option new September 2016:
27 --dumpallnames=filepath
28 This causes all the strings from the .debug_info and .debug_types
29 sections to be written to 'filepath'. Any previous contents
30 of the file are wiped out.
31 This could be handy if you want to use the set of strings to
32 investigate ways to improve the density of strings in some way.
33
34 Options new 03 May 2015:
35 These options do something different.
36 They use specific DWARF5 package file libdwarf operations
37 as a way to ensure libdwarf works properly (these
38 specials used by the libdwarf regresson test suite).
39 Examples given assuming dwp object fissionb-ld-new.dwp
40 from the regressiontests
41 --tuhash=hashvalue
42 example: --tuhash=b0dd19898e8aa823
43 It prints a DIE.
44
45 --cuhash=hashvalue
46 example: --cuhash=1e9983f631642b1a
47 It prints a DIE.
48
49 --cufissionhash=hashvalue
50 example: --tufissionhash=1e9983f631642b1a
51 It prints the fission data for this hash.
52
53 --tufissionhash=hashvalue
54 example: --tufissionhash=b0dd19898e8aa823
55 It prints the fission data for this hash.
56
57 --fissionfordie=cunumber
58 example: --fissionfordie=5
59 For CU number 5 (0 is the initial CU/TU)
60 it accesses the CU/TU DIE and then
61 uses that DIE to get the fission data.
62
63 New January 2019:
64 --use-init-fd
65 Instead of using dwarf_init_path(), use
66 dwarf_init_fd() to make particular
67 tests of that interface.
68
69 To use, try
70 make
71 ./simplereader simplereader
72 */
73
74 #include <config.h>
75
76 #include <stdio.h> /* fprintf() printf() snprintf() */
77 #include <stdlib.h> /* atoi() exit() free() */
78 #include <string.h> /* memset() strcmp() strcpy() strdup()
79 strlen() strncmp() */
80
81 #ifdef HAVE_STDINT_H
82 #include <stdint.h> /* uintptr_t */
83 #endif /* HAVE_STDINT_H */
84
85 #ifdef _WIN32
86 #include <io.h> /* close() open() */
87 #elif defined HAVE_UNISTD_H
88 #include <unistd.h> /* close() */
89 #endif /* _WIN32 */
90
91 #ifdef HAVE_FCNTL_H
92 #include <fcntl.h> /* open() O_RDONLY */
93 #endif /* HAVE_FCNTL_H */
94
95 #include "dwarf.h"
96 #include "libdwarf.h"
97 #include "libdwarf_private.h"
98
99 #ifndef O_RDONLY
100 /* This is for a Windows environment */
101 # define O_RDONLY _O_RDONLY
102 #endif
103
104 #ifdef _O_BINARY
105 /* This is for a Windows environment */
106 #define O_BINARY _O_BINARY
107 #else
108 # ifndef O_BINARY
109 # define O_BINARY 0 /* So it does nothing in Linux/Unix */
110 # endif
111 #endif /* O_BINARY */
112
113 struct srcfilesdata {
114 char ** srcfiles;
115 Dwarf_Signed srcfilescount;
116 int srcfilesres;
117 };
118
119 static void read_cu_list(Dwarf_Debug dbg);
120 static void print_die_data(Dwarf_Debug dbg, Dwarf_Die print_me,
121 int level,
122 struct srcfilesdata *sf);
123 static void get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die,
124 int is_info, int in_level,
125 struct srcfilesdata *sf);
126 static void resetsrcfiles(Dwarf_Debug dbg,struct srcfilesdata *sf);
127
128 /* Use a generic call to open the file, due to issues with Windows */
129
130 static int namesoptionon = 0;
131 static int checkoptionon = 0;
132 static int dumpallnames = 0;
133 FILE *dumpallnamesfile = 0;
134 static const char * dumpallnamespath = 0;
135 #if 0
136 DW_UT_compile 0x01 /* DWARF5 */
137 DW_UT_type 0x02 /* DWARF5 */
138 DW_UT_partial 0x03 /* DWARF5 */
139 #endif
140
141 static int stdrun = TRUE;
142
143 static int unittype = DW_UT_compile;
144 static Dwarf_Bool g_is_info = TRUE;
145
146 int cu_version_stamp = 0;
147 int cu_offset_size = 0;
148
149 /* dienumberr is used to count DIEs.
150 The point is to match fissionfordie. */
151 static int dienumber = 0;
152 static int fissionfordie = -1;
153 static int passnullerror = 0;
154 /* These hash representations have to be converted to Dwarf_Sig8
155 before use. */
156 static const char * cuhash = 0;
157 static const char * tuhash = 0;
158 static const char * cufissionhash = 0;
159 static const char * tufissionhash = 0;
160
161 /* So we get clean reports from valgrind and other tools
162 we clean up strdup strings.
163 With a primitive technique as we need nothing fancy. */
164 #define DUPSTRARRAYSIZE 100
165 static const char *dupstrarray[DUPSTRARRAYSIZE];
166 static unsigned dupstrused;
167
168 static void
169 cleanupstr(void)
170 {
171 unsigned i = 0;
172 for (i = 0; i < dupstrused; ++i) {
173 free((char *)dupstrarray[i]);
174 dupstrarray[i] = 0;
175 }
176 dupstrused = 0;
177 }
178
179 static unsigned char_to_uns4bit(Dwarf_Debug dbg,unsigned char c)
180 {
181 unsigned v;
182 if ( c >= '0' && c <= '9') {
183 v = c - '0';
184 }
185 else if ( c >= 'a' && c <= 'f') {
186 v = c - 'a' + 10;
187 }
188 else if ( c >= 'A' && c <= 'F') {
189 v = c - 'A' + 10;
190 } else {
191 printf("ERROR Garbage hex char in %c 0x%x\n",c,c);
192 cleanupstr();
193 dwarf_finish(dbg);
194 exit(EXIT_FAILURE);
195 }
196 return v;
197 }
198
199 static void
200 xfrm_to_sig8(Dwarf_Debug dbg,
201 const char *cuhash_in,
202 Dwarf_Sig8 *hash_out)
203 {
204 char localhash[16];
205 unsigned hashin_len = strlen(cuhash_in);
206 unsigned fixed_size = sizeof(localhash);
207 unsigned init_byte = 0;
208 unsigned i;
209
210 memset(localhash,0,fixed_size);
211 if (hashin_len > fixed_size) {
212 printf("FAIL: argument hash too long, len %u val:\"%s\"\n",
213 hashin_len, cuhash_in);
214 dwarf_finish(dbg);
215 cleanupstr();
216 exit(EXIT_FAILURE);
217 }
218 if (hashin_len < fixed_size) {
219 unsigned add_zeros = fixed_size - hashin_len;
220 for ( ; add_zeros > 0; add_zeros--) {
221 localhash[init_byte] = '0';
222 init_byte++;
223 }
224 }
225 for (i = 0; i < hashin_len; ++i,++init_byte) {
226 localhash[init_byte] = cuhash_in[i];
227 }
228
229 /* So now local hash as a full 16 bytes of hex characters with
230 any needed leading zeros.
231 transform it to 8 byte hex signature */
232
233 for (i = 0; i < sizeof(Dwarf_Sig8) ; ++i) {
234 unsigned char hichar = localhash[2*i];
235 unsigned char lochar = localhash[2*i+1];
236 hash_out->signature[i] =
237 (char_to_uns4bit(dbg,hichar) << 4) |
238 char_to_uns4bit(dbg,lochar);
239 }
240 printf("Hex key = 0x");
241 for (i = 0; i < sizeof(Dwarf_Sig8) ; ++i) {
242 unsigned char c = hash_out->signature[i];
243 printf("%02x",c);
244 }
245 printf("\n");
246 }
247
248 static int
249 startswithextractnum(const char *arg,const char *lookfor, int *numout)
250 {
251 const char *s = 0;
252 unsigned prefixlen = strlen(lookfor);
253 int v = 0;
254 if (strncmp(arg,lookfor,prefixlen)) {
255 return FALSE;
256 }
257 s = arg+prefixlen;
258 /* We are not making any attempt to deal with garbage.
259 Pass in good data... */
260 v = atoi(s);
261 *numout = v;
262 return TRUE;
263 }
264
265 static int
266 startswithextractstring(const char *arg,const char *lookfor,
267 const char ** ptrout)
268 {
269 const char *s = 0;
270 unsigned prefixlen = strlen(lookfor);
271 if (strncmp(arg,lookfor,prefixlen)) {
272 return FALSE;
273 }
274 s = arg+prefixlen;
275 *ptrout = strdup(s);
276 dupstrarray[dupstrused] = *ptrout;
277 dupstrused++;
278 if (dupstrused >= DUPSTRARRAYSIZE) {
279 printf("FAIL: increase the value DUPSTRARRAYSIZE"
280 " for test purposes\n");
281 cleanupstr();
282 exit(EXIT_FAILURE);
283 }
284 return TRUE;
285 }
286
287 static void
288 format_sig8_string(Dwarf_Sig8*data, char* str_buf,unsigned
289 buf_size)
290 {
291 unsigned i = 0;
292 char *cp = str_buf;
293 if (buf_size < 19) {
294 printf("FAIL: internal coding error in test.\n");
295 cleanupstr();
296 exit(EXIT_FAILURE);
297 }
298 strcpy(cp,"0x");
299 cp += 2;
300 buf_size -= 2;
301 for (; i < sizeof(data->signature); ++i,cp+=2,buf_size--) {
302 snprintf(cp, buf_size, "%02x",
303 (unsigned char)(data->signature[i]));
304 }
305 return;
306 }
307
308 static void
309 print_debug_fission_header(struct Dwarf_Debug_Fission_Per_CU_s *fsd)
310 {
311 const char * fissionsec = ".debug_cu_index";
312 unsigned i = 0;
313 char str_buf[30];
314
315 if (!fsd || !fsd->pcu_type) {
316 /* No fission data. */
317 return;
318 }
319 printf("\n");
320 if (!strcmp(fsd->pcu_type,"tu")) {
321 fissionsec = ".debug_tu_index";
322 }
323 printf(" %-19s = %s\n","Fission section",fissionsec);
324 printf(" %-19s = 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
325 "Fission index ",
326 fsd->pcu_index);
327 format_sig8_string(&fsd->pcu_hash,str_buf,sizeof(str_buf));
328 printf(" %-19s = %s\n","Fission hash",str_buf);
329 /* 0 is always unused. Skip it. */
330 printf(" %-19s = %s\n","Fission entries","offset "
331 "size DW_SECTn");
332 for ( i = 1; i < DW_FISSION_SECT_COUNT; ++i) {
333 const char *nstring = 0;
334 Dwarf_Unsigned off = 0;
335 Dwarf_Unsigned size = fsd->pcu_size[i];
336 int res = 0;
337 if (size == 0) {
338 continue;
339 }
340 res = dwarf_get_SECT_name(i,&nstring);
341 if (res != DW_DLV_OK) {
342 nstring = "Unknown SECT";
343 }
344 off = fsd->pcu_offset[i];
345 printf(" %-19s = 0x%" DW_PR_XZEROS DW_PR_DUx
346 " 0x%" DW_PR_XZEROS DW_PR_DUx " %2u\n",
347 nstring,
348 off,
349 size,i);
350 }
351 }
352
353 /* If there is no 'error' passed into a dwarf function
354 and there is an error, and an error-handler like this
355 is passed. This example simply returns so we
356 test how well that action works. */
357 static void
358 simple_error_handler(Dwarf_Error error,
359 Dwarf_Ptr errarg)
360 {
361 Dwarf_Unsigned earg = (Dwarf_Unsigned)(uintptr_t)errarg;
362
363 (void)errarg;
364 printf("\nlibdwarf error detected: 0x%" DW_PR_DUx " %s\n",
365 dwarf_errno(error),dwarf_errmsg(error));
366 printf("libdwarf errarg. %" DW_PR_DUu "\n", earg);
367 return;
368 }
369
370 int
371 main(int argc, char **argv)
372 {
373 Dwarf_Debug dbg = 0;
374 const char *filepath = 0;
375 int use_init_fd = FALSE;
376 int my_init_fd = 0;
377 int res = DW_DLV_ERROR;
378 Dwarf_Error error;
379 Dwarf_Handler errhand = 0;
380 Dwarf_Ptr errarg = 0;
381 Dwarf_Sig8 hash8;
382 Dwarf_Error *errp = 0;
383 int simpleerrhand = 0;
384 int i = 0;
385 #define MACHO_PATH_LEN 2000
386 char macho_real_path[MACHO_PATH_LEN];
387
388 macho_real_path[0] = 0;
389 for (i = 1; i < (argc-1) ; ++i) {
390 if (strcmp(argv[i],"--names") == 0) {
391 namesoptionon=1;
392 continue;
393 }
394 if (startswithextractstring(argv[1],"--dumpallnames=",
395 &dumpallnamespath)) {
396 dumpallnames=1;
397 continue;
398 }
399 if (strcmp(argv[i],"--check") == 0) {
400 checkoptionon=1;
401 continue;
402 }
403 if (startswithextractstring(argv[i],
404 "--tuhash=",&tuhash)) {
405 continue;
406 }
407 if (startswithextractstring(argv[i],
408 "--cuhash=",&cuhash)) {
409 continue;
410 }
411 if (startswithextractstring(argv[i],
412 "--tufissionhash=", &tufissionhash)) {
413 continue;
414 }
415 if (startswithextractstring(argv[i],
416 "--cufissionhash=",
417 &cufissionhash)) {
418 continue;
419 }
420 if (strcmp(argv[i],"--passnullerror") == 0) {
421 passnullerror=1;
422 /* If null error, we cannot do much.
423 Do not expect to avoid a like without
424 help from libdwarf. */
425 dwarf_set_de_alloc_flag(TRUE);
426 continue;
427 }
428 if (strcmp(argv[i],"--simpleerrhand") == 0) {
429 simpleerrhand=1;
430 continue;
431 }
432 if (startswithextractnum(argv[i],
433 "--isinfo=",&g_is_info)) {
434 continue;
435 }
436 if (startswithextractnum(argv[i],
437 "--type=",&unittype)) {
438 continue;
439 }
440 if (startswithextractnum(argv[i],
441 "--fissionfordie=",
442 &fissionfordie)) {
443 continue;
444 }
445 if (!strcmp(argv[i],"--use-init-fd")) {
446 use_init_fd = TRUE;
447 /* done */
448 }
449 if (!strcmp(argv[i],"--suppress-de-alloc-tree")) {
450 /* Never allow the call to suppress
451 alloc tracking in libdwarf as without normal
452 alloc tracking this source will have
453 leaks. Especially on corrupt DWARF or
454 corrupt Elf.
455 Just accept the option and continue. */
456 continue;
457 }
458 }
459 if (i >= argc) {
460 printf("simplereader not given file to open\n");
461 printf("simplereader exits\n");
462 cleanupstr();
463 exit(EXIT_FAILURE);
464 }
465 filepath = argv[i];
466 if (dumpallnames) {
467 if (!strcmp(dumpallnamespath,filepath)) {
468 printf("Using --dumpallnames with the same path "
469 "(%s) "
470 "as the file to read is not allowed. giving up.\n",
471 filepath);
472 cleanupstr();
473 exit(EXIT_FAILURE);
474 }
475 dumpallnamesfile = fopen(dumpallnamespath,"w");
476 if (!dumpallnamesfile) {
477 printf("Cannot open %s. Giving up.\n",
478 dumpallnamespath);
479 cleanupstr();
480 exit(EXIT_FAILURE);
481 }
482 }
483 if (passnullerror) {
484 errp = 0;
485 } else {
486 errp = &error;
487 }
488 if (simpleerrhand) {
489 errhand = simple_error_handler;
490 /* Not a very useful errarg... */
491 errarg = (Dwarf_Ptr)1;
492 }
493 if (use_init_fd) {
494 /* For testing a libdwarf init function.
495 We are not finding the true dSYM Macho-object
496 here if that applies, so it's up to the user
497 of simplereader to pass in the correct
498 dSYM object in the dSYM case.
499 dwarf_object_detector_path() could do
500 the dSYM object finding, but to keep this simple
501 we leave that to the reader. */
502 my_init_fd = open(filepath,O_RDONLY|O_BINARY);
503 if (my_init_fd == -1) {
504 printf("Giving up, cannot open %s\n",filepath);
505 cleanupstr();
506 exit(EXIT_FAILURE);
507 }
508 res = dwarf_init_b(my_init_fd,
509 DW_GROUPNUMBER_ANY,
510 errhand,errarg,&dbg,errp);
511 } else {
512 res = dwarf_init_path(filepath,
513 macho_real_path,
514 MACHO_PATH_LEN,
515 DW_GROUPNUMBER_ANY,errhand,errarg,&dbg, errp);
516 }
517 if (res != DW_DLV_OK) {
518 if (res == DW_DLV_ERROR) {
519 if (errp) {
520 dwarf_dealloc_error(dbg,*errp);
521 }
522 error = 0;
523 }
524 printf("Giving up, cannot do DWARF processing %s\n",
525 filepath?filepath:"");
526 cleanupstr();
527 dwarf_finish(dbg);
528 exit(EXIT_FAILURE);
529 }
530
531 if (cuhash) {
532 Dwarf_Die die;
533 stdrun = FALSE;
534 xfrm_to_sig8(dbg,cuhash,&hash8);
535 printf("\n");
536 printf("Getting die for CU key %s\n",cuhash);
537 res = dwarf_die_from_hash_signature(dbg,
538 &hash8,"cu",
539 &die,errp);
540 if (res == DW_DLV_OK) {
541 struct srcfilesdata sf;
542 printf("Hash found.\n");
543 sf.srcfilesres = DW_DLV_ERROR;
544 sf.srcfiles = 0;
545 sf.srcfilescount = 0;
546 print_die_data(dbg,die,0,&sf);
547 dwarf_dealloc(dbg,die, DW_DLA_DIE);
548 } else if (res == DW_DLV_NO_ENTRY) {
549 printf("cuhash DW_DLV_NO_ENTRY.\n");
550 } else { /* DW_DLV_ERROR */
551 printf("cuhash DW_DLV_ERROR %s\n",
552 errp? dwarf_errmsg(error):"an error");
553 }
554 }
555 if (tuhash) {
556 Dwarf_Die die;
557 stdrun = FALSE;
558 xfrm_to_sig8(dbg,tuhash,&hash8);
559 printf("\n");
560 printf("Getting die for TU key %s\n",tuhash);
561 res = dwarf_die_from_hash_signature(dbg,
562 &hash8,"tu",
563 &die,errp);
564 if (res == DW_DLV_OK) {
565 struct srcfilesdata sf;
566 printf("Hash found.\n");
567 sf.srcfilesres = DW_DLV_ERROR;
568 sf.srcfiles = 0;
569 sf.srcfilescount = 0;
570 print_die_data(dbg,die,0,&sf);
571 dwarf_dealloc(dbg,die, DW_DLA_DIE);
572 } else if (res == DW_DLV_NO_ENTRY) {
573 printf("tuhash DW_DLV_NO_ENTRY.\n");
574 } else { /* DW_DLV_ERROR */
575 printf("tuhash DW_DLV_ERROR %s\n",
576 errp?dwarf_errmsg(error):"error!");
577 }
578 }
579 if (cufissionhash) {
580 Dwarf_Debug_Fission_Per_CU fisdata;
581 stdrun = FALSE;
582 memset(&fisdata,0,sizeof(fisdata));
583 xfrm_to_sig8(dbg,cufissionhash,&hash8);
584 printf("\n");
585 printf("Getting fission data for CU key %s\n",cufissionhash);
586 res = dwarf_get_debugfission_for_key(dbg,
587 &hash8,"cu",
588 &fisdata,errp);
589 if (res == DW_DLV_OK) {
590 printf("Hash found.\n");
591 print_debug_fission_header(&fisdata);
592 } else if (res == DW_DLV_NO_ENTRY) {
593 printf("cufissionhash DW_DLV_NO_ENTRY.\n");
594 } else { /* DW_DLV_ERROR */
595 printf("cufissionhash DW_DLV_ERROR %s\n",
596 errp?dwarf_errmsg(error):"error...");
597 }
598 }
599 if (tufissionhash) {
600 Dwarf_Debug_Fission_Per_CU fisdata;
601 stdrun = FALSE;
602 memset(&fisdata,0,sizeof(fisdata));
603 xfrm_to_sig8(dbg,tufissionhash,&hash8);
604 printf("\n");
605 printf("Getting fission data for TU key %s\n",tufissionhash);
606 res = dwarf_get_debugfission_for_key(dbg,
607 &hash8,"tu",
608 &fisdata,errp);
609 if (res == DW_DLV_OK) {
610 printf("Hash found.\n");
611 print_debug_fission_header(&fisdata);
612 } else if (res == DW_DLV_NO_ENTRY) {
613 printf("tufissionhash DW_DLV_NO_ENTRY.\n");
614 } else { /* DW_DLV_ERROR */
615 printf("tufissionhash DW_DLV_ERROR %s\n",
616 errp?dwarf_errmsg(error):" Some error");
617 }
618 }
619 if (stdrun) {
620 read_cu_list(dbg);
621 }
622 res = dwarf_finish(dbg);
623 if (res != DW_DLV_OK) {
624 printf("dwarf_finish failed!\n");
625 }
626 if (use_init_fd) {
627 close(my_init_fd);
628 }
629 if (dumpallnamesfile) {
630 fclose(dumpallnamesfile);
631 }
632 cleanupstr();
633 return 0;
634 }
635
636 static void
637 read_cu_list(Dwarf_Debug dbg)
638 {
639 Dwarf_Unsigned cu_header_length = 0;
640 Dwarf_Unsigned abbrev_offset = 0;
641 Dwarf_Half address_size = 0;
642 Dwarf_Half version_stamp = 0;
643 Dwarf_Half offset_size = 0;
644 Dwarf_Half extension_size = 0;
645 Dwarf_Sig8 signature;
646 Dwarf_Unsigned typeoffset = 0;
647 Dwarf_Unsigned next_cu_header = 0;
648 Dwarf_Half header_cu_type = unittype;
649 Dwarf_Bool is_info = g_is_info;
650 Dwarf_Error error;
651 Dwarf_Error *errp = 0;
652
653 for (;;) {
654 Dwarf_Die no_die = 0;
655 Dwarf_Die cu_die = 0;
656 int res = DW_DLV_ERROR;
657 struct srcfilesdata sf;
658 sf.srcfilesres = DW_DLV_ERROR;
659 sf.srcfiles = 0;
660 sf.srcfilescount = 0;
661 memset(&signature,0, sizeof(signature));
662
663 if (passnullerror) {
664 errp = 0;
665 } else {
666 errp = &error;
667 }
668 res = dwarf_next_cu_header_d(dbg,is_info,&cu_header_length,
669 &version_stamp, &abbrev_offset,
670 &address_size, &offset_size,
671 &extension_size,&signature,
672 &typeoffset, &next_cu_header,
673 &header_cu_type,errp);
674 if (res == DW_DLV_ERROR) {
675 char *em = errp?dwarf_errmsg(*errp):
676 "An error next cu her";
677 printf("Error in dwarf_next_cu_header: %s\n",em);
678 if (errp) {
679 dwarf_dealloc_error(dbg,*errp);
680 }
681 dwarf_finish(dbg);
682 cleanupstr();
683 exit(EXIT_FAILURE);
684 }
685 if (res == DW_DLV_NO_ENTRY) {
686 /* Done. */
687 return;
688 }
689 cu_version_stamp = version_stamp;
690 cu_offset_size = offset_size;
691 /* The CU will have a single sibling, a cu_die. */
692 res = dwarf_siblingof_b(dbg,no_die,is_info,
693 &cu_die,errp);
694 if (res == DW_DLV_ERROR) {
695 char *em = (errp?dwarf_errmsg(*errp):"An error");
696 printf("Error in dwarf_siblingof_b on CU die: %s\n",em);
697 if (errp) {
698 dwarf_dealloc_error(dbg,*errp);
699 }
700 dwarf_finish(dbg);
701 cleanupstr();
702 exit(EXIT_FAILURE);
703 }
704 if (res == DW_DLV_NO_ENTRY) {
705 /* Impossible case. */
706 printf("no entry! in dwarf_siblingof on CU die \n");
707 dwarf_finish(dbg);
708 cleanupstr();
709 exit(EXIT_FAILURE);
710 }
711 get_die_and_siblings(dbg,cu_die,is_info,0,&sf);
712 dwarf_dealloc_die(cu_die);
713 resetsrcfiles(dbg,&sf);
714 }
715 }
716
717 static void
718 get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die,
719 int is_info,int in_level,
720 struct srcfilesdata *sf)
721 {
722 int res = DW_DLV_ERROR;
723 Dwarf_Die cur_die=in_die;
724 Dwarf_Die child = 0;
725 Dwarf_Error error = 0;
726 Dwarf_Error *errp = 0;
727
728 if (passnullerror) {
729 errp = 0;
730 } else {
731 errp = &error;
732 }
733 print_die_data(dbg,in_die,in_level,sf);
734
735 for (;;) {
736 Dwarf_Die sib_die = 0;
737 res = dwarf_child(cur_die,&child,errp);
738 if (res == DW_DLV_ERROR) {
739 printf("Error in dwarf_child , level %d \n",in_level);
740 dwarf_dealloc_die(cur_die);
741 if (errp) {
742 dwarf_dealloc_error(dbg,*errp);
743 }
744 dwarf_finish(dbg);
745 cleanupstr();
746 exit(EXIT_FAILURE);
747 }
748 if (res == DW_DLV_OK) {
749 get_die_and_siblings(dbg,child,is_info,
750 in_level+1,sf);
751 /* No longer need 'child' die. */
752 dwarf_dealloc_die(child);
753 child = 0;
754 }
755 /* res == DW_DLV_NO_ENTRY or DW_DLV_OK */
756 res = dwarf_siblingof_b(dbg,cur_die,is_info,&sib_die,errp);
757 if (res == DW_DLV_ERROR) {
758 char *em = errp?dwarf_errmsg(error):"Error siblingof_b";
759 printf("Error in dwarf_siblingof_b , level %d :%s \n",
760 in_level,em);
761 dwarf_finish(dbg);
762 cleanupstr();
763 exit(EXIT_FAILURE);
764 }
765 if (res == DW_DLV_NO_ENTRY) {
766 /* Done at this level. */
767 break;
768 }
769 /* res == DW_DLV_OK */
770 if (cur_die != in_die) {
771 dwarf_dealloc_die(cur_die);
772 }
773 cur_die = sib_die;
774 print_die_data(dbg,cur_die,in_level,sf);
775 }
776 return;
777 }
778 static void
779 get_addr(Dwarf_Attribute attr,Dwarf_Addr *val)
780 {
781 Dwarf_Error error = 0;
782 int res;
783 Dwarf_Addr uval = 0;
784 Dwarf_Error *errp = 0;
785
786 if (passnullerror) {
787 errp = 0;
788 } else {
789 errp = &error;
790 }
791
792 res = dwarf_formaddr(attr,&uval,errp);
793 if (res == DW_DLV_OK) {
794 *val = uval;
795 return;
796 }
797 return;
798 }
799 static void
800 get_number(Dwarf_Attribute attr,Dwarf_Unsigned *val)
801 {
802 Dwarf_Error error = 0;
803 int res;
804 Dwarf_Signed sval = 0;
805 Dwarf_Unsigned uval = 0;
806 Dwarf_Error *errp = 0;
807
808 if (passnullerror) {
809 errp = 0;
810 } else {
811 errp = &error;
812 }
813 res = dwarf_formudata(attr,&uval,errp);
814 if (res == DW_DLV_OK) {
815 *val = uval;
816 return;
817 }
818 res = dwarf_formsdata(attr,&sval,errp);
819 if (res == DW_DLV_OK) {
820 *val = sval;
821 return;
822 }
823 return;
824 }
825 static void
826 print_subprog(Dwarf_Debug dbg,Dwarf_Die die,
827 int level,
828 struct srcfilesdata *sf,
829 const char *name)
830 {
831 int res;
832 Dwarf_Error error = 0;
833 Dwarf_Attribute *attrbuf = 0;
834 Dwarf_Addr lowpc = 0;
835 Dwarf_Addr highpc = 0;
836 Dwarf_Signed attrcount = 0;
837 Dwarf_Signed i;
838 Dwarf_Unsigned filenum = 0;
839 Dwarf_Unsigned linenum = 0;
840 char *filename = 0;
841 Dwarf_Error *errp = 0;
842
843 if (passnullerror) {
844 errp = 0;
845 } else {
846 errp = &error;
847 }
848 res = dwarf_attrlist(die,&attrbuf,&attrcount,errp);
849 if (res != DW_DLV_OK) {
850 if (res == DW_DLV_ERROR) {
851 if (errp) {
852 dwarf_dealloc_error(dbg,error);
853 error = 0;
854 dwarf_finish(dbg);
855 cleanupstr();
856 exit(EXIT_FAILURE);
857 }
858 }
859 return;
860 }
861 for (i = 0; i < attrcount ; ++i) {
862 Dwarf_Half aform;
863 res = dwarf_whatattr(attrbuf[i],&aform,errp);
864 if (res == DW_DLV_OK) {
865 if (aform == DW_AT_decl_file) {
866 Dwarf_Signed filenum_s = 0;
867
868 get_number(attrbuf[i],&filenum);
869 filenum_s = filenum;
870 /* Would be good to evaluate filenum_s
871 sanity here, ensuring filenum_s-1 is sensible. */
872 if ((filenum > 0) &&
873 (sf->srcfilescount > (filenum_s-1))) {
874 filename = sf->srcfiles[filenum_s-1];
875 }
876 }
877 if (aform == DW_AT_decl_line) {
878 get_number(attrbuf[i],&linenum);
879 }
880 if (aform == DW_AT_low_pc) {
881 get_addr(attrbuf[i],&lowpc);
882 }
883 if (aform == DW_AT_high_pc) {
884 /* This will FAIL with DWARF4 highpc form
885 of 'class constant'. */
886 get_addr(attrbuf[i],&highpc);
887 }
888 }
889 if (res == DW_DLV_ERROR && errp) {
890 dwarf_dealloc_error(dbg,error);
891 error = 0;
892 }
893 dwarf_dealloc_attribute(attrbuf[i]);
894 }
895 /* Here let's test some alternative interfaces for
896 high and low pc. */
897 if (checkoptionon){
898 int hresb = 0;
899 int lres = 0;
900 Dwarf_Addr hipcoffset = 0;
901 Dwarf_Addr althipcb = 0;
902 Dwarf_Addr altlopc = 0;
903 Dwarf_Half highform = 0;
904 enum Dwarf_Form_Class highclass = 0;
905
906 /* Should work for all DWARF DW_AT_high_pc. */
907 hresb = dwarf_highpc_b(die,&althipcb,&highform,
908 &highclass,errp);
909 lres = dwarf_lowpc(die,&altlopc,errp);
910 printf("high_pc checking %s ",name);
911 if (hresb == DW_DLV_OK) {
912 /* present, FORM addr or const. */
913 if (highform == DW_FORM_addr) {
914 printf("highpcb 0x%" DW_PR_XZEROS DW_PR_DUx " ",
915 althipcb);
916 } else {
917 if (lres == DW_DLV_OK) {
918 hipcoffset = althipcb;
919 althipcb = altlopc + hipcoffset;
920 printf("highpcb 0x%" DW_PR_XZEROS DW_PR_DUx " "
921 "highoff 0x%" DW_PR_XZEROS DW_PR_DUx " ",
922 althipcb,hipcoffset);
923 } else {
924 printf("highoff 0x%" DW_PR_XZEROS DW_PR_DUx " ",
925 althipcb);
926 }
927 }
928 } else if (hresb == DW_DLV_ERROR) {
929 printf("dwarf_highpc_b() error!");
930 } else {
931 /* absent */
932 }
933
934 /* Should work for all DWARF DW_AT_low_pc */
935 if (lres == DW_DLV_OK) {
936 /* present, FORM addr. */
937 printf("lowpc 0x%" DW_PR_XZEROS DW_PR_DUx " ",
938 altlopc);
939 } else if (lres == DW_DLV_ERROR) {
940 printf("dwarf_lowpc() error!");
941 } else {
942 /* absent. */
943 }
944 printf("\n");
945
946 }
947 if (namesoptionon && (filenum || linenum)) {
948 printf("<%3d> file: %" DW_PR_DUu " %s line %"
949 DW_PR_DUu "\n",level,filenum,filename?filename:"",
950 linenum);
951 }
952 if (namesoptionon && lowpc) {
953 printf("<%3d> low_pc : 0x%" DW_PR_DUx "\n",
954 level, (Dwarf_Unsigned)lowpc);
955 }
956 if (namesoptionon && highpc) {
957 printf("<%3d> high_pc: 0x%" DW_PR_DUx "\n",
958 level, (Dwarf_Unsigned)highpc);
959 }
960 dwarf_dealloc(dbg,attrbuf,DW_DLA_LIST);
961 }
962
963 static void
964 print_comp_dir(Dwarf_Debug dbg,Dwarf_Die die,
965 int level, struct srcfilesdata *sf)
966 {
967 int res;
968 Dwarf_Error error = 0;
969 Dwarf_Attribute *attrbuf = 0;
970 Dwarf_Signed attrcount = 0;
971 Dwarf_Signed i;
972 Dwarf_Error *errp = 0;
973
974 if (passnullerror) {
975 errp = 0;
976 } else {
977 errp = &error;
978 }
979
980 res = dwarf_attrlist(die,&attrbuf,&attrcount,errp);
981 if (res != DW_DLV_OK) {
982 return;
983 }
984 sf->srcfilesres = dwarf_srcfiles(die,&sf->srcfiles,
985 &sf->srcfilescount,
986 &error);
987 for (i = 0; i < attrcount ; ++i) {
988 Dwarf_Half aform;
989 res = dwarf_whatattr(attrbuf[i],&aform,errp);
990 if (res == DW_DLV_OK) {
991 if (aform == DW_AT_comp_dir) {
992 char *name = 0;
993 res = dwarf_formstring(attrbuf[i],&name,errp);
994 if (res == DW_DLV_OK) {
995 printf("<%3d> compilation directory : \"%s\"\n",
996 level,name);
997 }
998 }
999 if (aform == DW_AT_stmt_list) {
1000 /* Offset of stmt list for this CU in .debug_line */
1001 }
1002 }
1003 dwarf_dealloc(dbg,attrbuf[i],DW_DLA_ATTR);
1004 }
1005 dwarf_dealloc(dbg,attrbuf,DW_DLA_LIST);
1006 }
1007
1008 static void
1009 resetsrcfiles(Dwarf_Debug dbg,struct srcfilesdata *sf)
1010 {
1011 Dwarf_Signed sri = 0;
1012 if (sf->srcfiles) {
1013 for (sri = 0; sri < sf->srcfilescount; ++sri) {
1014 dwarf_dealloc(dbg, sf->srcfiles[sri],
1015 DW_DLA_STRING);
1016 }
1017 dwarf_dealloc(dbg, sf->srcfiles, DW_DLA_LIST);
1018 }
1019 sf->srcfilesres = DW_DLV_ERROR;
1020 sf->srcfiles = 0;
1021 sf->srcfilescount = 0;
1022 }
1023
1024 static void
1025 print_single_string(Dwarf_Debug dbg, Dwarf_Die die,
1026 Dwarf_Half attrnum)
1027 {
1028 int res = 0;
1029 Dwarf_Error error = 0;
1030 char * stringval = 0;
1031
1032 res = dwarf_die_text(die,attrnum, &stringval,&error);
1033 if (res == DW_DLV_OK) {
1034 fprintf(dumpallnamesfile,"%s\n",stringval);
1035 dwarf_dealloc(dbg,stringval, DW_DLA_STRING);
1036 }
1037 return;
1038 }
1039
1040 static void
1041 print_name_strings_attr(Dwarf_Debug dbg, Dwarf_Die die,
1042 Dwarf_Attribute attr)
1043 {
1044 int res = 0;
1045 Dwarf_Half attrnum = 0;
1046 Dwarf_Half finalform = 0;
1047 enum Dwarf_Form_Class cl = DW_FORM_CLASS_UNKNOWN;
1048 Dwarf_Error error = 0;
1049
1050 res = dwarf_whatattr(attr,&attrnum,&error);
1051 if (res != DW_DLV_OK) {
1052 if (res == DW_DLV_ERROR) {
1053 dwarf_dealloc_error(dbg,error);
1054 }
1055 printf("Unable to get attr number");
1056 dwarf_finish(dbg);
1057 cleanupstr();
1058 exit(EXIT_FAILURE);
1059 }
1060
1061 res = dwarf_whatform(attr,&finalform,&error);
1062 if (res != DW_DLV_OK) {
1063 if (res == DW_DLV_ERROR) {
1064 dwarf_dealloc_error(dbg,error);
1065 }
1066 printf("Unable to get attr form");
1067 dwarf_finish(dbg);
1068 cleanupstr();
1069 exit(EXIT_FAILURE);
1070 }
1071
1072 cl = dwarf_get_form_class(cu_version_stamp,
1073 attrnum,cu_offset_size,finalform);
1074
1075 if (cl != DW_FORM_CLASS_STRING) {
1076 return;
1077 }
1078 print_single_string(dbg,die,attrnum);
1079 }
1080
1081 static void
1082 printnamestrings(Dwarf_Debug dbg, Dwarf_Die die)
1083 {
1084 Dwarf_Error error =0;
1085 Dwarf_Attribute *atlist = 0;
1086 Dwarf_Signed atcount = 0;
1087 Dwarf_Signed i = 0;
1088 int res = 0;
1089
1090 res = dwarf_attrlist(die,&atlist, &atcount,&error);
1091 if (res != DW_DLV_OK) {
1092 if (res == DW_DLV_ERROR) {
1093 dwarf_dealloc_error(dbg,error);
1094 }
1095 return;
1096 }
1097 for (i = 0; i < atcount; ++i) {
1098 Dwarf_Attribute attr = atlist[i];
1099 /* Use an empty attr to get a placeholder on
1100 the attr list for this IRDie. */
1101 print_name_strings_attr(dbg,die,attr);
1102 }
1103 dwarf_dealloc(dbg,atlist, DW_DLA_LIST);
1104 }
1105
1106 static void
1107 print_die_data_i(Dwarf_Debug dbg, Dwarf_Die print_me,
1108 int level,
1109 struct srcfilesdata *sf)
1110 {
1111 char *name = 0;
1112 Dwarf_Error error = 0;
1113 Dwarf_Half tag = 0;
1114 const char *tagname = 0;
1115 int res = 0;
1116 Dwarf_Error *errp = 0;
1117 Dwarf_Attribute attr = 0;
1118 Dwarf_Half formnum = 0;
1119 const char *formname = 0;
1120
1121 if (passnullerror) {
1122 errp = 0;
1123 } else {
1124 errp = &error;
1125 }
1126 res = dwarf_diename(print_me,&name,errp);
1127 if (res == DW_DLV_ERROR) {
1128 printf("Error in dwarf_diename , level %d \n",level);
1129 dwarf_finish(dbg);
1130 cleanupstr();
1131 exit(EXIT_FAILURE);
1132 }
1133 if (res == DW_DLV_NO_ENTRY) {
1134 name = "<no DW_AT_name attr>";
1135 }
1136 res = dwarf_tag(print_me,&tag,errp);
1137 if (res != DW_DLV_OK) {
1138 printf("Error in dwarf_tag , level %d \n",level);
1139 if (res == DW_DLV_ERROR && errp) {
1140 dwarf_dealloc_error(dbg,*errp);
1141 }
1142 cleanupstr();
1143 dwarf_finish(dbg);
1144 exit(EXIT_FAILURE);
1145 }
1146 res = dwarf_get_TAG_name(tag,&tagname);
1147 if (res != DW_DLV_OK) {
1148 if (res == DW_DLV_ERROR && errp) {
1149 dwarf_dealloc_error(dbg,*errp);
1150 }
1151 printf("Error in dwarf_get_TAG_name , level %d \n",level);
1152 cleanupstr();
1153 dwarf_finish(dbg);
1154 exit(EXIT_FAILURE);
1155 }
1156 if (dumpallnames) {
1157 printnamestrings(dbg,print_me);
1158 }
1159 res = dwarf_attr(print_me,DW_AT_name,&attr, errp);
1160 if (res != DW_DLV_OK) {
1161 /* do nothing */
1162 } else {
1163 res = dwarf_whatform(attr,&formnum,errp);
1164 if (res != DW_DLV_OK) {
1165 if (res == DW_DLV_ERROR && errp) {
1166 dwarf_dealloc_error(dbg,*errp);
1167 }
1168 printf("Error in dwarf_whatform , level %d \n",level);
1169 dwarf_finish(dbg);
1170 cleanupstr();
1171 exit(EXIT_FAILURE);
1172 }
1173 formname = "form-name-unavailable";
1174 res = dwarf_get_FORM_name(formnum,&formname);
1175 if (res != DW_DLV_OK) {
1176 formname = "UNKNoWn FORM!";
1177 }
1178 dwarf_dealloc(dbg,attr,DW_DLA_ATTR);
1179 }
1180
1181 if (namesoptionon ||checkoptionon) {
1182 if ( tag == DW_TAG_subprogram) {
1183 if (namesoptionon) {
1184 printf("<%3d> subprogram : \"%s\"\n",
1185 level,name);
1186 }
1187 print_subprog(dbg,print_me,level,sf,name);
1188 }
1189 if ( (namesoptionon) && (tag == DW_TAG_compile_unit ||
1190 tag == DW_TAG_partial_unit ||
1191 tag == DW_TAG_type_unit)) {
1192
1193 resetsrcfiles(dbg,sf);
1194 printf("<%3d> source file : \"%s\"\n",
1195 level,name);
1196 print_comp_dir(dbg,print_me,level,sf);
1197 }
1198 } else {
1199 printf("<%d> tag: %d %s name: \"%s\"",level,tag,
1200 tagname,name);
1201 if (formname) {
1202 printf(" FORM 0x%x \"%s\"",formnum, formname);
1203 }
1204 printf("\n");
1205 }
1206 /* This dwarf_dealloc was always wrong but
1207 before March 14, 2020 the documentation said
1208 the dwarf_dealloc was necessary.
1209 dwarf_dealloc(dbg,name,DW_DLA_STRING); */
1210
1211 }
1212
1213 static void
1214 print_die_data(Dwarf_Debug dbg, Dwarf_Die print_me,
1215 int level,
1216 struct srcfilesdata *sf)
1217 {
1218
1219 if (fissionfordie != -1) {
1220 Dwarf_Debug_Fission_Per_CU percu;
1221 memset(&percu,0,sizeof(percu));
1222 if (fissionfordie == dienumber) {
1223 int res = 0;
1224 Dwarf_Error error = 0;
1225 Dwarf_Error *errp = 0;
1226
1227 if (passnullerror) {
1228 errp = 0;
1229 } else {
1230 errp = &error;
1231 }
1232 res = dwarf_get_debugfission_for_die(print_me,
1233 &percu,errp);
1234 if (res == DW_DLV_ERROR) {
1235 printf("FAIL: Error in "
1236 "dwarf_get_debugfission_for_die %d\n",
1237 fissionfordie);
1238 if (errp) {
1239 dwarf_dealloc_error(dbg,*errp);
1240 }
1241 dwarf_finish(dbg);
1242 cleanupstr();
1243 exit(EXIT_FAILURE);
1244 }
1245 if (res == DW_DLV_NO_ENTRY) {
1246 printf("FAIL: no-entry in "
1247 "dwarf_get_debugfission_for_die %d\n",
1248 fissionfordie);
1249 dwarf_finish(dbg);
1250 cleanupstr();
1251 exit(EXIT_FAILURE);
1252 }
1253 print_die_data_i(dbg,print_me,level,sf);
1254 print_debug_fission_header(&percu);
1255 dwarf_finish(dbg);
1256 cleanupstr();
1257 exit(0);
1258 }
1259 dienumber++;
1260 return;
1261 }
1262 print_die_data_i(dbg,print_me,level,sf);
1263 dienumber++;
1264 }