Debian Codegraph @

src/bin/dwarfexample/findfuncbypc.c

raw
1 /*
2 Copyright (c) 2019 David Anderson.
3 This source code is placed in the Public Domain.
4 It may be copied or used in any way without restriction.
5 */
6 /* findfuncbypc.c
7 This is an example of code reading dwarf .debug_info.
8 It is kept simple to expose essential features.
9
10 It does not do all possible error reporting or error handling.
11 It does to a bit of error checking as a help in ensuring
12 that some code works properly... for error checks.
13
14 To use, try
15 make
16 ./findfuncbypc --pc=0x10000 ./findfuncbypc
17 */
18
19 #include <config.h>
20
21 #include <stdio.h> /* printf() */
22 #include <stdlib.h> /* exit() */
23 #include <string.h> /* memset() strcmp() strlen() strncmp() */
24
25 #include "dwarf.h"
26 #include "libdwarf.h"
27 #include "libdwarf_private.h"
28
29 struct srcfilesdata {
30 char ** srcfiles;
31 Dwarf_Signed srcfilescount;
32 int srcfilesres;
33 };
34 struct target_data_s {
35 Dwarf_Debug td_dbg;
36 Dwarf_Unsigned td_target_pc; /* from argv */
37 int td_print_details; /* from argv */
38 int td_reportallfound; /* from argv */
39
40 /* cu die data. */
41 Dwarf_Unsigned td_cu_lowpc;
42 Dwarf_Unsigned td_cu_highpc;
43 int td_cu_haslowhighpc;
44 Dwarf_Die td_cu_die;
45 char * td_cu_name;
46 char * td_cu_comp_dir;
47 Dwarf_Unsigned td_cu_number;
48 struct srcfilesdata td_cu_srcfiles;
49 /* Help deal with DW_AT_ranges */
50 /* This is base offset of ranges, the value from
51 DW_AT_rnglists_base. */
52 Dwarf_Unsigned td_cu_ranges_base;
53
54 /* Following subprog related. Offset has
55 tc_cu_ranges_base added in. */
56 Dwarf_Off td_ranges_offset;
57 /* DIE data of appropriate DIE */
58 /* From DW_AT_name */
59 char * td_subprog_name;
60 Dwarf_Unsigned td_subprog_fileindex;
61 Dwarf_Die td_subprog_die;
62 Dwarf_Unsigned td_subprog_lowpc;
63 Dwarf_Unsigned td_subprog_highpc;
64 int td_subprog_haslowhighpc;
65 Dwarf_Unsigned td_subprog_lineaddr;
66 Dwarf_Unsigned td_subprog_lineno;
67 char * td_subprog_srcfile; /* dealloc */
68 };
69 /* Adding return codes to DW_DLV, relevant to our purposes here. */
70 #define NOT_THIS_CU 10
71 #define IN_THIS_CU 11
72 #define FOUND_SUBPROG 12
73
74 static int look_for_our_target(Dwarf_Debug dbg,
75 struct target_data_s *target_data,
76 Dwarf_Error *errp);
77 static int examine_die_data(Dwarf_Debug dbg,
78 int is_info,Dwarf_Die die,
79 int level, struct target_data_s *td, Dwarf_Error *errp);
80 static int check_comp_dir(Dwarf_Debug dbg,Dwarf_Die die,
81 struct target_data_s *td,Dwarf_Error *errp);
82 static int get_die_and_siblings(Dwarf_Debug dbg,
83 Dwarf_Die in_die,
84 int is_info, int in_level,int cu_number,
85 struct target_data_s *td, Dwarf_Error *errp);
86
87 #if 0
88 DW_UT_compile 0x01 /* DWARF5 */
89 DW_UT_type 0x02 /* DWARF5 */
90 DW_UT_partial 0x03 /* DWARF5 */
91 #endif
92
93 static int unittype = DW_UT_compile;
94 static Dwarf_Bool g_is_info = TRUE;
95
96 int cu_version_stamp = 0;
97 int cu_offset_size = 0;
98
99 static void
100 printusage(void)
101 {
102 printf("Usage example: "
103 "./findfuncbypc --pc=0x10000 ./findfuncbypc\n");
104 printf(" options list:\n");
105 printf(" --help or -h prints this usage message and stops.\n");
106 printf(" --pc=(hex or decimal pc address)\n");
107 printf(" --printdetails \n");
108 printf(" prints some details of the discovery process\n");
109 printf(" --allinstances\n");
110 printf(" reports but does does not stop processing\n");
111 printf(" on finding pc address\n");
112 printf(" The argument following valid -- arguments must\n");
113 printf(" be a valid object file path\n");
114 }
115
116 static void target_data_destructor( struct target_data_s *td);
117
118 static int
119 startswithextractnum(const char *arg,
120 const char *lookfor,
121 Dwarf_Unsigned *numout)
122 {
123 const char *s = 0;
124 unsigned prefixlen = strlen(lookfor);
125 Dwarf_Unsigned v = 0;
126 char *endptr = 0;
127
128 if (strncmp(arg,lookfor,prefixlen)) {
129 return FALSE;
130 }
131 s = arg+prefixlen;
132 /* We are not making any attempt to deal with garbage.
133 Pass in good data... */
134 v = strtoul(s,&endptr,0);
135 if (!*s || *endptr) {
136 printf("Incoming argument in error: \"%s\"\n",arg);
137 return FALSE;
138 }
139 *numout = v;
140 return TRUE;
141 }
142
143 int
144 main(int argc, char **argv)
145 {
146 Dwarf_Debug dbg = 0;
147 const char *filepath = 0;
148 int res = DW_DLV_ERROR;
149 Dwarf_Error error;
150 Dwarf_Handler errhand = 0;
151 Dwarf_Ptr errarg = 0;
152 int i = 0;
153 Dwarf_Unsigned target_pc = 0;
154 #define PATH_LEN 2000
155 char real_path[PATH_LEN];
156 struct target_data_s target_data;
157
158 real_path[0] = 0;
159 memset(&target_data,0, sizeof(target_data));
160 for (i = 1; i < argc ; ++i) {
161 if (startswithextractnum(argv[i],"--pc=",
162 &target_pc)) {
163 /* done */
164 target_data.td_target_pc = target_pc;
165 } else if (!strcmp(argv[i],"--printdetails")){
166 target_data.td_print_details = TRUE;
167 } else if (!strcmp(argv[i],"--allinstances")){
168 target_data.td_reportallfound = TRUE;
169 } else if (!strcmp(argv[i],"-h")){
170 printusage();
171 exit(0);
172 } else if (!strcmp(argv[i],"--help")){
173 printusage();
174 exit(0);
175 } else if (!strcmp(argv[i],"--suppress-de-alloc-tree")){
176 /* Do nothing. This function needs that
177 left on (the default) */
178 } else {
179 /* Assume next arg is a pathname.*/
180 break;
181 }
182 }
183 if (i > (argc-1)) {
184 printusage();
185 exit(EXIT_FAILURE);
186 }
187 filepath = argv[i];
188 /* Ignoring any later arguments on the command line */
189 res = dwarf_init_path(filepath,
190 real_path,
191 PATH_LEN,
192 DW_GROUPNUMBER_ANY,errhand,errarg,&dbg,&error);
193 if (res == DW_DLV_ERROR) {
194 printf("Giving up, cannot do DWARF processing of %s "
195 "dwarf err %" DW_PR_DUu " %s\n",
196 filepath,
197 dwarf_errno(error),
198 dwarf_errmsg(error));
199 dwarf_dealloc_error(dbg,error);
200 dwarf_finish(dbg);
201 exit(EXIT_FAILURE);
202 }
203 if (res == DW_DLV_NO_ENTRY) {
204 printf("Giving up, file %s not found\n",filepath);
205 exit(EXIT_FAILURE);
206 }
207 res = look_for_our_target(dbg,&target_data,&error);
208 if (res == FOUND_SUBPROG) {
209 /* OK */
210 } else if (res != DW_DLV_OK) {
211 printf("Look for target PC failed!\n");
212 }
213 target_data_destructor(&target_data);
214 res = dwarf_finish(dbg);
215 if (res != DW_DLV_OK) {
216 printf("dwarf_finish failed!\n");
217 }
218 return 0;
219 }
220
221 static void
222 resetsrcfiles(Dwarf_Debug dbg,struct srcfilesdata *sf)
223 {
224 Dwarf_Signed sri = 0;
225 for (sri = 0; sri < sf->srcfilescount; ++sri) {
226 dwarf_dealloc(dbg, sf->srcfiles[sri], DW_DLA_STRING);
227 }
228 dwarf_dealloc(dbg, sf->srcfiles, DW_DLA_LIST);
229 sf->srcfilesres = DW_DLV_ERROR;
230 sf->srcfiles = 0;
231 sf->srcfilescount = 0;
232 }
233 static void resetsubprog(Dwarf_Debug dbg,struct target_data_s *td)
234 {
235 td->td_subprog_haslowhighpc = FALSE;
236 if (td->td_subprog_die) {
237 dwarf_dealloc(dbg,td->td_subprog_die,DW_DLA_DIE);
238 td->td_subprog_die = 0;
239 }
240 }
241 static void
242 reset_target_data(Dwarf_Debug dbg,struct target_data_s *td)
243 {
244 if (td->td_cu_die) {
245 dwarf_dealloc(dbg,td->td_cu_die,DW_DLA_DIE);
246 td->td_cu_die = 0;
247 }
248 if (td->td_subprog_die) {
249 dwarf_dealloc(dbg,td->td_subprog_die,DW_DLA_DIE);
250 td->td_subprog_die = 0;
251 }
252 td->td_cu_haslowhighpc = FALSE;
253 resetsrcfiles(dbg,&td->td_cu_srcfiles);
254 resetsubprog(dbg,td);
255 }
256 static void
257 target_data_destructor( struct target_data_s *td)
258 {
259 Dwarf_Debug dbg = 0;
260 if (!td || !td->td_dbg) {
261 return;
262 }
263 dbg = td->td_dbg;
264 reset_target_data(dbg,td);
265 if (td->td_subprog_srcfile) {
266 dwarf_dealloc(dbg,td->td_subprog_srcfile,DW_DLA_STRING);
267 td->td_subprog_srcfile = 0;
268 }
269 }
270
271 static void
272 print_target_info( Dwarf_Debug dbg,
273 struct target_data_s *td)
274 {
275 (void)dbg;
276 printf("FOUND function \"%s\", requested pc 0x%" DW_PR_DUx
277 "\n",
278 td->td_subprog_name?td->td_subprog_name:"<unknown>",
279 td->td_target_pc);
280 printf(" function lowpc 0x%" DW_PR_DUx
281 " highpc 0x%" DW_PR_DUx "\n",
282 td->td_subprog_lowpc,
283 td->td_subprog_highpc);
284 printf(" file name index 0x%" DW_PR_DUx "\n",
285 td->td_subprog_fileindex);
286 printf(" in:\n");
287 printf(" CU %" DW_PR_DUu " %s\n",
288 td->td_cu_number,
289 td->td_cu_name?td->td_cu_name:"<unknown>");
290 printf(" comp-dir %s\n",
291 td->td_cu_comp_dir?td->td_cu_comp_dir:"<unknown>");
292
293 printf(" Src line address 0x%" DW_PR_DUx
294 " lineno %" DW_PR_DUu
295 " %s\n",
296 td->td_subprog_lineaddr,
297 td->td_subprog_lineno,
298 td->td_subprog_srcfile);
299 printf("\n");
300 }
301
302 static int
303 read_line_data(Dwarf_Debug dbg,
304 struct target_data_s *td,
305 Dwarf_Error *errp)
306 {
307 int res = 0;
308 Dwarf_Unsigned line_version = 0;
309 Dwarf_Small table_type = 0;
310 Dwarf_Line_Context line_context = 0;
311 Dwarf_Signed i = 0;
312 Dwarf_Signed baseindex = 0;
313 Dwarf_Signed endindex = 0;
314 Dwarf_Signed file_count = 0;
315 Dwarf_Unsigned dirindex = 0;
316
317 (void)dbg;
318 res = dwarf_srclines_b(td->td_cu_die,&line_version,
319 &table_type,&line_context,errp);
320 if (res != DW_DLV_OK) {
321 return res;
322 }
323 if (td->td_print_details) {
324 printf(" Srclines: linetable version %" DW_PR_DUu
325 " table count %d\n",line_version,table_type);
326 }
327 if (table_type == 0) {
328 /* There are no lines here, just table header and names. */
329 int sres = 0;
330
331 sres = dwarf_srclines_files_indexes(line_context,
332 &baseindex,&file_count,&endindex,errp);
333 if (sres != DW_DLV_OK) {
334 dwarf_srclines_dealloc_b(line_context);
335 line_context = 0;
336 return sres;
337 }
338 if (td->td_print_details) {
339 printf(" Filenames base index %" DW_PR_DSd
340 " file count %" DW_PR_DSd
341 " endindex %" DW_PR_DSd "\n",
342 baseindex,file_count,endindex);
343 }
344 for (i = baseindex; i < endindex; i++) {
345 Dwarf_Unsigned modtime = 0;
346 Dwarf_Unsigned flength = 0;
347 Dwarf_Form_Data16 *md5data = 0;
348 int vres = 0;
349 const char *name = 0;
350
351 vres = dwarf_srclines_files_data_b(line_context,i,
352 &name,&dirindex, &modtime,&flength,
353 &md5data,errp);
354 if (vres != DW_DLV_OK) {
355 dwarf_srclines_dealloc_b(line_context);
356 line_context = 0;
357 /* something very wrong. */
358 return vres;
359 }
360 if (td->td_print_details) {
361 printf(" [%" DW_PR_DSd "] "
362 " directory index %" DW_PR_DUu
363 " %s \n",i,dirindex,name);
364 }
365 }
366 dwarf_srclines_dealloc_b(line_context);
367 return DW_DLV_OK;
368 } else if (table_type == 1) {
369 const char * dir_name = 0;
370 int sres = 0;
371 Dwarf_Line *linebuf = 0;
372 Dwarf_Signed linecount = 0;
373 Dwarf_Signed dir_count = 0;
374 Dwarf_Addr prev_lineaddr = 0;
375 Dwarf_Unsigned prev_lineno = 0;
376 char * prev_linesrcfile = 0;
377
378 sres = dwarf_srclines_files_indexes(line_context,
379 &baseindex,&file_count,&endindex,errp);
380 if (sres != DW_DLV_OK) {
381 dwarf_srclines_dealloc_b(line_context);
382 line_context = 0;
383 return sres;
384 }
385 if (td->td_print_details) {
386 printf(" Filenames base index %" DW_PR_DSd
387 " file count %" DW_PR_DSd
388 " endindex %" DW_PR_DSd "\n",
389 baseindex,file_count,endindex);
390 }
391 for (i = baseindex; i < endindex; i++) {
392 Dwarf_Unsigned dirindexb = 0;
393 Dwarf_Unsigned modtime = 0;
394 Dwarf_Unsigned flength = 0;
395 Dwarf_Form_Data16 *md5data = 0;
396 int vres = 0;
397 const char *name = 0;
398
399 vres = dwarf_srclines_files_data_b(line_context,i,
400 &name,&dirindexb, &modtime,&flength,
401 &md5data,errp);
402 if (vres != DW_DLV_OK) {
403 dwarf_srclines_dealloc_b(line_context);
404 line_context = 0;
405 /* something very wrong. */
406 return vres;
407 }
408 if (td->td_print_details) {
409 printf(" [%" DW_PR_DSd "] "
410 " directory index %" DW_PR_DUu
411 " file: %s \n",i,dirindexb,name);
412 }
413 }
414 sres = dwarf_srclines_include_dir_count(line_context,
415 &dir_count,errp);
416 if (sres != DW_DLV_OK) {
417 dwarf_srclines_dealloc_b(line_context);
418 line_context = 0;
419 return sres;
420 }
421 if (td->td_print_details) {
422 printf(" Directories count: %" DW_PR_DSd "\n",
423 dir_count);
424 }
425
426 for (i =1; i <= dir_count; ++i) {
427 dir_name = 0;
428 sres = dwarf_srclines_include_dir_data(line_context,
429 i,&dir_name,errp);
430 if (sres == DW_DLV_ERROR) {
431 dwarf_srclines_dealloc_b(line_context);
432 line_context = 0;
433 return sres;
434 }
435 if (sres == DW_DLV_NO_ENTRY) {
436 printf("Something wrong in dir tables line %d %s\n",
437 __LINE__,__FILE__);
438 break;
439 }
440 if (td->td_print_details) {
441 printf(" [%" DW_PR_DSd "] directory: "
442 " %s \n",i,dir_name);
443 }
444 }
445
446 /* For this case where we have a line table we will likely
447 wish to get the line details: */
448 sres = dwarf_srclines_from_linecontext(line_context,
449 &linebuf,&linecount,
450 errp);
451 if (sres != DW_DLV_OK) {
452 dwarf_srclines_dealloc_b(line_context);
453 line_context = 0;
454 return sres;
455 }
456 /* The lines are normal line table lines. */
457 for (i = 0; i < linecount; ++i) {
458 Dwarf_Addr lineaddr = 0;
459 Dwarf_Unsigned filenum = 0;
460 Dwarf_Unsigned lineno = 0;
461 char * linesrcfile = 0;
462
463 sres = dwarf_lineno(linebuf[i],&lineno,errp);
464 if (sres == DW_DLV_ERROR) {
465 if (prev_linesrcfile) {
466 dwarf_dealloc(dbg,prev_linesrcfile,DW_DLA_STRING);
467 }
468 return sres;
469 }
470 sres = dwarf_line_srcfileno(linebuf[i],&filenum,errp);
471 if (sres == DW_DLV_ERROR) {
472 if (prev_linesrcfile) {
473 dwarf_dealloc(dbg,prev_linesrcfile,DW_DLA_STRING);
474 }
475 return sres;
476 }
477 if (filenum) {
478 filenum -= 1;
479 }
480 sres = dwarf_lineaddr(linebuf[i],&lineaddr,errp);
481 if (sres == DW_DLV_ERROR) {
482 if (prev_linesrcfile) {
483 dwarf_dealloc(dbg,prev_linesrcfile,DW_DLA_STRING);
484 }
485 return sres;
486 }
487 sres = dwarf_linesrc(linebuf[i],&linesrcfile,errp);
488 if (sres == DW_DLV_ERROR) {
489 if (prev_linesrcfile) {
490 dwarf_dealloc(dbg,prev_linesrcfile,DW_DLA_STRING);
491 }
492 return sres;
493 }
494 if (td->td_print_details) {
495 printf(" [%" DW_PR_DSd "] "
496 " address 0x%" DW_PR_DUx
497 " filenum %" DW_PR_DUu
498 " lineno %" DW_PR_DUu
499 " %s \n",i,lineaddr,filenum,lineno,linesrcfile);
500 }
501 if (lineaddr > td->td_target_pc) {
502 /* Here we detect the right source and line */
503 td->td_subprog_lineaddr = prev_lineaddr;
504 td->td_subprog_lineno = prev_lineno;
505 td->td_subprog_srcfile = prev_linesrcfile;
506 dwarf_dealloc(dbg,linesrcfile,DW_DLA_STRING);
507 return DW_DLV_OK;
508 }
509 prev_lineaddr = lineaddr;
510 prev_lineno = lineno;
511 if (prev_linesrcfile) {
512 dwarf_dealloc(dbg,prev_linesrcfile,DW_DLA_STRING);
513 }
514 prev_linesrcfile = linesrcfile;
515 }
516 /* Here we detect the right source and line (last such
517 in this subprogram) */
518 td->td_subprog_lineaddr = prev_lineaddr;
519 td->td_subprog_lineno = prev_lineno;
520 td->td_subprog_srcfile = prev_linesrcfile;
521 dwarf_srclines_dealloc_b(line_context);
522 return DW_DLV_OK;
523 }
524 return DW_DLV_ERROR;
525 #if 0
526 /* ASSERT: table_type == 2,
527 Experimental two-level line table. Version 0xf006
528 We do not define the meaning of this non-standard
529 set of tables here. */
530 /* For ’something C’ (two-level line tables)
531 one codes something like this
532 Note that we do not define the meaning
533 or use of two-level li
534 tables as these are experimental,
535 not standard DWARF. */
536 sres = dwarf_srclines_two_level_from_linecontext(line_context,
537 &linebuf,&linecount,
538 &linebuf_actuals,&linecount_actuals,
539 &err);
540 if (sres == DW_DLV_OK) {
541 for (i = 0; i < linecount; ++i) {
542 /* use linebuf[i], these are
543 the ’logicals’ entries. */
544 }
545 for (i = 0; i < linecount_actuals; ++i) {
546 /* use linebuf_actuals[i],
547 these are the actuals entries */
548 }
549 dwarf_srclines_dealloc_b(line_context);
550 line_context = 0;
551 linebuf = 0;
552 linecount = 0;
553 linebuf_actuals = 0;
554 linecount_actuals = 0;
555 } else if (sres == DW_DLV_NO_ENTRY) {
556 dwarf_srclines_dealloc_b(line_context);
557 line_context = 0;
558 linebuf = 0;
559 linecount = 0;
560 linebuf_actuals = 0;
561 linecount_actuals = 0;
562 return sres;
563 } else { /*DW_DLV_ERROR */
564 return sres;
565 }
566 #endif /* 0 */
567 }
568
569 static int
570 look_for_our_target(Dwarf_Debug dbg,
571 struct target_data_s *td,
572 Dwarf_Error *errp)
573 {
574 Dwarf_Unsigned cu_header_length = 0;
575 Dwarf_Unsigned abbrev_offset = 0;
576 Dwarf_Half address_size = 0;
577 Dwarf_Half version_stamp = 0;
578 Dwarf_Half offset_size = 0;
579 Dwarf_Half extension_size = 0;
580 Dwarf_Unsigned typeoffset = 0;
581 Dwarf_Half header_cu_type = unittype;
582 Dwarf_Bool is_info = g_is_info;
583 int cu_number = 0;
584
585 for (;;++cu_number) {
586 Dwarf_Die no_die = 0;
587 Dwarf_Die cu_die = 0;
588 int res = DW_DLV_ERROR;
589 Dwarf_Sig8 signature;
590
591 memset(&signature,0, sizeof(signature));
592 reset_target_data(dbg,td);
593 res = dwarf_next_cu_header_d(dbg,is_info,&cu_header_length,
594 &version_stamp, &abbrev_offset,
595 &address_size, &offset_size,
596 &extension_size,&signature,
597 &typeoffset, 0,
598 &header_cu_type,errp);
599 if (res == DW_DLV_ERROR) {
600 char *em = dwarf_errmsg(*errp);
601 printf("Error in dwarf_next_cu_header: %s\n",em);
602 target_data_destructor(td);
603 dwarf_finish(dbg);
604 exit(EXIT_FAILURE);
605 }
606 if (res == DW_DLV_NO_ENTRY) {
607 /* Done. */
608 return DW_DLV_NO_ENTRY;
609 }
610 cu_version_stamp = version_stamp;
611 cu_offset_size = offset_size;
612 /* The CU will have a single sibling, a cu_die. */
613 res = dwarf_siblingof_b(dbg,no_die,is_info,
614 &cu_die,errp);
615 if (res == DW_DLV_ERROR) {
616 char *em = dwarf_errmsg(*errp);
617 printf("Error in dwarf_siblingof_b on CU die: %s\n",em);
618 target_data_destructor(td);
619 dwarf_finish(dbg);
620 exit(EXIT_FAILURE);
621 }
622 if (res == DW_DLV_NO_ENTRY) {
623 /* Impossible case. */
624 printf("no entry! in dwarf_siblingof on CU die \n");
625 target_data_destructor(td);
626 dwarf_finish(dbg);
627 exit(EXIT_FAILURE);
628 }
629
630 td->td_cu_die = cu_die;
631 res = get_die_and_siblings(dbg,cu_die,is_info,0,cu_number,
632 td,errp);
633 if (res == FOUND_SUBPROG) {
634 read_line_data(dbg,td,errp);
635 print_target_info(dbg,td);
636 if (td->td_reportallfound) {
637 return res;
638 }
639 reset_target_data(dbg,td);
640 return res;
641 }
642 else if (res == IN_THIS_CU) {
643 char *em = dwarf_errmsg(*errp);
644 printf("Impossible return code in reading DIEs: %s\n",em);
645 target_data_destructor(td);
646 dwarf_finish(dbg);
647 exit(EXIT_FAILURE);
648 }
649 else if (res == NOT_THIS_CU) {
650 /* This is normal. Keep looking */
651 }
652 else if (res == DW_DLV_ERROR) {
653 char *em = dwarf_errmsg(*errp);
654 printf("Error in reading DIEs: %s\n",em);
655 target_data_destructor(td);
656 dwarf_finish(dbg);
657 exit(EXIT_FAILURE);
658 }
659 else if (res == DW_DLV_NO_ENTRY) {
660 /* This is odd. Assume normal. */
661 } else {
662 /* DW_DLV_OK. normal. */
663 }
664 reset_target_data(dbg,td);
665 }
666 return DW_DLV_NO_ENTRY;
667 }
668
669 /* Recursion, following DIE tree.
670 On initial call in_die is a cu_die (in_level 0 )
671 */
672 static int
673 get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die,
674 int is_info,int in_level,int cu_number,
675 struct target_data_s *td,
676 Dwarf_Error *errp)
677 {
678 int res = DW_DLV_ERROR;
679 Dwarf_Die cur_die=in_die;
680 Dwarf_Die child = 0;
681
682 td->td_cu_number = cu_number;
683 res = examine_die_data(dbg,is_info,in_die,in_level,td,errp);
684 if (res == DW_DLV_ERROR) {
685 printf("Error in die access , level %d \n",in_level);
686 dwarf_finish(dbg);
687 exit(EXIT_FAILURE);
688 }
689 if (res == DW_DLV_NO_ENTRY) {
690 return res;
691 }
692 if (res == NOT_THIS_CU) {
693 return res;
694 }
695 if (res == IN_THIS_CU) {
696 /* Fall through to examine details. */
697 } else if (res == FOUND_SUBPROG) {
698 return res;
699 } else {
700 /* ASSERT: res == DW_DLV_OK */
701 /* Fall through to examine details. */
702 }
703
704 /* Now look at the children of the incoming DIE */
705 for (;;) {
706 Dwarf_Die sib_die = 0;
707 res = dwarf_child(cur_die,&child,errp);
708 if (res == DW_DLV_ERROR) {
709 printf("Error in dwarf_child , level %d \n",in_level);
710 dwarf_finish(dbg);
711 exit(EXIT_FAILURE);
712 }
713 if (res == DW_DLV_OK) {
714 int res2 = 0;
715
716 res2 = get_die_and_siblings(dbg,child,is_info,
717 in_level+1,cu_number,td,errp);
718 if (child != td->td_cu_die &&
719 child != td->td_subprog_die) {
720 /* No longer need 'child' die. */
721 dwarf_dealloc(dbg,child,DW_DLA_DIE);
722 }
723 if (res2 == FOUND_SUBPROG) {
724 return res2;
725 }
726 else if (res2 == IN_THIS_CU) {
727 /* fall thru */
728 }
729 else if (res2 == NOT_THIS_CU) {
730 return res2;
731 }
732 else if (res2 == DW_DLV_ERROR) {
733 return res2;
734 }
735 else if (res2 == DW_DLV_NO_ENTRY) {
736 /* fall thru */
737 }
738 else { /* DW_DLV_OK */
739 /* fall thru */
740 }
741 child = 0;
742 }
743 res = dwarf_siblingof_b(dbg,cur_die,is_info,
744 &sib_die,errp);
745 if (res == DW_DLV_ERROR) {
746 char *em = dwarf_errmsg(*errp);
747 printf("Error in dwarf_siblingof_b , level %d :%s \n",
748 in_level,em);
749 dwarf_finish(dbg);
750 exit(EXIT_FAILURE);
751 }
752 if (res == DW_DLV_NO_ENTRY) {
753 /* Done at this level. */
754 break;
755 }
756 /* res == DW_DLV_OK */
757 if (cur_die != in_die) {
758 if (child != td->td_cu_die &&
759 child != td->td_subprog_die) {
760 dwarf_dealloc(dbg,cur_die,DW_DLA_DIE);
761 }
762 }
763 cur_die = sib_die;
764 res = examine_die_data(dbg,is_info,cur_die,in_level,td,errp);
765 if (res == DW_DLV_ERROR) {
766 return res;
767 }
768 else if (res == DW_DLV_OK) {
769 /* fall through */
770 }
771 else if (res == FOUND_SUBPROG) {
772 return res;
773 }
774 else if (res == NOT_THIS_CU) {
775 /* impossible */
776 }
777 else if (res == IN_THIS_CU) {
778 /* impossible */
779 } else {/* res == DW_DLV_NO_ENTRY */
780 /* impossible */
781 /* fall through */
782 }
783 }
784 return DW_DLV_OK;
785 }
786 #if 0
787 static void
788 get_addr(Dwarf_Attribute attr,Dwarf_Addr *val)
789 {
790 Dwarf_Error error = 0;
791 int res;
792 Dwarf_Addr uval = 0;
793 Dwarf_Error *errp = 0;
794
795 errp = &error;
796 res = dwarf_formaddr(attr,&uval,errp);
797 if (res == DW_DLV_OK) {
798 *val = uval;
799 return;
800 }
801 return;
802 }
803 static void
804 get_number(Dwarf_Attribute attr,Dwarf_Unsigned *val)
805 {
806 Dwarf_Error error = 0;
807 int res;
808 Dwarf_Signed sval = 0;
809 Dwarf_Unsigned uval = 0;
810 Dwarf_Error *errp = 0;
811
812 errp = &error;
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 #endif
826
827 /* Used when have only processed *some* list items
828 and dealloc'd. This finishes up.
829 Not really essential as dwarf_finish() would
830 do it anyway, but Coverity Scan does not
831 understand that dwarf_finish() cleans up.
832 So this usefully avoids a coverity scan
833 defect report. */
834 static void
835 dealloc_rest_of_list(Dwarf_Debug dbg,
836 Dwarf_Attribute *attrbuf,
837 Dwarf_Signed attrcount,
838 Dwarf_Signed i)
839 {
840 for ( ; i < attrcount; ++i) {
841 dwarf_dealloc_attribute(attrbuf[i]);
842 }
843 dwarf_dealloc(dbg,attrbuf,DW_DLA_LIST);
844 }
845
846 static int
847 getlowhighpc(
848 Dwarf_Die die,
849 int *have_pc_range,
850 Dwarf_Addr *lowpc_out,
851 Dwarf_Addr *highpc_out,
852 Dwarf_Error*error)
853 {
854 Dwarf_Addr hipc = 0;
855 int res = 0;
856 Dwarf_Half form = 0;
857 enum Dwarf_Form_Class formclass = 0;
858
859 *have_pc_range = FALSE;
860 res = dwarf_lowpc(die,lowpc_out,error);
861 if (res == DW_DLV_OK) {
862 res = dwarf_highpc_b(die,&hipc,&form,&formclass,error);
863 if (res == DW_DLV_OK) {
864 if (formclass == DW_FORM_CLASS_CONSTANT) {
865 hipc += *lowpc_out;
866 }
867 *highpc_out = hipc;
868 *have_pc_range = TRUE;
869 return DW_DLV_OK;
870 }
871 }
872 /* Cannot check ranges yet, we don't know the ranges base
873 offset yet. */
874 return DW_DLV_NO_ENTRY;
875 }
876
877 static int
878 check_subprog_ranges_for_match(Dwarf_Debug dbg,
879 Dwarf_Die die,
880 struct target_data_s *td,
881 int *have_pc_range,
882 Dwarf_Addr *lowpc_out,
883 Dwarf_Addr *highpc_out,
884 Dwarf_Error *errp)
885 {
886 int res = 0;
887 Dwarf_Ranges *ranges;
888 Dwarf_Signed ranges_count;
889 Dwarf_Unsigned byte_count;
890 Dwarf_Signed i = 0;
891 Dwarf_Addr baseaddr = 0;
892 Dwarf_Off actualoffset = 0;
893 int done = FALSE;
894
895 /* Check libdwarf to ensure ranges_base not added
896 again. */
897 res = dwarf_get_ranges_b(dbg,td->td_ranges_offset,
898 die,
899 &actualoffset,
900 &ranges,
901 &ranges_count,
902 &byte_count,
903 errp);
904 if (res != DW_DLV_OK) {
905 return res;
906 }
907 for (i=0; i < ranges_count && !done; ++i) {
908 Dwarf_Ranges *cur = ranges+i;
909 Dwarf_Addr lowpc = 0;
910 Dwarf_Addr highpc = 0;
911 switch(cur->dwr_type) {
912 case DW_RANGES_ENTRY:
913 lowpc = cur->dwr_addr1 +baseaddr;
914 highpc = cur->dwr_addr2 +baseaddr;
915 if (td->td_target_pc < lowpc ||
916 td->td_target_pc >= highpc) {
917 /* We have no interest in this CU */
918 break;
919 }
920 *lowpc_out = lowpc;
921 *highpc_out = highpc;
922 *have_pc_range = TRUE;
923 done = TRUE;
924 res = FOUND_SUBPROG;
925 break;
926 case DW_RANGES_ADDRESS_SELECTION:
927 baseaddr = cur->dwr_addr2;
928 break;
929 case DW_RANGES_END:
930 break;
931 default:
932 printf("Impossible debug_ranges content!"
933 " enum val %d \n",(int)cur->dwr_type);
934 dwarf_finish(dbg);
935 exit(EXIT_FAILURE);
936 }
937 }
938 dwarf_dealloc_ranges(dbg,ranges,ranges_count);
939 return res;
940 }
941
942 /* The return value is not interesting. Getting
943 the name is interesting. */
944 static int
945 get_name_from_abstract_origin(Dwarf_Debug dbg,
946 int is_info,
947 Dwarf_Die die,
948 char ** name,
949 Dwarf_Error *errp)
950 {
951 int res = 0;
952 Dwarf_Die abrootdie = 0;
953 Dwarf_Attribute ab_attr = 0;
954 Dwarf_Off ab_offset = 0;
955
956 res = dwarf_attr(die,DW_AT_abstract_origin,&ab_attr,errp);
957 if (res != DW_DLV_OK) {
958 return res;
959 }
960
961 res = dwarf_global_formref(ab_attr,&ab_offset,errp);
962 if (res != DW_DLV_OK) {
963 dwarf_dealloc(dbg,ab_attr,DW_DLA_ATTR);
964 return res;
965 }
966
967 dwarf_dealloc(dbg,ab_attr,DW_DLA_ATTR);
968 res = dwarf_offdie_b(dbg,ab_offset,is_info,&abrootdie,errp) ;
969 if (res != DW_DLV_OK) {
970 return res;
971 }
972 res = dwarf_diename(abrootdie,name,errp);
973 dwarf_dealloc_die(abrootdie);
974 return res;
975 }
976
977 static int
978 check_subprog_details(Dwarf_Debug dbg,
979 int is_info,
980 Dwarf_Die die,
981 struct target_data_s *td,
982 int *have_pc_range_out,
983 Dwarf_Addr *lowpc_out,
984 Dwarf_Addr *highpc_out,
985 Dwarf_Error *errp)
986 {
987 int res = 0;
988 Dwarf_Addr lowpc = 0;
989 Dwarf_Addr highpc = 0;
990 int finalres = 0;
991 int have_pc_range = FALSE;
992
993 res = getlowhighpc(die, &have_pc_range,
994 &lowpc,&highpc,errp);
995 if (res == DW_DLV_OK) {
996 if (have_pc_range) {
997 int res2 = DW_DLV_OK;
998 char *name = 0;
999
1000 if (td->td_target_pc < lowpc ||
1001 td->td_target_pc >= highpc) {
1002 /* We have no interest in this subprogram */
1003 finalres = DW_DLV_OK;
1004 } else {
1005 td->td_subprog_die = die;
1006 td->td_subprog_lowpc = lowpc;
1007 *lowpc_out = lowpc;
1008 *highpc_out = highpc;
1009 *have_pc_range_out = have_pc_range;
1010 td->td_subprog_highpc = highpc;
1011 td->td_subprog_haslowhighpc = have_pc_range;
1012 res2 = dwarf_diename(die,&name,errp);
1013 if (res2 == DW_DLV_OK) {
1014 td->td_subprog_name = name;
1015 } else {
1016 get_name_from_abstract_origin(dbg,is_info,
1017 die, &name, errp);
1018 }
1019 td->td_subprog_name = name;
1020 name = 0;
1021 /* Means this is an address match */
1022 finalres = FOUND_SUBPROG;
1023 }
1024 }
1025 }
1026 {
1027 Dwarf_Signed i = 0;
1028 Dwarf_Signed atcount = 0;
1029 Dwarf_Attribute *atlist = 0;
1030
1031 res = dwarf_attrlist(die,&atlist,&atcount,errp);
1032 if (res != DW_DLV_OK) {
1033 return res;
1034 }
1035 for (i = 0; i < atcount ; ++i) {
1036 Dwarf_Half atr = 0;
1037 Dwarf_Attribute attrib = atlist[i];
1038
1039 res = dwarf_whatattr(attrib,&atr,errp);
1040 if (res != DW_DLV_OK) {
1041 /* Something is very wrong here.*/
1042 if (td->td_print_details) {
1043 printf("dwarf_whatattr returns bad errcode %d\n",
1044 res);
1045 }
1046 dealloc_rest_of_list(dbg,atlist,atcount,i);
1047 return res;
1048 }
1049 if (atr == DW_AT_ranges) {
1050 int res2 = 0;
1051 int res4 = 0;
1052 Dwarf_Off ret_offset = 0;
1053 int has_low_hi = FALSE;
1054 Dwarf_Addr low = 0;
1055 Dwarf_Addr high = 0;
1056
1057 res2 = dwarf_global_formref(attrib,
1058 &ret_offset,errp);
1059 if (res2 != DW_DLV_OK) {
1060 dealloc_rest_of_list(dbg,atlist,atcount,i);
1061 return res2;
1062 }
1063 td->td_ranges_offset = ret_offset +
1064 td->td_cu_ranges_base;
1065 res4 = check_subprog_ranges_for_match(dbg,
1066 die,
1067 td,
1068 &has_low_hi,
1069 &low,
1070 &high,
1071 errp);
1072 if (res4 == DW_DLV_OK) {
1073 continue;
1074 }
1075 if (res4 == DW_DLV_NO_ENTRY) {
1076 continue;
1077 }
1078 if (res4 == FOUND_SUBPROG) {
1079 td->td_subprog_lowpc = lowpc;
1080 td->td_subprog_highpc = highpc;
1081 td->td_subprog_haslowhighpc = has_low_hi;
1082 finalres = FOUND_SUBPROG;
1083 continue;
1084 }
1085 dealloc_rest_of_list(dbg,atlist,atcount,i);
1086 /* ASSERT: res4 == DW_DLV_ERROR; */
1087 return res4;
1088 } else if (atr == DW_AT_decl_file ) {
1089 int res5 = 0;
1090 Dwarf_Unsigned file_index = 0;
1091
1092 res5 = dwarf_formudata(attrib,&file_index,errp);
1093 if (res5 != DW_DLV_OK) {
1094 dealloc_rest_of_list(dbg,atlist,atcount,i);
1095 return res5;
1096 }
1097 td->td_subprog_fileindex = file_index;
1098 }
1099 dwarf_dealloc(dbg,attrib,DW_DLA_ATTR);
1100 }
1101 dwarf_dealloc(dbg,atlist,DW_DLA_LIST);
1102 }
1103 return finalres;
1104 }
1105
1106 static int
1107 check_comp_dir(Dwarf_Debug dbg,Dwarf_Die die,
1108 struct target_data_s *td,Dwarf_Error *errp)
1109 {
1110 int res = 0;
1111 int finalres = DW_DLV_NO_ENTRY;
1112 int have_pc_range = FALSE;
1113 Dwarf_Addr lowpc = 0;
1114 Dwarf_Addr highpc = 0;
1115 Dwarf_Off real_ranges_offset = 0;
1116 int rdone = FALSE;
1117
1118 res = getlowhighpc(die,
1119 &have_pc_range,
1120 &lowpc,&highpc,errp);
1121 if (res == DW_DLV_OK) {
1122 if (have_pc_range) {
1123 if (td->td_target_pc < lowpc ||
1124 td->td_target_pc >= highpc) {
1125 /* We have no interest in this CU */
1126 res = NOT_THIS_CU;
1127 } else {
1128 td->td_cu_lowpc = lowpc;
1129 td->td_cu_highpc = highpc;
1130 /* td->td_cu_haslowhighpc = have_pc_range; */
1131 res = IN_THIS_CU;
1132 }
1133 }
1134 }
1135 finalres = res;
1136 {
1137 Dwarf_Signed atcount = 0;
1138 Dwarf_Attribute *atlist = 0;
1139 Dwarf_Signed j = 0;
1140 int alres = 0;
1141
1142 alres = dwarf_attrlist(die,&atlist,&atcount,errp);
1143 if (alres != DW_DLV_OK) {
1144 return alres;
1145 }
1146 for (j = 0; j < atcount ; ++j) {
1147 Dwarf_Half atr = 0;
1148 Dwarf_Attribute attrib = atlist[j];
1149 int resb = 0;
1150
1151 resb = dwarf_whatattr(attrib,&atr,errp);
1152 if (resb != DW_DLV_OK) {
1153 dwarf_dealloc(dbg,atlist,DW_DLA_LIST);
1154 /* Something is very wrong here.*/
1155 printf("dwarf_whatattr returns bad errcode %d, "
1156 "serious error somewhere.\n",
1157 res);
1158 return resb;
1159 }
1160 if (atr == DW_AT_name) {
1161 char *name = 0;
1162 resb = dwarf_formstring(attrib,&name,errp);
1163 if (resb == DW_DLV_OK) {
1164 td->td_cu_name = name;
1165 }
1166 } else if (atr == DW_AT_comp_dir) {
1167 char *name = 0;
1168 resb = dwarf_formstring(attrib,&name,errp);
1169 if (resb == DW_DLV_OK) {
1170 td->td_cu_comp_dir = name;
1171 }
1172 } else if (atr == DW_AT_rnglists_base ||
1173 atr == DW_AT_GNU_ranges_base) {
1174 Dwarf_Off rbase = 0;
1175
1176 resb = dwarf_global_formref(attrib,&rbase,errp);
1177 if (resb != DW_DLV_OK) {
1178 dwarf_dealloc(dbg,atlist,DW_DLA_LIST);
1179 return resb;
1180 }
1181 td->td_cu_ranges_base = rbase;
1182 } else if (atr == DW_AT_ranges) {
1183 /* we have actual ranges. */
1184 Dwarf_Off rbase = 0;
1185
1186 resb = dwarf_global_formref(attrib,&rbase,errp);
1187 if (resb != DW_DLV_OK) {
1188 dwarf_dealloc(dbg,atlist,DW_DLA_LIST);
1189 return resb;
1190 }
1191 real_ranges_offset = rbase;
1192 rdone = TRUE;
1193 }
1194 dwarf_dealloc(dbg,attrib,DW_DLA_ATTR);
1195 }
1196 dwarf_dealloc(dbg,atlist,DW_DLA_LIST);
1197 }
1198 if (rdone) {
1199 /* We can do get ranges now as we already saw
1200 ranges base above (if any). */
1201 int resr = 0;
1202 Dwarf_Ranges *ranges = 0;
1203 Dwarf_Signed ranges_count =0;
1204 Dwarf_Unsigned byte_count =0;
1205 Dwarf_Off actualoffset = 0;
1206 Dwarf_Signed k = 0;
1207 int done = FALSE;
1208
1209 resr = dwarf_get_ranges_b(dbg,real_ranges_offset,
1210 die,
1211 &actualoffset,
1212 &ranges,
1213 &ranges_count,
1214 &byte_count,
1215 errp);
1216 if (resr != DW_DLV_OK) {
1217 /* Something badly wrong here. */
1218 return res;
1219 }
1220 for (k=0; k < ranges_count && !done; ++k) {
1221 Dwarf_Ranges *cur = ranges+k;
1222 Dwarf_Addr lowpcr = 0;
1223 Dwarf_Addr highpcr = 0;
1224 Dwarf_Addr baseaddr = td->td_cu_ranges_base;
1225
1226 switch(cur->dwr_type) {
1227 case DW_RANGES_ENTRY:
1228 lowpc = cur->dwr_addr1 +baseaddr;
1229 highpc = cur->dwr_addr2 +baseaddr;
1230 if (td->td_target_pc < lowpc ||
1231 td->td_target_pc >= highpc) {
1232 /* We have no interest in this CU */
1233 break;
1234 }
1235 td->td_cu_lowpc = lowpcr;
1236 td->td_cu_highpc = highpcr;
1237 td->td_cu_haslowhighpc = TRUE;
1238 done = TRUE;
1239 finalres = IN_THIS_CU;
1240 break;
1241 case DW_RANGES_ADDRESS_SELECTION:
1242 baseaddr = cur->dwr_addr2;
1243 break;
1244 case DW_RANGES_END:
1245 break;
1246 default:
1247 dwarf_finish(dbg);
1248 printf("Impossible debug_ranges content!"
1249 " enum val %d \n",(int)cur->dwr_type);
1250 exit(EXIT_FAILURE);
1251 }
1252 }
1253 dwarf_dealloc_ranges(dbg,ranges,ranges_count);
1254 }
1255 return finalres;
1256 }
1257
1258 static int
1259 examine_die_data(Dwarf_Debug dbg,
1260 int is_info,
1261 Dwarf_Die die,
1262 int level,
1263 struct target_data_s *td,
1264 Dwarf_Error *errp)
1265 {
1266 Dwarf_Half tag = 0;
1267 int res = 0;
1268
1269 res = dwarf_tag(die,&tag,errp);
1270 if (res != DW_DLV_OK) {
1271 printf("Error in dwarf_tag , level %d \n",level);
1272 dwarf_finish(dbg);
1273 exit(EXIT_FAILURE);
1274 }
1275 if ( tag == DW_TAG_subprogram ||
1276 tag == DW_TAG_inlined_subroutine) {
1277 int have_pc_range = 0;
1278 Dwarf_Addr lowpc = 0;
1279 Dwarf_Addr highpc = 0;
1280
1281 res = check_subprog_details(dbg,is_info,die,td,
1282 &have_pc_range,
1283 &lowpc,
1284 &highpc,
1285 errp);
1286 if (res == FOUND_SUBPROG) {
1287 td->td_subprog_die = die;
1288 return res;
1289 }
1290 else if (res == DW_DLV_ERROR) {
1291 return res;
1292 }
1293 else if (res == DW_DLV_NO_ENTRY) {
1294 /* impossible? */
1295 return res;
1296 }
1297 else if (res == NOT_THIS_CU) {
1298 /* impossible */
1299 return res;
1300 }
1301 else if (res == IN_THIS_CU) {
1302 /* impossible */
1303 return res;
1304 } else {
1305 /* DW_DLV_OK */
1306 }
1307 return DW_DLV_OK;
1308 } else if (tag == DW_TAG_compile_unit ||
1309 tag == DW_TAG_partial_unit ||
1310 tag == DW_TAG_type_unit) {
1311
1312 if (level) {
1313 /* Something badly wrong, CU DIEs are only
1314 at level 0. */
1315 return NOT_THIS_CU;
1316 }
1317 res = check_comp_dir(dbg,die,td,errp);
1318 return res;
1319 }
1320 /* Keep looking */
1321 return DW_DLV_OK;
1322 }