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