blob: bfa6d9d215569abc370c44adc0cdfcd86f3ae0a8 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 <inttypes.h>
23#include <sys/utsname.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdarg.h>
33#include <dwarf-regs.h>
34
35#include <linux/bitops.h>
36#include "event.h"
37#include "dso.h"
38#include "debug.h"
39#include "intlist.h"
40#include "util.h"
41#include "strlist.h"
42#include "symbol.h"
43#include "probe-finder.h"
44#include "probe-file.h"
45#include "string2.h"
46
47/* Kprobe tracer basic type is up to u64 */
48#define MAX_BASIC_TYPE_BITS 64
49
50/* Dwarf FL wrappers */
51static char *debuginfo_path; /* Currently dummy */
52
53static const Dwfl_Callbacks offline_callbacks = {
54 .find_debuginfo = dwfl_standard_find_debuginfo,
55 .debuginfo_path = &debuginfo_path,
56
57 .section_address = dwfl_offline_section_address,
58
59 /* We use this table for core files too. */
60 .find_elf = dwfl_build_id_find_elf,
61};
62
63/* Get a Dwarf from offline image */
64static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
65 const char *path)
66{
67 int fd;
68
69 fd = open(path, O_RDONLY);
70 if (fd < 0)
71 return fd;
72
73 dbg->dwfl = dwfl_begin(&offline_callbacks);
74 if (!dbg->dwfl)
75 goto error;
76
77 dwfl_report_begin(dbg->dwfl);
78 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
79 if (!dbg->mod)
80 goto error;
81
82 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
83 if (!dbg->dbg)
84 goto error;
85
86 dwfl_report_end(dbg->dwfl, NULL, NULL);
87
88 return 0;
89error:
90 if (dbg->dwfl)
91 dwfl_end(dbg->dwfl);
92 else
93 close(fd);
94 memset(dbg, 0, sizeof(*dbg));
95
96 return -ENOENT;
97}
98
99static struct debuginfo *__debuginfo__new(const char *path)
100{
101 struct debuginfo *dbg = zalloc(sizeof(*dbg));
102 if (!dbg)
103 return NULL;
104
105 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
106 zfree(&dbg);
107 if (dbg)
108 pr_debug("Open Debuginfo file: %s\n", path);
109 return dbg;
110}
111
112enum dso_binary_type distro_dwarf_types[] = {
113 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
114 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
115 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
116 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
117 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
118 DSO_BINARY_TYPE__NOT_FOUND,
119};
120
121struct debuginfo *debuginfo__new(const char *path)
122{
123 enum dso_binary_type *type;
124 char buf[PATH_MAX], nil = '\0';
125 struct dso *dso;
126 struct debuginfo *dinfo = NULL;
127
128 /* Try to open distro debuginfo files */
129 dso = dso__new(path);
130 if (!dso)
131 goto out;
132
133 for (type = distro_dwarf_types;
134 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
135 type++) {
136 if (dso__read_binary_type_filename(dso, *type, &nil,
137 buf, PATH_MAX) < 0)
138 continue;
139 dinfo = __debuginfo__new(buf);
140 }
141 dso__put(dso);
142
143out:
144 /* if failed to open all distro debuginfo, open given binary */
145 return dinfo ? : __debuginfo__new(path);
146}
147
148void debuginfo__delete(struct debuginfo *dbg)
149{
150 if (dbg) {
151 if (dbg->dwfl)
152 dwfl_end(dbg->dwfl);
153 free(dbg);
154 }
155}
156
157/*
158 * Probe finder related functions
159 */
160
161static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
162{
163 struct probe_trace_arg_ref *ref;
164 ref = zalloc(sizeof(struct probe_trace_arg_ref));
165 if (ref != NULL)
166 ref->offset = offs;
167 return ref;
168}
169
170/*
171 * Convert a location into trace_arg.
172 * If tvar == NULL, this just checks variable can be converted.
173 * If fentry == true and vr_die is a parameter, do huristic search
174 * for the location fuzzed by function entry mcount.
175 */
176static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
177 Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
178 unsigned int machine,
179 struct probe_trace_arg *tvar)
180{
181 Dwarf_Attribute attr;
182 Dwarf_Addr tmp = 0;
183 Dwarf_Op *op;
184 size_t nops;
185 unsigned int regn;
186 Dwarf_Word offs = 0;
187 bool ref = false;
188 const char *regs;
189 int ret, ret2 = 0;
190
191 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
192 goto static_var;
193
194 /* TODO: handle more than 1 exprs */
195 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
196 return -EINVAL; /* Broken DIE ? */
197 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
198 ret = dwarf_entrypc(sp_die, &tmp);
199 if (ret)
200 return -ENOENT;
201
202 if (probe_conf.show_location_range &&
203 (dwarf_tag(vr_die) == DW_TAG_variable)) {
204 ret2 = -ERANGE;
205 } else if (addr != tmp ||
206 dwarf_tag(vr_die) != DW_TAG_formal_parameter) {
207 return -ENOENT;
208 }
209
210 ret = dwarf_highpc(sp_die, &tmp);
211 if (ret)
212 return -ENOENT;
213 /*
214 * This is fuzzed by fentry mcount. We try to find the
215 * parameter location at the earliest address.
216 */
217 for (addr += 1; addr <= tmp; addr++) {
218 if (dwarf_getlocation_addr(&attr, addr, &op,
219 &nops, 1) > 0)
220 goto found;
221 }
222 return -ENOENT;
223 }
224found:
225 if (nops == 0)
226 /* TODO: Support const_value */
227 return -ENOENT;
228
229 if (op->atom == DW_OP_addr) {
230static_var:
231 if (!tvar)
232 return ret2;
233 /* Static variables on memory (not stack), make @varname */
234 ret = strlen(dwarf_diename(vr_die));
235 tvar->value = zalloc(ret + 2);
236 if (tvar->value == NULL)
237 return -ENOMEM;
238 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
239 tvar->ref = alloc_trace_arg_ref((long)offs);
240 if (tvar->ref == NULL)
241 return -ENOMEM;
242 return ret2;
243 }
244
245 /* If this is based on frame buffer, set the offset */
246 if (op->atom == DW_OP_fbreg) {
247 if (fb_ops == NULL)
248 return -ENOTSUP;
249 ref = true;
250 offs = op->number;
251 op = &fb_ops[0];
252 }
253
254 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
255 regn = op->atom - DW_OP_breg0;
256 offs += op->number;
257 ref = true;
258 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
259 regn = op->atom - DW_OP_reg0;
260 } else if (op->atom == DW_OP_bregx) {
261 regn = op->number;
262 offs += op->number2;
263 ref = true;
264 } else if (op->atom == DW_OP_regx) {
265 regn = op->number;
266 } else {
267 pr_debug("DW_OP %x is not supported.\n", op->atom);
268 return -ENOTSUP;
269 }
270
271 if (!tvar)
272 return ret2;
273
274 regs = get_dwarf_regstr(regn, machine);
275 if (!regs) {
276 /* This should be a bug in DWARF or this tool */
277 pr_warning("Mapping for the register number %u "
278 "missing on this architecture.\n", regn);
279 return -ENOTSUP;
280 }
281
282 tvar->value = strdup(regs);
283 if (tvar->value == NULL)
284 return -ENOMEM;
285
286 if (ref) {
287 tvar->ref = alloc_trace_arg_ref((long)offs);
288 if (tvar->ref == NULL)
289 return -ENOMEM;
290 }
291 return ret2;
292}
293
294#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
295
296static int convert_variable_type(Dwarf_Die *vr_die,
297 struct probe_trace_arg *tvar,
298 const char *cast)
299{
300 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
301 Dwarf_Die type;
302 char buf[16];
303 char sbuf[STRERR_BUFSIZE];
304 int bsize, boffs, total;
305 int ret;
306 char prefix;
307
308 /* TODO: check all types */
309 if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "x") != 0 &&
310 strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) {
311 /* Non string type is OK */
312 /* and respect signedness/hexadecimal cast */
313 tvar->type = strdup(cast);
314 return (tvar->type == NULL) ? -ENOMEM : 0;
315 }
316
317 bsize = dwarf_bitsize(vr_die);
318 if (bsize > 0) {
319 /* This is a bitfield */
320 boffs = dwarf_bitoffset(vr_die);
321 total = dwarf_bytesize(vr_die);
322 if (boffs < 0 || total < 0)
323 return -ENOENT;
324 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
325 BYTES_TO_BITS(total));
326 goto formatted;
327 }
328
329 if (die_get_real_type(vr_die, &type) == NULL) {
330 pr_warning("Failed to get a type information of %s.\n",
331 dwarf_diename(vr_die));
332 return -ENOENT;
333 }
334
335 pr_debug("%s type is %s.\n",
336 dwarf_diename(vr_die), dwarf_diename(&type));
337
338 if (cast && strcmp(cast, "string") == 0) { /* String type */
339 ret = dwarf_tag(&type);
340 if (ret != DW_TAG_pointer_type &&
341 ret != DW_TAG_array_type) {
342 pr_warning("Failed to cast into string: "
343 "%s(%s) is not a pointer nor array.\n",
344 dwarf_diename(vr_die), dwarf_diename(&type));
345 return -EINVAL;
346 }
347 if (die_get_real_type(&type, &type) == NULL) {
348 pr_warning("Failed to get a type"
349 " information.\n");
350 return -ENOENT;
351 }
352 if (ret == DW_TAG_pointer_type) {
353 while (*ref_ptr)
354 ref_ptr = &(*ref_ptr)->next;
355 /* Add new reference with offset +0 */
356 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
357 if (*ref_ptr == NULL) {
358 pr_warning("Out of memory error\n");
359 return -ENOMEM;
360 }
361 }
362 if (!die_compare_name(&type, "char") &&
363 !die_compare_name(&type, "unsigned char")) {
364 pr_warning("Failed to cast into string: "
365 "%s is not (unsigned) char *.\n",
366 dwarf_diename(vr_die));
367 return -EINVAL;
368 }
369 tvar->type = strdup(cast);
370 return (tvar->type == NULL) ? -ENOMEM : 0;
371 }
372
373 if (cast && (strcmp(cast, "u") == 0))
374 prefix = 'u';
375 else if (cast && (strcmp(cast, "s") == 0))
376 prefix = 's';
377 else if (cast && (strcmp(cast, "x") == 0) &&
378 probe_type_is_available(PROBE_TYPE_X))
379 prefix = 'x';
380 else
381 prefix = die_is_signed_type(&type) ? 's' :
382 probe_type_is_available(PROBE_TYPE_X) ? 'x' : 'u';
383
384 ret = dwarf_bytesize(&type);
385 if (ret <= 0)
386 /* No size ... try to use default type */
387 return 0;
388 ret = BYTES_TO_BITS(ret);
389
390 /* Check the bitwidth */
391 if (ret > MAX_BASIC_TYPE_BITS) {
392 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
393 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
394 ret = MAX_BASIC_TYPE_BITS;
395 }
396 ret = snprintf(buf, 16, "%c%d", prefix, ret);
397
398formatted:
399 if (ret < 0 || ret >= 16) {
400 if (ret >= 16)
401 ret = -E2BIG;
402 pr_warning("Failed to convert variable type: %s\n",
403 str_error_r(-ret, sbuf, sizeof(sbuf)));
404 return ret;
405 }
406 tvar->type = strdup(buf);
407 if (tvar->type == NULL)
408 return -ENOMEM;
409 return 0;
410}
411
412static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
413 struct perf_probe_arg_field *field,
414 struct probe_trace_arg_ref **ref_ptr,
415 Dwarf_Die *die_mem)
416{
417 struct probe_trace_arg_ref *ref = *ref_ptr;
418 Dwarf_Die type;
419 Dwarf_Word offs;
420 int ret, tag;
421
422 pr_debug("converting %s in %s\n", field->name, varname);
423 if (die_get_real_type(vr_die, &type) == NULL) {
424 pr_warning("Failed to get the type of %s.\n", varname);
425 return -ENOENT;
426 }
427 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
428 tag = dwarf_tag(&type);
429
430 if (field->name[0] == '[' &&
431 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
432 if (field->next)
433 /* Save original type for next field */
434 memcpy(die_mem, &type, sizeof(*die_mem));
435 /* Get the type of this array */
436 if (die_get_real_type(&type, &type) == NULL) {
437 pr_warning("Failed to get the type of %s.\n", varname);
438 return -ENOENT;
439 }
440 pr_debug2("Array real type: (%x)\n",
441 (unsigned)dwarf_dieoffset(&type));
442 if (tag == DW_TAG_pointer_type) {
443 ref = zalloc(sizeof(struct probe_trace_arg_ref));
444 if (ref == NULL)
445 return -ENOMEM;
446 if (*ref_ptr)
447 (*ref_ptr)->next = ref;
448 else
449 *ref_ptr = ref;
450 }
451 ref->offset += dwarf_bytesize(&type) * field->index;
452 if (!field->next)
453 /* Save vr_die for converting types */
454 memcpy(die_mem, vr_die, sizeof(*die_mem));
455 goto next;
456 } else if (tag == DW_TAG_pointer_type) {
457 /* Check the pointer and dereference */
458 if (!field->ref) {
459 pr_err("Semantic error: %s must be referred by '->'\n",
460 field->name);
461 return -EINVAL;
462 }
463 /* Get the type pointed by this pointer */
464 if (die_get_real_type(&type, &type) == NULL) {
465 pr_warning("Failed to get the type of %s.\n", varname);
466 return -ENOENT;
467 }
468 /* Verify it is a data structure */
469 tag = dwarf_tag(&type);
470 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
471 pr_warning("%s is not a data structure nor a union.\n",
472 varname);
473 return -EINVAL;
474 }
475
476 ref = zalloc(sizeof(struct probe_trace_arg_ref));
477 if (ref == NULL)
478 return -ENOMEM;
479 if (*ref_ptr)
480 (*ref_ptr)->next = ref;
481 else
482 *ref_ptr = ref;
483 } else {
484 /* Verify it is a data structure */
485 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
486 pr_warning("%s is not a data structure nor a union.\n",
487 varname);
488 return -EINVAL;
489 }
490 if (field->name[0] == '[') {
491 pr_err("Semantic error: %s is not a pointer"
492 " nor array.\n", varname);
493 return -EINVAL;
494 }
495 /* While prcessing unnamed field, we don't care about this */
496 if (field->ref && dwarf_diename(vr_die)) {
497 pr_err("Semantic error: %s must be referred by '.'\n",
498 field->name);
499 return -EINVAL;
500 }
501 if (!ref) {
502 pr_warning("Structure on a register is not "
503 "supported yet.\n");
504 return -ENOTSUP;
505 }
506 }
507
508 if (die_find_member(&type, field->name, die_mem) == NULL) {
509 pr_warning("%s(type:%s) has no member %s.\n", varname,
510 dwarf_diename(&type), field->name);
511 return -EINVAL;
512 }
513
514 /* Get the offset of the field */
515 if (tag == DW_TAG_union_type) {
516 offs = 0;
517 } else {
518 ret = die_get_data_member_location(die_mem, &offs);
519 if (ret < 0) {
520 pr_warning("Failed to get the offset of %s.\n",
521 field->name);
522 return ret;
523 }
524 }
525 ref->offset += (long)offs;
526
527 /* If this member is unnamed, we need to reuse this field */
528 if (!dwarf_diename(die_mem))
529 return convert_variable_fields(die_mem, varname, field,
530 &ref, die_mem);
531
532next:
533 /* Converting next field */
534 if (field->next)
535 return convert_variable_fields(die_mem, field->name,
536 field->next, &ref, die_mem);
537 else
538 return 0;
539}
540
541/* Show a variables in kprobe event format */
542static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
543{
544 Dwarf_Die die_mem;
545 int ret;
546
547 pr_debug("Converting variable %s into trace event.\n",
548 dwarf_diename(vr_die));
549
550 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
551 &pf->sp_die, pf->machine, pf->tvar);
552 if (ret == -ENOENT || ret == -EINVAL) {
553 pr_err("Failed to find the location of the '%s' variable at this address.\n"
554 " Perhaps it has been optimized out.\n"
555 " Use -V with the --range option to show '%s' location range.\n",
556 pf->pvar->var, pf->pvar->var);
557 } else if (ret == -ENOTSUP)
558 pr_err("Sorry, we don't support this variable location yet.\n");
559 else if (ret == 0 && pf->pvar->field) {
560 ret = convert_variable_fields(vr_die, pf->pvar->var,
561 pf->pvar->field, &pf->tvar->ref,
562 &die_mem);
563 vr_die = &die_mem;
564 }
565 if (ret == 0)
566 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
567 /* *expr will be cached in libdw. Don't free it. */
568 return ret;
569}
570
571/* Find a variable in a scope DIE */
572static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
573{
574 Dwarf_Die vr_die;
575 char *buf, *ptr;
576 int ret = 0;
577
578 /* Copy raw parameters */
579 if (!is_c_varname(pf->pvar->var))
580 return copy_to_probe_trace_arg(pf->tvar, pf->pvar);
581
582 if (pf->pvar->name)
583 pf->tvar->name = strdup(pf->pvar->name);
584 else {
585 buf = synthesize_perf_probe_arg(pf->pvar);
586 if (!buf)
587 return -ENOMEM;
588 ptr = strchr(buf, ':'); /* Change type separator to _ */
589 if (ptr)
590 *ptr = '_';
591 pf->tvar->name = buf;
592 }
593 if (pf->tvar->name == NULL)
594 return -ENOMEM;
595
596 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
597 /* Search child die for local variables and parameters. */
598 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
599 /* Search again in global variables */
600 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var,
601 0, &vr_die)) {
602 pr_warning("Failed to find '%s' in this function.\n",
603 pf->pvar->var);
604 ret = -ENOENT;
605 }
606 }
607 if (ret >= 0)
608 ret = convert_variable(&vr_die, pf);
609
610 return ret;
611}
612
613/* Convert subprogram DIE to trace point */
614static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
615 Dwarf_Addr paddr, bool retprobe,
616 const char *function,
617 struct probe_trace_point *tp)
618{
619 Dwarf_Addr eaddr;
620 GElf_Sym sym;
621 const char *symbol;
622
623 /* Verify the address is correct */
624 if (!dwarf_haspc(sp_die, paddr)) {
625 pr_warning("Specified offset is out of %s\n",
626 dwarf_diename(sp_die));
627 return -EINVAL;
628 }
629
630 if (dwarf_entrypc(sp_die, &eaddr) == 0) {
631 /* If the DIE has entrypc, use it. */
632 symbol = dwarf_diename(sp_die);
633 } else {
634 /* Try to get actual symbol name and address from symtab */
635 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
636 eaddr = sym.st_value;
637 }
638 if (!symbol) {
639 pr_warning("Failed to find symbol at 0x%lx\n",
640 (unsigned long)paddr);
641 return -ENOENT;
642 }
643
644 tp->offset = (unsigned long)(paddr - eaddr);
645 tp->address = (unsigned long)paddr;
646 tp->symbol = strdup(symbol);
647 if (!tp->symbol)
648 return -ENOMEM;
649
650 /* Return probe must be on the head of a subprogram */
651 if (retprobe) {
652 if (eaddr != paddr) {
653 pr_warning("Failed to find \"%s%%return\",\n"
654 " because %s is an inlined function and"
655 " has no return point.\n", function,
656 function);
657 return -EINVAL;
658 }
659 tp->retprobe = true;
660 }
661
662 return 0;
663}
664
665/* Call probe_finder callback with scope DIE */
666static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
667{
668 Dwarf_Attribute fb_attr;
669 Dwarf_Frame *frame = NULL;
670 size_t nops;
671 int ret;
672
673 if (!sc_die) {
674 pr_err("Caller must pass a scope DIE. Program error.\n");
675 return -EINVAL;
676 }
677
678 /* If not a real subprogram, find a real one */
679 if (!die_is_func_def(sc_die)) {
680 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
681 if (die_find_tailfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
682 pr_warning("Ignoring tail call from %s\n",
683 dwarf_diename(&pf->sp_die));
684 return 0;
685 } else {
686 pr_warning("Failed to find probe point in any "
687 "functions.\n");
688 return -ENOENT;
689 }
690 }
691 } else
692 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
693
694 /* Get the frame base attribute/ops from subprogram */
695 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
696 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
697 if (ret <= 0 || nops == 0) {
698 pf->fb_ops = NULL;
699#if _ELFUTILS_PREREQ(0, 142)
700 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
701 (pf->cfi_eh != NULL || pf->cfi_dbg != NULL)) {
702 if ((dwarf_cfi_addrframe(pf->cfi_eh, pf->addr, &frame) != 0 &&
703 (dwarf_cfi_addrframe(pf->cfi_dbg, pf->addr, &frame) != 0)) ||
704 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
705 pr_warning("Failed to get call frame on 0x%jx\n",
706 (uintmax_t)pf->addr);
707 free(frame);
708 return -ENOENT;
709 }
710#endif
711 }
712
713 /* Call finder's callback handler */
714 ret = pf->callback(sc_die, pf);
715
716 /* Since *pf->fb_ops can be a part of frame. we should free it here. */
717 free(frame);
718 pf->fb_ops = NULL;
719
720 return ret;
721}
722
723struct find_scope_param {
724 const char *function;
725 const char *file;
726 int line;
727 int diff;
728 Dwarf_Die *die_mem;
729 bool found;
730};
731
732static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
733{
734 struct find_scope_param *fsp = data;
735 const char *file;
736 int lno;
737
738 /* Skip if declared file name does not match */
739 if (fsp->file) {
740 file = dwarf_decl_file(fn_die);
741 if (!file || strcmp(fsp->file, file) != 0)
742 return 0;
743 }
744 /* If the function name is given, that's what user expects */
745 if (fsp->function) {
746 if (die_match_name(fn_die, fsp->function)) {
747 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
748 fsp->found = true;
749 return 1;
750 }
751 } else {
752 /* With the line number, find the nearest declared DIE */
753 dwarf_decl_line(fn_die, &lno);
754 if (lno < fsp->line && fsp->diff > fsp->line - lno) {
755 /* Keep a candidate and continue */
756 fsp->diff = fsp->line - lno;
757 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
758 fsp->found = true;
759 }
760 }
761 return 0;
762}
763
764/* Return innermost DIE */
765static int find_inner_scope_cb(Dwarf_Die *fn_die, void *data)
766{
767 struct find_scope_param *fsp = data;
768
769 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
770 fsp->found = true;
771 return 1;
772}
773
774/* Find an appropriate scope fits to given conditions */
775static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
776{
777 struct find_scope_param fsp = {
778 .function = pf->pev->point.function,
779 .file = pf->fname,
780 .line = pf->lno,
781 .diff = INT_MAX,
782 .die_mem = die_mem,
783 .found = false,
784 };
785 int ret;
786
787 ret = cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb,
788 &fsp);
789 if (!ret && !fsp.found)
790 cu_walk_functions_at(&pf->cu_die, pf->addr,
791 find_inner_scope_cb, &fsp);
792
793 return fsp.found ? die_mem : NULL;
794}
795
796static int probe_point_line_walker(const char *fname, int lineno,
797 Dwarf_Addr addr, void *data)
798{
799 struct probe_finder *pf = data;
800 Dwarf_Die *sc_die, die_mem;
801 int ret;
802
803 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
804 return 0;
805
806 pf->addr = addr;
807 sc_die = find_best_scope(pf, &die_mem);
808 if (!sc_die) {
809 pr_warning("Failed to find scope of probe point.\n");
810 return -ENOENT;
811 }
812
813 ret = call_probe_finder(sc_die, pf);
814
815 /* Continue if no error, because the line will be in inline function */
816 return ret < 0 ? ret : 0;
817}
818
819/* Find probe point from its line number */
820static int find_probe_point_by_line(struct probe_finder *pf)
821{
822 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
823}
824
825/* Find lines which match lazy pattern */
826static int find_lazy_match_lines(struct intlist *list,
827 const char *fname, const char *pat)
828{
829 FILE *fp;
830 char *line = NULL;
831 size_t line_len;
832 ssize_t len;
833 int count = 0, linenum = 1;
834 char sbuf[STRERR_BUFSIZE];
835
836 fp = fopen(fname, "r");
837 if (!fp) {
838 pr_warning("Failed to open %s: %s\n", fname,
839 str_error_r(errno, sbuf, sizeof(sbuf)));
840 return -errno;
841 }
842
843 while ((len = getline(&line, &line_len, fp)) > 0) {
844
845 if (line[len - 1] == '\n')
846 line[len - 1] = '\0';
847
848 if (strlazymatch(line, pat)) {
849 intlist__add(list, linenum);
850 count++;
851 }
852 linenum++;
853 }
854
855 if (ferror(fp))
856 count = -errno;
857 free(line);
858 fclose(fp);
859
860 if (count == 0)
861 pr_debug("No matched lines found in %s.\n", fname);
862 return count;
863}
864
865static int probe_point_lazy_walker(const char *fname, int lineno,
866 Dwarf_Addr addr, void *data)
867{
868 struct probe_finder *pf = data;
869 Dwarf_Die *sc_die, die_mem;
870 int ret;
871
872 if (!intlist__has_entry(pf->lcache, lineno) ||
873 strtailcmp(fname, pf->fname) != 0)
874 return 0;
875
876 pr_debug("Probe line found: line:%d addr:0x%llx\n",
877 lineno, (unsigned long long)addr);
878 pf->addr = addr;
879 pf->lno = lineno;
880 sc_die = find_best_scope(pf, &die_mem);
881 if (!sc_die) {
882 pr_warning("Failed to find scope of probe point.\n");
883 return -ENOENT;
884 }
885
886 ret = call_probe_finder(sc_die, pf);
887
888 /*
889 * Continue if no error, because the lazy pattern will match
890 * to other lines
891 */
892 return ret < 0 ? ret : 0;
893}
894
895/* Find probe points from lazy pattern */
896static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
897{
898 int ret = 0;
899 char *fpath;
900
901 if (intlist__empty(pf->lcache)) {
902 const char *comp_dir;
903
904 comp_dir = cu_get_comp_dir(&pf->cu_die);
905 ret = get_real_path(pf->fname, comp_dir, &fpath);
906 if (ret < 0) {
907 pr_warning("Failed to find source file path.\n");
908 return ret;
909 }
910
911 /* Matching lazy line pattern */
912 ret = find_lazy_match_lines(pf->lcache, fpath,
913 pf->pev->point.lazy_line);
914 free(fpath);
915 if (ret <= 0)
916 return ret;
917 }
918
919 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
920}
921
922static void skip_prologue(Dwarf_Die *sp_die, struct probe_finder *pf)
923{
924 struct perf_probe_point *pp = &pf->pev->point;
925
926 /* Not uprobe? */
927 if (!pf->pev->uprobes)
928 return;
929
930 /* Compiled with optimization? */
931 if (die_is_optimized_target(&pf->cu_die))
932 return;
933
934 /* Don't know entrypc? */
935 if (!pf->addr)
936 return;
937
938 /* Only FUNC and FUNC@SRC are eligible. */
939 if (!pp->function || pp->line || pp->retprobe || pp->lazy_line ||
940 pp->offset || pp->abs_address)
941 return;
942
943 /* Not interested in func parameter? */
944 if (!perf_probe_with_var(pf->pev))
945 return;
946
947 pr_info("Target program is compiled without optimization. Skipping prologue.\n"
948 "Probe on address 0x%" PRIx64 " to force probing at the function entry.\n\n",
949 pf->addr);
950
951 die_skip_prologue(sp_die, &pf->cu_die, &pf->addr);
952}
953
954static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
955{
956 struct probe_finder *pf = data;
957 struct perf_probe_point *pp = &pf->pev->point;
958 Dwarf_Addr addr;
959 int ret;
960
961 if (pp->lazy_line)
962 ret = find_probe_point_lazy(in_die, pf);
963 else {
964 /* Get probe address */
965 if (die_entrypc(in_die, &addr) != 0) {
966 pr_warning("Failed to get entry address of %s.\n",
967 dwarf_diename(in_die));
968 return -ENOENT;
969 }
970 if (addr == 0) {
971 pr_debug("%s has no valid entry address. skipped.\n",
972 dwarf_diename(in_die));
973 return -ENOENT;
974 }
975 pf->addr = addr;
976 pf->addr += pp->offset;
977 pr_debug("found inline addr: 0x%jx\n",
978 (uintmax_t)pf->addr);
979
980 ret = call_probe_finder(in_die, pf);
981 }
982
983 return ret;
984}
985
986/* Callback parameter with return value for libdw */
987struct dwarf_callback_param {
988 void *data;
989 int retval;
990};
991
992/* Search function from function name */
993static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
994{
995 struct dwarf_callback_param *param = data;
996 struct probe_finder *pf = param->data;
997 struct perf_probe_point *pp = &pf->pev->point;
998
999 /* Check tag and diename */
1000 if (!die_is_func_def(sp_die) ||
1001 !die_match_name(sp_die, pp->function))
1002 return DWARF_CB_OK;
1003
1004 /* Check declared file */
1005 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1006 return DWARF_CB_OK;
1007
1008 pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die),
1009 (unsigned long)dwarf_dieoffset(sp_die));
1010 pf->fname = dwarf_decl_file(sp_die);
1011 if (pp->line) { /* Function relative line */
1012 dwarf_decl_line(sp_die, &pf->lno);
1013 pf->lno += pp->line;
1014 param->retval = find_probe_point_by_line(pf);
1015 } else if (die_is_func_instance(sp_die)) {
1016 /* Instances always have the entry address */
1017 die_entrypc(sp_die, &pf->addr);
1018 /* But in some case the entry address is 0 */
1019 if (pf->addr == 0) {
1020 pr_debug("%s has no entry PC. Skipped\n",
1021 dwarf_diename(sp_die));
1022 param->retval = 0;
1023 /* Real function */
1024 } else if (pp->lazy_line)
1025 param->retval = find_probe_point_lazy(sp_die, pf);
1026 else {
1027 skip_prologue(sp_die, pf);
1028 pf->addr += pp->offset;
1029 /* TODO: Check the address in this function */
1030 param->retval = call_probe_finder(sp_die, pf);
1031 }
1032 } else if (!probe_conf.no_inlines) {
1033 /* Inlined function: search instances */
1034 param->retval = die_walk_instances(sp_die,
1035 probe_point_inline_cb, (void *)pf);
1036 /* This could be a non-existed inline definition */
1037 if (param->retval == -ENOENT)
1038 param->retval = 0;
1039 }
1040
1041 /* We need to find other candidates */
1042 if (strisglob(pp->function) && param->retval >= 0) {
1043 param->retval = 0; /* We have to clear the result */
1044 return DWARF_CB_OK;
1045 }
1046
1047 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
1048}
1049
1050static int find_probe_point_by_func(struct probe_finder *pf)
1051{
1052 struct dwarf_callback_param _param = {.data = (void *)pf,
1053 .retval = 0};
1054 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1055 return _param.retval;
1056}
1057
1058struct pubname_callback_param {
1059 char *function;
1060 char *file;
1061 Dwarf_Die *cu_die;
1062 Dwarf_Die *sp_die;
1063 int found;
1064};
1065
1066static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1067{
1068 struct pubname_callback_param *param = data;
1069
1070 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1071 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1072 return DWARF_CB_OK;
1073
1074 if (die_match_name(param->sp_die, param->function)) {
1075 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1076 return DWARF_CB_OK;
1077
1078 if (param->file &&
1079 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1080 return DWARF_CB_OK;
1081
1082 param->found = 1;
1083 return DWARF_CB_ABORT;
1084 }
1085 }
1086
1087 return DWARF_CB_OK;
1088}
1089
1090static int debuginfo__find_probe_location(struct debuginfo *dbg,
1091 struct probe_finder *pf)
1092{
1093 struct perf_probe_point *pp = &pf->pev->point;
1094 Dwarf_Off off, noff;
1095 size_t cuhl;
1096 Dwarf_Die *diep;
1097 int ret = 0;
1098
1099 off = 0;
1100 pf->lcache = intlist__new(NULL);
1101 if (!pf->lcache)
1102 return -ENOMEM;
1103
1104 /* Fastpath: lookup by function name from .debug_pubnames section */
1105 if (pp->function && !strisglob(pp->function)) {
1106 struct pubname_callback_param pubname_param = {
1107 .function = pp->function,
1108 .file = pp->file,
1109 .cu_die = &pf->cu_die,
1110 .sp_die = &pf->sp_die,
1111 .found = 0,
1112 };
1113 struct dwarf_callback_param probe_param = {
1114 .data = pf,
1115 };
1116
1117 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1118 &pubname_param, 0);
1119 if (pubname_param.found) {
1120 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1121 if (ret)
1122 goto found;
1123 }
1124 }
1125
1126 /* Loop on CUs (Compilation Unit) */
1127 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
1128 /* Get the DIE(Debugging Information Entry) of this CU */
1129 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
1130 if (!diep)
1131 continue;
1132
1133 /* Check if target file is included. */
1134 if (pp->file)
1135 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1136 else
1137 pf->fname = NULL;
1138
1139 if (!pp->file || pf->fname) {
1140 if (pp->function)
1141 ret = find_probe_point_by_func(pf);
1142 else if (pp->lazy_line)
1143 ret = find_probe_point_lazy(&pf->cu_die, pf);
1144 else {
1145 pf->lno = pp->line;
1146 ret = find_probe_point_by_line(pf);
1147 }
1148 if (ret < 0)
1149 break;
1150 }
1151 off = noff;
1152 }
1153
1154found:
1155 intlist__delete(pf->lcache);
1156 pf->lcache = NULL;
1157
1158 return ret;
1159}
1160
1161/* Find probe points from debuginfo */
1162static int debuginfo__find_probes(struct debuginfo *dbg,
1163 struct probe_finder *pf)
1164{
1165 int ret = 0;
1166 Elf *elf;
1167 GElf_Ehdr ehdr;
1168
1169 if (pf->cfi_eh || pf->cfi_dbg)
1170 return debuginfo__find_probe_location(dbg, pf);
1171
1172 /* Get the call frame information from this dwarf */
1173 elf = dwarf_getelf(dbg->dbg);
1174 if (elf == NULL)
1175 return -EINVAL;
1176
1177 if (gelf_getehdr(elf, &ehdr) == NULL)
1178 return -EINVAL;
1179
1180 pf->machine = ehdr.e_machine;
1181
1182#if _ELFUTILS_PREREQ(0, 142)
1183 do {
1184 GElf_Shdr shdr;
1185
1186 if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) &&
1187 shdr.sh_type == SHT_PROGBITS)
1188 pf->cfi_eh = dwarf_getcfi_elf(elf);
1189
1190 pf->cfi_dbg = dwarf_getcfi(dbg->dbg);
1191 } while (0);
1192#endif
1193
1194 ret = debuginfo__find_probe_location(dbg, pf);
1195 return ret;
1196}
1197
1198struct local_vars_finder {
1199 struct probe_finder *pf;
1200 struct perf_probe_arg *args;
1201 bool vars;
1202 int max_args;
1203 int nargs;
1204 int ret;
1205};
1206
1207/* Collect available variables in this scope */
1208static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
1209{
1210 struct local_vars_finder *vf = data;
1211 struct probe_finder *pf = vf->pf;
1212 int tag;
1213
1214 tag = dwarf_tag(die_mem);
1215 if (tag == DW_TAG_formal_parameter ||
1216 (tag == DW_TAG_variable && vf->vars)) {
1217 if (convert_variable_location(die_mem, vf->pf->addr,
1218 vf->pf->fb_ops, &pf->sp_die,
1219 pf->machine, NULL) == 0) {
1220 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
1221 if (vf->args[vf->nargs].var == NULL) {
1222 vf->ret = -ENOMEM;
1223 return DIE_FIND_CB_END;
1224 }
1225 pr_debug(" %s", vf->args[vf->nargs].var);
1226 vf->nargs++;
1227 }
1228 }
1229
1230 if (dwarf_haspc(die_mem, vf->pf->addr))
1231 return DIE_FIND_CB_CONTINUE;
1232 else
1233 return DIE_FIND_CB_SIBLING;
1234}
1235
1236static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
1237 struct perf_probe_arg *args)
1238{
1239 Dwarf_Die die_mem;
1240 int i;
1241 int n = 0;
1242 struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false,
1243 .max_args = MAX_PROBE_ARGS, .ret = 0};
1244
1245 for (i = 0; i < pf->pev->nargs; i++) {
1246 /* var never be NULL */
1247 if (strcmp(pf->pev->args[i].var, PROBE_ARG_VARS) == 0)
1248 vf.vars = true;
1249 else if (strcmp(pf->pev->args[i].var, PROBE_ARG_PARAMS) != 0) {
1250 /* Copy normal argument */
1251 args[n] = pf->pev->args[i];
1252 n++;
1253 continue;
1254 }
1255 pr_debug("Expanding %s into:", pf->pev->args[i].var);
1256 vf.nargs = n;
1257 /* Special local variables */
1258 die_find_child(sc_die, copy_variables_cb, (void *)&vf,
1259 &die_mem);
1260 pr_debug(" (%d)\n", vf.nargs - n);
1261 if (vf.ret < 0)
1262 return vf.ret;
1263 n = vf.nargs;
1264 }
1265 return n;
1266}
1267
1268/* Add a found probe point into trace event list */
1269static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
1270{
1271 struct trace_event_finder *tf =
1272 container_of(pf, struct trace_event_finder, pf);
1273 struct perf_probe_point *pp = &pf->pev->point;
1274 struct probe_trace_event *tev;
1275 struct perf_probe_arg *args = NULL;
1276 int ret, i;
1277
1278 /* Check number of tevs */
1279 if (tf->ntevs == tf->max_tevs) {
1280 pr_warning("Too many( > %d) probe point found.\n",
1281 tf->max_tevs);
1282 return -ERANGE;
1283 }
1284 tev = &tf->tevs[tf->ntevs++];
1285
1286 /* Trace point should be converted from subprogram DIE */
1287 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
1288 pp->retprobe, pp->function, &tev->point);
1289 if (ret < 0)
1290 goto end;
1291
1292 tev->point.realname = strdup(dwarf_diename(sc_die));
1293 if (!tev->point.realname) {
1294 ret = -ENOMEM;
1295 goto end;
1296 }
1297
1298 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1299 tev->point.offset);
1300
1301 /* Expand special probe argument if exist */
1302 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
1303 if (args == NULL) {
1304 ret = -ENOMEM;
1305 goto end;
1306 }
1307
1308 ret = expand_probe_args(sc_die, pf, args);
1309 if (ret < 0)
1310 goto end;
1311
1312 tev->nargs = ret;
1313 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1314 if (tev->args == NULL) {
1315 ret = -ENOMEM;
1316 goto end;
1317 }
1318
1319 /* Find each argument */
1320 for (i = 0; i < tev->nargs; i++) {
1321 pf->pvar = &args[i];
1322 pf->tvar = &tev->args[i];
1323 /* Variable should be found from scope DIE */
1324 ret = find_variable(sc_die, pf);
1325 if (ret != 0)
1326 break;
1327 }
1328
1329end:
1330 if (ret) {
1331 clear_probe_trace_event(tev);
1332 tf->ntevs--;
1333 }
1334 free(args);
1335 return ret;
1336}
1337
1338/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1339int debuginfo__find_trace_events(struct debuginfo *dbg,
1340 struct perf_probe_event *pev,
1341 struct probe_trace_event **tevs)
1342{
1343 struct trace_event_finder tf = {
1344 .pf = {.pev = pev, .callback = add_probe_trace_event},
1345 .max_tevs = probe_conf.max_probes, .mod = dbg->mod};
1346 int ret, i;
1347
1348 /* Allocate result tevs array */
1349 *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
1350 if (*tevs == NULL)
1351 return -ENOMEM;
1352
1353 tf.tevs = *tevs;
1354 tf.ntevs = 0;
1355
1356 ret = debuginfo__find_probes(dbg, &tf.pf);
1357 if (ret < 0 || tf.ntevs == 0) {
1358 for (i = 0; i < tf.ntevs; i++)
1359 clear_probe_trace_event(&tf.tevs[i]);
1360 zfree(tevs);
1361 return ret;
1362 }
1363
1364 return (ret < 0) ? ret : tf.ntevs;
1365}
1366
1367/* Collect available variables in this scope */
1368static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1369{
1370 struct available_var_finder *af = data;
1371 struct variable_list *vl;
1372 struct strbuf buf = STRBUF_INIT;
1373 int tag, ret;
1374
1375 vl = &af->vls[af->nvls - 1];
1376
1377 tag = dwarf_tag(die_mem);
1378 if (tag == DW_TAG_formal_parameter ||
1379 tag == DW_TAG_variable) {
1380 ret = convert_variable_location(die_mem, af->pf.addr,
1381 af->pf.fb_ops, &af->pf.sp_die,
1382 af->pf.machine, NULL);
1383 if (ret == 0 || ret == -ERANGE) {
1384 int ret2;
1385 bool externs = !af->child;
1386
1387 if (strbuf_init(&buf, 64) < 0)
1388 goto error;
1389
1390 if (probe_conf.show_location_range) {
1391 if (!externs)
1392 ret2 = strbuf_add(&buf,
1393 ret ? "[INV]\t" : "[VAL]\t", 6);
1394 else
1395 ret2 = strbuf_add(&buf, "[EXT]\t", 6);
1396 if (ret2)
1397 goto error;
1398 }
1399
1400 ret2 = die_get_varname(die_mem, &buf);
1401
1402 if (!ret2 && probe_conf.show_location_range &&
1403 !externs) {
1404 if (strbuf_addch(&buf, '\t') < 0)
1405 goto error;
1406 ret2 = die_get_var_range(&af->pf.sp_die,
1407 die_mem, &buf);
1408 }
1409
1410 pr_debug("Add new var: %s\n", buf.buf);
1411 if (ret2 == 0) {
1412 strlist__add(vl->vars,
1413 strbuf_detach(&buf, NULL));
1414 }
1415 strbuf_release(&buf);
1416 }
1417 }
1418
1419 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1420 return DIE_FIND_CB_CONTINUE;
1421 else
1422 return DIE_FIND_CB_SIBLING;
1423error:
1424 strbuf_release(&buf);
1425 pr_debug("Error in strbuf\n");
1426 return DIE_FIND_CB_END;
1427}
1428
1429static bool available_var_finder_overlap(struct available_var_finder *af)
1430{
1431 int i;
1432
1433 for (i = 0; i < af->nvls; i++) {
1434 if (af->pf.addr == af->vls[i].point.address)
1435 return true;
1436 }
1437 return false;
1438
1439}
1440
1441/* Add a found vars into available variables list */
1442static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
1443{
1444 struct available_var_finder *af =
1445 container_of(pf, struct available_var_finder, pf);
1446 struct perf_probe_point *pp = &pf->pev->point;
1447 struct variable_list *vl;
1448 Dwarf_Die die_mem;
1449 int ret;
1450
1451 /*
1452 * For some reason (e.g. different column assigned to same address),
1453 * this callback can be called with the address which already passed.
1454 * Ignore it first.
1455 */
1456 if (available_var_finder_overlap(af))
1457 return 0;
1458
1459 /* Check number of tevs */
1460 if (af->nvls == af->max_vls) {
1461 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1462 return -ERANGE;
1463 }
1464 vl = &af->vls[af->nvls++];
1465
1466 /* Trace point should be converted from subprogram DIE */
1467 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
1468 pp->retprobe, pp->function, &vl->point);
1469 if (ret < 0)
1470 return ret;
1471
1472 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1473 vl->point.offset);
1474
1475 /* Find local variables */
1476 vl->vars = strlist__new(NULL, NULL);
1477 if (vl->vars == NULL)
1478 return -ENOMEM;
1479 af->child = true;
1480 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
1481
1482 /* Find external variables */
1483 if (!probe_conf.show_ext_vars)
1484 goto out;
1485 /* Don't need to search child DIE for external vars. */
1486 af->child = false;
1487 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
1488
1489out:
1490 if (strlist__empty(vl->vars)) {
1491 strlist__delete(vl->vars);
1492 vl->vars = NULL;
1493 }
1494
1495 return ret;
1496}
1497
1498/*
1499 * Find available variables at given probe point
1500 * Return the number of found probe points. Return 0 if there is no
1501 * matched probe point. Return <0 if an error occurs.
1502 */
1503int debuginfo__find_available_vars_at(struct debuginfo *dbg,
1504 struct perf_probe_event *pev,
1505 struct variable_list **vls)
1506{
1507 struct available_var_finder af = {
1508 .pf = {.pev = pev, .callback = add_available_vars},
1509 .mod = dbg->mod,
1510 .max_vls = probe_conf.max_probes};
1511 int ret;
1512
1513 /* Allocate result vls array */
1514 *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
1515 if (*vls == NULL)
1516 return -ENOMEM;
1517
1518 af.vls = *vls;
1519 af.nvls = 0;
1520
1521 ret = debuginfo__find_probes(dbg, &af.pf);
1522 if (ret < 0) {
1523 /* Free vlist for error */
1524 while (af.nvls--) {
1525 zfree(&af.vls[af.nvls].point.symbol);
1526 strlist__delete(af.vls[af.nvls].vars);
1527 }
1528 zfree(vls);
1529 return ret;
1530 }
1531
1532 return (ret < 0) ? ret : af.nvls;
1533}
1534
1535/* For the kernel module, we need a special code to get a DIE */
1536int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
1537 bool adjust_offset)
1538{
1539 int n, i;
1540 Elf32_Word shndx;
1541 Elf_Scn *scn;
1542 Elf *elf;
1543 GElf_Shdr mem, *shdr;
1544 const char *p;
1545
1546 elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
1547 if (!elf)
1548 return -EINVAL;
1549
1550 /* Get the number of relocations */
1551 n = dwfl_module_relocations(dbg->mod);
1552 if (n < 0)
1553 return -ENOENT;
1554 /* Search the relocation related .text section */
1555 for (i = 0; i < n; i++) {
1556 p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
1557 if (strcmp(p, ".text") == 0) {
1558 /* OK, get the section header */
1559 scn = elf_getscn(elf, shndx);
1560 if (!scn)
1561 return -ENOENT;
1562 shdr = gelf_getshdr(scn, &mem);
1563 if (!shdr)
1564 return -ENOENT;
1565 *offs = shdr->sh_addr;
1566 if (adjust_offset)
1567 *offs -= shdr->sh_offset;
1568 }
1569 }
1570 return 0;
1571}
1572
1573/* Reverse search */
1574int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
1575 struct perf_probe_point *ppt)
1576{
1577 Dwarf_Die cudie, spdie, indie;
1578 Dwarf_Addr _addr = 0, baseaddr = 0;
1579 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
1580 int baseline = 0, lineno = 0, ret = 0;
1581
1582 /* We always need to relocate the address for aranges */
1583 if (debuginfo__get_text_offset(dbg, &baseaddr, false) == 0)
1584 addr += baseaddr;
1585 /* Find cu die */
1586 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
1587 pr_warning("Failed to find debug information for address %lx\n",
1588 addr);
1589 ret = -EINVAL;
1590 goto end;
1591 }
1592
1593 /* Find a corresponding line (filename and lineno) */
1594 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1595 /* Don't care whether it failed or not */
1596
1597 /* Find a corresponding function (name, baseline and baseaddr) */
1598 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
1599 /* Get function entry information */
1600 func = basefunc = dwarf_diename(&spdie);
1601 if (!func ||
1602 die_entrypc(&spdie, &baseaddr) != 0 ||
1603 dwarf_decl_line(&spdie, &baseline) != 0) {
1604 lineno = 0;
1605 goto post;
1606 }
1607
1608 fname = dwarf_decl_file(&spdie);
1609 if (addr == (unsigned long)baseaddr) {
1610 /* Function entry - Relative line number is 0 */
1611 lineno = baseline;
1612 goto post;
1613 }
1614
1615 /* Track down the inline functions step by step */
1616 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
1617 &indie)) {
1618 /* There is an inline function */
1619 if (die_entrypc(&indie, &_addr) == 0 &&
1620 _addr == addr) {
1621 /*
1622 * addr is at an inline function entry.
1623 * In this case, lineno should be the call-site
1624 * line number. (overwrite lineinfo)
1625 */
1626 lineno = die_get_call_lineno(&indie);
1627 fname = die_get_call_file(&indie);
1628 break;
1629 } else {
1630 /*
1631 * addr is in an inline function body.
1632 * Since lineno points one of the lines
1633 * of the inline function, baseline should
1634 * be the entry line of the inline function.
1635 */
1636 tmp = dwarf_diename(&indie);
1637 if (!tmp ||
1638 dwarf_decl_line(&indie, &baseline) != 0)
1639 break;
1640 func = tmp;
1641 spdie = indie;
1642 }
1643 }
1644 /* Verify the lineno and baseline are in a same file */
1645 tmp = dwarf_decl_file(&spdie);
1646 if (!tmp || strcmp(tmp, fname) != 0)
1647 lineno = 0;
1648 }
1649
1650post:
1651 /* Make a relative line number or an offset */
1652 if (lineno)
1653 ppt->line = lineno - baseline;
1654 else if (basefunc) {
1655 ppt->offset = addr - (unsigned long)baseaddr;
1656 func = basefunc;
1657 }
1658
1659 /* Duplicate strings */
1660 if (func) {
1661 ppt->function = strdup(func);
1662 if (ppt->function == NULL) {
1663 ret = -ENOMEM;
1664 goto end;
1665 }
1666 }
1667 if (fname) {
1668 ppt->file = strdup(fname);
1669 if (ppt->file == NULL) {
1670 zfree(&ppt->function);
1671 ret = -ENOMEM;
1672 goto end;
1673 }
1674 }
1675end:
1676 if (ret == 0 && (fname || func))
1677 ret = 1; /* Found a point */
1678 return ret;
1679}
1680
1681/* Add a line and store the src path */
1682static int line_range_add_line(const char *src, unsigned int lineno,
1683 struct line_range *lr)
1684{
1685 /* Copy source path */
1686 if (!lr->path) {
1687 lr->path = strdup(src);
1688 if (lr->path == NULL)
1689 return -ENOMEM;
1690 }
1691 return intlist__add(lr->line_list, lineno);
1692}
1693
1694static int line_range_walk_cb(const char *fname, int lineno,
1695 Dwarf_Addr addr __maybe_unused,
1696 void *data)
1697{
1698 struct line_finder *lf = data;
1699 int err;
1700
1701 if ((strtailcmp(fname, lf->fname) != 0) ||
1702 (lf->lno_s > lineno || lf->lno_e < lineno))
1703 return 0;
1704
1705 err = line_range_add_line(fname, lineno, lf->lr);
1706 if (err < 0 && err != -EEXIST)
1707 return err;
1708
1709 return 0;
1710}
1711
1712/* Find line range from its line number */
1713static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1714{
1715 int ret;
1716
1717 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
1718
1719 /* Update status */
1720 if (ret >= 0)
1721 if (!intlist__empty(lf->lr->line_list))
1722 ret = lf->found = 1;
1723 else
1724 ret = 0; /* Lines are not found */
1725 else {
1726 zfree(&lf->lr->path);
1727 }
1728 return ret;
1729}
1730
1731static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1732{
1733 int ret = find_line_range_by_line(in_die, data);
1734
1735 /*
1736 * We have to check all instances of inlined function, because
1737 * some execution paths can be optimized out depends on the
1738 * function argument of instances. However, if an error occurs,
1739 * it should be handled by the caller.
1740 */
1741 return ret < 0 ? ret : 0;
1742}
1743
1744/* Search function definition from function name */
1745static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1746{
1747 struct dwarf_callback_param *param = data;
1748 struct line_finder *lf = param->data;
1749 struct line_range *lr = lf->lr;
1750
1751 /* Check declared file */
1752 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1753 return DWARF_CB_OK;
1754
1755 if (die_is_func_def(sp_die) &&
1756 die_match_name(sp_die, lr->function)) {
1757 lf->fname = dwarf_decl_file(sp_die);
1758 dwarf_decl_line(sp_die, &lr->offset);
1759 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1760 lf->lno_s = lr->offset + lr->start;
1761 if (lf->lno_s < 0) /* Overflow */
1762 lf->lno_s = INT_MAX;
1763 lf->lno_e = lr->offset + lr->end;
1764 if (lf->lno_e < 0) /* Overflow */
1765 lf->lno_e = INT_MAX;
1766 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1767 lr->start = lf->lno_s;
1768 lr->end = lf->lno_e;
1769 if (!die_is_func_instance(sp_die))
1770 param->retval = die_walk_instances(sp_die,
1771 line_range_inline_cb, lf);
1772 else
1773 param->retval = find_line_range_by_line(sp_die, lf);
1774 return DWARF_CB_ABORT;
1775 }
1776 return DWARF_CB_OK;
1777}
1778
1779static int find_line_range_by_func(struct line_finder *lf)
1780{
1781 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1782 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1783 return param.retval;
1784}
1785
1786int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
1787{
1788 struct line_finder lf = {.lr = lr, .found = 0};
1789 int ret = 0;
1790 Dwarf_Off off = 0, noff;
1791 size_t cuhl;
1792 Dwarf_Die *diep;
1793 const char *comp_dir;
1794
1795 /* Fastpath: lookup by function name from .debug_pubnames section */
1796 if (lr->function) {
1797 struct pubname_callback_param pubname_param = {
1798 .function = lr->function, .file = lr->file,
1799 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1800 struct dwarf_callback_param line_range_param = {
1801 .data = (void *)&lf, .retval = 0};
1802
1803 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1804 &pubname_param, 0);
1805 if (pubname_param.found) {
1806 line_range_search_cb(&lf.sp_die, &line_range_param);
1807 if (lf.found)
1808 goto found;
1809 }
1810 }
1811
1812 /* Loop on CUs (Compilation Unit) */
1813 while (!lf.found && ret >= 0) {
1814 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
1815 NULL, NULL, NULL) != 0)
1816 break;
1817
1818 /* Get the DIE(Debugging Information Entry) of this CU */
1819 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
1820 if (!diep)
1821 continue;
1822
1823 /* Check if target file is included. */
1824 if (lr->file)
1825 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1826 else
1827 lf.fname = 0;
1828
1829 if (!lr->file || lf.fname) {
1830 if (lr->function)
1831 ret = find_line_range_by_func(&lf);
1832 else {
1833 lf.lno_s = lr->start;
1834 lf.lno_e = lr->end;
1835 ret = find_line_range_by_line(NULL, &lf);
1836 }
1837 }
1838 off = noff;
1839 }
1840
1841found:
1842 /* Store comp_dir */
1843 if (lf.found) {
1844 comp_dir = cu_get_comp_dir(&lf.cu_die);
1845 if (comp_dir) {
1846 lr->comp_dir = strdup(comp_dir);
1847 if (!lr->comp_dir)
1848 ret = -ENOMEM;
1849 }
1850 }
1851
1852 pr_debug("path: %s\n", lr->path);
1853 return (ret < 0) ? ret : lf.found;
1854}
1855
1856/*
1857 * Find a src file from a DWARF tag path. Prepend optional source path prefix
1858 * and chop off leading directories that do not exist. Result is passed back as
1859 * a newly allocated path on success.
1860 * Return 0 if file was found and readable, -errno otherwise.
1861 */
1862int get_real_path(const char *raw_path, const char *comp_dir,
1863 char **new_path)
1864{
1865 const char *prefix = symbol_conf.source_prefix;
1866
1867 if (!prefix) {
1868 if (raw_path[0] != '/' && comp_dir)
1869 /* If not an absolute path, try to use comp_dir */
1870 prefix = comp_dir;
1871 else {
1872 if (access(raw_path, R_OK) == 0) {
1873 *new_path = strdup(raw_path);
1874 return *new_path ? 0 : -ENOMEM;
1875 } else
1876 return -errno;
1877 }
1878 }
1879
1880 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
1881 if (!*new_path)
1882 return -ENOMEM;
1883
1884 for (;;) {
1885 sprintf(*new_path, "%s/%s", prefix, raw_path);
1886
1887 if (access(*new_path, R_OK) == 0)
1888 return 0;
1889
1890 if (!symbol_conf.source_prefix) {
1891 /* In case of searching comp_dir, don't retry */
1892 zfree(new_path);
1893 return -errno;
1894 }
1895
1896 switch (errno) {
1897 case ENAMETOOLONG:
1898 case ENOENT:
1899 case EROFS:
1900 case EFAULT:
1901 raw_path = strchr(++raw_path, '/');
1902 if (!raw_path) {
1903 zfree(new_path);
1904 return -ENOENT;
1905 }
1906 continue;
1907
1908 default:
1909 zfree(new_path);
1910 return -errno;
1911 }
1912 }
1913}