perf probe: Skip end-of-sequence and non statement lines
[linux-2.6-microblaze.git] / tools / perf / util / dwarf-aux.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dwarf-aux.c : libdw auxiliary interfaces
4  */
5
6 #include <errno.h>
7 #include <inttypes.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include "debug.h"
11 #include "dwarf-aux.h"
12 #include "strbuf.h"
13 #include "string2.h"
14
15 /**
16  * cu_find_realpath - Find the realpath of the target file
17  * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
18  * @fname:  The tail filename of the target file
19  *
20  * Find the real(long) path of @fname in @cu_die.
21  */
22 const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
23 {
24         Dwarf_Files *files;
25         size_t nfiles, i;
26         const char *src = NULL;
27         int ret;
28
29         if (!fname)
30                 return NULL;
31
32         ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
33         if (ret != 0)
34                 return NULL;
35
36         for (i = 0; i < nfiles; i++) {
37                 src = dwarf_filesrc(files, i, NULL, NULL);
38                 if (strtailcmp(src, fname) == 0)
39                         break;
40         }
41         if (i == nfiles)
42                 return NULL;
43         return src;
44 }
45
46 /**
47  * cu_get_comp_dir - Get the path of compilation directory
48  * @cu_die: a CU DIE
49  *
50  * Get the path of compilation directory of given @cu_die.
51  * Since this depends on DW_AT_comp_dir, older gcc will not
52  * embedded it. In that case, this returns NULL.
53  */
54 const char *cu_get_comp_dir(Dwarf_Die *cu_die)
55 {
56         Dwarf_Attribute attr;
57         if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
58                 return NULL;
59         return dwarf_formstring(&attr);
60 }
61
62 /**
63  * cu_find_lineinfo - Get a line number and file name for given address
64  * @cu_die: a CU DIE
65  * @addr: An address
66  * @fname: a pointer which returns the file name string
67  * @lineno: a pointer which returns the line number
68  *
69  * Find a line number and file name for @addr in @cu_die.
70  */
71 int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
72                     const char **fname, int *lineno)
73 {
74         Dwarf_Line *line;
75         Dwarf_Addr laddr;
76
77         line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr);
78         if (line && dwarf_lineaddr(line, &laddr) == 0 &&
79             addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
80                 *fname = dwarf_linesrc(line, NULL, NULL);
81                 if (!*fname)
82                         /* line number is useless without filename */
83                         *lineno = 0;
84         }
85
86         return *lineno ?: -ENOENT;
87 }
88
89 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
90
91 /**
92  * cu_walk_functions_at - Walk on function DIEs at given address
93  * @cu_die: A CU DIE
94  * @addr: An address
95  * @callback: A callback which called with found DIEs
96  * @data: A user data
97  *
98  * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
99  * should be subprogram or inlined-subroutines.
100  */
101 int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
102                     int (*callback)(Dwarf_Die *, void *), void *data)
103 {
104         Dwarf_Die die_mem;
105         Dwarf_Die *sc_die;
106         int ret = -ENOENT;
107
108         /* Inlined function could be recursive. Trace it until fail */
109         for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
110              sc_die != NULL;
111              sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
112                                      &die_mem)) {
113                 ret = callback(sc_die, data);
114                 if (ret)
115                         break;
116         }
117
118         return ret;
119
120 }
121
122 /**
123  * die_get_linkage_name - Get the linkage name of the object
124  * @dw_die: A DIE of the object
125  *
126  * Get the linkage name attiribute of given @dw_die.
127  * For C++ binary, the linkage name will be the mangled symbol.
128  */
129 const char *die_get_linkage_name(Dwarf_Die *dw_die)
130 {
131         Dwarf_Attribute attr;
132
133         if (dwarf_attr_integrate(dw_die, DW_AT_linkage_name, &attr) == NULL)
134                 return NULL;
135         return dwarf_formstring(&attr);
136 }
137
138 /**
139  * die_compare_name - Compare diename and tname
140  * @dw_die: a DIE
141  * @tname: a string of target name
142  *
143  * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
144  */
145 bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
146 {
147         const char *name;
148
149         name = dwarf_diename(dw_die);
150         return name ? (strcmp(tname, name) == 0) : false;
151 }
152
153 /**
154  * die_match_name - Match diename/linkage name and glob
155  * @dw_die: a DIE
156  * @glob: a string of target glob pattern
157  *
158  * Glob matching the name of @dw_die and @glob. Return false if matching fail.
159  * This also match linkage name.
160  */
161 bool die_match_name(Dwarf_Die *dw_die, const char *glob)
162 {
163         const char *name;
164
165         name = dwarf_diename(dw_die);
166         if (name && strglobmatch(name, glob))
167                 return true;
168         /* fall back to check linkage name */
169         name = die_get_linkage_name(dw_die);
170         if (name && strglobmatch(name, glob))
171                 return true;
172
173         return false;
174 }
175
176 /**
177  * die_get_call_lineno - Get callsite line number of inline-function instance
178  * @in_die: a DIE of an inlined function instance
179  *
180  * Get call-site line number of @in_die. This means from where the inline
181  * function is called.
182  */
183 int die_get_call_lineno(Dwarf_Die *in_die)
184 {
185         Dwarf_Attribute attr;
186         Dwarf_Word ret;
187
188         if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
189                 return -ENOENT;
190
191         dwarf_formudata(&attr, &ret);
192         return (int)ret;
193 }
194
195 /**
196  * die_get_type - Get type DIE
197  * @vr_die: a DIE of a variable
198  * @die_mem: where to store a type DIE
199  *
200  * Get a DIE of the type of given variable (@vr_die), and store
201  * it to die_mem. Return NULL if fails to get a type DIE.
202  */
203 Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
204 {
205         Dwarf_Attribute attr;
206
207         if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
208             dwarf_formref_die(&attr, die_mem))
209                 return die_mem;
210         else
211                 return NULL;
212 }
213
214 /* Get a type die, but skip qualifiers */
215 static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
216 {
217         int tag;
218
219         do {
220                 vr_die = die_get_type(vr_die, die_mem);
221                 if (!vr_die)
222                         break;
223                 tag = dwarf_tag(vr_die);
224         } while (tag == DW_TAG_const_type ||
225                  tag == DW_TAG_restrict_type ||
226                  tag == DW_TAG_volatile_type ||
227                  tag == DW_TAG_shared_type);
228
229         return vr_die;
230 }
231
232 /**
233  * die_get_real_type - Get a type die, but skip qualifiers and typedef
234  * @vr_die: a DIE of a variable
235  * @die_mem: where to store a type DIE
236  *
237  * Get a DIE of the type of given variable (@vr_die), and store
238  * it to die_mem. Return NULL if fails to get a type DIE.
239  * If the type is qualifiers (e.g. const) or typedef, this skips it
240  * and tries to find real type (structure or basic types, e.g. int).
241  */
242 Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
243 {
244         do {
245                 vr_die = __die_get_real_type(vr_die, die_mem);
246         } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
247
248         return vr_die;
249 }
250
251 /* Get attribute and translate it as a udata */
252 static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
253                               Dwarf_Word *result)
254 {
255         Dwarf_Attribute attr;
256
257         if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
258             dwarf_formudata(&attr, result) != 0)
259                 return -ENOENT;
260
261         return 0;
262 }
263
264 /* Get attribute and translate it as a sdata */
265 static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
266                               Dwarf_Sword *result)
267 {
268         Dwarf_Attribute attr;
269
270         if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
271             dwarf_formsdata(&attr, result) != 0)
272                 return -ENOENT;
273
274         return 0;
275 }
276
277 /**
278  * die_is_signed_type - Check whether a type DIE is signed or not
279  * @tp_die: a DIE of a type
280  *
281  * Get the encoding of @tp_die and return true if the encoding
282  * is signed.
283  */
284 bool die_is_signed_type(Dwarf_Die *tp_die)
285 {
286         Dwarf_Word ret;
287
288         if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
289                 return false;
290
291         return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
292                 ret == DW_ATE_signed_fixed);
293 }
294
295 /**
296  * die_is_func_def - Ensure that this DIE is a subprogram and definition
297  * @dw_die: a DIE
298  *
299  * Ensure that this DIE is a subprogram and NOT a declaration. This
300  * returns true if @dw_die is a function definition.
301  **/
302 bool die_is_func_def(Dwarf_Die *dw_die)
303 {
304         Dwarf_Attribute attr;
305
306         return (dwarf_tag(dw_die) == DW_TAG_subprogram &&
307                 dwarf_attr(dw_die, DW_AT_declaration, &attr) == NULL);
308 }
309
310 /**
311  * die_entrypc - Returns entry PC (the lowest address) of a DIE
312  * @dw_die: a DIE
313  * @addr: where to store entry PC
314  *
315  * Since dwarf_entrypc() does not return entry PC if the DIE has only address
316  * range, we have to use this to retrieve the lowest address from the address
317  * range attribute.
318  */
319 int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr)
320 {
321         Dwarf_Addr base, end;
322
323         if (!addr)
324                 return -EINVAL;
325
326         if (dwarf_entrypc(dw_die, addr) == 0)
327                 return 0;
328
329         return dwarf_ranges(dw_die, 0, &base, addr, &end) < 0 ? -ENOENT : 0;
330 }
331
332 /**
333  * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
334  * @dw_die: a DIE
335  *
336  * Ensure that this DIE is an instance (which has an entry address).
337  * This returns true if @dw_die is a function instance. If not, you need to
338  * call die_walk_instances() to find actual instances.
339  **/
340 bool die_is_func_instance(Dwarf_Die *dw_die)
341 {
342         Dwarf_Addr tmp;
343         Dwarf_Attribute attr_mem;
344
345         /* Actually gcc optimizes non-inline as like as inlined */
346         return !dwarf_func_inline(dw_die) &&
347                (dwarf_entrypc(dw_die, &tmp) == 0 ||
348                 dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL);
349 }
350
351 /**
352  * die_get_data_member_location - Get the data-member offset
353  * @mb_die: a DIE of a member of a data structure
354  * @offs: The offset of the member in the data structure
355  *
356  * Get the offset of @mb_die in the data structure including @mb_die, and
357  * stores result offset to @offs. If any error occurs this returns errno.
358  */
359 int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
360 {
361         Dwarf_Attribute attr;
362         Dwarf_Op *expr;
363         size_t nexpr;
364         int ret;
365
366         if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
367                 return -ENOENT;
368
369         if (dwarf_formudata(&attr, offs) != 0) {
370                 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
371                 ret = dwarf_getlocation(&attr, &expr, &nexpr);
372                 if (ret < 0 || nexpr == 0)
373                         return -ENOENT;
374
375                 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
376                         pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
377                                  expr[0].atom, nexpr);
378                         return -ENOTSUP;
379                 }
380                 *offs = (Dwarf_Word)expr[0].number;
381         }
382         return 0;
383 }
384
385 /* Get the call file index number in CU DIE */
386 static int die_get_call_fileno(Dwarf_Die *in_die)
387 {
388         Dwarf_Sword idx;
389
390         if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
391                 return (int)idx;
392         else
393                 return -ENOENT;
394 }
395
396 /* Get the declared file index number in CU DIE */
397 static int die_get_decl_fileno(Dwarf_Die *pdie)
398 {
399         Dwarf_Sword idx;
400
401         if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
402                 return (int)idx;
403         else
404                 return -ENOENT;
405 }
406
407 /**
408  * die_get_call_file - Get callsite file name of inlined function instance
409  * @in_die: a DIE of an inlined function instance
410  *
411  * Get call-site file name of @in_die. This means from which file the inline
412  * function is called.
413  */
414 const char *die_get_call_file(Dwarf_Die *in_die)
415 {
416         Dwarf_Die cu_die;
417         Dwarf_Files *files;
418         int idx;
419
420         idx = die_get_call_fileno(in_die);
421         if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
422             dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
423                 return NULL;
424
425         return dwarf_filesrc(files, idx, NULL, NULL);
426 }
427
428
429 /**
430  * die_find_child - Generic DIE search function in DIE tree
431  * @rt_die: a root DIE
432  * @callback: a callback function
433  * @data: a user data passed to the callback function
434  * @die_mem: a buffer for result DIE
435  *
436  * Trace DIE tree from @rt_die and call @callback for each child DIE.
437  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
438  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
439  * this continues to trace the tree. Optionally, @callback can return
440  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
441  * the children and trace only the siblings respectively.
442  * Returns NULL if @callback can't find any appropriate DIE.
443  */
444 Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
445                           int (*callback)(Dwarf_Die *, void *),
446                           void *data, Dwarf_Die *die_mem)
447 {
448         Dwarf_Die child_die;
449         int ret;
450
451         ret = dwarf_child(rt_die, die_mem);
452         if (ret != 0)
453                 return NULL;
454
455         do {
456                 ret = callback(die_mem, data);
457                 if (ret == DIE_FIND_CB_END)
458                         return die_mem;
459
460                 if ((ret & DIE_FIND_CB_CHILD) &&
461                     die_find_child(die_mem, callback, data, &child_die)) {
462                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
463                         return die_mem;
464                 }
465         } while ((ret & DIE_FIND_CB_SIBLING) &&
466                  dwarf_siblingof(die_mem, die_mem) == 0);
467
468         return NULL;
469 }
470
471 struct __addr_die_search_param {
472         Dwarf_Addr      addr;
473         Dwarf_Die       *die_mem;
474 };
475
476 static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
477 {
478         struct __addr_die_search_param *ad = data;
479         Dwarf_Addr addr = 0;
480
481         if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
482             !dwarf_highpc(fn_die, &addr) &&
483             addr == ad->addr) {
484                 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
485                 return DWARF_CB_ABORT;
486         }
487         return DWARF_CB_OK;
488 }
489
490 /**
491  * die_find_tailfunc - Search for a non-inlined function with tail call at
492  * given address
493  * @cu_die: a CU DIE which including @addr
494  * @addr: target address
495  * @die_mem: a buffer for result DIE
496  *
497  * Search for a non-inlined function DIE with tail call at @addr. Stores the
498  * DIE to @die_mem and returns it if found. Returns NULL if failed.
499  */
500 Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
501                                     Dwarf_Die *die_mem)
502 {
503         struct __addr_die_search_param ad;
504         ad.addr = addr;
505         ad.die_mem = die_mem;
506         /* dwarf_getscopes can't find subprogram. */
507         if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
508                 return NULL;
509         else
510                 return die_mem;
511 }
512
513 /* die_find callback for non-inlined function search */
514 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
515 {
516         struct __addr_die_search_param *ad = data;
517
518         /*
519          * Since a declaration entry doesn't has given pc, this always returns
520          * function definition entry.
521          */
522         if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
523             dwarf_haspc(fn_die, ad->addr)) {
524                 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
525                 return DWARF_CB_ABORT;
526         }
527         return DWARF_CB_OK;
528 }
529
530 /**
531  * die_find_realfunc - Search a non-inlined function at given address
532  * @cu_die: a CU DIE which including @addr
533  * @addr: target address
534  * @die_mem: a buffer for result DIE
535  *
536  * Search a non-inlined function DIE which includes @addr. Stores the
537  * DIE to @die_mem and returns it if found. Returns NULL if failed.
538  */
539 Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
540                                     Dwarf_Die *die_mem)
541 {
542         struct __addr_die_search_param ad;
543         ad.addr = addr;
544         ad.die_mem = die_mem;
545         /* dwarf_getscopes can't find subprogram. */
546         if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
547                 return NULL;
548         else
549                 return die_mem;
550 }
551
552 /* die_find callback for inline function search */
553 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
554 {
555         Dwarf_Addr *addr = data;
556
557         if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
558             dwarf_haspc(die_mem, *addr))
559                 return DIE_FIND_CB_END;
560
561         return DIE_FIND_CB_CONTINUE;
562 }
563
564 /**
565  * die_find_top_inlinefunc - Search the top inlined function at given address
566  * @sp_die: a subprogram DIE which including @addr
567  * @addr: target address
568  * @die_mem: a buffer for result DIE
569  *
570  * Search an inlined function DIE which includes @addr. Stores the
571  * DIE to @die_mem and returns it if found. Returns NULL if failed.
572  * Even if several inlined functions are expanded recursively, this
573  * doesn't trace it down, and returns the topmost one.
574  */
575 Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
576                                    Dwarf_Die *die_mem)
577 {
578         return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
579 }
580
581 /**
582  * die_find_inlinefunc - Search an inlined function at given address
583  * @sp_die: a subprogram DIE which including @addr
584  * @addr: target address
585  * @die_mem: a buffer for result DIE
586  *
587  * Search an inlined function DIE which includes @addr. Stores the
588  * DIE to @die_mem and returns it if found. Returns NULL if failed.
589  * If several inlined functions are expanded recursively, this trace
590  * it down and returns deepest one.
591  */
592 Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
593                                Dwarf_Die *die_mem)
594 {
595         Dwarf_Die tmp_die;
596
597         sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
598         if (!sp_die)
599                 return NULL;
600
601         /* Inlined function could be recursive. Trace it until fail */
602         while (sp_die) {
603                 memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
604                 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
605                                         &tmp_die);
606         }
607
608         return die_mem;
609 }
610
611 struct __instance_walk_param {
612         void    *addr;
613         int     (*callback)(Dwarf_Die *, void *);
614         void    *data;
615         int     retval;
616 };
617
618 static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
619 {
620         struct __instance_walk_param *iwp = data;
621         Dwarf_Attribute attr_mem;
622         Dwarf_Die origin_mem;
623         Dwarf_Attribute *attr;
624         Dwarf_Die *origin;
625         int tmp;
626
627         attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
628         if (attr == NULL)
629                 return DIE_FIND_CB_CONTINUE;
630
631         origin = dwarf_formref_die(attr, &origin_mem);
632         if (origin == NULL || origin->addr != iwp->addr)
633                 return DIE_FIND_CB_CONTINUE;
634
635         /* Ignore redundant instances */
636         if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
637                 dwarf_decl_line(origin, &tmp);
638                 if (die_get_call_lineno(inst) == tmp) {
639                         tmp = die_get_decl_fileno(origin);
640                         if (die_get_call_fileno(inst) == tmp)
641                                 return DIE_FIND_CB_CONTINUE;
642                 }
643         }
644
645         iwp->retval = iwp->callback(inst, iwp->data);
646
647         return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
648 }
649
650 /**
651  * die_walk_instances - Walk on instances of given DIE
652  * @or_die: an abstract original DIE
653  * @callback: a callback function which is called with instance DIE
654  * @data: user data
655  *
656  * Walk on the instances of give @in_die. @in_die must be an inlined function
657  * declartion. This returns the return value of @callback if it returns
658  * non-zero value, or -ENOENT if there is no instance.
659  */
660 int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
661                        void *data)
662 {
663         Dwarf_Die cu_die;
664         Dwarf_Die die_mem;
665         struct __instance_walk_param iwp = {
666                 .addr = or_die->addr,
667                 .callback = callback,
668                 .data = data,
669                 .retval = -ENOENT,
670         };
671
672         if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
673                 return -ENOENT;
674
675         die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
676
677         return iwp.retval;
678 }
679
680 /* Line walker internal parameters */
681 struct __line_walk_param {
682         bool recursive;
683         line_walk_callback_t callback;
684         void *data;
685         int retval;
686 };
687
688 static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
689 {
690         struct __line_walk_param *lw = data;
691         Dwarf_Addr addr = 0;
692         const char *fname;
693         int lineno;
694
695         if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
696                 fname = die_get_call_file(in_die);
697                 lineno = die_get_call_lineno(in_die);
698                 if (fname && lineno > 0 && die_entrypc(in_die, &addr) == 0) {
699                         lw->retval = lw->callback(fname, lineno, addr, lw->data);
700                         if (lw->retval != 0)
701                                 return DIE_FIND_CB_END;
702                 }
703                 if (!lw->recursive)
704                         return DIE_FIND_CB_SIBLING;
705         }
706
707         if (addr) {
708                 fname = dwarf_decl_file(in_die);
709                 if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
710                         lw->retval = lw->callback(fname, lineno, addr, lw->data);
711                         if (lw->retval != 0)
712                                 return DIE_FIND_CB_END;
713                 }
714         }
715
716         /* Continue to search nested inlined function call-sites */
717         return DIE_FIND_CB_CONTINUE;
718 }
719
720 /* Walk on lines of blocks included in given DIE */
721 static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
722                                 line_walk_callback_t callback, void *data)
723 {
724         struct __line_walk_param lw = {
725                 .recursive = recursive,
726                 .callback = callback,
727                 .data = data,
728                 .retval = 0,
729         };
730         Dwarf_Die die_mem;
731         Dwarf_Addr addr;
732         const char *fname;
733         int lineno;
734
735         /* Handle function declaration line */
736         fname = dwarf_decl_file(sp_die);
737         if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
738             die_entrypc(sp_die, &addr) == 0) {
739                 lw.retval = callback(fname, lineno, addr, data);
740                 if (lw.retval != 0)
741                         goto done;
742         }
743         die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
744 done:
745         return lw.retval;
746 }
747
748 static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
749 {
750         struct __line_walk_param *lw = data;
751
752         /*
753          * Since inlined function can include another inlined function in
754          * the same file, we need to walk in it recursively.
755          */
756         lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
757         if (lw->retval != 0)
758                 return DWARF_CB_ABORT;
759
760         return DWARF_CB_OK;
761 }
762
763 /**
764  * die_walk_lines - Walk on lines inside given DIE
765  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
766  * @callback: callback routine
767  * @data: user data
768  *
769  * Walk on all lines inside given @rt_die and call @callback on each line.
770  * If the @rt_die is a function, walk only on the lines inside the function,
771  * otherwise @rt_die must be a CU DIE.
772  * Note that this walks not only dwarf line list, but also function entries
773  * and inline call-site.
774  */
775 int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
776 {
777         Dwarf_Lines *lines;
778         Dwarf_Line *line;
779         Dwarf_Addr addr;
780         const char *fname, *decf = NULL;
781         int lineno, ret = 0;
782         int decl = 0, inl;
783         Dwarf_Die die_mem, *cu_die;
784         size_t nlines, i;
785         bool flag;
786
787         /* Get the CU die */
788         if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
789                 cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
790                 dwarf_decl_line(rt_die, &decl);
791                 decf = dwarf_decl_file(rt_die);
792         } else
793                 cu_die = rt_die;
794         if (!cu_die) {
795                 pr_debug2("Failed to get CU from given DIE.\n");
796                 return -EINVAL;
797         }
798
799         /* Get lines list in the CU */
800         if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
801                 pr_debug2("Failed to get source lines on this CU.\n");
802                 return -ENOENT;
803         }
804         pr_debug2("Get %zd lines from this CU\n", nlines);
805
806         /* Walk on the lines on lines list */
807         for (i = 0; i < nlines; i++) {
808                 line = dwarf_onesrcline(lines, i);
809                 if (line == NULL ||
810                     dwarf_lineno(line, &lineno) != 0 ||
811                     dwarf_lineaddr(line, &addr) != 0) {
812                         pr_debug2("Failed to get line info. "
813                                   "Possible error in debuginfo.\n");
814                         continue;
815                 }
816                 /* Skip end-of-sequence */
817                 if (dwarf_lineendsequence(line, &flag) != 0 || flag)
818                         continue;
819                 /* Skip Non statement line-info */
820                 if (dwarf_linebeginstatement(line, &flag) != 0 || !flag)
821                         continue;
822                 /* Filter lines based on address */
823                 if (rt_die != cu_die) {
824                         /*
825                          * Address filtering
826                          * The line is included in given function, and
827                          * no inline block includes it.
828                          */
829                         if (!dwarf_haspc(rt_die, addr))
830                                 continue;
831                         if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
832                                 dwarf_decl_line(&die_mem, &inl);
833                                 if (inl != decl ||
834                                     decf != dwarf_decl_file(&die_mem))
835                                         continue;
836                         }
837                 }
838                 /* Get source line */
839                 fname = dwarf_linesrc(line, NULL, NULL);
840
841                 ret = callback(fname, lineno, addr, data);
842                 if (ret != 0)
843                         return ret;
844         }
845
846         /*
847          * Dwarf lines doesn't include function declarations and inlined
848          * subroutines. We have to check functions list or given function.
849          */
850         if (rt_die != cu_die)
851                 /*
852                  * Don't need walk inlined functions recursively, because
853                  * inner inlined functions don't have the lines of the
854                  * specified function.
855                  */
856                 ret = __die_walk_funclines(rt_die, false, callback, data);
857         else {
858                 struct __line_walk_param param = {
859                         .callback = callback,
860                         .data = data,
861                         .retval = 0,
862                 };
863                 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
864                 ret = param.retval;
865         }
866
867         return ret;
868 }
869
870 struct __find_variable_param {
871         const char *name;
872         Dwarf_Addr addr;
873 };
874
875 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
876 {
877         struct __find_variable_param *fvp = data;
878         Dwarf_Attribute attr;
879         int tag;
880
881         tag = dwarf_tag(die_mem);
882         if ((tag == DW_TAG_formal_parameter ||
883              tag == DW_TAG_variable) &&
884             die_compare_name(die_mem, fvp->name) &&
885         /* Does the DIE have location information or external instance? */
886             (dwarf_attr(die_mem, DW_AT_external, &attr) ||
887              dwarf_attr(die_mem, DW_AT_location, &attr)))
888                 return DIE_FIND_CB_END;
889         if (dwarf_haspc(die_mem, fvp->addr))
890                 return DIE_FIND_CB_CONTINUE;
891         else
892                 return DIE_FIND_CB_SIBLING;
893 }
894
895 /**
896  * die_find_variable_at - Find a given name variable at given address
897  * @sp_die: a function DIE
898  * @name: variable name
899  * @addr: address
900  * @die_mem: a buffer for result DIE
901  *
902  * Find a variable DIE called @name at @addr in @sp_die.
903  */
904 Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
905                                 Dwarf_Addr addr, Dwarf_Die *die_mem)
906 {
907         struct __find_variable_param fvp = { .name = name, .addr = addr};
908
909         return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
910                               die_mem);
911 }
912
913 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
914 {
915         const char *name = data;
916
917         if (dwarf_tag(die_mem) == DW_TAG_member) {
918                 if (die_compare_name(die_mem, name))
919                         return DIE_FIND_CB_END;
920                 else if (!dwarf_diename(die_mem)) {     /* Unnamed structure */
921                         Dwarf_Die type_die, tmp_die;
922                         if (die_get_type(die_mem, &type_die) &&
923                             die_find_member(&type_die, name, &tmp_die))
924                                 return DIE_FIND_CB_END;
925                 }
926         }
927         return DIE_FIND_CB_SIBLING;
928 }
929
930 /**
931  * die_find_member - Find a given name member in a data structure
932  * @st_die: a data structure type DIE
933  * @name: member name
934  * @die_mem: a buffer for result DIE
935  *
936  * Find a member DIE called @name in @st_die.
937  */
938 Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
939                            Dwarf_Die *die_mem)
940 {
941         return die_find_child(st_die, __die_find_member_cb, (void *)name,
942                               die_mem);
943 }
944
945 /**
946  * die_get_typename - Get the name of given variable DIE
947  * @vr_die: a variable DIE
948  * @buf: a strbuf for result type name
949  *
950  * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
951  * and Return -ENOENT if failed to find type name.
952  * Note that the result will stores typedef name if possible, and stores
953  * "*(function_type)" if the type is a function pointer.
954  */
955 int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
956 {
957         Dwarf_Die type;
958         int tag, ret;
959         const char *tmp = "";
960
961         if (__die_get_real_type(vr_die, &type) == NULL)
962                 return -ENOENT;
963
964         tag = dwarf_tag(&type);
965         if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
966                 tmp = "*";
967         else if (tag == DW_TAG_subroutine_type) {
968                 /* Function pointer */
969                 return strbuf_add(buf, "(function_type)", 15);
970         } else {
971                 if (!dwarf_diename(&type))
972                         return -ENOENT;
973                 if (tag == DW_TAG_union_type)
974                         tmp = "union ";
975                 else if (tag == DW_TAG_structure_type)
976                         tmp = "struct ";
977                 else if (tag == DW_TAG_enumeration_type)
978                         tmp = "enum ";
979                 /* Write a base name */
980                 return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
981         }
982         ret = die_get_typename(&type, buf);
983         return ret ? ret : strbuf_addstr(buf, tmp);
984 }
985
986 /**
987  * die_get_varname - Get the name and type of given variable DIE
988  * @vr_die: a variable DIE
989  * @buf: a strbuf for type and variable name
990  *
991  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
992  */
993 int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
994 {
995         int ret;
996
997         ret = die_get_typename(vr_die, buf);
998         if (ret < 0) {
999                 pr_debug("Failed to get type, make it unknown.\n");
1000                 ret = strbuf_add(buf, " (unknown_type)", 14);
1001         }
1002
1003         return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
1004 }
1005
1006 #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
1007 /**
1008  * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
1009  * @sp_die: a subprogram DIE
1010  * @vr_die: a variable DIE
1011  * @buf: a strbuf for variable byte offset range
1012  *
1013  * Get the innermost scope range of @vr_die and stores it in @buf as
1014  * "@<function_name+[NN-NN,NN-NN]>".
1015  */
1016 static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
1017                                 struct strbuf *buf)
1018 {
1019         Dwarf_Die *scopes;
1020         int count;
1021         size_t offset = 0;
1022         Dwarf_Addr base;
1023         Dwarf_Addr start, end;
1024         Dwarf_Addr entry;
1025         int ret;
1026         bool first = true;
1027         const char *name;
1028
1029         ret = die_entrypc(sp_die, &entry);
1030         if (ret)
1031                 return ret;
1032
1033         name = dwarf_diename(sp_die);
1034         if (!name)
1035                 return -ENOENT;
1036
1037         count = dwarf_getscopes_die(vr_die, &scopes);
1038
1039         /* (*SCOPES)[1] is the DIE for the scope containing that scope */
1040         if (count <= 1) {
1041                 ret = -EINVAL;
1042                 goto out;
1043         }
1044
1045         while ((offset = dwarf_ranges(&scopes[1], offset, &base,
1046                                         &start, &end)) > 0) {
1047                 start -= entry;
1048                 end -= entry;
1049
1050                 if (first) {
1051                         ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1052                                           name, start, end);
1053                         first = false;
1054                 } else {
1055                         ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1056                                           start, end);
1057                 }
1058                 if (ret < 0)
1059                         goto out;
1060         }
1061
1062         if (!first)
1063                 ret = strbuf_add(buf, "]>", 2);
1064
1065 out:
1066         free(scopes);
1067         return ret;
1068 }
1069
1070 /**
1071  * die_get_var_range - Get byte offset range of given variable DIE
1072  * @sp_die: a subprogram DIE
1073  * @vr_die: a variable DIE
1074  * @buf: a strbuf for type and variable name and byte offset range
1075  *
1076  * Get the byte offset range of @vr_die and stores it in @buf as
1077  * "@<function_name+[NN-NN,NN-NN]>".
1078  */
1079 int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
1080 {
1081         int ret = 0;
1082         Dwarf_Addr base;
1083         Dwarf_Addr start, end;
1084         Dwarf_Addr entry;
1085         Dwarf_Op *op;
1086         size_t nops;
1087         size_t offset = 0;
1088         Dwarf_Attribute attr;
1089         bool first = true;
1090         const char *name;
1091
1092         ret = die_entrypc(sp_die, &entry);
1093         if (ret)
1094                 return ret;
1095
1096         name = dwarf_diename(sp_die);
1097         if (!name)
1098                 return -ENOENT;
1099
1100         if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
1101                 return -EINVAL;
1102
1103         while ((offset = dwarf_getlocations(&attr, offset, &base,
1104                                         &start, &end, &op, &nops)) > 0) {
1105                 if (start == 0) {
1106                         /* Single Location Descriptions */
1107                         ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
1108                         goto out;
1109                 }
1110
1111                 /* Location Lists */
1112                 start -= entry;
1113                 end -= entry;
1114                 if (first) {
1115                         ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1116                                           name, start, end);
1117                         first = false;
1118                 } else {
1119                         ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1120                                           start, end);
1121                 }
1122                 if (ret < 0)
1123                         goto out;
1124         }
1125
1126         if (!first)
1127                 ret = strbuf_add(buf, "]>", 2);
1128 out:
1129         return ret;
1130 }
1131 #else
1132 int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
1133                       Dwarf_Die *vr_die __maybe_unused,
1134                       struct strbuf *buf __maybe_unused)
1135 {
1136         return -ENOTSUP;
1137 }
1138 #endif
1139
1140 /*
1141  * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
1142  * @vr_die: a variable DIE
1143  */
1144 static bool die_has_loclist(Dwarf_Die *vr_die)
1145 {
1146         Dwarf_Attribute loc;
1147         int tag = dwarf_tag(vr_die);
1148
1149         if (tag != DW_TAG_formal_parameter &&
1150             tag != DW_TAG_variable)
1151                 return false;
1152
1153         return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
1154                 dwarf_whatform(&loc) == DW_FORM_sec_offset);
1155 }
1156
1157 /*
1158  * die_is_optimized_target - Check if target program is compiled with
1159  * optimization
1160  * @cu_die: a CU DIE
1161  *
1162  * For any object in given CU whose DW_AT_location is a location list,
1163  * target program is compiled with optimization. This is applicable to
1164  * clang as well.
1165  */
1166 bool die_is_optimized_target(Dwarf_Die *cu_die)
1167 {
1168         Dwarf_Die tmp_die;
1169
1170         if (die_has_loclist(cu_die))
1171                 return true;
1172
1173         if (!dwarf_child(cu_die, &tmp_die) &&
1174             die_is_optimized_target(&tmp_die))
1175                 return true;
1176
1177         if (!dwarf_siblingof(cu_die, &tmp_die) &&
1178             die_is_optimized_target(&tmp_die))
1179                 return true;
1180
1181         return false;
1182 }
1183
1184 /*
1185  * die_search_idx - Search index of given line address
1186  * @lines: Line records of single CU
1187  * @nr_lines: Number of @lines
1188  * @addr: address we are looking for
1189  * @idx: index to be set by this function (return value)
1190  *
1191  * Search for @addr by looping over every lines of CU. If address
1192  * matches, set index of that line in @idx. Note that single source
1193  * line can have multiple line records. i.e. single source line can
1194  * have multiple index.
1195  */
1196 static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
1197                            Dwarf_Addr addr, unsigned long *idx)
1198 {
1199         unsigned long i;
1200         Dwarf_Addr tmp;
1201
1202         for (i = 0; i < nr_lines; i++) {
1203                 if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
1204                         return false;
1205
1206                 if (tmp == addr) {
1207                         *idx = i;
1208                         return true;
1209                 }
1210         }
1211         return false;
1212 }
1213
1214 /*
1215  * die_get_postprologue_addr - Search next address after function prologue
1216  * @entrypc_idx: entrypc index
1217  * @lines: Line records of single CU
1218  * @nr_lines: Number of @lines
1219  * @hignpc: high PC address of function
1220  * @postprologue_addr: Next address after function prologue (return value)
1221  *
1222  * Look for prologue-end marker. If there is no explicit marker, return
1223  * address of next line record or next source line.
1224  */
1225 static bool die_get_postprologue_addr(unsigned long entrypc_idx,
1226                                       Dwarf_Lines *lines,
1227                                       unsigned long nr_lines,
1228                                       Dwarf_Addr highpc,
1229                                       Dwarf_Addr *postprologue_addr)
1230 {
1231         unsigned long i;
1232         int entrypc_lno, lno;
1233         Dwarf_Line *line;
1234         Dwarf_Addr addr;
1235         bool p_end;
1236
1237         /* entrypc_lno is actual source line number */
1238         line = dwarf_onesrcline(lines, entrypc_idx);
1239         if (dwarf_lineno(line, &entrypc_lno))
1240                 return false;
1241
1242         for (i = entrypc_idx; i < nr_lines; i++) {
1243                 line = dwarf_onesrcline(lines, i);
1244
1245                 if (dwarf_lineaddr(line, &addr) ||
1246                     dwarf_lineno(line, &lno)    ||
1247                     dwarf_lineprologueend(line, &p_end))
1248                         return false;
1249
1250                 /* highpc is exclusive. [entrypc,highpc) */
1251                 if (addr >= highpc)
1252                         break;
1253
1254                 /* clang supports prologue-end marker */
1255                 if (p_end)
1256                         break;
1257
1258                 /* Actual next line in source */
1259                 if (lno != entrypc_lno)
1260                         break;
1261
1262                 /*
1263                  * Single source line can have multiple line records.
1264                  * For Example,
1265                  *     void foo() { printf("hello\n"); }
1266                  * contains two line records. One points to declaration and
1267                  * other points to printf() line. Variable 'lno' won't get
1268                  * incremented in this case but 'i' will.
1269                  */
1270                 if (i != entrypc_idx)
1271                         break;
1272         }
1273
1274         dwarf_lineaddr(line, postprologue_addr);
1275         if (*postprologue_addr >= highpc)
1276                 dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
1277                                postprologue_addr);
1278
1279         return true;
1280 }
1281
1282 /*
1283  * die_skip_prologue - Use next address after prologue as probe location
1284  * @sp_die: a subprogram DIE
1285  * @cu_die: a CU DIE
1286  * @entrypc: entrypc of the function
1287  *
1288  * Function prologue prepares stack and registers before executing function
1289  * logic. When target program is compiled without optimization, function
1290  * parameter information is only valid after prologue. When we probe entrypc
1291  * of the function, and try to record function parameter, it contains
1292  * garbage value.
1293  */
1294 void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
1295                        Dwarf_Addr *entrypc)
1296 {
1297         size_t nr_lines = 0;
1298         unsigned long entrypc_idx = 0;
1299         Dwarf_Lines *lines = NULL;
1300         Dwarf_Addr postprologue_addr;
1301         Dwarf_Addr highpc;
1302
1303         if (dwarf_highpc(sp_die, &highpc))
1304                 return;
1305
1306         if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
1307                 return;
1308
1309         if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
1310                 return;
1311
1312         if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
1313                                        highpc, &postprologue_addr))
1314                 return;
1315
1316         *entrypc = postprologue_addr;
1317 }