blob: 90b629226bd74bc27f9ba0c608f524608221a36f [file] [log] [blame]
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001/*
2 * probe-finder.c : C expression to kprobe event converter
3 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <getopt.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdarg.h>
33#include <ctype.h>
Ian Munsiecd932c52010-04-20 16:58:32 +100034#include <dwarf-regs.h>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040035
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040036#include "event.h"
37#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040038#include "util.h"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040039#include "symbol.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040040#include "probe-finder.h"
41
Masami Hiramatsu49849122010-04-12 13:17:15 -040042/* Kprobe tracer basic type is up to u64 */
43#define MAX_BASIC_TYPE_BITS 64
44
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040045/*
46 * Compare the tail of two strings.
47 * Return 0 if whole of either string is same as another's tail part.
48 */
49static int strtailcmp(const char *s1, const char *s2)
50{
51 int i1 = strlen(s1);
52 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -050053 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040054 if (s1[i1] != s2[i2])
55 return s1[i1] - s2[i2];
56 }
57 return 0;
58}
59
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050060/* Line number list operations */
61
62/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040063static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050064{
65 struct line_node *ln;
66 struct list_head *p;
67
68 /* Reverse search, because new line will be the last one */
69 list_for_each_entry_reverse(ln, head, list) {
70 if (ln->line < line) {
71 p = &ln->list;
72 goto found;
73 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040074 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050075 }
76 /* List is empty, or the smallest entry */
77 p = head;
78found:
79 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040080 ln = zalloc(sizeof(struct line_node));
81 if (ln == NULL)
82 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050083 ln->line = line;
84 INIT_LIST_HEAD(&ln->list);
85 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040086 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050087}
88
89/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040090static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050091{
92 struct line_node *ln;
93
94 /* Reverse search, because new line will be the last one */
95 list_for_each_entry(ln, head, list)
96 if (ln->line == line)
97 return 1;
98
99 return 0;
100}
101
102/* Init line number list */
103static void line_list__init(struct list_head *head)
104{
105 INIT_LIST_HEAD(head);
106}
107
108/* Free line number list */
109static void line_list__free(struct list_head *head)
110{
111 struct line_node *ln;
112 while (!list_empty(head)) {
113 ln = list_first_entry(head, struct line_node, list);
114 list_del(&ln->list);
115 free(ln);
116 }
117}
118
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900119/* Dwarf FL wrappers */
120
121static int __linux_kernel_find_elf(Dwfl_Module *mod,
122 void **userdata,
123 const char *module_name,
124 Dwarf_Addr base,
125 char **file_name, Elf **elfp)
126{
127 int fd;
128 const char *path = kernel_get_module_path(module_name);
129
130 if (path) {
131 fd = open(path, O_RDONLY);
132 if (fd >= 0) {
133 *file_name = strdup(path);
134 return fd;
135 }
136 }
137 /* If failed, try to call standard method */
138 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
139 file_name, elfp);
140}
141
142static char *debuginfo_path; /* Currently dummy */
143
144static const Dwfl_Callbacks offline_callbacks = {
145 .find_debuginfo = dwfl_standard_find_debuginfo,
146 .debuginfo_path = &debuginfo_path,
147
148 .section_address = dwfl_offline_section_address,
149
150 /* We use this table for core files too. */
151 .find_elf = dwfl_build_id_find_elf,
152};
153
154static const Dwfl_Callbacks kernel_callbacks = {
155 .find_debuginfo = dwfl_standard_find_debuginfo,
156 .debuginfo_path = &debuginfo_path,
157
158 .find_elf = __linux_kernel_find_elf,
159 .section_address = dwfl_linux_kernel_module_section_address,
160};
161
162/* Get a Dwarf from offline image */
163static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
164{
165 Dwfl_Module *mod;
166 Dwarf *dbg = NULL;
167
168 if (!dwflp)
169 return NULL;
170
171 *dwflp = dwfl_begin(&offline_callbacks);
172 if (!*dwflp)
173 return NULL;
174
175 mod = dwfl_report_offline(*dwflp, "", "", fd);
176 if (!mod)
177 goto error;
178
179 dbg = dwfl_module_getdwarf(mod, bias);
180 if (!dbg) {
181error:
182 dwfl_end(*dwflp);
183 *dwflp = NULL;
184 }
185 return dbg;
186}
187
188/* Get a Dwarf from live kernel image */
189static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
190 Dwarf_Addr *bias)
191{
192 Dwarf *dbg;
193
194 if (!dwflp)
195 return NULL;
196
197 *dwflp = dwfl_begin(&kernel_callbacks);
198 if (!*dwflp)
199 return NULL;
200
201 /* Load the kernel dwarves: Don't care the result here */
202 dwfl_linux_kernel_report_kernel(*dwflp);
203 dwfl_linux_kernel_report_modules(*dwflp);
204
205 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
206 /* Here, check whether we could get a real dwarf */
207 if (!dbg) {
208 dwfl_end(*dwflp);
209 *dwflp = NULL;
210 }
211 return dbg;
212}
213
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500214/* Dwarf wrappers */
215
216/* Find the realpath of the target file. */
217static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400218{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500219 Dwarf_Files *files;
220 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300221 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400222 int ret;
223
224 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500225 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500226
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500227 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500228 if (ret != 0)
229 return NULL;
230
231 for (i = 0; i < nfiles; i++) {
232 src = dwarf_filesrc(files, i, NULL, NULL);
233 if (strtailcmp(src, fname) == 0)
234 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500235 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400236 if (i == nfiles)
237 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500238 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500239}
240
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900241/* Get DW_AT_comp_dir (should be NULL with older gcc) */
242static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
243{
244 Dwarf_Attribute attr;
245 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
246 return NULL;
247 return dwarf_formstring(&attr);
248}
249
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400250/* Compare diename and tname */
251static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
252{
253 const char *name;
254 name = dwarf_diename(dw_die);
Masami Hiramatsu82175632010-07-09 18:29:17 +0900255 return name ? (strcmp(tname, name) == 0) : false;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400256}
257
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900258/* Get type die */
259static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
260{
261 Dwarf_Attribute attr;
262
263 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
264 dwarf_formref_die(&attr, die_mem))
265 return die_mem;
266 else
267 return NULL;
268}
269
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900270/* Get a type die, but skip qualifiers */
271static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400272{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400273 int tag;
274
275 do {
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900276 vr_die = die_get_type(vr_die, die_mem);
277 if (!vr_die)
278 break;
279 tag = dwarf_tag(vr_die);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400280 } while (tag == DW_TAG_const_type ||
281 tag == DW_TAG_restrict_type ||
282 tag == DW_TAG_volatile_type ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900283 tag == DW_TAG_shared_type);
284
285 return vr_die;
286}
287
288/* Get a type die, but skip qualifiers and typedef */
289static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
290{
291 do {
292 vr_die = __die_get_real_type(vr_die, die_mem);
293 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400294
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900295 return vr_die;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400296}
297
Masami Hiramatsu49849122010-04-12 13:17:15 -0400298static bool die_is_signed_type(Dwarf_Die *tp_die)
299{
300 Dwarf_Attribute attr;
301 Dwarf_Word ret;
302
303 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
304 dwarf_formudata(&attr, &ret) != 0)
305 return false;
306
307 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
308 ret == DW_ATE_signed_fixed);
309}
310
311static int die_get_byte_size(Dwarf_Die *tp_die)
312{
313 Dwarf_Attribute attr;
314 Dwarf_Word ret;
315
316 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
317 dwarf_formudata(&attr, &ret) != 0)
318 return 0;
319
320 return (int)ret;
321}
322
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300323/* Get data_member_location offset */
324static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
325{
326 Dwarf_Attribute attr;
327 Dwarf_Op *expr;
328 size_t nexpr;
329 int ret;
330
331 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
332 return -ENOENT;
333
334 if (dwarf_formudata(&attr, offs) != 0) {
335 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
336 ret = dwarf_getlocation(&attr, &expr, &nexpr);
337 if (ret < 0 || nexpr == 0)
338 return -ENOENT;
339
340 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
341 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
342 expr[0].atom, nexpr);
343 return -ENOTSUP;
344 }
345 *offs = (Dwarf_Word)expr[0].number;
346 }
347 return 0;
348}
349
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400350/* Return values for die_find callbacks */
351enum {
352 DIE_FIND_CB_FOUND = 0, /* End of Search */
353 DIE_FIND_CB_CHILD = 1, /* Search only children */
354 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
355 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
356};
357
358/* Search a child die */
359static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
360 int (*callback)(Dwarf_Die *, void *),
361 void *data, Dwarf_Die *die_mem)
362{
363 Dwarf_Die child_die;
364 int ret;
365
366 ret = dwarf_child(rt_die, die_mem);
367 if (ret != 0)
368 return NULL;
369
370 do {
371 ret = callback(die_mem, data);
372 if (ret == DIE_FIND_CB_FOUND)
373 return die_mem;
374
375 if ((ret & DIE_FIND_CB_CHILD) &&
376 die_find_child(die_mem, callback, data, &child_die)) {
377 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
378 return die_mem;
379 }
380 } while ((ret & DIE_FIND_CB_SIBLING) &&
381 dwarf_siblingof(die_mem, die_mem) == 0);
382
383 return NULL;
384}
385
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500386struct __addr_die_search_param {
387 Dwarf_Addr addr;
388 Dwarf_Die *die_mem;
389};
390
391static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
392{
393 struct __addr_die_search_param *ad = data;
394
395 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
396 dwarf_haspc(fn_die, ad->addr)) {
397 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
398 return DWARF_CB_ABORT;
399 }
400 return DWARF_CB_OK;
401}
402
403/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400404static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
405 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500406{
407 struct __addr_die_search_param ad;
408 ad.addr = addr;
409 ad.die_mem = die_mem;
410 /* dwarf_getscopes can't find subprogram. */
411 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
412 return NULL;
413 else
414 return die_mem;
415}
416
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400417/* die_find callback for inline function search */
418static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
419{
420 Dwarf_Addr *addr = data;
421
422 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
423 dwarf_haspc(die_mem, *addr))
424 return DIE_FIND_CB_FOUND;
425
426 return DIE_FIND_CB_CONTINUE;
427}
428
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500429/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400430static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
431 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500432{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400433 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500434}
435
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900436struct __find_variable_param {
437 const char *name;
438 Dwarf_Addr addr;
439};
440
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400441static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400442{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900443 struct __find_variable_param *fvp = data;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400444 int tag;
445
446 tag = dwarf_tag(die_mem);
447 if ((tag == DW_TAG_formal_parameter ||
448 tag == DW_TAG_variable) &&
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900449 die_compare_name(die_mem, fvp->name))
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400450 return DIE_FIND_CB_FOUND;
451
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900452 if (dwarf_haspc(die_mem, fvp->addr))
453 return DIE_FIND_CB_CONTINUE;
454 else
455 return DIE_FIND_CB_SIBLING;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400456}
457
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900458/* Find a variable called 'name' at given address */
459static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
460 Dwarf_Addr addr, Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500461{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900462 struct __find_variable_param fvp = { .name = name, .addr = addr};
463
464 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400465 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400466}
467
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400468static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
469{
470 const char *name = data;
471
472 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900473 die_compare_name(die_mem, name))
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400474 return DIE_FIND_CB_FOUND;
475
476 return DIE_FIND_CB_SIBLING;
477}
478
479/* Find a member called 'name' */
480static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
481 Dwarf_Die *die_mem)
482{
483 return die_find_child(st_die, __die_find_member_cb, (void *)name,
484 die_mem);
485}
486
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900487/* Get the name of given variable DIE */
488static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
489{
490 Dwarf_Die type;
491 int tag, ret, ret2;
492 const char *tmp = "";
493
494 if (__die_get_real_type(vr_die, &type) == NULL)
495 return -ENOENT;
496
497 tag = dwarf_tag(&type);
498 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
499 tmp = "*";
500 else if (tag == DW_TAG_subroutine_type) {
501 /* Function pointer */
502 ret = snprintf(buf, len, "(function_type)");
503 return (ret >= len) ? -E2BIG : ret;
504 } else {
505 if (!dwarf_diename(&type))
506 return -ENOENT;
507 if (tag == DW_TAG_union_type)
508 tmp = "union ";
509 else if (tag == DW_TAG_structure_type)
510 tmp = "struct ";
511 /* Write a base name */
512 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
513 return (ret >= len) ? -E2BIG : ret;
514 }
515 ret = die_get_typename(&type, buf, len);
516 if (ret > 0) {
517 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
518 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
519 }
520 return ret;
521}
522
523/* Get the name and type of given variable DIE, stored as "type\tname" */
524static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
525{
526 int ret, ret2;
527
528 ret = die_get_typename(vr_die, buf, len);
529 if (ret < 0) {
530 pr_debug("Failed to get type, make it unknown.\n");
531 ret = snprintf(buf, len, "(unknown_type)");
532 }
533 if (ret > 0) {
534 ret2 = snprintf(buf + ret, len - ret, "\t%s",
535 dwarf_diename(vr_die));
536 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
537 }
538 return ret;
539}
540
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400541/*
542 * Probe finder related functions
543 */
544
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530545static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400546{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530547 struct probe_trace_arg_ref *ref;
548 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400549 if (ref != NULL)
550 ref->offset = offs;
551 return ref;
552}
553
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900554/*
555 * Convert a location into trace_arg.
556 * If tvar == NULL, this just checks variable can be converted.
557 */
558static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
559 Dwarf_Op *fb_ops,
560 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400561{
562 Dwarf_Attribute attr;
563 Dwarf_Op *op;
564 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500565 unsigned int regn;
566 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400567 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400568 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400569 int ret;
570
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900571 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
572 goto static_var;
573
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400574 /* TODO: handle more than 1 exprs */
575 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900576 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400577 nops == 0) {
578 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400579 return -ENOENT;
580 }
581
582 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900583static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900584 if (!tvar)
585 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400586 /* Static variables on memory (not stack), make @varname */
587 ret = strlen(dwarf_diename(vr_die));
588 tvar->value = zalloc(ret + 2);
589 if (tvar->value == NULL)
590 return -ENOMEM;
591 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
592 tvar->ref = alloc_trace_arg_ref((long)offs);
593 if (tvar->ref == NULL)
594 return -ENOMEM;
595 return 0;
596 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400597
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400598 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500599 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900600 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400601 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400602 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500603 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900604 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500605 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400606
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500607 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
608 regn = op->atom - DW_OP_breg0;
609 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400610 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500611 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
612 regn = op->atom - DW_OP_reg0;
613 } else if (op->atom == DW_OP_bregx) {
614 regn = op->number;
615 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400616 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500617 } else if (op->atom == DW_OP_regx) {
618 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400619 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900620 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400621 return -ENOTSUP;
622 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400623
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900624 if (!tvar)
625 return 0;
626
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400627 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400628 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900629 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900630 pr_warning("Mapping for the register number %u "
631 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400632 return -ERANGE;
633 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400634
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400635 tvar->value = strdup(regs);
636 if (tvar->value == NULL)
637 return -ENOMEM;
638
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400639 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400640 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400641 if (tvar->ref == NULL)
642 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400643 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400644 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400645}
646
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400647static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530648 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400649 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400650{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530651 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400652 Dwarf_Die type;
653 char buf[16];
654 int ret;
655
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400656 /* TODO: check all types */
657 if (cast && strcmp(cast, "string") != 0) {
658 /* Non string type is OK */
659 tvar->type = strdup(cast);
660 return (tvar->type == NULL) ? -ENOMEM : 0;
661 }
662
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400663 if (die_get_real_type(vr_die, &type) == NULL) {
664 pr_warning("Failed to get a type information of %s.\n",
665 dwarf_diename(vr_die));
666 return -ENOENT;
667 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400668
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400669 pr_debug("%s type is %s.\n",
670 dwarf_diename(vr_die), dwarf_diename(&type));
671
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400672 if (cast && strcmp(cast, "string") == 0) { /* String type */
673 ret = dwarf_tag(&type);
674 if (ret != DW_TAG_pointer_type &&
675 ret != DW_TAG_array_type) {
676 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900677 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400678 dwarf_diename(vr_die), dwarf_diename(&type));
679 return -EINVAL;
680 }
681 if (ret == DW_TAG_pointer_type) {
682 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900683 pr_warning("Failed to get a type"
684 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400685 return -ENOENT;
686 }
687 while (*ref_ptr)
688 ref_ptr = &(*ref_ptr)->next;
689 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530690 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400691 if (*ref_ptr == NULL) {
692 pr_warning("Out of memory error\n");
693 return -ENOMEM;
694 }
695 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900696 if (!die_compare_name(&type, "char") &&
697 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400698 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900699 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400700 dwarf_diename(vr_die));
701 return -EINVAL;
702 }
703 tvar->type = strdup(cast);
704 return (tvar->type == NULL) ? -ENOMEM : 0;
705 }
706
Masami Hiramatsu49849122010-04-12 13:17:15 -0400707 ret = die_get_byte_size(&type) * 8;
708 if (ret) {
709 /* Check the bitwidth */
710 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400711 pr_info("%s exceeds max-bitwidth."
712 " Cut down to %d bits.\n",
713 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400714 ret = MAX_BASIC_TYPE_BITS;
715 }
716
717 ret = snprintf(buf, 16, "%c%d",
718 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400719 if (ret < 0 || ret >= 16) {
720 if (ret >= 16)
721 ret = -E2BIG;
722 pr_warning("Failed to convert variable type: %s\n",
723 strerror(-ret));
724 return ret;
725 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400726 tvar->type = strdup(buf);
727 if (tvar->type == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400728 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400729 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400730 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400731}
732
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400733static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400734 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530735 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400736 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400737{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530738 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400739 Dwarf_Die type;
740 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400741 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400742
743 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400744 if (die_get_real_type(vr_die, &type) == NULL) {
745 pr_warning("Failed to get the type of %s.\n", varname);
746 return -ENOENT;
747 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400748 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
749 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400750
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400751 if (field->name[0] == '[' &&
752 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
753 if (field->next)
754 /* Save original type for next field */
755 memcpy(die_mem, &type, sizeof(*die_mem));
756 /* Get the type of this array */
757 if (die_get_real_type(&type, &type) == NULL) {
758 pr_warning("Failed to get the type of %s.\n", varname);
759 return -ENOENT;
760 }
761 pr_debug2("Array real type: (%x)\n",
762 (unsigned)dwarf_dieoffset(&type));
763 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530764 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400765 if (ref == NULL)
766 return -ENOMEM;
767 if (*ref_ptr)
768 (*ref_ptr)->next = ref;
769 else
770 *ref_ptr = ref;
771 }
772 ref->offset += die_get_byte_size(&type) * field->index;
773 if (!field->next)
774 /* Save vr_die for converting types */
775 memcpy(die_mem, vr_die, sizeof(*die_mem));
776 goto next;
777 } else if (tag == DW_TAG_pointer_type) {
778 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400779 if (!field->ref) {
780 pr_err("Semantic error: %s must be referred by '->'\n",
781 field->name);
782 return -EINVAL;
783 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400784 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400785 if (die_get_real_type(&type, &type) == NULL) {
786 pr_warning("Failed to get the type of %s.\n", varname);
787 return -ENOENT;
788 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400789 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400790 if (dwarf_tag(&type) != DW_TAG_structure_type) {
791 pr_warning("%s is not a data structure.\n", varname);
792 return -EINVAL;
793 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400794
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530795 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400796 if (ref == NULL)
797 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400798 if (*ref_ptr)
799 (*ref_ptr)->next = ref;
800 else
801 *ref_ptr = ref;
802 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400803 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400804 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400805 pr_warning("%s is not a data structure.\n", varname);
806 return -EINVAL;
807 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400808 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900809 pr_err("Semantic error: %s is not a pointor"
810 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400811 return -EINVAL;
812 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400813 if (field->ref) {
814 pr_err("Semantic error: %s must be referred by '.'\n",
815 field->name);
816 return -EINVAL;
817 }
818 if (!ref) {
819 pr_warning("Structure on a register is not "
820 "supported yet.\n");
821 return -ENOTSUP;
822 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400823 }
824
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400825 if (die_find_member(&type, field->name, die_mem) == NULL) {
826 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
827 dwarf_diename(&type), field->name);
828 return -EINVAL;
829 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400830
831 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300832 ret = die_get_data_member_location(die_mem, &offs);
833 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400834 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300835 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400836 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400837 ref->offset += (long)offs;
838
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400839next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400840 /* Converting next field */
841 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400842 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300843 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400844 else
845 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400846}
847
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400848/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400849static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400850{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400851 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400852 int ret;
853
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400854 pr_debug("Converting variable %s into trace event.\n",
855 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500856
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900857 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
858 pf->tvar);
859 if (ret == -ENOENT)
860 pr_err("Failed to find the location of %s at this address.\n"
861 " Perhaps, it has been optimized out.\n", pf->pvar->var);
862 else if (ret == -ENOTSUP)
863 pr_err("Sorry, we don't support this variable location yet.\n");
864 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400865 ret = convert_variable_fields(vr_die, pf->pvar->var,
866 pf->pvar->field, &pf->tvar->ref,
867 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400868 vr_die = &die_mem;
869 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400870 if (ret == 0)
871 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500872 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400873 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400874}
875
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400876/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400877static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400878{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400879 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400880 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400881 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400882
Masami Hiramatsu367e94c2010-08-27 20:38:59 +0900883 if (!is_c_varname(pf->pvar->var)) {
884 /* Copy raw parameters */
885 pf->tvar->value = strdup(pf->pvar->var);
886 if (pf->tvar->value == NULL)
887 return -ENOMEM;
888 if (pf->pvar->type) {
889 pf->tvar->type = strdup(pf->pvar->type);
890 if (pf->tvar->type == NULL)
891 return -ENOMEM;
892 }
893 if (pf->pvar->name) {
894 pf->tvar->name = strdup(pf->pvar->name);
895 if (pf->tvar->name == NULL)
896 return -ENOMEM;
897 } else
898 pf->tvar->name = NULL;
899 return 0;
900 }
901
Masami Hiramatsu48481932010-04-12 13:16:53 -0400902 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400903 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400904 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400905 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
906 if (ret < 0)
907 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400908 ptr = strchr(buf, ':'); /* Change type separator to _ */
909 if (ptr)
910 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400911 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400912 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400913 if (pf->tvar->name == NULL)
914 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400915
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400916 pr_debug("Searching '%s' variable in context.\n",
917 pf->pvar->var);
918 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900919 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400920 ret = convert_variable(&vr_die, pf);
921 else {
922 /* Search upper class */
923 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900924 while (nscopes-- > 1) {
925 pr_debug("Searching variables in %s\n",
926 dwarf_diename(&scopes[nscopes]));
927 /* We should check this scope, so give dummy address */
928 if (die_find_variable_at(&scopes[nscopes],
929 pf->pvar->var, 0,
930 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400931 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900932 goto found;
933 }
934 }
935 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400936 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900937 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400938 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900939found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400940 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400941 pr_warning("Failed to find '%s' in this function.\n",
942 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400943 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400944}
945
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900946/* Convert subprogram DIE to trace point */
947static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
948 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400949{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500950 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500951 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500952
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400953 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500954 name = dwarf_diename(sp_die);
955 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400956 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900957 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400958 dwarf_diename(sp_die));
959 return -ENOENT;
960 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900961 tp->symbol = strdup(name);
962 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400963 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900964 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400965 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400966 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900967 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400968
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900969 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900970 if (retprobe) {
971 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900972 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900973 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900974 return -EINVAL;
975 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900976 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900977 }
978
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900979 return 0;
980}
981
982/* Call probe_finder callback with real subprogram DIE */
983static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
984{
985 Dwarf_Die die_mem;
986 Dwarf_Attribute fb_attr;
987 size_t nops;
988 int ret;
989
990 /* If no real subprogram, find a real one */
991 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
992 sp_die = die_find_real_subprogram(&pf->cu_die,
993 pf->addr, &die_mem);
994 if (!sp_die) {
995 pr_warning("Failed to find probe point in any "
996 "functions.\n");
997 return -ENOENT;
998 }
999 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001000
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001001 /* Get the frame base attribute/ops */
1002 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -04001003 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001004 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001005 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001006#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001007 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1008 pf->cfi != NULL) {
1009 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001010 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1011 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001012 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001013 (uintmax_t)pf->addr);
1014 return -ENOENT;
1015 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001016#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001017 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001018
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001019 /* Call finder's callback handler */
1020 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001021
1022 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1023 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001024
1025 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001026}
1027
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001028/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001029static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001030{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001031 Dwarf_Lines *lines;
1032 Dwarf_Line *line;
1033 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001034 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001035 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001036 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001037
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001038 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001039 pr_warning("No source lines found.\n");
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001040 return -ENOENT;
1041 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001042
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001043 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001044 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001045 if (dwarf_lineno(line, &lineno) != 0 ||
1046 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001047 continue;
1048
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001049 /* TODO: Get fileno from line, but how? */
1050 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
1051 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001052
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001053 if (dwarf_lineaddr(line, &addr) != 0) {
1054 pr_warning("Failed to get the address of the line.\n");
1055 return -ENOENT;
1056 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001057 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
1058 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001059 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001060
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001061 ret = call_probe_finder(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001062 /* Continuing, because target line might be inlined. */
1063 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001064 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001065}
1066
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001067/* Find lines which match lazy pattern */
1068static int find_lazy_match_lines(struct list_head *head,
1069 const char *fname, const char *pat)
1070{
1071 char *fbuf, *p1, *p2;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001072 int fd, line, nlines = -1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001073 struct stat st;
1074
1075 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001076 if (fd < 0) {
1077 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001078 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001079 }
1080
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001081 if (fstat(fd, &st) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001082 pr_warning("Failed to get the size of %s: %s\n",
1083 fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001084 nlines = -errno;
1085 goto out_close;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001086 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001087
1088 nlines = -ENOMEM;
1089 fbuf = malloc(st.st_size + 2);
1090 if (fbuf == NULL)
1091 goto out_close;
1092 if (read(fd, fbuf, st.st_size) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001093 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001094 nlines = -errno;
1095 goto out_free_fbuf;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001096 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001097 fbuf[st.st_size] = '\n'; /* Dummy line */
1098 fbuf[st.st_size + 1] = '\0';
1099 p1 = fbuf;
1100 line = 1;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001101 nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001102 while ((p2 = strchr(p1, '\n')) != NULL) {
1103 *p2 = '\0';
1104 if (strlazymatch(p1, pat)) {
1105 line_list__add_line(head, line);
1106 nlines++;
1107 }
1108 line++;
1109 p1 = p2 + 1;
1110 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001111out_free_fbuf:
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001112 free(fbuf);
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001113out_close:
1114 close(fd);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001115 return nlines;
1116}
1117
1118/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001119static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001120{
1121 Dwarf_Lines *lines;
1122 Dwarf_Line *line;
1123 size_t nlines, i;
1124 Dwarf_Addr addr;
1125 Dwarf_Die die_mem;
1126 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001127 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001128
1129 if (list_empty(&pf->lcache)) {
1130 /* Matching lazy line pattern */
1131 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001132 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001133 if (ret == 0) {
1134 pr_debug("No matched lines found in %s.\n", pf->fname);
1135 return 0;
1136 } else if (ret < 0)
1137 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001138 }
1139
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001140 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001141 pr_warning("No source lines found.\n");
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001142 return -ENOENT;
1143 }
1144
1145 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001146 line = dwarf_onesrcline(lines, i);
1147
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001148 if (dwarf_lineno(line, &lineno) != 0 ||
1149 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001150 continue;
1151
1152 /* TODO: Get fileno from line, but how? */
1153 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
1154 continue;
1155
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001156 if (dwarf_lineaddr(line, &addr) != 0) {
1157 pr_debug("Failed to get the address of line %d.\n",
1158 lineno);
1159 continue;
1160 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001161 if (sp_die) {
1162 /* Address filtering 1: does sp_die include addr? */
1163 if (!dwarf_haspc(sp_die, addr))
1164 continue;
1165 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001166 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001167 continue;
1168 }
1169
1170 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
1171 (int)i, lineno, (unsigned long long)addr);
1172 pf->addr = addr;
1173
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001174 ret = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001175 /* Continuing, because target line might be inlined. */
1176 }
1177 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001178 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001179}
1180
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001181/* Callback parameter with return value */
1182struct dwarf_callback_param {
1183 void *data;
1184 int retval;
1185};
1186
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001187static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001188{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001189 struct dwarf_callback_param *param = data;
1190 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001191 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001192 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001193
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001194 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001195 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001196 else {
1197 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001198 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001199 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001200 dwarf_diename(in_die));
1201 param->retval = -ENOENT;
1202 return DWARF_CB_ABORT;
1203 }
1204 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001205 pf->addr += pp->offset;
1206 pr_debug("found inline addr: 0x%jx\n",
1207 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001208
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001209 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001210 if (param->retval < 0)
1211 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001212 }
1213
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001214 return DWARF_CB_OK;
1215}
1216
1217/* Search function from function name */
1218static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1219{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001220 struct dwarf_callback_param *param = data;
1221 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001222 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001223
1224 /* Check tag and diename */
1225 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001226 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001227 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001228
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001229 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001230 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001231 dwarf_decl_line(sp_die, &pf->lno);
1232 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001233 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001234 } else if (!dwarf_func_inline(sp_die)) {
1235 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001236 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001237 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001238 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001239 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001240 pr_warning("Failed to get entry address of "
1241 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001242 param->retval = -ENOENT;
1243 return DWARF_CB_ABORT;
1244 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001245 pf->addr += pp->offset;
1246 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001247 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001248 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001249 } else {
1250 struct dwarf_callback_param _param = {.data = (void *)pf,
1251 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001252 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001253 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1254 &_param);
1255 param->retval = _param.retval;
1256 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001257
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001258 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001259}
1260
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001261static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001262{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001263 struct dwarf_callback_param _param = {.data = (void *)pf,
1264 .retval = 0};
1265 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1266 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001267}
1268
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001269/* Find probe points from debuginfo */
1270static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001271{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001272 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001273 Dwarf_Off off, noff;
1274 size_t cuhl;
1275 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001276 Dwarf *dbg = NULL;
1277 Dwfl *dwfl;
1278 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001279 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001280
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001281 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001282 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001283 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001284 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1285 return -EBADF;
1286 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001287
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001288#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001289 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001290 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001291#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001292
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001293 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001294 line_list__init(&pf->lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001295 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001296 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1297 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001298 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001299 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001300 if (!diep)
1301 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001302
1303 /* Check if target file is included. */
1304 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001305 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001306 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001307 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001308
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001309 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001310 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001311 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001312 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001313 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001314 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001315 pf->lno = pp->line;
1316 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001317 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001318 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001319 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001320 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001321 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001322 if (dwfl)
1323 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001324
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001325 return ret;
1326}
1327
1328/* Add a found probe point into trace event list */
1329static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1330{
1331 struct trace_event_finder *tf =
1332 container_of(pf, struct trace_event_finder, pf);
1333 struct probe_trace_event *tev;
1334 int ret, i;
1335
1336 /* Check number of tevs */
1337 if (tf->ntevs == tf->max_tevs) {
1338 pr_warning("Too many( > %d) probe point found.\n",
1339 tf->max_tevs);
1340 return -ERANGE;
1341 }
1342 tev = &tf->tevs[tf->ntevs++];
1343
1344 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1345 &tev->point);
1346 if (ret < 0)
1347 return ret;
1348
1349 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1350 tev->point.offset);
1351
1352 /* Find each argument */
1353 tev->nargs = pf->pev->nargs;
1354 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1355 if (tev->args == NULL)
1356 return -ENOMEM;
1357 for (i = 0; i < pf->pev->nargs; i++) {
1358 pf->pvar = &pf->pev->args[i];
1359 pf->tvar = &tev->args[i];
1360 ret = find_variable(sp_die, pf);
1361 if (ret != 0)
1362 return ret;
1363 }
1364
1365 return 0;
1366}
1367
1368/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1369int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1370 struct probe_trace_event **tevs, int max_tevs)
1371{
1372 struct trace_event_finder tf = {
1373 .pf = {.pev = pev, .callback = add_probe_trace_event},
1374 .max_tevs = max_tevs};
1375 int ret;
1376
1377 /* Allocate result tevs array */
1378 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1379 if (*tevs == NULL)
1380 return -ENOMEM;
1381
1382 tf.tevs = *tevs;
1383 tf.ntevs = 0;
1384
1385 ret = find_probes(fd, &tf.pf);
1386 if (ret < 0) {
1387 free(*tevs);
1388 *tevs = NULL;
1389 return ret;
1390 }
1391
1392 return (ret < 0) ? ret : tf.ntevs;
1393}
1394
1395#define MAX_VAR_LEN 64
1396
1397/* Collect available variables in this scope */
1398static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1399{
1400 struct available_var_finder *af = data;
1401 struct variable_list *vl;
1402 char buf[MAX_VAR_LEN];
1403 int tag, ret;
1404
1405 vl = &af->vls[af->nvls - 1];
1406
1407 tag = dwarf_tag(die_mem);
1408 if (tag == DW_TAG_formal_parameter ||
1409 tag == DW_TAG_variable) {
1410 ret = convert_variable_location(die_mem, af->pf.addr,
1411 af->pf.fb_ops, NULL);
1412 if (ret == 0) {
1413 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001414 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001415 if (ret > 0)
1416 strlist__add(vl->vars, buf);
1417 }
1418 }
1419
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001420 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001421 return DIE_FIND_CB_CONTINUE;
1422 else
1423 return DIE_FIND_CB_SIBLING;
1424}
1425
1426/* Add a found vars into available variables list */
1427static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1428{
1429 struct available_var_finder *af =
1430 container_of(pf, struct available_var_finder, pf);
1431 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001432 Dwarf_Die die_mem, *scopes = NULL;
1433 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001434
1435 /* Check number of tevs */
1436 if (af->nvls == af->max_vls) {
1437 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1438 return -ERANGE;
1439 }
1440 vl = &af->vls[af->nvls++];
1441
1442 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1443 &vl->point);
1444 if (ret < 0)
1445 return ret;
1446
1447 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1448 vl->point.offset);
1449
1450 /* Find local variables */
1451 vl->vars = strlist__new(true, NULL);
1452 if (vl->vars == NULL)
1453 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001454 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001455 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1456
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001457 /* Find external variables */
1458 if (!af->externs)
1459 goto out;
1460 /* Don't need to search child DIE for externs. */
1461 af->child = false;
1462 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1463 while (nscopes-- > 1)
1464 die_find_child(&scopes[nscopes], collect_variables_cb,
1465 (void *)af, &die_mem);
1466 if (scopes)
1467 free(scopes);
1468
1469out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001470 if (strlist__empty(vl->vars)) {
1471 strlist__delete(vl->vars);
1472 vl->vars = NULL;
1473 }
1474
1475 return ret;
1476}
1477
1478/* Find available variables at given probe point */
1479int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001480 struct variable_list **vls, int max_vls,
1481 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001482{
1483 struct available_var_finder af = {
1484 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001485 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001486 int ret;
1487
1488 /* Allocate result vls array */
1489 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1490 if (*vls == NULL)
1491 return -ENOMEM;
1492
1493 af.vls = *vls;
1494 af.nvls = 0;
1495
1496 ret = find_probes(fd, &af.pf);
1497 if (ret < 0) {
1498 /* Free vlist for error */
1499 while (af.nvls--) {
1500 if (af.vls[af.nvls].point.symbol)
1501 free(af.vls[af.nvls].point.symbol);
1502 if (af.vls[af.nvls].vars)
1503 strlist__delete(af.vls[af.nvls].vars);
1504 }
1505 free(af.vls);
1506 *vls = NULL;
1507 return ret;
1508 }
1509
1510 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001511}
1512
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001513/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001514int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001515{
1516 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001517 Dwarf *dbg = NULL;
1518 Dwfl *dwfl = NULL;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001519 Dwarf_Line *line;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001520 Dwarf_Addr laddr, eaddr, bias = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001521 const char *tmp;
1522 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001523 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001524
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001525 /* Open the live linux kernel */
1526 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1527 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001528 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001529 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1530 ret = -EINVAL;
1531 goto end;
1532 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001533
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001534 /* Adjust address with bias */
1535 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001536 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001537 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001538 pr_warning("Failed to find debug information for address %lx\n",
1539 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001540 ret = -EINVAL;
1541 goto end;
1542 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001543
1544 /* Find a corresponding line */
1545 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1546 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001547 if (dwarf_lineaddr(line, &laddr) == 0 &&
1548 (Dwarf_Addr)addr == laddr &&
1549 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001550 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001551 if (tmp) {
1552 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001553 ppt->file = strdup(tmp);
1554 if (ppt->file == NULL) {
1555 ret = -ENOMEM;
1556 goto end;
1557 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001558 found = true;
1559 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001560 }
1561 }
1562
1563 /* Find a corresponding function */
1564 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1565 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001566 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001567 goto end;
1568
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001569 if (ppt->line) {
1570 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1571 &indie)) {
1572 /* addr in an inline function */
1573 tmp = dwarf_diename(&indie);
1574 if (!tmp)
1575 goto end;
1576 ret = dwarf_decl_line(&indie, &lineno);
1577 } else {
1578 if (eaddr == addr) { /* Function entry */
1579 lineno = ppt->line;
1580 ret = 0;
1581 } else
1582 ret = dwarf_decl_line(&spdie, &lineno);
1583 }
1584 if (ret == 0) {
1585 /* Make a relative line number */
1586 ppt->line -= lineno;
1587 goto found;
1588 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001589 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001590 /* We don't have a line number, let's use offset */
1591 ppt->offset = addr - (unsigned long)eaddr;
1592found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001593 ppt->function = strdup(tmp);
1594 if (ppt->function == NULL) {
1595 ret = -ENOMEM;
1596 goto end;
1597 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001598 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001599 }
1600
1601end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001602 if (dwfl)
1603 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001604 if (ret >= 0)
1605 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001606 return ret;
1607}
1608
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001609/* Add a line and store the src path */
1610static int line_range_add_line(const char *src, unsigned int lineno,
1611 struct line_range *lr)
1612{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001613 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001614 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001615 lr->path = strdup(src);
1616 if (lr->path == NULL)
1617 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001618 }
1619 return line_list__add_line(&lr->line_list, lineno);
1620}
1621
1622/* Search function declaration lines */
1623static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1624{
1625 struct dwarf_callback_param *param = data;
1626 struct line_finder *lf = param->data;
1627 const char *src;
1628 int lineno;
1629
1630 src = dwarf_decl_file(sp_die);
1631 if (src && strtailcmp(src, lf->fname) != 0)
1632 return DWARF_CB_OK;
1633
1634 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1635 (lf->lno_s > lineno || lf->lno_e < lineno))
1636 return DWARF_CB_OK;
1637
1638 param->retval = line_range_add_line(src, lineno, lf->lr);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001639 if (param->retval < 0)
1640 return DWARF_CB_ABORT;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001641 return DWARF_CB_OK;
1642}
1643
1644static int find_line_range_func_decl_lines(struct line_finder *lf)
1645{
1646 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1647 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1648 return param.retval;
1649}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001650
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001651/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001652static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001653{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001654 Dwarf_Lines *lines;
1655 Dwarf_Line *line;
1656 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001657 Dwarf_Addr addr;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001658 int lineno, ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001659 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001660 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001661
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001662 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001663 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001664 pr_warning("No source lines found.\n");
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001665 return -ENOENT;
1666 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001667
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001668 /* Search probable lines on lines list */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001669 for (i = 0; i < nlines; i++) {
1670 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001671 if (dwarf_lineno(line, &lineno) != 0 ||
1672 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001673 continue;
1674
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001675 if (sp_die) {
1676 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001677 if (dwarf_lineaddr(line, &addr) != 0 ||
1678 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001679 continue;
1680
1681 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001682 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001683 continue;
1684 }
1685
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001686 /* TODO: Get fileno from line, but how? */
1687 src = dwarf_linesrc(line, NULL, NULL);
1688 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001689 continue;
1690
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001691 ret = line_range_add_line(src, lineno, lf->lr);
1692 if (ret < 0)
1693 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001694 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001695
1696 /*
1697 * Dwarf lines doesn't include function declarations. We have to
1698 * check functions list or given function.
1699 */
1700 if (sp_die) {
1701 src = dwarf_decl_file(sp_die);
1702 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1703 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1704 ret = line_range_add_line(src, lineno, lf->lr);
1705 } else
1706 ret = find_line_range_func_decl_lines(lf);
1707
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001708 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001709 if (ret >= 0)
1710 if (!list_empty(&lf->lr->line_list))
1711 ret = lf->found = 1;
1712 else
1713 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001714 else {
1715 free(lf->lr->path);
1716 lf->lr->path = NULL;
1717 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001718 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001719}
1720
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001721static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1722{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001723 struct dwarf_callback_param *param = data;
1724
1725 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001726 return DWARF_CB_ABORT; /* No need to find other instances */
1727}
1728
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001729/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001730static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001731{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001732 struct dwarf_callback_param *param = data;
1733 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001734 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001735
Arnaldo Carvalho de Melo66a301c32010-10-23 15:12:29 -02001736 pr_debug("find (%llx) %s\n",
1737 (unsigned long long)dwarf_dieoffset(sp_die),
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001738 dwarf_diename(sp_die));
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001739 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001740 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001741 lf->fname = dwarf_decl_file(sp_die);
1742 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001743 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001744 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001745 if (lf->lno_s < 0) /* Overflow */
1746 lf->lno_s = INT_MAX;
1747 lf->lno_e = lr->offset + lr->end;
1748 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001749 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001750 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001751 lr->start = lf->lno_s;
1752 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001753 if (dwarf_func_inline(sp_die)) {
1754 struct dwarf_callback_param _param;
1755 _param.data = (void *)lf;
1756 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001757 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001758 line_range_inline_cb,
1759 &_param);
1760 param->retval = _param.retval;
1761 } else
1762 param->retval = find_line_range_by_line(sp_die, lf);
1763 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001764 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001765 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001766}
1767
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001768static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001769{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001770 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1771 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1772 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001773}
1774
1775int find_line_range(int fd, struct line_range *lr)
1776{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001777 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001778 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001779 Dwarf_Off off = 0, noff;
1780 size_t cuhl;
1781 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001782 Dwarf *dbg = NULL;
1783 Dwfl *dwfl;
1784 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001785 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001786
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001787 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001788 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001789 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001790 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1791 return -EBADF;
1792 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001793
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001794 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001795 while (!lf.found && ret >= 0) {
1796 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001797 break;
1798
1799 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001800 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1801 if (!diep)
1802 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001803
1804 /* Check if target file is included. */
1805 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001806 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001807 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001808 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001809
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001810 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001811 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001812 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001813 else {
1814 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001815 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001816 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001817 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001818 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001819 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001820 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001821
1822 /* Store comp_dir */
1823 if (lf.found) {
1824 comp_dir = cu_get_comp_dir(&lf.cu_die);
1825 if (comp_dir) {
1826 lr->comp_dir = strdup(comp_dir);
1827 if (!lr->comp_dir)
1828 ret = -ENOMEM;
1829 }
1830 }
1831
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001832 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001833 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001834 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001835}
1836