]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/probe-finder.c
perf probe: Use libdw callback routines
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / probe-finder.c
CommitLineData
4ea42b18
MH
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>
074fc0e4 34
89c69c0e
MH
35#include "event.h"
36#include "debug.h"
074fc0e4 37#include "util.h"
4ea42b18
MH
38#include "probe-finder.h"
39
40
4ea42b18
MH
41/*
42 * Generic dwarf analysis helpers
43 */
44
45#define X86_32_MAX_REGS 8
46const char *x86_32_regs_table[X86_32_MAX_REGS] = {
47 "%ax",
48 "%cx",
49 "%dx",
50 "%bx",
51 "$stack", /* Stack address instead of %sp */
52 "%bp",
53 "%si",
54 "%di",
55};
56
57#define X86_64_MAX_REGS 16
58const char *x86_64_regs_table[X86_64_MAX_REGS] = {
59 "%ax",
60 "%dx",
61 "%cx",
62 "%bx",
63 "%si",
64 "%di",
65 "%bp",
66 "%sp",
67 "%r8",
68 "%r9",
69 "%r10",
70 "%r11",
71 "%r12",
72 "%r13",
73 "%r14",
74 "%r15",
75};
76
77/* TODO: switching by dwarf address size */
78#ifdef __x86_64__
79#define ARCH_MAX_REGS X86_64_MAX_REGS
80#define arch_regs_table x86_64_regs_table
81#else
82#define ARCH_MAX_REGS X86_32_MAX_REGS
83#define arch_regs_table x86_32_regs_table
84#endif
85
86/* Return architecture dependent register string (for kprobe-tracer) */
87static const char *get_arch_regstr(unsigned int n)
88{
89 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
90}
91
92/*
93 * Compare the tail of two strings.
94 * Return 0 if whole of either string is same as another's tail part.
95 */
96static int strtailcmp(const char *s1, const char *s2)
97{
98 int i1 = strlen(s1);
99 int i2 = strlen(s2);
d56728b8 100 while (--i1 >= 0 && --i2 >= 0) {
4ea42b18
MH
101 if (s1[i1] != s2[i2])
102 return s1[i1] - s2[i2];
103 }
104 return 0;
105}
106
107/* Find the fileno of the target file. */
804b3606 108static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
4ea42b18 109{
804b3606
MH
110 Dwarf_Files *files;
111 size_t nfiles, i;
112 const char *src;
4ea42b18
MH
113 int ret;
114
115 if (!fname)
804b3606 116 return -EINVAL;
4ea42b18 117
804b3606
MH
118 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
119 if (ret == 0) {
120 for (i = 0; i < nfiles; i++) {
121 src = dwarf_filesrc(files, i, NULL, NULL);
122 if (strtailcmp(src, fname) == 0) {
123 ret = (int)i; /*???: +1 or not?*/
124 break;
125 }
4ea42b18 126 }
804b3606
MH
127 if (ret)
128 pr_debug("found fno: %d\n", ret);
4ea42b18 129 }
804b3606 130 return ret;
4ea42b18
MH
131}
132
804b3606
MH
133struct __addr_die_search_param {
134 Dwarf_Addr addr;
135 Dwarf_Die *die_mem;
136};
137
138static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
631c9def 139{
804b3606 140 struct __addr_die_search_param *ad = data;
631c9def 141
804b3606
MH
142 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
143 dwarf_haspc(fn_die, ad->addr)) {
144 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
145 return DWARF_CB_ABORT;
146 }
147 return DWARF_CB_OK;
148}
631c9def 149
804b3606
MH
150/* Search a real subprogram including this line, */
151static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
152 Dwarf_Die *die_mem)
153{
154 struct __addr_die_search_param ad;
155 ad.addr = addr;
156 ad.die_mem = die_mem;
157 /* dwarf_getscopes can't find subprogram. */
158 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
159 return NULL;
160 else
161 return die_mem;
631c9def
MH
162}
163
4ea42b18 164/* Compare diename and tname */
804b3606 165static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
4ea42b18 166{
804b3606
MH
167 const char *name;
168 name = dwarf_diename(dw_die);
169 DIE_IF(name == NULL);
170 return strcmp(tname, name);
4ea42b18
MH
171}
172
804b3606
MH
173/* Get entry pc(or low pc, 1st entry of ranges) of the die */
174static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
4ea42b18 175{
804b3606 176 Dwarf_Addr epc;
4ea42b18
MH
177 int ret;
178
804b3606
MH
179 ret = dwarf_entrypc(dw_die, &epc);
180 DIE_IF(ret == -1);
181 return epc;
4ea42b18
MH
182}
183
e92b85e1
MH
184/* Get a variable die */
185static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
186 Dwarf_Die *die_mem)
4ea42b18 187{
e92b85e1
MH
188 Dwarf_Die child_die;
189 int tag;
4ea42b18
MH
190 int ret;
191
e92b85e1
MH
192 ret = dwarf_child(sp_die, die_mem);
193 if (ret != 0)
194 return NULL;
4ea42b18 195
e92b85e1
MH
196 do {
197 tag = dwarf_tag(die_mem);
198 if ((tag == DW_TAG_formal_parameter ||
199 tag == DW_TAG_variable) &&
200 (die_compare_name(die_mem, name) == 0))
201 return die_mem;
4ea42b18 202
e92b85e1
MH
203 if (die_find_variable(die_mem, name, &child_die)) {
204 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
205 return die_mem;
206 }
207 } while (dwarf_siblingof(die_mem, die_mem) == 0);
4ea42b18 208
e92b85e1 209 return NULL;
4ea42b18
MH
210}
211
4ea42b18
MH
212/*
213 * Probe finder related functions
214 */
215
216/* Show a location */
804b3606 217static void show_location(Dwarf_Op *op, struct probe_finder *pf)
4ea42b18 218{
804b3606
MH
219 unsigned int regn;
220 Dwarf_Word offs = 0;
4ea42b18
MH
221 int deref = 0, ret;
222 const char *regs;
223
804b3606 224 /* TODO: support CFA */
4ea42b18 225 /* If this is based on frame buffer, set the offset */
804b3606
MH
226 if (op->atom == DW_OP_fbreg) {
227 if (pf->fb_ops == NULL)
228 die("The attribute of frame base is not supported.\n");
4ea42b18 229 deref = 1;
804b3606
MH
230 offs = op->number;
231 op = &pf->fb_ops[0];
232 }
4ea42b18 233
804b3606
MH
234 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
235 regn = op->atom - DW_OP_breg0;
236 offs += op->number;
4ea42b18 237 deref = 1;
804b3606
MH
238 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
239 regn = op->atom - DW_OP_reg0;
240 } else if (op->atom == DW_OP_bregx) {
241 regn = op->number;
242 offs += op->number2;
4ea42b18 243 deref = 1;
804b3606
MH
244 } else if (op->atom == DW_OP_regx) {
245 regn = op->number;
4ea42b18 246 } else
804b3606 247 die("DW_OP %d is not supported.", op->atom);
4ea42b18
MH
248
249 regs = get_arch_regstr(regn);
250 if (!regs)
804b3606 251 die("%u exceeds max register number.", regn);
4ea42b18
MH
252
253 if (deref)
804b3606
MH
254 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
255 pf->var, (uintmax_t)offs, regs);
4ea42b18
MH
256 else
257 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
9769833b
MH
258 DIE_IF(ret < 0);
259 DIE_IF(ret >= pf->len);
4ea42b18
MH
260}
261
262/* Show a variables in kprobe event format */
804b3606 263static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
4ea42b18
MH
264{
265 Dwarf_Attribute attr;
804b3606
MH
266 Dwarf_Op *expr;
267 size_t nexpr;
4ea42b18
MH
268 int ret;
269
804b3606 270 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
4ea42b18 271 goto error;
804b3606
MH
272 /* TODO: handle more than 1 exprs */
273 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
274 &expr, &nexpr, 1);
275 if (ret <= 0 || nexpr == 0)
4ea42b18 276 goto error;
804b3606
MH
277
278 show_location(expr, pf);
279 /* *expr will be cached in libdw. Don't free it. */
4ea42b18
MH
280 return ;
281error:
804b3606 282 /* TODO: Support const_value */
074fc0e4 283 die("Failed to find the location of %s at this address.\n"
bbaa46fa 284 " Perhaps, it has been optimized out.", pf->var);
4ea42b18
MH
285}
286
4ea42b18 287/* Find a variable in a subprogram die */
804b3606 288static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18
MH
289{
290 int ret;
e92b85e1 291 Dwarf_Die vr_die;
4ea42b18 292
e92b85e1 293 /* TODO: Support struct members and arrays */
4ea42b18
MH
294 if (!is_c_varname(pf->var)) {
295 /* Output raw parameters */
296 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
9769833b
MH
297 DIE_IF(ret < 0);
298 DIE_IF(ret >= pf->len);
4ea42b18
MH
299 return ;
300 }
301
b7cb10e7 302 pr_debug("Searching '%s' variable in context.\n", pf->var);
4ea42b18 303 /* Search child die for local variables and parameters. */
e92b85e1 304 if (!die_find_variable(sp_die, pf->var, &vr_die))
bbaa46fa 305 die("Failed to find '%s' in this function.", pf->var);
e92b85e1
MH
306
307 show_variable(&vr_die, pf);
4ea42b18
MH
308}
309
4ea42b18 310/* Show a probe point to output buffer */
e92b85e1 311static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18
MH
312{
313 struct probe_point *pp = pf->pp;
e92b85e1
MH
314 Dwarf_Addr eaddr;
315 Dwarf_Die die_mem;
804b3606 316 const char *name;
4ea42b18
MH
317 char tmp[MAX_PROBE_BUFFER];
318 int ret, i, len;
804b3606
MH
319 Dwarf_Attribute fb_attr;
320 size_t nops;
4ea42b18 321
e92b85e1
MH
322 /* If no real subprogram, find a real one */
323 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
324 sp_die = die_get_real_subprogram(&pf->cu_die,
325 pf->addr, &die_mem);
326 if (!sp_die)
327 die("Probe point is not found in subprograms.");
328 }
329
4ea42b18 330 /* Output name of probe point */
804b3606
MH
331 name = dwarf_diename(sp_die);
332 if (name) {
e92b85e1
MH
333 dwarf_entrypc(sp_die, &eaddr);
334 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
335 (unsigned long)(pf->addr - eaddr));
253977b0
MH
336 /* Copy the function name if possible */
337 if (!pp->function) {
338 pp->function = strdup(name);
e92b85e1 339 pp->offset = (size_t)(pf->addr - eaddr);
253977b0 340 }
4ea42b18
MH
341 } else {
342 /* This function has no name. */
804b3606
MH
343 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
344 (uintmax_t)pf->addr);
253977b0
MH
345 if (!pp->function) {
346 /* TODO: Use _stext */
347 pp->function = strdup("");
804b3606 348 pp->offset = (size_t)pf->addr;
253977b0 349 }
4ea42b18 350 }
9769833b
MH
351 DIE_IF(ret < 0);
352 DIE_IF(ret >= MAX_PROBE_BUFFER);
4ea42b18 353 len = ret;
b0ef0732 354 pr_debug("Probe point found: %s\n", tmp);
4ea42b18 355
804b3606
MH
356 /* Get the frame base attribute/ops */
357 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
358 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
359 &pf->fb_ops, &nops, 1);
360 if (ret <= 0 || nops == 0)
361 pf->fb_ops = NULL;
362
4ea42b18 363 /* Find each argument */
804b3606 364 /* TODO: use dwarf_cfi_addrframe */
4ea42b18
MH
365 for (i = 0; i < pp->nr_args; i++) {
366 pf->var = pp->args[i];
367 pf->buf = &tmp[len];
368 pf->len = MAX_PROBE_BUFFER - len;
369 find_variable(sp_die, pf);
370 len += strlen(pf->buf);
371 }
804b3606
MH
372
373 /* *pf->fb_ops will be cached in libdw. Don't free it. */
374 pf->fb_ops = NULL;
4ea42b18
MH
375
376 pp->probes[pp->found] = strdup(tmp);
377 pp->found++;
378}
379
4ea42b18 380/* Find probe point from its line number */
631c9def 381static void find_probe_point_by_line(struct probe_finder *pf)
4ea42b18 382{
804b3606
MH
383 Dwarf_Lines *lines;
384 Dwarf_Line *line;
385 size_t nlines, i;
e92b85e1 386 Dwarf_Addr addr;
804b3606 387 int lineno;
4ea42b18
MH
388 int ret;
389
804b3606
MH
390 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
391 DIE_IF(ret != 0);
4ea42b18 392
804b3606
MH
393 for (i = 0; i < nlines; i++) {
394 line = dwarf_onesrcline(lines, i);
395 dwarf_lineno(line, &lineno);
b0ef0732 396 if (lineno != pf->lno)
4ea42b18
MH
397 continue;
398
804b3606
MH
399 /* TODO: Get fileno from line, but how? */
400 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
401 continue;
b0ef0732 402
804b3606
MH
403 ret = dwarf_lineaddr(line, &addr);
404 DIE_IF(ret != 0);
405 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
406 (int)i, lineno, (uintmax_t)addr);
4ea42b18 407 pf->addr = addr;
804b3606 408
e92b85e1 409 show_probe_point(NULL, pf);
4ea42b18
MH
410 /* Continuing, because target line might be inlined. */
411 }
4ea42b18
MH
412}
413
e92b85e1
MH
414static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
415{
416 struct probe_finder *pf = (struct probe_finder *)data;
417 struct probe_point *pp = pf->pp;
418
419 /* Get probe address */
420 pf->addr = die_get_entrypc(in_die);
421 pf->addr += pp->offset;
422 pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr);
423
424 show_probe_point(in_die, pf);
425 return DWARF_CB_OK;
426}
804b3606 427
4ea42b18 428/* Search function from function name */
e92b85e1 429static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
4ea42b18
MH
430{
431 struct probe_finder *pf = (struct probe_finder *)data;
432 struct probe_point *pp = pf->pp;
4ea42b18 433
e92b85e1
MH
434 /* Check tag and diename */
435 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
436 die_compare_name(sp_die, pp->function) != 0)
437 return 0;
438
439 if (pp->line) { /* Function relative line */
440 pf->fname = dwarf_decl_file(sp_die);
441 dwarf_decl_line(sp_die, &pf->lno);
442 pf->lno += pp->line;
443 find_probe_point_by_line(pf);
444 } else if (!dwarf_func_inline(sp_die)) {
445 /* Real function */
446 pf->addr = die_get_entrypc(sp_die);
447 pf->addr += pp->offset;
448 /* TODO: Check the address in this function */
449 show_probe_point(sp_die, pf);
450 } else
451 /* Inlined function: search instances */
452 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
453
454 return 1; /* Exit; no same symbol in this CU. */
4ea42b18
MH
455}
456
631c9def 457static void find_probe_point_by_func(struct probe_finder *pf)
4ea42b18 458{
e92b85e1 459 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
4ea42b18
MH
460}
461
462/* Find a probe point */
81cb8aa3 463int find_probe_point(int fd, struct probe_point *pp)
4ea42b18 464{
4ea42b18 465 struct probe_finder pf = {.pp = pp};
804b3606
MH
466 int ret;
467 Dwarf_Off off, noff;
468 size_t cuhl;
469 Dwarf_Die *diep;
470 Dwarf *dbg;
471 int fno = 0;
472
473 dbg = dwarf_begin(fd, DWARF_C_READ);
474 if (!dbg)
a225a1d9 475 return -ENOENT;
4ea42b18
MH
476
477 pp->found = 0;
804b3606
MH
478 off = 0;
479 /* Loop on CUs (Compilation Unit) */
480 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
4ea42b18 481 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
482 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
483 if (!diep)
484 continue;
4ea42b18
MH
485
486 /* Check if target file is included. */
487 if (pp->file)
804b3606
MH
488 fno = cu_find_fileno(&pf.cu_die, pp->file);
489 else
490 fno = 0;
4ea42b18 491
804b3606 492 if (!pp->file || fno) {
4ea42b18 493 /* Save CU base address (for frame_base) */
804b3606
MH
494 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
495 if (ret != 0)
4ea42b18 496 pf.cu_base = 0;
4ea42b18 497 if (pp->function)
631c9def 498 find_probe_point_by_func(&pf);
b0ef0732
MH
499 else {
500 pf.lno = pp->line;
631c9def 501 find_probe_point_by_line(&pf);
b0ef0732 502 }
4ea42b18 503 }
804b3606 504 off = noff;
4ea42b18 505 }
804b3606 506 dwarf_end(dbg);
4ea42b18
MH
507
508 return pp->found;
509}
510
631c9def
MH
511
512static void line_range_add_line(struct line_range *lr, unsigned int line)
513{
514 struct line_node *ln;
515 struct list_head *p;
516
517 /* Reverse search, because new line will be the last one */
518 list_for_each_entry_reverse(ln, &lr->line_list, list) {
519 if (ln->line < line) {
520 p = &ln->list;
521 goto found;
522 } else if (ln->line == line) /* Already exist */
523 return ;
524 }
525 /* List is empty, or the smallest entry */
526 p = &lr->line_list;
527found:
528 pr_debug("Debug: add a line %u\n", line);
529 ln = zalloc(sizeof(struct line_node));
530 DIE_IF(ln == NULL);
531 ln->line = line;
532 INIT_LIST_HEAD(&ln->list);
533 list_add(&ln->list, p);
534}
535
536/* Find line range from its line number */
537static void find_line_range_by_line(struct line_finder *lf)
538{
804b3606
MH
539 Dwarf_Lines *lines;
540 Dwarf_Line *line;
541 size_t nlines, i;
631c9def 542 Dwarf_Addr addr;
804b3606 543 int lineno;
631c9def 544 int ret;
804b3606 545 const char *src;
631c9def 546
3cb8bc6a 547 INIT_LIST_HEAD(&lf->lr->line_list);
804b3606
MH
548 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
549 DIE_IF(ret != 0);
631c9def 550
804b3606
MH
551 for (i = 0; i < nlines; i++) {
552 line = dwarf_onesrcline(lines, i);
553 dwarf_lineno(line, &lineno);
554 if (lf->lno_s > lineno || lf->lno_e < lineno)
631c9def
MH
555 continue;
556
804b3606
MH
557 /* TODO: Get fileno from line, but how? */
558 src = dwarf_linesrc(line, NULL, NULL);
559 if (strtailcmp(src, lf->fname) != 0)
631c9def
MH
560 continue;
561
562 /* Filter line in the function address range */
563 if (lf->addr_s && lf->addr_e) {
804b3606
MH
564 ret = dwarf_lineaddr(line, &addr);
565 DIE_IF(ret != 0);
631c9def
MH
566 if (lf->addr_s > addr || lf->addr_e <= addr)
567 continue;
568 }
804b3606
MH
569 /* Copy real path */
570 if (!lf->lr->path)
571 lf->lr->path = strdup(src);
631c9def
MH
572 line_range_add_line(lf->lr, (unsigned int)lineno);
573 }
804b3606 574 /* Update status */
631c9def
MH
575 if (!list_empty(&lf->lr->line_list))
576 lf->found = 1;
804b3606
MH
577 else {
578 free(lf->lr->path);
579 lf->lr->path = NULL;
580 }
631c9def
MH
581}
582
583/* Search function from function name */
e92b85e1 584static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
631c9def
MH
585{
586 struct line_finder *lf = (struct line_finder *)data;
587 struct line_range *lr = lf->lr;
631c9def
MH
588 int ret;
589
e92b85e1
MH
590 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
591 die_compare_name(sp_die, lr->function) == 0) {
631c9def 592 /* Get the address range of this function */
e92b85e1 593 ret = dwarf_highpc(sp_die, &lf->addr_e);
804b3606 594 if (ret == 0)
e92b85e1 595 ret = dwarf_lowpc(sp_die, &lf->addr_s);
804b3606 596 if (ret != 0) {
631c9def
MH
597 lf->addr_s = 0;
598 lf->addr_e = 0;
599 }
600
e92b85e1
MH
601 lf->fname = dwarf_decl_file(sp_die);
602 dwarf_decl_line(sp_die, &lr->offset);
804b3606 603 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
631c9def
MH
604 lf->lno_s = lr->offset + lr->start;
605 if (!lr->end)
804b3606 606 lf->lno_e = INT_MAX;
631c9def
MH
607 else
608 lf->lno_e = lr->offset + lr->end;
609 lr->start = lf->lno_s;
610 lr->end = lf->lno_e;
611 find_line_range_by_line(lf);
631c9def
MH
612 return 1;
613 }
614 return 0;
615}
616
617static void find_line_range_by_func(struct line_finder *lf)
618{
e92b85e1 619 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
631c9def
MH
620}
621
622int find_line_range(int fd, struct line_range *lr)
623{
804b3606 624 struct line_finder lf = {.lr = lr, .found = 0};
631c9def 625 int ret;
804b3606
MH
626 Dwarf_Off off = 0, noff;
627 size_t cuhl;
628 Dwarf_Die *diep;
629 Dwarf *dbg;
630 int fno;
631
632 dbg = dwarf_begin(fd, DWARF_C_READ);
633 if (!dbg)
631c9def
MH
634 return -ENOENT;
635
804b3606 636 /* Loop on CUs (Compilation Unit) */
631c9def 637 while (!lf.found) {
804b3606
MH
638 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
639 if (ret != 0)
631c9def
MH
640 break;
641
642 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
643 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
644 if (!diep)
645 continue;
631c9def
MH
646
647 /* Check if target file is included. */
648 if (lr->file)
804b3606
MH
649 fno = cu_find_fileno(&lf.cu_die, lr->file);
650 else
651 fno = 0;
631c9def 652
804b3606 653 if (!lr->file || fno) {
631c9def
MH
654 if (lr->function)
655 find_line_range_by_func(&lf);
656 else {
804b3606 657 lf.fname = lr->file;
631c9def
MH
658 lf.lno_s = lr->start;
659 if (!lr->end)
804b3606 660 lf.lno_e = INT_MAX;
631c9def
MH
661 else
662 lf.lno_e = lr->end;
663 find_line_range_by_line(&lf);
664 }
631c9def 665 }
804b3606 666 off = noff;
631c9def 667 }
804b3606
MH
668 pr_debug("path: %lx\n", (unsigned long)lr->path);
669 dwarf_end(dbg);
631c9def
MH
670 return lf.found;
671}
672