blob: 459ebe8b04f356a44affb4e4a24b34ac96e65dc1 [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 Hiramatsu124bb832011-02-04 21:52:11 +090036#include <linux/bitops.h>
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040037#include "event.h"
38#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040039#include "util.h"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040040#include "symbol.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040041#include "probe-finder.h"
42
Masami Hiramatsu49849122010-04-12 13:17:15 -040043/* Kprobe tracer basic type is up to u64 */
44#define MAX_BASIC_TYPE_BITS 64
45
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050046/* Line number list operations */
47
48/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040049static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050050{
51 struct line_node *ln;
52 struct list_head *p;
53
54 /* Reverse search, because new line will be the last one */
55 list_for_each_entry_reverse(ln, head, list) {
56 if (ln->line < line) {
57 p = &ln->list;
58 goto found;
59 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040060 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050061 }
62 /* List is empty, or the smallest entry */
63 p = head;
64found:
65 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040066 ln = zalloc(sizeof(struct line_node));
67 if (ln == NULL)
68 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050069 ln->line = line;
70 INIT_LIST_HEAD(&ln->list);
71 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040072 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050073}
74
75/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040076static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050077{
78 struct line_node *ln;
79
80 /* Reverse search, because new line will be the last one */
81 list_for_each_entry(ln, head, list)
82 if (ln->line == line)
83 return 1;
84
85 return 0;
86}
87
88/* Init line number list */
89static void line_list__init(struct list_head *head)
90{
91 INIT_LIST_HEAD(head);
92}
93
94/* Free line number list */
95static void line_list__free(struct list_head *head)
96{
97 struct line_node *ln;
98 while (!list_empty(head)) {
99 ln = list_first_entry(head, struct line_node, list);
100 list_del(&ln->list);
101 free(ln);
102 }
103}
104
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900105/* Dwarf FL wrappers */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900106static char *debuginfo_path; /* Currently dummy */
107
108static const Dwfl_Callbacks offline_callbacks = {
109 .find_debuginfo = dwfl_standard_find_debuginfo,
110 .debuginfo_path = &debuginfo_path,
111
112 .section_address = dwfl_offline_section_address,
113
114 /* We use this table for core files too. */
115 .find_elf = dwfl_build_id_find_elf,
116};
117
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900118/* Get a Dwarf from offline image */
119static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
120{
121 Dwfl_Module *mod;
122 Dwarf *dbg = NULL;
123
124 if (!dwflp)
125 return NULL;
126
127 *dwflp = dwfl_begin(&offline_callbacks);
128 if (!*dwflp)
129 return NULL;
130
131 mod = dwfl_report_offline(*dwflp, "", "", fd);
132 if (!mod)
133 goto error;
134
135 dbg = dwfl_module_getdwarf(mod, bias);
136 if (!dbg) {
137error:
138 dwfl_end(*dwflp);
139 *dwflp = NULL;
140 }
141 return dbg;
142}
143
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900144#if _ELFUTILS_PREREQ(0, 148)
145/* This method is buggy if elfutils is older than 0.148 */
146static int __linux_kernel_find_elf(Dwfl_Module *mod,
147 void **userdata,
148 const char *module_name,
149 Dwarf_Addr base,
150 char **file_name, Elf **elfp)
151{
152 int fd;
153 const char *path = kernel_get_module_path(module_name);
154
155 pr_debug2("Use file %s for %s\n", path, module_name);
156 if (path) {
157 fd = open(path, O_RDONLY);
158 if (fd >= 0) {
159 *file_name = strdup(path);
160 return fd;
161 }
162 }
163 /* If failed, try to call standard method */
164 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
165 file_name, elfp);
166}
167
168static const Dwfl_Callbacks kernel_callbacks = {
169 .find_debuginfo = dwfl_standard_find_debuginfo,
170 .debuginfo_path = &debuginfo_path,
171
172 .find_elf = __linux_kernel_find_elf,
173 .section_address = dwfl_linux_kernel_module_section_address,
174};
175
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900176/* Get a Dwarf from live kernel image */
177static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
178 Dwarf_Addr *bias)
179{
180 Dwarf *dbg;
181
182 if (!dwflp)
183 return NULL;
184
185 *dwflp = dwfl_begin(&kernel_callbacks);
186 if (!*dwflp)
187 return NULL;
188
189 /* Load the kernel dwarves: Don't care the result here */
190 dwfl_linux_kernel_report_kernel(*dwflp);
191 dwfl_linux_kernel_report_modules(*dwflp);
192
193 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
194 /* Here, check whether we could get a real dwarf */
195 if (!dbg) {
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900196 pr_debug("Failed to find kernel dwarf at %lx\n",
197 (unsigned long)addr);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900198 dwfl_end(*dwflp);
199 *dwflp = NULL;
200 }
201 return dbg;
202}
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900203#else
204/* With older elfutils, this just support kernel module... */
205static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
206 Dwarf_Addr *bias)
207{
208 int fd;
209 const char *path = kernel_get_module_path("kernel");
210
211 if (!path) {
212 pr_err("Failed to find vmlinux path\n");
213 return NULL;
214 }
215
216 pr_debug2("Use file %s for debuginfo\n", path);
217 fd = open(path, O_RDONLY);
218 if (fd < 0)
219 return NULL;
220
221 return dwfl_init_offline_dwarf(fd, dwflp, bias);
222}
223#endif
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900224
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500225/* Dwarf wrappers */
226
227/* Find the realpath of the target file. */
228static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400229{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500230 Dwarf_Files *files;
231 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300232 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400233 int ret;
234
235 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500236 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500237
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500238 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500239 if (ret != 0)
240 return NULL;
241
242 for (i = 0; i < nfiles; i++) {
243 src = dwarf_filesrc(files, i, NULL, NULL);
244 if (strtailcmp(src, fname) == 0)
245 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500246 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400247 if (i == nfiles)
248 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500249 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500250}
251
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900252/* Get DW_AT_comp_dir (should be NULL with older gcc) */
253static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
254{
255 Dwarf_Attribute attr;
256 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
257 return NULL;
258 return dwarf_formstring(&attr);
259}
260
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +0900261/* Get a line number and file name for given address */
262static int cu_find_lineinfo(Dwarf_Die *cudie, unsigned long addr,
263 const char **fname, int *lineno)
264{
265 Dwarf_Line *line;
266 Dwarf_Addr laddr;
267
268 line = dwarf_getsrc_die(cudie, (Dwarf_Addr)addr);
269 if (line && dwarf_lineaddr(line, &laddr) == 0 &&
270 addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
271 *fname = dwarf_linesrc(line, NULL, NULL);
272 if (!*fname)
273 /* line number is useless without filename */
274 *lineno = 0;
275 }
276
277 return *lineno ?: -ENOENT;
278}
279
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400280/* Compare diename and tname */
281static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
282{
283 const char *name;
284 name = dwarf_diename(dw_die);
Masami Hiramatsu82175632010-07-09 18:29:17 +0900285 return name ? (strcmp(tname, name) == 0) : false;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400286}
287
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900288/* Get callsite line number of inline-function instance */
289static int die_get_call_lineno(Dwarf_Die *in_die)
290{
291 Dwarf_Attribute attr;
292 Dwarf_Word ret;
293
294 if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
295 return -ENOENT;
296
297 dwarf_formudata(&attr, &ret);
298 return (int)ret;
299}
300
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900301/* Get type die */
302static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
303{
304 Dwarf_Attribute attr;
305
306 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
307 dwarf_formref_die(&attr, die_mem))
308 return die_mem;
309 else
310 return NULL;
311}
312
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900313/* Get a type die, but skip qualifiers */
314static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400315{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400316 int tag;
317
318 do {
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900319 vr_die = die_get_type(vr_die, die_mem);
320 if (!vr_die)
321 break;
322 tag = dwarf_tag(vr_die);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400323 } while (tag == DW_TAG_const_type ||
324 tag == DW_TAG_restrict_type ||
325 tag == DW_TAG_volatile_type ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900326 tag == DW_TAG_shared_type);
327
328 return vr_die;
329}
330
331/* Get a type die, but skip qualifiers and typedef */
332static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
333{
334 do {
335 vr_die = __die_get_real_type(vr_die, die_mem);
336 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400337
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900338 return vr_die;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400339}
340
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900341static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
342 Dwarf_Word *result)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400343{
344 Dwarf_Attribute attr;
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900345
346 if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
347 dwarf_formudata(&attr, result) != 0)
348 return -ENOENT;
349
350 return 0;
351}
352
353static bool die_is_signed_type(Dwarf_Die *tp_die)
354{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400355 Dwarf_Word ret;
356
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900357 if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
Masami Hiramatsu49849122010-04-12 13:17:15 -0400358 return false;
359
360 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
361 ret == DW_ATE_signed_fixed);
362}
363
364static int die_get_byte_size(Dwarf_Die *tp_die)
365{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400366 Dwarf_Word ret;
367
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900368 if (die_get_attr_udata(tp_die, DW_AT_byte_size, &ret))
369 return 0;
370
371 return (int)ret;
372}
373
374static int die_get_bit_size(Dwarf_Die *tp_die)
375{
376 Dwarf_Word ret;
377
378 if (die_get_attr_udata(tp_die, DW_AT_bit_size, &ret))
379 return 0;
380
381 return (int)ret;
382}
383
384static int die_get_bit_offset(Dwarf_Die *tp_die)
385{
386 Dwarf_Word ret;
387
388 if (die_get_attr_udata(tp_die, DW_AT_bit_offset, &ret))
Masami Hiramatsu49849122010-04-12 13:17:15 -0400389 return 0;
390
391 return (int)ret;
392}
393
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300394/* Get data_member_location offset */
395static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
396{
397 Dwarf_Attribute attr;
398 Dwarf_Op *expr;
399 size_t nexpr;
400 int ret;
401
402 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
403 return -ENOENT;
404
405 if (dwarf_formudata(&attr, offs) != 0) {
406 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
407 ret = dwarf_getlocation(&attr, &expr, &nexpr);
408 if (ret < 0 || nexpr == 0)
409 return -ENOENT;
410
411 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
412 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
413 expr[0].atom, nexpr);
414 return -ENOTSUP;
415 }
416 *offs = (Dwarf_Word)expr[0].number;
417 }
418 return 0;
419}
420
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400421/* Return values for die_find callbacks */
422enum {
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900423 DIE_FIND_CB_END = 0, /* End of Search */
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400424 DIE_FIND_CB_CHILD = 1, /* Search only children */
425 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
426 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
427};
428
429/* Search a child die */
430static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
431 int (*callback)(Dwarf_Die *, void *),
432 void *data, Dwarf_Die *die_mem)
433{
434 Dwarf_Die child_die;
435 int ret;
436
437 ret = dwarf_child(rt_die, die_mem);
438 if (ret != 0)
439 return NULL;
440
441 do {
442 ret = callback(die_mem, data);
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900443 if (ret == DIE_FIND_CB_END)
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400444 return die_mem;
445
446 if ((ret & DIE_FIND_CB_CHILD) &&
447 die_find_child(die_mem, callback, data, &child_die)) {
448 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
449 return die_mem;
450 }
451 } while ((ret & DIE_FIND_CB_SIBLING) &&
452 dwarf_siblingof(die_mem, die_mem) == 0);
453
454 return NULL;
455}
456
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500457struct __addr_die_search_param {
458 Dwarf_Addr addr;
459 Dwarf_Die *die_mem;
460};
461
462static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
463{
464 struct __addr_die_search_param *ad = data;
465
466 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
467 dwarf_haspc(fn_die, ad->addr)) {
468 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
469 return DWARF_CB_ABORT;
470 }
471 return DWARF_CB_OK;
472}
473
474/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400475static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
476 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500477{
478 struct __addr_die_search_param ad;
479 ad.addr = addr;
480 ad.die_mem = die_mem;
481 /* dwarf_getscopes can't find subprogram. */
482 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
483 return NULL;
484 else
485 return die_mem;
486}
487
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400488/* die_find callback for inline function search */
489static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
490{
491 Dwarf_Addr *addr = data;
492
493 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
494 dwarf_haspc(die_mem, *addr))
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900495 return DIE_FIND_CB_END;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400496
497 return DIE_FIND_CB_CONTINUE;
498}
499
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500500/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400501static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
502 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500503{
Masami Hiramatsu1d878082011-03-30 18:25:59 +0900504 Dwarf_Die tmp_die;
505
506 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
507 if (!sp_die)
508 return NULL;
509
510 /* Inlined function could be recursive. Trace it until fail */
511 while (sp_die) {
512 memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
513 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
514 &tmp_die);
515 }
516
517 return die_mem;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500518}
519
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900520/* Walker on lines (Note: line number will not be sorted) */
521typedef int (* line_walk_handler_t) (const char *fname, int lineno,
522 Dwarf_Addr addr, void *data);
523
524struct __line_walk_param {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900525 const char *fname;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900526 line_walk_handler_t handler;
527 void *data;
528 int retval;
529};
530
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900531static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
532{
533 struct __line_walk_param *lw = data;
534 Dwarf_Addr addr;
535 int lineno;
536
537 if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
538 lineno = die_get_call_lineno(in_die);
539 if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
540 lw->retval = lw->handler(lw->fname, lineno, addr,
541 lw->data);
542 if (lw->retval != 0)
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900543 return DIE_FIND_CB_END;
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900544 }
545 }
546 return DIE_FIND_CB_SIBLING;
547}
548
549/* Walk on lines of blocks included in given DIE */
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900550static int __die_walk_funclines(Dwarf_Die *sp_die,
551 line_walk_handler_t handler, void *data)
552{
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900553 struct __line_walk_param lw = {
554 .handler = handler,
555 .data = data,
556 .retval = 0,
557 };
558 Dwarf_Die die_mem;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900559 Dwarf_Addr addr;
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900560 int lineno;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900561
562 /* Handle function declaration line */
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900563 lw.fname = dwarf_decl_file(sp_die);
564 if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900565 dwarf_entrypc(sp_die, &addr) == 0) {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900566 lw.retval = handler(lw.fname, lineno, addr, data);
567 if (lw.retval != 0)
568 goto done;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900569 }
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900570 die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
571done:
572 return lw.retval;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900573}
574
575static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
576{
577 struct __line_walk_param *lw = data;
578
579 lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
580 if (lw->retval != 0)
581 return DWARF_CB_ABORT;
582
583 return DWARF_CB_OK;
584}
585
586/*
587 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
588 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
589 */
590static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
591 void *data)
592{
593 Dwarf_Lines *lines;
594 Dwarf_Line *line;
595 Dwarf_Addr addr;
596 const char *fname;
597 int lineno, ret = 0;
598 Dwarf_Die die_mem, *cu_die;
599 size_t nlines, i;
600
601 /* Get the CU die */
602 if (dwarf_tag(pdie) == DW_TAG_subprogram)
603 cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
604 else
605 cu_die = pdie;
606 if (!cu_die) {
607 pr_debug2("Failed to get CU from subprogram\n");
608 return -EINVAL;
609 }
610
611 /* Get lines list in the CU */
612 if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
613 pr_debug2("Failed to get source lines on this CU.\n");
614 return -ENOENT;
615 }
616 pr_debug2("Get %zd lines from this CU\n", nlines);
617
618 /* Walk on the lines on lines list */
619 for (i = 0; i < nlines; i++) {
620 line = dwarf_onesrcline(lines, i);
621 if (line == NULL ||
622 dwarf_lineno(line, &lineno) != 0 ||
623 dwarf_lineaddr(line, &addr) != 0) {
624 pr_debug2("Failed to get line info. "
625 "Possible error in debuginfo.\n");
626 continue;
627 }
628 /* Filter lines based on address */
629 if (pdie != cu_die)
630 /*
631 * Address filtering
632 * The line is included in given function, and
633 * no inline block includes it.
634 */
635 if (!dwarf_haspc(pdie, addr) ||
636 die_find_inlinefunc(pdie, addr, &die_mem))
637 continue;
638 /* Get source line */
639 fname = dwarf_linesrc(line, NULL, NULL);
640
641 ret = handler(fname, lineno, addr, data);
642 if (ret != 0)
643 return ret;
644 }
645
646 /*
647 * Dwarf lines doesn't include function declarations and inlined
648 * subroutines. We have to check functions list or given function.
649 */
650 if (pdie != cu_die)
651 ret = __die_walk_funclines(pdie, handler, data);
652 else {
653 struct __line_walk_param param = {
654 .handler = handler,
655 .data = data,
656 .retval = 0,
657 };
658 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
659 ret = param.retval;
660 }
661
662 return ret;
663}
664
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900665struct __find_variable_param {
666 const char *name;
667 Dwarf_Addr addr;
668};
669
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400670static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400671{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900672 struct __find_variable_param *fvp = data;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400673 int tag;
674
675 tag = dwarf_tag(die_mem);
676 if ((tag == DW_TAG_formal_parameter ||
677 tag == DW_TAG_variable) &&
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900678 die_compare_name(die_mem, fvp->name))
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900679 return DIE_FIND_CB_END;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400680
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900681 if (dwarf_haspc(die_mem, fvp->addr))
682 return DIE_FIND_CB_CONTINUE;
683 else
684 return DIE_FIND_CB_SIBLING;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400685}
686
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900687/* Find a variable called 'name' at given address */
688static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
689 Dwarf_Addr addr, Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500690{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900691 struct __find_variable_param fvp = { .name = name, .addr = addr};
692
693 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400694 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400695}
696
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400697static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
698{
699 const char *name = data;
700
701 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900702 die_compare_name(die_mem, name))
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900703 return DIE_FIND_CB_END;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400704
705 return DIE_FIND_CB_SIBLING;
706}
707
708/* Find a member called 'name' */
709static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
710 Dwarf_Die *die_mem)
711{
712 return die_find_child(st_die, __die_find_member_cb, (void *)name,
713 die_mem);
714}
715
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900716/* Get the name of given variable DIE */
717static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
718{
719 Dwarf_Die type;
720 int tag, ret, ret2;
721 const char *tmp = "";
722
723 if (__die_get_real_type(vr_die, &type) == NULL)
724 return -ENOENT;
725
726 tag = dwarf_tag(&type);
727 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
728 tmp = "*";
729 else if (tag == DW_TAG_subroutine_type) {
730 /* Function pointer */
731 ret = snprintf(buf, len, "(function_type)");
732 return (ret >= len) ? -E2BIG : ret;
733 } else {
734 if (!dwarf_diename(&type))
735 return -ENOENT;
736 if (tag == DW_TAG_union_type)
737 tmp = "union ";
738 else if (tag == DW_TAG_structure_type)
739 tmp = "struct ";
740 /* Write a base name */
741 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
742 return (ret >= len) ? -E2BIG : ret;
743 }
744 ret = die_get_typename(&type, buf, len);
745 if (ret > 0) {
746 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
747 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
748 }
749 return ret;
750}
751
752/* Get the name and type of given variable DIE, stored as "type\tname" */
753static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
754{
755 int ret, ret2;
756
757 ret = die_get_typename(vr_die, buf, len);
758 if (ret < 0) {
759 pr_debug("Failed to get type, make it unknown.\n");
760 ret = snprintf(buf, len, "(unknown_type)");
761 }
762 if (ret > 0) {
763 ret2 = snprintf(buf + ret, len - ret, "\t%s",
764 dwarf_diename(vr_die));
765 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
766 }
767 return ret;
768}
769
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400770/*
771 * Probe finder related functions
772 */
773
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530774static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400775{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530776 struct probe_trace_arg_ref *ref;
777 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400778 if (ref != NULL)
779 ref->offset = offs;
780 return ref;
781}
782
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900783/*
784 * Convert a location into trace_arg.
785 * If tvar == NULL, this just checks variable can be converted.
786 */
787static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
788 Dwarf_Op *fb_ops,
789 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400790{
791 Dwarf_Attribute attr;
792 Dwarf_Op *op;
793 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500794 unsigned int regn;
795 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400796 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400797 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400798 int ret;
799
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900800 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
801 goto static_var;
802
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400803 /* TODO: handle more than 1 exprs */
804 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900805 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400806 nops == 0) {
807 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400808 return -ENOENT;
809 }
810
811 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900812static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900813 if (!tvar)
814 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400815 /* Static variables on memory (not stack), make @varname */
816 ret = strlen(dwarf_diename(vr_die));
817 tvar->value = zalloc(ret + 2);
818 if (tvar->value == NULL)
819 return -ENOMEM;
820 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
821 tvar->ref = alloc_trace_arg_ref((long)offs);
822 if (tvar->ref == NULL)
823 return -ENOMEM;
824 return 0;
825 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400826
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400827 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500828 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900829 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400830 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400831 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500832 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900833 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500834 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400835
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500836 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
837 regn = op->atom - DW_OP_breg0;
838 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400839 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500840 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
841 regn = op->atom - DW_OP_reg0;
842 } else if (op->atom == DW_OP_bregx) {
843 regn = op->number;
844 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400845 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500846 } else if (op->atom == DW_OP_regx) {
847 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400848 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900849 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400850 return -ENOTSUP;
851 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400852
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900853 if (!tvar)
854 return 0;
855
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400856 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400857 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900858 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900859 pr_warning("Mapping for the register number %u "
860 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400861 return -ERANGE;
862 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400863
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400864 tvar->value = strdup(regs);
865 if (tvar->value == NULL)
866 return -ENOMEM;
867
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400868 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400869 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400870 if (tvar->ref == NULL)
871 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400872 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400873 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400874}
875
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900876#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
877
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400878static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530879 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400880 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400881{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530882 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400883 Dwarf_Die type;
884 char buf[16];
885 int ret;
886
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400887 /* TODO: check all types */
888 if (cast && strcmp(cast, "string") != 0) {
889 /* Non string type is OK */
890 tvar->type = strdup(cast);
891 return (tvar->type == NULL) ? -ENOMEM : 0;
892 }
893
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900894 if (die_get_bit_size(vr_die) != 0) {
895 /* This is a bitfield */
896 ret = snprintf(buf, 16, "b%d@%d/%zd", die_get_bit_size(vr_die),
897 die_get_bit_offset(vr_die),
898 BYTES_TO_BITS(die_get_byte_size(vr_die)));
899 goto formatted;
900 }
901
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400902 if (die_get_real_type(vr_die, &type) == NULL) {
903 pr_warning("Failed to get a type information of %s.\n",
904 dwarf_diename(vr_die));
905 return -ENOENT;
906 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400907
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400908 pr_debug("%s type is %s.\n",
909 dwarf_diename(vr_die), dwarf_diename(&type));
910
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400911 if (cast && strcmp(cast, "string") == 0) { /* String type */
912 ret = dwarf_tag(&type);
913 if (ret != DW_TAG_pointer_type &&
914 ret != DW_TAG_array_type) {
915 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900916 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400917 dwarf_diename(vr_die), dwarf_diename(&type));
918 return -EINVAL;
919 }
920 if (ret == DW_TAG_pointer_type) {
921 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900922 pr_warning("Failed to get a type"
923 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400924 return -ENOENT;
925 }
926 while (*ref_ptr)
927 ref_ptr = &(*ref_ptr)->next;
928 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530929 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400930 if (*ref_ptr == NULL) {
931 pr_warning("Out of memory error\n");
932 return -ENOMEM;
933 }
934 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900935 if (!die_compare_name(&type, "char") &&
936 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400937 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900938 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400939 dwarf_diename(vr_die));
940 return -EINVAL;
941 }
942 tvar->type = strdup(cast);
943 return (tvar->type == NULL) ? -ENOMEM : 0;
944 }
945
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900946 ret = BYTES_TO_BITS(die_get_byte_size(&type));
947 if (!ret)
948 /* No size ... try to use default type */
949 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400950
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900951 /* Check the bitwidth */
952 if (ret > MAX_BASIC_TYPE_BITS) {
953 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
954 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
955 ret = MAX_BASIC_TYPE_BITS;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400956 }
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900957 ret = snprintf(buf, 16, "%c%d",
958 die_is_signed_type(&type) ? 's' : 'u', ret);
959
960formatted:
961 if (ret < 0 || ret >= 16) {
962 if (ret >= 16)
963 ret = -E2BIG;
964 pr_warning("Failed to convert variable type: %s\n",
965 strerror(-ret));
966 return ret;
967 }
968 tvar->type = strdup(buf);
969 if (tvar->type == NULL)
970 return -ENOMEM;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400971 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400972}
973
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400974static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400975 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530976 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400977 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400978{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530979 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400980 Dwarf_Die type;
981 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400982 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400983
984 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400985 if (die_get_real_type(vr_die, &type) == NULL) {
986 pr_warning("Failed to get the type of %s.\n", varname);
987 return -ENOENT;
988 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400989 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
990 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400991
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400992 if (field->name[0] == '[' &&
993 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
994 if (field->next)
995 /* Save original type for next field */
996 memcpy(die_mem, &type, sizeof(*die_mem));
997 /* Get the type of this array */
998 if (die_get_real_type(&type, &type) == NULL) {
999 pr_warning("Failed to get the type of %s.\n", varname);
1000 return -ENOENT;
1001 }
1002 pr_debug2("Array real type: (%x)\n",
1003 (unsigned)dwarf_dieoffset(&type));
1004 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301005 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001006 if (ref == NULL)
1007 return -ENOMEM;
1008 if (*ref_ptr)
1009 (*ref_ptr)->next = ref;
1010 else
1011 *ref_ptr = ref;
1012 }
1013 ref->offset += die_get_byte_size(&type) * field->index;
1014 if (!field->next)
1015 /* Save vr_die for converting types */
1016 memcpy(die_mem, vr_die, sizeof(*die_mem));
1017 goto next;
1018 } else if (tag == DW_TAG_pointer_type) {
1019 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001020 if (!field->ref) {
1021 pr_err("Semantic error: %s must be referred by '->'\n",
1022 field->name);
1023 return -EINVAL;
1024 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001025 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001026 if (die_get_real_type(&type, &type) == NULL) {
1027 pr_warning("Failed to get the type of %s.\n", varname);
1028 return -ENOENT;
1029 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001030 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001031 if (dwarf_tag(&type) != DW_TAG_structure_type) {
1032 pr_warning("%s is not a data structure.\n", varname);
1033 return -EINVAL;
1034 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001035
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301036 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001037 if (ref == NULL)
1038 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001039 if (*ref_ptr)
1040 (*ref_ptr)->next = ref;
1041 else
1042 *ref_ptr = ref;
1043 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001044 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001045 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001046 pr_warning("%s is not a data structure.\n", varname);
1047 return -EINVAL;
1048 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001049 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001050 pr_err("Semantic error: %s is not a pointor"
1051 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001052 return -EINVAL;
1053 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001054 if (field->ref) {
1055 pr_err("Semantic error: %s must be referred by '.'\n",
1056 field->name);
1057 return -EINVAL;
1058 }
1059 if (!ref) {
1060 pr_warning("Structure on a register is not "
1061 "supported yet.\n");
1062 return -ENOTSUP;
1063 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001064 }
1065
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001066 if (die_find_member(&type, field->name, die_mem) == NULL) {
1067 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
1068 dwarf_diename(&type), field->name);
1069 return -EINVAL;
1070 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001071
1072 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001073 ret = die_get_data_member_location(die_mem, &offs);
1074 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001075 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001076 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001077 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001078 ref->offset += (long)offs;
1079
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001080next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001081 /* Converting next field */
1082 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001083 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001084 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001085 else
1086 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001087}
1088
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001089/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001090static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001091{
Masami Hiramatsu49849122010-04-12 13:17:15 -04001092 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001093 int ret;
1094
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001095 pr_debug("Converting variable %s into trace event.\n",
1096 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001097
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001098 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
1099 pf->tvar);
1100 if (ret == -ENOENT)
1101 pr_err("Failed to find the location of %s at this address.\n"
1102 " Perhaps, it has been optimized out.\n", pf->pvar->var);
1103 else if (ret == -ENOTSUP)
1104 pr_err("Sorry, we don't support this variable location yet.\n");
1105 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001106 ret = convert_variable_fields(vr_die, pf->pvar->var,
1107 pf->pvar->field, &pf->tvar->ref,
1108 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001109 vr_die = &die_mem;
1110 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -04001111 if (ret == 0)
1112 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001113 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001114 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001115}
1116
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001117/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001118static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001119{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001120 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001121 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001122 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001123
Masami Hiramatsu367e94c2010-08-27 20:38:59 +09001124 if (!is_c_varname(pf->pvar->var)) {
1125 /* Copy raw parameters */
1126 pf->tvar->value = strdup(pf->pvar->var);
1127 if (pf->tvar->value == NULL)
1128 return -ENOMEM;
1129 if (pf->pvar->type) {
1130 pf->tvar->type = strdup(pf->pvar->type);
1131 if (pf->tvar->type == NULL)
1132 return -ENOMEM;
1133 }
1134 if (pf->pvar->name) {
1135 pf->tvar->name = strdup(pf->pvar->name);
1136 if (pf->tvar->name == NULL)
1137 return -ENOMEM;
1138 } else
1139 pf->tvar->name = NULL;
1140 return 0;
1141 }
1142
Masami Hiramatsu48481932010-04-12 13:16:53 -04001143 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001144 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001145 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001146 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
1147 if (ret < 0)
1148 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001149 ptr = strchr(buf, ':'); /* Change type separator to _ */
1150 if (ptr)
1151 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001152 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001153 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001154 if (pf->tvar->name == NULL)
1155 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001156
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001157 pr_debug("Searching '%s' variable in context.\n",
1158 pf->pvar->var);
1159 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +09001160 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001161 ret = convert_variable(&vr_die, pf);
1162 else {
1163 /* Search upper class */
1164 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001165 while (nscopes-- > 1) {
1166 pr_debug("Searching variables in %s\n",
1167 dwarf_diename(&scopes[nscopes]));
1168 /* We should check this scope, so give dummy address */
1169 if (die_find_variable_at(&scopes[nscopes],
1170 pf->pvar->var, 0,
1171 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001172 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001173 goto found;
1174 }
1175 }
1176 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001177 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001178 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001179 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001180found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001181 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001182 pr_warning("Failed to find '%s' in this function.\n",
1183 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001184 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001185}
1186
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001187/* Convert subprogram DIE to trace point */
1188static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
1189 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001190{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001191 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001192 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001193
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001194 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001195 name = dwarf_diename(sp_die);
1196 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001197 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001198 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001199 dwarf_diename(sp_die));
1200 return -ENOENT;
1201 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001202 tp->symbol = strdup(name);
1203 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001204 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001205 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001206 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001207 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001208 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001209
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001210 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001211 if (retprobe) {
1212 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001213 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001214 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001215 return -EINVAL;
1216 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001217 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001218 }
1219
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001220 return 0;
1221}
1222
1223/* Call probe_finder callback with real subprogram DIE */
1224static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
1225{
1226 Dwarf_Die die_mem;
1227 Dwarf_Attribute fb_attr;
1228 size_t nops;
1229 int ret;
1230
1231 /* If no real subprogram, find a real one */
1232 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
1233 sp_die = die_find_real_subprogram(&pf->cu_die,
1234 pf->addr, &die_mem);
1235 if (!sp_die) {
1236 pr_warning("Failed to find probe point in any "
1237 "functions.\n");
1238 return -ENOENT;
1239 }
1240 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001241
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001242 /* Get the frame base attribute/ops */
1243 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -04001244 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001245 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001246 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001247#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001248 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1249 pf->cfi != NULL) {
1250 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001251 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1252 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001253 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001254 (uintmax_t)pf->addr);
1255 return -ENOENT;
1256 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001257#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001258 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001259
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001260 /* Call finder's callback handler */
1261 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001262
1263 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1264 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001265
1266 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001267}
1268
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001269static int probe_point_line_walker(const char *fname, int lineno,
1270 Dwarf_Addr addr, void *data)
1271{
1272 struct probe_finder *pf = data;
1273 int ret;
1274
1275 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
1276 return 0;
1277
1278 pf->addr = addr;
1279 ret = call_probe_finder(NULL, pf);
1280
1281 /* Continue if no error, because the line will be in inline function */
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001282 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001283}
1284
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001285/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001286static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001287{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001288 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001289}
1290
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001291/* Find lines which match lazy pattern */
1292static int find_lazy_match_lines(struct list_head *head,
1293 const char *fname, const char *pat)
1294{
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001295 FILE *fp;
1296 char *line = NULL;
1297 size_t line_len;
1298 ssize_t len;
1299 int count = 0, linenum = 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001300
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001301 fp = fopen(fname, "r");
1302 if (!fp) {
1303 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001304 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001305 }
1306
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001307 while ((len = getline(&line, &line_len, fp)) > 0) {
1308
1309 if (line[len - 1] == '\n')
1310 line[len - 1] = '\0';
1311
1312 if (strlazymatch(line, pat)) {
1313 line_list__add_line(head, linenum);
1314 count++;
1315 }
1316 linenum++;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001317 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001318
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001319 if (ferror(fp))
1320 count = -errno;
1321 free(line);
1322 fclose(fp);
1323
1324 if (count == 0)
1325 pr_debug("No matched lines found in %s.\n", fname);
1326 return count;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001327}
1328
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001329static int probe_point_lazy_walker(const char *fname, int lineno,
1330 Dwarf_Addr addr, void *data)
1331{
1332 struct probe_finder *pf = data;
1333 int ret;
1334
1335 if (!line_list__has_line(&pf->lcache, lineno) ||
1336 strtailcmp(fname, pf->fname) != 0)
1337 return 0;
1338
1339 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1340 lineno, (unsigned long long)addr);
1341 pf->addr = addr;
1342 ret = call_probe_finder(NULL, pf);
1343
1344 /*
1345 * Continue if no error, because the lazy pattern will match
1346 * to other lines
1347 */
Ingo Molnar5e814dd2011-03-15 20:51:09 +01001348 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001349}
1350
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001351/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001352static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001353{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001354 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001355
1356 if (list_empty(&pf->lcache)) {
1357 /* Matching lazy line pattern */
1358 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001359 pf->pev->point.lazy_line);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001360 if (ret <= 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001361 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001362 }
1363
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001364 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001365}
1366
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001367/* Callback parameter with return value */
1368struct dwarf_callback_param {
1369 void *data;
1370 int retval;
1371};
1372
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001373static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001374{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001375 struct dwarf_callback_param *param = data;
1376 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001377 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001378 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001379
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001380 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001381 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001382 else {
1383 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001384 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001385 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001386 dwarf_diename(in_die));
1387 param->retval = -ENOENT;
1388 return DWARF_CB_ABORT;
1389 }
1390 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001391 pf->addr += pp->offset;
1392 pr_debug("found inline addr: 0x%jx\n",
1393 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001394
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001395 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001396 if (param->retval < 0)
1397 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001398 }
1399
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001400 return DWARF_CB_OK;
1401}
1402
1403/* Search function from function name */
1404static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1405{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001406 struct dwarf_callback_param *param = data;
1407 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001408 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001409
1410 /* Check tag and diename */
1411 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001412 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001413 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001414
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001415 /* Check declared file */
1416 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1417 return DWARF_CB_OK;
1418
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001419 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001420 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001421 dwarf_decl_line(sp_die, &pf->lno);
1422 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001423 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001424 } else if (!dwarf_func_inline(sp_die)) {
1425 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001426 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001427 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001428 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001429 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001430 pr_warning("Failed to get entry address of "
1431 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001432 param->retval = -ENOENT;
1433 return DWARF_CB_ABORT;
1434 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001435 pf->addr += pp->offset;
1436 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001437 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001438 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001439 } else {
1440 struct dwarf_callback_param _param = {.data = (void *)pf,
1441 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001442 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001443 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1444 &_param);
1445 param->retval = _param.retval;
1446 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001447
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001448 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001449}
1450
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001451static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001452{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001453 struct dwarf_callback_param _param = {.data = (void *)pf,
1454 .retval = 0};
1455 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1456 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001457}
1458
Lin Mingcd25f8b2011-03-25 16:27:48 +08001459struct pubname_callback_param {
1460 char *function;
1461 char *file;
1462 Dwarf_Die *cu_die;
1463 Dwarf_Die *sp_die;
1464 int found;
1465};
1466
1467static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1468{
1469 struct pubname_callback_param *param = data;
1470
1471 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1472 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1473 return DWARF_CB_OK;
1474
1475 if (die_compare_name(param->sp_die, param->function)) {
1476 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1477 return DWARF_CB_OK;
1478
1479 if (param->file &&
1480 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1481 return DWARF_CB_OK;
1482
1483 param->found = 1;
1484 return DWARF_CB_ABORT;
1485 }
1486 }
1487
1488 return DWARF_CB_OK;
1489}
1490
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001491/* Find probe points from debuginfo */
1492static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001493{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001494 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001495 Dwarf_Off off, noff;
1496 size_t cuhl;
1497 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001498 Dwarf *dbg = NULL;
1499 Dwfl *dwfl;
1500 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001501 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001502
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001503 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001504 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001505 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001506 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001507 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001508 return -EBADF;
1509 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001510
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001511#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001512 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001513 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001514#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001515
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001516 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001517 line_list__init(&pf->lcache);
Lin Mingcd25f8b2011-03-25 16:27:48 +08001518
1519 /* Fastpath: lookup by function name from .debug_pubnames section */
1520 if (pp->function) {
1521 struct pubname_callback_param pubname_param = {
1522 .function = pp->function,
1523 .file = pp->file,
1524 .cu_die = &pf->cu_die,
1525 .sp_die = &pf->sp_die,
Lin Ming2b348a72011-04-29 08:41:57 +00001526 .found = 0,
Lin Mingcd25f8b2011-03-25 16:27:48 +08001527 };
1528 struct dwarf_callback_param probe_param = {
1529 .data = pf,
1530 };
1531
1532 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1533 if (pubname_param.found) {
1534 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1535 if (ret)
1536 goto found;
1537 }
1538 }
1539
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001540 /* Loop on CUs (Compilation Unit) */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001541 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001542 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001543 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001544 if (!diep)
1545 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001546
1547 /* Check if target file is included. */
1548 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001549 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001550 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001551 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001552
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001553 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001554 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001555 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001556 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001557 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001558 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001559 pf->lno = pp->line;
1560 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001561 }
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001562 if (ret < 0)
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001563 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001564 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001565 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001566 }
Lin Mingcd25f8b2011-03-25 16:27:48 +08001567
1568found:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001569 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001570 if (dwfl)
1571 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001572
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001573 return ret;
1574}
1575
1576/* Add a found probe point into trace event list */
1577static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1578{
1579 struct trace_event_finder *tf =
1580 container_of(pf, struct trace_event_finder, pf);
1581 struct probe_trace_event *tev;
1582 int ret, i;
1583
1584 /* Check number of tevs */
1585 if (tf->ntevs == tf->max_tevs) {
1586 pr_warning("Too many( > %d) probe point found.\n",
1587 tf->max_tevs);
1588 return -ERANGE;
1589 }
1590 tev = &tf->tevs[tf->ntevs++];
1591
1592 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1593 &tev->point);
1594 if (ret < 0)
1595 return ret;
1596
1597 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1598 tev->point.offset);
1599
1600 /* Find each argument */
1601 tev->nargs = pf->pev->nargs;
1602 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1603 if (tev->args == NULL)
1604 return -ENOMEM;
1605 for (i = 0; i < pf->pev->nargs; i++) {
1606 pf->pvar = &pf->pev->args[i];
1607 pf->tvar = &tev->args[i];
1608 ret = find_variable(sp_die, pf);
1609 if (ret != 0)
1610 return ret;
1611 }
1612
1613 return 0;
1614}
1615
1616/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1617int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1618 struct probe_trace_event **tevs, int max_tevs)
1619{
1620 struct trace_event_finder tf = {
1621 .pf = {.pev = pev, .callback = add_probe_trace_event},
1622 .max_tevs = max_tevs};
1623 int ret;
1624
1625 /* Allocate result tevs array */
1626 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1627 if (*tevs == NULL)
1628 return -ENOMEM;
1629
1630 tf.tevs = *tevs;
1631 tf.ntevs = 0;
1632
1633 ret = find_probes(fd, &tf.pf);
1634 if (ret < 0) {
1635 free(*tevs);
1636 *tevs = NULL;
1637 return ret;
1638 }
1639
1640 return (ret < 0) ? ret : tf.ntevs;
1641}
1642
1643#define MAX_VAR_LEN 64
1644
1645/* Collect available variables in this scope */
1646static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1647{
1648 struct available_var_finder *af = data;
1649 struct variable_list *vl;
1650 char buf[MAX_VAR_LEN];
1651 int tag, ret;
1652
1653 vl = &af->vls[af->nvls - 1];
1654
1655 tag = dwarf_tag(die_mem);
1656 if (tag == DW_TAG_formal_parameter ||
1657 tag == DW_TAG_variable) {
1658 ret = convert_variable_location(die_mem, af->pf.addr,
1659 af->pf.fb_ops, NULL);
1660 if (ret == 0) {
1661 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001662 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001663 if (ret > 0)
1664 strlist__add(vl->vars, buf);
1665 }
1666 }
1667
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001668 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001669 return DIE_FIND_CB_CONTINUE;
1670 else
1671 return DIE_FIND_CB_SIBLING;
1672}
1673
1674/* Add a found vars into available variables list */
1675static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1676{
1677 struct available_var_finder *af =
1678 container_of(pf, struct available_var_finder, pf);
1679 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001680 Dwarf_Die die_mem, *scopes = NULL;
1681 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001682
1683 /* Check number of tevs */
1684 if (af->nvls == af->max_vls) {
1685 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1686 return -ERANGE;
1687 }
1688 vl = &af->vls[af->nvls++];
1689
1690 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1691 &vl->point);
1692 if (ret < 0)
1693 return ret;
1694
1695 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1696 vl->point.offset);
1697
1698 /* Find local variables */
1699 vl->vars = strlist__new(true, NULL);
1700 if (vl->vars == NULL)
1701 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001702 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001703 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1704
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001705 /* Find external variables */
1706 if (!af->externs)
1707 goto out;
1708 /* Don't need to search child DIE for externs. */
1709 af->child = false;
1710 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1711 while (nscopes-- > 1)
1712 die_find_child(&scopes[nscopes], collect_variables_cb,
1713 (void *)af, &die_mem);
1714 if (scopes)
1715 free(scopes);
1716
1717out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001718 if (strlist__empty(vl->vars)) {
1719 strlist__delete(vl->vars);
1720 vl->vars = NULL;
1721 }
1722
1723 return ret;
1724}
1725
1726/* Find available variables at given probe point */
1727int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001728 struct variable_list **vls, int max_vls,
1729 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001730{
1731 struct available_var_finder af = {
1732 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001733 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001734 int ret;
1735
1736 /* Allocate result vls array */
1737 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1738 if (*vls == NULL)
1739 return -ENOMEM;
1740
1741 af.vls = *vls;
1742 af.nvls = 0;
1743
1744 ret = find_probes(fd, &af.pf);
1745 if (ret < 0) {
1746 /* Free vlist for error */
1747 while (af.nvls--) {
1748 if (af.vls[af.nvls].point.symbol)
1749 free(af.vls[af.nvls].point.symbol);
1750 if (af.vls[af.nvls].vars)
1751 strlist__delete(af.vls[af.nvls].vars);
1752 }
1753 free(af.vls);
1754 *vls = NULL;
1755 return ret;
1756 }
1757
1758 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001759}
1760
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001761/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001762int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001763{
1764 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001765 Dwarf *dbg = NULL;
1766 Dwfl *dwfl = NULL;
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001767 Dwarf_Addr _addr, baseaddr, bias = 0;
1768 const char *fname = NULL, *func = NULL, *tmp;
1769 int baseline = 0, lineno = 0, ret = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001770
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001771 /* Open the live linux kernel */
1772 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1773 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001774 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001775 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1776 ret = -EINVAL;
1777 goto end;
1778 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001779
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001780 /* Adjust address with bias */
1781 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001782 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001783 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001784 pr_warning("Failed to find debug information for address %lx\n",
1785 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001786 ret = -EINVAL;
1787 goto end;
1788 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001789
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001790 /* Find a corresponding line (filename and lineno) */
1791 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1792 /* Don't care whether it failed or not */
1793
1794 /* Find a corresponding function (name, baseline and baseaddr) */
1795 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1796 /* Get function entry information */
1797 tmp = dwarf_diename(&spdie);
1798 if (!tmp ||
1799 dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1800 dwarf_decl_line(&spdie, &baseline) != 0)
1801 goto post;
1802 func = tmp;
1803
1804 if (addr == (unsigned long)baseaddr)
1805 /* Function entry - Relative line number is 0 */
1806 lineno = baseline;
1807 else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1808 &indie)) {
1809 if (dwarf_entrypc(&indie, &_addr) == 0 &&
1810 _addr == addr)
1811 /*
1812 * addr is at an inline function entry.
1813 * In this case, lineno should be the call-site
1814 * line number.
1815 */
1816 lineno = die_get_call_lineno(&indie);
1817 else {
1818 /*
1819 * addr is in an inline function body.
1820 * Since lineno points one of the lines
1821 * of the inline function, baseline should
1822 * be the entry line of the inline function.
1823 */
1824 tmp = dwarf_diename(&indie);
1825 if (tmp &&
1826 dwarf_decl_line(&spdie, &baseline) == 0)
1827 func = tmp;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001828 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001829 }
1830 }
1831
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001832post:
1833 /* Make a relative line number or an offset */
1834 if (lineno)
1835 ppt->line = lineno - baseline;
1836 else if (func)
1837 ppt->offset = addr - (unsigned long)baseaddr;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001838
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001839 /* Duplicate strings */
1840 if (func) {
1841 ppt->function = strdup(func);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001842 if (ppt->function == NULL) {
1843 ret = -ENOMEM;
1844 goto end;
1845 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001846 }
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001847 if (fname) {
1848 ppt->file = strdup(fname);
1849 if (ppt->file == NULL) {
1850 if (ppt->function) {
1851 free(ppt->function);
1852 ppt->function = NULL;
1853 }
1854 ret = -ENOMEM;
1855 goto end;
1856 }
1857 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001858end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001859 if (dwfl)
1860 dwfl_end(dwfl);
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001861 if (ret == 0 && (fname || func))
1862 ret = 1; /* Found a point */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001863 return ret;
1864}
1865
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001866/* Add a line and store the src path */
1867static int line_range_add_line(const char *src, unsigned int lineno,
1868 struct line_range *lr)
1869{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001870 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001871 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001872 lr->path = strdup(src);
1873 if (lr->path == NULL)
1874 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001875 }
1876 return line_list__add_line(&lr->line_list, lineno);
1877}
1878
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001879static int line_range_walk_cb(const char *fname, int lineno,
1880 Dwarf_Addr addr __used,
1881 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001882{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001883 struct line_finder *lf = data;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001884
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001885 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001886 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001887 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001888
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001889 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1890 return -EINVAL;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001891
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001892 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001893}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001894
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001895/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001896static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001897{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001898 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001899
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001900 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001901
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001902 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001903 if (ret >= 0)
1904 if (!list_empty(&lf->lr->line_list))
1905 ret = lf->found = 1;
1906 else
1907 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001908 else {
1909 free(lf->lr->path);
1910 lf->lr->path = NULL;
1911 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001912 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001913}
1914
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001915static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1916{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001917 struct dwarf_callback_param *param = data;
1918
1919 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001920 return DWARF_CB_ABORT; /* No need to find other instances */
1921}
1922
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001923/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001924static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001925{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001926 struct dwarf_callback_param *param = data;
1927 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001928 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001929
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001930 /* Check declared file */
1931 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1932 return DWARF_CB_OK;
1933
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001934 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001935 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001936 lf->fname = dwarf_decl_file(sp_die);
1937 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001938 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001939 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001940 if (lf->lno_s < 0) /* Overflow */
1941 lf->lno_s = INT_MAX;
1942 lf->lno_e = lr->offset + lr->end;
1943 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001944 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001945 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001946 lr->start = lf->lno_s;
1947 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001948 if (dwarf_func_inline(sp_die)) {
1949 struct dwarf_callback_param _param;
1950 _param.data = (void *)lf;
1951 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001952 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001953 line_range_inline_cb,
1954 &_param);
1955 param->retval = _param.retval;
1956 } else
1957 param->retval = find_line_range_by_line(sp_die, lf);
1958 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001959 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001960 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001961}
1962
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001963static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001964{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001965 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1966 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1967 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001968}
1969
1970int find_line_range(int fd, struct line_range *lr)
1971{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001972 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001973 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001974 Dwarf_Off off = 0, noff;
1975 size_t cuhl;
1976 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001977 Dwarf *dbg = NULL;
1978 Dwfl *dwfl;
1979 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001980 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001981
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001982 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001983 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001984 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001985 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001986 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001987 return -EBADF;
1988 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001989
Lin Mingcd25f8b2011-03-25 16:27:48 +08001990 /* Fastpath: lookup by function name from .debug_pubnames section */
1991 if (lr->function) {
1992 struct pubname_callback_param pubname_param = {
1993 .function = lr->function, .file = lr->file,
1994 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1995 struct dwarf_callback_param line_range_param = {
1996 .data = (void *)&lf, .retval = 0};
1997
1998 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1999 if (pubname_param.found) {
2000 line_range_search_cb(&lf.sp_die, &line_range_param);
2001 if (lf.found)
2002 goto found;
2003 }
2004 }
2005
Masami Hiramatsu804b3602010-02-25 08:35:42 -05002006 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002007 while (!lf.found && ret >= 0) {
2008 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002009 break;
2010
2011 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05002012 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
2013 if (!diep)
2014 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002015
2016 /* Check if target file is included. */
2017 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05002018 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05002019 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05002020 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002021
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05002022 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002023 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002024 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002025 else {
2026 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04002027 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002028 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002029 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002030 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05002031 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002032 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09002033
Lin Mingcd25f8b2011-03-25 16:27:48 +08002034found:
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09002035 /* Store comp_dir */
2036 if (lf.found) {
2037 comp_dir = cu_get_comp_dir(&lf.cu_die);
2038 if (comp_dir) {
2039 lr->comp_dir = strdup(comp_dir);
2040 if (!lr->comp_dir)
2041 ret = -ENOMEM;
2042 }
2043 }
2044
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09002045 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002046 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002047 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002048}
2049