perf-probe: Move dwarf library routines to dwarf-aux.{c, h}
[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>
cd932c59 34#include <dwarf-regs.h>
074fc0e4 35
124bb83c 36#include <linux/bitops.h>
89c69c0e
MH
37#include "event.h"
38#include "debug.h"
074fc0e4 39#include "util.h"
9ed7e1b8 40#include "symbol.h"
4ea42b18
MH
41#include "probe-finder.h"
42
4984912e
MH
43/* Kprobe tracer basic type is up to u64 */
44#define MAX_BASIC_TYPE_BITS 64
45
2a9c8c36
MH
46/* Line number list operations */
47
48/* Add a line to line number list */
d3b63d7a 49static int line_list__add_line(struct list_head *head, int line)
2a9c8c36
MH
50{
51 struct line_node *ln;
52 struct list_head *p;
53
54 /* Reverse search, because new line will be the last one */
55 list_for_each_entry_reverse(ln, head, list) {
56 if (ln->line < line) {
57 p = &ln->list;
58 goto found;
59 } else if (ln->line == line) /* Already exist */
e334016f 60 return 1;
2a9c8c36
MH
61 }
62 /* List is empty, or the smallest entry */
63 p = head;
64found:
65 pr_debug("line list: add a line %u\n", line);
e334016f
MH
66 ln = zalloc(sizeof(struct line_node));
67 if (ln == NULL)
68 return -ENOMEM;
2a9c8c36
MH
69 ln->line = line;
70 INIT_LIST_HEAD(&ln->list);
71 list_add(&ln->list, p);
e334016f 72 return 0;
2a9c8c36
MH
73}
74
75/* Check if the line in line number list */
d3b63d7a 76static int line_list__has_line(struct list_head *head, int line)
2a9c8c36
MH
77{
78 struct line_node *ln;
79
80 /* Reverse search, because new line will be the last one */
81 list_for_each_entry(ln, head, list)
82 if (ln->line == line)
83 return 1;
84
85 return 0;
86}
87
88/* Init line number list */
89static void line_list__init(struct list_head *head)
90{
91 INIT_LIST_HEAD(head);
92}
93
94/* Free line number list */
95static void line_list__free(struct list_head *head)
96{
97 struct line_node *ln;
98 while (!list_empty(head)) {
99 ln = list_first_entry(head, struct line_node, list);
100 list_del(&ln->list);
101 free(ln);
102 }
103}
104
469b9b88 105/* Dwarf FL wrappers */
469b9b88
MH
106static char *debuginfo_path; /* Currently dummy */
107
108static const Dwfl_Callbacks offline_callbacks = {
109 .find_debuginfo = dwfl_standard_find_debuginfo,
110 .debuginfo_path = &debuginfo_path,
111
112 .section_address = dwfl_offline_section_address,
113
114 /* We use this table for core files too. */
115 .find_elf = dwfl_build_id_find_elf,
116};
117
469b9b88
MH
118/* Get a Dwarf from offline image */
119static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
120{
121 Dwfl_Module *mod;
122 Dwarf *dbg = NULL;
123
124 if (!dwflp)
125 return NULL;
126
127 *dwflp = dwfl_begin(&offline_callbacks);
128 if (!*dwflp)
129 return NULL;
130
131 mod = dwfl_report_offline(*dwflp, "", "", fd);
132 if (!mod)
133 goto error;
134
135 dbg = dwfl_module_getdwarf(mod, bias);
136 if (!dbg) {
137error:
138 dwfl_end(*dwflp);
139 *dwflp = NULL;
140 }
141 return dbg;
142}
143
3b4694de
MH
144#if _ELFUTILS_PREREQ(0, 148)
145/* This method is buggy if elfutils is older than 0.148 */
146static int __linux_kernel_find_elf(Dwfl_Module *mod,
147 void **userdata,
148 const char *module_name,
149 Dwarf_Addr base,
150 char **file_name, Elf **elfp)
151{
152 int fd;
153 const char *path = kernel_get_module_path(module_name);
154
155 pr_debug2("Use file %s for %s\n", path, module_name);
156 if (path) {
157 fd = open(path, O_RDONLY);
158 if (fd >= 0) {
159 *file_name = strdup(path);
160 return fd;
161 }
162 }
163 /* If failed, try to call standard method */
164 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
165 file_name, elfp);
166}
167
168static const Dwfl_Callbacks kernel_callbacks = {
169 .find_debuginfo = dwfl_standard_find_debuginfo,
170 .debuginfo_path = &debuginfo_path,
171
172 .find_elf = __linux_kernel_find_elf,
173 .section_address = dwfl_linux_kernel_module_section_address,
174};
175
469b9b88
MH
176/* Get a Dwarf from live kernel image */
177static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
178 Dwarf_Addr *bias)
179{
180 Dwarf *dbg;
181
182 if (!dwflp)
183 return NULL;
184
185 *dwflp = dwfl_begin(&kernel_callbacks);
186 if (!*dwflp)
187 return NULL;
188
189 /* Load the kernel dwarves: Don't care the result here */
190 dwfl_linux_kernel_report_kernel(*dwflp);
191 dwfl_linux_kernel_report_modules(*dwflp);
192
193 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
194 /* Here, check whether we could get a real dwarf */
195 if (!dbg) {
3b4694de
MH
196 pr_debug("Failed to find kernel dwarf at %lx\n",
197 (unsigned long)addr);
469b9b88
MH
198 dwfl_end(*dwflp);
199 *dwflp = NULL;
200 }
201 return dbg;
202}
3b4694de
MH
203#else
204/* With older elfutils, this just support kernel module... */
205static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
206 Dwarf_Addr *bias)
207{
208 int fd;
209 const char *path = kernel_get_module_path("kernel");
210
211 if (!path) {
212 pr_err("Failed to find vmlinux path\n");
213 return NULL;
214 }
215
216 pr_debug2("Use file %s for debuginfo\n", path);
217 fd = open(path, O_RDONLY);
218 if (fd < 0)
219 return NULL;
220
221 return dwfl_init_offline_dwarf(fd, dwflp, bias);
222}
223#endif
469b9b88 224
4ea42b18
MH
225/*
226 * Probe finder related functions
227 */
228
0e60836b 229static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
b7dcb857 230{
0e60836b
SD
231 struct probe_trace_arg_ref *ref;
232 ref = zalloc(sizeof(struct probe_trace_arg_ref));
b7dcb857
MH
233 if (ref != NULL)
234 ref->offset = offs;
235 return ref;
236}
237
cf6eb489
MH
238/*
239 * Convert a location into trace_arg.
240 * If tvar == NULL, this just checks variable can be converted.
241 */
242static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
243 Dwarf_Op *fb_ops,
244 struct probe_trace_arg *tvar)
4ea42b18 245{
b7dcb857
MH
246 Dwarf_Attribute attr;
247 Dwarf_Op *op;
248 size_t nops;
804b3606
MH
249 unsigned int regn;
250 Dwarf_Word offs = 0;
4235b045 251 bool ref = false;
4ea42b18 252 const char *regs;
b7dcb857
MH
253 int ret;
254
632941c4
MH
255 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
256 goto static_var;
257
b7dcb857
MH
258 /* TODO: handle more than 1 exprs */
259 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
cf6eb489 260 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
b7dcb857
MH
261 nops == 0) {
262 /* TODO: Support const_value */
b7dcb857
MH
263 return -ENOENT;
264 }
265
266 if (op->atom == DW_OP_addr) {
632941c4 267static_var:
cf6eb489
MH
268 if (!tvar)
269 return 0;
b7dcb857
MH
270 /* Static variables on memory (not stack), make @varname */
271 ret = strlen(dwarf_diename(vr_die));
272 tvar->value = zalloc(ret + 2);
273 if (tvar->value == NULL)
274 return -ENOMEM;
275 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
276 tvar->ref = alloc_trace_arg_ref((long)offs);
277 if (tvar->ref == NULL)
278 return -ENOMEM;
279 return 0;
280 }
4ea42b18 281
4ea42b18 282 /* If this is based on frame buffer, set the offset */
804b3606 283 if (op->atom == DW_OP_fbreg) {
cf6eb489 284 if (fb_ops == NULL)
b55a87ad 285 return -ENOTSUP;
4235b045 286 ref = true;
804b3606 287 offs = op->number;
cf6eb489 288 op = &fb_ops[0];
804b3606 289 }
4ea42b18 290
804b3606
MH
291 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
292 regn = op->atom - DW_OP_breg0;
293 offs += op->number;
4235b045 294 ref = true;
804b3606
MH
295 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
296 regn = op->atom - DW_OP_reg0;
297 } else if (op->atom == DW_OP_bregx) {
298 regn = op->number;
299 offs += op->number2;
4235b045 300 ref = true;
804b3606
MH
301 } else if (op->atom == DW_OP_regx) {
302 regn = op->number;
b55a87ad 303 } else {
cf6eb489 304 pr_debug("DW_OP %x is not supported.\n", op->atom);
b55a87ad
MH
305 return -ENOTSUP;
306 }
4ea42b18 307
cf6eb489
MH
308 if (!tvar)
309 return 0;
310
4ea42b18 311 regs = get_arch_regstr(regn);
b55a87ad 312 if (!regs) {
cf6eb489 313 /* This should be a bug in DWARF or this tool */
0e43e5d2
MH
314 pr_warning("Mapping for the register number %u "
315 "missing on this architecture.\n", regn);
b55a87ad
MH
316 return -ERANGE;
317 }
4ea42b18 318
02b95dad
MH
319 tvar->value = strdup(regs);
320 if (tvar->value == NULL)
321 return -ENOMEM;
322
4235b045 323 if (ref) {
b7dcb857 324 tvar->ref = alloc_trace_arg_ref((long)offs);
e334016f
MH
325 if (tvar->ref == NULL)
326 return -ENOMEM;
4235b045 327 }
b55a87ad 328 return 0;
4ea42b18
MH
329}
330
124bb83c
MH
331#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
332
b55a87ad 333static int convert_variable_type(Dwarf_Die *vr_die,
0e60836b 334 struct probe_trace_arg *tvar,
73317b95 335 const char *cast)
4984912e 336{
0e60836b 337 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
4984912e
MH
338 Dwarf_Die type;
339 char buf[16];
bcfc0821 340 int bsize, boffs, total;
4984912e
MH
341 int ret;
342
73317b95
MH
343 /* TODO: check all types */
344 if (cast && strcmp(cast, "string") != 0) {
345 /* Non string type is OK */
346 tvar->type = strdup(cast);
347 return (tvar->type == NULL) ? -ENOMEM : 0;
348 }
349
bcfc0821
MH
350 bsize = dwarf_bitsize(vr_die);
351 if (bsize > 0) {
124bb83c 352 /* This is a bitfield */
bcfc0821
MH
353 boffs = dwarf_bitoffset(vr_die);
354 total = dwarf_bytesize(vr_die);
355 if (boffs < 0 || total < 0)
356 return -ENOENT;
357 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
358 BYTES_TO_BITS(total));
124bb83c
MH
359 goto formatted;
360 }
361
b55a87ad
MH
362 if (die_get_real_type(vr_die, &type) == NULL) {
363 pr_warning("Failed to get a type information of %s.\n",
364 dwarf_diename(vr_die));
365 return -ENOENT;
366 }
4984912e 367
b2a3c12b
MH
368 pr_debug("%s type is %s.\n",
369 dwarf_diename(vr_die), dwarf_diename(&type));
370
73317b95
MH
371 if (cast && strcmp(cast, "string") == 0) { /* String type */
372 ret = dwarf_tag(&type);
373 if (ret != DW_TAG_pointer_type &&
374 ret != DW_TAG_array_type) {
375 pr_warning("Failed to cast into string: "
0e43e5d2 376 "%s(%s) is not a pointer nor array.\n",
73317b95
MH
377 dwarf_diename(vr_die), dwarf_diename(&type));
378 return -EINVAL;
379 }
380 if (ret == DW_TAG_pointer_type) {
381 if (die_get_real_type(&type, &type) == NULL) {
0e43e5d2
MH
382 pr_warning("Failed to get a type"
383 " information.\n");
73317b95
MH
384 return -ENOENT;
385 }
386 while (*ref_ptr)
387 ref_ptr = &(*ref_ptr)->next;
388 /* Add new reference with offset +0 */
0e60836b 389 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
73317b95
MH
390 if (*ref_ptr == NULL) {
391 pr_warning("Out of memory error\n");
392 return -ENOMEM;
393 }
394 }
82175633
MH
395 if (!die_compare_name(&type, "char") &&
396 !die_compare_name(&type, "unsigned char")) {
73317b95 397 pr_warning("Failed to cast into string: "
0e43e5d2 398 "%s is not (unsigned) char *.\n",
73317b95
MH
399 dwarf_diename(vr_die));
400 return -EINVAL;
401 }
402 tvar->type = strdup(cast);
403 return (tvar->type == NULL) ? -ENOMEM : 0;
404 }
405
bcfc0821
MH
406 ret = dwarf_bytesize(&type);
407 if (ret <= 0)
124bb83c
MH
408 /* No size ... try to use default type */
409 return 0;
bcfc0821 410 ret = BYTES_TO_BITS(ret);
4984912e 411
124bb83c
MH
412 /* Check the bitwidth */
413 if (ret > MAX_BASIC_TYPE_BITS) {
414 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
415 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
416 ret = MAX_BASIC_TYPE_BITS;
4984912e 417 }
124bb83c
MH
418 ret = snprintf(buf, 16, "%c%d",
419 die_is_signed_type(&type) ? 's' : 'u', ret);
420
421formatted:
422 if (ret < 0 || ret >= 16) {
423 if (ret >= 16)
424 ret = -E2BIG;
425 pr_warning("Failed to convert variable type: %s\n",
426 strerror(-ret));
427 return ret;
428 }
429 tvar->type = strdup(buf);
430 if (tvar->type == NULL)
431 return -ENOMEM;
b55a87ad 432 return 0;
4984912e
MH
433}
434
b55a87ad 435static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
7df2f329 436 struct perf_probe_arg_field *field,
0e60836b 437 struct probe_trace_arg_ref **ref_ptr,
4984912e 438 Dwarf_Die *die_mem)
7df2f329 439{
0e60836b 440 struct probe_trace_arg_ref *ref = *ref_ptr;
7df2f329
MH
441 Dwarf_Die type;
442 Dwarf_Word offs;
b2a3c12b 443 int ret, tag;
7df2f329
MH
444
445 pr_debug("converting %s in %s\n", field->name, varname);
b55a87ad
MH
446 if (die_get_real_type(vr_die, &type) == NULL) {
447 pr_warning("Failed to get the type of %s.\n", varname);
448 return -ENOENT;
449 }
b2a3c12b
MH
450 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
451 tag = dwarf_tag(&type);
452
453 if (field->name[0] == '[' &&
454 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
455 if (field->next)
456 /* Save original type for next field */
457 memcpy(die_mem, &type, sizeof(*die_mem));
458 /* Get the type of this array */
459 if (die_get_real_type(&type, &type) == NULL) {
460 pr_warning("Failed to get the type of %s.\n", varname);
461 return -ENOENT;
462 }
463 pr_debug2("Array real type: (%x)\n",
464 (unsigned)dwarf_dieoffset(&type));
465 if (tag == DW_TAG_pointer_type) {
0e60836b 466 ref = zalloc(sizeof(struct probe_trace_arg_ref));
b2a3c12b
MH
467 if (ref == NULL)
468 return -ENOMEM;
469 if (*ref_ptr)
470 (*ref_ptr)->next = ref;
471 else
472 *ref_ptr = ref;
473 }
bcfc0821 474 ref->offset += dwarf_bytesize(&type) * field->index;
b2a3c12b
MH
475 if (!field->next)
476 /* Save vr_die for converting types */
477 memcpy(die_mem, vr_die, sizeof(*die_mem));
478 goto next;
479 } else if (tag == DW_TAG_pointer_type) {
480 /* Check the pointer and dereference */
b55a87ad
MH
481 if (!field->ref) {
482 pr_err("Semantic error: %s must be referred by '->'\n",
483 field->name);
484 return -EINVAL;
485 }
7df2f329 486 /* Get the type pointed by this pointer */
b55a87ad
MH
487 if (die_get_real_type(&type, &type) == NULL) {
488 pr_warning("Failed to get the type of %s.\n", varname);
489 return -ENOENT;
490 }
12e5a7ae 491 /* Verify it is a data structure */
b55a87ad
MH
492 if (dwarf_tag(&type) != DW_TAG_structure_type) {
493 pr_warning("%s is not a data structure.\n", varname);
494 return -EINVAL;
495 }
12e5a7ae 496
0e60836b 497 ref = zalloc(sizeof(struct probe_trace_arg_ref));
e334016f
MH
498 if (ref == NULL)
499 return -ENOMEM;
7df2f329
MH
500 if (*ref_ptr)
501 (*ref_ptr)->next = ref;
502 else
503 *ref_ptr = ref;
504 } else {
12e5a7ae 505 /* Verify it is a data structure */
b2a3c12b 506 if (tag != DW_TAG_structure_type) {
b55a87ad
MH
507 pr_warning("%s is not a data structure.\n", varname);
508 return -EINVAL;
509 }
b2a3c12b 510 if (field->name[0] == '[') {
0e43e5d2
MH
511 pr_err("Semantic error: %s is not a pointor"
512 " nor array.\n", varname);
b2a3c12b
MH
513 return -EINVAL;
514 }
b55a87ad
MH
515 if (field->ref) {
516 pr_err("Semantic error: %s must be referred by '.'\n",
517 field->name);
518 return -EINVAL;
519 }
520 if (!ref) {
521 pr_warning("Structure on a register is not "
522 "supported yet.\n");
523 return -ENOTSUP;
524 }
7df2f329
MH
525 }
526
b55a87ad
MH
527 if (die_find_member(&type, field->name, die_mem) == NULL) {
528 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
529 dwarf_diename(&type), field->name);
530 return -EINVAL;
531 }
7df2f329
MH
532
533 /* Get the offset of the field */
de1439d8
MH
534 ret = die_get_data_member_location(die_mem, &offs);
535 if (ret < 0) {
b55a87ad 536 pr_warning("Failed to get the offset of %s.\n", field->name);
de1439d8 537 return ret;
b55a87ad 538 }
7df2f329
MH
539 ref->offset += (long)offs;
540
b2a3c12b 541next:
7df2f329
MH
542 /* Converting next field */
543 if (field->next)
b55a87ad 544 return convert_variable_fields(die_mem, field->name,
de1439d8 545 field->next, &ref, die_mem);
b55a87ad
MH
546 else
547 return 0;
7df2f329
MH
548}
549
4ea42b18 550/* Show a variables in kprobe event format */
b55a87ad 551static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
4ea42b18 552{
4984912e 553 Dwarf_Die die_mem;
4ea42b18
MH
554 int ret;
555
b7dcb857
MH
556 pr_debug("Converting variable %s into trace event.\n",
557 dwarf_diename(vr_die));
804b3606 558
cf6eb489
MH
559 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
560 pf->tvar);
561 if (ret == -ENOENT)
562 pr_err("Failed to find the location of %s at this address.\n"
563 " Perhaps, it has been optimized out.\n", pf->pvar->var);
564 else if (ret == -ENOTSUP)
565 pr_err("Sorry, we don't support this variable location yet.\n");
566 else if (pf->pvar->field) {
b55a87ad
MH
567 ret = convert_variable_fields(vr_die, pf->pvar->var,
568 pf->pvar->field, &pf->tvar->ref,
569 &die_mem);
4984912e
MH
570 vr_die = &die_mem;
571 }
73317b95
MH
572 if (ret == 0)
573 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
804b3606 574 /* *expr will be cached in libdw. Don't free it. */
b55a87ad 575 return ret;
4ea42b18
MH
576}
577
4ea42b18 578/* Find a variable in a subprogram die */
b55a87ad 579static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18 580{
b7dcb857 581 Dwarf_Die vr_die, *scopes;
11a1ca35 582 char buf[32], *ptr;
b7dcb857 583 int ret, nscopes;
4ea42b18 584
367e94c1
MH
585 if (!is_c_varname(pf->pvar->var)) {
586 /* Copy raw parameters */
587 pf->tvar->value = strdup(pf->pvar->var);
588 if (pf->tvar->value == NULL)
589 return -ENOMEM;
590 if (pf->pvar->type) {
591 pf->tvar->type = strdup(pf->pvar->type);
592 if (pf->tvar->type == NULL)
593 return -ENOMEM;
594 }
595 if (pf->pvar->name) {
596 pf->tvar->name = strdup(pf->pvar->name);
597 if (pf->tvar->name == NULL)
598 return -ENOMEM;
599 } else
600 pf->tvar->name = NULL;
601 return 0;
602 }
603
48481938 604 if (pf->pvar->name)
02b95dad 605 pf->tvar->name = strdup(pf->pvar->name);
48481938 606 else {
02b95dad
MH
607 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
608 if (ret < 0)
609 return ret;
11a1ca35
MH
610 ptr = strchr(buf, ':'); /* Change type separator to _ */
611 if (ptr)
612 *ptr = '_';
02b95dad 613 pf->tvar->name = strdup(buf);
48481938 614 }
02b95dad
MH
615 if (pf->tvar->name == NULL)
616 return -ENOMEM;
48481938 617
b55a87ad
MH
618 pr_debug("Searching '%s' variable in context.\n",
619 pf->pvar->var);
620 /* Search child die for local variables and parameters. */
378eeaad 621 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
b7dcb857
MH
622 ret = convert_variable(&vr_die, pf);
623 else {
624 /* Search upper class */
625 nscopes = dwarf_getscopes_die(sp_die, &scopes);
632941c4
MH
626 while (nscopes-- > 1) {
627 pr_debug("Searching variables in %s\n",
628 dwarf_diename(&scopes[nscopes]));
629 /* We should check this scope, so give dummy address */
630 if (die_find_variable_at(&scopes[nscopes],
631 pf->pvar->var, 0,
632 &vr_die)) {
b7dcb857 633 ret = convert_variable(&vr_die, pf);
632941c4
MH
634 goto found;
635 }
636 }
637 if (scopes)
b7dcb857 638 free(scopes);
632941c4 639 ret = -ENOENT;
b7dcb857 640 }
632941c4 641found:
b7dcb857 642 if (ret < 0)
b55a87ad
MH
643 pr_warning("Failed to find '%s' in this function.\n",
644 pf->pvar->var);
b7dcb857 645 return ret;
4ea42b18
MH
646}
647
cf6eb489
MH
648/* Convert subprogram DIE to trace point */
649static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
650 bool retprobe, struct probe_trace_point *tp)
4ea42b18 651{
e92b85e1 652 Dwarf_Addr eaddr;
804b3606 653 const char *name;
e92b85e1 654
4235b045 655 /* Copy the name of probe point */
804b3606
MH
656 name = dwarf_diename(sp_die);
657 if (name) {
b55a87ad 658 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
0e43e5d2 659 pr_warning("Failed to get entry address of %s\n",
b55a87ad
MH
660 dwarf_diename(sp_die));
661 return -ENOENT;
662 }
cf6eb489
MH
663 tp->symbol = strdup(name);
664 if (tp->symbol == NULL)
02b95dad 665 return -ENOMEM;
cf6eb489 666 tp->offset = (unsigned long)(paddr - eaddr);
4235b045 667 } else
4ea42b18 668 /* This function has no name. */
cf6eb489 669 tp->offset = (unsigned long)paddr;
4235b045 670
04ddd04b 671 /* Return probe must be on the head of a subprogram */
cf6eb489
MH
672 if (retprobe) {
673 if (eaddr != paddr) {
04ddd04b 674 pr_warning("Return probe must be on the head of"
0e43e5d2 675 " a real function.\n");
04ddd04b
MH
676 return -EINVAL;
677 }
cf6eb489 678 tp->retprobe = true;
04ddd04b
MH
679 }
680
cf6eb489
MH
681 return 0;
682}
683
684/* Call probe_finder callback with real subprogram DIE */
685static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
686{
687 Dwarf_Die die_mem;
688 Dwarf_Attribute fb_attr;
689 size_t nops;
690 int ret;
691
692 /* If no real subprogram, find a real one */
693 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
e0d153c6 694 sp_die = die_find_realfunc(&pf->cu_die, pf->addr, &die_mem);
cf6eb489
MH
695 if (!sp_die) {
696 pr_warning("Failed to find probe point in any "
697 "functions.\n");
698 return -ENOENT;
699 }
700 }
4ea42b18 701
804b3606
MH
702 /* Get the frame base attribute/ops */
703 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
d0cb4260 704 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
a34a9854 705 if (ret <= 0 || nops == 0) {
804b3606 706 pf->fb_ops = NULL;
7752f1b0 707#if _ELFUTILS_PREREQ(0, 142)
a34a9854
MH
708 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
709 pf->cfi != NULL) {
710 Dwarf_Frame *frame;
b55a87ad
MH
711 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
712 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
0e43e5d2 713 pr_warning("Failed to get call frame on 0x%jx\n",
b55a87ad
MH
714 (uintmax_t)pf->addr);
715 return -ENOENT;
716 }
7752f1b0 717#endif
a34a9854 718 }
804b3606 719
cf6eb489
MH
720 /* Call finder's callback handler */
721 ret = pf->callback(sp_die, pf);
804b3606
MH
722
723 /* *pf->fb_ops will be cached in libdw. Don't free it. */
724 pf->fb_ops = NULL;
cf6eb489
MH
725
726 return ret;
4ea42b18
MH
727}
728
4cc9cec6
MH
729static int probe_point_line_walker(const char *fname, int lineno,
730 Dwarf_Addr addr, void *data)
4ea42b18 731{
4cc9cec6
MH
732 struct probe_finder *pf = data;
733 int ret;
4ea42b18 734
4cc9cec6
MH
735 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
736 return 0;
4ea42b18 737
4cc9cec6
MH
738 pf->addr = addr;
739 ret = call_probe_finder(NULL, pf);
b0ef0732 740
4cc9cec6 741 /* Continue if no error, because the line will be in inline function */
fbee632d 742 return ret < 0 ? ret : 0;
4cc9cec6 743}
804b3606 744
4cc9cec6
MH
745/* Find probe point from its line number */
746static int find_probe_point_by_line(struct probe_finder *pf)
747{
748 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
4ea42b18
MH
749}
750
2a9c8c36
MH
751/* Find lines which match lazy pattern */
752static int find_lazy_match_lines(struct list_head *head,
753 const char *fname, const char *pat)
754{
f50c2169
FBH
755 FILE *fp;
756 char *line = NULL;
757 size_t line_len;
758 ssize_t len;
759 int count = 0, linenum = 1;
760
761 fp = fopen(fname, "r");
762 if (!fp) {
763 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
b448c4b6 764 return -errno;
b55a87ad
MH
765 }
766
f50c2169 767 while ((len = getline(&line, &line_len, fp)) > 0) {
b448c4b6 768
f50c2169
FBH
769 if (line[len - 1] == '\n')
770 line[len - 1] = '\0';
771
772 if (strlazymatch(line, pat)) {
773 line_list__add_line(head, linenum);
774 count++;
2a9c8c36 775 }
f50c2169 776 linenum++;
2a9c8c36 777 }
f50c2169
FBH
778
779 if (ferror(fp))
780 count = -errno;
781 free(line);
782 fclose(fp);
783
784 if (count == 0)
785 pr_debug("No matched lines found in %s.\n", fname);
786 return count;
2a9c8c36
MH
787}
788
4cc9cec6
MH
789static int probe_point_lazy_walker(const char *fname, int lineno,
790 Dwarf_Addr addr, void *data)
791{
792 struct probe_finder *pf = data;
793 int ret;
794
795 if (!line_list__has_line(&pf->lcache, lineno) ||
796 strtailcmp(fname, pf->fname) != 0)
797 return 0;
798
799 pr_debug("Probe line found: line:%d addr:0x%llx\n",
800 lineno, (unsigned long long)addr);
801 pf->addr = addr;
802 ret = call_probe_finder(NULL, pf);
803
804 /*
805 * Continue if no error, because the lazy pattern will match
806 * to other lines
807 */
5e814dd5 808 return ret < 0 ? ret : 0;
4cc9cec6
MH
809}
810
2a9c8c36 811/* Find probe points from lazy pattern */
b55a87ad 812static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
2a9c8c36 813{
b55a87ad 814 int ret = 0;
2a9c8c36
MH
815
816 if (list_empty(&pf->lcache)) {
817 /* Matching lazy line pattern */
818 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
4235b045 819 pf->pev->point.lazy_line);
f50c2169 820 if (ret <= 0)
b55a87ad 821 return ret;
2a9c8c36
MH
822 }
823
4cc9cec6 824 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
2a9c8c36
MH
825}
826
b55a87ad
MH
827/* Callback parameter with return value */
828struct dwarf_callback_param {
829 void *data;
830 int retval;
831};
832
e92b85e1
MH
833static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
834{
b55a87ad
MH
835 struct dwarf_callback_param *param = data;
836 struct probe_finder *pf = param->data;
4235b045 837 struct perf_probe_point *pp = &pf->pev->point;
b55a87ad 838 Dwarf_Addr addr;
e92b85e1 839
2a9c8c36 840 if (pp->lazy_line)
b55a87ad 841 param->retval = find_probe_point_lazy(in_die, pf);
2a9c8c36
MH
842 else {
843 /* Get probe address */
b55a87ad 844 if (dwarf_entrypc(in_die, &addr) != 0) {
0e43e5d2 845 pr_warning("Failed to get entry address of %s.\n",
b55a87ad
MH
846 dwarf_diename(in_die));
847 param->retval = -ENOENT;
848 return DWARF_CB_ABORT;
849 }
850 pf->addr = addr;
2a9c8c36
MH
851 pf->addr += pp->offset;
852 pr_debug("found inline addr: 0x%jx\n",
853 (uintmax_t)pf->addr);
854
cf6eb489 855 param->retval = call_probe_finder(in_die, pf);
5d1ee041
MH
856 if (param->retval < 0)
857 return DWARF_CB_ABORT;
2a9c8c36 858 }
e92b85e1 859
e92b85e1
MH
860 return DWARF_CB_OK;
861}
804b3606 862
4ea42b18 863/* Search function from function name */
e92b85e1 864static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
4ea42b18 865{
b55a87ad
MH
866 struct dwarf_callback_param *param = data;
867 struct probe_finder *pf = param->data;
4235b045 868 struct perf_probe_point *pp = &pf->pev->point;
4ea42b18 869
e92b85e1
MH
870 /* Check tag and diename */
871 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
82175633 872 !die_compare_name(sp_die, pp->function))
b55a87ad 873 return DWARF_CB_OK;
e92b85e1 874
7d21635a
MH
875 /* Check declared file */
876 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
877 return DWARF_CB_OK;
878
2a9c8c36 879 pf->fname = dwarf_decl_file(sp_die);
e92b85e1 880 if (pp->line) { /* Function relative line */
e92b85e1
MH
881 dwarf_decl_line(sp_die, &pf->lno);
882 pf->lno += pp->line;
b55a87ad 883 param->retval = find_probe_point_by_line(pf);
e92b85e1
MH
884 } else if (!dwarf_func_inline(sp_die)) {
885 /* Real function */
2a9c8c36 886 if (pp->lazy_line)
b55a87ad 887 param->retval = find_probe_point_lazy(sp_die, pf);
2a9c8c36 888 else {
b55a87ad 889 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
0e43e5d2
MH
890 pr_warning("Failed to get entry address of "
891 "%s.\n", dwarf_diename(sp_die));
b55a87ad
MH
892 param->retval = -ENOENT;
893 return DWARF_CB_ABORT;
894 }
2a9c8c36
MH
895 pf->addr += pp->offset;
896 /* TODO: Check the address in this function */
cf6eb489 897 param->retval = call_probe_finder(sp_die, pf);
2a9c8c36 898 }
b55a87ad
MH
899 } else {
900 struct dwarf_callback_param _param = {.data = (void *)pf,
901 .retval = 0};
e92b85e1 902 /* Inlined function: search instances */
b55a87ad
MH
903 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
904 &_param);
905 param->retval = _param.retval;
906 }
e92b85e1 907
b55a87ad 908 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
4ea42b18
MH
909}
910
b55a87ad 911static int find_probe_point_by_func(struct probe_finder *pf)
4ea42b18 912{
b55a87ad
MH
913 struct dwarf_callback_param _param = {.data = (void *)pf,
914 .retval = 0};
915 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
916 return _param.retval;
4ea42b18
MH
917}
918
cd25f8bc
LM
919struct pubname_callback_param {
920 char *function;
921 char *file;
922 Dwarf_Die *cu_die;
923 Dwarf_Die *sp_die;
924 int found;
925};
926
927static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
928{
929 struct pubname_callback_param *param = data;
930
931 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
932 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
933 return DWARF_CB_OK;
934
935 if (die_compare_name(param->sp_die, param->function)) {
936 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
937 return DWARF_CB_OK;
938
939 if (param->file &&
940 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
941 return DWARF_CB_OK;
942
943 param->found = 1;
944 return DWARF_CB_ABORT;
945 }
946 }
947
948 return DWARF_CB_OK;
949}
950
cf6eb489
MH
951/* Find probe points from debuginfo */
952static int find_probes(int fd, struct probe_finder *pf)
4ea42b18 953{
cf6eb489 954 struct perf_probe_point *pp = &pf->pev->point;
804b3606
MH
955 Dwarf_Off off, noff;
956 size_t cuhl;
957 Dwarf_Die *diep;
469b9b88
MH
958 Dwarf *dbg = NULL;
959 Dwfl *dwfl;
960 Dwarf_Addr bias; /* Currently ignored */
b55a87ad 961 int ret = 0;
804b3606 962
469b9b88 963 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
b55a87ad 964 if (!dbg) {
0e43e5d2 965 pr_warning("No debug information found in the vmlinux - "
b55a87ad 966 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
f0c4801a 967 close(fd); /* Without dwfl_end(), fd isn't closed. */
b55a87ad
MH
968 return -EBADF;
969 }
4ea42b18 970
7752f1b0 971#if _ELFUTILS_PREREQ(0, 142)
a34a9854 972 /* Get the call frame information from this dwarf */
cf6eb489 973 pf->cfi = dwarf_getcfi(dbg);
7752f1b0 974#endif
a34a9854 975
804b3606 976 off = 0;
cf6eb489 977 line_list__init(&pf->lcache);
cd25f8bc
LM
978
979 /* Fastpath: lookup by function name from .debug_pubnames section */
980 if (pp->function) {
981 struct pubname_callback_param pubname_param = {
982 .function = pp->function,
983 .file = pp->file,
984 .cu_die = &pf->cu_die,
985 .sp_die = &pf->sp_die,
2b348a77 986 .found = 0,
cd25f8bc
LM
987 };
988 struct dwarf_callback_param probe_param = {
989 .data = pf,
990 };
991
992 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
993 if (pubname_param.found) {
994 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
995 if (ret)
996 goto found;
997 }
998 }
999
804b3606 1000 /* Loop on CUs (Compilation Unit) */
8635bf6e 1001 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
4ea42b18 1002 /* Get the DIE(Debugging Information Entry) of this CU */
cf6eb489 1003 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
804b3606
MH
1004 if (!diep)
1005 continue;
4ea42b18
MH
1006
1007 /* Check if target file is included. */
1008 if (pp->file)
cf6eb489 1009 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
804b3606 1010 else
cf6eb489 1011 pf->fname = NULL;
4ea42b18 1012
cf6eb489 1013 if (!pp->file || pf->fname) {
4ea42b18 1014 if (pp->function)
cf6eb489 1015 ret = find_probe_point_by_func(pf);
2a9c8c36 1016 else if (pp->lazy_line)
cf6eb489 1017 ret = find_probe_point_lazy(NULL, pf);
b0ef0732 1018 else {
cf6eb489
MH
1019 pf->lno = pp->line;
1020 ret = find_probe_point_by_line(pf);
b0ef0732 1021 }
8635bf6e 1022 if (ret < 0)
fbee632d 1023 break;
4ea42b18 1024 }
804b3606 1025 off = noff;
4ea42b18 1026 }
cd25f8bc
LM
1027
1028found:
cf6eb489 1029 line_list__free(&pf->lcache);
469b9b88
MH
1030 if (dwfl)
1031 dwfl_end(dwfl);
4ea42b18 1032
cf6eb489
MH
1033 return ret;
1034}
1035
1036/* Add a found probe point into trace event list */
1037static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1038{
1039 struct trace_event_finder *tf =
1040 container_of(pf, struct trace_event_finder, pf);
1041 struct probe_trace_event *tev;
1042 int ret, i;
1043
1044 /* Check number of tevs */
1045 if (tf->ntevs == tf->max_tevs) {
1046 pr_warning("Too many( > %d) probe point found.\n",
1047 tf->max_tevs);
1048 return -ERANGE;
1049 }
1050 tev = &tf->tevs[tf->ntevs++];
1051
1052 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1053 &tev->point);
1054 if (ret < 0)
1055 return ret;
1056
1057 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1058 tev->point.offset);
1059
1060 /* Find each argument */
1061 tev->nargs = pf->pev->nargs;
1062 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1063 if (tev->args == NULL)
1064 return -ENOMEM;
1065 for (i = 0; i < pf->pev->nargs; i++) {
1066 pf->pvar = &pf->pev->args[i];
1067 pf->tvar = &tev->args[i];
1068 ret = find_variable(sp_die, pf);
1069 if (ret != 0)
1070 return ret;
1071 }
1072
1073 return 0;
1074}
1075
1076/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1077int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1078 struct probe_trace_event **tevs, int max_tevs)
1079{
1080 struct trace_event_finder tf = {
1081 .pf = {.pev = pev, .callback = add_probe_trace_event},
1082 .max_tevs = max_tevs};
1083 int ret;
1084
1085 /* Allocate result tevs array */
1086 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1087 if (*tevs == NULL)
1088 return -ENOMEM;
1089
1090 tf.tevs = *tevs;
1091 tf.ntevs = 0;
1092
1093 ret = find_probes(fd, &tf.pf);
1094 if (ret < 0) {
1095 free(*tevs);
1096 *tevs = NULL;
1097 return ret;
1098 }
1099
1100 return (ret < 0) ? ret : tf.ntevs;
1101}
1102
1103#define MAX_VAR_LEN 64
1104
1105/* Collect available variables in this scope */
1106static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1107{
1108 struct available_var_finder *af = data;
1109 struct variable_list *vl;
1110 char buf[MAX_VAR_LEN];
1111 int tag, ret;
1112
1113 vl = &af->vls[af->nvls - 1];
1114
1115 tag = dwarf_tag(die_mem);
1116 if (tag == DW_TAG_formal_parameter ||
1117 tag == DW_TAG_variable) {
1118 ret = convert_variable_location(die_mem, af->pf.addr,
1119 af->pf.fb_ops, NULL);
1120 if (ret == 0) {
1121 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
fb8c5a56 1122 pr_debug2("Add new var: %s\n", buf);
cf6eb489
MH
1123 if (ret > 0)
1124 strlist__add(vl->vars, buf);
1125 }
1126 }
1127
fb8c5a56 1128 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
cf6eb489
MH
1129 return DIE_FIND_CB_CONTINUE;
1130 else
1131 return DIE_FIND_CB_SIBLING;
1132}
1133
1134/* Add a found vars into available variables list */
1135static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1136{
1137 struct available_var_finder *af =
1138 container_of(pf, struct available_var_finder, pf);
1139 struct variable_list *vl;
fb8c5a56
MH
1140 Dwarf_Die die_mem, *scopes = NULL;
1141 int ret, nscopes;
cf6eb489
MH
1142
1143 /* Check number of tevs */
1144 if (af->nvls == af->max_vls) {
1145 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1146 return -ERANGE;
1147 }
1148 vl = &af->vls[af->nvls++];
1149
1150 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1151 &vl->point);
1152 if (ret < 0)
1153 return ret;
1154
1155 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1156 vl->point.offset);
1157
1158 /* Find local variables */
1159 vl->vars = strlist__new(true, NULL);
1160 if (vl->vars == NULL)
1161 return -ENOMEM;
fb8c5a56 1162 af->child = true;
cf6eb489
MH
1163 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1164
fb8c5a56
MH
1165 /* Find external variables */
1166 if (!af->externs)
1167 goto out;
1168 /* Don't need to search child DIE for externs. */
1169 af->child = false;
1170 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1171 while (nscopes-- > 1)
1172 die_find_child(&scopes[nscopes], collect_variables_cb,
1173 (void *)af, &die_mem);
1174 if (scopes)
1175 free(scopes);
1176
1177out:
cf6eb489
MH
1178 if (strlist__empty(vl->vars)) {
1179 strlist__delete(vl->vars);
1180 vl->vars = NULL;
1181 }
1182
1183 return ret;
1184}
1185
1186/* Find available variables at given probe point */
1187int find_available_vars_at(int fd, struct perf_probe_event *pev,
fb8c5a56
MH
1188 struct variable_list **vls, int max_vls,
1189 bool externs)
cf6eb489
MH
1190{
1191 struct available_var_finder af = {
1192 .pf = {.pev = pev, .callback = add_available_vars},
fb8c5a56 1193 .max_vls = max_vls, .externs = externs};
cf6eb489
MH
1194 int ret;
1195
1196 /* Allocate result vls array */
1197 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1198 if (*vls == NULL)
1199 return -ENOMEM;
1200
1201 af.vls = *vls;
1202 af.nvls = 0;
1203
1204 ret = find_probes(fd, &af.pf);
1205 if (ret < 0) {
1206 /* Free vlist for error */
1207 while (af.nvls--) {
1208 if (af.vls[af.nvls].point.symbol)
1209 free(af.vls[af.nvls].point.symbol);
1210 if (af.vls[af.nvls].vars)
1211 strlist__delete(af.vls[af.nvls].vars);
1212 }
1213 free(af.vls);
1214 *vls = NULL;
1215 return ret;
1216 }
1217
1218 return (ret < 0) ? ret : af.nvls;
4ea42b18
MH
1219}
1220
fb1587d8 1221/* Reverse search */
469b9b88 1222int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
fb1587d8
MH
1223{
1224 Dwarf_Die cudie, spdie, indie;
469b9b88
MH
1225 Dwarf *dbg = NULL;
1226 Dwfl *dwfl = NULL;
1d46ea2a
MH
1227 Dwarf_Addr _addr, baseaddr, bias = 0;
1228 const char *fname = NULL, *func = NULL, *tmp;
1229 int baseline = 0, lineno = 0, ret = 0;
fb1587d8 1230
469b9b88
MH
1231 /* Open the live linux kernel */
1232 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1233 if (!dbg) {
0e43e5d2 1234 pr_warning("No debug information found in the vmlinux - "
469b9b88
MH
1235 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1236 ret = -EINVAL;
1237 goto end;
1238 }
fb1587d8 1239
469b9b88
MH
1240 /* Adjust address with bias */
1241 addr += bias;
fb1587d8 1242 /* Find cu die */
469b9b88 1243 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
0e43e5d2
MH
1244 pr_warning("Failed to find debug information for address %lx\n",
1245 addr);
75ec5a24
MH
1246 ret = -EINVAL;
1247 goto end;
1248 }
fb1587d8 1249
1d46ea2a
MH
1250 /* Find a corresponding line (filename and lineno) */
1251 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1252 /* Don't care whether it failed or not */
fb1587d8 1253
1d46ea2a 1254 /* Find a corresponding function (name, baseline and baseaddr) */
e0d153c6 1255 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
1d46ea2a 1256 /* Get function entry information */
fb1587d8 1257 tmp = dwarf_diename(&spdie);
1d46ea2a
MH
1258 if (!tmp ||
1259 dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1260 dwarf_decl_line(&spdie, &baseline) != 0)
1261 goto post;
1262 func = tmp;
1263
1264 if (addr == (unsigned long)baseaddr)
1265 /* Function entry - Relative line number is 0 */
1266 lineno = baseline;
1267 else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1268 &indie)) {
1269 if (dwarf_entrypc(&indie, &_addr) == 0 &&
1270 _addr == addr)
1271 /*
1272 * addr is at an inline function entry.
1273 * In this case, lineno should be the call-site
1274 * line number.
1275 */
1276 lineno = die_get_call_lineno(&indie);
1277 else {
1278 /*
1279 * addr is in an inline function body.
1280 * Since lineno points one of the lines
1281 * of the inline function, baseline should
1282 * be the entry line of the inline function.
1283 */
b55a87ad 1284 tmp = dwarf_diename(&indie);
1d46ea2a
MH
1285 if (tmp &&
1286 dwarf_decl_line(&spdie, &baseline) == 0)
1287 func = tmp;
b55a87ad 1288 }
fb1587d8 1289 }
1d46ea2a
MH
1290 }
1291
1292post:
1293 /* Make a relative line number or an offset */
1294 if (lineno)
1295 ppt->line = lineno - baseline;
1296 else if (func)
1297 ppt->offset = addr - (unsigned long)baseaddr;
1298
1299 /* Duplicate strings */
1300 if (func) {
1301 ppt->function = strdup(func);
02b95dad
MH
1302 if (ppt->function == NULL) {
1303 ret = -ENOMEM;
1304 goto end;
1305 }
fb1587d8 1306 }
1d46ea2a
MH
1307 if (fname) {
1308 ppt->file = strdup(fname);
1309 if (ppt->file == NULL) {
1310 if (ppt->function) {
1311 free(ppt->function);
1312 ppt->function = NULL;
1313 }
1314 ret = -ENOMEM;
1315 goto end;
1316 }
1317 }
fb1587d8 1318end:
469b9b88
MH
1319 if (dwfl)
1320 dwfl_end(dwfl);
1d46ea2a
MH
1321 if (ret == 0 && (fname || func))
1322 ret = 1; /* Found a point */
fb1587d8
MH
1323 return ret;
1324}
1325
f6c903f5
MH
1326/* Add a line and store the src path */
1327static int line_range_add_line(const char *src, unsigned int lineno,
1328 struct line_range *lr)
1329{
7cf0b79e 1330 /* Copy source path */
f6c903f5 1331 if (!lr->path) {
7cf0b79e
MH
1332 lr->path = strdup(src);
1333 if (lr->path == NULL)
1334 return -ENOMEM;
f6c903f5
MH
1335 }
1336 return line_list__add_line(&lr->line_list, lineno);
1337}
1338
4cc9cec6
MH
1339static int line_range_walk_cb(const char *fname, int lineno,
1340 Dwarf_Addr addr __used,
1341 void *data)
f6c903f5 1342{
4cc9cec6 1343 struct line_finder *lf = data;
f6c903f5 1344
4cc9cec6 1345 if ((strtailcmp(fname, lf->fname) != 0) ||
f6c903f5 1346 (lf->lno_s > lineno || lf->lno_e < lineno))
4cc9cec6 1347 return 0;
f6c903f5 1348
4cc9cec6
MH
1349 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1350 return -EINVAL;
f6c903f5 1351
4cc9cec6 1352 return 0;
f6c903f5 1353}
fb1587d8 1354
631c9def 1355/* Find line range from its line number */
b55a87ad 1356static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
631c9def 1357{
4cc9cec6 1358 int ret;
f6c903f5 1359
4cc9cec6 1360 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
f6c903f5 1361
804b3606 1362 /* Update status */
f6c903f5
MH
1363 if (ret >= 0)
1364 if (!list_empty(&lf->lr->line_list))
1365 ret = lf->found = 1;
1366 else
1367 ret = 0; /* Lines are not found */
804b3606
MH
1368 else {
1369 free(lf->lr->path);
1370 lf->lr->path = NULL;
1371 }
f6c903f5 1372 return ret;
631c9def
MH
1373}
1374
161a26b0
MH
1375static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1376{
b55a87ad
MH
1377 struct dwarf_callback_param *param = data;
1378
1379 param->retval = find_line_range_by_line(in_die, param->data);
161a26b0
MH
1380 return DWARF_CB_ABORT; /* No need to find other instances */
1381}
1382
631c9def 1383/* Search function from function name */
e92b85e1 1384static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
631c9def 1385{
b55a87ad
MH
1386 struct dwarf_callback_param *param = data;
1387 struct line_finder *lf = param->data;
631c9def 1388 struct line_range *lr = lf->lr;
631c9def 1389
7d21635a
MH
1390 /* Check declared file */
1391 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1392 return DWARF_CB_OK;
1393
e92b85e1 1394 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
82175633 1395 die_compare_name(sp_die, lr->function)) {
e92b85e1
MH
1396 lf->fname = dwarf_decl_file(sp_die);
1397 dwarf_decl_line(sp_die, &lr->offset);
804b3606 1398 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
631c9def 1399 lf->lno_s = lr->offset + lr->start;
d3b63d7a
MH
1400 if (lf->lno_s < 0) /* Overflow */
1401 lf->lno_s = INT_MAX;
1402 lf->lno_e = lr->offset + lr->end;
1403 if (lf->lno_e < 0) /* Overflow */
804b3606 1404 lf->lno_e = INT_MAX;
d3b63d7a 1405 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
631c9def
MH
1406 lr->start = lf->lno_s;
1407 lr->end = lf->lno_e;
b55a87ad
MH
1408 if (dwarf_func_inline(sp_die)) {
1409 struct dwarf_callback_param _param;
1410 _param.data = (void *)lf;
1411 _param.retval = 0;
161a26b0 1412 dwarf_func_inline_instances(sp_die,
b55a87ad
MH
1413 line_range_inline_cb,
1414 &_param);
1415 param->retval = _param.retval;
1416 } else
1417 param->retval = find_line_range_by_line(sp_die, lf);
1418 return DWARF_CB_ABORT;
631c9def 1419 }
b55a87ad 1420 return DWARF_CB_OK;
631c9def
MH
1421}
1422
b55a87ad 1423static int find_line_range_by_func(struct line_finder *lf)
631c9def 1424{
b55a87ad
MH
1425 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1426 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1427 return param.retval;
631c9def
MH
1428}
1429
1430int find_line_range(int fd, struct line_range *lr)
1431{
804b3606 1432 struct line_finder lf = {.lr = lr, .found = 0};
b55a87ad 1433 int ret = 0;
804b3606
MH
1434 Dwarf_Off off = 0, noff;
1435 size_t cuhl;
1436 Dwarf_Die *diep;
469b9b88
MH
1437 Dwarf *dbg = NULL;
1438 Dwfl *dwfl;
1439 Dwarf_Addr bias; /* Currently ignored */
6a330a3c 1440 const char *comp_dir;
804b3606 1441
469b9b88 1442 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
b55a87ad 1443 if (!dbg) {
0e43e5d2 1444 pr_warning("No debug information found in the vmlinux - "
b55a87ad 1445 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
f0c4801a 1446 close(fd); /* Without dwfl_end(), fd isn't closed. */
b55a87ad
MH
1447 return -EBADF;
1448 }
631c9def 1449
cd25f8bc
LM
1450 /* Fastpath: lookup by function name from .debug_pubnames section */
1451 if (lr->function) {
1452 struct pubname_callback_param pubname_param = {
1453 .function = lr->function, .file = lr->file,
1454 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1455 struct dwarf_callback_param line_range_param = {
1456 .data = (void *)&lf, .retval = 0};
1457
1458 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1459 if (pubname_param.found) {
1460 line_range_search_cb(&lf.sp_die, &line_range_param);
1461 if (lf.found)
1462 goto found;
1463 }
1464 }
1465
804b3606 1466 /* Loop on CUs (Compilation Unit) */
b55a87ad
MH
1467 while (!lf.found && ret >= 0) {
1468 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
631c9def
MH
1469 break;
1470
1471 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
1472 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1473 if (!diep)
1474 continue;
631c9def
MH
1475
1476 /* Check if target file is included. */
1477 if (lr->file)
2a9c8c36 1478 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
804b3606 1479 else
2a9c8c36 1480 lf.fname = 0;
631c9def 1481
2a9c8c36 1482 if (!lr->file || lf.fname) {
631c9def 1483 if (lr->function)
b55a87ad 1484 ret = find_line_range_by_func(&lf);
631c9def
MH
1485 else {
1486 lf.lno_s = lr->start;
d3b63d7a 1487 lf.lno_e = lr->end;
b55a87ad 1488 ret = find_line_range_by_line(NULL, &lf);
631c9def 1489 }
631c9def 1490 }
804b3606 1491 off = noff;
631c9def 1492 }
6a330a3c 1493
cd25f8bc 1494found:
6a330a3c
MH
1495 /* Store comp_dir */
1496 if (lf.found) {
1497 comp_dir = cu_get_comp_dir(&lf.cu_die);
1498 if (comp_dir) {
1499 lr->comp_dir = strdup(comp_dir);
1500 if (!lr->comp_dir)
1501 ret = -ENOMEM;
1502 }
1503 }
1504
7cf0b79e 1505 pr_debug("path: %s\n", lr->path);
469b9b88 1506 dwfl_end(dwfl);
b55a87ad 1507 return (ret < 0) ? ret : lf.found;
631c9def
MH
1508}
1509