blob: 54daa91e901d68117c911591bb1c30f2354ad179 [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 */
114static void line_list__add_line(struct list_head *head, unsigned int line)
115{
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 */
125 return ;
126 }
127 /* List is empty, or the smallest entry */
128 p = head;
129found:
130 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400131 ln = xzalloc(sizeof(struct line_node));
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500132 ln->line = line;
133 INIT_LIST_HEAD(&ln->list);
134 list_add(&ln->list, p);
135}
136
137/* Check if the line in line number list */
138static int line_list__has_line(struct list_head *head, unsigned int line)
139{
140 struct line_node *ln;
141
142 /* Reverse search, because new line will be the last one */
143 list_for_each_entry(ln, head, list)
144 if (ln->line == line)
145 return 1;
146
147 return 0;
148}
149
150/* Init line number list */
151static void line_list__init(struct list_head *head)
152{
153 INIT_LIST_HEAD(head);
154}
155
156/* Free line number list */
157static void line_list__free(struct list_head *head)
158{
159 struct line_node *ln;
160 while (!list_empty(head)) {
161 ln = list_first_entry(head, struct line_node, list);
162 list_del(&ln->list);
163 free(ln);
164 }
165}
166
167/* Dwarf wrappers */
168
169/* Find the realpath of the target file. */
170static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400171{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500172 Dwarf_Files *files;
173 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300174 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400175 int ret;
176
177 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500178 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500179
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500180 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500181 if (ret != 0)
182 return NULL;
183
184 for (i = 0; i < nfiles; i++) {
185 src = dwarf_filesrc(files, i, NULL, NULL);
186 if (strtailcmp(src, fname) == 0)
187 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500188 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400189 if (i == nfiles)
190 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500191 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500192}
193
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400194/* Compare diename and tname */
195static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
196{
197 const char *name;
198 name = dwarf_diename(dw_die);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400199 return name ? strcmp(tname, name) : -1;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400200}
201
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400202/* Get type die, but skip qualifiers and typedef */
203static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
204{
205 Dwarf_Attribute attr;
206 int tag;
207
208 do {
209 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
210 dwarf_formref_die(&attr, die_mem) == NULL)
211 return NULL;
212
213 tag = dwarf_tag(die_mem);
214 vr_die = die_mem;
215 } while (tag == DW_TAG_const_type ||
216 tag == DW_TAG_restrict_type ||
217 tag == DW_TAG_volatile_type ||
218 tag == DW_TAG_shared_type ||
219 tag == DW_TAG_typedef);
220
221 return die_mem;
222}
223
Masami Hiramatsu49849122010-04-12 13:17:15 -0400224static bool die_is_signed_type(Dwarf_Die *tp_die)
225{
226 Dwarf_Attribute attr;
227 Dwarf_Word ret;
228
229 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
230 dwarf_formudata(&attr, &ret) != 0)
231 return false;
232
233 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
234 ret == DW_ATE_signed_fixed);
235}
236
237static int die_get_byte_size(Dwarf_Die *tp_die)
238{
239 Dwarf_Attribute attr;
240 Dwarf_Word ret;
241
242 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
243 dwarf_formudata(&attr, &ret) != 0)
244 return 0;
245
246 return (int)ret;
247}
248
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400249/* Return values for die_find callbacks */
250enum {
251 DIE_FIND_CB_FOUND = 0, /* End of Search */
252 DIE_FIND_CB_CHILD = 1, /* Search only children */
253 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
254 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
255};
256
257/* Search a child die */
258static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
259 int (*callback)(Dwarf_Die *, void *),
260 void *data, Dwarf_Die *die_mem)
261{
262 Dwarf_Die child_die;
263 int ret;
264
265 ret = dwarf_child(rt_die, die_mem);
266 if (ret != 0)
267 return NULL;
268
269 do {
270 ret = callback(die_mem, data);
271 if (ret == DIE_FIND_CB_FOUND)
272 return die_mem;
273
274 if ((ret & DIE_FIND_CB_CHILD) &&
275 die_find_child(die_mem, callback, data, &child_die)) {
276 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
277 return die_mem;
278 }
279 } while ((ret & DIE_FIND_CB_SIBLING) &&
280 dwarf_siblingof(die_mem, die_mem) == 0);
281
282 return NULL;
283}
284
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500285struct __addr_die_search_param {
286 Dwarf_Addr addr;
287 Dwarf_Die *die_mem;
288};
289
290static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
291{
292 struct __addr_die_search_param *ad = data;
293
294 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
295 dwarf_haspc(fn_die, ad->addr)) {
296 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
297 return DWARF_CB_ABORT;
298 }
299 return DWARF_CB_OK;
300}
301
302/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400303static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
304 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500305{
306 struct __addr_die_search_param ad;
307 ad.addr = addr;
308 ad.die_mem = die_mem;
309 /* dwarf_getscopes can't find subprogram. */
310 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
311 return NULL;
312 else
313 return die_mem;
314}
315
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400316/* die_find callback for inline function search */
317static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
318{
319 Dwarf_Addr *addr = data;
320
321 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
322 dwarf_haspc(die_mem, *addr))
323 return DIE_FIND_CB_FOUND;
324
325 return DIE_FIND_CB_CONTINUE;
326}
327
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500328/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400329static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
330 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500331{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400332 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500333}
334
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400335static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400336{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400337 const char *name = data;
338 int tag;
339
340 tag = dwarf_tag(die_mem);
341 if ((tag == DW_TAG_formal_parameter ||
342 tag == DW_TAG_variable) &&
343 (die_compare_name(die_mem, name) == 0))
344 return DIE_FIND_CB_FOUND;
345
346 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400347}
348
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400349/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500350static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
351 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500352{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400353 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
354 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400355}
356
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400357static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
358{
359 const char *name = data;
360
361 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
362 (die_compare_name(die_mem, name) == 0))
363 return DIE_FIND_CB_FOUND;
364
365 return DIE_FIND_CB_SIBLING;
366}
367
368/* Find a member called 'name' */
369static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
370 Dwarf_Die *die_mem)
371{
372 return die_find_child(st_die, __die_find_member_cb, (void *)name,
373 die_mem);
374}
375
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400376/*
377 * Probe finder related functions
378 */
379
380/* Show a location */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400381static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400382{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500383 unsigned int regn;
384 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400385 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400386 const char *regs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400387 struct kprobe_trace_arg *tvar = pf->tvar;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400388
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400389 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500390 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400391 if (pf->fb_ops == NULL) {
392 pr_warning("The attribute of frame base is not "
393 "supported.\n");
394 return -ENOTSUP;
395 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400396 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500397 offs = op->number;
398 op = &pf->fb_ops[0];
399 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400400
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500401 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
402 regn = op->atom - DW_OP_breg0;
403 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400404 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500405 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
406 regn = op->atom - DW_OP_reg0;
407 } else if (op->atom == DW_OP_bregx) {
408 regn = op->number;
409 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400410 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500411 } else if (op->atom == DW_OP_regx) {
412 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400413 } else {
414 pr_warning("DW_OP %x is not supported.\n", op->atom);
415 return -ENOTSUP;
416 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400417
418 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400419 if (!regs) {
420 pr_warning("%u exceeds max register number.\n", regn);
421 return -ERANGE;
422 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400423
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400424 tvar->value = xstrdup(regs);
425 if (ref) {
426 tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
427 tvar->ref->offset = (long)offs;
428 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400429 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400430}
431
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400432static int convert_variable_type(Dwarf_Die *vr_die,
433 struct kprobe_trace_arg *targ)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400434{
435 Dwarf_Die type;
436 char buf[16];
437 int ret;
438
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400439 if (die_get_real_type(vr_die, &type) == NULL) {
440 pr_warning("Failed to get a type information of %s.\n",
441 dwarf_diename(vr_die));
442 return -ENOENT;
443 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400444
445 ret = die_get_byte_size(&type) * 8;
446 if (ret) {
447 /* Check the bitwidth */
448 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400449 pr_info("%s exceeds max-bitwidth."
450 " Cut down to %d bits.\n",
451 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400452 ret = MAX_BASIC_TYPE_BITS;
453 }
454
455 ret = snprintf(buf, 16, "%c%d",
456 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400457 if (ret < 0 || ret >= 16) {
458 if (ret >= 16)
459 ret = -E2BIG;
460 pr_warning("Failed to convert variable type: %s\n",
461 strerror(-ret));
462 return ret;
463 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400464 targ->type = xstrdup(buf);
465 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400466 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400467}
468
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400469static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400470 struct perf_probe_arg_field *field,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400471 struct kprobe_trace_arg_ref **ref_ptr,
472 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400473{
474 struct kprobe_trace_arg_ref *ref = *ref_ptr;
475 Dwarf_Attribute attr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400476 Dwarf_Die type;
477 Dwarf_Word offs;
478
479 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400480 if (die_get_real_type(vr_die, &type) == NULL) {
481 pr_warning("Failed to get the type of %s.\n", varname);
482 return -ENOENT;
483 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400484
485 /* Check the pointer and dereference */
486 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400487 if (!field->ref) {
488 pr_err("Semantic error: %s must be referred by '->'\n",
489 field->name);
490 return -EINVAL;
491 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400492 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400493 if (die_get_real_type(&type, &type) == NULL) {
494 pr_warning("Failed to get the type of %s.\n", varname);
495 return -ENOENT;
496 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400497 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400498 if (dwarf_tag(&type) != DW_TAG_structure_type) {
499 pr_warning("%s is not a data structure.\n", varname);
500 return -EINVAL;
501 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400502
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400503 ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
504 if (*ref_ptr)
505 (*ref_ptr)->next = ref;
506 else
507 *ref_ptr = ref;
508 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400509 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400510 if (dwarf_tag(&type) != DW_TAG_structure_type) {
511 pr_warning("%s is not a data structure.\n", varname);
512 return -EINVAL;
513 }
514 if (field->ref) {
515 pr_err("Semantic error: %s must be referred by '.'\n",
516 field->name);
517 return -EINVAL;
518 }
519 if (!ref) {
520 pr_warning("Structure on a register is not "
521 "supported yet.\n");
522 return -ENOTSUP;
523 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400524 }
525
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400526 if (die_find_member(&type, field->name, die_mem) == NULL) {
527 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
528 dwarf_diename(&type), field->name);
529 return -EINVAL;
530 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400531
532 /* Get the offset of the field */
Masami Hiramatsu49849122010-04-12 13:17:15 -0400533 if (dwarf_attr(die_mem, DW_AT_data_member_location, &attr) == NULL ||
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400534 dwarf_formudata(&attr, &offs) != 0) {
535 pr_warning("Failed to get the offset of %s.\n", field->name);
536 return -ENOENT;
537 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400538 ref->offset += (long)offs;
539
540 /* Converting next field */
541 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400542 return convert_variable_fields(die_mem, field->name,
543 field->next, &ref, die_mem);
544 else
545 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400546}
547
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400548/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400549static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400550{
551 Dwarf_Attribute attr;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400552 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500553 Dwarf_Op *expr;
554 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400555 int ret;
556
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500557 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400558 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500559 /* TODO: handle more than 1 exprs */
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400560 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500561 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400562 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500563
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400564 ret = convert_location(expr, pf);
565 if (ret == 0 && pf->pvar->field) {
566 ret = convert_variable_fields(vr_die, pf->pvar->var,
567 pf->pvar->field, &pf->tvar->ref,
568 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400569 vr_die = &die_mem;
570 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400571 if (ret == 0) {
572 if (pf->pvar->type)
573 pf->tvar->type = xstrdup(pf->pvar->type);
574 else
575 ret = convert_variable_type(vr_die, pf->tvar);
576 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500577 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400578 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400579error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500580 /* TODO: Support const_value */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400581 pr_err("Failed to find the location of %s at this address.\n"
582 " Perhaps, it has been optimized out.\n", pf->pvar->var);
583 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400584}
585
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400586/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400587static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400588{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500589 Dwarf_Die vr_die;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400590 char buf[32], *ptr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400591
Masami Hiramatsu48481932010-04-12 13:16:53 -0400592 /* TODO: Support arrays */
593 if (pf->pvar->name)
594 pf->tvar->name = xstrdup(pf->pvar->name);
595 else {
596 synthesize_perf_probe_arg(pf->pvar, buf, 32);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400597 ptr = strchr(buf, ':'); /* Change type separator to _ */
598 if (ptr)
599 *ptr = '_';
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400600 pf->tvar->name = xstrdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400601 }
602
603 if (!is_c_varname(pf->pvar->var)) {
604 /* Copy raw parameters */
605 pf->tvar->value = xstrdup(pf->pvar->var);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400606 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400607 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400608
609 pr_debug("Searching '%s' variable in context.\n",
610 pf->pvar->var);
611 /* Search child die for local variables and parameters. */
612 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
613 pr_warning("Failed to find '%s' in this function.\n",
614 pf->pvar->var);
615 return -ENOENT;
616 }
617 return convert_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400618}
619
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400620/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400621static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400622{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400623 struct kprobe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500624 Dwarf_Addr eaddr;
625 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500626 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400627 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500628 Dwarf_Attribute fb_attr;
629 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400630
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400631 if (pf->ntevs == MAX_PROBES) {
632 pr_warning("Too many( > %d) probe point found.\n", MAX_PROBES);
633 return -ERANGE;
634 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400635 tev = &pf->tevs[pf->ntevs++];
636
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500637 /* If no real subprogram, find a real one */
638 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400639 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500640 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400641 if (!sp_die) {
642 pr_warning("Failed to find probe point in any "
643 "functions.\n");
644 return -ENOENT;
645 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500646 }
647
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400648 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500649 name = dwarf_diename(sp_die);
650 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400651 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
652 pr_warning("Failed to get entry pc of %s\n",
653 dwarf_diename(sp_die));
654 return -ENOENT;
655 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400656 tev->point.symbol = xstrdup(name);
657 tev->point.offset = (unsigned long)(pf->addr - eaddr);
658 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400659 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400660 tev->point.offset = (unsigned long)pf->addr;
661
662 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
663 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400664
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500665 /* Get the frame base attribute/ops */
666 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400667 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400668 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500669 pf->fb_ops = NULL;
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400670 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
671 pf->cfi != NULL) {
672 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400673 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
674 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
675 pr_warning("Failed to get CFA on 0x%jx\n",
676 (uintmax_t)pf->addr);
677 return -ENOENT;
678 }
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400679 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500680
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400681 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400682 tev->nargs = pf->pev->nargs;
683 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
684 for (i = 0; i < pf->pev->nargs; i++) {
685 pf->pvar = &pf->pev->args[i];
686 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400687 ret = find_variable(sp_die, pf);
688 if (ret != 0)
689 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400690 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500691
692 /* *pf->fb_ops will be cached in libdw. Don't free it. */
693 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400694 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400695}
696
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400697/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400698static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400699{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500700 Dwarf_Lines *lines;
701 Dwarf_Line *line;
702 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500703 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500704 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400705 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400706
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400707 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
708 pr_warning("No source lines found in this CU.\n");
709 return -ENOENT;
710 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400711
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400712 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500713 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400714 if (dwarf_lineno(line, &lineno) != 0 ||
715 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400716 continue;
717
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500718 /* TODO: Get fileno from line, but how? */
719 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
720 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400721
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400722 if (dwarf_lineaddr(line, &addr) != 0) {
723 pr_warning("Failed to get the address of the line.\n");
724 return -ENOENT;
725 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500726 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
727 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400728 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500729
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400730 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400731 /* Continuing, because target line might be inlined. */
732 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400733 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400734}
735
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500736/* Find lines which match lazy pattern */
737static int find_lazy_match_lines(struct list_head *head,
738 const char *fname, const char *pat)
739{
740 char *fbuf, *p1, *p2;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400741 int fd, ret, line, nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500742 struct stat st;
743
744 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400745 if (fd < 0) {
746 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
747 return fd;
748 }
749
750 ret = fstat(fd, &st);
751 if (ret < 0) {
752 pr_warning("Failed to get the size of %s: %s\n",
753 fname, strerror(errno));
754 return ret;
755 }
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400756 fbuf = xmalloc(st.st_size + 2);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400757 ret = read(fd, fbuf, st.st_size);
758 if (ret < 0) {
759 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
760 return ret;
761 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500762 close(fd);
763 fbuf[st.st_size] = '\n'; /* Dummy line */
764 fbuf[st.st_size + 1] = '\0';
765 p1 = fbuf;
766 line = 1;
767 while ((p2 = strchr(p1, '\n')) != NULL) {
768 *p2 = '\0';
769 if (strlazymatch(p1, pat)) {
770 line_list__add_line(head, line);
771 nlines++;
772 }
773 line++;
774 p1 = p2 + 1;
775 }
776 free(fbuf);
777 return nlines;
778}
779
780/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400781static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500782{
783 Dwarf_Lines *lines;
784 Dwarf_Line *line;
785 size_t nlines, i;
786 Dwarf_Addr addr;
787 Dwarf_Die die_mem;
788 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400789 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500790
791 if (list_empty(&pf->lcache)) {
792 /* Matching lazy line pattern */
793 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400794 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400795 if (ret == 0) {
796 pr_debug("No matched lines found in %s.\n", pf->fname);
797 return 0;
798 } else if (ret < 0)
799 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500800 }
801
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400802 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
803 pr_warning("No source lines found in this CU.\n");
804 return -ENOENT;
805 }
806
807 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500808 line = dwarf_onesrcline(lines, i);
809
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400810 if (dwarf_lineno(line, &lineno) != 0 ||
811 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500812 continue;
813
814 /* TODO: Get fileno from line, but how? */
815 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
816 continue;
817
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400818 if (dwarf_lineaddr(line, &addr) != 0) {
819 pr_debug("Failed to get the address of line %d.\n",
820 lineno);
821 continue;
822 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500823 if (sp_die) {
824 /* Address filtering 1: does sp_die include addr? */
825 if (!dwarf_haspc(sp_die, addr))
826 continue;
827 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400828 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500829 continue;
830 }
831
832 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
833 (int)i, lineno, (unsigned long long)addr);
834 pf->addr = addr;
835
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400836 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500837 /* Continuing, because target line might be inlined. */
838 }
839 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400840 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500841}
842
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400843/* Callback parameter with return value */
844struct dwarf_callback_param {
845 void *data;
846 int retval;
847};
848
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500849static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400850{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400851 struct dwarf_callback_param *param = data;
852 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400853 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400854 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400855
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500856 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400857 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500858 else {
859 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400860 if (dwarf_entrypc(in_die, &addr) != 0) {
861 pr_warning("Failed to get entry pc of %s.\n",
862 dwarf_diename(in_die));
863 param->retval = -ENOENT;
864 return DWARF_CB_ABORT;
865 }
866 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500867 pf->addr += pp->offset;
868 pr_debug("found inline addr: 0x%jx\n",
869 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500870
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400871 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500872 }
873
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500874 return DWARF_CB_OK;
875}
876
877/* Search function from function name */
878static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
879{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400880 struct dwarf_callback_param *param = data;
881 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400882 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500883
884 /* Check tag and diename */
885 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
886 die_compare_name(sp_die, pp->function) != 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400887 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500888
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500889 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500890 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500891 dwarf_decl_line(sp_die, &pf->lno);
892 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400893 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500894 } else if (!dwarf_func_inline(sp_die)) {
895 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500896 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400897 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500898 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400899 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
900 pr_warning("Failed to get entry pc of %s.\n",
901 dwarf_diename(sp_die));
902 param->retval = -ENOENT;
903 return DWARF_CB_ABORT;
904 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500905 pf->addr += pp->offset;
906 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400907 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500908 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400909 } else {
910 struct dwarf_callback_param _param = {.data = (void *)pf,
911 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500912 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400913 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
914 &_param);
915 param->retval = _param.retval;
916 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500917
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400918 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400919}
920
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400921static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400922{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400923 struct dwarf_callback_param _param = {.data = (void *)pf,
924 .retval = 0};
925 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
926 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400927}
928
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400929/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
930int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
931 struct kprobe_trace_event **tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400932{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400933 struct probe_finder pf = {.pev = pev};
934 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500935 Dwarf_Off off, noff;
936 size_t cuhl;
937 Dwarf_Die *diep;
938 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400939 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400940
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400941 pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
942 *tevs = pf.tevs;
943 pf.ntevs = 0;
944
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500945 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400946 if (!dbg) {
947 pr_warning("No dwarf info found in the vmlinux - "
948 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
949 return -EBADF;
950 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400951
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400952 /* Get the call frame information from this dwarf */
953 pf.cfi = dwarf_getcfi(dbg);
954
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500955 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500956 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500957 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400958 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
959 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400960 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500961 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
962 if (!diep)
963 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400964
965 /* Check if target file is included. */
966 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500967 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500968 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500969 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400970
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500971 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400972 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400973 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500974 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400975 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400976 else {
977 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400978 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400979 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400980 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500981 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400982 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500983 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500984 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400985
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400986 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400987}
988
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400989/* Reverse search */
990int find_perf_probe_point(int fd, unsigned long addr,
991 struct perf_probe_point *ppt)
992{
993 Dwarf_Die cudie, spdie, indie;
994 Dwarf *dbg;
995 Dwarf_Line *line;
996 Dwarf_Addr laddr, eaddr;
997 const char *tmp;
998 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400999 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001000
1001 dbg = dwarf_begin(fd, DWARF_C_READ);
1002 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001003 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001004
1005 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001006 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1007 ret = -EINVAL;
1008 goto end;
1009 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001010
1011 /* Find a corresponding line */
1012 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1013 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001014 if (dwarf_lineaddr(line, &laddr) == 0 &&
1015 (Dwarf_Addr)addr == laddr &&
1016 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001017 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001018 if (tmp) {
1019 ppt->line = lineno;
1020 ppt->file = xstrdup(tmp);
1021 found = true;
1022 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001023 }
1024 }
1025
1026 /* Find a corresponding function */
1027 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1028 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001029 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001030 goto end;
1031
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001032 if (ppt->line) {
1033 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1034 &indie)) {
1035 /* addr in an inline function */
1036 tmp = dwarf_diename(&indie);
1037 if (!tmp)
1038 goto end;
1039 ret = dwarf_decl_line(&indie, &lineno);
1040 } else {
1041 if (eaddr == addr) { /* Function entry */
1042 lineno = ppt->line;
1043 ret = 0;
1044 } else
1045 ret = dwarf_decl_line(&spdie, &lineno);
1046 }
1047 if (ret == 0) {
1048 /* Make a relative line number */
1049 ppt->line -= lineno;
1050 goto found;
1051 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001052 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001053 /* We don't have a line number, let's use offset */
1054 ppt->offset = addr - (unsigned long)eaddr;
1055found:
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001056 ppt->function = xstrdup(tmp);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001057 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001058 }
1059
1060end:
1061 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001062 if (ret >= 0)
1063 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001064 return ret;
1065}
1066
1067
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001068/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001069static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001070{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001071 Dwarf_Lines *lines;
1072 Dwarf_Line *line;
1073 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001074 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001075 int lineno;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001076 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001077 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001078
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001079 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001080 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1081 pr_warning("No source lines found in this CU.\n");
1082 return -ENOENT;
1083 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001084
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001085 for (i = 0; i < nlines; i++) {
1086 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001087 if (dwarf_lineno(line, &lineno) != 0 ||
1088 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001089 continue;
1090
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001091 if (sp_die) {
1092 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001093 if (dwarf_lineaddr(line, &addr) != 0 ||
1094 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001095 continue;
1096
1097 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001098 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001099 continue;
1100 }
1101
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001102 /* TODO: Get fileno from line, but how? */
1103 src = dwarf_linesrc(line, NULL, NULL);
1104 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001105 continue;
1106
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001107 /* Copy real path */
1108 if (!lf->lr->path)
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001109 lf->lr->path = xstrdup(src);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001110 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001111 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001112 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001113 if (!list_empty(&lf->lr->line_list))
1114 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001115 else {
1116 free(lf->lr->path);
1117 lf->lr->path = NULL;
1118 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001119 return lf->found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001120}
1121
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001122static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1123{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001124 struct dwarf_callback_param *param = data;
1125
1126 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001127 return DWARF_CB_ABORT; /* No need to find other instances */
1128}
1129
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001130/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001131static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001132{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001133 struct dwarf_callback_param *param = data;
1134 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001135 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001136
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001137 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1138 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001139 lf->fname = dwarf_decl_file(sp_die);
1140 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001141 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001142 lf->lno_s = lr->offset + lr->start;
1143 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001144 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001145 else
1146 lf->lno_e = lr->offset + lr->end;
1147 lr->start = lf->lno_s;
1148 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001149 if (dwarf_func_inline(sp_die)) {
1150 struct dwarf_callback_param _param;
1151 _param.data = (void *)lf;
1152 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001153 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001154 line_range_inline_cb,
1155 &_param);
1156 param->retval = _param.retval;
1157 } else
1158 param->retval = find_line_range_by_line(sp_die, lf);
1159 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001160 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001161 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001162}
1163
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001164static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001165{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001166 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1167 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1168 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001169}
1170
1171int find_line_range(int fd, struct line_range *lr)
1172{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001173 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001174 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001175 Dwarf_Off off = 0, noff;
1176 size_t cuhl;
1177 Dwarf_Die *diep;
1178 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001179
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001180 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001181 if (!dbg) {
1182 pr_warning("No dwarf info found in the vmlinux - "
1183 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1184 return -EBADF;
1185 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001186
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001187 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001188 while (!lf.found && ret >= 0) {
1189 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001190 break;
1191
1192 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001193 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1194 if (!diep)
1195 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001196
1197 /* Check if target file is included. */
1198 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001199 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001200 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001201 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001202
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001203 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001204 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001205 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001206 else {
1207 lf.lno_s = lr->start;
1208 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001209 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001210 else
1211 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001212 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001213 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001214 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001215 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001216 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001217 pr_debug("path: %lx\n", (unsigned long)lr->path);
1218 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001219
1220 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001221}
1222