blob: 508c017f566acf7e0576513abb50117e40ad8fdb [file] [log] [blame]
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001/*
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>
Ian Munsiecd932c52010-04-20 16:58:32 +100034#include <dwarf-regs.h>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040035
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040036#include "event.h"
37#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040038#include "util.h"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040039#include "symbol.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040040#include "probe-finder.h"
41
Masami Hiramatsu49849122010-04-12 13:17:15 -040042/* Kprobe tracer basic type is up to u64 */
43#define MAX_BASIC_TYPE_BITS 64
44
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040045/*
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);
Juha Leppanend56728b2009-12-07 12:00:40 -050053 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040054 if (s1[i1] != s2[i2])
55 return s1[i1] - s2[i2];
56 }
57 return 0;
58}
59
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050060/* Line number list operations */
61
62/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040063static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050064{
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 */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040074 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050075 }
76 /* List is empty, or the smallest entry */
77 p = head;
78found:
79 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040080 ln = zalloc(sizeof(struct line_node));
81 if (ln == NULL)
82 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050083 ln->line = line;
84 INIT_LIST_HEAD(&ln->list);
85 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040086 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050087}
88
89/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040090static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050091{
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
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900119/* Dwarf FL wrappers */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900120static char *debuginfo_path; /* Currently dummy */
121
122static const Dwfl_Callbacks offline_callbacks = {
123 .find_debuginfo = dwfl_standard_find_debuginfo,
124 .debuginfo_path = &debuginfo_path,
125
126 .section_address = dwfl_offline_section_address,
127
128 /* We use this table for core files too. */
129 .find_elf = dwfl_build_id_find_elf,
130};
131
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900132/* Get a Dwarf from offline image */
133static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
134{
135 Dwfl_Module *mod;
136 Dwarf *dbg = NULL;
137
138 if (!dwflp)
139 return NULL;
140
141 *dwflp = dwfl_begin(&offline_callbacks);
142 if (!*dwflp)
143 return NULL;
144
145 mod = dwfl_report_offline(*dwflp, "", "", fd);
146 if (!mod)
147 goto error;
148
149 dbg = dwfl_module_getdwarf(mod, bias);
150 if (!dbg) {
151error:
152 dwfl_end(*dwflp);
153 *dwflp = NULL;
154 }
155 return dbg;
156}
157
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900158#if _ELFUTILS_PREREQ(0, 148)
159/* This method is buggy if elfutils is older than 0.148 */
160static int __linux_kernel_find_elf(Dwfl_Module *mod,
161 void **userdata,
162 const char *module_name,
163 Dwarf_Addr base,
164 char **file_name, Elf **elfp)
165{
166 int fd;
167 const char *path = kernel_get_module_path(module_name);
168
169 pr_debug2("Use file %s for %s\n", path, module_name);
170 if (path) {
171 fd = open(path, O_RDONLY);
172 if (fd >= 0) {
173 *file_name = strdup(path);
174 return fd;
175 }
176 }
177 /* If failed, try to call standard method */
178 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
179 file_name, elfp);
180}
181
182static const Dwfl_Callbacks kernel_callbacks = {
183 .find_debuginfo = dwfl_standard_find_debuginfo,
184 .debuginfo_path = &debuginfo_path,
185
186 .find_elf = __linux_kernel_find_elf,
187 .section_address = dwfl_linux_kernel_module_section_address,
188};
189
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900190/* Get a Dwarf from live kernel image */
191static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
192 Dwarf_Addr *bias)
193{
194 Dwarf *dbg;
195
196 if (!dwflp)
197 return NULL;
198
199 *dwflp = dwfl_begin(&kernel_callbacks);
200 if (!*dwflp)
201 return NULL;
202
203 /* Load the kernel dwarves: Don't care the result here */
204 dwfl_linux_kernel_report_kernel(*dwflp);
205 dwfl_linux_kernel_report_modules(*dwflp);
206
207 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
208 /* Here, check whether we could get a real dwarf */
209 if (!dbg) {
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900210 pr_debug("Failed to find kernel dwarf at %lx\n",
211 (unsigned long)addr);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900212 dwfl_end(*dwflp);
213 *dwflp = NULL;
214 }
215 return dbg;
216}
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900217#else
218/* With older elfutils, this just support kernel module... */
219static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
220 Dwarf_Addr *bias)
221{
222 int fd;
223 const char *path = kernel_get_module_path("kernel");
224
225 if (!path) {
226 pr_err("Failed to find vmlinux path\n");
227 return NULL;
228 }
229
230 pr_debug2("Use file %s for debuginfo\n", path);
231 fd = open(path, O_RDONLY);
232 if (fd < 0)
233 return NULL;
234
235 return dwfl_init_offline_dwarf(fd, dwflp, bias);
236}
237#endif
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900238
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500239/* Dwarf wrappers */
240
241/* Find the realpath of the target file. */
242static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400243{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500244 Dwarf_Files *files;
245 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300246 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400247 int ret;
248
249 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500250 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500251
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500252 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500253 if (ret != 0)
254 return NULL;
255
256 for (i = 0; i < nfiles; i++) {
257 src = dwarf_filesrc(files, i, NULL, NULL);
258 if (strtailcmp(src, fname) == 0)
259 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500260 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400261 if (i == nfiles)
262 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500263 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500264}
265
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900266/* Get DW_AT_comp_dir (should be NULL with older gcc) */
267static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
268{
269 Dwarf_Attribute attr;
270 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
271 return NULL;
272 return dwarf_formstring(&attr);
273}
274
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400275/* Compare diename and tname */
276static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
277{
278 const char *name;
279 name = dwarf_diename(dw_die);
Masami Hiramatsu82175632010-07-09 18:29:17 +0900280 return name ? (strcmp(tname, name) == 0) : false;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400281}
282
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900283/* Get type die */
284static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
285{
286 Dwarf_Attribute attr;
287
288 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
289 dwarf_formref_die(&attr, die_mem))
290 return die_mem;
291 else
292 return NULL;
293}
294
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900295/* Get a type die, but skip qualifiers */
296static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400297{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400298 int tag;
299
300 do {
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900301 vr_die = die_get_type(vr_die, die_mem);
302 if (!vr_die)
303 break;
304 tag = dwarf_tag(vr_die);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400305 } while (tag == DW_TAG_const_type ||
306 tag == DW_TAG_restrict_type ||
307 tag == DW_TAG_volatile_type ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900308 tag == DW_TAG_shared_type);
309
310 return vr_die;
311}
312
313/* Get a type die, but skip qualifiers and typedef */
314static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
315{
316 do {
317 vr_die = __die_get_real_type(vr_die, die_mem);
318 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400319
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900320 return vr_die;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400321}
322
Masami Hiramatsu49849122010-04-12 13:17:15 -0400323static bool die_is_signed_type(Dwarf_Die *tp_die)
324{
325 Dwarf_Attribute attr;
326 Dwarf_Word ret;
327
328 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
329 dwarf_formudata(&attr, &ret) != 0)
330 return false;
331
332 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
333 ret == DW_ATE_signed_fixed);
334}
335
336static int die_get_byte_size(Dwarf_Die *tp_die)
337{
338 Dwarf_Attribute attr;
339 Dwarf_Word ret;
340
341 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
342 dwarf_formudata(&attr, &ret) != 0)
343 return 0;
344
345 return (int)ret;
346}
347
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300348/* Get data_member_location offset */
349static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
350{
351 Dwarf_Attribute attr;
352 Dwarf_Op *expr;
353 size_t nexpr;
354 int ret;
355
356 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
357 return -ENOENT;
358
359 if (dwarf_formudata(&attr, offs) != 0) {
360 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
361 ret = dwarf_getlocation(&attr, &expr, &nexpr);
362 if (ret < 0 || nexpr == 0)
363 return -ENOENT;
364
365 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
366 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
367 expr[0].atom, nexpr);
368 return -ENOTSUP;
369 }
370 *offs = (Dwarf_Word)expr[0].number;
371 }
372 return 0;
373}
374
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400375/* Return values for die_find callbacks */
376enum {
377 DIE_FIND_CB_FOUND = 0, /* End of Search */
378 DIE_FIND_CB_CHILD = 1, /* Search only children */
379 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
380 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
381};
382
383/* Search a child die */
384static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
385 int (*callback)(Dwarf_Die *, void *),
386 void *data, Dwarf_Die *die_mem)
387{
388 Dwarf_Die child_die;
389 int ret;
390
391 ret = dwarf_child(rt_die, die_mem);
392 if (ret != 0)
393 return NULL;
394
395 do {
396 ret = callback(die_mem, data);
397 if (ret == DIE_FIND_CB_FOUND)
398 return die_mem;
399
400 if ((ret & DIE_FIND_CB_CHILD) &&
401 die_find_child(die_mem, callback, data, &child_die)) {
402 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
403 return die_mem;
404 }
405 } while ((ret & DIE_FIND_CB_SIBLING) &&
406 dwarf_siblingof(die_mem, die_mem) == 0);
407
408 return NULL;
409}
410
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500411struct __addr_die_search_param {
412 Dwarf_Addr addr;
413 Dwarf_Die *die_mem;
414};
415
416static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
417{
418 struct __addr_die_search_param *ad = data;
419
420 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
421 dwarf_haspc(fn_die, ad->addr)) {
422 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
423 return DWARF_CB_ABORT;
424 }
425 return DWARF_CB_OK;
426}
427
428/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400429static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
430 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500431{
432 struct __addr_die_search_param ad;
433 ad.addr = addr;
434 ad.die_mem = die_mem;
435 /* dwarf_getscopes can't find subprogram. */
436 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
437 return NULL;
438 else
439 return die_mem;
440}
441
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400442/* die_find callback for inline function search */
443static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
444{
445 Dwarf_Addr *addr = data;
446
447 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
448 dwarf_haspc(die_mem, *addr))
449 return DIE_FIND_CB_FOUND;
450
451 return DIE_FIND_CB_CONTINUE;
452}
453
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500454/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400455static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
456 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500457{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400458 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500459}
460
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900461/* Walker on lines (Note: line number will not be sorted) */
462typedef int (* line_walk_handler_t) (const char *fname, int lineno,
463 Dwarf_Addr addr, void *data);
464
465struct __line_walk_param {
466 line_walk_handler_t handler;
467 void *data;
468 int retval;
469};
470
471/* Walk on decl lines in given DIE */
472static int __die_walk_funclines(Dwarf_Die *sp_die,
473 line_walk_handler_t handler, void *data)
474{
475 const char *fname;
476 Dwarf_Addr addr;
477 int lineno, ret = 0;
478
479 /* Handle function declaration line */
480 fname = dwarf_decl_file(sp_die);
481 if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
482 dwarf_entrypc(sp_die, &addr) == 0) {
483 ret = handler(fname, lineno, addr, data);
484 }
485
486 return ret;
487}
488
489static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
490{
491 struct __line_walk_param *lw = data;
492
493 lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
494 if (lw->retval != 0)
495 return DWARF_CB_ABORT;
496
497 return DWARF_CB_OK;
498}
499
500/*
501 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
502 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
503 */
504static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
505 void *data)
506{
507 Dwarf_Lines *lines;
508 Dwarf_Line *line;
509 Dwarf_Addr addr;
510 const char *fname;
511 int lineno, ret = 0;
512 Dwarf_Die die_mem, *cu_die;
513 size_t nlines, i;
514
515 /* Get the CU die */
516 if (dwarf_tag(pdie) == DW_TAG_subprogram)
517 cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
518 else
519 cu_die = pdie;
520 if (!cu_die) {
521 pr_debug2("Failed to get CU from subprogram\n");
522 return -EINVAL;
523 }
524
525 /* Get lines list in the CU */
526 if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
527 pr_debug2("Failed to get source lines on this CU.\n");
528 return -ENOENT;
529 }
530 pr_debug2("Get %zd lines from this CU\n", nlines);
531
532 /* Walk on the lines on lines list */
533 for (i = 0; i < nlines; i++) {
534 line = dwarf_onesrcline(lines, i);
535 if (line == NULL ||
536 dwarf_lineno(line, &lineno) != 0 ||
537 dwarf_lineaddr(line, &addr) != 0) {
538 pr_debug2("Failed to get line info. "
539 "Possible error in debuginfo.\n");
540 continue;
541 }
542 /* Filter lines based on address */
543 if (pdie != cu_die)
544 /*
545 * Address filtering
546 * The line is included in given function, and
547 * no inline block includes it.
548 */
549 if (!dwarf_haspc(pdie, addr) ||
550 die_find_inlinefunc(pdie, addr, &die_mem))
551 continue;
552 /* Get source line */
553 fname = dwarf_linesrc(line, NULL, NULL);
554
555 ret = handler(fname, lineno, addr, data);
556 if (ret != 0)
557 return ret;
558 }
559
560 /*
561 * Dwarf lines doesn't include function declarations and inlined
562 * subroutines. We have to check functions list or given function.
563 */
564 if (pdie != cu_die)
565 ret = __die_walk_funclines(pdie, handler, data);
566 else {
567 struct __line_walk_param param = {
568 .handler = handler,
569 .data = data,
570 .retval = 0,
571 };
572 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
573 ret = param.retval;
574 }
575
576 return ret;
577}
578
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900579struct __find_variable_param {
580 const char *name;
581 Dwarf_Addr addr;
582};
583
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400584static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400585{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900586 struct __find_variable_param *fvp = data;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400587 int tag;
588
589 tag = dwarf_tag(die_mem);
590 if ((tag == DW_TAG_formal_parameter ||
591 tag == DW_TAG_variable) &&
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900592 die_compare_name(die_mem, fvp->name))
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400593 return DIE_FIND_CB_FOUND;
594
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900595 if (dwarf_haspc(die_mem, fvp->addr))
596 return DIE_FIND_CB_CONTINUE;
597 else
598 return DIE_FIND_CB_SIBLING;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400599}
600
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900601/* Find a variable called 'name' at given address */
602static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
603 Dwarf_Addr addr, Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500604{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900605 struct __find_variable_param fvp = { .name = name, .addr = addr};
606
607 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400608 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400609}
610
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400611static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
612{
613 const char *name = data;
614
615 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900616 die_compare_name(die_mem, name))
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400617 return DIE_FIND_CB_FOUND;
618
619 return DIE_FIND_CB_SIBLING;
620}
621
622/* Find a member called 'name' */
623static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
624 Dwarf_Die *die_mem)
625{
626 return die_find_child(st_die, __die_find_member_cb, (void *)name,
627 die_mem);
628}
629
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900630/* Get the name of given variable DIE */
631static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
632{
633 Dwarf_Die type;
634 int tag, ret, ret2;
635 const char *tmp = "";
636
637 if (__die_get_real_type(vr_die, &type) == NULL)
638 return -ENOENT;
639
640 tag = dwarf_tag(&type);
641 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
642 tmp = "*";
643 else if (tag == DW_TAG_subroutine_type) {
644 /* Function pointer */
645 ret = snprintf(buf, len, "(function_type)");
646 return (ret >= len) ? -E2BIG : ret;
647 } else {
648 if (!dwarf_diename(&type))
649 return -ENOENT;
650 if (tag == DW_TAG_union_type)
651 tmp = "union ";
652 else if (tag == DW_TAG_structure_type)
653 tmp = "struct ";
654 /* Write a base name */
655 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
656 return (ret >= len) ? -E2BIG : ret;
657 }
658 ret = die_get_typename(&type, buf, len);
659 if (ret > 0) {
660 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
661 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
662 }
663 return ret;
664}
665
666/* Get the name and type of given variable DIE, stored as "type\tname" */
667static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
668{
669 int ret, ret2;
670
671 ret = die_get_typename(vr_die, buf, len);
672 if (ret < 0) {
673 pr_debug("Failed to get type, make it unknown.\n");
674 ret = snprintf(buf, len, "(unknown_type)");
675 }
676 if (ret > 0) {
677 ret2 = snprintf(buf + ret, len - ret, "\t%s",
678 dwarf_diename(vr_die));
679 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
680 }
681 return ret;
682}
683
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400684/*
685 * Probe finder related functions
686 */
687
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530688static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400689{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530690 struct probe_trace_arg_ref *ref;
691 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400692 if (ref != NULL)
693 ref->offset = offs;
694 return ref;
695}
696
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900697/*
698 * Convert a location into trace_arg.
699 * If tvar == NULL, this just checks variable can be converted.
700 */
701static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
702 Dwarf_Op *fb_ops,
703 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400704{
705 Dwarf_Attribute attr;
706 Dwarf_Op *op;
707 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500708 unsigned int regn;
709 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400710 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400711 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400712 int ret;
713
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900714 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
715 goto static_var;
716
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400717 /* TODO: handle more than 1 exprs */
718 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900719 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400720 nops == 0) {
721 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400722 return -ENOENT;
723 }
724
725 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900726static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900727 if (!tvar)
728 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400729 /* Static variables on memory (not stack), make @varname */
730 ret = strlen(dwarf_diename(vr_die));
731 tvar->value = zalloc(ret + 2);
732 if (tvar->value == NULL)
733 return -ENOMEM;
734 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
735 tvar->ref = alloc_trace_arg_ref((long)offs);
736 if (tvar->ref == NULL)
737 return -ENOMEM;
738 return 0;
739 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400740
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400741 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500742 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900743 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400744 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400745 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500746 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900747 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500748 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400749
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500750 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
751 regn = op->atom - DW_OP_breg0;
752 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400753 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500754 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
755 regn = op->atom - DW_OP_reg0;
756 } else if (op->atom == DW_OP_bregx) {
757 regn = op->number;
758 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400759 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500760 } else if (op->atom == DW_OP_regx) {
761 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400762 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900763 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400764 return -ENOTSUP;
765 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400766
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900767 if (!tvar)
768 return 0;
769
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400770 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400771 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900772 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900773 pr_warning("Mapping for the register number %u "
774 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400775 return -ERANGE;
776 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400777
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400778 tvar->value = strdup(regs);
779 if (tvar->value == NULL)
780 return -ENOMEM;
781
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400782 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400783 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400784 if (tvar->ref == NULL)
785 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400786 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400787 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400788}
789
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400790static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530791 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400792 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400793{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530794 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400795 Dwarf_Die type;
796 char buf[16];
797 int ret;
798
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400799 /* TODO: check all types */
800 if (cast && strcmp(cast, "string") != 0) {
801 /* Non string type is OK */
802 tvar->type = strdup(cast);
803 return (tvar->type == NULL) ? -ENOMEM : 0;
804 }
805
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400806 if (die_get_real_type(vr_die, &type) == NULL) {
807 pr_warning("Failed to get a type information of %s.\n",
808 dwarf_diename(vr_die));
809 return -ENOENT;
810 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400811
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400812 pr_debug("%s type is %s.\n",
813 dwarf_diename(vr_die), dwarf_diename(&type));
814
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400815 if (cast && strcmp(cast, "string") == 0) { /* String type */
816 ret = dwarf_tag(&type);
817 if (ret != DW_TAG_pointer_type &&
818 ret != DW_TAG_array_type) {
819 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900820 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400821 dwarf_diename(vr_die), dwarf_diename(&type));
822 return -EINVAL;
823 }
824 if (ret == DW_TAG_pointer_type) {
825 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900826 pr_warning("Failed to get a type"
827 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400828 return -ENOENT;
829 }
830 while (*ref_ptr)
831 ref_ptr = &(*ref_ptr)->next;
832 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530833 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400834 if (*ref_ptr == NULL) {
835 pr_warning("Out of memory error\n");
836 return -ENOMEM;
837 }
838 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900839 if (!die_compare_name(&type, "char") &&
840 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400841 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900842 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400843 dwarf_diename(vr_die));
844 return -EINVAL;
845 }
846 tvar->type = strdup(cast);
847 return (tvar->type == NULL) ? -ENOMEM : 0;
848 }
849
Masami Hiramatsu49849122010-04-12 13:17:15 -0400850 ret = die_get_byte_size(&type) * 8;
851 if (ret) {
852 /* Check the bitwidth */
853 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400854 pr_info("%s exceeds max-bitwidth."
855 " Cut down to %d bits.\n",
856 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400857 ret = MAX_BASIC_TYPE_BITS;
858 }
859
860 ret = snprintf(buf, 16, "%c%d",
861 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400862 if (ret < 0 || ret >= 16) {
863 if (ret >= 16)
864 ret = -E2BIG;
865 pr_warning("Failed to convert variable type: %s\n",
866 strerror(-ret));
867 return ret;
868 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400869 tvar->type = strdup(buf);
870 if (tvar->type == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400871 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400872 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400873 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400874}
875
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400876static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400877 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530878 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400879 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400880{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530881 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400882 Dwarf_Die type;
883 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400884 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400885
886 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400887 if (die_get_real_type(vr_die, &type) == NULL) {
888 pr_warning("Failed to get the type of %s.\n", varname);
889 return -ENOENT;
890 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400891 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
892 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400893
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400894 if (field->name[0] == '[' &&
895 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
896 if (field->next)
897 /* Save original type for next field */
898 memcpy(die_mem, &type, sizeof(*die_mem));
899 /* Get the type of this array */
900 if (die_get_real_type(&type, &type) == NULL) {
901 pr_warning("Failed to get the type of %s.\n", varname);
902 return -ENOENT;
903 }
904 pr_debug2("Array real type: (%x)\n",
905 (unsigned)dwarf_dieoffset(&type));
906 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530907 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400908 if (ref == NULL)
909 return -ENOMEM;
910 if (*ref_ptr)
911 (*ref_ptr)->next = ref;
912 else
913 *ref_ptr = ref;
914 }
915 ref->offset += die_get_byte_size(&type) * field->index;
916 if (!field->next)
917 /* Save vr_die for converting types */
918 memcpy(die_mem, vr_die, sizeof(*die_mem));
919 goto next;
920 } else if (tag == DW_TAG_pointer_type) {
921 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400922 if (!field->ref) {
923 pr_err("Semantic error: %s must be referred by '->'\n",
924 field->name);
925 return -EINVAL;
926 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400927 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400928 if (die_get_real_type(&type, &type) == NULL) {
929 pr_warning("Failed to get the type of %s.\n", varname);
930 return -ENOENT;
931 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400932 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400933 if (dwarf_tag(&type) != DW_TAG_structure_type) {
934 pr_warning("%s is not a data structure.\n", varname);
935 return -EINVAL;
936 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400937
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530938 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400939 if (ref == NULL)
940 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400941 if (*ref_ptr)
942 (*ref_ptr)->next = ref;
943 else
944 *ref_ptr = ref;
945 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400946 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400947 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400948 pr_warning("%s is not a data structure.\n", varname);
949 return -EINVAL;
950 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400951 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900952 pr_err("Semantic error: %s is not a pointor"
953 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400954 return -EINVAL;
955 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400956 if (field->ref) {
957 pr_err("Semantic error: %s must be referred by '.'\n",
958 field->name);
959 return -EINVAL;
960 }
961 if (!ref) {
962 pr_warning("Structure on a register is not "
963 "supported yet.\n");
964 return -ENOTSUP;
965 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400966 }
967
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400968 if (die_find_member(&type, field->name, die_mem) == NULL) {
969 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
970 dwarf_diename(&type), field->name);
971 return -EINVAL;
972 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400973
974 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300975 ret = die_get_data_member_location(die_mem, &offs);
976 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400977 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300978 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400979 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400980 ref->offset += (long)offs;
981
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400982next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400983 /* Converting next field */
984 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400985 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300986 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400987 else
988 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400989}
990
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400991/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400992static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400993{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400994 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400995 int ret;
996
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400997 pr_debug("Converting variable %s into trace event.\n",
998 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500999
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001000 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
1001 pf->tvar);
1002 if (ret == -ENOENT)
1003 pr_err("Failed to find the location of %s at this address.\n"
1004 " Perhaps, it has been optimized out.\n", pf->pvar->var);
1005 else if (ret == -ENOTSUP)
1006 pr_err("Sorry, we don't support this variable location yet.\n");
1007 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001008 ret = convert_variable_fields(vr_die, pf->pvar->var,
1009 pf->pvar->field, &pf->tvar->ref,
1010 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001011 vr_die = &die_mem;
1012 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -04001013 if (ret == 0)
1014 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001015 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001016 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001017}
1018
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001019/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001020static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001021{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001022 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001023 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001024 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001025
Masami Hiramatsu367e94c2010-08-27 20:38:59 +09001026 if (!is_c_varname(pf->pvar->var)) {
1027 /* Copy raw parameters */
1028 pf->tvar->value = strdup(pf->pvar->var);
1029 if (pf->tvar->value == NULL)
1030 return -ENOMEM;
1031 if (pf->pvar->type) {
1032 pf->tvar->type = strdup(pf->pvar->type);
1033 if (pf->tvar->type == NULL)
1034 return -ENOMEM;
1035 }
1036 if (pf->pvar->name) {
1037 pf->tvar->name = strdup(pf->pvar->name);
1038 if (pf->tvar->name == NULL)
1039 return -ENOMEM;
1040 } else
1041 pf->tvar->name = NULL;
1042 return 0;
1043 }
1044
Masami Hiramatsu48481932010-04-12 13:16:53 -04001045 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001046 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001047 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001048 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
1049 if (ret < 0)
1050 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001051 ptr = strchr(buf, ':'); /* Change type separator to _ */
1052 if (ptr)
1053 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001054 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001055 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001056 if (pf->tvar->name == NULL)
1057 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001058
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001059 pr_debug("Searching '%s' variable in context.\n",
1060 pf->pvar->var);
1061 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +09001062 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001063 ret = convert_variable(&vr_die, pf);
1064 else {
1065 /* Search upper class */
1066 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001067 while (nscopes-- > 1) {
1068 pr_debug("Searching variables in %s\n",
1069 dwarf_diename(&scopes[nscopes]));
1070 /* We should check this scope, so give dummy address */
1071 if (die_find_variable_at(&scopes[nscopes],
1072 pf->pvar->var, 0,
1073 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001074 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001075 goto found;
1076 }
1077 }
1078 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001079 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001080 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001081 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001082found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001083 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001084 pr_warning("Failed to find '%s' in this function.\n",
1085 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001086 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001087}
1088
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001089/* Convert subprogram DIE to trace point */
1090static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
1091 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001092{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001093 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001094 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001095
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001096 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001097 name = dwarf_diename(sp_die);
1098 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001099 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001100 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001101 dwarf_diename(sp_die));
1102 return -ENOENT;
1103 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001104 tp->symbol = strdup(name);
1105 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001106 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001107 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001108 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001109 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001110 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001111
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001112 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001113 if (retprobe) {
1114 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001115 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001116 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001117 return -EINVAL;
1118 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001119 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001120 }
1121
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001122 return 0;
1123}
1124
1125/* Call probe_finder callback with real subprogram DIE */
1126static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
1127{
1128 Dwarf_Die die_mem;
1129 Dwarf_Attribute fb_attr;
1130 size_t nops;
1131 int ret;
1132
1133 /* If no real subprogram, find a real one */
1134 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
1135 sp_die = die_find_real_subprogram(&pf->cu_die,
1136 pf->addr, &die_mem);
1137 if (!sp_die) {
1138 pr_warning("Failed to find probe point in any "
1139 "functions.\n");
1140 return -ENOENT;
1141 }
1142 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001143
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001144 /* Get the frame base attribute/ops */
1145 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -04001146 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001147 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001148 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001149#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001150 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1151 pf->cfi != NULL) {
1152 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001153 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1154 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001155 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001156 (uintmax_t)pf->addr);
1157 return -ENOENT;
1158 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001159#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001160 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001161
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001162 /* Call finder's callback handler */
1163 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001164
1165 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1166 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001167
1168 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001169}
1170
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001171static int probe_point_line_walker(const char *fname, int lineno,
1172 Dwarf_Addr addr, void *data)
1173{
1174 struct probe_finder *pf = data;
1175 int ret;
1176
1177 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
1178 return 0;
1179
1180 pf->addr = addr;
1181 ret = call_probe_finder(NULL, pf);
1182
1183 /* Continue if no error, because the line will be in inline function */
1184 return ret < 0 ?: 0;
1185}
1186
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001187/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001188static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001189{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001190 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001191}
1192
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001193/* Find lines which match lazy pattern */
1194static int find_lazy_match_lines(struct list_head *head,
1195 const char *fname, const char *pat)
1196{
1197 char *fbuf, *p1, *p2;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001198 int fd, line, nlines = -1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001199 struct stat st;
1200
1201 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001202 if (fd < 0) {
1203 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001204 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001205 }
1206
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001207 if (fstat(fd, &st) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001208 pr_warning("Failed to get the size of %s: %s\n",
1209 fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001210 nlines = -errno;
1211 goto out_close;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001212 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001213
1214 nlines = -ENOMEM;
1215 fbuf = malloc(st.st_size + 2);
1216 if (fbuf == NULL)
1217 goto out_close;
1218 if (read(fd, fbuf, st.st_size) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001219 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001220 nlines = -errno;
1221 goto out_free_fbuf;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001222 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001223 fbuf[st.st_size] = '\n'; /* Dummy line */
1224 fbuf[st.st_size + 1] = '\0';
1225 p1 = fbuf;
1226 line = 1;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001227 nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001228 while ((p2 = strchr(p1, '\n')) != NULL) {
1229 *p2 = '\0';
1230 if (strlazymatch(p1, pat)) {
1231 line_list__add_line(head, line);
1232 nlines++;
1233 }
1234 line++;
1235 p1 = p2 + 1;
1236 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001237out_free_fbuf:
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001238 free(fbuf);
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001239out_close:
1240 close(fd);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001241 return nlines;
1242}
1243
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001244static int probe_point_lazy_walker(const char *fname, int lineno,
1245 Dwarf_Addr addr, void *data)
1246{
1247 struct probe_finder *pf = data;
1248 int ret;
1249
1250 if (!line_list__has_line(&pf->lcache, lineno) ||
1251 strtailcmp(fname, pf->fname) != 0)
1252 return 0;
1253
1254 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1255 lineno, (unsigned long long)addr);
1256 pf->addr = addr;
1257 ret = call_probe_finder(NULL, pf);
1258
1259 /*
1260 * Continue if no error, because the lazy pattern will match
1261 * to other lines
1262 */
1263 return ret < 0 ?: 0;
1264}
1265
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001266/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001267static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001268{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001269 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001270
1271 if (list_empty(&pf->lcache)) {
1272 /* Matching lazy line pattern */
1273 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001274 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001275 if (ret == 0) {
1276 pr_debug("No matched lines found in %s.\n", pf->fname);
1277 return 0;
1278 } else if (ret < 0)
1279 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001280 }
1281
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001282 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001283}
1284
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001285/* Callback parameter with return value */
1286struct dwarf_callback_param {
1287 void *data;
1288 int retval;
1289};
1290
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001291static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001292{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001293 struct dwarf_callback_param *param = data;
1294 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001295 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001296 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001297
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001298 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001299 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001300 else {
1301 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001302 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001303 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001304 dwarf_diename(in_die));
1305 param->retval = -ENOENT;
1306 return DWARF_CB_ABORT;
1307 }
1308 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001309 pf->addr += pp->offset;
1310 pr_debug("found inline addr: 0x%jx\n",
1311 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001312
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001313 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001314 if (param->retval < 0)
1315 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001316 }
1317
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001318 return DWARF_CB_OK;
1319}
1320
1321/* Search function from function name */
1322static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1323{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001324 struct dwarf_callback_param *param = data;
1325 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001326 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001327
1328 /* Check tag and diename */
1329 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001330 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001331 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001332
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001333 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001334 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001335 dwarf_decl_line(sp_die, &pf->lno);
1336 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001337 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001338 } else if (!dwarf_func_inline(sp_die)) {
1339 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001340 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001341 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001342 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001343 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001344 pr_warning("Failed to get entry address of "
1345 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001346 param->retval = -ENOENT;
1347 return DWARF_CB_ABORT;
1348 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001349 pf->addr += pp->offset;
1350 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001351 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001352 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001353 } else {
1354 struct dwarf_callback_param _param = {.data = (void *)pf,
1355 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001356 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001357 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1358 &_param);
1359 param->retval = _param.retval;
1360 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001361
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001362 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001363}
1364
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001365static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001366{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001367 struct dwarf_callback_param _param = {.data = (void *)pf,
1368 .retval = 0};
1369 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1370 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001371}
1372
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001373/* Find probe points from debuginfo */
1374static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001375{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001376 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001377 Dwarf_Off off, noff;
1378 size_t cuhl;
1379 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001380 Dwarf *dbg = NULL;
1381 Dwfl *dwfl;
1382 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001383 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001384
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001385 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001386 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001387 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001388 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1389 return -EBADF;
1390 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001391
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001392#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001393 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001394 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001395#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001396
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001397 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001398 line_list__init(&pf->lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001399 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001400 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1401 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001402 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001403 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001404 if (!diep)
1405 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001406
1407 /* Check if target file is included. */
1408 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001409 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001410 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001411 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001412
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001413 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001414 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001415 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001416 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001417 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001418 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001419 pf->lno = pp->line;
1420 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001421 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001422 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001423 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001424 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001425 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001426 if (dwfl)
1427 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001428
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001429 return ret;
1430}
1431
1432/* Add a found probe point into trace event list */
1433static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1434{
1435 struct trace_event_finder *tf =
1436 container_of(pf, struct trace_event_finder, pf);
1437 struct probe_trace_event *tev;
1438 int ret, i;
1439
1440 /* Check number of tevs */
1441 if (tf->ntevs == tf->max_tevs) {
1442 pr_warning("Too many( > %d) probe point found.\n",
1443 tf->max_tevs);
1444 return -ERANGE;
1445 }
1446 tev = &tf->tevs[tf->ntevs++];
1447
1448 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1449 &tev->point);
1450 if (ret < 0)
1451 return ret;
1452
1453 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1454 tev->point.offset);
1455
1456 /* Find each argument */
1457 tev->nargs = pf->pev->nargs;
1458 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1459 if (tev->args == NULL)
1460 return -ENOMEM;
1461 for (i = 0; i < pf->pev->nargs; i++) {
1462 pf->pvar = &pf->pev->args[i];
1463 pf->tvar = &tev->args[i];
1464 ret = find_variable(sp_die, pf);
1465 if (ret != 0)
1466 return ret;
1467 }
1468
1469 return 0;
1470}
1471
1472/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1473int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1474 struct probe_trace_event **tevs, int max_tevs)
1475{
1476 struct trace_event_finder tf = {
1477 .pf = {.pev = pev, .callback = add_probe_trace_event},
1478 .max_tevs = max_tevs};
1479 int ret;
1480
1481 /* Allocate result tevs array */
1482 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1483 if (*tevs == NULL)
1484 return -ENOMEM;
1485
1486 tf.tevs = *tevs;
1487 tf.ntevs = 0;
1488
1489 ret = find_probes(fd, &tf.pf);
1490 if (ret < 0) {
1491 free(*tevs);
1492 *tevs = NULL;
1493 return ret;
1494 }
1495
1496 return (ret < 0) ? ret : tf.ntevs;
1497}
1498
1499#define MAX_VAR_LEN 64
1500
1501/* Collect available variables in this scope */
1502static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1503{
1504 struct available_var_finder *af = data;
1505 struct variable_list *vl;
1506 char buf[MAX_VAR_LEN];
1507 int tag, ret;
1508
1509 vl = &af->vls[af->nvls - 1];
1510
1511 tag = dwarf_tag(die_mem);
1512 if (tag == DW_TAG_formal_parameter ||
1513 tag == DW_TAG_variable) {
1514 ret = convert_variable_location(die_mem, af->pf.addr,
1515 af->pf.fb_ops, NULL);
1516 if (ret == 0) {
1517 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001518 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001519 if (ret > 0)
1520 strlist__add(vl->vars, buf);
1521 }
1522 }
1523
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001524 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001525 return DIE_FIND_CB_CONTINUE;
1526 else
1527 return DIE_FIND_CB_SIBLING;
1528}
1529
1530/* Add a found vars into available variables list */
1531static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1532{
1533 struct available_var_finder *af =
1534 container_of(pf, struct available_var_finder, pf);
1535 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001536 Dwarf_Die die_mem, *scopes = NULL;
1537 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001538
1539 /* Check number of tevs */
1540 if (af->nvls == af->max_vls) {
1541 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1542 return -ERANGE;
1543 }
1544 vl = &af->vls[af->nvls++];
1545
1546 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1547 &vl->point);
1548 if (ret < 0)
1549 return ret;
1550
1551 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1552 vl->point.offset);
1553
1554 /* Find local variables */
1555 vl->vars = strlist__new(true, NULL);
1556 if (vl->vars == NULL)
1557 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001558 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001559 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1560
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001561 /* Find external variables */
1562 if (!af->externs)
1563 goto out;
1564 /* Don't need to search child DIE for externs. */
1565 af->child = false;
1566 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1567 while (nscopes-- > 1)
1568 die_find_child(&scopes[nscopes], collect_variables_cb,
1569 (void *)af, &die_mem);
1570 if (scopes)
1571 free(scopes);
1572
1573out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001574 if (strlist__empty(vl->vars)) {
1575 strlist__delete(vl->vars);
1576 vl->vars = NULL;
1577 }
1578
1579 return ret;
1580}
1581
1582/* Find available variables at given probe point */
1583int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001584 struct variable_list **vls, int max_vls,
1585 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001586{
1587 struct available_var_finder af = {
1588 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001589 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001590 int ret;
1591
1592 /* Allocate result vls array */
1593 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1594 if (*vls == NULL)
1595 return -ENOMEM;
1596
1597 af.vls = *vls;
1598 af.nvls = 0;
1599
1600 ret = find_probes(fd, &af.pf);
1601 if (ret < 0) {
1602 /* Free vlist for error */
1603 while (af.nvls--) {
1604 if (af.vls[af.nvls].point.symbol)
1605 free(af.vls[af.nvls].point.symbol);
1606 if (af.vls[af.nvls].vars)
1607 strlist__delete(af.vls[af.nvls].vars);
1608 }
1609 free(af.vls);
1610 *vls = NULL;
1611 return ret;
1612 }
1613
1614 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001615}
1616
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001617/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001618int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001619{
1620 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001621 Dwarf *dbg = NULL;
1622 Dwfl *dwfl = NULL;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001623 Dwarf_Line *line;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001624 Dwarf_Addr laddr, eaddr, bias = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001625 const char *tmp;
1626 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001627 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001628
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001629 /* Open the live linux kernel */
1630 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1631 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001632 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001633 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1634 ret = -EINVAL;
1635 goto end;
1636 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001637
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001638 /* Adjust address with bias */
1639 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001640 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001641 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001642 pr_warning("Failed to find debug information for address %lx\n",
1643 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001644 ret = -EINVAL;
1645 goto end;
1646 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001647
1648 /* Find a corresponding line */
1649 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1650 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001651 if (dwarf_lineaddr(line, &laddr) == 0 &&
1652 (Dwarf_Addr)addr == laddr &&
1653 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001654 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001655 if (tmp) {
1656 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001657 ppt->file = strdup(tmp);
1658 if (ppt->file == NULL) {
1659 ret = -ENOMEM;
1660 goto end;
1661 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001662 found = true;
1663 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001664 }
1665 }
1666
1667 /* Find a corresponding function */
1668 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1669 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001670 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001671 goto end;
1672
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001673 if (ppt->line) {
1674 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1675 &indie)) {
1676 /* addr in an inline function */
1677 tmp = dwarf_diename(&indie);
1678 if (!tmp)
1679 goto end;
1680 ret = dwarf_decl_line(&indie, &lineno);
1681 } else {
1682 if (eaddr == addr) { /* Function entry */
1683 lineno = ppt->line;
1684 ret = 0;
1685 } else
1686 ret = dwarf_decl_line(&spdie, &lineno);
1687 }
1688 if (ret == 0) {
1689 /* Make a relative line number */
1690 ppt->line -= lineno;
1691 goto found;
1692 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001693 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001694 /* We don't have a line number, let's use offset */
1695 ppt->offset = addr - (unsigned long)eaddr;
1696found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001697 ppt->function = strdup(tmp);
1698 if (ppt->function == NULL) {
1699 ret = -ENOMEM;
1700 goto end;
1701 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001702 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001703 }
1704
1705end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001706 if (dwfl)
1707 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001708 if (ret >= 0)
1709 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001710 return ret;
1711}
1712
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001713/* Add a line and store the src path */
1714static int line_range_add_line(const char *src, unsigned int lineno,
1715 struct line_range *lr)
1716{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001717 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001718 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001719 lr->path = strdup(src);
1720 if (lr->path == NULL)
1721 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001722 }
1723 return line_list__add_line(&lr->line_list, lineno);
1724}
1725
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001726static int line_range_walk_cb(const char *fname, int lineno,
1727 Dwarf_Addr addr __used,
1728 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001729{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001730 struct line_finder *lf = data;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001731
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001732 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001733 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001734 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001735
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001736 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1737 return -EINVAL;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001738
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001739 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001740}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001741
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001742/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001743static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001744{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001745 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001746
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001747 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001748
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001749 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001750 if (ret >= 0)
1751 if (!list_empty(&lf->lr->line_list))
1752 ret = lf->found = 1;
1753 else
1754 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001755 else {
1756 free(lf->lr->path);
1757 lf->lr->path = NULL;
1758 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001759 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001760}
1761
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001762static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1763{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001764 struct dwarf_callback_param *param = data;
1765
1766 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001767 return DWARF_CB_ABORT; /* No need to find other instances */
1768}
1769
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001770/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001771static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001772{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001773 struct dwarf_callback_param *param = data;
1774 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001775 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001776
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001777 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001778 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001779 lf->fname = dwarf_decl_file(sp_die);
1780 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001781 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001782 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001783 if (lf->lno_s < 0) /* Overflow */
1784 lf->lno_s = INT_MAX;
1785 lf->lno_e = lr->offset + lr->end;
1786 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001787 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001788 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001789 lr->start = lf->lno_s;
1790 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001791 if (dwarf_func_inline(sp_die)) {
1792 struct dwarf_callback_param _param;
1793 _param.data = (void *)lf;
1794 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001795 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001796 line_range_inline_cb,
1797 &_param);
1798 param->retval = _param.retval;
1799 } else
1800 param->retval = find_line_range_by_line(sp_die, lf);
1801 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001802 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001803 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001804}
1805
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001806static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001807{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001808 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1809 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1810 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001811}
1812
1813int find_line_range(int fd, struct line_range *lr)
1814{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001815 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001816 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001817 Dwarf_Off off = 0, noff;
1818 size_t cuhl;
1819 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001820 Dwarf *dbg = NULL;
1821 Dwfl *dwfl;
1822 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001823 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001824
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001825 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001826 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001827 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001828 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1829 return -EBADF;
1830 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001831
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001832 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001833 while (!lf.found && ret >= 0) {
1834 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001835 break;
1836
1837 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001838 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1839 if (!diep)
1840 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001841
1842 /* Check if target file is included. */
1843 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001844 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001845 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001846 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001847
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001848 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001849 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001850 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001851 else {
1852 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001853 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001854 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001855 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001856 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001857 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001858 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001859
1860 /* Store comp_dir */
1861 if (lf.found) {
1862 comp_dir = cu_get_comp_dir(&lf.cu_die);
1863 if (comp_dir) {
1864 lr->comp_dir = strdup(comp_dir);
1865 if (!lr->comp_dir)
1866 ret = -ENOMEM;
1867 }
1868 }
1869
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001870 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001871 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001872 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001873}
1874