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