blob: d964cb199c672f96712c4125ba036b7bfe038687 [file] [log] [blame]
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001/*
2 * probe-finder.c : C expression to kprobe event converter
3 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <getopt.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdarg.h>
33#include <ctype.h>
Ian Munsiecd932c52010-04-20 16:58:32 +100034#include <dwarf-regs.h>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040035
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050036#include "string.h"
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040037#include "event.h"
38#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040039#include "util.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040040#include "probe-finder.h"
41
Masami Hiramatsu49849122010-04-12 13:17:15 -040042/* Kprobe tracer basic type is up to u64 */
43#define MAX_BASIC_TYPE_BITS 64
44
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040045/*
46 * Compare the tail of two strings.
47 * Return 0 if whole of either string is same as another's tail part.
48 */
49static int strtailcmp(const char *s1, const char *s2)
50{
51 int i1 = strlen(s1);
52 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -050053 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040054 if (s1[i1] != s2[i2])
55 return s1[i1] - s2[i2];
56 }
57 return 0;
58}
59
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050060/* Line number list operations */
61
62/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040063static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050064{
65 struct line_node *ln;
66 struct list_head *p;
67
68 /* Reverse search, because new line will be the last one */
69 list_for_each_entry_reverse(ln, head, list) {
70 if (ln->line < line) {
71 p = &ln->list;
72 goto found;
73 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040074 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050075 }
76 /* List is empty, or the smallest entry */
77 p = head;
78found:
79 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040080 ln = zalloc(sizeof(struct line_node));
81 if (ln == NULL)
82 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050083 ln->line = line;
84 INIT_LIST_HEAD(&ln->list);
85 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040086 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050087}
88
89/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040090static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050091{
92 struct line_node *ln;
93
94 /* Reverse search, because new line will be the last one */
95 list_for_each_entry(ln, head, list)
96 if (ln->line == line)
97 return 1;
98
99 return 0;
100}
101
102/* Init line number list */
103static void line_list__init(struct list_head *head)
104{
105 INIT_LIST_HEAD(head);
106}
107
108/* Free line number list */
109static void line_list__free(struct list_head *head)
110{
111 struct line_node *ln;
112 while (!list_empty(head)) {
113 ln = list_first_entry(head, struct line_node, list);
114 list_del(&ln->list);
115 free(ln);
116 }
117}
118
119/* Dwarf wrappers */
120
121/* Find the realpath of the target file. */
122static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400123{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500124 Dwarf_Files *files;
125 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300126 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400127 int ret;
128
129 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500130 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500131
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500132 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500133 if (ret != 0)
134 return NULL;
135
136 for (i = 0; i < nfiles; i++) {
137 src = dwarf_filesrc(files, i, NULL, NULL);
138 if (strtailcmp(src, fname) == 0)
139 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500140 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400141 if (i == nfiles)
142 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500143 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500144}
145
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400146/* Compare diename and tname */
147static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
148{
149 const char *name;
150 name = dwarf_diename(dw_die);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400151 return name ? strcmp(tname, name) : -1;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400152}
153
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400154/* Get type die, but skip qualifiers and typedef */
155static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
156{
157 Dwarf_Attribute attr;
158 int tag;
159
160 do {
161 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
162 dwarf_formref_die(&attr, die_mem) == NULL)
163 return NULL;
164
165 tag = dwarf_tag(die_mem);
166 vr_die = die_mem;
167 } while (tag == DW_TAG_const_type ||
168 tag == DW_TAG_restrict_type ||
169 tag == DW_TAG_volatile_type ||
170 tag == DW_TAG_shared_type ||
171 tag == DW_TAG_typedef);
172
173 return die_mem;
174}
175
Masami Hiramatsu49849122010-04-12 13:17:15 -0400176static bool die_is_signed_type(Dwarf_Die *tp_die)
177{
178 Dwarf_Attribute attr;
179 Dwarf_Word ret;
180
181 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
182 dwarf_formudata(&attr, &ret) != 0)
183 return false;
184
185 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
186 ret == DW_ATE_signed_fixed);
187}
188
189static int die_get_byte_size(Dwarf_Die *tp_die)
190{
191 Dwarf_Attribute attr;
192 Dwarf_Word ret;
193
194 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
195 dwarf_formudata(&attr, &ret) != 0)
196 return 0;
197
198 return (int)ret;
199}
200
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300201/* Get data_member_location offset */
202static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
203{
204 Dwarf_Attribute attr;
205 Dwarf_Op *expr;
206 size_t nexpr;
207 int ret;
208
209 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
210 return -ENOENT;
211
212 if (dwarf_formudata(&attr, offs) != 0) {
213 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
214 ret = dwarf_getlocation(&attr, &expr, &nexpr);
215 if (ret < 0 || nexpr == 0)
216 return -ENOENT;
217
218 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
219 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
220 expr[0].atom, nexpr);
221 return -ENOTSUP;
222 }
223 *offs = (Dwarf_Word)expr[0].number;
224 }
225 return 0;
226}
227
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400228/* Return values for die_find callbacks */
229enum {
230 DIE_FIND_CB_FOUND = 0, /* End of Search */
231 DIE_FIND_CB_CHILD = 1, /* Search only children */
232 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
233 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
234};
235
236/* Search a child die */
237static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
238 int (*callback)(Dwarf_Die *, void *),
239 void *data, Dwarf_Die *die_mem)
240{
241 Dwarf_Die child_die;
242 int ret;
243
244 ret = dwarf_child(rt_die, die_mem);
245 if (ret != 0)
246 return NULL;
247
248 do {
249 ret = callback(die_mem, data);
250 if (ret == DIE_FIND_CB_FOUND)
251 return die_mem;
252
253 if ((ret & DIE_FIND_CB_CHILD) &&
254 die_find_child(die_mem, callback, data, &child_die)) {
255 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
256 return die_mem;
257 }
258 } while ((ret & DIE_FIND_CB_SIBLING) &&
259 dwarf_siblingof(die_mem, die_mem) == 0);
260
261 return NULL;
262}
263
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500264struct __addr_die_search_param {
265 Dwarf_Addr addr;
266 Dwarf_Die *die_mem;
267};
268
269static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
270{
271 struct __addr_die_search_param *ad = data;
272
273 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
274 dwarf_haspc(fn_die, ad->addr)) {
275 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
276 return DWARF_CB_ABORT;
277 }
278 return DWARF_CB_OK;
279}
280
281/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400282static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
283 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500284{
285 struct __addr_die_search_param ad;
286 ad.addr = addr;
287 ad.die_mem = die_mem;
288 /* dwarf_getscopes can't find subprogram. */
289 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
290 return NULL;
291 else
292 return die_mem;
293}
294
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400295/* die_find callback for inline function search */
296static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
297{
298 Dwarf_Addr *addr = data;
299
300 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
301 dwarf_haspc(die_mem, *addr))
302 return DIE_FIND_CB_FOUND;
303
304 return DIE_FIND_CB_CONTINUE;
305}
306
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500307/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400308static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
309 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500310{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400311 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500312}
313
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400314static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400315{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400316 const char *name = data;
317 int tag;
318
319 tag = dwarf_tag(die_mem);
320 if ((tag == DW_TAG_formal_parameter ||
321 tag == DW_TAG_variable) &&
322 (die_compare_name(die_mem, name) == 0))
323 return DIE_FIND_CB_FOUND;
324
325 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400326}
327
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400328/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500329static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
330 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500331{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400332 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
333 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400334}
335
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400336static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
337{
338 const char *name = data;
339
340 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
341 (die_compare_name(die_mem, name) == 0))
342 return DIE_FIND_CB_FOUND;
343
344 return DIE_FIND_CB_SIBLING;
345}
346
347/* Find a member called 'name' */
348static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
349 Dwarf_Die *die_mem)
350{
351 return die_find_child(st_die, __die_find_member_cb, (void *)name,
352 die_mem);
353}
354
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400355/*
356 * Probe finder related functions
357 */
358
359/* Show a location */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400360static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400361{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500362 unsigned int regn;
363 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400364 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400365 const char *regs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400366 struct kprobe_trace_arg *tvar = pf->tvar;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400367
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400368 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500369 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400370 if (pf->fb_ops == NULL) {
371 pr_warning("The attribute of frame base is not "
372 "supported.\n");
373 return -ENOTSUP;
374 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400375 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500376 offs = op->number;
377 op = &pf->fb_ops[0];
378 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400379
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500380 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
381 regn = op->atom - DW_OP_breg0;
382 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400383 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500384 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
385 regn = op->atom - DW_OP_reg0;
386 } else if (op->atom == DW_OP_bregx) {
387 regn = op->number;
388 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400389 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500390 } else if (op->atom == DW_OP_regx) {
391 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400392 } else {
393 pr_warning("DW_OP %x is not supported.\n", op->atom);
394 return -ENOTSUP;
395 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400396
397 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400398 if (!regs) {
Ian Munsiecd932c52010-04-20 16:58:32 +1000399 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400400 return -ERANGE;
401 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400402
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400403 tvar->value = strdup(regs);
404 if (tvar->value == NULL)
405 return -ENOMEM;
406
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400407 if (ref) {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400408 tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
409 if (tvar->ref == NULL)
410 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400411 tvar->ref->offset = (long)offs;
412 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400413 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400414}
415
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400416static int convert_variable_type(Dwarf_Die *vr_die,
417 struct kprobe_trace_arg *targ)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400418{
419 Dwarf_Die type;
420 char buf[16];
421 int ret;
422
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400423 if (die_get_real_type(vr_die, &type) == NULL) {
424 pr_warning("Failed to get a type information of %s.\n",
425 dwarf_diename(vr_die));
426 return -ENOENT;
427 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400428
429 ret = die_get_byte_size(&type) * 8;
430 if (ret) {
431 /* Check the bitwidth */
432 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400433 pr_info("%s exceeds max-bitwidth."
434 " Cut down to %d bits.\n",
435 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400436 ret = MAX_BASIC_TYPE_BITS;
437 }
438
439 ret = snprintf(buf, 16, "%c%d",
440 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400441 if (ret < 0 || ret >= 16) {
442 if (ret >= 16)
443 ret = -E2BIG;
444 pr_warning("Failed to convert variable type: %s\n",
445 strerror(-ret));
446 return ret;
447 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400448 targ->type = strdup(buf);
449 if (targ->type == NULL)
450 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400451 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400452 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400453}
454
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400455static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400456 struct perf_probe_arg_field *field,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400457 struct kprobe_trace_arg_ref **ref_ptr,
458 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400459{
460 struct kprobe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400461 Dwarf_Die type;
462 Dwarf_Word offs;
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300463 int ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400464
465 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400466 if (die_get_real_type(vr_die, &type) == NULL) {
467 pr_warning("Failed to get the type of %s.\n", varname);
468 return -ENOENT;
469 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400470
471 /* Check the pointer and dereference */
472 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400473 if (!field->ref) {
474 pr_err("Semantic error: %s must be referred by '->'\n",
475 field->name);
476 return -EINVAL;
477 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400478 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400479 if (die_get_real_type(&type, &type) == NULL) {
480 pr_warning("Failed to get the type of %s.\n", varname);
481 return -ENOENT;
482 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400483 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400484 if (dwarf_tag(&type) != DW_TAG_structure_type) {
485 pr_warning("%s is not a data structure.\n", varname);
486 return -EINVAL;
487 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400488
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400489 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
490 if (ref == NULL)
491 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400492 if (*ref_ptr)
493 (*ref_ptr)->next = ref;
494 else
495 *ref_ptr = ref;
496 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400497 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400498 if (dwarf_tag(&type) != DW_TAG_structure_type) {
499 pr_warning("%s is not a data structure.\n", varname);
500 return -EINVAL;
501 }
502 if (field->ref) {
503 pr_err("Semantic error: %s must be referred by '.'\n",
504 field->name);
505 return -EINVAL;
506 }
507 if (!ref) {
508 pr_warning("Structure on a register is not "
509 "supported yet.\n");
510 return -ENOTSUP;
511 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400512 }
513
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400514 if (die_find_member(&type, field->name, die_mem) == NULL) {
515 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
516 dwarf_diename(&type), field->name);
517 return -EINVAL;
518 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400519
520 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300521 ret = die_get_data_member_location(die_mem, &offs);
522 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400523 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300524 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400525 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400526 ref->offset += (long)offs;
527
528 /* Converting next field */
529 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400530 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300531 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400532 else
533 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400534}
535
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400536/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400537static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400538{
539 Dwarf_Attribute attr;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400540 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500541 Dwarf_Op *expr;
542 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400543 int ret;
544
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500545 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400546 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500547 /* TODO: handle more than 1 exprs */
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400548 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500549 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400550 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500551
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400552 ret = convert_location(expr, pf);
553 if (ret == 0 && pf->pvar->field) {
554 ret = convert_variable_fields(vr_die, pf->pvar->var,
555 pf->pvar->field, &pf->tvar->ref,
556 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400557 vr_die = &die_mem;
558 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400559 if (ret == 0) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400560 if (pf->pvar->type) {
561 pf->tvar->type = strdup(pf->pvar->type);
562 if (pf->tvar->type == NULL)
563 ret = -ENOMEM;
564 } else
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400565 ret = convert_variable_type(vr_die, pf->tvar);
566 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500567 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400568 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400569error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500570 /* TODO: Support const_value */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400571 pr_err("Failed to find the location of %s at this address.\n"
572 " Perhaps, it has been optimized out.\n", pf->pvar->var);
573 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400574}
575
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400576/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400577static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400578{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500579 Dwarf_Die vr_die;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400580 char buf[32], *ptr;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400581 int ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400582
Masami Hiramatsu48481932010-04-12 13:16:53 -0400583 /* TODO: Support arrays */
584 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400585 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400586 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400587 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
588 if (ret < 0)
589 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400590 ptr = strchr(buf, ':'); /* Change type separator to _ */
591 if (ptr)
592 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400593 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400594 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400595 if (pf->tvar->name == NULL)
596 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400597
598 if (!is_c_varname(pf->pvar->var)) {
599 /* Copy raw parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400600 pf->tvar->value = strdup(pf->pvar->var);
601 if (pf->tvar->value == NULL)
602 return -ENOMEM;
603 else
604 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400605 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400606
607 pr_debug("Searching '%s' variable in context.\n",
608 pf->pvar->var);
609 /* Search child die for local variables and parameters. */
610 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
611 pr_warning("Failed to find '%s' in this function.\n",
612 pf->pvar->var);
613 return -ENOENT;
614 }
615 return convert_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400616}
617
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400618/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400619static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400620{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400621 struct kprobe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500622 Dwarf_Addr eaddr;
623 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500624 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400625 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500626 Dwarf_Attribute fb_attr;
627 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400628
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400629 if (pf->ntevs == pf->max_tevs) {
630 pr_warning("Too many( > %d) probe point found.\n",
631 pf->max_tevs);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400632 return -ERANGE;
633 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400634 tev = &pf->tevs[pf->ntevs++];
635
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500636 /* If no real subprogram, find a real one */
637 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400638 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500639 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400640 if (!sp_die) {
641 pr_warning("Failed to find probe point in any "
642 "functions.\n");
643 return -ENOENT;
644 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500645 }
646
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400647 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500648 name = dwarf_diename(sp_die);
649 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400650 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
651 pr_warning("Failed to get entry pc of %s\n",
652 dwarf_diename(sp_die));
653 return -ENOENT;
654 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400655 tev->point.symbol = strdup(name);
656 if (tev->point.symbol == NULL)
657 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400658 tev->point.offset = (unsigned long)(pf->addr - eaddr);
659 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400660 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400661 tev->point.offset = (unsigned long)pf->addr;
662
663 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
664 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400665
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500666 /* Get the frame base attribute/ops */
667 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400668 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400669 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500670 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400671#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400672 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
673 pf->cfi != NULL) {
674 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400675 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
676 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
677 pr_warning("Failed to get CFA on 0x%jx\n",
678 (uintmax_t)pf->addr);
679 return -ENOENT;
680 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400681#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400682 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500683
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400684 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400685 tev->nargs = pf->pev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400686 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
687 if (tev->args == NULL)
688 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400689 for (i = 0; i < pf->pev->nargs; i++) {
690 pf->pvar = &pf->pev->args[i];
691 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400692 ret = find_variable(sp_die, pf);
693 if (ret != 0)
694 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400695 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500696
697 /* *pf->fb_ops will be cached in libdw. Don't free it. */
698 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400699 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400700}
701
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400702/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400703static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400704{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500705 Dwarf_Lines *lines;
706 Dwarf_Line *line;
707 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500708 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500709 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400710 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400711
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400712 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
713 pr_warning("No source lines found in this CU.\n");
714 return -ENOENT;
715 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400716
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400717 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500718 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400719 if (dwarf_lineno(line, &lineno) != 0 ||
720 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400721 continue;
722
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500723 /* TODO: Get fileno from line, but how? */
724 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
725 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400726
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400727 if (dwarf_lineaddr(line, &addr) != 0) {
728 pr_warning("Failed to get the address of the line.\n");
729 return -ENOENT;
730 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500731 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
732 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400733 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500734
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400735 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400736 /* Continuing, because target line might be inlined. */
737 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400738 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400739}
740
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500741/* Find lines which match lazy pattern */
742static int find_lazy_match_lines(struct list_head *head,
743 const char *fname, const char *pat)
744{
745 char *fbuf, *p1, *p2;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300746 int fd, line, nlines = -1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500747 struct stat st;
748
749 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400750 if (fd < 0) {
751 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300752 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400753 }
754
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300755 if (fstat(fd, &st) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400756 pr_warning("Failed to get the size of %s: %s\n",
757 fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300758 nlines = -errno;
759 goto out_close;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400760 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300761
762 nlines = -ENOMEM;
763 fbuf = malloc(st.st_size + 2);
764 if (fbuf == NULL)
765 goto out_close;
766 if (read(fd, fbuf, st.st_size) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400767 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300768 nlines = -errno;
769 goto out_free_fbuf;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400770 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500771 fbuf[st.st_size] = '\n'; /* Dummy line */
772 fbuf[st.st_size + 1] = '\0';
773 p1 = fbuf;
774 line = 1;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300775 nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500776 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 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300785out_free_fbuf:
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500786 free(fbuf);
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300787out_close:
788 close(fd);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500789 return nlines;
790}
791
792/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400793static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500794{
795 Dwarf_Lines *lines;
796 Dwarf_Line *line;
797 size_t nlines, i;
798 Dwarf_Addr addr;
799 Dwarf_Die die_mem;
800 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400801 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500802
803 if (list_empty(&pf->lcache)) {
804 /* Matching lazy line pattern */
805 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400806 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400807 if (ret == 0) {
808 pr_debug("No matched lines found in %s.\n", pf->fname);
809 return 0;
810 } else if (ret < 0)
811 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500812 }
813
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400814 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
815 pr_warning("No source lines found in this CU.\n");
816 return -ENOENT;
817 }
818
819 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500820 line = dwarf_onesrcline(lines, i);
821
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400822 if (dwarf_lineno(line, &lineno) != 0 ||
823 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500824 continue;
825
826 /* TODO: Get fileno from line, but how? */
827 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
828 continue;
829
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400830 if (dwarf_lineaddr(line, &addr) != 0) {
831 pr_debug("Failed to get the address of line %d.\n",
832 lineno);
833 continue;
834 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500835 if (sp_die) {
836 /* Address filtering 1: does sp_die include addr? */
837 if (!dwarf_haspc(sp_die, addr))
838 continue;
839 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400840 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500841 continue;
842 }
843
844 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
845 (int)i, lineno, (unsigned long long)addr);
846 pf->addr = addr;
847
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400848 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500849 /* Continuing, because target line might be inlined. */
850 }
851 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400852 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500853}
854
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400855/* Callback parameter with return value */
856struct dwarf_callback_param {
857 void *data;
858 int retval;
859};
860
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500861static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400862{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400863 struct dwarf_callback_param *param = data;
864 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400865 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400866 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400867
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500868 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400869 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500870 else {
871 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400872 if (dwarf_entrypc(in_die, &addr) != 0) {
873 pr_warning("Failed to get entry pc of %s.\n",
874 dwarf_diename(in_die));
875 param->retval = -ENOENT;
876 return DWARF_CB_ABORT;
877 }
878 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500879 pf->addr += pp->offset;
880 pr_debug("found inline addr: 0x%jx\n",
881 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500882
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400883 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -0400884 if (param->retval < 0)
885 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500886 }
887
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500888 return DWARF_CB_OK;
889}
890
891/* Search function from function name */
892static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
893{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400894 struct dwarf_callback_param *param = data;
895 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400896 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500897
898 /* Check tag and diename */
899 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
900 die_compare_name(sp_die, pp->function) != 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400901 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500902
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500903 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500904 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500905 dwarf_decl_line(sp_die, &pf->lno);
906 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400907 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500908 } else if (!dwarf_func_inline(sp_die)) {
909 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500910 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400911 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500912 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400913 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
914 pr_warning("Failed to get entry pc of %s.\n",
915 dwarf_diename(sp_die));
916 param->retval = -ENOENT;
917 return DWARF_CB_ABORT;
918 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500919 pf->addr += pp->offset;
920 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400921 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500922 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400923 } else {
924 struct dwarf_callback_param _param = {.data = (void *)pf,
925 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500926 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400927 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
928 &_param);
929 param->retval = _param.retval;
930 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500931
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400932 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400933}
934
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400935static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400936{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400937 struct dwarf_callback_param _param = {.data = (void *)pf,
938 .retval = 0};
939 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
940 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400941}
942
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400943/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
944int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400945 struct kprobe_trace_event **tevs, int max_tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400946{
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400947 struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400948 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500949 Dwarf_Off off, noff;
950 size_t cuhl;
951 Dwarf_Die *diep;
952 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400953 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400954
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400955 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400956 if (pf.tevs == NULL)
957 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400958 *tevs = pf.tevs;
959 pf.ntevs = 0;
960
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500961 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400962 if (!dbg) {
963 pr_warning("No dwarf info found in the vmlinux - "
964 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300965 free(pf.tevs);
966 *tevs = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400967 return -EBADF;
968 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400969
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400970#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400971 /* Get the call frame information from this dwarf */
972 pf.cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400973#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400974
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500975 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500976 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500977 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400978 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
979 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400980 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500981 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
982 if (!diep)
983 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400984
985 /* Check if target file is included. */
986 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500987 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500988 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500989 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400990
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500991 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400992 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400993 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500994 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400995 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400996 else {
997 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400998 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400999 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001000 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001001 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001002 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001003 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001004 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001005
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001006 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001007}
1008
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001009/* Reverse search */
1010int find_perf_probe_point(int fd, unsigned long addr,
1011 struct perf_probe_point *ppt)
1012{
1013 Dwarf_Die cudie, spdie, indie;
1014 Dwarf *dbg;
1015 Dwarf_Line *line;
1016 Dwarf_Addr laddr, eaddr;
1017 const char *tmp;
1018 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001019 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001020
1021 dbg = dwarf_begin(fd, DWARF_C_READ);
1022 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001023 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001024
1025 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001026 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1027 ret = -EINVAL;
1028 goto end;
1029 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001030
1031 /* Find a corresponding line */
1032 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1033 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001034 if (dwarf_lineaddr(line, &laddr) == 0 &&
1035 (Dwarf_Addr)addr == laddr &&
1036 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001037 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001038 if (tmp) {
1039 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001040 ppt->file = strdup(tmp);
1041 if (ppt->file == NULL) {
1042 ret = -ENOMEM;
1043 goto end;
1044 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001045 found = true;
1046 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001047 }
1048 }
1049
1050 /* Find a corresponding function */
1051 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1052 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001053 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001054 goto end;
1055
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001056 if (ppt->line) {
1057 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1058 &indie)) {
1059 /* addr in an inline function */
1060 tmp = dwarf_diename(&indie);
1061 if (!tmp)
1062 goto end;
1063 ret = dwarf_decl_line(&indie, &lineno);
1064 } else {
1065 if (eaddr == addr) { /* Function entry */
1066 lineno = ppt->line;
1067 ret = 0;
1068 } else
1069 ret = dwarf_decl_line(&spdie, &lineno);
1070 }
1071 if (ret == 0) {
1072 /* Make a relative line number */
1073 ppt->line -= lineno;
1074 goto found;
1075 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001076 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001077 /* We don't have a line number, let's use offset */
1078 ppt->offset = addr - (unsigned long)eaddr;
1079found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001080 ppt->function = strdup(tmp);
1081 if (ppt->function == NULL) {
1082 ret = -ENOMEM;
1083 goto end;
1084 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001085 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001086 }
1087
1088end:
1089 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001090 if (ret >= 0)
1091 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001092 return ret;
1093}
1094
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001095/* Add a line and store the src path */
1096static int line_range_add_line(const char *src, unsigned int lineno,
1097 struct line_range *lr)
1098{
1099 /* Copy real path */
1100 if (!lr->path) {
1101 lr->path = strdup(src);
1102 if (lr->path == NULL)
1103 return -ENOMEM;
1104 }
1105 return line_list__add_line(&lr->line_list, lineno);
1106}
1107
1108/* Search function declaration lines */
1109static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1110{
1111 struct dwarf_callback_param *param = data;
1112 struct line_finder *lf = param->data;
1113 const char *src;
1114 int lineno;
1115
1116 src = dwarf_decl_file(sp_die);
1117 if (src && strtailcmp(src, lf->fname) != 0)
1118 return DWARF_CB_OK;
1119
1120 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1121 (lf->lno_s > lineno || lf->lno_e < lineno))
1122 return DWARF_CB_OK;
1123
1124 param->retval = line_range_add_line(src, lineno, lf->lr);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001125 if (param->retval < 0)
1126 return DWARF_CB_ABORT;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001127 return DWARF_CB_OK;
1128}
1129
1130static int find_line_range_func_decl_lines(struct line_finder *lf)
1131{
1132 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1133 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1134 return param.retval;
1135}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001136
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001137/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001138static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001139{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001140 Dwarf_Lines *lines;
1141 Dwarf_Line *line;
1142 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001143 Dwarf_Addr addr;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001144 int lineno, ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001145 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001146 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001147
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001148 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001149 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1150 pr_warning("No source lines found in this CU.\n");
1151 return -ENOENT;
1152 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001153
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001154 /* Search probable lines on lines list */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001155 for (i = 0; i < nlines; i++) {
1156 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001157 if (dwarf_lineno(line, &lineno) != 0 ||
1158 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001159 continue;
1160
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001161 if (sp_die) {
1162 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001163 if (dwarf_lineaddr(line, &addr) != 0 ||
1164 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001165 continue;
1166
1167 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001168 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001169 continue;
1170 }
1171
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001172 /* TODO: Get fileno from line, but how? */
1173 src = dwarf_linesrc(line, NULL, NULL);
1174 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001175 continue;
1176
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001177 ret = line_range_add_line(src, lineno, lf->lr);
1178 if (ret < 0)
1179 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001180 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001181
1182 /*
1183 * Dwarf lines doesn't include function declarations. We have to
1184 * check functions list or given function.
1185 */
1186 if (sp_die) {
1187 src = dwarf_decl_file(sp_die);
1188 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1189 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1190 ret = line_range_add_line(src, lineno, lf->lr);
1191 } else
1192 ret = find_line_range_func_decl_lines(lf);
1193
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001194 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001195 if (ret >= 0)
1196 if (!list_empty(&lf->lr->line_list))
1197 ret = lf->found = 1;
1198 else
1199 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001200 else {
1201 free(lf->lr->path);
1202 lf->lr->path = NULL;
1203 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001204 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001205}
1206
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001207static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1208{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001209 struct dwarf_callback_param *param = data;
1210
1211 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001212 return DWARF_CB_ABORT; /* No need to find other instances */
1213}
1214
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001215/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001216static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001217{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001218 struct dwarf_callback_param *param = data;
1219 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001220 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001221
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001222 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1223 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001224 lf->fname = dwarf_decl_file(sp_die);
1225 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001226 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001227 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001228 if (lf->lno_s < 0) /* Overflow */
1229 lf->lno_s = INT_MAX;
1230 lf->lno_e = lr->offset + lr->end;
1231 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001232 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001233 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001234 lr->start = lf->lno_s;
1235 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001236 if (dwarf_func_inline(sp_die)) {
1237 struct dwarf_callback_param _param;
1238 _param.data = (void *)lf;
1239 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001240 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001241 line_range_inline_cb,
1242 &_param);
1243 param->retval = _param.retval;
1244 } else
1245 param->retval = find_line_range_by_line(sp_die, lf);
1246 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001247 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001248 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001249}
1250
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001251static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001252{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001253 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1254 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1255 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001256}
1257
1258int find_line_range(int fd, struct line_range *lr)
1259{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001260 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001261 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001262 Dwarf_Off off = 0, noff;
1263 size_t cuhl;
1264 Dwarf_Die *diep;
1265 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001266
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001267 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001268 if (!dbg) {
1269 pr_warning("No dwarf info found in the vmlinux - "
1270 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1271 return -EBADF;
1272 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001273
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001274 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001275 while (!lf.found && ret >= 0) {
1276 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001277 break;
1278
1279 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001280 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1281 if (!diep)
1282 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001283
1284 /* Check if target file is included. */
1285 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001286 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001287 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001288 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001289
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001290 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001291 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001292 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001293 else {
1294 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001295 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001296 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001297 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001298 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001299 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001300 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001301 pr_debug("path: %lx\n", (unsigned long)lr->path);
1302 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001303
1304 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001305}
1306