perf probe: Support comp_dir to find an absolute source path
[deliverable/linux.git] / tools / perf / util / probe-finder.c
CommitLineData
4ea42b18
MH
1/*
2 * probe-finder.c : C expression to kprobe event converter
3 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <getopt.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdarg.h>
33#include <ctype.h>
cd932c59 34#include <dwarf-regs.h>
074fc0e4 35
2a9c8c36 36#include "string.h"
89c69c0e
MH
37#include "event.h"
38#include "debug.h"
074fc0e4 39#include "util.h"
9ed7e1b8 40#include "symbol.h"
4ea42b18
MH
41#include "probe-finder.h"
42
4984912e
MH
43/* Kprobe tracer basic type is up to u64 */
44#define MAX_BASIC_TYPE_BITS 64
45
4ea42b18
MH
46/*
47 * Compare the tail of two strings.
48 * Return 0 if whole of either string is same as another's tail part.
49 */
50static int strtailcmp(const char *s1, const char *s2)
51{
52 int i1 = strlen(s1);
53 int i2 = strlen(s2);
d56728b8 54 while (--i1 >= 0 && --i2 >= 0) {
4ea42b18
MH
55 if (s1[i1] != s2[i2])
56 return s1[i1] - s2[i2];
57 }
58 return 0;
59}
60
2a9c8c36
MH
61/* Line number list operations */
62
63/* Add a line to line number list */
d3b63d7a 64static int line_list__add_line(struct list_head *head, int line)
2a9c8c36
MH
65{
66 struct line_node *ln;
67 struct list_head *p;
68
69 /* Reverse search, because new line will be the last one */
70 list_for_each_entry_reverse(ln, head, list) {
71 if (ln->line < line) {
72 p = &ln->list;
73 goto found;
74 } else if (ln->line == line) /* Already exist */
e334016f 75 return 1;
2a9c8c36
MH
76 }
77 /* List is empty, or the smallest entry */
78 p = head;
79found:
80 pr_debug("line list: add a line %u\n", line);
e334016f
MH
81 ln = zalloc(sizeof(struct line_node));
82 if (ln == NULL)
83 return -ENOMEM;
2a9c8c36
MH
84 ln->line = line;
85 INIT_LIST_HEAD(&ln->list);
86 list_add(&ln->list, p);
e334016f 87 return 0;
2a9c8c36
MH
88}
89
90/* Check if the line in line number list */
d3b63d7a 91static int line_list__has_line(struct list_head *head, int line)
2a9c8c36
MH
92{
93 struct line_node *ln;
94
95 /* Reverse search, because new line will be the last one */
96 list_for_each_entry(ln, head, list)
97 if (ln->line == line)
98 return 1;
99
100 return 0;
101}
102
103/* Init line number list */
104static void line_list__init(struct list_head *head)
105{
106 INIT_LIST_HEAD(head);
107}
108
109/* Free line number list */
110static void line_list__free(struct list_head *head)
111{
112 struct line_node *ln;
113 while (!list_empty(head)) {
114 ln = list_first_entry(head, struct line_node, list);
115 list_del(&ln->list);
116 free(ln);
117 }
118}
119
120/* Dwarf wrappers */
121
122/* Find the realpath of the target file. */
123static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
4ea42b18 124{
804b3606
MH
125 Dwarf_Files *files;
126 size_t nfiles, i;
accd3cc4 127 const char *src = NULL;
4ea42b18
MH
128 int ret;
129
130 if (!fname)
2a9c8c36 131 return NULL;
4ea42b18 132
804b3606 133 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
2a9c8c36
MH
134 if (ret != 0)
135 return NULL;
136
137 for (i = 0; i < nfiles; i++) {
138 src = dwarf_filesrc(files, i, NULL, NULL);
139 if (strtailcmp(src, fname) == 0)
140 break;
4ea42b18 141 }
c9e38582
MH
142 if (i == nfiles)
143 return NULL;
2a9c8c36 144 return src;
4ea42b18
MH
145}
146
6a330a3c
MH
147/* Get DW_AT_comp_dir (should be NULL with older gcc) */
148static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
149{
150 Dwarf_Attribute attr;
151 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
152 return NULL;
153 return dwarf_formstring(&attr);
154}
155
016f262e
MH
156/* Compare diename and tname */
157static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
158{
159 const char *name;
160 name = dwarf_diename(dw_die);
b55a87ad 161 return name ? strcmp(tname, name) : -1;
016f262e
MH
162}
163
7df2f329
MH
164/* Get type die, but skip qualifiers and typedef */
165static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
166{
167 Dwarf_Attribute attr;
168 int tag;
169
170 do {
171 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
172 dwarf_formref_die(&attr, die_mem) == NULL)
173 return NULL;
174
175 tag = dwarf_tag(die_mem);
176 vr_die = die_mem;
177 } while (tag == DW_TAG_const_type ||
178 tag == DW_TAG_restrict_type ||
179 tag == DW_TAG_volatile_type ||
180 tag == DW_TAG_shared_type ||
181 tag == DW_TAG_typedef);
182
183 return die_mem;
184}
185
4984912e
MH
186static bool die_is_signed_type(Dwarf_Die *tp_die)
187{
188 Dwarf_Attribute attr;
189 Dwarf_Word ret;
190
191 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
192 dwarf_formudata(&attr, &ret) != 0)
193 return false;
194
195 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
196 ret == DW_ATE_signed_fixed);
197}
198
199static int die_get_byte_size(Dwarf_Die *tp_die)
200{
201 Dwarf_Attribute attr;
202 Dwarf_Word ret;
203
204 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
205 dwarf_formudata(&attr, &ret) != 0)
206 return 0;
207
208 return (int)ret;
209}
210
de1439d8
MH
211/* Get data_member_location offset */
212static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
213{
214 Dwarf_Attribute attr;
215 Dwarf_Op *expr;
216 size_t nexpr;
217 int ret;
218
219 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
220 return -ENOENT;
221
222 if (dwarf_formudata(&attr, offs) != 0) {
223 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
224 ret = dwarf_getlocation(&attr, &expr, &nexpr);
225 if (ret < 0 || nexpr == 0)
226 return -ENOENT;
227
228 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
229 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
230 expr[0].atom, nexpr);
231 return -ENOTSUP;
232 }
233 *offs = (Dwarf_Word)expr[0].number;
234 }
235 return 0;
236}
237
016f262e
MH
238/* Return values for die_find callbacks */
239enum {
240 DIE_FIND_CB_FOUND = 0, /* End of Search */
241 DIE_FIND_CB_CHILD = 1, /* Search only children */
242 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
243 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
244};
245
246/* Search a child die */
247static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
248 int (*callback)(Dwarf_Die *, void *),
249 void *data, Dwarf_Die *die_mem)
250{
251 Dwarf_Die child_die;
252 int ret;
253
254 ret = dwarf_child(rt_die, die_mem);
255 if (ret != 0)
256 return NULL;
257
258 do {
259 ret = callback(die_mem, data);
260 if (ret == DIE_FIND_CB_FOUND)
261 return die_mem;
262
263 if ((ret & DIE_FIND_CB_CHILD) &&
264 die_find_child(die_mem, callback, data, &child_die)) {
265 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
266 return die_mem;
267 }
268 } while ((ret & DIE_FIND_CB_SIBLING) &&
269 dwarf_siblingof(die_mem, die_mem) == 0);
270
271 return NULL;
272}
273
804b3606
MH
274struct __addr_die_search_param {
275 Dwarf_Addr addr;
276 Dwarf_Die *die_mem;
277};
278
279static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
631c9def 280{
804b3606 281 struct __addr_die_search_param *ad = data;
631c9def 282
804b3606
MH
283 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
284 dwarf_haspc(fn_die, ad->addr)) {
285 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
286 return DWARF_CB_ABORT;
287 }
288 return DWARF_CB_OK;
289}
631c9def 290
804b3606 291/* Search a real subprogram including this line, */
95a3e4c4
MH
292static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
293 Dwarf_Die *die_mem)
804b3606
MH
294{
295 struct __addr_die_search_param ad;
296 ad.addr = addr;
297 ad.die_mem = die_mem;
298 /* dwarf_getscopes can't find subprogram. */
299 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
300 return NULL;
301 else
302 return die_mem;
631c9def
MH
303}
304
016f262e
MH
305/* die_find callback for inline function search */
306static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
161a26b0 307{
016f262e 308 Dwarf_Addr *addr = data;
161a26b0 309
016f262e
MH
310 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
311 dwarf_haspc(die_mem, *addr))
312 return DIE_FIND_CB_FOUND;
161a26b0 313
016f262e 314 return DIE_FIND_CB_CONTINUE;
161a26b0
MH
315}
316
016f262e
MH
317/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
318static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
319 Dwarf_Die *die_mem)
4ea42b18 320{
016f262e 321 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
4ea42b18
MH
322}
323
016f262e 324static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
4ea42b18 325{
016f262e
MH
326 const char *name = data;
327 int tag;
4ea42b18 328
016f262e
MH
329 tag = dwarf_tag(die_mem);
330 if ((tag == DW_TAG_formal_parameter ||
331 tag == DW_TAG_variable) &&
332 (die_compare_name(die_mem, name) == 0))
333 return DIE_FIND_CB_FOUND;
334
335 return DIE_FIND_CB_CONTINUE;
4ea42b18
MH
336}
337
016f262e 338/* Find a variable called 'name' */
e92b85e1
MH
339static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
340 Dwarf_Die *die_mem)
4ea42b18 341{
016f262e
MH
342 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
343 die_mem);
4ea42b18
MH
344}
345
7df2f329
MH
346static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
347{
348 const char *name = data;
349
350 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
351 (die_compare_name(die_mem, name) == 0))
352 return DIE_FIND_CB_FOUND;
353
354 return DIE_FIND_CB_SIBLING;
355}
356
357/* Find a member called 'name' */
358static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
359 Dwarf_Die *die_mem)
360{
361 return die_find_child(st_die, __die_find_member_cb, (void *)name,
362 die_mem);
363}
364
4ea42b18
MH
365/*
366 * Probe finder related functions
367 */
368
b7dcb857
MH
369static struct kprobe_trace_arg_ref *alloc_trace_arg_ref(long offs)
370{
371 struct kprobe_trace_arg_ref *ref;
372 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
373 if (ref != NULL)
374 ref->offset = offs;
375 return ref;
376}
377
4ea42b18 378/* Show a location */
b7dcb857 379static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
4ea42b18 380{
b7dcb857
MH
381 Dwarf_Attribute attr;
382 Dwarf_Op *op;
383 size_t nops;
804b3606
MH
384 unsigned int regn;
385 Dwarf_Word offs = 0;
4235b045 386 bool ref = false;
4ea42b18 387 const char *regs;
4235b045 388 struct kprobe_trace_arg *tvar = pf->tvar;
b7dcb857
MH
389 int ret;
390
391 /* TODO: handle more than 1 exprs */
392 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
393 dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
394 nops == 0) {
395 /* TODO: Support const_value */
396 pr_err("Failed to find the location of %s at this address.\n"
397 " Perhaps, it has been optimized out.\n", pf->pvar->var);
398 return -ENOENT;
399 }
400
401 if (op->atom == DW_OP_addr) {
402 /* Static variables on memory (not stack), make @varname */
403 ret = strlen(dwarf_diename(vr_die));
404 tvar->value = zalloc(ret + 2);
405 if (tvar->value == NULL)
406 return -ENOMEM;
407 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
408 tvar->ref = alloc_trace_arg_ref((long)offs);
409 if (tvar->ref == NULL)
410 return -ENOMEM;
411 return 0;
412 }
4ea42b18 413
4ea42b18 414 /* If this is based on frame buffer, set the offset */
804b3606 415 if (op->atom == DW_OP_fbreg) {
b55a87ad
MH
416 if (pf->fb_ops == NULL) {
417 pr_warning("The attribute of frame base is not "
418 "supported.\n");
419 return -ENOTSUP;
420 }
4235b045 421 ref = true;
804b3606
MH
422 offs = op->number;
423 op = &pf->fb_ops[0];
424 }
4ea42b18 425
804b3606
MH
426 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
427 regn = op->atom - DW_OP_breg0;
428 offs += op->number;
4235b045 429 ref = true;
804b3606
MH
430 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
431 regn = op->atom - DW_OP_reg0;
432 } else if (op->atom == DW_OP_bregx) {
433 regn = op->number;
434 offs += op->number2;
4235b045 435 ref = true;
804b3606
MH
436 } else if (op->atom == DW_OP_regx) {
437 regn = op->number;
b55a87ad
MH
438 } else {
439 pr_warning("DW_OP %x is not supported.\n", op->atom);
440 return -ENOTSUP;
441 }
4ea42b18
MH
442
443 regs = get_arch_regstr(regn);
b55a87ad 444 if (!regs) {
cd932c59 445 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
b55a87ad
MH
446 return -ERANGE;
447 }
4ea42b18 448
02b95dad
MH
449 tvar->value = strdup(regs);
450 if (tvar->value == NULL)
451 return -ENOMEM;
452
4235b045 453 if (ref) {
b7dcb857 454 tvar->ref = alloc_trace_arg_ref((long)offs);
e334016f
MH
455 if (tvar->ref == NULL)
456 return -ENOMEM;
4235b045 457 }
b55a87ad 458 return 0;
4ea42b18
MH
459}
460
b55a87ad 461static int convert_variable_type(Dwarf_Die *vr_die,
73317b95
MH
462 struct kprobe_trace_arg *tvar,
463 const char *cast)
4984912e 464{
73317b95 465 struct kprobe_trace_arg_ref **ref_ptr = &tvar->ref;
4984912e
MH
466 Dwarf_Die type;
467 char buf[16];
468 int ret;
469
73317b95
MH
470 /* TODO: check all types */
471 if (cast && strcmp(cast, "string") != 0) {
472 /* Non string type is OK */
473 tvar->type = strdup(cast);
474 return (tvar->type == NULL) ? -ENOMEM : 0;
475 }
476
b55a87ad
MH
477 if (die_get_real_type(vr_die, &type) == NULL) {
478 pr_warning("Failed to get a type information of %s.\n",
479 dwarf_diename(vr_die));
480 return -ENOENT;
481 }
4984912e 482
b2a3c12b
MH
483 pr_debug("%s type is %s.\n",
484 dwarf_diename(vr_die), dwarf_diename(&type));
485
73317b95
MH
486 if (cast && strcmp(cast, "string") == 0) { /* String type */
487 ret = dwarf_tag(&type);
488 if (ret != DW_TAG_pointer_type &&
489 ret != DW_TAG_array_type) {
490 pr_warning("Failed to cast into string: "
491 "%s(%s) is not a pointer nor array.",
492 dwarf_diename(vr_die), dwarf_diename(&type));
493 return -EINVAL;
494 }
495 if (ret == DW_TAG_pointer_type) {
496 if (die_get_real_type(&type, &type) == NULL) {
497 pr_warning("Failed to get a type information.");
498 return -ENOENT;
499 }
500 while (*ref_ptr)
501 ref_ptr = &(*ref_ptr)->next;
502 /* Add new reference with offset +0 */
503 *ref_ptr = zalloc(sizeof(struct kprobe_trace_arg_ref));
504 if (*ref_ptr == NULL) {
505 pr_warning("Out of memory error\n");
506 return -ENOMEM;
507 }
508 }
509 if (die_compare_name(&type, "char") != 0 &&
510 die_compare_name(&type, "unsigned char") != 0) {
511 pr_warning("Failed to cast into string: "
512 "%s is not (unsigned) char *.",
513 dwarf_diename(vr_die));
514 return -EINVAL;
515 }
516 tvar->type = strdup(cast);
517 return (tvar->type == NULL) ? -ENOMEM : 0;
518 }
519
4984912e
MH
520 ret = die_get_byte_size(&type) * 8;
521 if (ret) {
522 /* Check the bitwidth */
523 if (ret > MAX_BASIC_TYPE_BITS) {
b55a87ad
MH
524 pr_info("%s exceeds max-bitwidth."
525 " Cut down to %d bits.\n",
526 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
4984912e
MH
527 ret = MAX_BASIC_TYPE_BITS;
528 }
529
530 ret = snprintf(buf, 16, "%c%d",
531 die_is_signed_type(&type) ? 's' : 'u', ret);
b55a87ad
MH
532 if (ret < 0 || ret >= 16) {
533 if (ret >= 16)
534 ret = -E2BIG;
535 pr_warning("Failed to convert variable type: %s\n",
536 strerror(-ret));
537 return ret;
538 }
73317b95
MH
539 tvar->type = strdup(buf);
540 if (tvar->type == NULL)
02b95dad 541 return -ENOMEM;
4984912e 542 }
b55a87ad 543 return 0;
4984912e
MH
544}
545
b55a87ad 546static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
7df2f329 547 struct perf_probe_arg_field *field,
4984912e
MH
548 struct kprobe_trace_arg_ref **ref_ptr,
549 Dwarf_Die *die_mem)
7df2f329
MH
550{
551 struct kprobe_trace_arg_ref *ref = *ref_ptr;
7df2f329
MH
552 Dwarf_Die type;
553 Dwarf_Word offs;
b2a3c12b 554 int ret, tag;
7df2f329
MH
555
556 pr_debug("converting %s in %s\n", field->name, varname);
b55a87ad
MH
557 if (die_get_real_type(vr_die, &type) == NULL) {
558 pr_warning("Failed to get the type of %s.\n", varname);
559 return -ENOENT;
560 }
b2a3c12b
MH
561 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
562 tag = dwarf_tag(&type);
563
564 if (field->name[0] == '[' &&
565 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
566 if (field->next)
567 /* Save original type for next field */
568 memcpy(die_mem, &type, sizeof(*die_mem));
569 /* Get the type of this array */
570 if (die_get_real_type(&type, &type) == NULL) {
571 pr_warning("Failed to get the type of %s.\n", varname);
572 return -ENOENT;
573 }
574 pr_debug2("Array real type: (%x)\n",
575 (unsigned)dwarf_dieoffset(&type));
576 if (tag == DW_TAG_pointer_type) {
577 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
578 if (ref == NULL)
579 return -ENOMEM;
580 if (*ref_ptr)
581 (*ref_ptr)->next = ref;
582 else
583 *ref_ptr = ref;
584 }
585 ref->offset += die_get_byte_size(&type) * field->index;
586 if (!field->next)
587 /* Save vr_die for converting types */
588 memcpy(die_mem, vr_die, sizeof(*die_mem));
589 goto next;
590 } else if (tag == DW_TAG_pointer_type) {
591 /* Check the pointer and dereference */
b55a87ad
MH
592 if (!field->ref) {
593 pr_err("Semantic error: %s must be referred by '->'\n",
594 field->name);
595 return -EINVAL;
596 }
7df2f329 597 /* Get the type pointed by this pointer */
b55a87ad
MH
598 if (die_get_real_type(&type, &type) == NULL) {
599 pr_warning("Failed to get the type of %s.\n", varname);
600 return -ENOENT;
601 }
12e5a7ae 602 /* Verify it is a data structure */
b55a87ad
MH
603 if (dwarf_tag(&type) != DW_TAG_structure_type) {
604 pr_warning("%s is not a data structure.\n", varname);
605 return -EINVAL;
606 }
12e5a7ae 607
e334016f
MH
608 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
609 if (ref == NULL)
610 return -ENOMEM;
7df2f329
MH
611 if (*ref_ptr)
612 (*ref_ptr)->next = ref;
613 else
614 *ref_ptr = ref;
615 } else {
12e5a7ae 616 /* Verify it is a data structure */
b2a3c12b 617 if (tag != DW_TAG_structure_type) {
b55a87ad
MH
618 pr_warning("%s is not a data structure.\n", varname);
619 return -EINVAL;
620 }
b2a3c12b
MH
621 if (field->name[0] == '[') {
622 pr_err("Semantic error: %s is not a pointor nor array.",
623 varname);
624 return -EINVAL;
625 }
b55a87ad
MH
626 if (field->ref) {
627 pr_err("Semantic error: %s must be referred by '.'\n",
628 field->name);
629 return -EINVAL;
630 }
631 if (!ref) {
632 pr_warning("Structure on a register is not "
633 "supported yet.\n");
634 return -ENOTSUP;
635 }
7df2f329
MH
636 }
637
b55a87ad
MH
638 if (die_find_member(&type, field->name, die_mem) == NULL) {
639 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
640 dwarf_diename(&type), field->name);
641 return -EINVAL;
642 }
7df2f329
MH
643
644 /* Get the offset of the field */
de1439d8
MH
645 ret = die_get_data_member_location(die_mem, &offs);
646 if (ret < 0) {
b55a87ad 647 pr_warning("Failed to get the offset of %s.\n", field->name);
de1439d8 648 return ret;
b55a87ad 649 }
7df2f329
MH
650 ref->offset += (long)offs;
651
b2a3c12b 652next:
7df2f329
MH
653 /* Converting next field */
654 if (field->next)
b55a87ad 655 return convert_variable_fields(die_mem, field->name,
de1439d8 656 field->next, &ref, die_mem);
b55a87ad
MH
657 else
658 return 0;
7df2f329
MH
659}
660
4ea42b18 661/* Show a variables in kprobe event format */
b55a87ad 662static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
4ea42b18 663{
4984912e 664 Dwarf_Die die_mem;
4ea42b18
MH
665 int ret;
666
b7dcb857
MH
667 pr_debug("Converting variable %s into trace event.\n",
668 dwarf_diename(vr_die));
804b3606 669
b7dcb857 670 ret = convert_variable_location(vr_die, pf);
b55a87ad
MH
671 if (ret == 0 && pf->pvar->field) {
672 ret = convert_variable_fields(vr_die, pf->pvar->var,
673 pf->pvar->field, &pf->tvar->ref,
674 &die_mem);
4984912e
MH
675 vr_die = &die_mem;
676 }
73317b95
MH
677 if (ret == 0)
678 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
804b3606 679 /* *expr will be cached in libdw. Don't free it. */
b55a87ad 680 return ret;
4ea42b18
MH
681}
682
4ea42b18 683/* Find a variable in a subprogram die */
b55a87ad 684static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18 685{
b7dcb857 686 Dwarf_Die vr_die, *scopes;
11a1ca35 687 char buf[32], *ptr;
b7dcb857 688 int ret, nscopes;
4ea42b18 689
48481938 690 if (pf->pvar->name)
02b95dad 691 pf->tvar->name = strdup(pf->pvar->name);
48481938 692 else {
02b95dad
MH
693 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
694 if (ret < 0)
695 return ret;
11a1ca35
MH
696 ptr = strchr(buf, ':'); /* Change type separator to _ */
697 if (ptr)
698 *ptr = '_';
02b95dad 699 pf->tvar->name = strdup(buf);
48481938 700 }
02b95dad
MH
701 if (pf->tvar->name == NULL)
702 return -ENOMEM;
48481938
MH
703
704 if (!is_c_varname(pf->pvar->var)) {
4235b045 705 /* Copy raw parameters */
02b95dad
MH
706 pf->tvar->value = strdup(pf->pvar->var);
707 if (pf->tvar->value == NULL)
708 return -ENOMEM;
709 else
710 return 0;
4ea42b18 711 }
b55a87ad
MH
712
713 pr_debug("Searching '%s' variable in context.\n",
714 pf->pvar->var);
715 /* Search child die for local variables and parameters. */
b7dcb857
MH
716 if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
717 ret = convert_variable(&vr_die, pf);
718 else {
719 /* Search upper class */
720 nscopes = dwarf_getscopes_die(sp_die, &scopes);
721 if (nscopes > 0) {
722 ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
723 0, NULL, 0, 0, &vr_die);
724 if (ret >= 0)
725 ret = convert_variable(&vr_die, pf);
726 else
727 ret = -ENOENT;
728 free(scopes);
729 } else
730 ret = -ENOENT;
731 }
732 if (ret < 0)
b55a87ad
MH
733 pr_warning("Failed to find '%s' in this function.\n",
734 pf->pvar->var);
b7dcb857 735 return ret;
4ea42b18
MH
736}
737
4ea42b18 738/* Show a probe point to output buffer */
b55a87ad 739static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
4ea42b18 740{
4235b045 741 struct kprobe_trace_event *tev;
e92b85e1
MH
742 Dwarf_Addr eaddr;
743 Dwarf_Die die_mem;
804b3606 744 const char *name;
4235b045 745 int ret, i;
804b3606
MH
746 Dwarf_Attribute fb_attr;
747 size_t nops;
4ea42b18 748
ef4a3565
MH
749 if (pf->ntevs == pf->max_tevs) {
750 pr_warning("Too many( > %d) probe point found.\n",
751 pf->max_tevs);
b55a87ad
MH
752 return -ERANGE;
753 }
4235b045
MH
754 tev = &pf->tevs[pf->ntevs++];
755
e92b85e1
MH
756 /* If no real subprogram, find a real one */
757 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
95a3e4c4 758 sp_die = die_find_real_subprogram(&pf->cu_die,
e92b85e1 759 pf->addr, &die_mem);
b55a87ad
MH
760 if (!sp_die) {
761 pr_warning("Failed to find probe point in any "
762 "functions.\n");
763 return -ENOENT;
764 }
e92b85e1
MH
765 }
766
4235b045 767 /* Copy the name of probe point */
804b3606
MH
768 name = dwarf_diename(sp_die);
769 if (name) {
b55a87ad
MH
770 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
771 pr_warning("Failed to get entry pc of %s\n",
772 dwarf_diename(sp_die));
773 return -ENOENT;
774 }
02b95dad
MH
775 tev->point.symbol = strdup(name);
776 if (tev->point.symbol == NULL)
777 return -ENOMEM;
4235b045
MH
778 tev->point.offset = (unsigned long)(pf->addr - eaddr);
779 } else
4ea42b18 780 /* This function has no name. */
4235b045
MH
781 tev->point.offset = (unsigned long)pf->addr;
782
783 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
784 tev->point.offset);
4ea42b18 785
804b3606
MH
786 /* Get the frame base attribute/ops */
787 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
d0cb4260 788 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
a34a9854 789 if (ret <= 0 || nops == 0) {
804b3606 790 pf->fb_ops = NULL;
7752f1b0 791#if _ELFUTILS_PREREQ(0, 142)
a34a9854
MH
792 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
793 pf->cfi != NULL) {
794 Dwarf_Frame *frame;
b55a87ad
MH
795 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
796 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
797 pr_warning("Failed to get CFA on 0x%jx\n",
798 (uintmax_t)pf->addr);
799 return -ENOENT;
800 }
7752f1b0 801#endif
a34a9854 802 }
804b3606 803
4ea42b18 804 /* Find each argument */
4235b045 805 tev->nargs = pf->pev->nargs;
e334016f
MH
806 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
807 if (tev->args == NULL)
808 return -ENOMEM;
4235b045
MH
809 for (i = 0; i < pf->pev->nargs; i++) {
810 pf->pvar = &pf->pev->args[i];
811 pf->tvar = &tev->args[i];
b55a87ad
MH
812 ret = find_variable(sp_die, pf);
813 if (ret != 0)
814 return ret;
4ea42b18 815 }
804b3606
MH
816
817 /* *pf->fb_ops will be cached in libdw. Don't free it. */
818 pf->fb_ops = NULL;
b55a87ad 819 return 0;
4ea42b18
MH
820}
821
4ea42b18 822/* Find probe point from its line number */
b55a87ad 823static int find_probe_point_by_line(struct probe_finder *pf)
4ea42b18 824{
804b3606
MH
825 Dwarf_Lines *lines;
826 Dwarf_Line *line;
827 size_t nlines, i;
e92b85e1 828 Dwarf_Addr addr;
804b3606 829 int lineno;
b55a87ad 830 int ret = 0;
4ea42b18 831
b55a87ad
MH
832 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
833 pr_warning("No source lines found in this CU.\n");
834 return -ENOENT;
835 }
4ea42b18 836
b55a87ad 837 for (i = 0; i < nlines && ret == 0; i++) {
804b3606 838 line = dwarf_onesrcline(lines, i);
b55a87ad
MH
839 if (dwarf_lineno(line, &lineno) != 0 ||
840 lineno != pf->lno)
4ea42b18
MH
841 continue;
842
804b3606
MH
843 /* TODO: Get fileno from line, but how? */
844 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
845 continue;
b0ef0732 846
b55a87ad
MH
847 if (dwarf_lineaddr(line, &addr) != 0) {
848 pr_warning("Failed to get the address of the line.\n");
849 return -ENOENT;
850 }
804b3606
MH
851 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
852 (int)i, lineno, (uintmax_t)addr);
4ea42b18 853 pf->addr = addr;
804b3606 854
b55a87ad 855 ret = convert_probe_point(NULL, pf);
4ea42b18
MH
856 /* Continuing, because target line might be inlined. */
857 }
b55a87ad 858 return ret;
4ea42b18
MH
859}
860
2a9c8c36
MH
861/* Find lines which match lazy pattern */
862static int find_lazy_match_lines(struct list_head *head,
863 const char *fname, const char *pat)
864{
865 char *fbuf, *p1, *p2;
b448c4b6 866 int fd, line, nlines = -1;
2a9c8c36
MH
867 struct stat st;
868
869 fd = open(fname, O_RDONLY);
b55a87ad
MH
870 if (fd < 0) {
871 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
b448c4b6 872 return -errno;
b55a87ad
MH
873 }
874
b448c4b6 875 if (fstat(fd, &st) < 0) {
b55a87ad
MH
876 pr_warning("Failed to get the size of %s: %s\n",
877 fname, strerror(errno));
b448c4b6
ACM
878 nlines = -errno;
879 goto out_close;
b55a87ad 880 }
b448c4b6
ACM
881
882 nlines = -ENOMEM;
883 fbuf = malloc(st.st_size + 2);
884 if (fbuf == NULL)
885 goto out_close;
886 if (read(fd, fbuf, st.st_size) < 0) {
b55a87ad 887 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
b448c4b6
ACM
888 nlines = -errno;
889 goto out_free_fbuf;
b55a87ad 890 }
2a9c8c36
MH
891 fbuf[st.st_size] = '\n'; /* Dummy line */
892 fbuf[st.st_size + 1] = '\0';
893 p1 = fbuf;
894 line = 1;
b448c4b6 895 nlines = 0;
2a9c8c36
MH
896 while ((p2 = strchr(p1, '\n')) != NULL) {
897 *p2 = '\0';
898 if (strlazymatch(p1, pat)) {
899 line_list__add_line(head, line);
900 nlines++;
901 }
902 line++;
903 p1 = p2 + 1;
904 }
b448c4b6 905out_free_fbuf:
2a9c8c36 906 free(fbuf);
b448c4b6
ACM
907out_close:
908 close(fd);
2a9c8c36
MH
909 return nlines;
910}
911
912/* Find probe points from lazy pattern */
b55a87ad 913static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
2a9c8c36
MH
914{
915 Dwarf_Lines *lines;
916 Dwarf_Line *line;
917 size_t nlines, i;
918 Dwarf_Addr addr;
919 Dwarf_Die die_mem;
920 int lineno;
b55a87ad 921 int ret = 0;
2a9c8c36
MH
922
923 if (list_empty(&pf->lcache)) {
924 /* Matching lazy line pattern */
925 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
4235b045 926 pf->pev->point.lazy_line);
b55a87ad
MH
927 if (ret == 0) {
928 pr_debug("No matched lines found in %s.\n", pf->fname);
929 return 0;
930 } else if (ret < 0)
931 return ret;
2a9c8c36
MH
932 }
933
b55a87ad
MH
934 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
935 pr_warning("No source lines found in this CU.\n");
936 return -ENOENT;
937 }
938
939 for (i = 0; i < nlines && ret >= 0; i++) {
2a9c8c36
MH
940 line = dwarf_onesrcline(lines, i);
941
b55a87ad
MH
942 if (dwarf_lineno(line, &lineno) != 0 ||
943 !line_list__has_line(&pf->lcache, lineno))
2a9c8c36
MH
944 continue;
945
946 /* TODO: Get fileno from line, but how? */
947 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
948 continue;
949
b55a87ad
MH
950 if (dwarf_lineaddr(line, &addr) != 0) {
951 pr_debug("Failed to get the address of line %d.\n",
952 lineno);
953 continue;
954 }
2a9c8c36
MH
955 if (sp_die) {
956 /* Address filtering 1: does sp_die include addr? */
957 if (!dwarf_haspc(sp_die, addr))
958 continue;
959 /* Address filtering 2: No child include addr? */
95a3e4c4 960 if (die_find_inlinefunc(sp_die, addr, &die_mem))
2a9c8c36
MH
961 continue;
962 }
963
964 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
965 (int)i, lineno, (unsigned long long)addr);
966 pf->addr = addr;
967
b55a87ad 968 ret = convert_probe_point(sp_die, pf);
2a9c8c36
MH
969 /* Continuing, because target line might be inlined. */
970 }
971 /* TODO: deallocate lines, but how? */
b55a87ad 972 return ret;
2a9c8c36
MH
973}
974
b55a87ad
MH
975/* Callback parameter with return value */
976struct dwarf_callback_param {
977 void *data;
978 int retval;
979};
980
e92b85e1
MH
981static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
982{
b55a87ad
MH
983 struct dwarf_callback_param *param = data;
984 struct probe_finder *pf = param->data;
4235b045 985 struct perf_probe_point *pp = &pf->pev->point;
b55a87ad 986 Dwarf_Addr addr;
e92b85e1 987
2a9c8c36 988 if (pp->lazy_line)
b55a87ad 989 param->retval = find_probe_point_lazy(in_die, pf);
2a9c8c36
MH
990 else {
991 /* Get probe address */
b55a87ad
MH
992 if (dwarf_entrypc(in_die, &addr) != 0) {
993 pr_warning("Failed to get entry pc of %s.\n",
994 dwarf_diename(in_die));
995 param->retval = -ENOENT;
996 return DWARF_CB_ABORT;
997 }
998 pf->addr = addr;
2a9c8c36
MH
999 pf->addr += pp->offset;
1000 pr_debug("found inline addr: 0x%jx\n",
1001 (uintmax_t)pf->addr);
1002
b55a87ad 1003 param->retval = convert_probe_point(in_die, pf);
5d1ee041
MH
1004 if (param->retval < 0)
1005 return DWARF_CB_ABORT;
2a9c8c36 1006 }
e92b85e1 1007
e92b85e1
MH
1008 return DWARF_CB_OK;
1009}
804b3606 1010
4ea42b18 1011/* Search function from function name */
e92b85e1 1012static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
4ea42b18 1013{
b55a87ad
MH
1014 struct dwarf_callback_param *param = data;
1015 struct probe_finder *pf = param->data;
4235b045 1016 struct perf_probe_point *pp = &pf->pev->point;
4ea42b18 1017
e92b85e1
MH
1018 /* Check tag and diename */
1019 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
1020 die_compare_name(sp_die, pp->function) != 0)
b55a87ad 1021 return DWARF_CB_OK;
e92b85e1 1022
2a9c8c36 1023 pf->fname = dwarf_decl_file(sp_die);
e92b85e1 1024 if (pp->line) { /* Function relative line */
e92b85e1
MH
1025 dwarf_decl_line(sp_die, &pf->lno);
1026 pf->lno += pp->line;
b55a87ad 1027 param->retval = find_probe_point_by_line(pf);
e92b85e1
MH
1028 } else if (!dwarf_func_inline(sp_die)) {
1029 /* Real function */
2a9c8c36 1030 if (pp->lazy_line)
b55a87ad 1031 param->retval = find_probe_point_lazy(sp_die, pf);
2a9c8c36 1032 else {
b55a87ad
MH
1033 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
1034 pr_warning("Failed to get entry pc of %s.\n",
1035 dwarf_diename(sp_die));
1036 param->retval = -ENOENT;
1037 return DWARF_CB_ABORT;
1038 }
2a9c8c36
MH
1039 pf->addr += pp->offset;
1040 /* TODO: Check the address in this function */
b55a87ad 1041 param->retval = convert_probe_point(sp_die, pf);
2a9c8c36 1042 }
b55a87ad
MH
1043 } else {
1044 struct dwarf_callback_param _param = {.data = (void *)pf,
1045 .retval = 0};
e92b85e1 1046 /* Inlined function: search instances */
b55a87ad
MH
1047 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1048 &_param);
1049 param->retval = _param.retval;
1050 }
e92b85e1 1051
b55a87ad 1052 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
4ea42b18
MH
1053}
1054
b55a87ad 1055static int find_probe_point_by_func(struct probe_finder *pf)
4ea42b18 1056{
b55a87ad
MH
1057 struct dwarf_callback_param _param = {.data = (void *)pf,
1058 .retval = 0};
1059 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1060 return _param.retval;
4ea42b18
MH
1061}
1062
4235b045
MH
1063/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
1064int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
ef4a3565 1065 struct kprobe_trace_event **tevs, int max_tevs)
4ea42b18 1066{
ef4a3565 1067 struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
4235b045 1068 struct perf_probe_point *pp = &pev->point;
804b3606
MH
1069 Dwarf_Off off, noff;
1070 size_t cuhl;
1071 Dwarf_Die *diep;
1072 Dwarf *dbg;
b55a87ad 1073 int ret = 0;
804b3606 1074
ef4a3565 1075 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
e334016f
MH
1076 if (pf.tevs == NULL)
1077 return -ENOMEM;
4235b045
MH
1078 *tevs = pf.tevs;
1079 pf.ntevs = 0;
1080
804b3606 1081 dbg = dwarf_begin(fd, DWARF_C_READ);
b55a87ad
MH
1082 if (!dbg) {
1083 pr_warning("No dwarf info found in the vmlinux - "
1084 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
b448c4b6
ACM
1085 free(pf.tevs);
1086 *tevs = NULL;
b55a87ad
MH
1087 return -EBADF;
1088 }
4ea42b18 1089
7752f1b0 1090#if _ELFUTILS_PREREQ(0, 142)
a34a9854
MH
1091 /* Get the call frame information from this dwarf */
1092 pf.cfi = dwarf_getcfi(dbg);
7752f1b0 1093#endif
a34a9854 1094
804b3606 1095 off = 0;
2a9c8c36 1096 line_list__init(&pf.lcache);
804b3606 1097 /* Loop on CUs (Compilation Unit) */
b55a87ad
MH
1098 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1099 ret >= 0) {
4ea42b18 1100 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
1101 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
1102 if (!diep)
1103 continue;
4ea42b18
MH
1104
1105 /* Check if target file is included. */
1106 if (pp->file)
2a9c8c36 1107 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
804b3606 1108 else
2a9c8c36 1109 pf.fname = NULL;
4ea42b18 1110
2a9c8c36 1111 if (!pp->file || pf.fname) {
4ea42b18 1112 if (pp->function)
b55a87ad 1113 ret = find_probe_point_by_func(&pf);
2a9c8c36 1114 else if (pp->lazy_line)
b55a87ad 1115 ret = find_probe_point_lazy(NULL, &pf);
b0ef0732
MH
1116 else {
1117 pf.lno = pp->line;
b55a87ad 1118 ret = find_probe_point_by_line(&pf);
b0ef0732 1119 }
4ea42b18 1120 }
804b3606 1121 off = noff;
4ea42b18 1122 }
2a9c8c36 1123 line_list__free(&pf.lcache);
804b3606 1124 dwarf_end(dbg);
4ea42b18 1125
b55a87ad 1126 return (ret < 0) ? ret : pf.ntevs;
4ea42b18
MH
1127}
1128
fb1587d8
MH
1129/* Reverse search */
1130int find_perf_probe_point(int fd, unsigned long addr,
1131 struct perf_probe_point *ppt)
1132{
1133 Dwarf_Die cudie, spdie, indie;
1134 Dwarf *dbg;
1135 Dwarf_Line *line;
1136 Dwarf_Addr laddr, eaddr;
1137 const char *tmp;
1138 int lineno, ret = 0;
b55a87ad 1139 bool found = false;
fb1587d8
MH
1140
1141 dbg = dwarf_begin(fd, DWARF_C_READ);
1142 if (!dbg)
b55a87ad 1143 return -EBADF;
fb1587d8
MH
1144
1145 /* Find cu die */
75ec5a24
MH
1146 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1147 ret = -EINVAL;
1148 goto end;
1149 }
fb1587d8
MH
1150
1151 /* Find a corresponding line */
1152 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1153 if (line) {
b55a87ad
MH
1154 if (dwarf_lineaddr(line, &laddr) == 0 &&
1155 (Dwarf_Addr)addr == laddr &&
1156 dwarf_lineno(line, &lineno) == 0) {
fb1587d8 1157 tmp = dwarf_linesrc(line, NULL, NULL);
b55a87ad
MH
1158 if (tmp) {
1159 ppt->line = lineno;
02b95dad
MH
1160 ppt->file = strdup(tmp);
1161 if (ppt->file == NULL) {
1162 ret = -ENOMEM;
1163 goto end;
1164 }
b55a87ad
MH
1165 found = true;
1166 }
fb1587d8
MH
1167 }
1168 }
1169
1170 /* Find a corresponding function */
1171 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1172 tmp = dwarf_diename(&spdie);
b55a87ad 1173 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
fb1587d8
MH
1174 goto end;
1175
b55a87ad
MH
1176 if (ppt->line) {
1177 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1178 &indie)) {
1179 /* addr in an inline function */
1180 tmp = dwarf_diename(&indie);
1181 if (!tmp)
1182 goto end;
1183 ret = dwarf_decl_line(&indie, &lineno);
1184 } else {
1185 if (eaddr == addr) { /* Function entry */
1186 lineno = ppt->line;
1187 ret = 0;
1188 } else
1189 ret = dwarf_decl_line(&spdie, &lineno);
1190 }
1191 if (ret == 0) {
1192 /* Make a relative line number */
1193 ppt->line -= lineno;
1194 goto found;
1195 }
fb1587d8 1196 }
b55a87ad
MH
1197 /* We don't have a line number, let's use offset */
1198 ppt->offset = addr - (unsigned long)eaddr;
1199found:
02b95dad
MH
1200 ppt->function = strdup(tmp);
1201 if (ppt->function == NULL) {
1202 ret = -ENOMEM;
1203 goto end;
1204 }
b55a87ad 1205 found = true;
fb1587d8
MH
1206 }
1207
1208end:
1209 dwarf_end(dbg);
b55a87ad
MH
1210 if (ret >= 0)
1211 ret = found ? 1 : 0;
fb1587d8
MH
1212 return ret;
1213}
1214
f6c903f5
MH
1215/* Add a line and store the src path */
1216static int line_range_add_line(const char *src, unsigned int lineno,
1217 struct line_range *lr)
1218{
7cf0b79e 1219 /* Copy source path */
f6c903f5 1220 if (!lr->path) {
7cf0b79e
MH
1221 lr->path = strdup(src);
1222 if (lr->path == NULL)
1223 return -ENOMEM;
f6c903f5
MH
1224 }
1225 return line_list__add_line(&lr->line_list, lineno);
1226}
1227
1228/* Search function declaration lines */
1229static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1230{
1231 struct dwarf_callback_param *param = data;
1232 struct line_finder *lf = param->data;
1233 const char *src;
1234 int lineno;
1235
1236 src = dwarf_decl_file(sp_die);
1237 if (src && strtailcmp(src, lf->fname) != 0)
1238 return DWARF_CB_OK;
1239
1240 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1241 (lf->lno_s > lineno || lf->lno_e < lineno))
1242 return DWARF_CB_OK;
1243
1244 param->retval = line_range_add_line(src, lineno, lf->lr);
5d1ee041
MH
1245 if (param->retval < 0)
1246 return DWARF_CB_ABORT;
f6c903f5
MH
1247 return DWARF_CB_OK;
1248}
1249
1250static int find_line_range_func_decl_lines(struct line_finder *lf)
1251{
1252 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1253 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1254 return param.retval;
1255}
fb1587d8 1256
631c9def 1257/* Find line range from its line number */
b55a87ad 1258static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
631c9def 1259{
804b3606
MH
1260 Dwarf_Lines *lines;
1261 Dwarf_Line *line;
1262 size_t nlines, i;
631c9def 1263 Dwarf_Addr addr;
f6c903f5 1264 int lineno, ret = 0;
804b3606 1265 const char *src;
161a26b0 1266 Dwarf_Die die_mem;
631c9def 1267
2a9c8c36 1268 line_list__init(&lf->lr->line_list);
b55a87ad
MH
1269 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1270 pr_warning("No source lines found in this CU.\n");
1271 return -ENOENT;
1272 }
631c9def 1273
f6c903f5 1274 /* Search probable lines on lines list */
804b3606
MH
1275 for (i = 0; i < nlines; i++) {
1276 line = dwarf_onesrcline(lines, i);
b55a87ad
MH
1277 if (dwarf_lineno(line, &lineno) != 0 ||
1278 (lf->lno_s > lineno || lf->lno_e < lineno))
631c9def
MH
1279 continue;
1280
161a26b0
MH
1281 if (sp_die) {
1282 /* Address filtering 1: does sp_die include addr? */
b55a87ad
MH
1283 if (dwarf_lineaddr(line, &addr) != 0 ||
1284 !dwarf_haspc(sp_die, addr))
161a26b0
MH
1285 continue;
1286
1287 /* Address filtering 2: No child include addr? */
95a3e4c4 1288 if (die_find_inlinefunc(sp_die, addr, &die_mem))
161a26b0
MH
1289 continue;
1290 }
1291
804b3606
MH
1292 /* TODO: Get fileno from line, but how? */
1293 src = dwarf_linesrc(line, NULL, NULL);
1294 if (strtailcmp(src, lf->fname) != 0)
631c9def
MH
1295 continue;
1296
f6c903f5
MH
1297 ret = line_range_add_line(src, lineno, lf->lr);
1298 if (ret < 0)
1299 return ret;
631c9def 1300 }
f6c903f5
MH
1301
1302 /*
1303 * Dwarf lines doesn't include function declarations. We have to
1304 * check functions list or given function.
1305 */
1306 if (sp_die) {
1307 src = dwarf_decl_file(sp_die);
1308 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1309 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1310 ret = line_range_add_line(src, lineno, lf->lr);
1311 } else
1312 ret = find_line_range_func_decl_lines(lf);
1313
804b3606 1314 /* Update status */
f6c903f5
MH
1315 if (ret >= 0)
1316 if (!list_empty(&lf->lr->line_list))
1317 ret = lf->found = 1;
1318 else
1319 ret = 0; /* Lines are not found */
804b3606
MH
1320 else {
1321 free(lf->lr->path);
1322 lf->lr->path = NULL;
1323 }
f6c903f5 1324 return ret;
631c9def
MH
1325}
1326
161a26b0
MH
1327static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1328{
b55a87ad
MH
1329 struct dwarf_callback_param *param = data;
1330
1331 param->retval = find_line_range_by_line(in_die, param->data);
161a26b0
MH
1332 return DWARF_CB_ABORT; /* No need to find other instances */
1333}
1334
631c9def 1335/* Search function from function name */
e92b85e1 1336static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
631c9def 1337{
b55a87ad
MH
1338 struct dwarf_callback_param *param = data;
1339 struct line_finder *lf = param->data;
631c9def 1340 struct line_range *lr = lf->lr;
631c9def 1341
e92b85e1
MH
1342 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1343 die_compare_name(sp_die, lr->function) == 0) {
e92b85e1
MH
1344 lf->fname = dwarf_decl_file(sp_die);
1345 dwarf_decl_line(sp_die, &lr->offset);
804b3606 1346 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
631c9def 1347 lf->lno_s = lr->offset + lr->start;
d3b63d7a
MH
1348 if (lf->lno_s < 0) /* Overflow */
1349 lf->lno_s = INT_MAX;
1350 lf->lno_e = lr->offset + lr->end;
1351 if (lf->lno_e < 0) /* Overflow */
804b3606 1352 lf->lno_e = INT_MAX;
d3b63d7a 1353 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
631c9def
MH
1354 lr->start = lf->lno_s;
1355 lr->end = lf->lno_e;
b55a87ad
MH
1356 if (dwarf_func_inline(sp_die)) {
1357 struct dwarf_callback_param _param;
1358 _param.data = (void *)lf;
1359 _param.retval = 0;
161a26b0 1360 dwarf_func_inline_instances(sp_die,
b55a87ad
MH
1361 line_range_inline_cb,
1362 &_param);
1363 param->retval = _param.retval;
1364 } else
1365 param->retval = find_line_range_by_line(sp_die, lf);
1366 return DWARF_CB_ABORT;
631c9def 1367 }
b55a87ad 1368 return DWARF_CB_OK;
631c9def
MH
1369}
1370
b55a87ad 1371static int find_line_range_by_func(struct line_finder *lf)
631c9def 1372{
b55a87ad
MH
1373 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1374 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1375 return param.retval;
631c9def
MH
1376}
1377
1378int find_line_range(int fd, struct line_range *lr)
1379{
804b3606 1380 struct line_finder lf = {.lr = lr, .found = 0};
b55a87ad 1381 int ret = 0;
804b3606
MH
1382 Dwarf_Off off = 0, noff;
1383 size_t cuhl;
1384 Dwarf_Die *diep;
1385 Dwarf *dbg;
6a330a3c 1386 const char *comp_dir;
804b3606
MH
1387
1388 dbg = dwarf_begin(fd, DWARF_C_READ);
b55a87ad
MH
1389 if (!dbg) {
1390 pr_warning("No dwarf info found in the vmlinux - "
1391 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1392 return -EBADF;
1393 }
631c9def 1394
804b3606 1395 /* Loop on CUs (Compilation Unit) */
b55a87ad
MH
1396 while (!lf.found && ret >= 0) {
1397 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
631c9def
MH
1398 break;
1399
1400 /* Get the DIE(Debugging Information Entry) of this CU */
804b3606
MH
1401 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1402 if (!diep)
1403 continue;
631c9def
MH
1404
1405 /* Check if target file is included. */
1406 if (lr->file)
2a9c8c36 1407 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
804b3606 1408 else
2a9c8c36 1409 lf.fname = 0;
631c9def 1410
2a9c8c36 1411 if (!lr->file || lf.fname) {
631c9def 1412 if (lr->function)
b55a87ad 1413 ret = find_line_range_by_func(&lf);
631c9def
MH
1414 else {
1415 lf.lno_s = lr->start;
d3b63d7a 1416 lf.lno_e = lr->end;
b55a87ad 1417 ret = find_line_range_by_line(NULL, &lf);
631c9def 1418 }
631c9def 1419 }
804b3606 1420 off = noff;
631c9def 1421 }
6a330a3c
MH
1422
1423 /* Store comp_dir */
1424 if (lf.found) {
1425 comp_dir = cu_get_comp_dir(&lf.cu_die);
1426 if (comp_dir) {
1427 lr->comp_dir = strdup(comp_dir);
1428 if (!lr->comp_dir)
1429 ret = -ENOMEM;
1430 }
1431 }
1432
7cf0b79e 1433 pr_debug("path: %s\n", lr->path);
804b3606 1434 dwarf_end(dbg);
b55a87ad
MH
1435
1436 return (ret < 0) ? ret : lf.found;
631c9def
MH
1437}
1438
This page took 0.210181 seconds and 5 git commands to generate.