blob: d9b8ad09849824883498598ef598356b77256f58 [file] [log] [blame]
Masami Hiramatsue0d153c2011-06-27 16:27:27 +09001/*
2 * dwarf-aux.c : libdw auxiliary interfaces
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 */
19
20#include <stdbool.h>
21#include "util.h"
22#include "debug.h"
23#include "dwarf-aux.h"
24
25/**
26 * cu_find_realpath - Find the realpath of the target file
27 * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
28 * @fname: The tail filename of the target file
29 *
30 * Find the real(long) path of @fname in @cu_die.
31 */
32const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
33{
34 Dwarf_Files *files;
35 size_t nfiles, i;
36 const char *src = NULL;
37 int ret;
38
39 if (!fname)
40 return NULL;
41
42 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
43 if (ret != 0)
44 return NULL;
45
46 for (i = 0; i < nfiles; i++) {
47 src = dwarf_filesrc(files, i, NULL, NULL);
48 if (strtailcmp(src, fname) == 0)
49 break;
50 }
51 if (i == nfiles)
52 return NULL;
53 return src;
54}
55
56/**
57 * cu_get_comp_dir - Get the path of compilation directory
58 * @cu_die: a CU DIE
59 *
60 * Get the path of compilation directory of given @cu_die.
61 * Since this depends on DW_AT_comp_dir, older gcc will not
62 * embedded it. In that case, this returns NULL.
63 */
64const char *cu_get_comp_dir(Dwarf_Die *cu_die)
65{
66 Dwarf_Attribute attr;
67 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
68 return NULL;
69 return dwarf_formstring(&attr);
70}
71
72/**
73 * cu_find_lineinfo - Get a line number and file name for given address
74 * @cu_die: a CU DIE
75 * @addr: An address
76 * @fname: a pointer which returns the file name string
77 * @lineno: a pointer which returns the line number
78 *
79 * Find a line number and file name for @addr in @cu_die.
80 */
81int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
82 const char **fname, int *lineno)
83{
84 Dwarf_Line *line;
85 Dwarf_Addr laddr;
86
87 line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr);
88 if (line && dwarf_lineaddr(line, &laddr) == 0 &&
89 addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
90 *fname = dwarf_linesrc(line, NULL, NULL);
91 if (!*fname)
92 /* line number is useless without filename */
93 *lineno = 0;
94 }
95
96 return *lineno ?: -ENOENT;
97}
98
99/**
100 * die_compare_name - Compare diename and tname
101 * @dw_die: a DIE
102 * @tname: a string of target name
103 *
104 * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
105 */
106bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
107{
108 const char *name;
109 name = dwarf_diename(dw_die);
110 return name ? (strcmp(tname, name) == 0) : false;
111}
112
113/**
114 * die_get_call_lineno - Get callsite line number of inline-function instance
115 * @in_die: a DIE of an inlined function instance
116 *
117 * Get call-site line number of @in_die. This means from where the inline
118 * function is called.
119 */
120int die_get_call_lineno(Dwarf_Die *in_die)
121{
122 Dwarf_Attribute attr;
123 Dwarf_Word ret;
124
125 if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
126 return -ENOENT;
127
128 dwarf_formudata(&attr, &ret);
129 return (int)ret;
130}
131
132/**
133 * die_get_type - Get type DIE
134 * @vr_die: a DIE of a variable
135 * @die_mem: where to store a type DIE
136 *
137 * Get a DIE of the type of given variable (@vr_die), and store
138 * it to die_mem. Return NULL if fails to get a type DIE.
139 */
140Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
141{
142 Dwarf_Attribute attr;
143
144 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
145 dwarf_formref_die(&attr, die_mem))
146 return die_mem;
147 else
148 return NULL;
149}
150
151/* Get a type die, but skip qualifiers */
152static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
153{
154 int tag;
155
156 do {
157 vr_die = die_get_type(vr_die, die_mem);
158 if (!vr_die)
159 break;
160 tag = dwarf_tag(vr_die);
161 } while (tag == DW_TAG_const_type ||
162 tag == DW_TAG_restrict_type ||
163 tag == DW_TAG_volatile_type ||
164 tag == DW_TAG_shared_type);
165
166 return vr_die;
167}
168
169/**
170 * die_get_real_type - Get a type die, but skip qualifiers and typedef
171 * @vr_die: a DIE of a variable
172 * @die_mem: where to store a type DIE
173 *
174 * Get a DIE of the type of given variable (@vr_die), and store
175 * it to die_mem. Return NULL if fails to get a type DIE.
176 * If the type is qualifiers (e.g. const) or typedef, this skips it
177 * and tries to find real type (structure or basic types, e.g. int).
178 */
179Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
180{
181 do {
182 vr_die = __die_get_real_type(vr_die, die_mem);
183 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
184
185 return vr_die;
186}
187
188/* Get attribute and translate it as a udata */
189static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
190 Dwarf_Word *result)
191{
192 Dwarf_Attribute attr;
193
194 if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
195 dwarf_formudata(&attr, result) != 0)
196 return -ENOENT;
197
198 return 0;
199}
200
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900201/* Get attribute and translate it as a sdata */
202static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
203 Dwarf_Sword *result)
204{
205 Dwarf_Attribute attr;
206
207 if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
208 dwarf_formsdata(&attr, result) != 0)
209 return -ENOENT;
210
211 return 0;
212}
213
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900214/**
215 * die_is_signed_type - Check whether a type DIE is signed or not
216 * @tp_die: a DIE of a type
217 *
218 * Get the encoding of @tp_die and return true if the encoding
219 * is signed.
220 */
221bool die_is_signed_type(Dwarf_Die *tp_die)
222{
223 Dwarf_Word ret;
224
225 if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
226 return false;
227
228 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
229 ret == DW_ATE_signed_fixed);
230}
231
232/**
233 * die_get_data_member_location - Get the data-member offset
234 * @mb_die: a DIE of a member of a data structure
235 * @offs: The offset of the member in the data structure
236 *
237 * Get the offset of @mb_die in the data structure including @mb_die, and
238 * stores result offset to @offs. If any error occurs this returns errno.
239 */
240int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
241{
242 Dwarf_Attribute attr;
243 Dwarf_Op *expr;
244 size_t nexpr;
245 int ret;
246
247 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
248 return -ENOENT;
249
250 if (dwarf_formudata(&attr, offs) != 0) {
251 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
252 ret = dwarf_getlocation(&attr, &expr, &nexpr);
253 if (ret < 0 || nexpr == 0)
254 return -ENOENT;
255
256 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
257 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
258 expr[0].atom, nexpr);
259 return -ENOTSUP;
260 }
261 *offs = (Dwarf_Word)expr[0].number;
262 }
263 return 0;
264}
265
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900266/* Get the call file index number in CU DIE */
267static int die_get_call_fileno(Dwarf_Die *in_die)
268{
269 Dwarf_Sword idx;
270
271 if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
272 return (int)idx;
273 else
274 return -ENOENT;
275}
276
277/**
278 * die_get_call_file - Get callsite file name of inlined function instance
279 * @in_die: a DIE of an inlined function instance
280 *
281 * Get call-site file name of @in_die. This means from which file the inline
282 * function is called.
283 */
284const char *die_get_call_file(Dwarf_Die *in_die)
285{
286 Dwarf_Die cu_die;
287 Dwarf_Files *files;
288 int idx;
289
290 idx = die_get_call_fileno(in_die);
291 if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
292 dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
293 return NULL;
294
295 return dwarf_filesrc(files, idx, NULL, NULL);
296}
297
298
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900299/**
300 * die_find_child - Generic DIE search function in DIE tree
301 * @rt_die: a root DIE
302 * @callback: a callback function
303 * @data: a user data passed to the callback function
304 * @die_mem: a buffer for result DIE
305 *
306 * Trace DIE tree from @rt_die and call @callback for each child DIE.
307 * If @callback returns DIE_FIND_CB_END, this stores the DIE into
308 * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
309 * this continues to trace the tree. Optionally, @callback can return
310 * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
311 * the children and trace only the siblings respectively.
312 * Returns NULL if @callback can't find any appropriate DIE.
313 */
314Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
315 int (*callback)(Dwarf_Die *, void *),
316 void *data, Dwarf_Die *die_mem)
317{
318 Dwarf_Die child_die;
319 int ret;
320
321 ret = dwarf_child(rt_die, die_mem);
322 if (ret != 0)
323 return NULL;
324
325 do {
326 ret = callback(die_mem, data);
327 if (ret == DIE_FIND_CB_END)
328 return die_mem;
329
330 if ((ret & DIE_FIND_CB_CHILD) &&
331 die_find_child(die_mem, callback, data, &child_die)) {
332 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
333 return die_mem;
334 }
335 } while ((ret & DIE_FIND_CB_SIBLING) &&
336 dwarf_siblingof(die_mem, die_mem) == 0);
337
338 return NULL;
339}
340
341struct __addr_die_search_param {
342 Dwarf_Addr addr;
343 Dwarf_Die *die_mem;
344};
345
346/* die_find callback for non-inlined function search */
347static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
348{
349 struct __addr_die_search_param *ad = data;
350
351 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
352 dwarf_haspc(fn_die, ad->addr)) {
353 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
354 return DWARF_CB_ABORT;
355 }
356 return DWARF_CB_OK;
357}
358
359/**
360 * die_find_realfunc - Search a non-inlined function at given address
361 * @cu_die: a CU DIE which including @addr
362 * @addr: target address
363 * @die_mem: a buffer for result DIE
364 *
365 * Search a non-inlined function DIE which includes @addr. Stores the
366 * DIE to @die_mem and returns it if found. Returns NULl if failed.
367 */
368Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
369 Dwarf_Die *die_mem)
370{
371 struct __addr_die_search_param ad;
372 ad.addr = addr;
373 ad.die_mem = die_mem;
374 /* dwarf_getscopes can't find subprogram. */
375 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
376 return NULL;
377 else
378 return die_mem;
379}
380
381/* die_find callback for inline function search */
382static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
383{
384 Dwarf_Addr *addr = data;
385
386 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
387 dwarf_haspc(die_mem, *addr))
388 return DIE_FIND_CB_END;
389
390 return DIE_FIND_CB_CONTINUE;
391}
392
393/**
394 * die_find_inlinefunc - Search an inlined function at given address
395 * @cu_die: a CU DIE which including @addr
396 * @addr: target address
397 * @die_mem: a buffer for result DIE
398 *
399 * Search an inlined function DIE which includes @addr. Stores the
400 * DIE to @die_mem and returns it if found. Returns NULl if failed.
401 * If several inlined functions are expanded recursively, this trace
402 * it and returns deepest one.
403 */
404Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
405 Dwarf_Die *die_mem)
406{
407 Dwarf_Die tmp_die;
408
409 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
410 if (!sp_die)
411 return NULL;
412
413 /* Inlined function could be recursive. Trace it until fail */
414 while (sp_die) {
415 memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
416 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
417 &tmp_die);
418 }
419
420 return die_mem;
421}
422
423/* Line walker internal parameters */
424struct __line_walk_param {
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900425 bool recursive;
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900426 line_walk_callback_t callback;
427 void *data;
428 int retval;
429};
430
431static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
432{
433 struct __line_walk_param *lw = data;
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900434 Dwarf_Addr addr = 0;
435 const char *fname;
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900436 int lineno;
437
438 if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900439 fname = die_get_call_file(in_die);
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900440 lineno = die_get_call_lineno(in_die);
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900441 if (fname && lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
442 lw->retval = lw->callback(fname, lineno, addr, lw->data);
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900443 if (lw->retval != 0)
444 return DIE_FIND_CB_END;
445 }
446 }
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900447 if (!lw->recursive)
448 /* Don't need to search recursively */
449 return DIE_FIND_CB_SIBLING;
450
451 if (addr) {
452 fname = dwarf_decl_file(in_die);
453 if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
454 lw->retval = lw->callback(fname, lineno, addr, lw->data);
455 if (lw->retval != 0)
456 return DIE_FIND_CB_END;
457 }
458 }
459
460 /* Continue to search nested inlined function call-sites */
461 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900462}
463
464/* Walk on lines of blocks included in given DIE */
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900465static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900466 line_walk_callback_t callback, void *data)
467{
468 struct __line_walk_param lw = {
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900469 .recursive = recursive,
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900470 .callback = callback,
471 .data = data,
472 .retval = 0,
473 };
474 Dwarf_Die die_mem;
475 Dwarf_Addr addr;
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900476 const char *fname;
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900477 int lineno;
478
479 /* Handle function declaration line */
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900480 fname = dwarf_decl_file(sp_die);
481 if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900482 dwarf_entrypc(sp_die, &addr) == 0) {
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900483 lw.retval = callback(fname, lineno, addr, data);
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900484 if (lw.retval != 0)
485 goto done;
486 }
487 die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
488done:
489 return lw.retval;
490}
491
492static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
493{
494 struct __line_walk_param *lw = data;
495
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900496 lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900497 if (lw->retval != 0)
498 return DWARF_CB_ABORT;
499
500 return DWARF_CB_OK;
501}
502
503/**
504 * die_walk_lines - Walk on lines inside given DIE
Masami Hiramatsua1284052011-08-11 20:02:35 +0900505 * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900506 * @callback: callback routine
507 * @data: user data
508 *
509 * Walk on all lines inside given @rt_die and call @callback on each line.
510 * If the @rt_die is a function, walk only on the lines inside the function,
511 * otherwise @rt_die must be a CU DIE.
512 * Note that this walks not only dwarf line list, but also function entries
513 * and inline call-site.
514 */
515int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
516{
517 Dwarf_Lines *lines;
518 Dwarf_Line *line;
519 Dwarf_Addr addr;
520 const char *fname;
521 int lineno, ret = 0;
522 Dwarf_Die die_mem, *cu_die;
523 size_t nlines, i;
524
525 /* Get the CU die */
Masami Hiramatsua1284052011-08-11 20:02:35 +0900526 if (dwarf_tag(rt_die) != DW_TAG_compile_unit)
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900527 cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
528 else
529 cu_die = rt_die;
530 if (!cu_die) {
Masami Hiramatsua1284052011-08-11 20:02:35 +0900531 pr_debug2("Failed to get CU from given DIE.\n");
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900532 return -EINVAL;
533 }
534
535 /* Get lines list in the CU */
536 if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
537 pr_debug2("Failed to get source lines on this CU.\n");
538 return -ENOENT;
539 }
540 pr_debug2("Get %zd lines from this CU\n", nlines);
541
542 /* Walk on the lines on lines list */
543 for (i = 0; i < nlines; i++) {
544 line = dwarf_onesrcline(lines, i);
545 if (line == NULL ||
546 dwarf_lineno(line, &lineno) != 0 ||
547 dwarf_lineaddr(line, &addr) != 0) {
548 pr_debug2("Failed to get line info. "
549 "Possible error in debuginfo.\n");
550 continue;
551 }
552 /* Filter lines based on address */
553 if (rt_die != cu_die)
554 /*
555 * Address filtering
556 * The line is included in given function, and
557 * no inline block includes it.
558 */
559 if (!dwarf_haspc(rt_die, addr) ||
560 die_find_inlinefunc(rt_die, addr, &die_mem))
561 continue;
562 /* Get source line */
563 fname = dwarf_linesrc(line, NULL, NULL);
564
565 ret = callback(fname, lineno, addr, data);
566 if (ret != 0)
567 return ret;
568 }
569
570 /*
571 * Dwarf lines doesn't include function declarations and inlined
572 * subroutines. We have to check functions list or given function.
573 */
574 if (rt_die != cu_die)
Masami Hiramatsub0e9cb22011-08-11 20:02:41 +0900575 /*
576 * Don't need walk functions recursively, because nested
577 * inlined functions don't have lines of the specified DIE.
578 */
579 ret = __die_walk_funclines(rt_die, false, callback, data);
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900580 else {
581 struct __line_walk_param param = {
582 .callback = callback,
583 .data = data,
584 .retval = 0,
585 };
586 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
587 ret = param.retval;
588 }
589
590 return ret;
591}
592
593struct __find_variable_param {
594 const char *name;
595 Dwarf_Addr addr;
596};
597
598static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
599{
600 struct __find_variable_param *fvp = data;
601 int tag;
602
603 tag = dwarf_tag(die_mem);
604 if ((tag == DW_TAG_formal_parameter ||
605 tag == DW_TAG_variable) &&
606 die_compare_name(die_mem, fvp->name))
607 return DIE_FIND_CB_END;
608
609 if (dwarf_haspc(die_mem, fvp->addr))
610 return DIE_FIND_CB_CONTINUE;
611 else
612 return DIE_FIND_CB_SIBLING;
613}
614
615/**
616 * die_find_variable_at - Find a given name variable at given address
617 * @sp_die: a function DIE
618 * @name: variable name
619 * @addr: address
620 * @die_mem: a buffer for result DIE
621 *
622 * Find a variable DIE called @name at @addr in @sp_die.
623 */
624Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
625 Dwarf_Addr addr, Dwarf_Die *die_mem)
626{
627 struct __find_variable_param fvp = { .name = name, .addr = addr};
628
629 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
630 die_mem);
631}
632
633static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
634{
635 const char *name = data;
636
637 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
638 die_compare_name(die_mem, name))
639 return DIE_FIND_CB_END;
640
641 return DIE_FIND_CB_SIBLING;
642}
643
644/**
645 * die_find_member - Find a given name member in a data structure
646 * @st_die: a data structure type DIE
647 * @name: member name
648 * @die_mem: a buffer for result DIE
649 *
650 * Find a member DIE called @name in @st_die.
651 */
652Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
653 Dwarf_Die *die_mem)
654{
655 return die_find_child(st_die, __die_find_member_cb, (void *)name,
656 die_mem);
657}
658
659/**
660 * die_get_typename - Get the name of given variable DIE
661 * @vr_die: a variable DIE
662 * @buf: a buffer for result type name
663 * @len: a max-length of @buf
664 *
665 * Get the name of @vr_die and stores it to @buf. Return the actual length
666 * of type name if succeeded. Return -E2BIG if @len is not enough long, and
667 * Return -ENOENT if failed to find type name.
668 * Note that the result will stores typedef name if possible, and stores
669 * "*(function_type)" if the type is a function pointer.
670 */
671int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
672{
673 Dwarf_Die type;
674 int tag, ret, ret2;
675 const char *tmp = "";
676
677 if (__die_get_real_type(vr_die, &type) == NULL)
678 return -ENOENT;
679
680 tag = dwarf_tag(&type);
681 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
682 tmp = "*";
683 else if (tag == DW_TAG_subroutine_type) {
684 /* Function pointer */
685 ret = snprintf(buf, len, "(function_type)");
686 return (ret >= len) ? -E2BIG : ret;
687 } else {
688 if (!dwarf_diename(&type))
689 return -ENOENT;
690 if (tag == DW_TAG_union_type)
691 tmp = "union ";
692 else if (tag == DW_TAG_structure_type)
693 tmp = "struct ";
694 /* Write a base name */
695 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
696 return (ret >= len) ? -E2BIG : ret;
697 }
698 ret = die_get_typename(&type, buf, len);
699 if (ret > 0) {
700 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
701 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
702 }
703 return ret;
704}
705
706/**
707 * die_get_varname - Get the name and type of given variable DIE
708 * @vr_die: a variable DIE
709 * @buf: a buffer for type and variable name
710 * @len: the max-length of @buf
711 *
712 * Get the name and type of @vr_die and stores it in @buf as "type\tname".
713 */
714int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
715{
716 int ret, ret2;
717
718 ret = die_get_typename(vr_die, buf, len);
719 if (ret < 0) {
720 pr_debug("Failed to get type, make it unknown.\n");
721 ret = snprintf(buf, len, "(unknown_type)");
722 }
723 if (ret > 0) {
724 ret2 = snprintf(buf + ret, len - ret, "\t%s",
725 dwarf_diename(vr_die));
726 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
727 }
728 return ret;
729}
730