blob: 46addfb3183efd3151b5cc75d8a128a0015c4317 [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 Hiramatsu5069ed82011-01-13 21:46:05 +0900283/* Get callsite line number of inline-function instance */
284static int die_get_call_lineno(Dwarf_Die *in_die)
285{
286 Dwarf_Attribute attr;
287 Dwarf_Word ret;
288
289 if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
290 return -ENOENT;
291
292 dwarf_formudata(&attr, &ret);
293 return (int)ret;
294}
295
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900296/* Get type die */
297static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
298{
299 Dwarf_Attribute attr;
300
301 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
302 dwarf_formref_die(&attr, die_mem))
303 return die_mem;
304 else
305 return NULL;
306}
307
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900308/* Get a type die, but skip qualifiers */
309static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400310{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400311 int tag;
312
313 do {
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900314 vr_die = die_get_type(vr_die, die_mem);
315 if (!vr_die)
316 break;
317 tag = dwarf_tag(vr_die);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400318 } while (tag == DW_TAG_const_type ||
319 tag == DW_TAG_restrict_type ||
320 tag == DW_TAG_volatile_type ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900321 tag == DW_TAG_shared_type);
322
323 return vr_die;
324}
325
326/* Get a type die, but skip qualifiers and typedef */
327static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
328{
329 do {
330 vr_die = __die_get_real_type(vr_die, die_mem);
331 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400332
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900333 return vr_die;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400334}
335
Masami Hiramatsu49849122010-04-12 13:17:15 -0400336static bool die_is_signed_type(Dwarf_Die *tp_die)
337{
338 Dwarf_Attribute attr;
339 Dwarf_Word ret;
340
341 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
342 dwarf_formudata(&attr, &ret) != 0)
343 return false;
344
345 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
346 ret == DW_ATE_signed_fixed);
347}
348
349static int die_get_byte_size(Dwarf_Die *tp_die)
350{
351 Dwarf_Attribute attr;
352 Dwarf_Word ret;
353
354 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
355 dwarf_formudata(&attr, &ret) != 0)
356 return 0;
357
358 return (int)ret;
359}
360
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300361/* Get data_member_location offset */
362static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
363{
364 Dwarf_Attribute attr;
365 Dwarf_Op *expr;
366 size_t nexpr;
367 int ret;
368
369 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
370 return -ENOENT;
371
372 if (dwarf_formudata(&attr, offs) != 0) {
373 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
374 ret = dwarf_getlocation(&attr, &expr, &nexpr);
375 if (ret < 0 || nexpr == 0)
376 return -ENOENT;
377
378 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
379 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
380 expr[0].atom, nexpr);
381 return -ENOTSUP;
382 }
383 *offs = (Dwarf_Word)expr[0].number;
384 }
385 return 0;
386}
387
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400388/* Return values for die_find callbacks */
389enum {
390 DIE_FIND_CB_FOUND = 0, /* End of Search */
391 DIE_FIND_CB_CHILD = 1, /* Search only children */
392 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
393 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
394};
395
396/* Search a child die */
397static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
398 int (*callback)(Dwarf_Die *, void *),
399 void *data, Dwarf_Die *die_mem)
400{
401 Dwarf_Die child_die;
402 int ret;
403
404 ret = dwarf_child(rt_die, die_mem);
405 if (ret != 0)
406 return NULL;
407
408 do {
409 ret = callback(die_mem, data);
410 if (ret == DIE_FIND_CB_FOUND)
411 return die_mem;
412
413 if ((ret & DIE_FIND_CB_CHILD) &&
414 die_find_child(die_mem, callback, data, &child_die)) {
415 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
416 return die_mem;
417 }
418 } while ((ret & DIE_FIND_CB_SIBLING) &&
419 dwarf_siblingof(die_mem, die_mem) == 0);
420
421 return NULL;
422}
423
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500424struct __addr_die_search_param {
425 Dwarf_Addr addr;
426 Dwarf_Die *die_mem;
427};
428
429static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
430{
431 struct __addr_die_search_param *ad = data;
432
433 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
434 dwarf_haspc(fn_die, ad->addr)) {
435 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
436 return DWARF_CB_ABORT;
437 }
438 return DWARF_CB_OK;
439}
440
441/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400442static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
443 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500444{
445 struct __addr_die_search_param ad;
446 ad.addr = addr;
447 ad.die_mem = die_mem;
448 /* dwarf_getscopes can't find subprogram. */
449 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
450 return NULL;
451 else
452 return die_mem;
453}
454
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400455/* die_find callback for inline function search */
456static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
457{
458 Dwarf_Addr *addr = data;
459
460 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
461 dwarf_haspc(die_mem, *addr))
462 return DIE_FIND_CB_FOUND;
463
464 return DIE_FIND_CB_CONTINUE;
465}
466
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500467/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400468static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
469 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500470{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400471 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500472}
473
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900474/* Walker on lines (Note: line number will not be sorted) */
475typedef int (* line_walk_handler_t) (const char *fname, int lineno,
476 Dwarf_Addr addr, void *data);
477
478struct __line_walk_param {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900479 const char *fname;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900480 line_walk_handler_t handler;
481 void *data;
482 int retval;
483};
484
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900485static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
486{
487 struct __line_walk_param *lw = data;
488 Dwarf_Addr addr;
489 int lineno;
490
491 if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
492 lineno = die_get_call_lineno(in_die);
493 if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
494 lw->retval = lw->handler(lw->fname, lineno, addr,
495 lw->data);
496 if (lw->retval != 0)
497 return DIE_FIND_CB_FOUND;
498 }
499 }
500 return DIE_FIND_CB_SIBLING;
501}
502
503/* Walk on lines of blocks included in given DIE */
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900504static int __die_walk_funclines(Dwarf_Die *sp_die,
505 line_walk_handler_t handler, void *data)
506{
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900507 struct __line_walk_param lw = {
508 .handler = handler,
509 .data = data,
510 .retval = 0,
511 };
512 Dwarf_Die die_mem;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900513 Dwarf_Addr addr;
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900514 int lineno;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900515
516 /* Handle function declaration line */
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900517 lw.fname = dwarf_decl_file(sp_die);
518 if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900519 dwarf_entrypc(sp_die, &addr) == 0) {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900520 lw.retval = handler(lw.fname, lineno, addr, data);
521 if (lw.retval != 0)
522 goto done;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900523 }
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900524 die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
525done:
526 return lw.retval;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900527}
528
529static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
530{
531 struct __line_walk_param *lw = data;
532
533 lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
534 if (lw->retval != 0)
535 return DWARF_CB_ABORT;
536
537 return DWARF_CB_OK;
538}
539
540/*
541 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
542 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
543 */
544static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
545 void *data)
546{
547 Dwarf_Lines *lines;
548 Dwarf_Line *line;
549 Dwarf_Addr addr;
550 const char *fname;
551 int lineno, ret = 0;
552 Dwarf_Die die_mem, *cu_die;
553 size_t nlines, i;
554
555 /* Get the CU die */
556 if (dwarf_tag(pdie) == DW_TAG_subprogram)
557 cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
558 else
559 cu_die = pdie;
560 if (!cu_die) {
561 pr_debug2("Failed to get CU from subprogram\n");
562 return -EINVAL;
563 }
564
565 /* Get lines list in the CU */
566 if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
567 pr_debug2("Failed to get source lines on this CU.\n");
568 return -ENOENT;
569 }
570 pr_debug2("Get %zd lines from this CU\n", nlines);
571
572 /* Walk on the lines on lines list */
573 for (i = 0; i < nlines; i++) {
574 line = dwarf_onesrcline(lines, i);
575 if (line == NULL ||
576 dwarf_lineno(line, &lineno) != 0 ||
577 dwarf_lineaddr(line, &addr) != 0) {
578 pr_debug2("Failed to get line info. "
579 "Possible error in debuginfo.\n");
580 continue;
581 }
582 /* Filter lines based on address */
583 if (pdie != cu_die)
584 /*
585 * Address filtering
586 * The line is included in given function, and
587 * no inline block includes it.
588 */
589 if (!dwarf_haspc(pdie, addr) ||
590 die_find_inlinefunc(pdie, addr, &die_mem))
591 continue;
592 /* Get source line */
593 fname = dwarf_linesrc(line, NULL, NULL);
594
595 ret = handler(fname, lineno, addr, data);
596 if (ret != 0)
597 return ret;
598 }
599
600 /*
601 * Dwarf lines doesn't include function declarations and inlined
602 * subroutines. We have to check functions list or given function.
603 */
604 if (pdie != cu_die)
605 ret = __die_walk_funclines(pdie, handler, data);
606 else {
607 struct __line_walk_param param = {
608 .handler = handler,
609 .data = data,
610 .retval = 0,
611 };
612 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
613 ret = param.retval;
614 }
615
616 return ret;
617}
618
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900619struct __find_variable_param {
620 const char *name;
621 Dwarf_Addr addr;
622};
623
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400624static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400625{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900626 struct __find_variable_param *fvp = data;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400627 int tag;
628
629 tag = dwarf_tag(die_mem);
630 if ((tag == DW_TAG_formal_parameter ||
631 tag == DW_TAG_variable) &&
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900632 die_compare_name(die_mem, fvp->name))
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400633 return DIE_FIND_CB_FOUND;
634
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900635 if (dwarf_haspc(die_mem, fvp->addr))
636 return DIE_FIND_CB_CONTINUE;
637 else
638 return DIE_FIND_CB_SIBLING;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400639}
640
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900641/* Find a variable called 'name' at given address */
642static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
643 Dwarf_Addr addr, Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500644{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900645 struct __find_variable_param fvp = { .name = name, .addr = addr};
646
647 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400648 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400649}
650
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400651static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
652{
653 const char *name = data;
654
655 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900656 die_compare_name(die_mem, name))
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400657 return DIE_FIND_CB_FOUND;
658
659 return DIE_FIND_CB_SIBLING;
660}
661
662/* Find a member called 'name' */
663static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
664 Dwarf_Die *die_mem)
665{
666 return die_find_child(st_die, __die_find_member_cb, (void *)name,
667 die_mem);
668}
669
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900670/* Get the name of given variable DIE */
671static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
672{
673 Dwarf_Die type;
674 int tag, ret, ret2;
675 const char *tmp = "";
676
677 if (__die_get_real_type(vr_die, &type) == NULL)
678 return -ENOENT;
679
680 tag = dwarf_tag(&type);
681 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
682 tmp = "*";
683 else if (tag == DW_TAG_subroutine_type) {
684 /* Function pointer */
685 ret = snprintf(buf, len, "(function_type)");
686 return (ret >= len) ? -E2BIG : ret;
687 } else {
688 if (!dwarf_diename(&type))
689 return -ENOENT;
690 if (tag == DW_TAG_union_type)
691 tmp = "union ";
692 else if (tag == DW_TAG_structure_type)
693 tmp = "struct ";
694 /* Write a base name */
695 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
696 return (ret >= len) ? -E2BIG : ret;
697 }
698 ret = die_get_typename(&type, buf, len);
699 if (ret > 0) {
700 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
701 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
702 }
703 return ret;
704}
705
706/* Get the name and type of given variable DIE, stored as "type\tname" */
707static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
708{
709 int ret, ret2;
710
711 ret = die_get_typename(vr_die, buf, len);
712 if (ret < 0) {
713 pr_debug("Failed to get type, make it unknown.\n");
714 ret = snprintf(buf, len, "(unknown_type)");
715 }
716 if (ret > 0) {
717 ret2 = snprintf(buf + ret, len - ret, "\t%s",
718 dwarf_diename(vr_die));
719 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
720 }
721 return ret;
722}
723
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400724/*
725 * Probe finder related functions
726 */
727
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530728static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400729{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530730 struct probe_trace_arg_ref *ref;
731 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400732 if (ref != NULL)
733 ref->offset = offs;
734 return ref;
735}
736
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900737/*
738 * Convert a location into trace_arg.
739 * If tvar == NULL, this just checks variable can be converted.
740 */
741static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
742 Dwarf_Op *fb_ops,
743 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400744{
745 Dwarf_Attribute attr;
746 Dwarf_Op *op;
747 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500748 unsigned int regn;
749 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400750 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400751 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400752 int ret;
753
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900754 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
755 goto static_var;
756
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400757 /* TODO: handle more than 1 exprs */
758 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900759 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400760 nops == 0) {
761 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400762 return -ENOENT;
763 }
764
765 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900766static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900767 if (!tvar)
768 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400769 /* Static variables on memory (not stack), make @varname */
770 ret = strlen(dwarf_diename(vr_die));
771 tvar->value = zalloc(ret + 2);
772 if (tvar->value == NULL)
773 return -ENOMEM;
774 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
775 tvar->ref = alloc_trace_arg_ref((long)offs);
776 if (tvar->ref == NULL)
777 return -ENOMEM;
778 return 0;
779 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400780
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400781 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500782 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900783 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400784 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400785 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500786 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900787 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500788 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400789
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500790 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
791 regn = op->atom - DW_OP_breg0;
792 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400793 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500794 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
795 regn = op->atom - DW_OP_reg0;
796 } else if (op->atom == DW_OP_bregx) {
797 regn = op->number;
798 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400799 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500800 } else if (op->atom == DW_OP_regx) {
801 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400802 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900803 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400804 return -ENOTSUP;
805 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400806
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900807 if (!tvar)
808 return 0;
809
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400810 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400811 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900812 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900813 pr_warning("Mapping for the register number %u "
814 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400815 return -ERANGE;
816 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400817
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400818 tvar->value = strdup(regs);
819 if (tvar->value == NULL)
820 return -ENOMEM;
821
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400822 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400823 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400824 if (tvar->ref == NULL)
825 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400826 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400827 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400828}
829
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400830static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530831 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400832 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400833{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530834 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400835 Dwarf_Die type;
836 char buf[16];
837 int ret;
838
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400839 /* TODO: check all types */
840 if (cast && strcmp(cast, "string") != 0) {
841 /* Non string type is OK */
842 tvar->type = strdup(cast);
843 return (tvar->type == NULL) ? -ENOMEM : 0;
844 }
845
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400846 if (die_get_real_type(vr_die, &type) == NULL) {
847 pr_warning("Failed to get a type information of %s.\n",
848 dwarf_diename(vr_die));
849 return -ENOENT;
850 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400851
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400852 pr_debug("%s type is %s.\n",
853 dwarf_diename(vr_die), dwarf_diename(&type));
854
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400855 if (cast && strcmp(cast, "string") == 0) { /* String type */
856 ret = dwarf_tag(&type);
857 if (ret != DW_TAG_pointer_type &&
858 ret != DW_TAG_array_type) {
859 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900860 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400861 dwarf_diename(vr_die), dwarf_diename(&type));
862 return -EINVAL;
863 }
864 if (ret == DW_TAG_pointer_type) {
865 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900866 pr_warning("Failed to get a type"
867 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400868 return -ENOENT;
869 }
870 while (*ref_ptr)
871 ref_ptr = &(*ref_ptr)->next;
872 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530873 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400874 if (*ref_ptr == NULL) {
875 pr_warning("Out of memory error\n");
876 return -ENOMEM;
877 }
878 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900879 if (!die_compare_name(&type, "char") &&
880 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400881 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900882 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400883 dwarf_diename(vr_die));
884 return -EINVAL;
885 }
886 tvar->type = strdup(cast);
887 return (tvar->type == NULL) ? -ENOMEM : 0;
888 }
889
Masami Hiramatsu49849122010-04-12 13:17:15 -0400890 ret = die_get_byte_size(&type) * 8;
891 if (ret) {
892 /* Check the bitwidth */
893 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400894 pr_info("%s exceeds max-bitwidth."
895 " Cut down to %d bits.\n",
896 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400897 ret = MAX_BASIC_TYPE_BITS;
898 }
899
900 ret = snprintf(buf, 16, "%c%d",
901 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400902 if (ret < 0 || ret >= 16) {
903 if (ret >= 16)
904 ret = -E2BIG;
905 pr_warning("Failed to convert variable type: %s\n",
906 strerror(-ret));
907 return ret;
908 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400909 tvar->type = strdup(buf);
910 if (tvar->type == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400911 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400912 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400913 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400914}
915
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400916static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400917 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530918 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400919 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400920{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530921 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400922 Dwarf_Die type;
923 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400924 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400925
926 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400927 if (die_get_real_type(vr_die, &type) == NULL) {
928 pr_warning("Failed to get the type of %s.\n", varname);
929 return -ENOENT;
930 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400931 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
932 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400933
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400934 if (field->name[0] == '[' &&
935 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
936 if (field->next)
937 /* Save original type for next field */
938 memcpy(die_mem, &type, sizeof(*die_mem));
939 /* Get the type of this array */
940 if (die_get_real_type(&type, &type) == NULL) {
941 pr_warning("Failed to get the type of %s.\n", varname);
942 return -ENOENT;
943 }
944 pr_debug2("Array real type: (%x)\n",
945 (unsigned)dwarf_dieoffset(&type));
946 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530947 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400948 if (ref == NULL)
949 return -ENOMEM;
950 if (*ref_ptr)
951 (*ref_ptr)->next = ref;
952 else
953 *ref_ptr = ref;
954 }
955 ref->offset += die_get_byte_size(&type) * field->index;
956 if (!field->next)
957 /* Save vr_die for converting types */
958 memcpy(die_mem, vr_die, sizeof(*die_mem));
959 goto next;
960 } else if (tag == DW_TAG_pointer_type) {
961 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400962 if (!field->ref) {
963 pr_err("Semantic error: %s must be referred by '->'\n",
964 field->name);
965 return -EINVAL;
966 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400967 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400968 if (die_get_real_type(&type, &type) == NULL) {
969 pr_warning("Failed to get the type of %s.\n", varname);
970 return -ENOENT;
971 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400972 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400973 if (dwarf_tag(&type) != DW_TAG_structure_type) {
974 pr_warning("%s is not a data structure.\n", varname);
975 return -EINVAL;
976 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400977
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530978 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400979 if (ref == NULL)
980 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400981 if (*ref_ptr)
982 (*ref_ptr)->next = ref;
983 else
984 *ref_ptr = ref;
985 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400986 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400987 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400988 pr_warning("%s is not a data structure.\n", varname);
989 return -EINVAL;
990 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400991 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900992 pr_err("Semantic error: %s is not a pointor"
993 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400994 return -EINVAL;
995 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400996 if (field->ref) {
997 pr_err("Semantic error: %s must be referred by '.'\n",
998 field->name);
999 return -EINVAL;
1000 }
1001 if (!ref) {
1002 pr_warning("Structure on a register is not "
1003 "supported yet.\n");
1004 return -ENOTSUP;
1005 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001006 }
1007
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001008 if (die_find_member(&type, field->name, die_mem) == NULL) {
1009 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
1010 dwarf_diename(&type), field->name);
1011 return -EINVAL;
1012 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001013
1014 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001015 ret = die_get_data_member_location(die_mem, &offs);
1016 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001017 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001018 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001019 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001020 ref->offset += (long)offs;
1021
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001022next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001023 /* Converting next field */
1024 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001025 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001026 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001027 else
1028 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001029}
1030
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001031/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001032static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001033{
Masami Hiramatsu49849122010-04-12 13:17:15 -04001034 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001035 int ret;
1036
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001037 pr_debug("Converting variable %s into trace event.\n",
1038 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001039
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001040 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
1041 pf->tvar);
1042 if (ret == -ENOENT)
1043 pr_err("Failed to find the location of %s at this address.\n"
1044 " Perhaps, it has been optimized out.\n", pf->pvar->var);
1045 else if (ret == -ENOTSUP)
1046 pr_err("Sorry, we don't support this variable location yet.\n");
1047 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001048 ret = convert_variable_fields(vr_die, pf->pvar->var,
1049 pf->pvar->field, &pf->tvar->ref,
1050 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001051 vr_die = &die_mem;
1052 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -04001053 if (ret == 0)
1054 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001055 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001056 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001057}
1058
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001059/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001060static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001061{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001062 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001063 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001064 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001065
Masami Hiramatsu367e94c2010-08-27 20:38:59 +09001066 if (!is_c_varname(pf->pvar->var)) {
1067 /* Copy raw parameters */
1068 pf->tvar->value = strdup(pf->pvar->var);
1069 if (pf->tvar->value == NULL)
1070 return -ENOMEM;
1071 if (pf->pvar->type) {
1072 pf->tvar->type = strdup(pf->pvar->type);
1073 if (pf->tvar->type == NULL)
1074 return -ENOMEM;
1075 }
1076 if (pf->pvar->name) {
1077 pf->tvar->name = strdup(pf->pvar->name);
1078 if (pf->tvar->name == NULL)
1079 return -ENOMEM;
1080 } else
1081 pf->tvar->name = NULL;
1082 return 0;
1083 }
1084
Masami Hiramatsu48481932010-04-12 13:16:53 -04001085 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001086 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001087 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001088 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
1089 if (ret < 0)
1090 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001091 ptr = strchr(buf, ':'); /* Change type separator to _ */
1092 if (ptr)
1093 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001094 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001095 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001096 if (pf->tvar->name == NULL)
1097 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001098
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001099 pr_debug("Searching '%s' variable in context.\n",
1100 pf->pvar->var);
1101 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +09001102 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001103 ret = convert_variable(&vr_die, pf);
1104 else {
1105 /* Search upper class */
1106 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001107 while (nscopes-- > 1) {
1108 pr_debug("Searching variables in %s\n",
1109 dwarf_diename(&scopes[nscopes]));
1110 /* We should check this scope, so give dummy address */
1111 if (die_find_variable_at(&scopes[nscopes],
1112 pf->pvar->var, 0,
1113 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001114 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001115 goto found;
1116 }
1117 }
1118 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001119 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001120 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001121 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001122found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001123 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001124 pr_warning("Failed to find '%s' in this function.\n",
1125 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001126 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001127}
1128
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001129/* Convert subprogram DIE to trace point */
1130static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
1131 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001132{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001133 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001134 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001135
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001136 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001137 name = dwarf_diename(sp_die);
1138 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001139 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001140 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001141 dwarf_diename(sp_die));
1142 return -ENOENT;
1143 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001144 tp->symbol = strdup(name);
1145 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001146 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001147 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001148 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001149 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001150 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001151
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001152 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001153 if (retprobe) {
1154 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001155 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001156 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001157 return -EINVAL;
1158 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001159 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001160 }
1161
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001162 return 0;
1163}
1164
1165/* Call probe_finder callback with real subprogram DIE */
1166static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
1167{
1168 Dwarf_Die die_mem;
1169 Dwarf_Attribute fb_attr;
1170 size_t nops;
1171 int ret;
1172
1173 /* If no real subprogram, find a real one */
1174 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
1175 sp_die = die_find_real_subprogram(&pf->cu_die,
1176 pf->addr, &die_mem);
1177 if (!sp_die) {
1178 pr_warning("Failed to find probe point in any "
1179 "functions.\n");
1180 return -ENOENT;
1181 }
1182 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001183
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001184 /* Get the frame base attribute/ops */
1185 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -04001186 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001187 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001188 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001189#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001190 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1191 pf->cfi != NULL) {
1192 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001193 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1194 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001195 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001196 (uintmax_t)pf->addr);
1197 return -ENOENT;
1198 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001199#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001200 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001201
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001202 /* Call finder's callback handler */
1203 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001204
1205 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1206 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001207
1208 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001209}
1210
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001211static int probe_point_line_walker(const char *fname, int lineno,
1212 Dwarf_Addr addr, void *data)
1213{
1214 struct probe_finder *pf = data;
1215 int ret;
1216
1217 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
1218 return 0;
1219
1220 pf->addr = addr;
1221 ret = call_probe_finder(NULL, pf);
1222
1223 /* Continue if no error, because the line will be in inline function */
1224 return ret < 0 ?: 0;
1225}
1226
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001227/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001228static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001229{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001230 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001231}
1232
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001233/* Find lines which match lazy pattern */
1234static int find_lazy_match_lines(struct list_head *head,
1235 const char *fname, const char *pat)
1236{
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001237 FILE *fp;
1238 char *line = NULL;
1239 size_t line_len;
1240 ssize_t len;
1241 int count = 0, linenum = 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001242
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001243 fp = fopen(fname, "r");
1244 if (!fp) {
1245 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001246 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001247 }
1248
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001249 while ((len = getline(&line, &line_len, fp)) > 0) {
1250
1251 if (line[len - 1] == '\n')
1252 line[len - 1] = '\0';
1253
1254 if (strlazymatch(line, pat)) {
1255 line_list__add_line(head, linenum);
1256 count++;
1257 }
1258 linenum++;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001259 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001260
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001261 if (ferror(fp))
1262 count = -errno;
1263 free(line);
1264 fclose(fp);
1265
1266 if (count == 0)
1267 pr_debug("No matched lines found in %s.\n", fname);
1268 return count;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001269}
1270
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001271static int probe_point_lazy_walker(const char *fname, int lineno,
1272 Dwarf_Addr addr, void *data)
1273{
1274 struct probe_finder *pf = data;
1275 int ret;
1276
1277 if (!line_list__has_line(&pf->lcache, lineno) ||
1278 strtailcmp(fname, pf->fname) != 0)
1279 return 0;
1280
1281 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1282 lineno, (unsigned long long)addr);
1283 pf->addr = addr;
1284 ret = call_probe_finder(NULL, pf);
1285
1286 /*
1287 * Continue if no error, because the lazy pattern will match
1288 * to other lines
1289 */
1290 return ret < 0 ?: 0;
1291}
1292
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001293/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001294static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001295{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001296 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001297
1298 if (list_empty(&pf->lcache)) {
1299 /* Matching lazy line pattern */
1300 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001301 pf->pev->point.lazy_line);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001302 if (ret <= 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001303 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001304 }
1305
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001306 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001307}
1308
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001309/* Callback parameter with return value */
1310struct dwarf_callback_param {
1311 void *data;
1312 int retval;
1313};
1314
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001315static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001316{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001317 struct dwarf_callback_param *param = data;
1318 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001319 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001320 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001321
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001322 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001323 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001324 else {
1325 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001326 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001327 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001328 dwarf_diename(in_die));
1329 param->retval = -ENOENT;
1330 return DWARF_CB_ABORT;
1331 }
1332 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001333 pf->addr += pp->offset;
1334 pr_debug("found inline addr: 0x%jx\n",
1335 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001336
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001337 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001338 if (param->retval < 0)
1339 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001340 }
1341
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001342 return DWARF_CB_OK;
1343}
1344
1345/* Search function from function name */
1346static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1347{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001348 struct dwarf_callback_param *param = data;
1349 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001350 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001351
1352 /* Check tag and diename */
1353 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001354 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001355 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001356
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001357 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001358 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001359 dwarf_decl_line(sp_die, &pf->lno);
1360 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001361 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001362 } else if (!dwarf_func_inline(sp_die)) {
1363 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001364 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001365 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001366 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001367 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001368 pr_warning("Failed to get entry address of "
1369 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001370 param->retval = -ENOENT;
1371 return DWARF_CB_ABORT;
1372 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001373 pf->addr += pp->offset;
1374 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001375 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001376 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001377 } else {
1378 struct dwarf_callback_param _param = {.data = (void *)pf,
1379 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001380 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001381 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1382 &_param);
1383 param->retval = _param.retval;
1384 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001385
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001386 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001387}
1388
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001389static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001390{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001391 struct dwarf_callback_param _param = {.data = (void *)pf,
1392 .retval = 0};
1393 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1394 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001395}
1396
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001397/* Find probe points from debuginfo */
1398static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001399{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001400 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001401 Dwarf_Off off, noff;
1402 size_t cuhl;
1403 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001404 Dwarf *dbg = NULL;
1405 Dwfl *dwfl;
1406 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001407 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001408
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001409 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001410 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001411 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001412 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1413 return -EBADF;
1414 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001415
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001416#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001417 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001418 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001419#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001420
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001421 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001422 line_list__init(&pf->lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001423 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001424 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1425 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001426 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001427 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001428 if (!diep)
1429 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001430
1431 /* Check if target file is included. */
1432 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001433 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001434 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001435 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001436
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001437 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001438 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001439 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001440 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001441 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001442 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001443 pf->lno = pp->line;
1444 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001445 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001446 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001447 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001448 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001449 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001450 if (dwfl)
1451 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001452
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001453 return ret;
1454}
1455
1456/* Add a found probe point into trace event list */
1457static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1458{
1459 struct trace_event_finder *tf =
1460 container_of(pf, struct trace_event_finder, pf);
1461 struct probe_trace_event *tev;
1462 int ret, i;
1463
1464 /* Check number of tevs */
1465 if (tf->ntevs == tf->max_tevs) {
1466 pr_warning("Too many( > %d) probe point found.\n",
1467 tf->max_tevs);
1468 return -ERANGE;
1469 }
1470 tev = &tf->tevs[tf->ntevs++];
1471
1472 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1473 &tev->point);
1474 if (ret < 0)
1475 return ret;
1476
1477 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1478 tev->point.offset);
1479
1480 /* Find each argument */
1481 tev->nargs = pf->pev->nargs;
1482 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1483 if (tev->args == NULL)
1484 return -ENOMEM;
1485 for (i = 0; i < pf->pev->nargs; i++) {
1486 pf->pvar = &pf->pev->args[i];
1487 pf->tvar = &tev->args[i];
1488 ret = find_variable(sp_die, pf);
1489 if (ret != 0)
1490 return ret;
1491 }
1492
1493 return 0;
1494}
1495
1496/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1497int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1498 struct probe_trace_event **tevs, int max_tevs)
1499{
1500 struct trace_event_finder tf = {
1501 .pf = {.pev = pev, .callback = add_probe_trace_event},
1502 .max_tevs = max_tevs};
1503 int ret;
1504
1505 /* Allocate result tevs array */
1506 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1507 if (*tevs == NULL)
1508 return -ENOMEM;
1509
1510 tf.tevs = *tevs;
1511 tf.ntevs = 0;
1512
1513 ret = find_probes(fd, &tf.pf);
1514 if (ret < 0) {
1515 free(*tevs);
1516 *tevs = NULL;
1517 return ret;
1518 }
1519
1520 return (ret < 0) ? ret : tf.ntevs;
1521}
1522
1523#define MAX_VAR_LEN 64
1524
1525/* Collect available variables in this scope */
1526static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1527{
1528 struct available_var_finder *af = data;
1529 struct variable_list *vl;
1530 char buf[MAX_VAR_LEN];
1531 int tag, ret;
1532
1533 vl = &af->vls[af->nvls - 1];
1534
1535 tag = dwarf_tag(die_mem);
1536 if (tag == DW_TAG_formal_parameter ||
1537 tag == DW_TAG_variable) {
1538 ret = convert_variable_location(die_mem, af->pf.addr,
1539 af->pf.fb_ops, NULL);
1540 if (ret == 0) {
1541 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001542 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001543 if (ret > 0)
1544 strlist__add(vl->vars, buf);
1545 }
1546 }
1547
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001548 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001549 return DIE_FIND_CB_CONTINUE;
1550 else
1551 return DIE_FIND_CB_SIBLING;
1552}
1553
1554/* Add a found vars into available variables list */
1555static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1556{
1557 struct available_var_finder *af =
1558 container_of(pf, struct available_var_finder, pf);
1559 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001560 Dwarf_Die die_mem, *scopes = NULL;
1561 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001562
1563 /* Check number of tevs */
1564 if (af->nvls == af->max_vls) {
1565 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1566 return -ERANGE;
1567 }
1568 vl = &af->vls[af->nvls++];
1569
1570 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1571 &vl->point);
1572 if (ret < 0)
1573 return ret;
1574
1575 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1576 vl->point.offset);
1577
1578 /* Find local variables */
1579 vl->vars = strlist__new(true, NULL);
1580 if (vl->vars == NULL)
1581 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001582 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001583 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1584
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001585 /* Find external variables */
1586 if (!af->externs)
1587 goto out;
1588 /* Don't need to search child DIE for externs. */
1589 af->child = false;
1590 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1591 while (nscopes-- > 1)
1592 die_find_child(&scopes[nscopes], collect_variables_cb,
1593 (void *)af, &die_mem);
1594 if (scopes)
1595 free(scopes);
1596
1597out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001598 if (strlist__empty(vl->vars)) {
1599 strlist__delete(vl->vars);
1600 vl->vars = NULL;
1601 }
1602
1603 return ret;
1604}
1605
1606/* Find available variables at given probe point */
1607int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001608 struct variable_list **vls, int max_vls,
1609 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001610{
1611 struct available_var_finder af = {
1612 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001613 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001614 int ret;
1615
1616 /* Allocate result vls array */
1617 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1618 if (*vls == NULL)
1619 return -ENOMEM;
1620
1621 af.vls = *vls;
1622 af.nvls = 0;
1623
1624 ret = find_probes(fd, &af.pf);
1625 if (ret < 0) {
1626 /* Free vlist for error */
1627 while (af.nvls--) {
1628 if (af.vls[af.nvls].point.symbol)
1629 free(af.vls[af.nvls].point.symbol);
1630 if (af.vls[af.nvls].vars)
1631 strlist__delete(af.vls[af.nvls].vars);
1632 }
1633 free(af.vls);
1634 *vls = NULL;
1635 return ret;
1636 }
1637
1638 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001639}
1640
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001641/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001642int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001643{
1644 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001645 Dwarf *dbg = NULL;
1646 Dwfl *dwfl = NULL;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001647 Dwarf_Line *line;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001648 Dwarf_Addr laddr, eaddr, bias = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001649 const char *tmp;
1650 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001651 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001652
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001653 /* Open the live linux kernel */
1654 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1655 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001656 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001657 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1658 ret = -EINVAL;
1659 goto end;
1660 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001661
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001662 /* Adjust address with bias */
1663 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001664 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001665 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001666 pr_warning("Failed to find debug information for address %lx\n",
1667 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001668 ret = -EINVAL;
1669 goto end;
1670 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001671
1672 /* Find a corresponding line */
1673 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1674 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001675 if (dwarf_lineaddr(line, &laddr) == 0 &&
1676 (Dwarf_Addr)addr == laddr &&
1677 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001678 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001679 if (tmp) {
1680 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001681 ppt->file = strdup(tmp);
1682 if (ppt->file == NULL) {
1683 ret = -ENOMEM;
1684 goto end;
1685 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001686 found = true;
1687 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001688 }
1689 }
1690
1691 /* Find a corresponding function */
1692 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1693 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001694 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001695 goto end;
1696
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001697 if (ppt->line) {
1698 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1699 &indie)) {
1700 /* addr in an inline function */
1701 tmp = dwarf_diename(&indie);
1702 if (!tmp)
1703 goto end;
1704 ret = dwarf_decl_line(&indie, &lineno);
1705 } else {
1706 if (eaddr == addr) { /* Function entry */
1707 lineno = ppt->line;
1708 ret = 0;
1709 } else
1710 ret = dwarf_decl_line(&spdie, &lineno);
1711 }
1712 if (ret == 0) {
1713 /* Make a relative line number */
1714 ppt->line -= lineno;
1715 goto found;
1716 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001717 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001718 /* We don't have a line number, let's use offset */
1719 ppt->offset = addr - (unsigned long)eaddr;
1720found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001721 ppt->function = strdup(tmp);
1722 if (ppt->function == NULL) {
1723 ret = -ENOMEM;
1724 goto end;
1725 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001726 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001727 }
1728
1729end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001730 if (dwfl)
1731 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001732 if (ret >= 0)
1733 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001734 return ret;
1735}
1736
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001737/* Add a line and store the src path */
1738static int line_range_add_line(const char *src, unsigned int lineno,
1739 struct line_range *lr)
1740{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001741 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001742 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001743 lr->path = strdup(src);
1744 if (lr->path == NULL)
1745 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001746 }
1747 return line_list__add_line(&lr->line_list, lineno);
1748}
1749
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001750static int line_range_walk_cb(const char *fname, int lineno,
1751 Dwarf_Addr addr __used,
1752 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001753{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001754 struct line_finder *lf = data;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001755
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001756 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001757 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001758 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001759
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001760 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1761 return -EINVAL;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001762
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001763 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001764}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001765
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001766/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001767static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001768{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001769 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001770
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001771 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001772
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001773 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001774 if (ret >= 0)
1775 if (!list_empty(&lf->lr->line_list))
1776 ret = lf->found = 1;
1777 else
1778 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001779 else {
1780 free(lf->lr->path);
1781 lf->lr->path = NULL;
1782 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001783 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001784}
1785
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001786static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1787{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001788 struct dwarf_callback_param *param = data;
1789
1790 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001791 return DWARF_CB_ABORT; /* No need to find other instances */
1792}
1793
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001794/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001795static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001796{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001797 struct dwarf_callback_param *param = data;
1798 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001799 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001800
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001801 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001802 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001803 lf->fname = dwarf_decl_file(sp_die);
1804 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001805 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001806 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001807 if (lf->lno_s < 0) /* Overflow */
1808 lf->lno_s = INT_MAX;
1809 lf->lno_e = lr->offset + lr->end;
1810 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001811 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001812 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001813 lr->start = lf->lno_s;
1814 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001815 if (dwarf_func_inline(sp_die)) {
1816 struct dwarf_callback_param _param;
1817 _param.data = (void *)lf;
1818 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001819 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001820 line_range_inline_cb,
1821 &_param);
1822 param->retval = _param.retval;
1823 } else
1824 param->retval = find_line_range_by_line(sp_die, lf);
1825 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001826 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001827 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001828}
1829
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001830static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001831{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001832 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1833 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1834 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001835}
1836
1837int find_line_range(int fd, struct line_range *lr)
1838{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001839 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001840 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001841 Dwarf_Off off = 0, noff;
1842 size_t cuhl;
1843 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001844 Dwarf *dbg = NULL;
1845 Dwfl *dwfl;
1846 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001847 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001848
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001849 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001850 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001851 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001852 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1853 return -EBADF;
1854 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001855
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001856 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001857 while (!lf.found && ret >= 0) {
1858 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001859 break;
1860
1861 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001862 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1863 if (!diep)
1864 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001865
1866 /* Check if target file is included. */
1867 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001868 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001869 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001870 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001871
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001872 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001873 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001874 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001875 else {
1876 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001877 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001878 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001879 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001880 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001881 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001882 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001883
1884 /* Store comp_dir */
1885 if (lf.found) {
1886 comp_dir = cu_get_comp_dir(&lf.cu_die);
1887 if (comp_dir) {
1888 lr->comp_dir = strdup(comp_dir);
1889 if (!lr->comp_dir)
1890 ret = -ENOMEM;
1891 }
1892 }
1893
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001894 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001895 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001896 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001897}
1898