blob: 689ab462a188e2a758ec3b84a72eb8cc9846aed0 [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 Hiramatsu4ea42b12009-10-08 17:17:38 -040046/*
47 * Compare the tail of two strings.
48 * Return 0 if whole of either string is same as another's tail part.
49 */
50static int strtailcmp(const char *s1, const char *s2)
51{
52 int i1 = strlen(s1);
53 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -050054 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040055 if (s1[i1] != s2[i2])
56 return s1[i1] - s2[i2];
57 }
58 return 0;
59}
60
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050061/* Line number list operations */
62
63/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040064static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050065{
66 struct line_node *ln;
67 struct list_head *p;
68
69 /* Reverse search, because new line will be the last one */
70 list_for_each_entry_reverse(ln, head, list) {
71 if (ln->line < line) {
72 p = &ln->list;
73 goto found;
74 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040075 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050076 }
77 /* List is empty, or the smallest entry */
78 p = head;
79found:
80 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040081 ln = zalloc(sizeof(struct line_node));
82 if (ln == NULL)
83 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050084 ln->line = line;
85 INIT_LIST_HEAD(&ln->list);
86 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040087 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050088}
89
90/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040091static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050092{
93 struct line_node *ln;
94
95 /* Reverse search, because new line will be the last one */
96 list_for_each_entry(ln, head, list)
97 if (ln->line == line)
98 return 1;
99
100 return 0;
101}
102
103/* Init line number list */
104static void line_list__init(struct list_head *head)
105{
106 INIT_LIST_HEAD(head);
107}
108
109/* Free line number list */
110static void line_list__free(struct list_head *head)
111{
112 struct line_node *ln;
113 while (!list_empty(head)) {
114 ln = list_first_entry(head, struct line_node, list);
115 list_del(&ln->list);
116 free(ln);
117 }
118}
119
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900120/* Dwarf FL wrappers */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900121static char *debuginfo_path; /* Currently dummy */
122
123static const Dwfl_Callbacks offline_callbacks = {
124 .find_debuginfo = dwfl_standard_find_debuginfo,
125 .debuginfo_path = &debuginfo_path,
126
127 .section_address = dwfl_offline_section_address,
128
129 /* We use this table for core files too. */
130 .find_elf = dwfl_build_id_find_elf,
131};
132
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900133/* Get a Dwarf from offline image */
134static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
135{
136 Dwfl_Module *mod;
137 Dwarf *dbg = NULL;
138
139 if (!dwflp)
140 return NULL;
141
142 *dwflp = dwfl_begin(&offline_callbacks);
143 if (!*dwflp)
144 return NULL;
145
146 mod = dwfl_report_offline(*dwflp, "", "", fd);
147 if (!mod)
148 goto error;
149
150 dbg = dwfl_module_getdwarf(mod, bias);
151 if (!dbg) {
152error:
153 dwfl_end(*dwflp);
154 *dwflp = NULL;
155 }
156 return dbg;
157}
158
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900159#if _ELFUTILS_PREREQ(0, 148)
160/* This method is buggy if elfutils is older than 0.148 */
161static int __linux_kernel_find_elf(Dwfl_Module *mod,
162 void **userdata,
163 const char *module_name,
164 Dwarf_Addr base,
165 char **file_name, Elf **elfp)
166{
167 int fd;
168 const char *path = kernel_get_module_path(module_name);
169
170 pr_debug2("Use file %s for %s\n", path, module_name);
171 if (path) {
172 fd = open(path, O_RDONLY);
173 if (fd >= 0) {
174 *file_name = strdup(path);
175 return fd;
176 }
177 }
178 /* If failed, try to call standard method */
179 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
180 file_name, elfp);
181}
182
183static const Dwfl_Callbacks kernel_callbacks = {
184 .find_debuginfo = dwfl_standard_find_debuginfo,
185 .debuginfo_path = &debuginfo_path,
186
187 .find_elf = __linux_kernel_find_elf,
188 .section_address = dwfl_linux_kernel_module_section_address,
189};
190
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900191/* Get a Dwarf from live kernel image */
192static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
193 Dwarf_Addr *bias)
194{
195 Dwarf *dbg;
196
197 if (!dwflp)
198 return NULL;
199
200 *dwflp = dwfl_begin(&kernel_callbacks);
201 if (!*dwflp)
202 return NULL;
203
204 /* Load the kernel dwarves: Don't care the result here */
205 dwfl_linux_kernel_report_kernel(*dwflp);
206 dwfl_linux_kernel_report_modules(*dwflp);
207
208 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
209 /* Here, check whether we could get a real dwarf */
210 if (!dbg) {
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900211 pr_debug("Failed to find kernel dwarf at %lx\n",
212 (unsigned long)addr);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900213 dwfl_end(*dwflp);
214 *dwflp = NULL;
215 }
216 return dbg;
217}
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900218#else
219/* With older elfutils, this just support kernel module... */
220static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
221 Dwarf_Addr *bias)
222{
223 int fd;
224 const char *path = kernel_get_module_path("kernel");
225
226 if (!path) {
227 pr_err("Failed to find vmlinux path\n");
228 return NULL;
229 }
230
231 pr_debug2("Use file %s for debuginfo\n", path);
232 fd = open(path, O_RDONLY);
233 if (fd < 0)
234 return NULL;
235
236 return dwfl_init_offline_dwarf(fd, dwflp, bias);
237}
238#endif
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900239
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500240/* Dwarf wrappers */
241
242/* Find the realpath of the target file. */
243static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400244{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500245 Dwarf_Files *files;
246 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300247 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400248 int ret;
249
250 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500251 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500252
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500253 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500254 if (ret != 0)
255 return NULL;
256
257 for (i = 0; i < nfiles; i++) {
258 src = dwarf_filesrc(files, i, NULL, NULL);
259 if (strtailcmp(src, fname) == 0)
260 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500261 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400262 if (i == nfiles)
263 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500264 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500265}
266
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900267/* Get DW_AT_comp_dir (should be NULL with older gcc) */
268static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
269{
270 Dwarf_Attribute attr;
271 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
272 return NULL;
273 return dwarf_formstring(&attr);
274}
275
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400276/* Compare diename and tname */
277static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
278{
279 const char *name;
280 name = dwarf_diename(dw_die);
Masami Hiramatsu82175632010-07-09 18:29:17 +0900281 return name ? (strcmp(tname, name) == 0) : false;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400282}
283
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900284/* Get callsite line number of inline-function instance */
285static int die_get_call_lineno(Dwarf_Die *in_die)
286{
287 Dwarf_Attribute attr;
288 Dwarf_Word ret;
289
290 if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
291 return -ENOENT;
292
293 dwarf_formudata(&attr, &ret);
294 return (int)ret;
295}
296
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900297/* Get type die */
298static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
299{
300 Dwarf_Attribute attr;
301
302 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
303 dwarf_formref_die(&attr, die_mem))
304 return die_mem;
305 else
306 return NULL;
307}
308
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900309/* Get a type die, but skip qualifiers */
310static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400311{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400312 int tag;
313
314 do {
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900315 vr_die = die_get_type(vr_die, die_mem);
316 if (!vr_die)
317 break;
318 tag = dwarf_tag(vr_die);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400319 } while (tag == DW_TAG_const_type ||
320 tag == DW_TAG_restrict_type ||
321 tag == DW_TAG_volatile_type ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900322 tag == DW_TAG_shared_type);
323
324 return vr_die;
325}
326
327/* Get a type die, but skip qualifiers and typedef */
328static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
329{
330 do {
331 vr_die = __die_get_real_type(vr_die, die_mem);
332 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400333
Masami Hiramatsu4046b8b2010-10-21 19:13:02 +0900334 return vr_die;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400335}
336
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900337static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
338 Dwarf_Word *result)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400339{
340 Dwarf_Attribute attr;
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900341
342 if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
343 dwarf_formudata(&attr, result) != 0)
344 return -ENOENT;
345
346 return 0;
347}
348
349static bool die_is_signed_type(Dwarf_Die *tp_die)
350{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400351 Dwarf_Word ret;
352
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900353 if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
Masami Hiramatsu49849122010-04-12 13:17:15 -0400354 return false;
355
356 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
357 ret == DW_ATE_signed_fixed);
358}
359
360static int die_get_byte_size(Dwarf_Die *tp_die)
361{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400362 Dwarf_Word ret;
363
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900364 if (die_get_attr_udata(tp_die, DW_AT_byte_size, &ret))
365 return 0;
366
367 return (int)ret;
368}
369
370static int die_get_bit_size(Dwarf_Die *tp_die)
371{
372 Dwarf_Word ret;
373
374 if (die_get_attr_udata(tp_die, DW_AT_bit_size, &ret))
375 return 0;
376
377 return (int)ret;
378}
379
380static int die_get_bit_offset(Dwarf_Die *tp_die)
381{
382 Dwarf_Word ret;
383
384 if (die_get_attr_udata(tp_die, DW_AT_bit_offset, &ret))
Masami Hiramatsu49849122010-04-12 13:17:15 -0400385 return 0;
386
387 return (int)ret;
388}
389
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300390/* Get data_member_location offset */
391static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
392{
393 Dwarf_Attribute attr;
394 Dwarf_Op *expr;
395 size_t nexpr;
396 int ret;
397
398 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
399 return -ENOENT;
400
401 if (dwarf_formudata(&attr, offs) != 0) {
402 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
403 ret = dwarf_getlocation(&attr, &expr, &nexpr);
404 if (ret < 0 || nexpr == 0)
405 return -ENOENT;
406
407 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
408 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
409 expr[0].atom, nexpr);
410 return -ENOTSUP;
411 }
412 *offs = (Dwarf_Word)expr[0].number;
413 }
414 return 0;
415}
416
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400417/* Return values for die_find callbacks */
418enum {
419 DIE_FIND_CB_FOUND = 0, /* End of Search */
420 DIE_FIND_CB_CHILD = 1, /* Search only children */
421 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
422 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
423};
424
425/* Search a child die */
426static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
427 int (*callback)(Dwarf_Die *, void *),
428 void *data, Dwarf_Die *die_mem)
429{
430 Dwarf_Die child_die;
431 int ret;
432
433 ret = dwarf_child(rt_die, die_mem);
434 if (ret != 0)
435 return NULL;
436
437 do {
438 ret = callback(die_mem, data);
439 if (ret == DIE_FIND_CB_FOUND)
440 return die_mem;
441
442 if ((ret & DIE_FIND_CB_CHILD) &&
443 die_find_child(die_mem, callback, data, &child_die)) {
444 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
445 return die_mem;
446 }
447 } while ((ret & DIE_FIND_CB_SIBLING) &&
448 dwarf_siblingof(die_mem, die_mem) == 0);
449
450 return NULL;
451}
452
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500453struct __addr_die_search_param {
454 Dwarf_Addr addr;
455 Dwarf_Die *die_mem;
456};
457
458static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
459{
460 struct __addr_die_search_param *ad = data;
461
462 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
463 dwarf_haspc(fn_die, ad->addr)) {
464 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
465 return DWARF_CB_ABORT;
466 }
467 return DWARF_CB_OK;
468}
469
470/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400471static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
472 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500473{
474 struct __addr_die_search_param ad;
475 ad.addr = addr;
476 ad.die_mem = die_mem;
477 /* dwarf_getscopes can't find subprogram. */
478 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
479 return NULL;
480 else
481 return die_mem;
482}
483
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400484/* die_find callback for inline function search */
485static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
486{
487 Dwarf_Addr *addr = data;
488
489 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
490 dwarf_haspc(die_mem, *addr))
491 return DIE_FIND_CB_FOUND;
492
493 return DIE_FIND_CB_CONTINUE;
494}
495
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500496/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400497static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
498 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500499{
Masami Hiramatsu1d878082011-03-30 18:25:59 +0900500 Dwarf_Die tmp_die;
501
502 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
503 if (!sp_die)
504 return NULL;
505
506 /* Inlined function could be recursive. Trace it until fail */
507 while (sp_die) {
508 memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
509 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
510 &tmp_die);
511 }
512
513 return die_mem;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500514}
515
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900516/* Walker on lines (Note: line number will not be sorted) */
517typedef int (* line_walk_handler_t) (const char *fname, int lineno,
518 Dwarf_Addr addr, void *data);
519
520struct __line_walk_param {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900521 const char *fname;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900522 line_walk_handler_t handler;
523 void *data;
524 int retval;
525};
526
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900527static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
528{
529 struct __line_walk_param *lw = data;
530 Dwarf_Addr addr;
531 int lineno;
532
533 if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
534 lineno = die_get_call_lineno(in_die);
535 if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
536 lw->retval = lw->handler(lw->fname, lineno, addr,
537 lw->data);
538 if (lw->retval != 0)
539 return DIE_FIND_CB_FOUND;
540 }
541 }
542 return DIE_FIND_CB_SIBLING;
543}
544
545/* Walk on lines of blocks included in given DIE */
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900546static int __die_walk_funclines(Dwarf_Die *sp_die,
547 line_walk_handler_t handler, void *data)
548{
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900549 struct __line_walk_param lw = {
550 .handler = handler,
551 .data = data,
552 .retval = 0,
553 };
554 Dwarf_Die die_mem;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900555 Dwarf_Addr addr;
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900556 int lineno;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900557
558 /* Handle function declaration line */
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900559 lw.fname = dwarf_decl_file(sp_die);
560 if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900561 dwarf_entrypc(sp_die, &addr) == 0) {
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900562 lw.retval = handler(lw.fname, lineno, addr, data);
563 if (lw.retval != 0)
564 goto done;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900565 }
Masami Hiramatsu5069ed82011-01-13 21:46:05 +0900566 die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
567done:
568 return lw.retval;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900569}
570
571static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
572{
573 struct __line_walk_param *lw = data;
574
575 lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
576 if (lw->retval != 0)
577 return DWARF_CB_ABORT;
578
579 return DWARF_CB_OK;
580}
581
582/*
583 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
584 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
585 */
586static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
587 void *data)
588{
589 Dwarf_Lines *lines;
590 Dwarf_Line *line;
591 Dwarf_Addr addr;
592 const char *fname;
593 int lineno, ret = 0;
594 Dwarf_Die die_mem, *cu_die;
595 size_t nlines, i;
596
597 /* Get the CU die */
598 if (dwarf_tag(pdie) == DW_TAG_subprogram)
599 cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
600 else
601 cu_die = pdie;
602 if (!cu_die) {
603 pr_debug2("Failed to get CU from subprogram\n");
604 return -EINVAL;
605 }
606
607 /* Get lines list in the CU */
608 if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
609 pr_debug2("Failed to get source lines on this CU.\n");
610 return -ENOENT;
611 }
612 pr_debug2("Get %zd lines from this CU\n", nlines);
613
614 /* Walk on the lines on lines list */
615 for (i = 0; i < nlines; i++) {
616 line = dwarf_onesrcline(lines, i);
617 if (line == NULL ||
618 dwarf_lineno(line, &lineno) != 0 ||
619 dwarf_lineaddr(line, &addr) != 0) {
620 pr_debug2("Failed to get line info. "
621 "Possible error in debuginfo.\n");
622 continue;
623 }
624 /* Filter lines based on address */
625 if (pdie != cu_die)
626 /*
627 * Address filtering
628 * The line is included in given function, and
629 * no inline block includes it.
630 */
631 if (!dwarf_haspc(pdie, addr) ||
632 die_find_inlinefunc(pdie, addr, &die_mem))
633 continue;
634 /* Get source line */
635 fname = dwarf_linesrc(line, NULL, NULL);
636
637 ret = handler(fname, lineno, addr, data);
638 if (ret != 0)
639 return ret;
640 }
641
642 /*
643 * Dwarf lines doesn't include function declarations and inlined
644 * subroutines. We have to check functions list or given function.
645 */
646 if (pdie != cu_die)
647 ret = __die_walk_funclines(pdie, handler, data);
648 else {
649 struct __line_walk_param param = {
650 .handler = handler,
651 .data = data,
652 .retval = 0,
653 };
654 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
655 ret = param.retval;
656 }
657
658 return ret;
659}
660
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900661struct __find_variable_param {
662 const char *name;
663 Dwarf_Addr addr;
664};
665
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400666static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400667{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900668 struct __find_variable_param *fvp = data;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400669 int tag;
670
671 tag = dwarf_tag(die_mem);
672 if ((tag == DW_TAG_formal_parameter ||
673 tag == DW_TAG_variable) &&
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900674 die_compare_name(die_mem, fvp->name))
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400675 return DIE_FIND_CB_FOUND;
676
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900677 if (dwarf_haspc(die_mem, fvp->addr))
678 return DIE_FIND_CB_CONTINUE;
679 else
680 return DIE_FIND_CB_SIBLING;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400681}
682
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900683/* Find a variable called 'name' at given address */
684static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
685 Dwarf_Addr addr, Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500686{
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900687 struct __find_variable_param fvp = { .name = name, .addr = addr};
688
689 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400690 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400691}
692
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400693static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
694{
695 const char *name = data;
696
697 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900698 die_compare_name(die_mem, name))
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400699 return DIE_FIND_CB_FOUND;
700
701 return DIE_FIND_CB_SIBLING;
702}
703
704/* Find a member called 'name' */
705static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
706 Dwarf_Die *die_mem)
707{
708 return die_find_child(st_die, __die_find_member_cb, (void *)name,
709 die_mem);
710}
711
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900712/* Get the name of given variable DIE */
713static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
714{
715 Dwarf_Die type;
716 int tag, ret, ret2;
717 const char *tmp = "";
718
719 if (__die_get_real_type(vr_die, &type) == NULL)
720 return -ENOENT;
721
722 tag = dwarf_tag(&type);
723 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
724 tmp = "*";
725 else if (tag == DW_TAG_subroutine_type) {
726 /* Function pointer */
727 ret = snprintf(buf, len, "(function_type)");
728 return (ret >= len) ? -E2BIG : ret;
729 } else {
730 if (!dwarf_diename(&type))
731 return -ENOENT;
732 if (tag == DW_TAG_union_type)
733 tmp = "union ";
734 else if (tag == DW_TAG_structure_type)
735 tmp = "struct ";
736 /* Write a base name */
737 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
738 return (ret >= len) ? -E2BIG : ret;
739 }
740 ret = die_get_typename(&type, buf, len);
741 if (ret > 0) {
742 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
743 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
744 }
745 return ret;
746}
747
748/* Get the name and type of given variable DIE, stored as "type\tname" */
749static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
750{
751 int ret, ret2;
752
753 ret = die_get_typename(vr_die, buf, len);
754 if (ret < 0) {
755 pr_debug("Failed to get type, make it unknown.\n");
756 ret = snprintf(buf, len, "(unknown_type)");
757 }
758 if (ret > 0) {
759 ret2 = snprintf(buf + ret, len - ret, "\t%s",
760 dwarf_diename(vr_die));
761 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
762 }
763 return ret;
764}
765
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400766/*
767 * Probe finder related functions
768 */
769
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530770static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400771{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530772 struct probe_trace_arg_ref *ref;
773 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400774 if (ref != NULL)
775 ref->offset = offs;
776 return ref;
777}
778
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900779/*
780 * Convert a location into trace_arg.
781 * If tvar == NULL, this just checks variable can be converted.
782 */
783static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
784 Dwarf_Op *fb_ops,
785 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400786{
787 Dwarf_Attribute attr;
788 Dwarf_Op *op;
789 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500790 unsigned int regn;
791 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400792 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400793 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400794 int ret;
795
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900796 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
797 goto static_var;
798
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400799 /* TODO: handle more than 1 exprs */
800 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900801 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400802 nops == 0) {
803 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400804 return -ENOENT;
805 }
806
807 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900808static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900809 if (!tvar)
810 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400811 /* Static variables on memory (not stack), make @varname */
812 ret = strlen(dwarf_diename(vr_die));
813 tvar->value = zalloc(ret + 2);
814 if (tvar->value == NULL)
815 return -ENOMEM;
816 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
817 tvar->ref = alloc_trace_arg_ref((long)offs);
818 if (tvar->ref == NULL)
819 return -ENOMEM;
820 return 0;
821 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400822
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400823 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500824 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900825 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400826 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400827 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500828 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900829 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500830 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400831
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500832 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
833 regn = op->atom - DW_OP_breg0;
834 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400835 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500836 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
837 regn = op->atom - DW_OP_reg0;
838 } else if (op->atom == DW_OP_bregx) {
839 regn = op->number;
840 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400841 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500842 } else if (op->atom == DW_OP_regx) {
843 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400844 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900845 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400846 return -ENOTSUP;
847 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400848
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900849 if (!tvar)
850 return 0;
851
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400852 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400853 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900854 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900855 pr_warning("Mapping for the register number %u "
856 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400857 return -ERANGE;
858 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400859
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400860 tvar->value = strdup(regs);
861 if (tvar->value == NULL)
862 return -ENOMEM;
863
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400864 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400865 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400866 if (tvar->ref == NULL)
867 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400868 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400869 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400870}
871
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900872#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
873
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400874static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530875 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400876 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400877{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530878 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400879 Dwarf_Die type;
880 char buf[16];
881 int ret;
882
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400883 /* TODO: check all types */
884 if (cast && strcmp(cast, "string") != 0) {
885 /* Non string type is OK */
886 tvar->type = strdup(cast);
887 return (tvar->type == NULL) ? -ENOMEM : 0;
888 }
889
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900890 if (die_get_bit_size(vr_die) != 0) {
891 /* This is a bitfield */
892 ret = snprintf(buf, 16, "b%d@%d/%zd", die_get_bit_size(vr_die),
893 die_get_bit_offset(vr_die),
894 BYTES_TO_BITS(die_get_byte_size(vr_die)));
895 goto formatted;
896 }
897
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400898 if (die_get_real_type(vr_die, &type) == NULL) {
899 pr_warning("Failed to get a type information of %s.\n",
900 dwarf_diename(vr_die));
901 return -ENOENT;
902 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400903
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400904 pr_debug("%s type is %s.\n",
905 dwarf_diename(vr_die), dwarf_diename(&type));
906
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400907 if (cast && strcmp(cast, "string") == 0) { /* String type */
908 ret = dwarf_tag(&type);
909 if (ret != DW_TAG_pointer_type &&
910 ret != DW_TAG_array_type) {
911 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900912 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400913 dwarf_diename(vr_die), dwarf_diename(&type));
914 return -EINVAL;
915 }
916 if (ret == DW_TAG_pointer_type) {
917 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900918 pr_warning("Failed to get a type"
919 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400920 return -ENOENT;
921 }
922 while (*ref_ptr)
923 ref_ptr = &(*ref_ptr)->next;
924 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530925 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400926 if (*ref_ptr == NULL) {
927 pr_warning("Out of memory error\n");
928 return -ENOMEM;
929 }
930 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900931 if (!die_compare_name(&type, "char") &&
932 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400933 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900934 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400935 dwarf_diename(vr_die));
936 return -EINVAL;
937 }
938 tvar->type = strdup(cast);
939 return (tvar->type == NULL) ? -ENOMEM : 0;
940 }
941
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900942 ret = BYTES_TO_BITS(die_get_byte_size(&type));
943 if (!ret)
944 /* No size ... try to use default type */
945 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400946
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900947 /* Check the bitwidth */
948 if (ret > MAX_BASIC_TYPE_BITS) {
949 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
950 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
951 ret = MAX_BASIC_TYPE_BITS;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400952 }
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900953 ret = snprintf(buf, 16, "%c%d",
954 die_is_signed_type(&type) ? 's' : 'u', ret);
955
956formatted:
957 if (ret < 0 || ret >= 16) {
958 if (ret >= 16)
959 ret = -E2BIG;
960 pr_warning("Failed to convert variable type: %s\n",
961 strerror(-ret));
962 return ret;
963 }
964 tvar->type = strdup(buf);
965 if (tvar->type == NULL)
966 return -ENOMEM;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400967 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400968}
969
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400970static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400971 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530972 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400973 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400974{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530975 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400976 Dwarf_Die type;
977 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400978 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400979
980 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400981 if (die_get_real_type(vr_die, &type) == NULL) {
982 pr_warning("Failed to get the type of %s.\n", varname);
983 return -ENOENT;
984 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400985 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
986 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400987
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400988 if (field->name[0] == '[' &&
989 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
990 if (field->next)
991 /* Save original type for next field */
992 memcpy(die_mem, &type, sizeof(*die_mem));
993 /* Get the type of this array */
994 if (die_get_real_type(&type, &type) == NULL) {
995 pr_warning("Failed to get the type of %s.\n", varname);
996 return -ENOENT;
997 }
998 pr_debug2("Array real type: (%x)\n",
999 (unsigned)dwarf_dieoffset(&type));
1000 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301001 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001002 if (ref == NULL)
1003 return -ENOMEM;
1004 if (*ref_ptr)
1005 (*ref_ptr)->next = ref;
1006 else
1007 *ref_ptr = ref;
1008 }
1009 ref->offset += die_get_byte_size(&type) * field->index;
1010 if (!field->next)
1011 /* Save vr_die for converting types */
1012 memcpy(die_mem, vr_die, sizeof(*die_mem));
1013 goto next;
1014 } else if (tag == DW_TAG_pointer_type) {
1015 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001016 if (!field->ref) {
1017 pr_err("Semantic error: %s must be referred by '->'\n",
1018 field->name);
1019 return -EINVAL;
1020 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001021 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001022 if (die_get_real_type(&type, &type) == NULL) {
1023 pr_warning("Failed to get the type of %s.\n", varname);
1024 return -ENOENT;
1025 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001026 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001027 if (dwarf_tag(&type) != DW_TAG_structure_type) {
1028 pr_warning("%s is not a data structure.\n", varname);
1029 return -EINVAL;
1030 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001031
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301032 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001033 if (ref == NULL)
1034 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001035 if (*ref_ptr)
1036 (*ref_ptr)->next = ref;
1037 else
1038 *ref_ptr = ref;
1039 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -04001040 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001041 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001042 pr_warning("%s is not a data structure.\n", varname);
1043 return -EINVAL;
1044 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001045 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001046 pr_err("Semantic error: %s is not a pointor"
1047 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001048 return -EINVAL;
1049 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001050 if (field->ref) {
1051 pr_err("Semantic error: %s must be referred by '.'\n",
1052 field->name);
1053 return -EINVAL;
1054 }
1055 if (!ref) {
1056 pr_warning("Structure on a register is not "
1057 "supported yet.\n");
1058 return -ENOTSUP;
1059 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001060 }
1061
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001062 if (die_find_member(&type, field->name, die_mem) == NULL) {
1063 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
1064 dwarf_diename(&type), field->name);
1065 return -EINVAL;
1066 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001067
1068 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001069 ret = die_get_data_member_location(die_mem, &offs);
1070 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001071 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001072 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001073 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001074 ref->offset += (long)offs;
1075
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001076next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001077 /* Converting next field */
1078 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001079 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -03001080 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001081 else
1082 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001083}
1084
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001085/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001086static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001087{
Masami Hiramatsu49849122010-04-12 13:17:15 -04001088 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001089 int ret;
1090
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001091 pr_debug("Converting variable %s into trace event.\n",
1092 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001093
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001094 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
1095 pf->tvar);
1096 if (ret == -ENOENT)
1097 pr_err("Failed to find the location of %s at this address.\n"
1098 " Perhaps, it has been optimized out.\n", pf->pvar->var);
1099 else if (ret == -ENOTSUP)
1100 pr_err("Sorry, we don't support this variable location yet.\n");
1101 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001102 ret = convert_variable_fields(vr_die, pf->pvar->var,
1103 pf->pvar->field, &pf->tvar->ref,
1104 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001105 vr_die = &die_mem;
1106 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -04001107 if (ret == 0)
1108 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001109 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001110 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001111}
1112
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001113/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001114static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001115{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001116 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001117 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001118 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001119
Masami Hiramatsu367e94c2010-08-27 20:38:59 +09001120 if (!is_c_varname(pf->pvar->var)) {
1121 /* Copy raw parameters */
1122 pf->tvar->value = strdup(pf->pvar->var);
1123 if (pf->tvar->value == NULL)
1124 return -ENOMEM;
1125 if (pf->pvar->type) {
1126 pf->tvar->type = strdup(pf->pvar->type);
1127 if (pf->tvar->type == NULL)
1128 return -ENOMEM;
1129 }
1130 if (pf->pvar->name) {
1131 pf->tvar->name = strdup(pf->pvar->name);
1132 if (pf->tvar->name == NULL)
1133 return -ENOMEM;
1134 } else
1135 pf->tvar->name = NULL;
1136 return 0;
1137 }
1138
Masami Hiramatsu48481932010-04-12 13:16:53 -04001139 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001140 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001141 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001142 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
1143 if (ret < 0)
1144 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001145 ptr = strchr(buf, ':'); /* Change type separator to _ */
1146 if (ptr)
1147 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001148 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001149 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001150 if (pf->tvar->name == NULL)
1151 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001152
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001153 pr_debug("Searching '%s' variable in context.\n",
1154 pf->pvar->var);
1155 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +09001156 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001157 ret = convert_variable(&vr_die, pf);
1158 else {
1159 /* Search upper class */
1160 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001161 while (nscopes-- > 1) {
1162 pr_debug("Searching variables in %s\n",
1163 dwarf_diename(&scopes[nscopes]));
1164 /* We should check this scope, so give dummy address */
1165 if (die_find_variable_at(&scopes[nscopes],
1166 pf->pvar->var, 0,
1167 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001168 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001169 goto found;
1170 }
1171 }
1172 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001173 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001174 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001175 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +09001176found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001177 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001178 pr_warning("Failed to find '%s' in this function.\n",
1179 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001180 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001181}
1182
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001183/* Convert subprogram DIE to trace point */
1184static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
1185 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001186{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001187 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001188 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001189
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001190 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001191 name = dwarf_diename(sp_die);
1192 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001193 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001194 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001195 dwarf_diename(sp_die));
1196 return -ENOENT;
1197 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001198 tp->symbol = strdup(name);
1199 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001200 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001201 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001202 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001203 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001204 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001205
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001206 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001207 if (retprobe) {
1208 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001209 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001210 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001211 return -EINVAL;
1212 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001213 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001214 }
1215
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001216 return 0;
1217}
1218
1219/* Call probe_finder callback with real subprogram DIE */
1220static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
1221{
1222 Dwarf_Die die_mem;
1223 Dwarf_Attribute fb_attr;
1224 size_t nops;
1225 int ret;
1226
1227 /* If no real subprogram, find a real one */
1228 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
1229 sp_die = die_find_real_subprogram(&pf->cu_die,
1230 pf->addr, &die_mem);
1231 if (!sp_die) {
1232 pr_warning("Failed to find probe point in any "
1233 "functions.\n");
1234 return -ENOENT;
1235 }
1236 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001237
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001238 /* Get the frame base attribute/ops */
1239 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -04001240 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001241 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001242 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001243#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001244 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1245 pf->cfi != NULL) {
1246 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001247 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1248 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001249 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001250 (uintmax_t)pf->addr);
1251 return -ENOENT;
1252 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001253#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001254 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001255
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001256 /* Call finder's callback handler */
1257 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001258
1259 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1260 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001261
1262 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001263}
1264
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001265static int probe_point_line_walker(const char *fname, int lineno,
1266 Dwarf_Addr addr, void *data)
1267{
1268 struct probe_finder *pf = data;
1269 int ret;
1270
1271 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
1272 return 0;
1273
1274 pf->addr = addr;
1275 ret = call_probe_finder(NULL, pf);
1276
1277 /* Continue if no error, because the line will be in inline function */
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001278 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001279}
1280
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001281/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001282static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001283{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001284 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001285}
1286
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001287/* Find lines which match lazy pattern */
1288static int find_lazy_match_lines(struct list_head *head,
1289 const char *fname, const char *pat)
1290{
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001291 FILE *fp;
1292 char *line = NULL;
1293 size_t line_len;
1294 ssize_t len;
1295 int count = 0, linenum = 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001296
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001297 fp = fopen(fname, "r");
1298 if (!fp) {
1299 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001300 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001301 }
1302
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001303 while ((len = getline(&line, &line_len, fp)) > 0) {
1304
1305 if (line[len - 1] == '\n')
1306 line[len - 1] = '\0';
1307
1308 if (strlazymatch(line, pat)) {
1309 line_list__add_line(head, linenum);
1310 count++;
1311 }
1312 linenum++;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001313 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001314
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001315 if (ferror(fp))
1316 count = -errno;
1317 free(line);
1318 fclose(fp);
1319
1320 if (count == 0)
1321 pr_debug("No matched lines found in %s.\n", fname);
1322 return count;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001323}
1324
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001325static int probe_point_lazy_walker(const char *fname, int lineno,
1326 Dwarf_Addr addr, void *data)
1327{
1328 struct probe_finder *pf = data;
1329 int ret;
1330
1331 if (!line_list__has_line(&pf->lcache, lineno) ||
1332 strtailcmp(fname, pf->fname) != 0)
1333 return 0;
1334
1335 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1336 lineno, (unsigned long long)addr);
1337 pf->addr = addr;
1338 ret = call_probe_finder(NULL, pf);
1339
1340 /*
1341 * Continue if no error, because the lazy pattern will match
1342 * to other lines
1343 */
Ingo Molnar5e814dd2011-03-15 20:51:09 +01001344 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001345}
1346
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001347/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001348static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001349{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001350 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001351
1352 if (list_empty(&pf->lcache)) {
1353 /* Matching lazy line pattern */
1354 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001355 pf->pev->point.lazy_line);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +01001356 if (ret <= 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001357 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001358 }
1359
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001360 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001361}
1362
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001363/* Callback parameter with return value */
1364struct dwarf_callback_param {
1365 void *data;
1366 int retval;
1367};
1368
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001369static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001370{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001371 struct dwarf_callback_param *param = data;
1372 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001373 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001374 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001375
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001376 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001377 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001378 else {
1379 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001380 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001381 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001382 dwarf_diename(in_die));
1383 param->retval = -ENOENT;
1384 return DWARF_CB_ABORT;
1385 }
1386 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001387 pf->addr += pp->offset;
1388 pr_debug("found inline addr: 0x%jx\n",
1389 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001390
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001391 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001392 if (param->retval < 0)
1393 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001394 }
1395
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001396 return DWARF_CB_OK;
1397}
1398
1399/* Search function from function name */
1400static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1401{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001402 struct dwarf_callback_param *param = data;
1403 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001404 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001405
1406 /* Check tag and diename */
1407 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001408 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001409 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001410
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001411 /* Check declared file */
1412 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1413 return DWARF_CB_OK;
1414
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001415 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001416 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001417 dwarf_decl_line(sp_die, &pf->lno);
1418 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001419 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001420 } else if (!dwarf_func_inline(sp_die)) {
1421 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001422 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001423 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001424 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001425 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001426 pr_warning("Failed to get entry address of "
1427 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001428 param->retval = -ENOENT;
1429 return DWARF_CB_ABORT;
1430 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001431 pf->addr += pp->offset;
1432 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001433 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001434 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001435 } else {
1436 struct dwarf_callback_param _param = {.data = (void *)pf,
1437 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001438 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001439 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1440 &_param);
1441 param->retval = _param.retval;
1442 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001443
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001444 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001445}
1446
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001447static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001448{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001449 struct dwarf_callback_param _param = {.data = (void *)pf,
1450 .retval = 0};
1451 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1452 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001453}
1454
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001455/* Find probe points from debuginfo */
1456static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001457{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001458 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001459 Dwarf_Off off, noff;
1460 size_t cuhl;
1461 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001462 Dwarf *dbg = NULL;
1463 Dwfl *dwfl;
1464 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001465 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001466
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001467 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001468 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001469 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001470 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001471 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001472 return -EBADF;
1473 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001474
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001475#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001476 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001477 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001478#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001479
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001480 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001481 line_list__init(&pf->lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001482 /* Loop on CUs (Compilation Unit) */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001483 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001484 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001485 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001486 if (!diep)
1487 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001488
1489 /* Check if target file is included. */
1490 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001491 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001492 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001493 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001494
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001495 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001496 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001497 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001498 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001499 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001500 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001501 pf->lno = pp->line;
1502 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001503 }
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001504 if (ret < 0)
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001505 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001506 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001507 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001508 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001509 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001510 if (dwfl)
1511 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001512
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001513 return ret;
1514}
1515
1516/* Add a found probe point into trace event list */
1517static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1518{
1519 struct trace_event_finder *tf =
1520 container_of(pf, struct trace_event_finder, pf);
1521 struct probe_trace_event *tev;
1522 int ret, i;
1523
1524 /* Check number of tevs */
1525 if (tf->ntevs == tf->max_tevs) {
1526 pr_warning("Too many( > %d) probe point found.\n",
1527 tf->max_tevs);
1528 return -ERANGE;
1529 }
1530 tev = &tf->tevs[tf->ntevs++];
1531
1532 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1533 &tev->point);
1534 if (ret < 0)
1535 return ret;
1536
1537 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1538 tev->point.offset);
1539
1540 /* Find each argument */
1541 tev->nargs = pf->pev->nargs;
1542 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1543 if (tev->args == NULL)
1544 return -ENOMEM;
1545 for (i = 0; i < pf->pev->nargs; i++) {
1546 pf->pvar = &pf->pev->args[i];
1547 pf->tvar = &tev->args[i];
1548 ret = find_variable(sp_die, pf);
1549 if (ret != 0)
1550 return ret;
1551 }
1552
1553 return 0;
1554}
1555
1556/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1557int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1558 struct probe_trace_event **tevs, int max_tevs)
1559{
1560 struct trace_event_finder tf = {
1561 .pf = {.pev = pev, .callback = add_probe_trace_event},
1562 .max_tevs = max_tevs};
1563 int ret;
1564
1565 /* Allocate result tevs array */
1566 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1567 if (*tevs == NULL)
1568 return -ENOMEM;
1569
1570 tf.tevs = *tevs;
1571 tf.ntevs = 0;
1572
1573 ret = find_probes(fd, &tf.pf);
1574 if (ret < 0) {
1575 free(*tevs);
1576 *tevs = NULL;
1577 return ret;
1578 }
1579
1580 return (ret < 0) ? ret : tf.ntevs;
1581}
1582
1583#define MAX_VAR_LEN 64
1584
1585/* Collect available variables in this scope */
1586static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1587{
1588 struct available_var_finder *af = data;
1589 struct variable_list *vl;
1590 char buf[MAX_VAR_LEN];
1591 int tag, ret;
1592
1593 vl = &af->vls[af->nvls - 1];
1594
1595 tag = dwarf_tag(die_mem);
1596 if (tag == DW_TAG_formal_parameter ||
1597 tag == DW_TAG_variable) {
1598 ret = convert_variable_location(die_mem, af->pf.addr,
1599 af->pf.fb_ops, NULL);
1600 if (ret == 0) {
1601 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001602 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001603 if (ret > 0)
1604 strlist__add(vl->vars, buf);
1605 }
1606 }
1607
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001608 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001609 return DIE_FIND_CB_CONTINUE;
1610 else
1611 return DIE_FIND_CB_SIBLING;
1612}
1613
1614/* Add a found vars into available variables list */
1615static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1616{
1617 struct available_var_finder *af =
1618 container_of(pf, struct available_var_finder, pf);
1619 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001620 Dwarf_Die die_mem, *scopes = NULL;
1621 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001622
1623 /* Check number of tevs */
1624 if (af->nvls == af->max_vls) {
1625 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1626 return -ERANGE;
1627 }
1628 vl = &af->vls[af->nvls++];
1629
1630 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1631 &vl->point);
1632 if (ret < 0)
1633 return ret;
1634
1635 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1636 vl->point.offset);
1637
1638 /* Find local variables */
1639 vl->vars = strlist__new(true, NULL);
1640 if (vl->vars == NULL)
1641 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001642 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001643 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1644
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001645 /* Find external variables */
1646 if (!af->externs)
1647 goto out;
1648 /* Don't need to search child DIE for externs. */
1649 af->child = false;
1650 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1651 while (nscopes-- > 1)
1652 die_find_child(&scopes[nscopes], collect_variables_cb,
1653 (void *)af, &die_mem);
1654 if (scopes)
1655 free(scopes);
1656
1657out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001658 if (strlist__empty(vl->vars)) {
1659 strlist__delete(vl->vars);
1660 vl->vars = NULL;
1661 }
1662
1663 return ret;
1664}
1665
1666/* Find available variables at given probe point */
1667int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001668 struct variable_list **vls, int max_vls,
1669 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001670{
1671 struct available_var_finder af = {
1672 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001673 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001674 int ret;
1675
1676 /* Allocate result vls array */
1677 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1678 if (*vls == NULL)
1679 return -ENOMEM;
1680
1681 af.vls = *vls;
1682 af.nvls = 0;
1683
1684 ret = find_probes(fd, &af.pf);
1685 if (ret < 0) {
1686 /* Free vlist for error */
1687 while (af.nvls--) {
1688 if (af.vls[af.nvls].point.symbol)
1689 free(af.vls[af.nvls].point.symbol);
1690 if (af.vls[af.nvls].vars)
1691 strlist__delete(af.vls[af.nvls].vars);
1692 }
1693 free(af.vls);
1694 *vls = NULL;
1695 return ret;
1696 }
1697
1698 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001699}
1700
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001701/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001702int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001703{
1704 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001705 Dwarf *dbg = NULL;
1706 Dwfl *dwfl = NULL;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001707 Dwarf_Line *line;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001708 Dwarf_Addr laddr, eaddr, bias = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001709 const char *tmp;
1710 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001711 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001712
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001713 /* Open the live linux kernel */
1714 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1715 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001716 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001717 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1718 ret = -EINVAL;
1719 goto end;
1720 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001721
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001722 /* Adjust address with bias */
1723 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001724 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001725 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001726 pr_warning("Failed to find debug information for address %lx\n",
1727 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001728 ret = -EINVAL;
1729 goto end;
1730 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001731
1732 /* Find a corresponding line */
1733 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1734 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001735 if (dwarf_lineaddr(line, &laddr) == 0 &&
1736 (Dwarf_Addr)addr == laddr &&
1737 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001738 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001739 if (tmp) {
1740 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001741 ppt->file = strdup(tmp);
1742 if (ppt->file == NULL) {
1743 ret = -ENOMEM;
1744 goto end;
1745 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001746 found = true;
1747 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001748 }
1749 }
1750
1751 /* Find a corresponding function */
1752 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1753 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001754 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001755 goto end;
1756
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001757 if (ppt->line) {
1758 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1759 &indie)) {
1760 /* addr in an inline function */
1761 tmp = dwarf_diename(&indie);
1762 if (!tmp)
1763 goto end;
1764 ret = dwarf_decl_line(&indie, &lineno);
1765 } else {
1766 if (eaddr == addr) { /* Function entry */
1767 lineno = ppt->line;
1768 ret = 0;
1769 } else
1770 ret = dwarf_decl_line(&spdie, &lineno);
1771 }
1772 if (ret == 0) {
1773 /* Make a relative line number */
1774 ppt->line -= lineno;
1775 goto found;
1776 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001777 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001778 /* We don't have a line number, let's use offset */
1779 ppt->offset = addr - (unsigned long)eaddr;
1780found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001781 ppt->function = strdup(tmp);
1782 if (ppt->function == NULL) {
1783 ret = -ENOMEM;
1784 goto end;
1785 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001786 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001787 }
1788
1789end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001790 if (dwfl)
1791 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001792 if (ret >= 0)
1793 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001794 return ret;
1795}
1796
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001797/* Add a line and store the src path */
1798static int line_range_add_line(const char *src, unsigned int lineno,
1799 struct line_range *lr)
1800{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001801 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001802 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001803 lr->path = strdup(src);
1804 if (lr->path == NULL)
1805 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001806 }
1807 return line_list__add_line(&lr->line_list, lineno);
1808}
1809
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001810static int line_range_walk_cb(const char *fname, int lineno,
1811 Dwarf_Addr addr __used,
1812 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001813{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001814 struct line_finder *lf = data;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001815
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001816 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001817 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001818 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001819
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001820 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1821 return -EINVAL;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001822
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001823 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001824}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001825
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001826/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001827static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001828{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001829 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001830
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001831 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001832
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001833 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001834 if (ret >= 0)
1835 if (!list_empty(&lf->lr->line_list))
1836 ret = lf->found = 1;
1837 else
1838 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001839 else {
1840 free(lf->lr->path);
1841 lf->lr->path = NULL;
1842 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001843 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001844}
1845
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001846static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1847{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001848 struct dwarf_callback_param *param = data;
1849
1850 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001851 return DWARF_CB_ABORT; /* No need to find other instances */
1852}
1853
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001854/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001855static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001856{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001857 struct dwarf_callback_param *param = data;
1858 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001859 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001860
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001861 /* Check declared file */
1862 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1863 return DWARF_CB_OK;
1864
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001865 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001866 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001867 lf->fname = dwarf_decl_file(sp_die);
1868 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001869 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001870 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001871 if (lf->lno_s < 0) /* Overflow */
1872 lf->lno_s = INT_MAX;
1873 lf->lno_e = lr->offset + lr->end;
1874 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001875 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001876 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001877 lr->start = lf->lno_s;
1878 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001879 if (dwarf_func_inline(sp_die)) {
1880 struct dwarf_callback_param _param;
1881 _param.data = (void *)lf;
1882 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001883 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001884 line_range_inline_cb,
1885 &_param);
1886 param->retval = _param.retval;
1887 } else
1888 param->retval = find_line_range_by_line(sp_die, lf);
1889 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001890 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001891 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001892}
1893
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001894static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001895{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001896 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1897 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1898 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001899}
1900
1901int find_line_range(int fd, struct line_range *lr)
1902{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001903 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001904 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001905 Dwarf_Off off = 0, noff;
1906 size_t cuhl;
1907 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001908 Dwarf *dbg = NULL;
1909 Dwfl *dwfl;
1910 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001911 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001912
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001913 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001914 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001915 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001916 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001917 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001918 return -EBADF;
1919 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001920
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001921 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001922 while (!lf.found && ret >= 0) {
1923 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001924 break;
1925
1926 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001927 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1928 if (!diep)
1929 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001930
1931 /* Check if target file is included. */
1932 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001933 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001934 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001935 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001936
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001937 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001938 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001939 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001940 else {
1941 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001942 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001943 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001944 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001945 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001946 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001947 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001948
1949 /* Store comp_dir */
1950 if (lf.found) {
1951 comp_dir = cu_get_comp_dir(&lf.cu_die);
1952 if (comp_dir) {
1953 lr->comp_dir = strdup(comp_dir);
1954 if (!lr->comp_dir)
1955 ret = -ENOMEM;
1956 }
1957 }
1958
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001959 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001960 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001961 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001962}
1963