blob: ce1ac827f3d2003cf78f14f22df55df51485105c [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 Hiramatsue334016f12010-04-12 13:17:49 -0400114static int line_list__add_line(struct list_head *head, unsigned 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 */
141static int line_list__has_line(struct list_head *head, unsigned int line)
142{
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 Hiramatsu016f2622010-03-16 18:05:58 -0400252/* Return values for die_find callbacks */
253enum {
254 DIE_FIND_CB_FOUND = 0, /* End of Search */
255 DIE_FIND_CB_CHILD = 1, /* Search only children */
256 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
257 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
258};
259
260/* Search a child die */
261static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
262 int (*callback)(Dwarf_Die *, void *),
263 void *data, Dwarf_Die *die_mem)
264{
265 Dwarf_Die child_die;
266 int ret;
267
268 ret = dwarf_child(rt_die, die_mem);
269 if (ret != 0)
270 return NULL;
271
272 do {
273 ret = callback(die_mem, data);
274 if (ret == DIE_FIND_CB_FOUND)
275 return die_mem;
276
277 if ((ret & DIE_FIND_CB_CHILD) &&
278 die_find_child(die_mem, callback, data, &child_die)) {
279 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
280 return die_mem;
281 }
282 } while ((ret & DIE_FIND_CB_SIBLING) &&
283 dwarf_siblingof(die_mem, die_mem) == 0);
284
285 return NULL;
286}
287
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500288struct __addr_die_search_param {
289 Dwarf_Addr addr;
290 Dwarf_Die *die_mem;
291};
292
293static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
294{
295 struct __addr_die_search_param *ad = data;
296
297 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
298 dwarf_haspc(fn_die, ad->addr)) {
299 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
300 return DWARF_CB_ABORT;
301 }
302 return DWARF_CB_OK;
303}
304
305/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400306static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
307 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500308{
309 struct __addr_die_search_param ad;
310 ad.addr = addr;
311 ad.die_mem = die_mem;
312 /* dwarf_getscopes can't find subprogram. */
313 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
314 return NULL;
315 else
316 return die_mem;
317}
318
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400319/* die_find callback for inline function search */
320static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
321{
322 Dwarf_Addr *addr = data;
323
324 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
325 dwarf_haspc(die_mem, *addr))
326 return DIE_FIND_CB_FOUND;
327
328 return DIE_FIND_CB_CONTINUE;
329}
330
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500331/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400332static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
333 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500334{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400335 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500336}
337
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400338static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400339{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400340 const char *name = data;
341 int tag;
342
343 tag = dwarf_tag(die_mem);
344 if ((tag == DW_TAG_formal_parameter ||
345 tag == DW_TAG_variable) &&
346 (die_compare_name(die_mem, name) == 0))
347 return DIE_FIND_CB_FOUND;
348
349 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400350}
351
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400352/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500353static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
354 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500355{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400356 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
357 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400358}
359
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400360static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
361{
362 const char *name = data;
363
364 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
365 (die_compare_name(die_mem, name) == 0))
366 return DIE_FIND_CB_FOUND;
367
368 return DIE_FIND_CB_SIBLING;
369}
370
371/* Find a member called 'name' */
372static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
373 Dwarf_Die *die_mem)
374{
375 return die_find_child(st_die, __die_find_member_cb, (void *)name,
376 die_mem);
377}
378
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400379/*
380 * Probe finder related functions
381 */
382
383/* Show a location */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400384static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400385{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500386 unsigned int regn;
387 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400388 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400389 const char *regs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400390 struct kprobe_trace_arg *tvar = pf->tvar;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400391
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400392 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500393 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400394 if (pf->fb_ops == NULL) {
395 pr_warning("The attribute of frame base is not "
396 "supported.\n");
397 return -ENOTSUP;
398 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400399 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500400 offs = op->number;
401 op = &pf->fb_ops[0];
402 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400403
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500404 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
405 regn = op->atom - DW_OP_breg0;
406 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400407 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500408 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
409 regn = op->atom - DW_OP_reg0;
410 } else if (op->atom == DW_OP_bregx) {
411 regn = op->number;
412 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400413 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500414 } else if (op->atom == DW_OP_regx) {
415 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400416 } else {
417 pr_warning("DW_OP %x is not supported.\n", op->atom);
418 return -ENOTSUP;
419 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400420
421 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400422 if (!regs) {
423 pr_warning("%u exceeds max register number.\n", regn);
424 return -ERANGE;
425 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400426
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400427 tvar->value = xstrdup(regs);
428 if (ref) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400429 tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
430 if (tvar->ref == NULL)
431 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400432 tvar->ref->offset = (long)offs;
433 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400434 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400435}
436
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400437static int convert_variable_type(Dwarf_Die *vr_die,
438 struct kprobe_trace_arg *targ)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400439{
440 Dwarf_Die type;
441 char buf[16];
442 int ret;
443
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400444 if (die_get_real_type(vr_die, &type) == NULL) {
445 pr_warning("Failed to get a type information of %s.\n",
446 dwarf_diename(vr_die));
447 return -ENOENT;
448 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400449
450 ret = die_get_byte_size(&type) * 8;
451 if (ret) {
452 /* Check the bitwidth */
453 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400454 pr_info("%s exceeds max-bitwidth."
455 " Cut down to %d bits.\n",
456 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400457 ret = MAX_BASIC_TYPE_BITS;
458 }
459
460 ret = snprintf(buf, 16, "%c%d",
461 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400462 if (ret < 0 || ret >= 16) {
463 if (ret >= 16)
464 ret = -E2BIG;
465 pr_warning("Failed to convert variable type: %s\n",
466 strerror(-ret));
467 return ret;
468 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400469 targ->type = xstrdup(buf);
470 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400471 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400472}
473
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400474static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400475 struct perf_probe_arg_field *field,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400476 struct kprobe_trace_arg_ref **ref_ptr,
477 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400478{
479 struct kprobe_trace_arg_ref *ref = *ref_ptr;
480 Dwarf_Attribute attr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400481 Dwarf_Die type;
482 Dwarf_Word offs;
483
484 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400485 if (die_get_real_type(vr_die, &type) == NULL) {
486 pr_warning("Failed to get the type of %s.\n", varname);
487 return -ENOENT;
488 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400489
490 /* Check the pointer and dereference */
491 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400492 if (!field->ref) {
493 pr_err("Semantic error: %s must be referred by '->'\n",
494 field->name);
495 return -EINVAL;
496 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400497 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400498 if (die_get_real_type(&type, &type) == NULL) {
499 pr_warning("Failed to get the type of %s.\n", varname);
500 return -ENOENT;
501 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400502 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400503 if (dwarf_tag(&type) != DW_TAG_structure_type) {
504 pr_warning("%s is not a data structure.\n", varname);
505 return -EINVAL;
506 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400507
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400508 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
509 if (ref == NULL)
510 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400511 if (*ref_ptr)
512 (*ref_ptr)->next = ref;
513 else
514 *ref_ptr = ref;
515 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400516 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400517 if (dwarf_tag(&type) != DW_TAG_structure_type) {
518 pr_warning("%s is not a data structure.\n", varname);
519 return -EINVAL;
520 }
521 if (field->ref) {
522 pr_err("Semantic error: %s must be referred by '.'\n",
523 field->name);
524 return -EINVAL;
525 }
526 if (!ref) {
527 pr_warning("Structure on a register is not "
528 "supported yet.\n");
529 return -ENOTSUP;
530 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400531 }
532
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400533 if (die_find_member(&type, field->name, die_mem) == NULL) {
534 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
535 dwarf_diename(&type), field->name);
536 return -EINVAL;
537 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400538
539 /* Get the offset of the field */
Masami Hiramatsu49849122010-04-12 13:17:15 -0400540 if (dwarf_attr(die_mem, DW_AT_data_member_location, &attr) == NULL ||
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400541 dwarf_formudata(&attr, &offs) != 0) {
542 pr_warning("Failed to get the offset of %s.\n", field->name);
543 return -ENOENT;
544 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400545 ref->offset += (long)offs;
546
547 /* Converting next field */
548 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400549 return convert_variable_fields(die_mem, field->name,
550 field->next, &ref, die_mem);
551 else
552 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400553}
554
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400555/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400556static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400557{
558 Dwarf_Attribute attr;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400559 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500560 Dwarf_Op *expr;
561 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400562 int ret;
563
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500564 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400565 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500566 /* TODO: handle more than 1 exprs */
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400567 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500568 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400569 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500570
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400571 ret = convert_location(expr, pf);
572 if (ret == 0 && pf->pvar->field) {
573 ret = convert_variable_fields(vr_die, pf->pvar->var,
574 pf->pvar->field, &pf->tvar->ref,
575 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400576 vr_die = &die_mem;
577 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400578 if (ret == 0) {
579 if (pf->pvar->type)
580 pf->tvar->type = xstrdup(pf->pvar->type);
581 else
582 ret = convert_variable_type(vr_die, pf->tvar);
583 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500584 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400585 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400586error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500587 /* TODO: Support const_value */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400588 pr_err("Failed to find the location of %s at this address.\n"
589 " Perhaps, it has been optimized out.\n", pf->pvar->var);
590 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400591}
592
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400593/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400594static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400595{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500596 Dwarf_Die vr_die;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400597 char buf[32], *ptr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400598
Masami Hiramatsu48481932010-04-12 13:16:53 -0400599 /* TODO: Support arrays */
600 if (pf->pvar->name)
601 pf->tvar->name = xstrdup(pf->pvar->name);
602 else {
603 synthesize_perf_probe_arg(pf->pvar, buf, 32);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400604 ptr = strchr(buf, ':'); /* Change type separator to _ */
605 if (ptr)
606 *ptr = '_';
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400607 pf->tvar->name = xstrdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400608 }
609
610 if (!is_c_varname(pf->pvar->var)) {
611 /* Copy raw parameters */
612 pf->tvar->value = xstrdup(pf->pvar->var);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400613 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400614 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400615
616 pr_debug("Searching '%s' variable in context.\n",
617 pf->pvar->var);
618 /* Search child die for local variables and parameters. */
619 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
620 pr_warning("Failed to find '%s' in this function.\n",
621 pf->pvar->var);
622 return -ENOENT;
623 }
624 return convert_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400625}
626
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400627/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400628static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400629{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400630 struct kprobe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500631 Dwarf_Addr eaddr;
632 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500633 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400634 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500635 Dwarf_Attribute fb_attr;
636 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400637
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400638 if (pf->ntevs == MAX_PROBES) {
639 pr_warning("Too many( > %d) probe point found.\n", MAX_PROBES);
640 return -ERANGE;
641 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400642 tev = &pf->tevs[pf->ntevs++];
643
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500644 /* If no real subprogram, find a real one */
645 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400646 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500647 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400648 if (!sp_die) {
649 pr_warning("Failed to find probe point in any "
650 "functions.\n");
651 return -ENOENT;
652 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500653 }
654
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400655 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500656 name = dwarf_diename(sp_die);
657 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400658 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
659 pr_warning("Failed to get entry pc of %s\n",
660 dwarf_diename(sp_die));
661 return -ENOENT;
662 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400663 tev->point.symbol = xstrdup(name);
664 tev->point.offset = (unsigned long)(pf->addr - eaddr);
665 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400666 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400667 tev->point.offset = (unsigned long)pf->addr;
668
669 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
670 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400671
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500672 /* Get the frame base attribute/ops */
673 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400674 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400675 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500676 pf->fb_ops = NULL;
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400677 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
678 pf->cfi != NULL) {
679 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400680 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
681 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
682 pr_warning("Failed to get CFA on 0x%jx\n",
683 (uintmax_t)pf->addr);
684 return -ENOENT;
685 }
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400686 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500687
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400688 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400689 tev->nargs = pf->pev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400690 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
691 if (tev->args == NULL)
692 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400693 for (i = 0; i < pf->pev->nargs; i++) {
694 pf->pvar = &pf->pev->args[i];
695 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400696 ret = find_variable(sp_die, pf);
697 if (ret != 0)
698 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400699 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500700
701 /* *pf->fb_ops will be cached in libdw. Don't free it. */
702 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400703 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400704}
705
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400706/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400707static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400708{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500709 Dwarf_Lines *lines;
710 Dwarf_Line *line;
711 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500712 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500713 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400714 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400715
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400716 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
717 pr_warning("No source lines found in this CU.\n");
718 return -ENOENT;
719 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400720
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400721 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500722 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400723 if (dwarf_lineno(line, &lineno) != 0 ||
724 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400725 continue;
726
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500727 /* TODO: Get fileno from line, but how? */
728 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
729 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400730
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400731 if (dwarf_lineaddr(line, &addr) != 0) {
732 pr_warning("Failed to get the address of the line.\n");
733 return -ENOENT;
734 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500735 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
736 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400737 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500738
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400739 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400740 /* Continuing, because target line might be inlined. */
741 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400742 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400743}
744
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500745/* Find lines which match lazy pattern */
746static int find_lazy_match_lines(struct list_head *head,
747 const char *fname, const char *pat)
748{
749 char *fbuf, *p1, *p2;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400750 int fd, ret, line, nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500751 struct stat st;
752
753 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400754 if (fd < 0) {
755 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
756 return fd;
757 }
758
759 ret = fstat(fd, &st);
760 if (ret < 0) {
761 pr_warning("Failed to get the size of %s: %s\n",
762 fname, strerror(errno));
763 return ret;
764 }
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400765 fbuf = xmalloc(st.st_size + 2);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400766 ret = read(fd, fbuf, st.st_size);
767 if (ret < 0) {
768 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
769 return ret;
770 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500771 close(fd);
772 fbuf[st.st_size] = '\n'; /* Dummy line */
773 fbuf[st.st_size + 1] = '\0';
774 p1 = fbuf;
775 line = 1;
776 while ((p2 = strchr(p1, '\n')) != NULL) {
777 *p2 = '\0';
778 if (strlazymatch(p1, pat)) {
779 line_list__add_line(head, line);
780 nlines++;
781 }
782 line++;
783 p1 = p2 + 1;
784 }
785 free(fbuf);
786 return nlines;
787}
788
789/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400790static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500791{
792 Dwarf_Lines *lines;
793 Dwarf_Line *line;
794 size_t nlines, i;
795 Dwarf_Addr addr;
796 Dwarf_Die die_mem;
797 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400798 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500799
800 if (list_empty(&pf->lcache)) {
801 /* Matching lazy line pattern */
802 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400803 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400804 if (ret == 0) {
805 pr_debug("No matched lines found in %s.\n", pf->fname);
806 return 0;
807 } else if (ret < 0)
808 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500809 }
810
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400811 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
812 pr_warning("No source lines found in this CU.\n");
813 return -ENOENT;
814 }
815
816 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500817 line = dwarf_onesrcline(lines, i);
818
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400819 if (dwarf_lineno(line, &lineno) != 0 ||
820 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500821 continue;
822
823 /* TODO: Get fileno from line, but how? */
824 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
825 continue;
826
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400827 if (dwarf_lineaddr(line, &addr) != 0) {
828 pr_debug("Failed to get the address of line %d.\n",
829 lineno);
830 continue;
831 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500832 if (sp_die) {
833 /* Address filtering 1: does sp_die include addr? */
834 if (!dwarf_haspc(sp_die, addr))
835 continue;
836 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400837 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500838 continue;
839 }
840
841 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
842 (int)i, lineno, (unsigned long long)addr);
843 pf->addr = addr;
844
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400845 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500846 /* Continuing, because target line might be inlined. */
847 }
848 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400849 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500850}
851
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400852/* Callback parameter with return value */
853struct dwarf_callback_param {
854 void *data;
855 int retval;
856};
857
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500858static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400859{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400860 struct dwarf_callback_param *param = data;
861 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400862 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400863 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400864
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500865 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400866 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500867 else {
868 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400869 if (dwarf_entrypc(in_die, &addr) != 0) {
870 pr_warning("Failed to get entry pc of %s.\n",
871 dwarf_diename(in_die));
872 param->retval = -ENOENT;
873 return DWARF_CB_ABORT;
874 }
875 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500876 pf->addr += pp->offset;
877 pr_debug("found inline addr: 0x%jx\n",
878 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500879
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400880 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500881 }
882
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500883 return DWARF_CB_OK;
884}
885
886/* Search function from function name */
887static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
888{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400889 struct dwarf_callback_param *param = data;
890 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400891 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500892
893 /* Check tag and diename */
894 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
895 die_compare_name(sp_die, pp->function) != 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400896 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500897
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500898 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500899 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500900 dwarf_decl_line(sp_die, &pf->lno);
901 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400902 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500903 } else if (!dwarf_func_inline(sp_die)) {
904 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500905 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400906 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500907 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400908 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
909 pr_warning("Failed to get entry pc of %s.\n",
910 dwarf_diename(sp_die));
911 param->retval = -ENOENT;
912 return DWARF_CB_ABORT;
913 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500914 pf->addr += pp->offset;
915 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400916 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500917 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400918 } else {
919 struct dwarf_callback_param _param = {.data = (void *)pf,
920 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500921 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400922 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
923 &_param);
924 param->retval = _param.retval;
925 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500926
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400927 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400928}
929
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400930static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400931{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400932 struct dwarf_callback_param _param = {.data = (void *)pf,
933 .retval = 0};
934 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
935 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400936}
937
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400938/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
939int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
940 struct kprobe_trace_event **tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400941{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400942 struct probe_finder pf = {.pev = pev};
943 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500944 Dwarf_Off off, noff;
945 size_t cuhl;
946 Dwarf_Die *diep;
947 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400948 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400949
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400950 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
951 if (pf.tevs == NULL)
952 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400953 *tevs = pf.tevs;
954 pf.ntevs = 0;
955
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500956 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400957 if (!dbg) {
958 pr_warning("No dwarf info found in the vmlinux - "
959 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
960 return -EBADF;
961 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400962
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400963 /* Get the call frame information from this dwarf */
964 pf.cfi = dwarf_getcfi(dbg);
965
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500966 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500967 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500968 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400969 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
970 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400971 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500972 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
973 if (!diep)
974 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400975
976 /* Check if target file is included. */
977 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500978 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500979 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500980 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400981
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500982 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400983 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400984 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500985 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400986 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400987 else {
988 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400989 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400990 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400991 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500992 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400993 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500994 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500995 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400996
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400997 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400998}
999
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001000/* Reverse search */
1001int find_perf_probe_point(int fd, unsigned long addr,
1002 struct perf_probe_point *ppt)
1003{
1004 Dwarf_Die cudie, spdie, indie;
1005 Dwarf *dbg;
1006 Dwarf_Line *line;
1007 Dwarf_Addr laddr, eaddr;
1008 const char *tmp;
1009 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001010 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001011
1012 dbg = dwarf_begin(fd, DWARF_C_READ);
1013 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001014 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001015
1016 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001017 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1018 ret = -EINVAL;
1019 goto end;
1020 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001021
1022 /* Find a corresponding line */
1023 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1024 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001025 if (dwarf_lineaddr(line, &laddr) == 0 &&
1026 (Dwarf_Addr)addr == laddr &&
1027 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001028 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001029 if (tmp) {
1030 ppt->line = lineno;
1031 ppt->file = xstrdup(tmp);
1032 found = true;
1033 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001034 }
1035 }
1036
1037 /* Find a corresponding function */
1038 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1039 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001040 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001041 goto end;
1042
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001043 if (ppt->line) {
1044 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1045 &indie)) {
1046 /* addr in an inline function */
1047 tmp = dwarf_diename(&indie);
1048 if (!tmp)
1049 goto end;
1050 ret = dwarf_decl_line(&indie, &lineno);
1051 } else {
1052 if (eaddr == addr) { /* Function entry */
1053 lineno = ppt->line;
1054 ret = 0;
1055 } else
1056 ret = dwarf_decl_line(&spdie, &lineno);
1057 }
1058 if (ret == 0) {
1059 /* Make a relative line number */
1060 ppt->line -= lineno;
1061 goto found;
1062 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001063 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001064 /* We don't have a line number, let's use offset */
1065 ppt->offset = addr - (unsigned long)eaddr;
1066found:
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001067 ppt->function = xstrdup(tmp);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001068 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001069 }
1070
1071end:
1072 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001073 if (ret >= 0)
1074 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001075 return ret;
1076}
1077
1078
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001079/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001080static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001081{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001082 Dwarf_Lines *lines;
1083 Dwarf_Line *line;
1084 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001085 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001086 int lineno;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001087 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001088 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001089
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001090 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001091 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1092 pr_warning("No source lines found in this CU.\n");
1093 return -ENOENT;
1094 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001095
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001096 for (i = 0; i < nlines; i++) {
1097 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001098 if (dwarf_lineno(line, &lineno) != 0 ||
1099 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001100 continue;
1101
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001102 if (sp_die) {
1103 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001104 if (dwarf_lineaddr(line, &addr) != 0 ||
1105 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001106 continue;
1107
1108 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001109 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001110 continue;
1111 }
1112
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001113 /* TODO: Get fileno from line, but how? */
1114 src = dwarf_linesrc(line, NULL, NULL);
1115 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001116 continue;
1117
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001118 /* Copy real path */
1119 if (!lf->lr->path)
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001120 lf->lr->path = xstrdup(src);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001121 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001122 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001123 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001124 if (!list_empty(&lf->lr->line_list))
1125 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001126 else {
1127 free(lf->lr->path);
1128 lf->lr->path = NULL;
1129 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001130 return lf->found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001131}
1132
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001133static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1134{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001135 struct dwarf_callback_param *param = data;
1136
1137 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001138 return DWARF_CB_ABORT; /* No need to find other instances */
1139}
1140
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001141/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001142static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001143{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001144 struct dwarf_callback_param *param = data;
1145 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001146 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001147
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001148 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1149 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001150 lf->fname = dwarf_decl_file(sp_die);
1151 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001152 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001153 lf->lno_s = lr->offset + lr->start;
1154 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001155 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001156 else
1157 lf->lno_e = lr->offset + lr->end;
1158 lr->start = lf->lno_s;
1159 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001160 if (dwarf_func_inline(sp_die)) {
1161 struct dwarf_callback_param _param;
1162 _param.data = (void *)lf;
1163 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001164 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001165 line_range_inline_cb,
1166 &_param);
1167 param->retval = _param.retval;
1168 } else
1169 param->retval = find_line_range_by_line(sp_die, lf);
1170 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001171 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001172 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001173}
1174
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001175static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001176{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001177 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1178 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1179 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001180}
1181
1182int find_line_range(int fd, struct line_range *lr)
1183{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001184 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001185 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001186 Dwarf_Off off = 0, noff;
1187 size_t cuhl;
1188 Dwarf_Die *diep;
1189 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001190
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001191 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001192 if (!dbg) {
1193 pr_warning("No dwarf info found in the vmlinux - "
1194 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1195 return -EBADF;
1196 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001197
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001198 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001199 while (!lf.found && ret >= 0) {
1200 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001201 break;
1202
1203 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001204 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1205 if (!diep)
1206 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001207
1208 /* Check if target file is included. */
1209 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001210 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001211 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001212 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001213
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001214 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001215 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001216 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001217 else {
1218 lf.lno_s = lr->start;
1219 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001220 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001221 else
1222 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001223 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001224 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001225 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001226 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001227 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001228 pr_debug("path: %lx\n", (unsigned long)lr->path);
1229 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001230
1231 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001232}
1233