perf probe: Introduce die_find_child() function
[cascardo/linux.git] / tools / perf / util / probe-finder.c
1 /*
2  * probe-finder.c : C expression to kprobe event converter
3  *
4  * Written by Masami Hiramatsu <mhiramat@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #include <sys/utsname.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34
35 #include "string.h"
36 #include "event.h"
37 #include "debug.h"
38 #include "util.h"
39 #include "probe-finder.h"
40
41
42 /*
43  * Generic dwarf analysis helpers
44  */
45
46 #define X86_32_MAX_REGS 8
47 const char *x86_32_regs_table[X86_32_MAX_REGS] = {
48         "%ax",
49         "%cx",
50         "%dx",
51         "%bx",
52         "$stack",       /* Stack address instead of %sp */
53         "%bp",
54         "%si",
55         "%di",
56 };
57
58 #define X86_64_MAX_REGS 16
59 const char *x86_64_regs_table[X86_64_MAX_REGS] = {
60         "%ax",
61         "%dx",
62         "%cx",
63         "%bx",
64         "%si",
65         "%di",
66         "%bp",
67         "%sp",
68         "%r8",
69         "%r9",
70         "%r10",
71         "%r11",
72         "%r12",
73         "%r13",
74         "%r14",
75         "%r15",
76 };
77
78 /* TODO: switching by dwarf address size */
79 #ifdef __x86_64__
80 #define ARCH_MAX_REGS X86_64_MAX_REGS
81 #define arch_regs_table x86_64_regs_table
82 #else
83 #define ARCH_MAX_REGS X86_32_MAX_REGS
84 #define arch_regs_table x86_32_regs_table
85 #endif
86
87 /* Return architecture dependent register string (for kprobe-tracer) */
88 static const char *get_arch_regstr(unsigned int n)
89 {
90         return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
91 }
92
93 /*
94  * Compare the tail of two strings.
95  * Return 0 if whole of either string is same as another's tail part.
96  */
97 static int strtailcmp(const char *s1, const char *s2)
98 {
99         int i1 = strlen(s1);
100         int i2 = strlen(s2);
101         while (--i1 >= 0 && --i2 >= 0) {
102                 if (s1[i1] != s2[i2])
103                         return s1[i1] - s2[i2];
104         }
105         return 0;
106 }
107
108 /* Line number list operations */
109
110 /* Add a line to line number list */
111 static void line_list__add_line(struct list_head *head, unsigned int line)
112 {
113         struct line_node *ln;
114         struct list_head *p;
115
116         /* Reverse search, because new line will be the last one */
117         list_for_each_entry_reverse(ln, head, list) {
118                 if (ln->line < line) {
119                         p = &ln->list;
120                         goto found;
121                 } else if (ln->line == line)    /* Already exist */
122                         return ;
123         }
124         /* List is empty, or the smallest entry */
125         p = head;
126 found:
127         pr_debug("line list: add a line %u\n", line);
128         ln = xzalloc(sizeof(struct line_node));
129         ln->line = line;
130         INIT_LIST_HEAD(&ln->list);
131         list_add(&ln->list, p);
132 }
133
134 /* Check if the line in line number list */
135 static int line_list__has_line(struct list_head *head, unsigned int line)
136 {
137         struct line_node *ln;
138
139         /* Reverse search, because new line will be the last one */
140         list_for_each_entry(ln, head, list)
141                 if (ln->line == line)
142                         return 1;
143
144         return 0;
145 }
146
147 /* Init line number list */
148 static void line_list__init(struct list_head *head)
149 {
150         INIT_LIST_HEAD(head);
151 }
152
153 /* Free line number list */
154 static void line_list__free(struct list_head *head)
155 {
156         struct line_node *ln;
157         while (!list_empty(head)) {
158                 ln = list_first_entry(head, struct line_node, list);
159                 list_del(&ln->list);
160                 free(ln);
161         }
162 }
163
164 /* Dwarf wrappers */
165
166 /* Find the realpath of the target file. */
167 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
168 {
169         Dwarf_Files *files;
170         size_t nfiles, i;
171         const char *src = NULL;
172         int ret;
173
174         if (!fname)
175                 return NULL;
176
177         ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
178         if (ret != 0)
179                 return NULL;
180
181         for (i = 0; i < nfiles; i++) {
182                 src = dwarf_filesrc(files, i, NULL, NULL);
183                 if (strtailcmp(src, fname) == 0)
184                         break;
185         }
186         return src;
187 }
188
189 /* Compare diename and tname */
190 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
191 {
192         const char *name;
193         name = dwarf_diename(dw_die);
194         DIE_IF(name == NULL);
195         return strcmp(tname, name);
196 }
197
198 /* Get entry pc(or low pc, 1st entry of ranges)  of the die */
199 static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
200 {
201         Dwarf_Addr epc;
202         int ret;
203
204         ret = dwarf_entrypc(dw_die, &epc);
205         DIE_IF(ret == -1);
206         return epc;
207 }
208
209 /* Return values for die_find callbacks */
210 enum {
211         DIE_FIND_CB_FOUND = 0,          /* End of Search */
212         DIE_FIND_CB_CHILD = 1,          /* Search only children */
213         DIE_FIND_CB_SIBLING = 2,        /* Search only siblings */
214         DIE_FIND_CB_CONTINUE = 3,       /* Search children and siblings */
215 };
216
217 /* Search a child die */
218 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
219                                  int (*callback)(Dwarf_Die *, void *),
220                                  void *data, Dwarf_Die *die_mem)
221 {
222         Dwarf_Die child_die;
223         int ret;
224
225         ret = dwarf_child(rt_die, die_mem);
226         if (ret != 0)
227                 return NULL;
228
229         do {
230                 ret = callback(die_mem, data);
231                 if (ret == DIE_FIND_CB_FOUND)
232                         return die_mem;
233
234                 if ((ret & DIE_FIND_CB_CHILD) &&
235                     die_find_child(die_mem, callback, data, &child_die)) {
236                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
237                         return die_mem;
238                 }
239         } while ((ret & DIE_FIND_CB_SIBLING) &&
240                  dwarf_siblingof(die_mem, die_mem) == 0);
241
242         return NULL;
243 }
244
245 struct __addr_die_search_param {
246         Dwarf_Addr      addr;
247         Dwarf_Die       *die_mem;
248 };
249
250 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
251 {
252         struct __addr_die_search_param *ad = data;
253
254         if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
255             dwarf_haspc(fn_die, ad->addr)) {
256                 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
257                 return DWARF_CB_ABORT;
258         }
259         return DWARF_CB_OK;
260 }
261
262 /* Search a real subprogram including this line, */
263 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
264                                            Dwarf_Die *die_mem)
265 {
266         struct __addr_die_search_param ad;
267         ad.addr = addr;
268         ad.die_mem = die_mem;
269         /* dwarf_getscopes can't find subprogram. */
270         if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
271                 return NULL;
272         else
273                 return die_mem;
274 }
275
276 /* die_find callback for inline function search */
277 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
278 {
279         Dwarf_Addr *addr = data;
280
281         if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
282             dwarf_haspc(die_mem, *addr))
283                 return DIE_FIND_CB_FOUND;
284
285         return DIE_FIND_CB_CONTINUE;
286 }
287
288 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
289 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
290                                       Dwarf_Die *die_mem)
291 {
292         return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
293 }
294
295 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
296 {
297         const char *name = data;
298         int tag;
299
300         tag = dwarf_tag(die_mem);
301         if ((tag == DW_TAG_formal_parameter ||
302              tag == DW_TAG_variable) &&
303             (die_compare_name(die_mem, name) == 0))
304                 return DIE_FIND_CB_FOUND;
305
306         return DIE_FIND_CB_CONTINUE;
307 }
308
309 /* Find a variable called 'name' */
310 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
311                                     Dwarf_Die *die_mem)
312 {
313         return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
314                               die_mem);
315 }
316
317 /*
318  * Probe finder related functions
319  */
320
321 /* Show a location */
322 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
323 {
324         unsigned int regn;
325         Dwarf_Word offs = 0;
326         int deref = 0, ret;
327         const char *regs;
328
329         /* TODO: support CFA */
330         /* If this is based on frame buffer, set the offset */
331         if (op->atom == DW_OP_fbreg) {
332                 if (pf->fb_ops == NULL)
333                         die("The attribute of frame base is not supported.\n");
334                 deref = 1;
335                 offs = op->number;
336                 op = &pf->fb_ops[0];
337         }
338
339         if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
340                 regn = op->atom - DW_OP_breg0;
341                 offs += op->number;
342                 deref = 1;
343         } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
344                 regn = op->atom - DW_OP_reg0;
345         } else if (op->atom == DW_OP_bregx) {
346                 regn = op->number;
347                 offs += op->number2;
348                 deref = 1;
349         } else if (op->atom == DW_OP_regx) {
350                 regn = op->number;
351         } else
352                 die("DW_OP %d is not supported.", op->atom);
353
354         regs = get_arch_regstr(regn);
355         if (!regs)
356                 die("%u exceeds max register number.", regn);
357
358         if (deref)
359                 ret = snprintf(pf->buf, pf->len, " %s=%+jd(%s)",
360                                pf->var, (intmax_t)offs, regs);
361         else
362                 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
363         DIE_IF(ret < 0);
364         DIE_IF(ret >= pf->len);
365 }
366
367 /* Show a variables in kprobe event format */
368 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
369 {
370         Dwarf_Attribute attr;
371         Dwarf_Op *expr;
372         size_t nexpr;
373         int ret;
374
375         if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
376                 goto error;
377         /* TODO: handle more than 1 exprs */
378         ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
379         if (ret <= 0 || nexpr == 0)
380                 goto error;
381
382         show_location(expr, pf);
383         /* *expr will be cached in libdw. Don't free it. */
384         return ;
385 error:
386         /* TODO: Support const_value */
387         die("Failed to find the location of %s at this address.\n"
388             " Perhaps, it has been optimized out.", pf->var);
389 }
390
391 /* Find a variable in a subprogram die */
392 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
393 {
394         int ret;
395         Dwarf_Die vr_die;
396
397         /* TODO: Support struct members and arrays */
398         if (!is_c_varname(pf->var)) {
399                 /* Output raw parameters */
400                 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
401                 DIE_IF(ret < 0);
402                 DIE_IF(ret >= pf->len);
403                 return ;
404         }
405
406         pr_debug("Searching '%s' variable in context.\n", pf->var);
407         /* Search child die for local variables and parameters. */
408         if (!die_find_variable(sp_die, pf->var, &vr_die))
409                 die("Failed to find '%s' in this function.", pf->var);
410
411         show_variable(&vr_die, pf);
412 }
413
414 /* Show a probe point to output buffer */
415 static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
416 {
417         struct probe_point *pp = pf->pp;
418         Dwarf_Addr eaddr;
419         Dwarf_Die die_mem;
420         const char *name;
421         char tmp[MAX_PROBE_BUFFER];
422         int ret, i, len;
423         Dwarf_Attribute fb_attr;
424         size_t nops;
425
426         /* If no real subprogram, find a real one */
427         if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
428                 sp_die = die_find_real_subprogram(&pf->cu_die,
429                                                  pf->addr, &die_mem);
430                 if (!sp_die)
431                         die("Probe point is not found in subprograms.");
432         }
433
434         /* Output name of probe point */
435         name = dwarf_diename(sp_die);
436         if (name) {
437                 dwarf_entrypc(sp_die, &eaddr);
438                 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
439                                 (unsigned long)(pf->addr - eaddr));
440                 /* Copy the function name if possible */
441                 if (!pp->function) {
442                         pp->function = xstrdup(name);
443                         pp->offset = (size_t)(pf->addr - eaddr);
444                 }
445         } else {
446                 /* This function has no name. */
447                 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
448                                (uintmax_t)pf->addr);
449                 if (!pp->function) {
450                         /* TODO: Use _stext */
451                         pp->function = xstrdup("");
452                         pp->offset = (size_t)pf->addr;
453                 }
454         }
455         DIE_IF(ret < 0);
456         DIE_IF(ret >= MAX_PROBE_BUFFER);
457         len = ret;
458         pr_debug("Probe point found: %s\n", tmp);
459
460         /* Get the frame base attribute/ops */
461         dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
462         ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
463         if (ret <= 0 || nops == 0)
464                 pf->fb_ops = NULL;
465
466         /* Find each argument */
467         /* TODO: use dwarf_cfi_addrframe */
468         for (i = 0; i < pp->nr_args; i++) {
469                 pf->var = pp->args[i];
470                 pf->buf = &tmp[len];
471                 pf->len = MAX_PROBE_BUFFER - len;
472                 find_variable(sp_die, pf);
473                 len += strlen(pf->buf);
474         }
475
476         /* *pf->fb_ops will be cached in libdw. Don't free it. */
477         pf->fb_ops = NULL;
478
479         if (pp->found == MAX_PROBES)
480                 die("Too many( > %d) probe point found.\n", MAX_PROBES);
481
482         pp->probes[pp->found] = xstrdup(tmp);
483         pp->found++;
484 }
485
486 /* Find probe point from its line number */
487 static void find_probe_point_by_line(struct probe_finder *pf)
488 {
489         Dwarf_Lines *lines;
490         Dwarf_Line *line;
491         size_t nlines, i;
492         Dwarf_Addr addr;
493         int lineno;
494         int ret;
495
496         ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
497         DIE_IF(ret != 0);
498
499         for (i = 0; i < nlines; i++) {
500                 line = dwarf_onesrcline(lines, i);
501                 dwarf_lineno(line, &lineno);
502                 if (lineno != pf->lno)
503                         continue;
504
505                 /* TODO: Get fileno from line, but how? */
506                 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
507                         continue;
508
509                 ret = dwarf_lineaddr(line, &addr);
510                 DIE_IF(ret != 0);
511                 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
512                          (int)i, lineno, (uintmax_t)addr);
513                 pf->addr = addr;
514
515                 show_probe_point(NULL, pf);
516                 /* Continuing, because target line might be inlined. */
517         }
518 }
519
520 /* Find lines which match lazy pattern */
521 static int find_lazy_match_lines(struct list_head *head,
522                                  const char *fname, const char *pat)
523 {
524         char *fbuf, *p1, *p2;
525         int fd, line, nlines = 0;
526         struct stat st;
527
528         fd = open(fname, O_RDONLY);
529         if (fd < 0)
530                 die("failed to open %s", fname);
531         DIE_IF(fstat(fd, &st) < 0);
532         fbuf = xmalloc(st.st_size + 2);
533         DIE_IF(read(fd, fbuf, st.st_size) < 0);
534         close(fd);
535         fbuf[st.st_size] = '\n';        /* Dummy line */
536         fbuf[st.st_size + 1] = '\0';
537         p1 = fbuf;
538         line = 1;
539         while ((p2 = strchr(p1, '\n')) != NULL) {
540                 *p2 = '\0';
541                 if (strlazymatch(p1, pat)) {
542                         line_list__add_line(head, line);
543                         nlines++;
544                 }
545                 line++;
546                 p1 = p2 + 1;
547         }
548         free(fbuf);
549         return nlines;
550 }
551
552 /* Find probe points from lazy pattern  */
553 static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
554 {
555         Dwarf_Lines *lines;
556         Dwarf_Line *line;
557         size_t nlines, i;
558         Dwarf_Addr addr;
559         Dwarf_Die die_mem;
560         int lineno;
561         int ret;
562
563         if (list_empty(&pf->lcache)) {
564                 /* Matching lazy line pattern */
565                 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
566                                             pf->pp->lazy_line);
567                 if (ret <= 0)
568                         die("No matched lines found in %s.", pf->fname);
569         }
570
571         ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
572         DIE_IF(ret != 0);
573         for (i = 0; i < nlines; i++) {
574                 line = dwarf_onesrcline(lines, i);
575
576                 dwarf_lineno(line, &lineno);
577                 if (!line_list__has_line(&pf->lcache, lineno))
578                         continue;
579
580                 /* TODO: Get fileno from line, but how? */
581                 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
582                         continue;
583
584                 ret = dwarf_lineaddr(line, &addr);
585                 DIE_IF(ret != 0);
586                 if (sp_die) {
587                         /* Address filtering 1: does sp_die include addr? */
588                         if (!dwarf_haspc(sp_die, addr))
589                                 continue;
590                         /* Address filtering 2: No child include addr? */
591                         if (die_find_inlinefunc(sp_die, addr, &die_mem))
592                                 continue;
593                 }
594
595                 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
596                          (int)i, lineno, (unsigned long long)addr);
597                 pf->addr = addr;
598
599                 show_probe_point(sp_die, pf);
600                 /* Continuing, because target line might be inlined. */
601         }
602         /* TODO: deallocate lines, but how? */
603 }
604
605 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
606 {
607         struct probe_finder *pf = (struct probe_finder *)data;
608         struct probe_point *pp = pf->pp;
609
610         if (pp->lazy_line)
611                 find_probe_point_lazy(in_die, pf);
612         else {
613                 /* Get probe address */
614                 pf->addr = die_get_entrypc(in_die);
615                 pf->addr += pp->offset;
616                 pr_debug("found inline addr: 0x%jx\n",
617                          (uintmax_t)pf->addr);
618
619                 show_probe_point(in_die, pf);
620         }
621
622         return DWARF_CB_OK;
623 }
624
625 /* Search function from function name */
626 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
627 {
628         struct probe_finder *pf = (struct probe_finder *)data;
629         struct probe_point *pp = pf->pp;
630
631         /* Check tag and diename */
632         if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
633             die_compare_name(sp_die, pp->function) != 0)
634                 return 0;
635
636         pf->fname = dwarf_decl_file(sp_die);
637         if (pp->line) { /* Function relative line */
638                 dwarf_decl_line(sp_die, &pf->lno);
639                 pf->lno += pp->line;
640                 find_probe_point_by_line(pf);
641         } else if (!dwarf_func_inline(sp_die)) {
642                 /* Real function */
643                 if (pp->lazy_line)
644                         find_probe_point_lazy(sp_die, pf);
645                 else {
646                         pf->addr = die_get_entrypc(sp_die);
647                         pf->addr += pp->offset;
648                         /* TODO: Check the address in this function */
649                         show_probe_point(sp_die, pf);
650                 }
651         } else
652                 /* Inlined function: search instances */
653                 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
654
655         return 1; /* Exit; no same symbol in this CU. */
656 }
657
658 static void find_probe_point_by_func(struct probe_finder *pf)
659 {
660         dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
661 }
662
663 /* Find a probe point */
664 int find_probe_point(int fd, struct probe_point *pp)
665 {
666         struct probe_finder pf = {.pp = pp};
667         Dwarf_Off off, noff;
668         size_t cuhl;
669         Dwarf_Die *diep;
670         Dwarf *dbg;
671
672         dbg = dwarf_begin(fd, DWARF_C_READ);
673         if (!dbg)
674                 return -ENOENT;
675
676         pp->found = 0;
677         off = 0;
678         line_list__init(&pf.lcache);
679         /* Loop on CUs (Compilation Unit) */
680         while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
681                 /* Get the DIE(Debugging Information Entry) of this CU */
682                 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
683                 if (!diep)
684                         continue;
685
686                 /* Check if target file is included. */
687                 if (pp->file)
688                         pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
689                 else
690                         pf.fname = NULL;
691
692                 if (!pp->file || pf.fname) {
693                         if (pp->function)
694                                 find_probe_point_by_func(&pf);
695                         else if (pp->lazy_line)
696                                 find_probe_point_lazy(NULL, &pf);
697                         else {
698                                 pf.lno = pp->line;
699                                 find_probe_point_by_line(&pf);
700                         }
701                 }
702                 off = noff;
703         }
704         line_list__free(&pf.lcache);
705         dwarf_end(dbg);
706
707         return pp->found;
708 }
709
710 /* Find line range from its line number */
711 static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
712 {
713         Dwarf_Lines *lines;
714         Dwarf_Line *line;
715         size_t nlines, i;
716         Dwarf_Addr addr;
717         int lineno;
718         int ret;
719         const char *src;
720         Dwarf_Die die_mem;
721
722         line_list__init(&lf->lr->line_list);
723         ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
724         DIE_IF(ret != 0);
725
726         for (i = 0; i < nlines; i++) {
727                 line = dwarf_onesrcline(lines, i);
728                 ret = dwarf_lineno(line, &lineno);
729                 DIE_IF(ret != 0);
730                 if (lf->lno_s > lineno || lf->lno_e < lineno)
731                         continue;
732
733                 if (sp_die) {
734                         /* Address filtering 1: does sp_die include addr? */
735                         ret = dwarf_lineaddr(line, &addr);
736                         DIE_IF(ret != 0);
737                         if (!dwarf_haspc(sp_die, addr))
738                                 continue;
739
740                         /* Address filtering 2: No child include addr? */
741                         if (die_find_inlinefunc(sp_die, addr, &die_mem))
742                                 continue;
743                 }
744
745                 /* TODO: Get fileno from line, but how? */
746                 src = dwarf_linesrc(line, NULL, NULL);
747                 if (strtailcmp(src, lf->fname) != 0)
748                         continue;
749
750                 /* Copy real path */
751                 if (!lf->lr->path)
752                         lf->lr->path = xstrdup(src);
753                 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
754         }
755         /* Update status */
756         if (!list_empty(&lf->lr->line_list))
757                 lf->found = 1;
758         else {
759                 free(lf->lr->path);
760                 lf->lr->path = NULL;
761         }
762 }
763
764 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
765 {
766         find_line_range_by_line(in_die, (struct line_finder *)data);
767         return DWARF_CB_ABORT;  /* No need to find other instances */
768 }
769
770 /* Search function from function name */
771 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
772 {
773         struct line_finder *lf = (struct line_finder *)data;
774         struct line_range *lr = lf->lr;
775
776         if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
777             die_compare_name(sp_die, lr->function) == 0) {
778                 lf->fname = dwarf_decl_file(sp_die);
779                 dwarf_decl_line(sp_die, &lr->offset);
780                 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
781                 lf->lno_s = lr->offset + lr->start;
782                 if (!lr->end)
783                         lf->lno_e = INT_MAX;
784                 else
785                         lf->lno_e = lr->offset + lr->end;
786                 lr->start = lf->lno_s;
787                 lr->end = lf->lno_e;
788                 if (dwarf_func_inline(sp_die))
789                         dwarf_func_inline_instances(sp_die,
790                                                     line_range_inline_cb, lf);
791                 else
792                         find_line_range_by_line(sp_die, lf);
793                 return 1;
794         }
795         return 0;
796 }
797
798 static void find_line_range_by_func(struct line_finder *lf)
799 {
800         dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
801 }
802
803 int find_line_range(int fd, struct line_range *lr)
804 {
805         struct line_finder lf = {.lr = lr, .found = 0};
806         int ret;
807         Dwarf_Off off = 0, noff;
808         size_t cuhl;
809         Dwarf_Die *diep;
810         Dwarf *dbg;
811
812         dbg = dwarf_begin(fd, DWARF_C_READ);
813         if (!dbg)
814                 return -ENOENT;
815
816         /* Loop on CUs (Compilation Unit) */
817         while (!lf.found) {
818                 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
819                 if (ret != 0)
820                         break;
821
822                 /* Get the DIE(Debugging Information Entry) of this CU */
823                 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
824                 if (!diep)
825                         continue;
826
827                 /* Check if target file is included. */
828                 if (lr->file)
829                         lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
830                 else
831                         lf.fname = 0;
832
833                 if (!lr->file || lf.fname) {
834                         if (lr->function)
835                                 find_line_range_by_func(&lf);
836                         else {
837                                 lf.lno_s = lr->start;
838                                 if (!lr->end)
839                                         lf.lno_e = INT_MAX;
840                                 else
841                                         lf.lno_e = lr->end;
842                                 find_line_range_by_line(NULL, &lf);
843                         }
844                 }
845                 off = noff;
846         }
847         pr_debug("path: %lx\n", (unsigned long)lr->path);
848         dwarf_end(dbg);
849         return lf.found;
850 }
851