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