blob: 3e7977560be53f52b84bc590ba35e9275ee58b23 [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>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040034
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050035#include "string.h"
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"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040039#include "probe-finder.h"
40
41
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040042/*
43 * Generic dwarf analysis helpers
44 */
45
46#define X86_32_MAX_REGS 8
47const char *x86_32_regs_table[X86_32_MAX_REGS] = {
48 "%ax",
49 "%cx",
50 "%dx",
51 "%bx",
52 "$stack", /* Stack address instead of %sp */
53 "%bp",
54 "%si",
55 "%di",
56};
57
58#define X86_64_MAX_REGS 16
59const char *x86_64_regs_table[X86_64_MAX_REGS] = {
60 "%ax",
61 "%dx",
62 "%cx",
63 "%bx",
64 "%si",
65 "%di",
66 "%bp",
67 "%sp",
68 "%r8",
69 "%r9",
70 "%r10",
71 "%r11",
72 "%r12",
73 "%r13",
74 "%r14",
75 "%r15",
76};
77
78/* TODO: switching by dwarf address size */
79#ifdef __x86_64__
80#define ARCH_MAX_REGS X86_64_MAX_REGS
81#define arch_regs_table x86_64_regs_table
82#else
83#define ARCH_MAX_REGS X86_32_MAX_REGS
84#define arch_regs_table x86_32_regs_table
85#endif
86
Masami Hiramatsu49849122010-04-12 13:17:15 -040087/* Kprobe tracer basic type is up to u64 */
88#define MAX_BASIC_TYPE_BITS 64
89
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040090/* Return architecture dependent register string (for kprobe-tracer) */
91static const char *get_arch_regstr(unsigned int n)
92{
93 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
94}
95
96/*
97 * Compare the tail of two strings.
98 * Return 0 if whole of either string is same as another's tail part.
99 */
100static int strtailcmp(const char *s1, const char *s2)
101{
102 int i1 = strlen(s1);
103 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -0500104 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400105 if (s1[i1] != s2[i2])
106 return s1[i1] - s2[i2];
107 }
108 return 0;
109}
110
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500111/* Line number list operations */
112
113/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400114static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500115{
116 struct line_node *ln;
117 struct list_head *p;
118
119 /* Reverse search, because new line will be the last one */
120 list_for_each_entry_reverse(ln, head, list) {
121 if (ln->line < line) {
122 p = &ln->list;
123 goto found;
124 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400125 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500126 }
127 /* List is empty, or the smallest entry */
128 p = head;
129found:
130 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400131 ln = zalloc(sizeof(struct line_node));
132 if (ln == NULL)
133 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500134 ln->line = line;
135 INIT_LIST_HEAD(&ln->list);
136 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400137 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500138}
139
140/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400141static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500142{
143 struct line_node *ln;
144
145 /* Reverse search, because new line will be the last one */
146 list_for_each_entry(ln, head, list)
147 if (ln->line == line)
148 return 1;
149
150 return 0;
151}
152
153/* Init line number list */
154static void line_list__init(struct list_head *head)
155{
156 INIT_LIST_HEAD(head);
157}
158
159/* Free line number list */
160static void line_list__free(struct list_head *head)
161{
162 struct line_node *ln;
163 while (!list_empty(head)) {
164 ln = list_first_entry(head, struct line_node, list);
165 list_del(&ln->list);
166 free(ln);
167 }
168}
169
170/* Dwarf wrappers */
171
172/* Find the realpath of the target file. */
173static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400174{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500175 Dwarf_Files *files;
176 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300177 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400178 int ret;
179
180 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500181 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500182
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500183 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500184 if (ret != 0)
185 return NULL;
186
187 for (i = 0; i < nfiles; i++) {
188 src = dwarf_filesrc(files, i, NULL, NULL);
189 if (strtailcmp(src, fname) == 0)
190 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500191 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400192 if (i == nfiles)
193 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500194 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500195}
196
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400197/* Compare diename and tname */
198static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
199{
200 const char *name;
201 name = dwarf_diename(dw_die);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400202 return name ? strcmp(tname, name) : -1;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400203}
204
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400205/* Get type die, but skip qualifiers and typedef */
206static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
207{
208 Dwarf_Attribute attr;
209 int tag;
210
211 do {
212 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
213 dwarf_formref_die(&attr, die_mem) == NULL)
214 return NULL;
215
216 tag = dwarf_tag(die_mem);
217 vr_die = die_mem;
218 } while (tag == DW_TAG_const_type ||
219 tag == DW_TAG_restrict_type ||
220 tag == DW_TAG_volatile_type ||
221 tag == DW_TAG_shared_type ||
222 tag == DW_TAG_typedef);
223
224 return die_mem;
225}
226
Masami Hiramatsu49849122010-04-12 13:17:15 -0400227static bool die_is_signed_type(Dwarf_Die *tp_die)
228{
229 Dwarf_Attribute attr;
230 Dwarf_Word ret;
231
232 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
233 dwarf_formudata(&attr, &ret) != 0)
234 return false;
235
236 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
237 ret == DW_ATE_signed_fixed);
238}
239
240static int die_get_byte_size(Dwarf_Die *tp_die)
241{
242 Dwarf_Attribute attr;
243 Dwarf_Word ret;
244
245 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
246 dwarf_formudata(&attr, &ret) != 0)
247 return 0;
248
249 return (int)ret;
250}
251
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300252/* Get data_member_location offset */
253static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
254{
255 Dwarf_Attribute attr;
256 Dwarf_Op *expr;
257 size_t nexpr;
258 int ret;
259
260 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
261 return -ENOENT;
262
263 if (dwarf_formudata(&attr, offs) != 0) {
264 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
265 ret = dwarf_getlocation(&attr, &expr, &nexpr);
266 if (ret < 0 || nexpr == 0)
267 return -ENOENT;
268
269 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
270 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
271 expr[0].atom, nexpr);
272 return -ENOTSUP;
273 }
274 *offs = (Dwarf_Word)expr[0].number;
275 }
276 return 0;
277}
278
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400279/* Return values for die_find callbacks */
280enum {
281 DIE_FIND_CB_FOUND = 0, /* End of Search */
282 DIE_FIND_CB_CHILD = 1, /* Search only children */
283 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
284 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
285};
286
287/* Search a child die */
288static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
289 int (*callback)(Dwarf_Die *, void *),
290 void *data, Dwarf_Die *die_mem)
291{
292 Dwarf_Die child_die;
293 int ret;
294
295 ret = dwarf_child(rt_die, die_mem);
296 if (ret != 0)
297 return NULL;
298
299 do {
300 ret = callback(die_mem, data);
301 if (ret == DIE_FIND_CB_FOUND)
302 return die_mem;
303
304 if ((ret & DIE_FIND_CB_CHILD) &&
305 die_find_child(die_mem, callback, data, &child_die)) {
306 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
307 return die_mem;
308 }
309 } while ((ret & DIE_FIND_CB_SIBLING) &&
310 dwarf_siblingof(die_mem, die_mem) == 0);
311
312 return NULL;
313}
314
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500315struct __addr_die_search_param {
316 Dwarf_Addr addr;
317 Dwarf_Die *die_mem;
318};
319
320static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
321{
322 struct __addr_die_search_param *ad = data;
323
324 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
325 dwarf_haspc(fn_die, ad->addr)) {
326 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
327 return DWARF_CB_ABORT;
328 }
329 return DWARF_CB_OK;
330}
331
332/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400333static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
334 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500335{
336 struct __addr_die_search_param ad;
337 ad.addr = addr;
338 ad.die_mem = die_mem;
339 /* dwarf_getscopes can't find subprogram. */
340 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
341 return NULL;
342 else
343 return die_mem;
344}
345
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400346/* die_find callback for inline function search */
347static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
348{
349 Dwarf_Addr *addr = data;
350
351 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
352 dwarf_haspc(die_mem, *addr))
353 return DIE_FIND_CB_FOUND;
354
355 return DIE_FIND_CB_CONTINUE;
356}
357
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500358/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400359static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
360 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500361{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400362 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500363}
364
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400365static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400366{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400367 const char *name = data;
368 int tag;
369
370 tag = dwarf_tag(die_mem);
371 if ((tag == DW_TAG_formal_parameter ||
372 tag == DW_TAG_variable) &&
373 (die_compare_name(die_mem, name) == 0))
374 return DIE_FIND_CB_FOUND;
375
376 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400377}
378
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400379/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500380static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
381 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500382{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400383 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
384 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400385}
386
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400387static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
388{
389 const char *name = data;
390
391 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
392 (die_compare_name(die_mem, name) == 0))
393 return DIE_FIND_CB_FOUND;
394
395 return DIE_FIND_CB_SIBLING;
396}
397
398/* Find a member called 'name' */
399static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
400 Dwarf_Die *die_mem)
401{
402 return die_find_child(st_die, __die_find_member_cb, (void *)name,
403 die_mem);
404}
405
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400406/*
407 * Probe finder related functions
408 */
409
410/* Show a location */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400411static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400412{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500413 unsigned int regn;
414 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400415 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400416 const char *regs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400417 struct kprobe_trace_arg *tvar = pf->tvar;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400418
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400419 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500420 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400421 if (pf->fb_ops == NULL) {
422 pr_warning("The attribute of frame base is not "
423 "supported.\n");
424 return -ENOTSUP;
425 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400426 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500427 offs = op->number;
428 op = &pf->fb_ops[0];
429 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400430
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500431 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
432 regn = op->atom - DW_OP_breg0;
433 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400434 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500435 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
436 regn = op->atom - DW_OP_reg0;
437 } else if (op->atom == DW_OP_bregx) {
438 regn = op->number;
439 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400440 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500441 } else if (op->atom == DW_OP_regx) {
442 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400443 } else {
444 pr_warning("DW_OP %x is not supported.\n", op->atom);
445 return -ENOTSUP;
446 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400447
448 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400449 if (!regs) {
450 pr_warning("%u exceeds max register number.\n", regn);
451 return -ERANGE;
452 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400453
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400454 tvar->value = strdup(regs);
455 if (tvar->value == NULL)
456 return -ENOMEM;
457
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400458 if (ref) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400459 tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
460 if (tvar->ref == NULL)
461 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400462 tvar->ref->offset = (long)offs;
463 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400464 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400465}
466
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400467static int convert_variable_type(Dwarf_Die *vr_die,
468 struct kprobe_trace_arg *targ)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400469{
470 Dwarf_Die type;
471 char buf[16];
472 int ret;
473
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400474 if (die_get_real_type(vr_die, &type) == NULL) {
475 pr_warning("Failed to get a type information of %s.\n",
476 dwarf_diename(vr_die));
477 return -ENOENT;
478 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400479
480 ret = die_get_byte_size(&type) * 8;
481 if (ret) {
482 /* Check the bitwidth */
483 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400484 pr_info("%s exceeds max-bitwidth."
485 " Cut down to %d bits.\n",
486 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400487 ret = MAX_BASIC_TYPE_BITS;
488 }
489
490 ret = snprintf(buf, 16, "%c%d",
491 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400492 if (ret < 0 || ret >= 16) {
493 if (ret >= 16)
494 ret = -E2BIG;
495 pr_warning("Failed to convert variable type: %s\n",
496 strerror(-ret));
497 return ret;
498 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400499 targ->type = strdup(buf);
500 if (targ->type == NULL)
501 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400502 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400503 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400504}
505
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400506static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400507 struct perf_probe_arg_field *field,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400508 struct kprobe_trace_arg_ref **ref_ptr,
509 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400510{
511 struct kprobe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400512 Dwarf_Die type;
513 Dwarf_Word offs;
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300514 int ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400515
516 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400517 if (die_get_real_type(vr_die, &type) == NULL) {
518 pr_warning("Failed to get the type of %s.\n", varname);
519 return -ENOENT;
520 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400521
522 /* Check the pointer and dereference */
523 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400524 if (!field->ref) {
525 pr_err("Semantic error: %s must be referred by '->'\n",
526 field->name);
527 return -EINVAL;
528 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400529 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400530 if (die_get_real_type(&type, &type) == NULL) {
531 pr_warning("Failed to get the type of %s.\n", varname);
532 return -ENOENT;
533 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400534 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400535 if (dwarf_tag(&type) != DW_TAG_structure_type) {
536 pr_warning("%s is not a data structure.\n", varname);
537 return -EINVAL;
538 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400539
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400540 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
541 if (ref == NULL)
542 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400543 if (*ref_ptr)
544 (*ref_ptr)->next = ref;
545 else
546 *ref_ptr = ref;
547 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400548 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400549 if (dwarf_tag(&type) != DW_TAG_structure_type) {
550 pr_warning("%s is not a data structure.\n", varname);
551 return -EINVAL;
552 }
553 if (field->ref) {
554 pr_err("Semantic error: %s must be referred by '.'\n",
555 field->name);
556 return -EINVAL;
557 }
558 if (!ref) {
559 pr_warning("Structure on a register is not "
560 "supported yet.\n");
561 return -ENOTSUP;
562 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400563 }
564
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400565 if (die_find_member(&type, field->name, die_mem) == NULL) {
566 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
567 dwarf_diename(&type), field->name);
568 return -EINVAL;
569 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400570
571 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300572 ret = die_get_data_member_location(die_mem, &offs);
573 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400574 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300575 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400576 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400577 ref->offset += (long)offs;
578
579 /* Converting next field */
580 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400581 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300582 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400583 else
584 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400585}
586
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400587/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400588static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400589{
590 Dwarf_Attribute attr;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400591 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500592 Dwarf_Op *expr;
593 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400594 int ret;
595
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500596 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400597 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500598 /* TODO: handle more than 1 exprs */
Masami Hiramatsud0cb4260f2010-03-15 13:02:35 -0400599 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500600 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400601 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500602
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400603 ret = convert_location(expr, pf);
604 if (ret == 0 && pf->pvar->field) {
605 ret = convert_variable_fields(vr_die, pf->pvar->var,
606 pf->pvar->field, &pf->tvar->ref,
607 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400608 vr_die = &die_mem;
609 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400610 if (ret == 0) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400611 if (pf->pvar->type) {
612 pf->tvar->type = strdup(pf->pvar->type);
613 if (pf->tvar->type == NULL)
614 ret = -ENOMEM;
615 } else
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400616 ret = convert_variable_type(vr_die, pf->tvar);
617 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500618 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400619 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400620error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500621 /* TODO: Support const_value */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400622 pr_err("Failed to find the location of %s at this address.\n"
623 " Perhaps, it has been optimized out.\n", pf->pvar->var);
624 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400625}
626
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400627/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400628static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400629{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500630 Dwarf_Die vr_die;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400631 char buf[32], *ptr;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400632 int ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400633
Masami Hiramatsu48481932010-04-12 13:16:53 -0400634 /* TODO: Support arrays */
635 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400636 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400637 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400638 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
639 if (ret < 0)
640 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400641 ptr = strchr(buf, ':'); /* Change type separator to _ */
642 if (ptr)
643 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400644 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400645 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400646 if (pf->tvar->name == NULL)
647 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400648
649 if (!is_c_varname(pf->pvar->var)) {
650 /* Copy raw parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400651 pf->tvar->value = strdup(pf->pvar->var);
652 if (pf->tvar->value == NULL)
653 return -ENOMEM;
654 else
655 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400656 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400657
658 pr_debug("Searching '%s' variable in context.\n",
659 pf->pvar->var);
660 /* Search child die for local variables and parameters. */
661 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
662 pr_warning("Failed to find '%s' in this function.\n",
663 pf->pvar->var);
664 return -ENOENT;
665 }
666 return convert_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400667}
668
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400669/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400670static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400671{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400672 struct kprobe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500673 Dwarf_Addr eaddr;
674 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500675 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400676 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500677 Dwarf_Attribute fb_attr;
678 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400679
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400680 if (pf->ntevs == MAX_PROBES) {
681 pr_warning("Too many( > %d) probe point found.\n", MAX_PROBES);
682 return -ERANGE;
683 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400684 tev = &pf->tevs[pf->ntevs++];
685
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500686 /* If no real subprogram, find a real one */
687 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400688 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500689 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400690 if (!sp_die) {
691 pr_warning("Failed to find probe point in any "
692 "functions.\n");
693 return -ENOENT;
694 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500695 }
696
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400697 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500698 name = dwarf_diename(sp_die);
699 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400700 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
701 pr_warning("Failed to get entry pc of %s\n",
702 dwarf_diename(sp_die));
703 return -ENOENT;
704 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400705 tev->point.symbol = strdup(name);
706 if (tev->point.symbol == NULL)
707 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400708 tev->point.offset = (unsigned long)(pf->addr - eaddr);
709 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400710 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400711 tev->point.offset = (unsigned long)pf->addr;
712
713 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
714 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400715
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500716 /* Get the frame base attribute/ops */
717 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4260f2010-03-15 13:02:35 -0400718 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400719 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500720 pf->fb_ops = NULL;
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400721 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
722 pf->cfi != NULL) {
723 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400724 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
725 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
726 pr_warning("Failed to get CFA on 0x%jx\n",
727 (uintmax_t)pf->addr);
728 return -ENOENT;
729 }
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400730 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500731
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400732 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400733 tev->nargs = pf->pev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400734 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
735 if (tev->args == NULL)
736 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400737 for (i = 0; i < pf->pev->nargs; i++) {
738 pf->pvar = &pf->pev->args[i];
739 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400740 ret = find_variable(sp_die, pf);
741 if (ret != 0)
742 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400743 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500744
745 /* *pf->fb_ops will be cached in libdw. Don't free it. */
746 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400747 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400748}
749
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400750/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400751static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400752{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500753 Dwarf_Lines *lines;
754 Dwarf_Line *line;
755 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500756 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500757 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400758 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400759
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400760 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
761 pr_warning("No source lines found in this CU.\n");
762 return -ENOENT;
763 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400764
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400765 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500766 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400767 if (dwarf_lineno(line, &lineno) != 0 ||
768 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400769 continue;
770
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500771 /* TODO: Get fileno from line, but how? */
772 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
773 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400774
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400775 if (dwarf_lineaddr(line, &addr) != 0) {
776 pr_warning("Failed to get the address of the line.\n");
777 return -ENOENT;
778 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500779 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
780 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400781 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500782
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400783 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400784 /* Continuing, because target line might be inlined. */
785 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400786 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400787}
788
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500789/* Find lines which match lazy pattern */
790static int find_lazy_match_lines(struct list_head *head,
791 const char *fname, const char *pat)
792{
793 char *fbuf, *p1, *p2;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400794 int fd, ret, line, nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500795 struct stat st;
796
797 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400798 if (fd < 0) {
799 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
800 return fd;
801 }
802
803 ret = fstat(fd, &st);
804 if (ret < 0) {
805 pr_warning("Failed to get the size of %s: %s\n",
806 fname, strerror(errno));
807 return ret;
808 }
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400809 fbuf = xmalloc(st.st_size + 2);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400810 ret = read(fd, fbuf, st.st_size);
811 if (ret < 0) {
812 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
813 return ret;
814 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500815 close(fd);
816 fbuf[st.st_size] = '\n'; /* Dummy line */
817 fbuf[st.st_size + 1] = '\0';
818 p1 = fbuf;
819 line = 1;
820 while ((p2 = strchr(p1, '\n')) != NULL) {
821 *p2 = '\0';
822 if (strlazymatch(p1, pat)) {
823 line_list__add_line(head, line);
824 nlines++;
825 }
826 line++;
827 p1 = p2 + 1;
828 }
829 free(fbuf);
830 return nlines;
831}
832
833/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400834static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500835{
836 Dwarf_Lines *lines;
837 Dwarf_Line *line;
838 size_t nlines, i;
839 Dwarf_Addr addr;
840 Dwarf_Die die_mem;
841 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400842 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500843
844 if (list_empty(&pf->lcache)) {
845 /* Matching lazy line pattern */
846 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400847 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400848 if (ret == 0) {
849 pr_debug("No matched lines found in %s.\n", pf->fname);
850 return 0;
851 } else if (ret < 0)
852 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500853 }
854
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400855 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
856 pr_warning("No source lines found in this CU.\n");
857 return -ENOENT;
858 }
859
860 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500861 line = dwarf_onesrcline(lines, i);
862
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400863 if (dwarf_lineno(line, &lineno) != 0 ||
864 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500865 continue;
866
867 /* TODO: Get fileno from line, but how? */
868 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
869 continue;
870
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400871 if (dwarf_lineaddr(line, &addr) != 0) {
872 pr_debug("Failed to get the address of line %d.\n",
873 lineno);
874 continue;
875 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500876 if (sp_die) {
877 /* Address filtering 1: does sp_die include addr? */
878 if (!dwarf_haspc(sp_die, addr))
879 continue;
880 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400881 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500882 continue;
883 }
884
885 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
886 (int)i, lineno, (unsigned long long)addr);
887 pf->addr = addr;
888
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400889 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500890 /* Continuing, because target line might be inlined. */
891 }
892 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400893 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500894}
895
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400896/* Callback parameter with return value */
897struct dwarf_callback_param {
898 void *data;
899 int retval;
900};
901
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500902static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400903{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400904 struct dwarf_callback_param *param = data;
905 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400906 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400907 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400908
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500909 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400910 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500911 else {
912 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400913 if (dwarf_entrypc(in_die, &addr) != 0) {
914 pr_warning("Failed to get entry pc of %s.\n",
915 dwarf_diename(in_die));
916 param->retval = -ENOENT;
917 return DWARF_CB_ABORT;
918 }
919 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500920 pf->addr += pp->offset;
921 pr_debug("found inline addr: 0x%jx\n",
922 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500923
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400924 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500925 }
926
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500927 return DWARF_CB_OK;
928}
929
930/* Search function from function name */
931static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
932{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400933 struct dwarf_callback_param *param = data;
934 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400935 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500936
937 /* Check tag and diename */
938 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
939 die_compare_name(sp_die, pp->function) != 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400940 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500941
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500942 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500943 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500944 dwarf_decl_line(sp_die, &pf->lno);
945 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400946 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500947 } else if (!dwarf_func_inline(sp_die)) {
948 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500949 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400950 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500951 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400952 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
953 pr_warning("Failed to get entry pc of %s.\n",
954 dwarf_diename(sp_die));
955 param->retval = -ENOENT;
956 return DWARF_CB_ABORT;
957 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500958 pf->addr += pp->offset;
959 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400960 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500961 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400962 } else {
963 struct dwarf_callback_param _param = {.data = (void *)pf,
964 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500965 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400966 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
967 &_param);
968 param->retval = _param.retval;
969 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500970
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400971 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400972}
973
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400974static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400975{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400976 struct dwarf_callback_param _param = {.data = (void *)pf,
977 .retval = 0};
978 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
979 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400980}
981
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
983int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
984 struct kprobe_trace_event **tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400985{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400986 struct probe_finder pf = {.pev = pev};
987 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500988 Dwarf_Off off, noff;
989 size_t cuhl;
990 Dwarf_Die *diep;
991 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400992 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400993
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400994 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
995 if (pf.tevs == NULL)
996 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400997 *tevs = pf.tevs;
998 pf.ntevs = 0;
999
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001000 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001001 if (!dbg) {
1002 pr_warning("No dwarf info found in the vmlinux - "
1003 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1004 return -EBADF;
1005 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001006
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001007 /* Get the call frame information from this dwarf */
1008 pf.cfi = dwarf_getcfi(dbg);
1009
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001010 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001011 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001012 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001013 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1014 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001015 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001016 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
1017 if (!diep)
1018 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001019
1020 /* Check if target file is included. */
1021 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001022 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001023 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001024 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001025
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001026 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001027 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001028 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001029 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001030 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001031 else {
1032 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001033 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001034 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001035 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001036 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001037 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001038 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001039 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001040
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001041 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001042}
1043
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001044/* Reverse search */
1045int find_perf_probe_point(int fd, unsigned long addr,
1046 struct perf_probe_point *ppt)
1047{
1048 Dwarf_Die cudie, spdie, indie;
1049 Dwarf *dbg;
1050 Dwarf_Line *line;
1051 Dwarf_Addr laddr, eaddr;
1052 const char *tmp;
1053 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001054 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001055
1056 dbg = dwarf_begin(fd, DWARF_C_READ);
1057 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001058 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001059
1060 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001061 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1062 ret = -EINVAL;
1063 goto end;
1064 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001065
1066 /* Find a corresponding line */
1067 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1068 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001069 if (dwarf_lineaddr(line, &laddr) == 0 &&
1070 (Dwarf_Addr)addr == laddr &&
1071 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001072 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001073 if (tmp) {
1074 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001075 ppt->file = strdup(tmp);
1076 if (ppt->file == NULL) {
1077 ret = -ENOMEM;
1078 goto end;
1079 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001080 found = true;
1081 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001082 }
1083 }
1084
1085 /* Find a corresponding function */
1086 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1087 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001088 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001089 goto end;
1090
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001091 if (ppt->line) {
1092 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1093 &indie)) {
1094 /* addr in an inline function */
1095 tmp = dwarf_diename(&indie);
1096 if (!tmp)
1097 goto end;
1098 ret = dwarf_decl_line(&indie, &lineno);
1099 } else {
1100 if (eaddr == addr) { /* Function entry */
1101 lineno = ppt->line;
1102 ret = 0;
1103 } else
1104 ret = dwarf_decl_line(&spdie, &lineno);
1105 }
1106 if (ret == 0) {
1107 /* Make a relative line number */
1108 ppt->line -= lineno;
1109 goto found;
1110 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001111 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001112 /* We don't have a line number, let's use offset */
1113 ppt->offset = addr - (unsigned long)eaddr;
1114found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001115 ppt->function = strdup(tmp);
1116 if (ppt->function == NULL) {
1117 ret = -ENOMEM;
1118 goto end;
1119 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001120 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001121 }
1122
1123end:
1124 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001125 if (ret >= 0)
1126 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001127 return ret;
1128}
1129
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001130/* Add a line and store the src path */
1131static int line_range_add_line(const char *src, unsigned int lineno,
1132 struct line_range *lr)
1133{
1134 /* Copy real path */
1135 if (!lr->path) {
1136 lr->path = strdup(src);
1137 if (lr->path == NULL)
1138 return -ENOMEM;
1139 }
1140 return line_list__add_line(&lr->line_list, lineno);
1141}
1142
1143/* Search function declaration lines */
1144static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1145{
1146 struct dwarf_callback_param *param = data;
1147 struct line_finder *lf = param->data;
1148 const char *src;
1149 int lineno;
1150
1151 src = dwarf_decl_file(sp_die);
1152 if (src && strtailcmp(src, lf->fname) != 0)
1153 return DWARF_CB_OK;
1154
1155 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1156 (lf->lno_s > lineno || lf->lno_e < lineno))
1157 return DWARF_CB_OK;
1158
1159 param->retval = line_range_add_line(src, lineno, lf->lr);
1160 return DWARF_CB_OK;
1161}
1162
1163static int find_line_range_func_decl_lines(struct line_finder *lf)
1164{
1165 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1166 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1167 return param.retval;
1168}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001169
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001170/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001171static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001172{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001173 Dwarf_Lines *lines;
1174 Dwarf_Line *line;
1175 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001176 Dwarf_Addr addr;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001177 int lineno, ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001178 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001179 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001180
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001181 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001182 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1183 pr_warning("No source lines found in this CU.\n");
1184 return -ENOENT;
1185 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001186
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001187 /* Search probable lines on lines list */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001188 for (i = 0; i < nlines; i++) {
1189 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001190 if (dwarf_lineno(line, &lineno) != 0 ||
1191 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001192 continue;
1193
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001194 if (sp_die) {
1195 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001196 if (dwarf_lineaddr(line, &addr) != 0 ||
1197 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001198 continue;
1199
1200 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001201 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001202 continue;
1203 }
1204
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001205 /* TODO: Get fileno from line, but how? */
1206 src = dwarf_linesrc(line, NULL, NULL);
1207 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001208 continue;
1209
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001210 ret = line_range_add_line(src, lineno, lf->lr);
1211 if (ret < 0)
1212 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001213 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001214
1215 /*
1216 * Dwarf lines doesn't include function declarations. We have to
1217 * check functions list or given function.
1218 */
1219 if (sp_die) {
1220 src = dwarf_decl_file(sp_die);
1221 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1222 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1223 ret = line_range_add_line(src, lineno, lf->lr);
1224 } else
1225 ret = find_line_range_func_decl_lines(lf);
1226
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001227 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001228 if (ret >= 0)
1229 if (!list_empty(&lf->lr->line_list))
1230 ret = lf->found = 1;
1231 else
1232 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001233 else {
1234 free(lf->lr->path);
1235 lf->lr->path = NULL;
1236 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001237 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001238}
1239
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001240static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1241{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001242 struct dwarf_callback_param *param = data;
1243
1244 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001245 return DWARF_CB_ABORT; /* No need to find other instances */
1246}
1247
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001248/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001249static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001250{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001251 struct dwarf_callback_param *param = data;
1252 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001253 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001254
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001255 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1256 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001257 lf->fname = dwarf_decl_file(sp_die);
1258 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001259 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001260 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001261 if (lf->lno_s < 0) /* Overflow */
1262 lf->lno_s = INT_MAX;
1263 lf->lno_e = lr->offset + lr->end;
1264 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001265 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001266 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001267 lr->start = lf->lno_s;
1268 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001269 if (dwarf_func_inline(sp_die)) {
1270 struct dwarf_callback_param _param;
1271 _param.data = (void *)lf;
1272 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001273 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001274 line_range_inline_cb,
1275 &_param);
1276 param->retval = _param.retval;
1277 } else
1278 param->retval = find_line_range_by_line(sp_die, lf);
1279 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001280 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001281 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001282}
1283
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001284static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001285{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001286 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1287 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1288 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001289}
1290
1291int find_line_range(int fd, struct line_range *lr)
1292{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001293 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001294 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001295 Dwarf_Off off = 0, noff;
1296 size_t cuhl;
1297 Dwarf_Die *diep;
1298 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001299
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001300 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001301 if (!dbg) {
1302 pr_warning("No dwarf info found in the vmlinux - "
1303 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1304 return -EBADF;
1305 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001306
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001307 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001308 while (!lf.found && ret >= 0) {
1309 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001310 break;
1311
1312 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001313 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1314 if (!diep)
1315 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001316
1317 /* Check if target file is included. */
1318 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001319 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001320 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001321 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001322
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001323 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001324 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001325 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001326 else {
1327 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001328 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001329 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001330 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001331 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001332 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001333 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001334 pr_debug("path: %lx\n", (unsigned long)lr->path);
1335 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001336
1337 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001338}
1339