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