blob: ab476736cbe77b15b79a4c7725c300d3f3e5169f [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);
199 DIE_IF(name == NULL);
200 return strcmp(tname, name);
201}
202
203/* Get entry pc(or low pc, 1st entry of ranges) of the die */
204static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
205{
206 Dwarf_Addr epc;
207 int ret;
208
209 ret = dwarf_entrypc(dw_die, &epc);
210 DIE_IF(ret == -1);
211 return epc;
212}
213
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400214/* Get type die, but skip qualifiers and typedef */
215static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
216{
217 Dwarf_Attribute attr;
218 int tag;
219
220 do {
221 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
222 dwarf_formref_die(&attr, die_mem) == NULL)
223 return NULL;
224
225 tag = dwarf_tag(die_mem);
226 vr_die = die_mem;
227 } while (tag == DW_TAG_const_type ||
228 tag == DW_TAG_restrict_type ||
229 tag == DW_TAG_volatile_type ||
230 tag == DW_TAG_shared_type ||
231 tag == DW_TAG_typedef);
232
233 return die_mem;
234}
235
Masami Hiramatsu49849122010-04-12 13:17:15 -0400236static bool die_is_signed_type(Dwarf_Die *tp_die)
237{
238 Dwarf_Attribute attr;
239 Dwarf_Word ret;
240
241 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
242 dwarf_formudata(&attr, &ret) != 0)
243 return false;
244
245 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
246 ret == DW_ATE_signed_fixed);
247}
248
249static int die_get_byte_size(Dwarf_Die *tp_die)
250{
251 Dwarf_Attribute attr;
252 Dwarf_Word ret;
253
254 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
255 dwarf_formudata(&attr, &ret) != 0)
256 return 0;
257
258 return (int)ret;
259}
260
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400261/* Return values for die_find callbacks */
262enum {
263 DIE_FIND_CB_FOUND = 0, /* End of Search */
264 DIE_FIND_CB_CHILD = 1, /* Search only children */
265 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
266 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
267};
268
269/* Search a child die */
270static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
271 int (*callback)(Dwarf_Die *, void *),
272 void *data, Dwarf_Die *die_mem)
273{
274 Dwarf_Die child_die;
275 int ret;
276
277 ret = dwarf_child(rt_die, die_mem);
278 if (ret != 0)
279 return NULL;
280
281 do {
282 ret = callback(die_mem, data);
283 if (ret == DIE_FIND_CB_FOUND)
284 return die_mem;
285
286 if ((ret & DIE_FIND_CB_CHILD) &&
287 die_find_child(die_mem, callback, data, &child_die)) {
288 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
289 return die_mem;
290 }
291 } while ((ret & DIE_FIND_CB_SIBLING) &&
292 dwarf_siblingof(die_mem, die_mem) == 0);
293
294 return NULL;
295}
296
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500297struct __addr_die_search_param {
298 Dwarf_Addr addr;
299 Dwarf_Die *die_mem;
300};
301
302static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
303{
304 struct __addr_die_search_param *ad = data;
305
306 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
307 dwarf_haspc(fn_die, ad->addr)) {
308 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
309 return DWARF_CB_ABORT;
310 }
311 return DWARF_CB_OK;
312}
313
314/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400315static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
316 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500317{
318 struct __addr_die_search_param ad;
319 ad.addr = addr;
320 ad.die_mem = die_mem;
321 /* dwarf_getscopes can't find subprogram. */
322 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
323 return NULL;
324 else
325 return die_mem;
326}
327
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400328/* die_find callback for inline function search */
329static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
330{
331 Dwarf_Addr *addr = data;
332
333 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
334 dwarf_haspc(die_mem, *addr))
335 return DIE_FIND_CB_FOUND;
336
337 return DIE_FIND_CB_CONTINUE;
338}
339
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500340/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400341static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
342 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500343{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400344 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500345}
346
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400347static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400348{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400349 const char *name = data;
350 int tag;
351
352 tag = dwarf_tag(die_mem);
353 if ((tag == DW_TAG_formal_parameter ||
354 tag == DW_TAG_variable) &&
355 (die_compare_name(die_mem, name) == 0))
356 return DIE_FIND_CB_FOUND;
357
358 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400359}
360
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400361/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500362static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
363 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500364{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400365 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
366 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400367}
368
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400369static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
370{
371 const char *name = data;
372
373 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
374 (die_compare_name(die_mem, name) == 0))
375 return DIE_FIND_CB_FOUND;
376
377 return DIE_FIND_CB_SIBLING;
378}
379
380/* Find a member called 'name' */
381static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
382 Dwarf_Die *die_mem)
383{
384 return die_find_child(st_die, __die_find_member_cb, (void *)name,
385 die_mem);
386}
387
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400388/*
389 * Probe finder related functions
390 */
391
392/* Show a location */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400393static void convert_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400394{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500395 unsigned int regn;
396 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400397 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400398 const char *regs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400399 struct kprobe_trace_arg *tvar = pf->tvar;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400400
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500401 /* TODO: support CFA */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400402 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500403 if (op->atom == DW_OP_fbreg) {
404 if (pf->fb_ops == NULL)
405 die("The attribute of frame base is not supported.\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400406 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500407 offs = op->number;
408 op = &pf->fb_ops[0];
409 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400410
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500411 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
412 regn = op->atom - DW_OP_breg0;
413 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400414 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500415 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
416 regn = op->atom - DW_OP_reg0;
417 } else if (op->atom == DW_OP_bregx) {
418 regn = op->number;
419 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400420 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500421 } else if (op->atom == DW_OP_regx) {
422 regn = op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400423 } else
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500424 die("DW_OP %d is not supported.", op->atom);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400425
426 regs = get_arch_regstr(regn);
427 if (!regs)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500428 die("%u exceeds max register number.", regn);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400429
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400430 tvar->value = xstrdup(regs);
431 if (ref) {
432 tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
433 tvar->ref->offset = (long)offs;
434 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400435}
436
Masami Hiramatsu49849122010-04-12 13:17:15 -0400437static void convert_variable_type(Dwarf_Die *vr_die,
438 struct kprobe_trace_arg *targ)
439{
440 Dwarf_Die type;
441 char buf[16];
442 int ret;
443
444 if (die_get_real_type(vr_die, &type) == NULL)
445 die("Failed to get a type information of %s.",
446 dwarf_diename(vr_die));
447
448 ret = die_get_byte_size(&type) * 8;
449 if (ret) {
450 /* Check the bitwidth */
451 if (ret > MAX_BASIC_TYPE_BITS) {
452 pr_warning(" Warning: %s exceeds max-bitwidth."
453 " Cut down to %d bits.\n",
454 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
455 ret = MAX_BASIC_TYPE_BITS;
456 }
457
458 ret = snprintf(buf, 16, "%c%d",
459 die_is_signed_type(&type) ? 's' : 'u', ret);
460 if (ret < 0 || ret >= 16)
461 die("Failed to convert variable type.");
462 targ->type = xstrdup(buf);
463 }
464}
465
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400466static void convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
467 struct perf_probe_arg_field *field,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400468 struct kprobe_trace_arg_ref **ref_ptr,
469 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400470{
471 struct kprobe_trace_arg_ref *ref = *ref_ptr;
472 Dwarf_Attribute attr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400473 Dwarf_Die type;
474 Dwarf_Word offs;
475
476 pr_debug("converting %s in %s\n", field->name, varname);
477 if (die_get_real_type(vr_die, &type) == NULL)
478 die("Failed to get a type information of %s.", varname);
479
480 /* Check the pointer and dereference */
481 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
482 if (!field->ref)
483 die("Semantic error: %s must be referred by '->'",
484 field->name);
485 /* Get the type pointed by this pointer */
486 if (die_get_real_type(&type, &type) == NULL)
487 die("Failed to get a type information of %s.", varname);
488
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400489 /* Verify it is a data structure */
490 if (dwarf_tag(&type) != DW_TAG_structure_type)
491 die("%s is not a data structure.", varname);
492
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400493 ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
494 if (*ref_ptr)
495 (*ref_ptr)->next = ref;
496 else
497 *ref_ptr = ref;
498 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400499 /* Verify it is a data structure */
500 if (dwarf_tag(&type) != DW_TAG_structure_type)
501 die("%s is not a data structure.", varname);
502
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400503 if (field->ref)
504 die("Semantic error: %s must be referred by '.'",
505 field->name);
506 if (!ref)
507 die("Structure on a register is not supported yet.");
508 }
509
Masami Hiramatsu49849122010-04-12 13:17:15 -0400510 if (die_find_member(&type, field->name, die_mem) == NULL)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400511 die("%s(tyep:%s) has no member %s.", varname,
512 dwarf_diename(&type), field->name);
513
514 /* Get the offset of the field */
Masami Hiramatsu49849122010-04-12 13:17:15 -0400515 if (dwarf_attr(die_mem, DW_AT_data_member_location, &attr) == NULL ||
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400516 dwarf_formudata(&attr, &offs) != 0)
517 die("Failed to get the offset of %s.", field->name);
518 ref->offset += (long)offs;
519
520 /* Converting next field */
521 if (field->next)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400522 convert_variable_fields(die_mem, field->name, field->next,
523 &ref, die_mem);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400524}
525
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400526/* Show a variables in kprobe event format */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400527static void convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400528{
529 Dwarf_Attribute attr;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400530 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500531 Dwarf_Op *expr;
532 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400533 int ret;
534
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500535 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400536 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500537 /* TODO: handle more than 1 exprs */
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400538 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500539 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400540 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500541
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400542 convert_location(expr, pf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400543
Masami Hiramatsu49849122010-04-12 13:17:15 -0400544 if (pf->pvar->field) {
Masami Hiramatsu48481932010-04-12 13:16:53 -0400545 convert_variable_fields(vr_die, pf->pvar->var,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400546 pf->pvar->field, &pf->tvar->ref,
547 &die_mem);
548 vr_die = &die_mem;
549 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400550 if (pf->pvar->type)
551 pf->tvar->type = xstrdup(pf->pvar->type);
552 else
553 convert_variable_type(vr_die, pf->tvar);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500554 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400555 return ;
556error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500557 /* TODO: Support const_value */
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400558 die("Failed to find the location of %s at this address.\n"
Masami Hiramatsu48481932010-04-12 13:16:53 -0400559 " Perhaps, it has been optimized out.", pf->pvar->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400560}
561
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400562/* Find a variable in a subprogram die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500563static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400564{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500565 Dwarf_Die vr_die;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400566 char buf[32], *ptr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400567
Masami Hiramatsu48481932010-04-12 13:16:53 -0400568 /* TODO: Support arrays */
569 if (pf->pvar->name)
570 pf->tvar->name = xstrdup(pf->pvar->name);
571 else {
572 synthesize_perf_probe_arg(pf->pvar, buf, 32);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400573 ptr = strchr(buf, ':'); /* Change type separator to _ */
574 if (ptr)
575 *ptr = '_';
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400576 pf->tvar->name = xstrdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400577 }
578
579 if (!is_c_varname(pf->pvar->var)) {
580 /* Copy raw parameters */
581 pf->tvar->value = xstrdup(pf->pvar->var);
582 } else {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400583 pr_debug("Searching '%s' variable in context.\n",
Masami Hiramatsu48481932010-04-12 13:16:53 -0400584 pf->pvar->var);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400585 /* Search child die for local variables and parameters. */
Masami Hiramatsu48481932010-04-12 13:16:53 -0400586 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400587 die("Failed to find '%s' in this function.",
Masami Hiramatsu48481932010-04-12 13:16:53 -0400588 pf->pvar->var);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400589 convert_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400590 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400591}
592
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400593/* Show a probe point to output buffer */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400594static void convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400595{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400596 struct kprobe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500597 Dwarf_Addr eaddr;
598 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500599 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400600 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500601 Dwarf_Attribute fb_attr;
602 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400603
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400604 if (pf->ntevs == MAX_PROBES)
605 die("Too many( > %d) probe point found.\n", MAX_PROBES);
606 tev = &pf->tevs[pf->ntevs++];
607
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500608 /* If no real subprogram, find a real one */
609 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400610 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500611 pf->addr, &die_mem);
612 if (!sp_die)
613 die("Probe point is not found in subprograms.");
614 }
615
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400616 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500617 name = dwarf_diename(sp_die);
618 if (name) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500619 dwarf_entrypc(sp_die, &eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400620 tev->point.symbol = xstrdup(name);
621 tev->point.offset = (unsigned long)(pf->addr - eaddr);
622 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400623 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400624 tev->point.offset = (unsigned long)pf->addr;
625
626 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
627 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400628
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500629 /* Get the frame base attribute/ops */
630 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400631 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500632 if (ret <= 0 || nops == 0)
633 pf->fb_ops = NULL;
634
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400635 /* Find each argument */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500636 /* TODO: use dwarf_cfi_addrframe */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400637 tev->nargs = pf->pev->nargs;
638 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
639 for (i = 0; i < pf->pev->nargs; i++) {
640 pf->pvar = &pf->pev->args[i];
641 pf->tvar = &tev->args[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400642 find_variable(sp_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400643 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500644
645 /* *pf->fb_ops will be cached in libdw. Don't free it. */
646 pf->fb_ops = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400647}
648
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400649/* Find probe point from its line number */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500650static void find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400651{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500652 Dwarf_Lines *lines;
653 Dwarf_Line *line;
654 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500655 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500656 int lineno;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400657 int ret;
658
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500659 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
660 DIE_IF(ret != 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400661
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500662 for (i = 0; i < nlines; i++) {
663 line = dwarf_onesrcline(lines, i);
664 dwarf_lineno(line, &lineno);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400665 if (lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400666 continue;
667
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500668 /* TODO: Get fileno from line, but how? */
669 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
670 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400671
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500672 ret = dwarf_lineaddr(line, &addr);
673 DIE_IF(ret != 0);
674 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
675 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400676 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500677
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400678 convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400679 /* Continuing, because target line might be inlined. */
680 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400681}
682
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500683/* Find lines which match lazy pattern */
684static int find_lazy_match_lines(struct list_head *head,
685 const char *fname, const char *pat)
686{
687 char *fbuf, *p1, *p2;
688 int fd, line, nlines = 0;
689 struct stat st;
690
691 fd = open(fname, O_RDONLY);
692 if (fd < 0)
693 die("failed to open %s", fname);
694 DIE_IF(fstat(fd, &st) < 0);
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400695 fbuf = xmalloc(st.st_size + 2);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500696 DIE_IF(read(fd, fbuf, st.st_size) < 0);
697 close(fd);
698 fbuf[st.st_size] = '\n'; /* Dummy line */
699 fbuf[st.st_size + 1] = '\0';
700 p1 = fbuf;
701 line = 1;
702 while ((p2 = strchr(p1, '\n')) != NULL) {
703 *p2 = '\0';
704 if (strlazymatch(p1, pat)) {
705 line_list__add_line(head, line);
706 nlines++;
707 }
708 line++;
709 p1 = p2 + 1;
710 }
711 free(fbuf);
712 return nlines;
713}
714
715/* Find probe points from lazy pattern */
716static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
717{
718 Dwarf_Lines *lines;
719 Dwarf_Line *line;
720 size_t nlines, i;
721 Dwarf_Addr addr;
722 Dwarf_Die die_mem;
723 int lineno;
724 int ret;
725
726 if (list_empty(&pf->lcache)) {
727 /* Matching lazy line pattern */
728 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400729 pf->pev->point.lazy_line);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500730 if (ret <= 0)
731 die("No matched lines found in %s.", pf->fname);
732 }
733
734 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
735 DIE_IF(ret != 0);
736 for (i = 0; i < nlines; i++) {
737 line = dwarf_onesrcline(lines, i);
738
739 dwarf_lineno(line, &lineno);
740 if (!line_list__has_line(&pf->lcache, lineno))
741 continue;
742
743 /* TODO: Get fileno from line, but how? */
744 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
745 continue;
746
747 ret = dwarf_lineaddr(line, &addr);
748 DIE_IF(ret != 0);
749 if (sp_die) {
750 /* Address filtering 1: does sp_die include addr? */
751 if (!dwarf_haspc(sp_die, addr))
752 continue;
753 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400754 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500755 continue;
756 }
757
758 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
759 (int)i, lineno, (unsigned long long)addr);
760 pf->addr = addr;
761
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400762 convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500763 /* Continuing, because target line might be inlined. */
764 }
765 /* TODO: deallocate lines, but how? */
766}
767
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500768static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400769{
770 struct probe_finder *pf = (struct probe_finder *)data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400771 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400772
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500773 if (pp->lazy_line)
774 find_probe_point_lazy(in_die, pf);
775 else {
776 /* Get probe address */
777 pf->addr = die_get_entrypc(in_die);
778 pf->addr += pp->offset;
779 pr_debug("found inline addr: 0x%jx\n",
780 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500781
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400782 convert_probe_point(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500783 }
784
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500785 return DWARF_CB_OK;
786}
787
788/* Search function from function name */
789static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
790{
791 struct probe_finder *pf = (struct probe_finder *)data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400792 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500793
794 /* Check tag and diename */
795 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
796 die_compare_name(sp_die, pp->function) != 0)
797 return 0;
798
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500799 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500800 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500801 dwarf_decl_line(sp_die, &pf->lno);
802 pf->lno += pp->line;
803 find_probe_point_by_line(pf);
804 } else if (!dwarf_func_inline(sp_die)) {
805 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500806 if (pp->lazy_line)
807 find_probe_point_lazy(sp_die, pf);
808 else {
809 pf->addr = die_get_entrypc(sp_die);
810 pf->addr += pp->offset;
811 /* TODO: Check the address in this function */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400812 convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500813 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500814 } else
815 /* Inlined function: search instances */
816 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
817
818 return 1; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400819}
820
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500821static void find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400822{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500823 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400824}
825
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400826/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
827int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
828 struct kprobe_trace_event **tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400829{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400830 struct probe_finder pf = {.pev = pev};
831 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500832 Dwarf_Off off, noff;
833 size_t cuhl;
834 Dwarf_Die *diep;
835 Dwarf *dbg;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400836
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400837 pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
838 *tevs = pf.tevs;
839 pf.ntevs = 0;
840
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500841 dbg = dwarf_begin(fd, DWARF_C_READ);
842 if (!dbg)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500843 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400844
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500845 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500846 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500847 /* Loop on CUs (Compilation Unit) */
848 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400849 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500850 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
851 if (!diep)
852 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400853
854 /* Check if target file is included. */
855 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500856 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500857 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500858 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400859
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500860 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400861 if (pp->function)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500862 find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500863 else if (pp->lazy_line)
864 find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400865 else {
866 pf.lno = pp->line;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500867 find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400868 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400869 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500870 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400871 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500872 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500873 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400874
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400875 return pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400876}
877
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400878/* Reverse search */
879int find_perf_probe_point(int fd, unsigned long addr,
880 struct perf_probe_point *ppt)
881{
882 Dwarf_Die cudie, spdie, indie;
883 Dwarf *dbg;
884 Dwarf_Line *line;
885 Dwarf_Addr laddr, eaddr;
886 const char *tmp;
887 int lineno, ret = 0;
888
889 dbg = dwarf_begin(fd, DWARF_C_READ);
890 if (!dbg)
891 return -ENOENT;
892
893 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -0400894 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
895 ret = -EINVAL;
896 goto end;
897 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400898
899 /* Find a corresponding line */
900 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
901 if (line) {
902 dwarf_lineaddr(line, &laddr);
903 if ((Dwarf_Addr)addr == laddr) {
904 dwarf_lineno(line, &lineno);
905 ppt->line = lineno;
906
907 tmp = dwarf_linesrc(line, NULL, NULL);
908 DIE_IF(!tmp);
909 ppt->file = xstrdup(tmp);
910 ret = 1;
911 }
912 }
913
914 /* Find a corresponding function */
915 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
916 tmp = dwarf_diename(&spdie);
917 if (!tmp)
918 goto end;
919
920 dwarf_entrypc(&spdie, &eaddr);
921 if (!lineno) {
922 /* We don't have a line number, let's use offset */
923 ppt->function = xstrdup(tmp);
924 ppt->offset = addr - (unsigned long)eaddr;
925 ret = 1;
926 goto end;
927 }
928 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) {
929 /* addr in an inline function */
930 tmp = dwarf_diename(&indie);
931 if (!tmp)
932 goto end;
933 dwarf_decl_line(&indie, &lineno);
934 } else {
935 if (eaddr == addr) /* No offset: function entry */
936 lineno = ppt->line;
937 else
938 dwarf_decl_line(&spdie, &lineno);
939 }
940 ppt->function = xstrdup(tmp);
941 ppt->line -= lineno; /* Make a relative line number */
942 }
943
944end:
945 dwarf_end(dbg);
946 return ret;
947}
948
949
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500950/* Find line range from its line number */
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500951static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500952{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500953 Dwarf_Lines *lines;
954 Dwarf_Line *line;
955 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500956 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500957 int lineno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500958 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500959 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500960 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500961
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500962 line_list__init(&lf->lr->line_list);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500963 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
964 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500965
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500966 for (i = 0; i < nlines; i++) {
967 line = dwarf_onesrcline(lines, i);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500968 ret = dwarf_lineno(line, &lineno);
969 DIE_IF(ret != 0);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500970 if (lf->lno_s > lineno || lf->lno_e < lineno)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500971 continue;
972
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500973 if (sp_die) {
974 /* Address filtering 1: does sp_die include addr? */
975 ret = dwarf_lineaddr(line, &addr);
976 DIE_IF(ret != 0);
977 if (!dwarf_haspc(sp_die, addr))
978 continue;
979
980 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400981 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500982 continue;
983 }
984
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500985 /* TODO: Get fileno from line, but how? */
986 src = dwarf_linesrc(line, NULL, NULL);
987 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500988 continue;
989
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500990 /* Copy real path */
991 if (!lf->lr->path)
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400992 lf->lr->path = xstrdup(src);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500993 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500994 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500995 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500996 if (!list_empty(&lf->lr->line_list))
997 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500998 else {
999 free(lf->lr->path);
1000 lf->lr->path = NULL;
1001 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001002}
1003
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001004static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1005{
1006 find_line_range_by_line(in_die, (struct line_finder *)data);
1007 return DWARF_CB_ABORT; /* No need to find other instances */
1008}
1009
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001010/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001011static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001012{
1013 struct line_finder *lf = (struct line_finder *)data;
1014 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001015
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001016 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1017 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001018 lf->fname = dwarf_decl_file(sp_die);
1019 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001020 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001021 lf->lno_s = lr->offset + lr->start;
1022 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001023 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001024 else
1025 lf->lno_e = lr->offset + lr->end;
1026 lr->start = lf->lno_s;
1027 lr->end = lf->lno_e;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001028 if (dwarf_func_inline(sp_die))
1029 dwarf_func_inline_instances(sp_die,
1030 line_range_inline_cb, lf);
1031 else
1032 find_line_range_by_line(sp_die, lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001033 return 1;
1034 }
1035 return 0;
1036}
1037
1038static void find_line_range_by_func(struct line_finder *lf)
1039{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001040 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001041}
1042
1043int find_line_range(int fd, struct line_range *lr)
1044{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001045 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001046 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001047 Dwarf_Off off = 0, noff;
1048 size_t cuhl;
1049 Dwarf_Die *diep;
1050 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001051
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001052 dbg = dwarf_begin(fd, DWARF_C_READ);
1053 if (!dbg)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001054 return -ENOENT;
1055
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001056 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001057 while (!lf.found) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001058 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
1059 if (ret != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001060 break;
1061
1062 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001063 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1064 if (!diep)
1065 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001066
1067 /* Check if target file is included. */
1068 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001069 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001070 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001071 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001072
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001073 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001074 if (lr->function)
1075 find_line_range_by_func(&lf);
1076 else {
1077 lf.lno_s = lr->start;
1078 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001079 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001080 else
1081 lf.lno_e = lr->end;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001082 find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001083 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001084 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001085 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001086 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001087 pr_debug("path: %lx\n", (unsigned long)lr->path);
1088 dwarf_end(dbg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001089 return lf.found;
1090}
1091