blob: d443b643957fe0f95a8b3b6e70adfd7fba3b14e4 [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
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300364/* Get data_member_location offset */
365static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
366{
367 Dwarf_Attribute attr;
368 Dwarf_Op *expr;
369 size_t nexpr;
370 int ret;
371
372 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
373 return -ENOENT;
374
375 if (dwarf_formudata(&attr, offs) != 0) {
376 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
377 ret = dwarf_getlocation(&attr, &expr, &nexpr);
378 if (ret < 0 || nexpr == 0)
379 return -ENOENT;
380
381 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
382 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
383 expr[0].atom, nexpr);
384 return -ENOTSUP;
385 }
386 *offs = (Dwarf_Word)expr[0].number;
387 }
388 return 0;
389}
390
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400391/* Return values for die_find callbacks */
392enum {
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900393 DIE_FIND_CB_END = 0, /* End of Search */
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400394 DIE_FIND_CB_CHILD = 1, /* Search only children */
395 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
396 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
397};
398
399/* Search a child die */
400static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
401 int (*callback)(Dwarf_Die *, void *),
402 void *data, Dwarf_Die *die_mem)
403{
404 Dwarf_Die child_die;
405 int ret;
406
407 ret = dwarf_child(rt_die, die_mem);
408 if (ret != 0)
409 return NULL;
410
411 do {
412 ret = callback(die_mem, data);
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900413 if (ret == DIE_FIND_CB_END)
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400414 return die_mem;
415
416 if ((ret & DIE_FIND_CB_CHILD) &&
417 die_find_child(die_mem, callback, data, &child_die)) {
418 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
419 return die_mem;
420 }
421 } while ((ret & DIE_FIND_CB_SIBLING) &&
422 dwarf_siblingof(die_mem, die_mem) == 0);
423
424 return NULL;
425}
426
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500427struct __addr_die_search_param {
428 Dwarf_Addr addr;
429 Dwarf_Die *die_mem;
430};
431
432static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
433{
434 struct __addr_die_search_param *ad = data;
435
436 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
437 dwarf_haspc(fn_die, ad->addr)) {
438 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
439 return DWARF_CB_ABORT;
440 }
441 return DWARF_CB_OK;
442}
443
444/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400445static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
446 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500447{
448 struct __addr_die_search_param ad;
449 ad.addr = addr;
450 ad.die_mem = die_mem;
451 /* dwarf_getscopes can't find subprogram. */
452 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
453 return NULL;
454 else
455 return die_mem;
456}
457
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400458/* die_find callback for inline function search */
459static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
460{
461 Dwarf_Addr *addr = data;
462
463 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
464 dwarf_haspc(die_mem, *addr))
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900465 return DIE_FIND_CB_END;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400466
467 return DIE_FIND_CB_CONTINUE;
468}
469
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500470/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400471static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
472 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500473{
Masami Hiramatsu1d878082011-03-30 18:25:59 +0900474 Dwarf_Die tmp_die;
475
476 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
477 if (!sp_die)
478 return NULL;
479
480 /* Inlined function could be recursive. Trace it until fail */
481 while (sp_die) {
482 memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
483 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
484 &tmp_die);
485 }
486
487 return die_mem;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500488}
489
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900490/* Walker on lines (Note: line number will not be sorted) */
491typedef int (* line_walk_handler_t) (const char *fname, int lineno,
492 Dwarf_Addr addr, void *data);
493
494struct __line_walk_param {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900495 const char *fname;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900496 line_walk_handler_t handler;
497 void *data;
498 int retval;
499};
500
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900501static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
502{
503 struct __line_walk_param *lw = data;
504 Dwarf_Addr addr;
505 int lineno;
506
507 if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
508 lineno = die_get_call_lineno(in_die);
509 if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
510 lw->retval = lw->handler(lw->fname, lineno, addr,
511 lw->data);
512 if (lw->retval != 0)
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900513 return DIE_FIND_CB_END;
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900514 }
515 }
516 return DIE_FIND_CB_SIBLING;
517}
518
519/* Walk on lines of blocks included in given DIE */
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900520static int __die_walk_funclines(Dwarf_Die *sp_die,
521 line_walk_handler_t handler, void *data)
522{
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900523 struct __line_walk_param lw = {
524 .handler = handler,
525 .data = data,
526 .retval = 0,
527 };
528 Dwarf_Die die_mem;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900529 Dwarf_Addr addr;
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900530 int lineno;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900531
532 /* Handle function declaration line */
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900533 lw.fname = dwarf_decl_file(sp_die);
534 if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900535 dwarf_entrypc(sp_die, &addr) == 0) {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900536 lw.retval = handler(lw.fname, lineno, addr, data);
537 if (lw.retval != 0)
538 goto done;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900539 }
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900540 die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
541done:
542 return lw.retval;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900543}
544
545static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
546{
547 struct __line_walk_param *lw = data;
548
549 lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
550 if (lw->retval != 0)
551 return DWARF_CB_ABORT;
552
553 return DWARF_CB_OK;
554}
555
556/*
557 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
558 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
559 */
560static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
561 void *data)
562{
563 Dwarf_Lines *lines;
564 Dwarf_Line *line;
565 Dwarf_Addr addr;
566 const char *fname;
567 int lineno, ret = 0;
568 Dwarf_Die die_mem, *cu_die;
569 size_t nlines, i;
570
571 /* Get the CU die */
572 if (dwarf_tag(pdie) == DW_TAG_subprogram)
573 cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
574 else
575 cu_die = pdie;
576 if (!cu_die) {
577 pr_debug2("Failed to get CU from subprogram\n");
578 return -EINVAL;
579 }
580
581 /* Get lines list in the CU */
582 if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
583 pr_debug2("Failed to get source lines on this CU.\n");
584 return -ENOENT;
585 }
586 pr_debug2("Get %zd lines from this CU\n", nlines);
587
588 /* Walk on the lines on lines list */
589 for (i = 0; i < nlines; i++) {
590 line = dwarf_onesrcline(lines, i);
591 if (line == NULL ||
592 dwarf_lineno(line, &lineno) != 0 ||
593 dwarf_lineaddr(line, &addr) != 0) {
594 pr_debug2("Failed to get line info. "
595 "Possible error in debuginfo.\n");
596 continue;
597 }
598 /* Filter lines based on address */
599 if (pdie != cu_die)
600 /*
601 * Address filtering
602 * The line is included in given function, and
603 * no inline block includes it.
604 */
605 if (!dwarf_haspc(pdie, addr) ||
606 die_find_inlinefunc(pdie, addr, &die_mem))
607 continue;
608 /* Get source line */
609 fname = dwarf_linesrc(line, NULL, NULL);
610
611 ret = handler(fname, lineno, addr, data);
612 if (ret != 0)
613 return ret;
614 }
615
616 /*
617 * Dwarf lines doesn't include function declarations and inlined
618 * subroutines. We have to check functions list or given function.
619 */
620 if (pdie != cu_die)
621 ret = __die_walk_funclines(pdie, handler, data);
622 else {
623 struct __line_walk_param param = {
624 .handler = handler,
625 .data = data,
626 .retval = 0,
627 };
628 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
629 ret = param.retval;
630 }
631
632 return ret;
633}
634
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900635struct __find_variable_param {
636 const char *name;
637 Dwarf_Addr addr;
638};
639
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400640static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400641{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900642 struct __find_variable_param *fvp = data;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400643 int tag;
644
645 tag = dwarf_tag(die_mem);
646 if ((tag == DW_TAG_formal_parameter ||
647 tag == DW_TAG_variable) &&
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900648 die_compare_name(die_mem, fvp->name))
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900649 return DIE_FIND_CB_END;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400650
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900651 if (dwarf_haspc(die_mem, fvp->addr))
652 return DIE_FIND_CB_CONTINUE;
653 else
654 return DIE_FIND_CB_SIBLING;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400655}
656
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900657/* Find a variable called 'name' at given address */
658static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
659 Dwarf_Addr addr, Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500660{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900661 struct __find_variable_param fvp = { .name = name, .addr = addr};
662
663 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400664 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400665}
666
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400667static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
668{
669 const char *name = data;
670
671 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900672 die_compare_name(die_mem, name))
Masami Hiramatsubaad2d32011-06-27 16:27:09 +0900673 return DIE_FIND_CB_END;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400674
675 return DIE_FIND_CB_SIBLING;
676}
677
678/* Find a member called 'name' */
679static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
680 Dwarf_Die *die_mem)
681{
682 return die_find_child(st_die, __die_find_member_cb, (void *)name,
683 die_mem);
684}
685
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900686/* Get the name of given variable DIE */
687static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
688{
689 Dwarf_Die type;
690 int tag, ret, ret2;
691 const char *tmp = "";
692
693 if (__die_get_real_type(vr_die, &type) == NULL)
694 return -ENOENT;
695
696 tag = dwarf_tag(&type);
697 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
698 tmp = "*";
699 else if (tag == DW_TAG_subroutine_type) {
700 /* Function pointer */
701 ret = snprintf(buf, len, "(function_type)");
702 return (ret >= len) ? -E2BIG : ret;
703 } else {
704 if (!dwarf_diename(&type))
705 return -ENOENT;
706 if (tag == DW_TAG_union_type)
707 tmp = "union ";
708 else if (tag == DW_TAG_structure_type)
709 tmp = "struct ";
710 /* Write a base name */
711 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
712 return (ret >= len) ? -E2BIG : ret;
713 }
714 ret = die_get_typename(&type, buf, len);
715 if (ret > 0) {
716 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
717 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
718 }
719 return ret;
720}
721
722/* Get the name and type of given variable DIE, stored as "type\tname" */
723static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
724{
725 int ret, ret2;
726
727 ret = die_get_typename(vr_die, buf, len);
728 if (ret < 0) {
729 pr_debug("Failed to get type, make it unknown.\n");
730 ret = snprintf(buf, len, "(unknown_type)");
731 }
732 if (ret > 0) {
733 ret2 = snprintf(buf + ret, len - ret, "\t%s",
734 dwarf_diename(vr_die));
735 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
736 }
737 return ret;
738}
739
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400740/*
741 * Probe finder related functions
742 */
743
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530744static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400745{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530746 struct probe_trace_arg_ref *ref;
747 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400748 if (ref != NULL)
749 ref->offset = offs;
750 return ref;
751}
752
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900753/*
754 * Convert a location into trace_arg.
755 * If tvar == NULL, this just checks variable can be converted.
756 */
757static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
758 Dwarf_Op *fb_ops,
759 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400760{
761 Dwarf_Attribute attr;
762 Dwarf_Op *op;
763 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500764 unsigned int regn;
765 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400766 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400767 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400768 int ret;
769
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900770 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
771 goto static_var;
772
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400773 /* TODO: handle more than 1 exprs */
774 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900775 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400776 nops == 0) {
777 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400778 return -ENOENT;
779 }
780
781 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900782static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900783 if (!tvar)
784 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400785 /* Static variables on memory (not stack), make @varname */
786 ret = strlen(dwarf_diename(vr_die));
787 tvar->value = zalloc(ret + 2);
788 if (tvar->value == NULL)
789 return -ENOMEM;
790 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
791 tvar->ref = alloc_trace_arg_ref((long)offs);
792 if (tvar->ref == NULL)
793 return -ENOMEM;
794 return 0;
795 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400796
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400797 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500798 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900799 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400800 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400801 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500802 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900803 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500804 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400805
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500806 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
807 regn = op->atom - DW_OP_breg0;
808 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400809 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500810 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
811 regn = op->atom - DW_OP_reg0;
812 } else if (op->atom == DW_OP_bregx) {
813 regn = op->number;
814 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400815 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500816 } else if (op->atom == DW_OP_regx) {
817 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400818 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900819 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400820 return -ENOTSUP;
821 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400822
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900823 if (!tvar)
824 return 0;
825
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400826 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400827 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900828 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900829 pr_warning("Mapping for the register number %u "
830 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400831 return -ERANGE;
832 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400833
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400834 tvar->value = strdup(regs);
835 if (tvar->value == NULL)
836 return -ENOMEM;
837
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400838 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400839 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400840 if (tvar->ref == NULL)
841 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400842 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400843 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400844}
845
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900846#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
847
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400848static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530849 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400850 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400851{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530852 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400853 Dwarf_Die type;
854 char buf[16];
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900855 int bsize, boffs, total;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400856 int ret;
857
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400858 /* TODO: check all types */
859 if (cast && strcmp(cast, "string") != 0) {
860 /* Non string type is OK */
861 tvar->type = strdup(cast);
862 return (tvar->type == NULL) ? -ENOMEM : 0;
863 }
864
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900865 bsize = dwarf_bitsize(vr_die);
866 if (bsize > 0) {
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900867 /* This is a bitfield */
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900868 boffs = dwarf_bitoffset(vr_die);
869 total = dwarf_bytesize(vr_die);
870 if (boffs < 0 || total < 0)
871 return -ENOENT;
872 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
873 BYTES_TO_BITS(total));
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900874 goto formatted;
875 }
876
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400877 if (die_get_real_type(vr_die, &type) == NULL) {
878 pr_warning("Failed to get a type information of %s.\n",
879 dwarf_diename(vr_die));
880 return -ENOENT;
881 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400882
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400883 pr_debug("%s type is %s.\n",
884 dwarf_diename(vr_die), dwarf_diename(&type));
885
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400886 if (cast && strcmp(cast, "string") == 0) { /* String type */
887 ret = dwarf_tag(&type);
888 if (ret != DW_TAG_pointer_type &&
889 ret != DW_TAG_array_type) {
890 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900891 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400892 dwarf_diename(vr_die), dwarf_diename(&type));
893 return -EINVAL;
894 }
895 if (ret == DW_TAG_pointer_type) {
896 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900897 pr_warning("Failed to get a type"
898 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400899 return -ENOENT;
900 }
901 while (*ref_ptr)
902 ref_ptr = &(*ref_ptr)->next;
903 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530904 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400905 if (*ref_ptr == NULL) {
906 pr_warning("Out of memory error\n");
907 return -ENOMEM;
908 }
909 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900910 if (!die_compare_name(&type, "char") &&
911 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400912 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900913 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400914 dwarf_diename(vr_die));
915 return -EINVAL;
916 }
917 tvar->type = strdup(cast);
918 return (tvar->type == NULL) ? -ENOMEM : 0;
919 }
920
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900921 ret = dwarf_bytesize(&type);
922 if (ret <= 0)
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900923 /* No size ... try to use default type */
924 return 0;
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900925 ret = BYTES_TO_BITS(ret);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400926
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900927 /* Check the bitwidth */
928 if (ret > MAX_BASIC_TYPE_BITS) {
929 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
930 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
931 ret = MAX_BASIC_TYPE_BITS;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400932 }
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900933 ret = snprintf(buf, 16, "%c%d",
934 die_is_signed_type(&type) ? 's' : 'u', ret);
935
936formatted:
937 if (ret < 0 || ret >= 16) {
938 if (ret >= 16)
939 ret = -E2BIG;
940 pr_warning("Failed to convert variable type: %s\n",
941 strerror(-ret));
942 return ret;
943 }
944 tvar->type = strdup(buf);
945 if (tvar->type == NULL)
946 return -ENOMEM;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400947 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400948}
949
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400950static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400951 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530952 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400953 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400954{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530955 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400956 Dwarf_Die type;
957 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400958 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400959
960 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400961 if (die_get_real_type(vr_die, &type) == NULL) {
962 pr_warning("Failed to get the type of %s.\n", varname);
963 return -ENOENT;
964 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400965 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
966 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400967
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400968 if (field->name[0] == '[' &&
969 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
970 if (field->next)
971 /* Save original type for next field */
972 memcpy(die_mem, &type, sizeof(*die_mem));
973 /* Get the type of this array */
974 if (die_get_real_type(&type, &type) == NULL) {
975 pr_warning("Failed to get the type of %s.\n", varname);
976 return -ENOENT;
977 }
978 pr_debug2("Array real type: (%x)\n",
979 (unsigned)dwarf_dieoffset(&type));
980 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530981 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400982 if (ref == NULL)
983 return -ENOMEM;
984 if (*ref_ptr)
985 (*ref_ptr)->next = ref;
986 else
987 *ref_ptr = ref;
988 }
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900989 ref->offset += dwarf_bytesize(&type) * field->index;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400990 if (!field->next)
991 /* Save vr_die for converting types */
992 memcpy(die_mem, vr_die, sizeof(*die_mem));
993 goto next;
994 } else if (tag == DW_TAG_pointer_type) {
995 /* Check the pointer and dereference */
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 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001001 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001002 if (die_get_real_type(&type, &type) == NULL) {
1003 pr_warning("Failed to get the type of %s.\n", varname);
1004 return -ENOENT;
1005 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001006 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001007 if (dwarf_tag(&type) != DW_TAG_structure_type) {
1008 pr_warning("%s is not a data structure.\n", varname);
1009 return -EINVAL;
1010 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001011
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301012 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001013 if (ref == NULL)
1014 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001015 if (*ref_ptr)
1016 (*ref_ptr)->next = ref;
1017 else
1018 *ref_ptr = ref;
1019 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001020 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001021 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001022 pr_warning("%s is not a data structure.\n", varname);
1023 return -EINVAL;
1024 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001025 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001026 pr_err("Semantic error: %s is not a pointor"
1027 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001028 return -EINVAL;
1029 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001030 if (field->ref) {
1031 pr_err("Semantic error: %s must be referred by '.'\n",
1032 field->name);
1033 return -EINVAL;
1034 }
1035 if (!ref) {
1036 pr_warning("Structure on a register is not "
1037 "supported yet.\n");
1038 return -ENOTSUP;
1039 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001040 }
1041
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001042 if (die_find_member(&type, field->name, die_mem) == NULL) {
1043 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
1044 dwarf_diename(&type), field->name);
1045 return -EINVAL;
1046 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001047
1048 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001049 ret = die_get_data_member_location(die_mem, &offs);
1050 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001051 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001052 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001053 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001054 ref->offset += (long)offs;
1055
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001056next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001057 /* Converting next field */
1058 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001059 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001060 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001061 else
1062 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001063}
1064
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001065/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001066static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001067{
Masami Hiramatsu49849122010-04-12 13:17:15 -04001068 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001069 int ret;
1070
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001071 pr_debug("Converting variable %s into trace event.\n",
1072 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001073
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001074 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
1075 pf->tvar);
1076 if (ret == -ENOENT)
1077 pr_err("Failed to find the location of %s at this address.\n"
1078 " Perhaps, it has been optimized out.\n", pf->pvar->var);
1079 else if (ret == -ENOTSUP)
1080 pr_err("Sorry, we don't support this variable location yet.\n");
1081 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001082 ret = convert_variable_fields(vr_die, pf->pvar->var,
1083 pf->pvar->field, &pf->tvar->ref,
1084 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001085 vr_die = &die_mem;
1086 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -04001087 if (ret == 0)
1088 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001089 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001090 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001091}
1092
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001093/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001094static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001095{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001096 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001097 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001098 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001099
Masami Hiramatsu367e94c2010-08-27 20:38:59 +09001100 if (!is_c_varname(pf->pvar->var)) {
1101 /* Copy raw parameters */
1102 pf->tvar->value = strdup(pf->pvar->var);
1103 if (pf->tvar->value == NULL)
1104 return -ENOMEM;
1105 if (pf->pvar->type) {
1106 pf->tvar->type = strdup(pf->pvar->type);
1107 if (pf->tvar->type == NULL)
1108 return -ENOMEM;
1109 }
1110 if (pf->pvar->name) {
1111 pf->tvar->name = strdup(pf->pvar->name);
1112 if (pf->tvar->name == NULL)
1113 return -ENOMEM;
1114 } else
1115 pf->tvar->name = NULL;
1116 return 0;
1117 }
1118
Masami Hiramatsu48481932010-04-12 13:16:53 -04001119 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001120 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001121 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001122 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
1123 if (ret < 0)
1124 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001125 ptr = strchr(buf, ':'); /* Change type separator to _ */
1126 if (ptr)
1127 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001128 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001129 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001130 if (pf->tvar->name == NULL)
1131 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001132
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001133 pr_debug("Searching '%s' variable in context.\n",
1134 pf->pvar->var);
1135 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +09001136 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001137 ret = convert_variable(&vr_die, pf);
1138 else {
1139 /* Search upper class */
1140 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001141 while (nscopes-- > 1) {
1142 pr_debug("Searching variables in %s\n",
1143 dwarf_diename(&scopes[nscopes]));
1144 /* We should check this scope, so give dummy address */
1145 if (die_find_variable_at(&scopes[nscopes],
1146 pf->pvar->var, 0,
1147 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001148 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001149 goto found;
1150 }
1151 }
1152 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001153 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001154 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001155 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001156found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001157 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001158 pr_warning("Failed to find '%s' in this function.\n",
1159 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001160 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001161}
1162
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001163/* Convert subprogram DIE to trace point */
1164static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
1165 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001166{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001167 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001168 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001169
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001170 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001171 name = dwarf_diename(sp_die);
1172 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001173 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001174 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001175 dwarf_diename(sp_die));
1176 return -ENOENT;
1177 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001178 tp->symbol = strdup(name);
1179 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001180 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001181 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001182 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001183 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001184 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001185
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001186 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001187 if (retprobe) {
1188 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001189 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001190 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001191 return -EINVAL;
1192 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001193 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001194 }
1195
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001196 return 0;
1197}
1198
1199/* Call probe_finder callback with real subprogram DIE */
1200static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
1201{
1202 Dwarf_Die die_mem;
1203 Dwarf_Attribute fb_attr;
1204 size_t nops;
1205 int ret;
1206
1207 /* If no real subprogram, find a real one */
1208 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
1209 sp_die = die_find_real_subprogram(&pf->cu_die,
1210 pf->addr, &die_mem);
1211 if (!sp_die) {
1212 pr_warning("Failed to find probe point in any "
1213 "functions.\n");
1214 return -ENOENT;
1215 }
1216 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001217
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001218 /* Get the frame base attribute/ops */
1219 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -04001220 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001221 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001222 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001223#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001224 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1225 pf->cfi != NULL) {
1226 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001227 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1228 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001229 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001230 (uintmax_t)pf->addr);
1231 return -ENOENT;
1232 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001233#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001234 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001235
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001236 /* Call finder's callback handler */
1237 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001238
1239 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1240 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001241
1242 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001243}
1244
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001245static int probe_point_line_walker(const char *fname, int lineno,
1246 Dwarf_Addr addr, void *data)
1247{
1248 struct probe_finder *pf = data;
1249 int ret;
1250
1251 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
1252 return 0;
1253
1254 pf->addr = addr;
1255 ret = call_probe_finder(NULL, pf);
1256
1257 /* Continue if no error, because the line will be in inline function */
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001258 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001259}
1260
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001261/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001262static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001263{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001264 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001265}
1266
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001267/* Find lines which match lazy pattern */
1268static int find_lazy_match_lines(struct list_head *head,
1269 const char *fname, const char *pat)
1270{
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001271 FILE *fp;
1272 char *line = NULL;
1273 size_t line_len;
1274 ssize_t len;
1275 int count = 0, linenum = 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001276
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001277 fp = fopen(fname, "r");
1278 if (!fp) {
1279 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001280 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001281 }
1282
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001283 while ((len = getline(&line, &line_len, fp)) > 0) {
1284
1285 if (line[len - 1] == '\n')
1286 line[len - 1] = '\0';
1287
1288 if (strlazymatch(line, pat)) {
1289 line_list__add_line(head, linenum);
1290 count++;
1291 }
1292 linenum++;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001293 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001294
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001295 if (ferror(fp))
1296 count = -errno;
1297 free(line);
1298 fclose(fp);
1299
1300 if (count == 0)
1301 pr_debug("No matched lines found in %s.\n", fname);
1302 return count;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001303}
1304
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001305static int probe_point_lazy_walker(const char *fname, int lineno,
1306 Dwarf_Addr addr, void *data)
1307{
1308 struct probe_finder *pf = data;
1309 int ret;
1310
1311 if (!line_list__has_line(&pf->lcache, lineno) ||
1312 strtailcmp(fname, pf->fname) != 0)
1313 return 0;
1314
1315 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1316 lineno, (unsigned long long)addr);
1317 pf->addr = addr;
1318 ret = call_probe_finder(NULL, pf);
1319
1320 /*
1321 * Continue if no error, because the lazy pattern will match
1322 * to other lines
1323 */
Ingo Molnar5e814dd2011-03-15 20:51:09 +01001324 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001325}
1326
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001327/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001328static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001329{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001330 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001331
1332 if (list_empty(&pf->lcache)) {
1333 /* Matching lazy line pattern */
1334 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001335 pf->pev->point.lazy_line);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001336 if (ret <= 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001337 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001338 }
1339
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001340 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001341}
1342
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001343/* Callback parameter with return value */
1344struct dwarf_callback_param {
1345 void *data;
1346 int retval;
1347};
1348
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001349static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001350{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001351 struct dwarf_callback_param *param = data;
1352 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001354 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001355
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001356 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001357 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001358 else {
1359 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001360 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001361 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001362 dwarf_diename(in_die));
1363 param->retval = -ENOENT;
1364 return DWARF_CB_ABORT;
1365 }
1366 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001367 pf->addr += pp->offset;
1368 pr_debug("found inline addr: 0x%jx\n",
1369 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001370
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001371 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001372 if (param->retval < 0)
1373 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001374 }
1375
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001376 return DWARF_CB_OK;
1377}
1378
1379/* Search function from function name */
1380static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1381{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001382 struct dwarf_callback_param *param = data;
1383 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001384 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001385
1386 /* Check tag and diename */
1387 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001388 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001389 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001390
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001391 /* Check declared file */
1392 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1393 return DWARF_CB_OK;
1394
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001395 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001396 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001397 dwarf_decl_line(sp_die, &pf->lno);
1398 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001399 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001400 } else if (!dwarf_func_inline(sp_die)) {
1401 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001402 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001403 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001404 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001405 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001406 pr_warning("Failed to get entry address of "
1407 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001408 param->retval = -ENOENT;
1409 return DWARF_CB_ABORT;
1410 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001411 pf->addr += pp->offset;
1412 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001413 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001414 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001415 } else {
1416 struct dwarf_callback_param _param = {.data = (void *)pf,
1417 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001418 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001419 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1420 &_param);
1421 param->retval = _param.retval;
1422 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001423
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001424 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001425}
1426
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001427static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001428{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001429 struct dwarf_callback_param _param = {.data = (void *)pf,
1430 .retval = 0};
1431 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1432 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001433}
1434
Lin Mingcd25f8b2011-03-25 16:27:48 +08001435struct pubname_callback_param {
1436 char *function;
1437 char *file;
1438 Dwarf_Die *cu_die;
1439 Dwarf_Die *sp_die;
1440 int found;
1441};
1442
1443static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1444{
1445 struct pubname_callback_param *param = data;
1446
1447 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1448 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1449 return DWARF_CB_OK;
1450
1451 if (die_compare_name(param->sp_die, param->function)) {
1452 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1453 return DWARF_CB_OK;
1454
1455 if (param->file &&
1456 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1457 return DWARF_CB_OK;
1458
1459 param->found = 1;
1460 return DWARF_CB_ABORT;
1461 }
1462 }
1463
1464 return DWARF_CB_OK;
1465}
1466
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001467/* Find probe points from debuginfo */
1468static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001469{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001470 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001471 Dwarf_Off off, noff;
1472 size_t cuhl;
1473 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001474 Dwarf *dbg = NULL;
1475 Dwfl *dwfl;
1476 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001477 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001478
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001479 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001480 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001481 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001482 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001483 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001484 return -EBADF;
1485 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001486
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001487#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001488 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001489 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001490#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001491
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001492 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001493 line_list__init(&pf->lcache);
Lin Mingcd25f8b2011-03-25 16:27:48 +08001494
1495 /* Fastpath: lookup by function name from .debug_pubnames section */
1496 if (pp->function) {
1497 struct pubname_callback_param pubname_param = {
1498 .function = pp->function,
1499 .file = pp->file,
1500 .cu_die = &pf->cu_die,
1501 .sp_die = &pf->sp_die,
Lin Ming2b348a72011-04-29 08:41:57 +00001502 .found = 0,
Lin Mingcd25f8b2011-03-25 16:27:48 +08001503 };
1504 struct dwarf_callback_param probe_param = {
1505 .data = pf,
1506 };
1507
1508 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1509 if (pubname_param.found) {
1510 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1511 if (ret)
1512 goto found;
1513 }
1514 }
1515
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001516 /* Loop on CUs (Compilation Unit) */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001517 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001518 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001519 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001520 if (!diep)
1521 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001522
1523 /* Check if target file is included. */
1524 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001525 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001526 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001527 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001528
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001529 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001530 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001531 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001532 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001533 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001534 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001535 pf->lno = pp->line;
1536 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001537 }
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001538 if (ret < 0)
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001539 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001540 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001541 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001542 }
Lin Mingcd25f8b2011-03-25 16:27:48 +08001543
1544found:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001545 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001546 if (dwfl)
1547 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001548
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001549 return ret;
1550}
1551
1552/* Add a found probe point into trace event list */
1553static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1554{
1555 struct trace_event_finder *tf =
1556 container_of(pf, struct trace_event_finder, pf);
1557 struct probe_trace_event *tev;
1558 int ret, i;
1559
1560 /* Check number of tevs */
1561 if (tf->ntevs == tf->max_tevs) {
1562 pr_warning("Too many( > %d) probe point found.\n",
1563 tf->max_tevs);
1564 return -ERANGE;
1565 }
1566 tev = &tf->tevs[tf->ntevs++];
1567
1568 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1569 &tev->point);
1570 if (ret < 0)
1571 return ret;
1572
1573 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1574 tev->point.offset);
1575
1576 /* Find each argument */
1577 tev->nargs = pf->pev->nargs;
1578 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1579 if (tev->args == NULL)
1580 return -ENOMEM;
1581 for (i = 0; i < pf->pev->nargs; i++) {
1582 pf->pvar = &pf->pev->args[i];
1583 pf->tvar = &tev->args[i];
1584 ret = find_variable(sp_die, pf);
1585 if (ret != 0)
1586 return ret;
1587 }
1588
1589 return 0;
1590}
1591
1592/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1593int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1594 struct probe_trace_event **tevs, int max_tevs)
1595{
1596 struct trace_event_finder tf = {
1597 .pf = {.pev = pev, .callback = add_probe_trace_event},
1598 .max_tevs = max_tevs};
1599 int ret;
1600
1601 /* Allocate result tevs array */
1602 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1603 if (*tevs == NULL)
1604 return -ENOMEM;
1605
1606 tf.tevs = *tevs;
1607 tf.ntevs = 0;
1608
1609 ret = find_probes(fd, &tf.pf);
1610 if (ret < 0) {
1611 free(*tevs);
1612 *tevs = NULL;
1613 return ret;
1614 }
1615
1616 return (ret < 0) ? ret : tf.ntevs;
1617}
1618
1619#define MAX_VAR_LEN 64
1620
1621/* Collect available variables in this scope */
1622static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1623{
1624 struct available_var_finder *af = data;
1625 struct variable_list *vl;
1626 char buf[MAX_VAR_LEN];
1627 int tag, ret;
1628
1629 vl = &af->vls[af->nvls - 1];
1630
1631 tag = dwarf_tag(die_mem);
1632 if (tag == DW_TAG_formal_parameter ||
1633 tag == DW_TAG_variable) {
1634 ret = convert_variable_location(die_mem, af->pf.addr,
1635 af->pf.fb_ops, NULL);
1636 if (ret == 0) {
1637 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001638 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001639 if (ret > 0)
1640 strlist__add(vl->vars, buf);
1641 }
1642 }
1643
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001644 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001645 return DIE_FIND_CB_CONTINUE;
1646 else
1647 return DIE_FIND_CB_SIBLING;
1648}
1649
1650/* Add a found vars into available variables list */
1651static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1652{
1653 struct available_var_finder *af =
1654 container_of(pf, struct available_var_finder, pf);
1655 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001656 Dwarf_Die die_mem, *scopes = NULL;
1657 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001658
1659 /* Check number of tevs */
1660 if (af->nvls == af->max_vls) {
1661 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1662 return -ERANGE;
1663 }
1664 vl = &af->vls[af->nvls++];
1665
1666 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1667 &vl->point);
1668 if (ret < 0)
1669 return ret;
1670
1671 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1672 vl->point.offset);
1673
1674 /* Find local variables */
1675 vl->vars = strlist__new(true, NULL);
1676 if (vl->vars == NULL)
1677 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001678 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001679 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1680
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001681 /* Find external variables */
1682 if (!af->externs)
1683 goto out;
1684 /* Don't need to search child DIE for externs. */
1685 af->child = false;
1686 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1687 while (nscopes-- > 1)
1688 die_find_child(&scopes[nscopes], collect_variables_cb,
1689 (void *)af, &die_mem);
1690 if (scopes)
1691 free(scopes);
1692
1693out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001694 if (strlist__empty(vl->vars)) {
1695 strlist__delete(vl->vars);
1696 vl->vars = NULL;
1697 }
1698
1699 return ret;
1700}
1701
1702/* Find available variables at given probe point */
1703int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001704 struct variable_list **vls, int max_vls,
1705 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001706{
1707 struct available_var_finder af = {
1708 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001709 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001710 int ret;
1711
1712 /* Allocate result vls array */
1713 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1714 if (*vls == NULL)
1715 return -ENOMEM;
1716
1717 af.vls = *vls;
1718 af.nvls = 0;
1719
1720 ret = find_probes(fd, &af.pf);
1721 if (ret < 0) {
1722 /* Free vlist for error */
1723 while (af.nvls--) {
1724 if (af.vls[af.nvls].point.symbol)
1725 free(af.vls[af.nvls].point.symbol);
1726 if (af.vls[af.nvls].vars)
1727 strlist__delete(af.vls[af.nvls].vars);
1728 }
1729 free(af.vls);
1730 *vls = NULL;
1731 return ret;
1732 }
1733
1734 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001735}
1736
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001737/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001738int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001739{
1740 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001741 Dwarf *dbg = NULL;
1742 Dwfl *dwfl = NULL;
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001743 Dwarf_Addr _addr, baseaddr, bias = 0;
1744 const char *fname = NULL, *func = NULL, *tmp;
1745 int baseline = 0, lineno = 0, ret = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001746
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001747 /* Open the live linux kernel */
1748 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1749 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001750 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001751 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1752 ret = -EINVAL;
1753 goto end;
1754 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001755
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001756 /* Adjust address with bias */
1757 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001758 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001759 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001760 pr_warning("Failed to find debug information for address %lx\n",
1761 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001762 ret = -EINVAL;
1763 goto end;
1764 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001765
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001766 /* Find a corresponding line (filename and lineno) */
1767 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1768 /* Don't care whether it failed or not */
1769
1770 /* Find a corresponding function (name, baseline and baseaddr) */
1771 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1772 /* Get function entry information */
1773 tmp = dwarf_diename(&spdie);
1774 if (!tmp ||
1775 dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1776 dwarf_decl_line(&spdie, &baseline) != 0)
1777 goto post;
1778 func = tmp;
1779
1780 if (addr == (unsigned long)baseaddr)
1781 /* Function entry - Relative line number is 0 */
1782 lineno = baseline;
1783 else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1784 &indie)) {
1785 if (dwarf_entrypc(&indie, &_addr) == 0 &&
1786 _addr == addr)
1787 /*
1788 * addr is at an inline function entry.
1789 * In this case, lineno should be the call-site
1790 * line number.
1791 */
1792 lineno = die_get_call_lineno(&indie);
1793 else {
1794 /*
1795 * addr is in an inline function body.
1796 * Since lineno points one of the lines
1797 * of the inline function, baseline should
1798 * be the entry line of the inline function.
1799 */
1800 tmp = dwarf_diename(&indie);
1801 if (tmp &&
1802 dwarf_decl_line(&spdie, &baseline) == 0)
1803 func = tmp;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001804 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001805 }
1806 }
1807
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001808post:
1809 /* Make a relative line number or an offset */
1810 if (lineno)
1811 ppt->line = lineno - baseline;
1812 else if (func)
1813 ppt->offset = addr - (unsigned long)baseaddr;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001814
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001815 /* Duplicate strings */
1816 if (func) {
1817 ppt->function = strdup(func);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001818 if (ppt->function == NULL) {
1819 ret = -ENOMEM;
1820 goto end;
1821 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001822 }
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001823 if (fname) {
1824 ppt->file = strdup(fname);
1825 if (ppt->file == NULL) {
1826 if (ppt->function) {
1827 free(ppt->function);
1828 ppt->function = NULL;
1829 }
1830 ret = -ENOMEM;
1831 goto end;
1832 }
1833 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001834end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001835 if (dwfl)
1836 dwfl_end(dwfl);
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001837 if (ret == 0 && (fname || func))
1838 ret = 1; /* Found a point */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001839 return ret;
1840}
1841
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001842/* Add a line and store the src path */
1843static int line_range_add_line(const char *src, unsigned int lineno,
1844 struct line_range *lr)
1845{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001846 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001847 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001848 lr->path = strdup(src);
1849 if (lr->path == NULL)
1850 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001851 }
1852 return line_list__add_line(&lr->line_list, lineno);
1853}
1854
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001855static int line_range_walk_cb(const char *fname, int lineno,
1856 Dwarf_Addr addr __used,
1857 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001858{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001859 struct line_finder *lf = data;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001860
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001861 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001862 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001863 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001864
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001865 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1866 return -EINVAL;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001867
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001868 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001869}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001870
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001871/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001872static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001873{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001874 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001875
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001876 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001877
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001878 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001879 if (ret >= 0)
1880 if (!list_empty(&lf->lr->line_list))
1881 ret = lf->found = 1;
1882 else
1883 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001884 else {
1885 free(lf->lr->path);
1886 lf->lr->path = NULL;
1887 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001888 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001889}
1890
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001891static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1892{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001893 struct dwarf_callback_param *param = data;
1894
1895 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001896 return DWARF_CB_ABORT; /* No need to find other instances */
1897}
1898
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001899/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001900static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001901{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001902 struct dwarf_callback_param *param = data;
1903 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001904 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001905
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001906 /* Check declared file */
1907 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1908 return DWARF_CB_OK;
1909
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001910 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001911 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001912 lf->fname = dwarf_decl_file(sp_die);
1913 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001914 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001915 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001916 if (lf->lno_s < 0) /* Overflow */
1917 lf->lno_s = INT_MAX;
1918 lf->lno_e = lr->offset + lr->end;
1919 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001920 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001921 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001922 lr->start = lf->lno_s;
1923 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001924 if (dwarf_func_inline(sp_die)) {
1925 struct dwarf_callback_param _param;
1926 _param.data = (void *)lf;
1927 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001928 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001929 line_range_inline_cb,
1930 &_param);
1931 param->retval = _param.retval;
1932 } else
1933 param->retval = find_line_range_by_line(sp_die, lf);
1934 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001935 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001936 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001937}
1938
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001939static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001940{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001941 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1942 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1943 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001944}
1945
1946int find_line_range(int fd, struct line_range *lr)
1947{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001948 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001949 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001950 Dwarf_Off off = 0, noff;
1951 size_t cuhl;
1952 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001953 Dwarf *dbg = NULL;
1954 Dwfl *dwfl;
1955 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001956 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001957
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001958 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001959 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001960 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001961 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001962 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001963 return -EBADF;
1964 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001965
Lin Mingcd25f8b2011-03-25 16:27:48 +08001966 /* Fastpath: lookup by function name from .debug_pubnames section */
1967 if (lr->function) {
1968 struct pubname_callback_param pubname_param = {
1969 .function = lr->function, .file = lr->file,
1970 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1971 struct dwarf_callback_param line_range_param = {
1972 .data = (void *)&lf, .retval = 0};
1973
1974 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1975 if (pubname_param.found) {
1976 line_range_search_cb(&lf.sp_die, &line_range_param);
1977 if (lf.found)
1978 goto found;
1979 }
1980 }
1981
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001982 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001983 while (!lf.found && ret >= 0) {
1984 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001985 break;
1986
1987 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001988 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1989 if (!diep)
1990 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001991
1992 /* Check if target file is included. */
1993 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001994 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001995 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001996 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001997
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001998 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001999 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002000 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002001 else {
2002 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04002003 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002004 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002005 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002006 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05002007 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002008 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09002009
Lin Mingcd25f8b2011-03-25 16:27:48 +08002010found:
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09002011 /* Store comp_dir */
2012 if (lf.found) {
2013 comp_dir = cu_get_comp_dir(&lf.cu_die);
2014 if (comp_dir) {
2015 lr->comp_dir = strdup(comp_dir);
2016 if (!lr->comp_dir)
2017 ret = -ENOMEM;
2018 }
2019 }
2020
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09002021 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09002022 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04002023 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05002024}
2025