perf probe: Fix offset to allow signed value
[linux-2.6-block.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
2a9c8c36 35#include "string.h"
89c69c0e
MH
36#include "event.h"
37#include "debug.h"
074fc0e4 38#include "util.h"
4ea42b18
MH
39#include "probe-finder.h"
40
41
4ea42b18
MH
42/*
43 * Generic dwarf analysis helpers
44 */
45
46#define X86_32_MAX_REGS 8
47const 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
59const 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) */
88static 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 */
97static int strtailcmp(const char *s1, const char *s2)
98{
99 int i1 = strlen(s1);
100 int i2 = strlen(s2);
d56728b8 101 while (--i1 >= 0 && --i2 >= 0) {
4ea42b18
MH
102 if (s1[i1] != s2[i2])
103 return s1[i1] - s2[i2];
104 }
105 return 0;
106}
107
2a9c8c36
MH
108/* Line number list operations */
109
110/* Add a line to line number list */
111static 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;
126found:
127 pr_debug("line list: add a line %u\n", line);
128 ln = zalloc(sizeof(struct line_node));
129 DIE_IF(ln == NULL);
130 ln->line = line;
131 INIT_LIST_HEAD(&ln->list);
132 list_add(&ln->list, p);
133}
134
135/* Check if the line in line number list */
136static int line_list__has_line(struct list_head *head, unsigned int line)
137{
138 struct line_node *ln;
139
140 /* Reverse search, because new line will be the last one */
141 list_for_each_entry(ln, head, list)
142 if (ln->line == line)
143 return 1;
144
145 return 0;
146}
147
148/* Init line number list */
149static void line_list__init(struct list_head *head)
150{
151 INIT_LIST_HEAD(head);
152}
153
154/* Free line number list */
155static void line_list__free(struct list_head *head)
156{
157 struct line_node *ln;
158 while (!list_empty(head)) {
159 ln = list_first_entry(head, struct line_node, list);
160 list_del(&ln->list);
161 free(ln);
162 }
163}
164
165/* Dwarf wrappers */
166
167/* Find the realpath of the target file. */
168static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
4ea42b18 169{
804b3606
MH
170 Dwarf_Files *files;
171 size_t nfiles, i;
accd3cc4 172 const char *src = NULL;
4ea42b18
MH
173 int ret;
174
175 if (!fname)
2a9c8c36 176 return NULL;
4ea42b18 177
804b3606 178 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
2a9c8c36
MH
179 if (ret != 0)
180 return NULL;
181
182 for (i = 0; i < nfiles; i++) {
183 src = dwarf_filesrc(files, i, NULL, NULL);
184 if (strtailcmp(src, fname) == 0)
185 break;
4ea42b18 186 }
2a9c8c36 187 return src;
4ea42b18
MH
188}
189
804b3606
MH
190struct __addr_die_search_param {
191 Dwarf_Addr addr;
192 Dwarf_Die *die_mem;
193};
194
195static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
631c9def 196{
804b3606 197 struct __addr_die_search_param *ad = data;
631c9def 198
804b3606
MH
199 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
200 dwarf_haspc(fn_die, ad->addr)) {
201 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
202 return DWARF_CB_ABORT;
203 }
204 return DWARF_CB_OK;
205}
631c9def 206
804b3606
MH
207/* Search a real subprogram including this line, */
208static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
209 Dwarf_Die *die_mem)
210{
211 struct __addr_die_search_param ad;
212 ad.addr = addr;
213 ad.die_mem = die_mem;
214 /* dwarf_getscopes can't find subprogram. */
215 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
216 return NULL;
217 else
218 return die_mem;
631c9def
MH
219}
220
161a26b0
MH
221/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
222static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
223 Dwarf_Die *die_mem)
224{
225 Dwarf_Die child_die;
226 int ret;
227
228 ret = dwarf_child(sp_die, die_mem);
229 if (ret != 0)
230 return NULL;
231
232 do {
233 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
234 dwarf_haspc(die_mem, addr))
235 return die_mem;
236
237 if (die_get_inlinefunc(die_mem, addr, &child_die)) {
238 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
239 return die_mem;
240 }
241 } while (dwarf_siblingof(die_mem, die_mem) == 0);
242
243 return NULL;
244}
245
4ea42b18 246/* Compare diename and tname */
804b3606 247static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
4ea42b18 248{
804b3606
MH
249 const char *name;
250 name = dwarf_diename(dw_die);
251 DIE_IF(name == NULL);
252 return strcmp(tname, name);
4ea42b18
MH
253}
254
804b3606
MH
255/* Get entry pc(or low pc, 1st entry of ranges) of the die */
256static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
4ea42b18 257{
804b3606 258 Dwarf_Addr epc;
4ea42b18
MH
259 int ret;
260
804b3606
MH
261 ret = dwarf_entrypc(dw_die, &epc);
262 DIE_IF(ret == -1);
263 return epc;
4ea42b18
MH
264}
265
e92b85e1
MH
266/* Get a variable die */
267static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
268 Dwarf_Die *die_mem)
4ea42b18 269{
e92b85e1
MH
270 Dwarf_Die child_die;
271 int tag;
4ea42b18
MH
272 int ret;
273
e92b85e1
MH
274 ret = dwarf_child(sp_die, die_mem);
275 if (ret != 0)
276 return NULL;
4ea42b18 277
e92b85e1
MH
278 do {
279 tag = dwarf_tag(die_mem);
280 if ((tag == DW_TAG_formal_parameter ||
281 tag == DW_TAG_variable) &&
282 (die_compare_name(die_mem, name) == 0))
283 return die_mem;
4ea42b18 284
e92b85e1
MH
285 if (die_find_variable(die_mem, name, &child_die)) {
286 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
287 return die_mem;
288 }
289 } while (dwarf_siblingof(die_mem, die_mem) == 0);
4ea42b18 290
e92b85e1 291 return NULL;
4ea42b18
MH
292}
293
4ea42b18
MH
294/*
295 * Probe finder related functions
296 */
297
298/* Show a location */
804b3606 299static void show_location(Dwarf_Op *op, struct probe_finder *pf)
4ea42b18 300{
804b3606
MH
301 unsigned int regn;
302 Dwarf_Word offs = 0;
4ea42b18
MH
303 int deref = 0, ret;
304 const char *regs;
305
804b3606 306 /* TODO: support CFA */
4ea42b18 307 /* If this is based on frame buffer, set the offset */
804b3606
MH
308 if (op->atom == DW_OP_fbreg) {
309 if (pf->fb_ops == NULL)
310 die("The attribute of frame base is not supported.\n");
4ea42b18 311 deref = 1;
804b3606
MH
312 offs = op->number;
313 op = &pf->fb_ops[0];
314 }
4ea42b18 315
804b3606
MH
316 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
317 regn = op->atom - DW_OP_breg0;
318 offs += op->number;
4ea42b18 319 deref = 1;
804b3606
MH
320 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
321 regn = op->atom - DW_OP_reg0;
322 } else if (op->atom == DW_OP_bregx) {
323 regn = op->number;
324 offs += op->number2;
4ea42b18 325 deref = 1;
804b3606
MH
326 } else if (op->atom == DW_OP_regx) {
327 regn = op->number;
4ea42b18 328 } else
804b3606 329 die("DW_OP %d is not supported.", op->atom);
4ea42b18
MH
330
331 regs = get_arch_regstr(regn);
332 if (!regs)
804b3606 333 die("%u exceeds max register number.", regn);
4ea42b18
MH
334
335 if (deref)
67c7ff7c
MH
336 ret = snprintf(pf->buf, pf->len, " %s=%+jd(%s)",
337 pf->var, (intmax_t)offs, regs);
4ea42b18
MH
338 else
339 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
9769833b
MH
340 DIE_IF(ret < 0);
341 DIE_IF(ret >= pf->len);
4ea42b18
MH
342}
343
344/* Show a variables in kprobe event format */
804b3606 345static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
4ea42b18
MH
346{
347 Dwarf_Attribute attr;
804b3606
MH
348 Dwarf_Op *expr;
349 size_t nexpr;
4ea42b18
MH
350 int ret;
351
804b3606 352 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
4ea42b18 353 goto error;
804b3606
MH
354 /* TODO: handle more than 1 exprs */
355 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
356 &expr, &nexpr, 1);
357 if (ret <= 0 || nexpr == 0)
4ea42b18 358 goto error;
804b3606
MH
359
360 show_location(expr, pf);
361 /* *expr will be cached in libdw. Don't free it. */
4ea42b18
MH
362 return ;
363error:
804b3606 364 /* TODO: Support const_value */
074fc0e4 365 die("Failed to find the location of %s at this address.\n"
bbaa46fa 366 " Perhaps, it has been optimized out.", pf->var);
4ea42b18
MH
367}
368
4ea42b18 369/* Find a variable in a subprogram die */
804b3606 370static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18
MH
371{
372 int ret;
e92b85e1 373 Dwarf_Die vr_die;
4ea42b18 374
e92b85e1 375 /* TODO: Support struct members and arrays */
4ea42b18
MH
376 if (!is_c_varname(pf->var)) {
377 /* Output raw parameters */
378 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
9769833b
MH
379 DIE_IF(ret < 0);
380 DIE_IF(ret >= pf->len);
4ea42b18
MH
381 return ;
382 }
383
b7cb10e7 384 pr_debug("Searching '%s' variable in context.\n", pf->var);
4ea42b18 385 /* Search child die for local variables and parameters. */
e92b85e1 386 if (!die_find_variable(sp_die, pf->var, &vr_die))
bbaa46fa 387 die("Failed to find '%s' in this function.", pf->var);
e92b85e1
MH
388
389 show_variable(&vr_die, pf);
4ea42b18
MH
390}
391
4ea42b18 392/* Show a probe point to output buffer */
e92b85e1 393static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18
MH
394{
395 struct probe_point *pp = pf->pp;
e92b85e1
MH
396 Dwarf_Addr eaddr;
397 Dwarf_Die die_mem;
804b3606 398 const char *name;
4ea42b18
MH
399 char tmp[MAX_PROBE_BUFFER];
400 int ret, i, len;
804b3606
MH
401 Dwarf_Attribute fb_attr;
402 size_t nops;
4ea42b18 403
e92b85e1
MH
404 /* If no real subprogram, find a real one */
405 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
406 sp_die = die_get_real_subprogram(&pf->cu_die,
407 pf->addr, &die_mem);
408 if (!sp_die)
409 die("Probe point is not found in subprograms.");
410 }
411
4ea42b18 412 /* Output name of probe point */
804b3606
MH
413 name = dwarf_diename(sp_die);
414 if (name) {
e92b85e1
MH
415 dwarf_entrypc(sp_die, &eaddr);
416 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
417 (unsigned long)(pf->addr - eaddr));
253977b0
MH
418 /* Copy the function name if possible */
419 if (!pp->function) {
420 pp->function = strdup(name);
e92b85e1 421 pp->offset = (size_t)(pf->addr - eaddr);
253977b0 422 }
4ea42b18
MH
423 } else {
424 /* This function has no name. */
804b3606
MH
425 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
426 (uintmax_t)pf->addr);
253977b0
MH
427 if (!pp->function) {
428 /* TODO: Use _stext */
429 pp->function = strdup("");
804b3606 430 pp->offset = (size_t)pf->addr;
253977b0 431 }
4ea42b18 432 }
9769833b
MH
433 DIE_IF(ret < 0);
434 DIE_IF(ret >= MAX_PROBE_BUFFER);
4ea42b18 435 len = ret;
b0ef0732 436 pr_debug("Probe point found: %s\n", tmp);
4ea42b18 437
804b3606
MH
438 /* Get the frame base attribute/ops */
439 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
440 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
441 &pf->fb_ops, &nops, 1);
442 if (ret <= 0 || nops == 0)
443 pf->fb_ops = NULL;
444
4ea42b18 445 /* Find each argument */
804b3606 446 /* TODO: use dwarf_cfi_addrframe */
4ea42b18
MH
447 for (i = 0; i < pp->nr_args; i++) {
448 pf->var = pp->args[i];
449 pf->buf = &tmp[len];
450 pf->len = MAX_PROBE_BUFFER - len;
451 find_variable(sp_die, pf);
452 len += strlen(pf->buf);
453 }
804b3606
MH
454
455 /* *pf->fb_ops will be cached in libdw. Don't free it. */
456 pf->fb_ops = NULL;
4ea42b18 457
594087a0
MH
458 if (pp->found == MAX_PROBES)
459 die("Too many( > %d) probe point found.\n", MAX_PROBES);
460
4ea42b18
MH
461 pp->probes[pp->found] = strdup(tmp);
462 pp->found++;
463}
464
4ea42b18 465/* Find probe point from its line number */
631c9def 466static void find_probe_point_by_line(struct probe_finder *pf)
4ea42b18 467{
804b3606
MH
468 Dwarf_Lines *lines;
469 Dwarf_Line *line;
470 size_t nlines, i;
e92b85e1 471 Dwarf_Addr addr;
804b3606 472 int lineno;
4ea42b18
MH
473 int ret;
474
804b3606
MH
475 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
476 DIE_IF(ret != 0);
4ea42b18 477
804b3606
MH
478 for (i = 0; i < nlines; i++) {
479 line = dwarf_onesrcline(lines, i);
480 dwarf_lineno(line, &lineno);
b0ef0732 481 if (lineno != pf->lno)
4ea42b18
MH
482 continue;
483
804b3606
MH
484 /* TODO: Get fileno from line, but how? */
485 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
486 continue;
b0ef0732 487
804b3606
MH
488 ret = dwarf_lineaddr(line, &addr);
489 DIE_IF(ret != 0);
490 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
491 (int)i, lineno, (uintmax_t)addr);
4ea42b18 492 pf->addr = addr;
804b3606 493
e92b85e1 494 show_probe_point(NULL, pf);
4ea42b18
MH
495 /* Continuing, because target line might be inlined. */
496 }
4ea42b18
MH
497}
498
2a9c8c36
MH
499/* Find lines which match lazy pattern */
500static int find_lazy_match_lines(struct list_head *head,
501 const char *fname, const char *pat)
502{
503 char *fbuf, *p1, *p2;
504 int fd, line, nlines = 0;
505 struct stat st;
506
507 fd = open(fname, O_RDONLY);
508 if (fd < 0)
509 die("failed to open %s", fname);
510 DIE_IF(fstat(fd, &st) < 0);
511 fbuf = malloc(st.st_size + 2);
512 DIE_IF(fbuf == NULL);
513 DIE_IF(read(fd, fbuf, st.st_size) < 0);
514 close(fd);
515 fbuf[st.st_size] = '\n'; /* Dummy line */
516 fbuf[st.st_size + 1] = '\0';
517 p1 = fbuf;
518 line = 1;
519 while ((p2 = strchr(p1, '\n')) != NULL) {
520 *p2 = '\0';
521 if (strlazymatch(p1, pat)) {
522 line_list__add_line(head, line);
523 nlines++;
524 }
525 line++;
526 p1 = p2 + 1;
527 }
528 free(fbuf);
529 return nlines;
530}
531
532/* Find probe points from lazy pattern */
533static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
534{
535 Dwarf_Lines *lines;
536 Dwarf_Line *line;
537 size_t nlines, i;
538 Dwarf_Addr addr;
539 Dwarf_Die die_mem;
540 int lineno;
541 int ret;
542
543 if (list_empty(&pf->lcache)) {
544 /* Matching lazy line pattern */
545 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
546 pf->pp->lazy_line);
547 if (ret <= 0)
548 die("No matched lines found in %s.", pf->fname);
549 }
550
551 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
552 DIE_IF(ret != 0);
553 for (i = 0; i < nlines; i++) {
554 line = dwarf_onesrcline(lines, i);
555
556 dwarf_lineno(line, &lineno);
557 if (!line_list__has_line(&pf->lcache, lineno))
558 continue;
559
560 /* TODO: Get fileno from line, but how? */
561 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
562 continue;
563
564 ret = dwarf_lineaddr(line, &addr);
565 DIE_IF(ret != 0);
566 if (sp_die) {
567 /* Address filtering 1: does sp_die include addr? */
568 if (!dwarf_haspc(sp_die, addr))
569 continue;
570 /* Address filtering 2: No child include addr? */
571 if (die_get_inlinefunc(sp_die, addr, &die_mem))
572 continue;
573 }
574
575 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
576 (int)i, lineno, (unsigned long long)addr);
577 pf->addr = addr;
578
579 show_probe_point(sp_die, pf);
580 /* Continuing, because target line might be inlined. */
581 }
582 /* TODO: deallocate lines, but how? */
583}
584
e92b85e1
MH
585static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
586{
587 struct probe_finder *pf = (struct probe_finder *)data;
588 struct probe_point *pp = pf->pp;
589
2a9c8c36
MH
590 if (pp->lazy_line)
591 find_probe_point_lazy(in_die, pf);
592 else {
593 /* Get probe address */
594 pf->addr = die_get_entrypc(in_die);
595 pf->addr += pp->offset;
596 pr_debug("found inline addr: 0x%jx\n",
597 (uintmax_t)pf->addr);
598
599 show_probe_point(in_die, pf);
600 }
e92b85e1 601
e92b85e1
MH
602 return DWARF_CB_OK;
603}
804b3606 604
4ea42b18 605/* Search function from function name */
e92b85e1 606static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
4ea42b18
MH
607{
608 struct probe_finder *pf = (struct probe_finder *)data;
609 struct probe_point *pp = pf->pp;
4ea42b18 610
e92b85e1
MH
611 /* Check tag and diename */
612 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
613 die_compare_name(sp_die, pp->function) != 0)
614 return 0;
615
2a9c8c36 616 pf->fname = dwarf_decl_file(sp_die);
e92b85e1 617 if (pp->line) { /* Function relative line */
e92b85e1
MH
618 dwarf_decl_line(sp_die, &pf->lno);
619 pf->lno += pp->line;
620 find_probe_point_by_line(pf);
621 } else if (!dwarf_func_inline(sp_die)) {
622 /* Real function */
2a9c8c36
MH
623 if (pp->lazy_line)
624 find_probe_point_lazy(sp_die, pf);
625 else {
626 pf->addr = die_get_entrypc(sp_die);
627 pf->addr += pp->offset;
628 /* TODO: Check the address in this function */
629 show_probe_point(sp_die, pf);
630 }
e92b85e1
MH
631 } else
632 /* Inlined function: search instances */
633 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
634
635 return 1; /* Exit; no same symbol in this CU. */
4ea42b18
MH
636}
637
631c9def 638static void find_probe_point_by_func(struct probe_finder *pf)
4ea42b18 639{
e92b85e1 640 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
4ea42b18
MH
641}
642
643/* Find a probe point */
81cb8aa3 644int find_probe_point(int fd, struct probe_point *pp)
4ea42b18 645{
4ea42b18 646 struct probe_finder pf = {.pp = pp};
804b3606
MH
647 int ret;
648 Dwarf_Off off, noff;
649 size_t cuhl;
650 Dwarf_Die *diep;
651 Dwarf *dbg;
804b3606
MH
652
653 dbg = dwarf_begin(fd, DWARF_C_READ);
654 if (!dbg)
a225a1d9 655 return -ENOENT;
4ea42b18
MH
656
657 pp->found = 0;
804b3606 658 off = 0;
2a9c8c36 659 line_list__init(&pf.lcache);
804b3606
MH
660 /* Loop on CUs (Compilation Unit) */
661 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
4ea42b18 662 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
663 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
664 if (!diep)
665 continue;
4ea42b18
MH
666
667 /* Check if target file is included. */
668 if (pp->file)
2a9c8c36 669 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
804b3606 670 else
2a9c8c36 671 pf.fname = NULL;
4ea42b18 672
2a9c8c36 673 if (!pp->file || pf.fname) {
4ea42b18 674 /* Save CU base address (for frame_base) */
804b3606
MH
675 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
676 if (ret != 0)
4ea42b18 677 pf.cu_base = 0;
4ea42b18 678 if (pp->function)
631c9def 679 find_probe_point_by_func(&pf);
2a9c8c36
MH
680 else if (pp->lazy_line)
681 find_probe_point_lazy(NULL, &pf);
b0ef0732
MH
682 else {
683 pf.lno = pp->line;
631c9def 684 find_probe_point_by_line(&pf);
b0ef0732 685 }
4ea42b18 686 }
804b3606 687 off = noff;
4ea42b18 688 }
2a9c8c36 689 line_list__free(&pf.lcache);
804b3606 690 dwarf_end(dbg);
4ea42b18
MH
691
692 return pp->found;
693}
694
631c9def 695/* Find line range from its line number */
161a26b0 696static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
631c9def 697{
804b3606
MH
698 Dwarf_Lines *lines;
699 Dwarf_Line *line;
700 size_t nlines, i;
631c9def 701 Dwarf_Addr addr;
804b3606 702 int lineno;
631c9def 703 int ret;
804b3606 704 const char *src;
161a26b0 705 Dwarf_Die die_mem;
631c9def 706
2a9c8c36 707 line_list__init(&lf->lr->line_list);
804b3606
MH
708 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
709 DIE_IF(ret != 0);
631c9def 710
804b3606
MH
711 for (i = 0; i < nlines; i++) {
712 line = dwarf_onesrcline(lines, i);
161a26b0
MH
713 ret = dwarf_lineno(line, &lineno);
714 DIE_IF(ret != 0);
804b3606 715 if (lf->lno_s > lineno || lf->lno_e < lineno)
631c9def
MH
716 continue;
717
161a26b0
MH
718 if (sp_die) {
719 /* Address filtering 1: does sp_die include addr? */
720 ret = dwarf_lineaddr(line, &addr);
721 DIE_IF(ret != 0);
722 if (!dwarf_haspc(sp_die, addr))
723 continue;
724
725 /* Address filtering 2: No child include addr? */
726 if (die_get_inlinefunc(sp_die, addr, &die_mem))
727 continue;
728 }
729
804b3606
MH
730 /* TODO: Get fileno from line, but how? */
731 src = dwarf_linesrc(line, NULL, NULL);
732 if (strtailcmp(src, lf->fname) != 0)
631c9def
MH
733 continue;
734
804b3606
MH
735 /* Copy real path */
736 if (!lf->lr->path)
737 lf->lr->path = strdup(src);
2a9c8c36 738 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
631c9def 739 }
804b3606 740 /* Update status */
631c9def
MH
741 if (!list_empty(&lf->lr->line_list))
742 lf->found = 1;
804b3606
MH
743 else {
744 free(lf->lr->path);
745 lf->lr->path = NULL;
746 }
631c9def
MH
747}
748
161a26b0
MH
749static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
750{
751 find_line_range_by_line(in_die, (struct line_finder *)data);
752 return DWARF_CB_ABORT; /* No need to find other instances */
753}
754
631c9def 755/* Search function from function name */
e92b85e1 756static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
631c9def
MH
757{
758 struct line_finder *lf = (struct line_finder *)data;
759 struct line_range *lr = lf->lr;
631c9def 760
e92b85e1
MH
761 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
762 die_compare_name(sp_die, lr->function) == 0) {
e92b85e1
MH
763 lf->fname = dwarf_decl_file(sp_die);
764 dwarf_decl_line(sp_die, &lr->offset);
804b3606 765 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
631c9def
MH
766 lf->lno_s = lr->offset + lr->start;
767 if (!lr->end)
804b3606 768 lf->lno_e = INT_MAX;
631c9def
MH
769 else
770 lf->lno_e = lr->offset + lr->end;
771 lr->start = lf->lno_s;
772 lr->end = lf->lno_e;
161a26b0
MH
773 if (dwarf_func_inline(sp_die))
774 dwarf_func_inline_instances(sp_die,
775 line_range_inline_cb, lf);
776 else
777 find_line_range_by_line(sp_die, lf);
631c9def
MH
778 return 1;
779 }
780 return 0;
781}
782
783static void find_line_range_by_func(struct line_finder *lf)
784{
e92b85e1 785 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
631c9def
MH
786}
787
788int find_line_range(int fd, struct line_range *lr)
789{
804b3606 790 struct line_finder lf = {.lr = lr, .found = 0};
631c9def 791 int ret;
804b3606
MH
792 Dwarf_Off off = 0, noff;
793 size_t cuhl;
794 Dwarf_Die *diep;
795 Dwarf *dbg;
804b3606
MH
796
797 dbg = dwarf_begin(fd, DWARF_C_READ);
798 if (!dbg)
631c9def
MH
799 return -ENOENT;
800
804b3606 801 /* Loop on CUs (Compilation Unit) */
631c9def 802 while (!lf.found) {
804b3606
MH
803 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
804 if (ret != 0)
631c9def
MH
805 break;
806
807 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
808 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
809 if (!diep)
810 continue;
631c9def
MH
811
812 /* Check if target file is included. */
813 if (lr->file)
2a9c8c36 814 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
804b3606 815 else
2a9c8c36 816 lf.fname = 0;
631c9def 817
2a9c8c36 818 if (!lr->file || lf.fname) {
631c9def
MH
819 if (lr->function)
820 find_line_range_by_func(&lf);
821 else {
822 lf.lno_s = lr->start;
823 if (!lr->end)
804b3606 824 lf.lno_e = INT_MAX;
631c9def
MH
825 else
826 lf.lno_e = lr->end;
161a26b0 827 find_line_range_by_line(NULL, &lf);
631c9def 828 }
631c9def 829 }
804b3606 830 off = noff;
631c9def 831 }
804b3606
MH
832 pr_debug("path: %lx\n", (unsigned long)lr->path);
833 dwarf_end(dbg);
631c9def
MH
834 return lf.found;
835}
836