blob: a934a364c30fd6814a7633dfdfa23d5758941637 [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"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040040#include "symbol.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040041#include "probe-finder.h"
42
Masami Hiramatsu49849122010-04-12 13:17:15 -040043/* Kprobe tracer basic type is up to u64 */
44#define MAX_BASIC_TYPE_BITS 64
45
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040046/*
47 * Compare the tail of two strings.
48 * Return 0 if whole of either string is same as another's tail part.
49 */
50static int strtailcmp(const char *s1, const char *s2)
51{
52 int i1 = strlen(s1);
53 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -050054 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040055 if (s1[i1] != s2[i2])
56 return s1[i1] - s2[i2];
57 }
58 return 0;
59}
60
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050061/* Line number list operations */
62
63/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040064static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050065{
66 struct line_node *ln;
67 struct list_head *p;
68
69 /* Reverse search, because new line will be the last one */
70 list_for_each_entry_reverse(ln, head, list) {
71 if (ln->line < line) {
72 p = &ln->list;
73 goto found;
74 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040075 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050076 }
77 /* List is empty, or the smallest entry */
78 p = head;
79found:
80 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040081 ln = zalloc(sizeof(struct line_node));
82 if (ln == NULL)
83 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050084 ln->line = line;
85 INIT_LIST_HEAD(&ln->list);
86 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040087 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050088}
89
90/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040091static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050092{
93 struct line_node *ln;
94
95 /* Reverse search, because new line will be the last one */
96 list_for_each_entry(ln, head, list)
97 if (ln->line == line)
98 return 1;
99
100 return 0;
101}
102
103/* Init line number list */
104static void line_list__init(struct list_head *head)
105{
106 INIT_LIST_HEAD(head);
107}
108
109/* Free line number list */
110static void line_list__free(struct list_head *head)
111{
112 struct line_node *ln;
113 while (!list_empty(head)) {
114 ln = list_first_entry(head, struct line_node, list);
115 list_del(&ln->list);
116 free(ln);
117 }
118}
119
120/* Dwarf wrappers */
121
122/* Find the realpath of the target file. */
123static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400124{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500125 Dwarf_Files *files;
126 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300127 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400128 int ret;
129
130 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500131 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500132
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500133 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500134 if (ret != 0)
135 return NULL;
136
137 for (i = 0; i < nfiles; i++) {
138 src = dwarf_filesrc(files, i, NULL, NULL);
139 if (strtailcmp(src, fname) == 0)
140 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500141 }
Masami Hiramatsuc9e38582010-04-02 12:50:45 -0400142 if (i == nfiles)
143 return NULL;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500144 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500145}
146
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400147/* Compare diename and tname */
148static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
149{
150 const char *name;
151 name = dwarf_diename(dw_die);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400152 return name ? strcmp(tname, name) : -1;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400153}
154
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400155/* Get type die, but skip qualifiers and typedef */
156static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
157{
158 Dwarf_Attribute attr;
159 int tag;
160
161 do {
162 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
163 dwarf_formref_die(&attr, die_mem) == NULL)
164 return NULL;
165
166 tag = dwarf_tag(die_mem);
167 vr_die = die_mem;
168 } while (tag == DW_TAG_const_type ||
169 tag == DW_TAG_restrict_type ||
170 tag == DW_TAG_volatile_type ||
171 tag == DW_TAG_shared_type ||
172 tag == DW_TAG_typedef);
173
174 return die_mem;
175}
176
Masami Hiramatsu49849122010-04-12 13:17:15 -0400177static bool die_is_signed_type(Dwarf_Die *tp_die)
178{
179 Dwarf_Attribute attr;
180 Dwarf_Word ret;
181
182 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
183 dwarf_formudata(&attr, &ret) != 0)
184 return false;
185
186 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
187 ret == DW_ATE_signed_fixed);
188}
189
190static int die_get_byte_size(Dwarf_Die *tp_die)
191{
192 Dwarf_Attribute attr;
193 Dwarf_Word ret;
194
195 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
196 dwarf_formudata(&attr, &ret) != 0)
197 return 0;
198
199 return (int)ret;
200}
201
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300202/* Get data_member_location offset */
203static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
204{
205 Dwarf_Attribute attr;
206 Dwarf_Op *expr;
207 size_t nexpr;
208 int ret;
209
210 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
211 return -ENOENT;
212
213 if (dwarf_formudata(&attr, offs) != 0) {
214 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
215 ret = dwarf_getlocation(&attr, &expr, &nexpr);
216 if (ret < 0 || nexpr == 0)
217 return -ENOENT;
218
219 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
220 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
221 expr[0].atom, nexpr);
222 return -ENOTSUP;
223 }
224 *offs = (Dwarf_Word)expr[0].number;
225 }
226 return 0;
227}
228
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400229/* Return values for die_find callbacks */
230enum {
231 DIE_FIND_CB_FOUND = 0, /* End of Search */
232 DIE_FIND_CB_CHILD = 1, /* Search only children */
233 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
234 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
235};
236
237/* Search a child die */
238static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
239 int (*callback)(Dwarf_Die *, void *),
240 void *data, Dwarf_Die *die_mem)
241{
242 Dwarf_Die child_die;
243 int ret;
244
245 ret = dwarf_child(rt_die, die_mem);
246 if (ret != 0)
247 return NULL;
248
249 do {
250 ret = callback(die_mem, data);
251 if (ret == DIE_FIND_CB_FOUND)
252 return die_mem;
253
254 if ((ret & DIE_FIND_CB_CHILD) &&
255 die_find_child(die_mem, callback, data, &child_die)) {
256 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
257 return die_mem;
258 }
259 } while ((ret & DIE_FIND_CB_SIBLING) &&
260 dwarf_siblingof(die_mem, die_mem) == 0);
261
262 return NULL;
263}
264
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500265struct __addr_die_search_param {
266 Dwarf_Addr addr;
267 Dwarf_Die *die_mem;
268};
269
270static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
271{
272 struct __addr_die_search_param *ad = data;
273
274 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
275 dwarf_haspc(fn_die, ad->addr)) {
276 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
277 return DWARF_CB_ABORT;
278 }
279 return DWARF_CB_OK;
280}
281
282/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400283static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
284 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500285{
286 struct __addr_die_search_param ad;
287 ad.addr = addr;
288 ad.die_mem = die_mem;
289 /* dwarf_getscopes can't find subprogram. */
290 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
291 return NULL;
292 else
293 return die_mem;
294}
295
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400296/* die_find callback for inline function search */
297static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
298{
299 Dwarf_Addr *addr = data;
300
301 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
302 dwarf_haspc(die_mem, *addr))
303 return DIE_FIND_CB_FOUND;
304
305 return DIE_FIND_CB_CONTINUE;
306}
307
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500308/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400309static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
310 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500311{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400312 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500313}
314
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400315static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400316{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400317 const char *name = data;
318 int tag;
319
320 tag = dwarf_tag(die_mem);
321 if ((tag == DW_TAG_formal_parameter ||
322 tag == DW_TAG_variable) &&
323 (die_compare_name(die_mem, name) == 0))
324 return DIE_FIND_CB_FOUND;
325
326 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400327}
328
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400329/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500330static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
331 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500332{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400333 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
334 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400335}
336
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400337static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
338{
339 const char *name = data;
340
341 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
342 (die_compare_name(die_mem, name) == 0))
343 return DIE_FIND_CB_FOUND;
344
345 return DIE_FIND_CB_SIBLING;
346}
347
348/* Find a member called 'name' */
349static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
350 Dwarf_Die *die_mem)
351{
352 return die_find_child(st_die, __die_find_member_cb, (void *)name,
353 die_mem);
354}
355
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400356/*
357 * Probe finder related functions
358 */
359
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400360static struct kprobe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400361{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400362 struct kprobe_trace_arg_ref *ref;
363 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
364 if (ref != NULL)
365 ref->offset = offs;
366 return ref;
367}
368
369/* Show a location */
370static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
371{
372 Dwarf_Attribute attr;
373 Dwarf_Op *op;
374 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500375 unsigned int regn;
376 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400377 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400378 const char *regs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400379 struct kprobe_trace_arg *tvar = pf->tvar;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400380 int ret;
381
382 /* TODO: handle more than 1 exprs */
383 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
384 dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
385 nops == 0) {
386 /* TODO: Support const_value */
387 pr_err("Failed to find the location of %s at this address.\n"
388 " Perhaps, it has been optimized out.\n", pf->pvar->var);
389 return -ENOENT;
390 }
391
392 if (op->atom == DW_OP_addr) {
393 /* Static variables on memory (not stack), make @varname */
394 ret = strlen(dwarf_diename(vr_die));
395 tvar->value = zalloc(ret + 2);
396 if (tvar->value == NULL)
397 return -ENOMEM;
398 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
399 tvar->ref = alloc_trace_arg_ref((long)offs);
400 if (tvar->ref == NULL)
401 return -ENOMEM;
402 return 0;
403 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400404
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400405 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500406 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400407 if (pf->fb_ops == NULL) {
408 pr_warning("The attribute of frame base is not "
409 "supported.\n");
410 return -ENOTSUP;
411 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400412 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500413 offs = op->number;
414 op = &pf->fb_ops[0];
415 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400416
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500417 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
418 regn = op->atom - DW_OP_breg0;
419 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400420 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500421 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
422 regn = op->atom - DW_OP_reg0;
423 } else if (op->atom == DW_OP_bregx) {
424 regn = op->number;
425 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400426 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500427 } else if (op->atom == DW_OP_regx) {
428 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400429 } else {
430 pr_warning("DW_OP %x is not supported.\n", op->atom);
431 return -ENOTSUP;
432 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400433
434 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400435 if (!regs) {
Ian Munsiecd932c52010-04-20 16:58:32 +1000436 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400437 return -ERANGE;
438 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400439
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400440 tvar->value = strdup(regs);
441 if (tvar->value == NULL)
442 return -ENOMEM;
443
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400444 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400445 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400446 if (tvar->ref == NULL)
447 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400448 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400449 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400450}
451
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400452static int convert_variable_type(Dwarf_Die *vr_die,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400453 struct kprobe_trace_arg *tvar,
454 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400455{
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400456 struct kprobe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400457 Dwarf_Die type;
458 char buf[16];
459 int ret;
460
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400461 /* TODO: check all types */
462 if (cast && strcmp(cast, "string") != 0) {
463 /* Non string type is OK */
464 tvar->type = strdup(cast);
465 return (tvar->type == NULL) ? -ENOMEM : 0;
466 }
467
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400468 if (die_get_real_type(vr_die, &type) == NULL) {
469 pr_warning("Failed to get a type information of %s.\n",
470 dwarf_diename(vr_die));
471 return -ENOENT;
472 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400473
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400474 pr_debug("%s type is %s.\n",
475 dwarf_diename(vr_die), dwarf_diename(&type));
476
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400477 if (cast && strcmp(cast, "string") == 0) { /* String type */
478 ret = dwarf_tag(&type);
479 if (ret != DW_TAG_pointer_type &&
480 ret != DW_TAG_array_type) {
481 pr_warning("Failed to cast into string: "
482 "%s(%s) is not a pointer nor array.",
483 dwarf_diename(vr_die), dwarf_diename(&type));
484 return -EINVAL;
485 }
486 if (ret == DW_TAG_pointer_type) {
487 if (die_get_real_type(&type, &type) == NULL) {
488 pr_warning("Failed to get a type information.");
489 return -ENOENT;
490 }
491 while (*ref_ptr)
492 ref_ptr = &(*ref_ptr)->next;
493 /* Add new reference with offset +0 */
494 *ref_ptr = zalloc(sizeof(struct kprobe_trace_arg_ref));
495 if (*ref_ptr == NULL) {
496 pr_warning("Out of memory error\n");
497 return -ENOMEM;
498 }
499 }
500 if (die_compare_name(&type, "char") != 0 &&
501 die_compare_name(&type, "unsigned char") != 0) {
502 pr_warning("Failed to cast into string: "
503 "%s is not (unsigned) char *.",
504 dwarf_diename(vr_die));
505 return -EINVAL;
506 }
507 tvar->type = strdup(cast);
508 return (tvar->type == NULL) ? -ENOMEM : 0;
509 }
510
Masami Hiramatsu49849122010-04-12 13:17:15 -0400511 ret = die_get_byte_size(&type) * 8;
512 if (ret) {
513 /* Check the bitwidth */
514 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400515 pr_info("%s exceeds max-bitwidth."
516 " Cut down to %d bits.\n",
517 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400518 ret = MAX_BASIC_TYPE_BITS;
519 }
520
521 ret = snprintf(buf, 16, "%c%d",
522 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400523 if (ret < 0 || ret >= 16) {
524 if (ret >= 16)
525 ret = -E2BIG;
526 pr_warning("Failed to convert variable type: %s\n",
527 strerror(-ret));
528 return ret;
529 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400530 tvar->type = strdup(buf);
531 if (tvar->type == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400532 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400533 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400534 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400535}
536
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400537static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400538 struct perf_probe_arg_field *field,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400539 struct kprobe_trace_arg_ref **ref_ptr,
540 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400541{
542 struct kprobe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400543 Dwarf_Die type;
544 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400545 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400546
547 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400548 if (die_get_real_type(vr_die, &type) == NULL) {
549 pr_warning("Failed to get the type of %s.\n", varname);
550 return -ENOENT;
551 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400552 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
553 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400554
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400555 if (field->name[0] == '[' &&
556 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
557 if (field->next)
558 /* Save original type for next field */
559 memcpy(die_mem, &type, sizeof(*die_mem));
560 /* Get the type of this array */
561 if (die_get_real_type(&type, &type) == NULL) {
562 pr_warning("Failed to get the type of %s.\n", varname);
563 return -ENOENT;
564 }
565 pr_debug2("Array real type: (%x)\n",
566 (unsigned)dwarf_dieoffset(&type));
567 if (tag == DW_TAG_pointer_type) {
568 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
569 if (ref == NULL)
570 return -ENOMEM;
571 if (*ref_ptr)
572 (*ref_ptr)->next = ref;
573 else
574 *ref_ptr = ref;
575 }
576 ref->offset += die_get_byte_size(&type) * field->index;
577 if (!field->next)
578 /* Save vr_die for converting types */
579 memcpy(die_mem, vr_die, sizeof(*die_mem));
580 goto next;
581 } else if (tag == DW_TAG_pointer_type) {
582 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400583 if (!field->ref) {
584 pr_err("Semantic error: %s must be referred by '->'\n",
585 field->name);
586 return -EINVAL;
587 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400588 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400589 if (die_get_real_type(&type, &type) == NULL) {
590 pr_warning("Failed to get the type of %s.\n", varname);
591 return -ENOENT;
592 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400593 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400594 if (dwarf_tag(&type) != DW_TAG_structure_type) {
595 pr_warning("%s is not a data structure.\n", varname);
596 return -EINVAL;
597 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400598
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400599 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
600 if (ref == NULL)
601 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400602 if (*ref_ptr)
603 (*ref_ptr)->next = ref;
604 else
605 *ref_ptr = ref;
606 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400607 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400608 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400609 pr_warning("%s is not a data structure.\n", varname);
610 return -EINVAL;
611 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400612 if (field->name[0] == '[') {
613 pr_err("Semantic error: %s is not a pointor nor array.",
614 varname);
615 return -EINVAL;
616 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400617 if (field->ref) {
618 pr_err("Semantic error: %s must be referred by '.'\n",
619 field->name);
620 return -EINVAL;
621 }
622 if (!ref) {
623 pr_warning("Structure on a register is not "
624 "supported yet.\n");
625 return -ENOTSUP;
626 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400627 }
628
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400629 if (die_find_member(&type, field->name, die_mem) == NULL) {
630 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
631 dwarf_diename(&type), field->name);
632 return -EINVAL;
633 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400634
635 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300636 ret = die_get_data_member_location(die_mem, &offs);
637 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400638 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300639 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400640 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400641 ref->offset += (long)offs;
642
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400643next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400644 /* Converting next field */
645 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400646 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300647 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400648 else
649 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400650}
651
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400652/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400653static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400654{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400655 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400656 int ret;
657
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400658 pr_debug("Converting variable %s into trace event.\n",
659 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500660
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400661 ret = convert_variable_location(vr_die, pf);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400662 if (ret == 0 && pf->pvar->field) {
663 ret = convert_variable_fields(vr_die, pf->pvar->var,
664 pf->pvar->field, &pf->tvar->ref,
665 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400666 vr_die = &die_mem;
667 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400668 if (ret == 0)
669 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500670 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400671 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400672}
673
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400674/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400675static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400676{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400677 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400678 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400679 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400680
Masami Hiramatsu48481932010-04-12 13:16:53 -0400681 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400682 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400683 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400684 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
685 if (ret < 0)
686 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400687 ptr = strchr(buf, ':'); /* Change type separator to _ */
688 if (ptr)
689 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400690 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400691 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400692 if (pf->tvar->name == NULL)
693 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400694
695 if (!is_c_varname(pf->pvar->var)) {
696 /* Copy raw parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400697 pf->tvar->value = strdup(pf->pvar->var);
698 if (pf->tvar->value == NULL)
699 return -ENOMEM;
700 else
701 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400702 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400703
704 pr_debug("Searching '%s' variable in context.\n",
705 pf->pvar->var);
706 /* Search child die for local variables and parameters. */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400707 if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
708 ret = convert_variable(&vr_die, pf);
709 else {
710 /* Search upper class */
711 nscopes = dwarf_getscopes_die(sp_die, &scopes);
712 if (nscopes > 0) {
713 ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
714 0, NULL, 0, 0, &vr_die);
715 if (ret >= 0)
716 ret = convert_variable(&vr_die, pf);
717 else
718 ret = -ENOENT;
719 free(scopes);
720 } else
721 ret = -ENOENT;
722 }
723 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400724 pr_warning("Failed to find '%s' in this function.\n",
725 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400726 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400727}
728
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400729/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400730static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400731{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400732 struct kprobe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500733 Dwarf_Addr eaddr;
734 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500735 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400736 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500737 Dwarf_Attribute fb_attr;
738 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400739
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400740 if (pf->ntevs == pf->max_tevs) {
741 pr_warning("Too many( > %d) probe point found.\n",
742 pf->max_tevs);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400743 return -ERANGE;
744 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400745 tev = &pf->tevs[pf->ntevs++];
746
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500747 /* If no real subprogram, find a real one */
748 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400749 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500750 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400751 if (!sp_die) {
752 pr_warning("Failed to find probe point in any "
753 "functions.\n");
754 return -ENOENT;
755 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500756 }
757
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400758 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500759 name = dwarf_diename(sp_die);
760 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400761 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
762 pr_warning("Failed to get entry pc of %s\n",
763 dwarf_diename(sp_die));
764 return -ENOENT;
765 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400766 tev->point.symbol = strdup(name);
767 if (tev->point.symbol == NULL)
768 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400769 tev->point.offset = (unsigned long)(pf->addr - eaddr);
770 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400771 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400772 tev->point.offset = (unsigned long)pf->addr;
773
774 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
775 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400776
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500777 /* Get the frame base attribute/ops */
778 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400779 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400780 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500781 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400782#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400783 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
784 pf->cfi != NULL) {
785 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400786 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
787 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
788 pr_warning("Failed to get CFA on 0x%jx\n",
789 (uintmax_t)pf->addr);
790 return -ENOENT;
791 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400792#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400793 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500794
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400795 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400796 tev->nargs = pf->pev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400797 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
798 if (tev->args == NULL)
799 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400800 for (i = 0; i < pf->pev->nargs; i++) {
801 pf->pvar = &pf->pev->args[i];
802 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400803 ret = find_variable(sp_die, pf);
804 if (ret != 0)
805 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400806 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500807
808 /* *pf->fb_ops will be cached in libdw. Don't free it. */
809 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400810 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400811}
812
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400813/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400814static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400815{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500816 Dwarf_Lines *lines;
817 Dwarf_Line *line;
818 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500819 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500820 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400821 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400822
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400823 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
824 pr_warning("No source lines found in this CU.\n");
825 return -ENOENT;
826 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400827
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400828 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500829 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400830 if (dwarf_lineno(line, &lineno) != 0 ||
831 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400832 continue;
833
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500834 /* TODO: Get fileno from line, but how? */
835 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
836 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400837
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400838 if (dwarf_lineaddr(line, &addr) != 0) {
839 pr_warning("Failed to get the address of the line.\n");
840 return -ENOENT;
841 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500842 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
843 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400844 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500845
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400846 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400847 /* Continuing, because target line might be inlined. */
848 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400849 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400850}
851
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500852/* Find lines which match lazy pattern */
853static int find_lazy_match_lines(struct list_head *head,
854 const char *fname, const char *pat)
855{
856 char *fbuf, *p1, *p2;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300857 int fd, line, nlines = -1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500858 struct stat st;
859
860 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400861 if (fd < 0) {
862 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300863 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400864 }
865
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300866 if (fstat(fd, &st) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400867 pr_warning("Failed to get the size of %s: %s\n",
868 fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300869 nlines = -errno;
870 goto out_close;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400871 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300872
873 nlines = -ENOMEM;
874 fbuf = malloc(st.st_size + 2);
875 if (fbuf == NULL)
876 goto out_close;
877 if (read(fd, fbuf, st.st_size) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400878 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300879 nlines = -errno;
880 goto out_free_fbuf;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400881 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500882 fbuf[st.st_size] = '\n'; /* Dummy line */
883 fbuf[st.st_size + 1] = '\0';
884 p1 = fbuf;
885 line = 1;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300886 nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500887 while ((p2 = strchr(p1, '\n')) != NULL) {
888 *p2 = '\0';
889 if (strlazymatch(p1, pat)) {
890 line_list__add_line(head, line);
891 nlines++;
892 }
893 line++;
894 p1 = p2 + 1;
895 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300896out_free_fbuf:
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500897 free(fbuf);
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300898out_close:
899 close(fd);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500900 return nlines;
901}
902
903/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400904static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500905{
906 Dwarf_Lines *lines;
907 Dwarf_Line *line;
908 size_t nlines, i;
909 Dwarf_Addr addr;
910 Dwarf_Die die_mem;
911 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400912 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500913
914 if (list_empty(&pf->lcache)) {
915 /* Matching lazy line pattern */
916 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400917 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400918 if (ret == 0) {
919 pr_debug("No matched lines found in %s.\n", pf->fname);
920 return 0;
921 } else if (ret < 0)
922 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500923 }
924
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400925 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
926 pr_warning("No source lines found in this CU.\n");
927 return -ENOENT;
928 }
929
930 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500931 line = dwarf_onesrcline(lines, i);
932
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400933 if (dwarf_lineno(line, &lineno) != 0 ||
934 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500935 continue;
936
937 /* TODO: Get fileno from line, but how? */
938 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
939 continue;
940
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400941 if (dwarf_lineaddr(line, &addr) != 0) {
942 pr_debug("Failed to get the address of line %d.\n",
943 lineno);
944 continue;
945 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500946 if (sp_die) {
947 /* Address filtering 1: does sp_die include addr? */
948 if (!dwarf_haspc(sp_die, addr))
949 continue;
950 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400951 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500952 continue;
953 }
954
955 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
956 (int)i, lineno, (unsigned long long)addr);
957 pf->addr = addr;
958
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400959 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500960 /* Continuing, because target line might be inlined. */
961 }
962 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400963 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500964}
965
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400966/* Callback parameter with return value */
967struct dwarf_callback_param {
968 void *data;
969 int retval;
970};
971
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500972static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400973{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400974 struct dwarf_callback_param *param = data;
975 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400976 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400977 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400978
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500979 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400980 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500981 else {
982 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400983 if (dwarf_entrypc(in_die, &addr) != 0) {
984 pr_warning("Failed to get entry pc of %s.\n",
985 dwarf_diename(in_die));
986 param->retval = -ENOENT;
987 return DWARF_CB_ABORT;
988 }
989 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500990 pf->addr += pp->offset;
991 pr_debug("found inline addr: 0x%jx\n",
992 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500993
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400994 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -0400995 if (param->retval < 0)
996 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500997 }
998
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500999 return DWARF_CB_OK;
1000}
1001
1002/* Search function from function name */
1003static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1004{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001005 struct dwarf_callback_param *param = data;
1006 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001007 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001008
1009 /* Check tag and diename */
1010 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
1011 die_compare_name(sp_die, pp->function) != 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001012 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001013
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001014 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001015 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001016 dwarf_decl_line(sp_die, &pf->lno);
1017 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001018 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001019 } else if (!dwarf_func_inline(sp_die)) {
1020 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001021 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001022 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001023 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001024 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
1025 pr_warning("Failed to get entry pc of %s.\n",
1026 dwarf_diename(sp_die));
1027 param->retval = -ENOENT;
1028 return DWARF_CB_ABORT;
1029 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001030 pf->addr += pp->offset;
1031 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001032 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001033 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001034 } else {
1035 struct dwarf_callback_param _param = {.data = (void *)pf,
1036 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001037 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001038 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1039 &_param);
1040 param->retval = _param.retval;
1041 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001042
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001043 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001044}
1045
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001046static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001047{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001048 struct dwarf_callback_param _param = {.data = (void *)pf,
1049 .retval = 0};
1050 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1051 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001052}
1053
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001054/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
1055int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001056 struct kprobe_trace_event **tevs, int max_tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001057{
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001058 struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001059 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001060 Dwarf_Off off, noff;
1061 size_t cuhl;
1062 Dwarf_Die *diep;
1063 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001064 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001065
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001066 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001067 if (pf.tevs == NULL)
1068 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001069 *tevs = pf.tevs;
1070 pf.ntevs = 0;
1071
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001072 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001073 if (!dbg) {
1074 pr_warning("No dwarf info found in the vmlinux - "
1075 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001076 free(pf.tevs);
1077 *tevs = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001078 return -EBADF;
1079 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001080
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001081#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001082 /* Get the call frame information from this dwarf */
1083 pf.cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001084#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001085
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001086 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001087 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001088 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001089 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1090 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001091 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001092 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
1093 if (!diep)
1094 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001095
1096 /* Check if target file is included. */
1097 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001098 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001099 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001100 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001101
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001102 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001103 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001104 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001105 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001106 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001107 else {
1108 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001109 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001110 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001111 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001112 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001113 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001114 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001115 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001116
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001117 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001118}
1119
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001120/* Reverse search */
1121int find_perf_probe_point(int fd, unsigned long addr,
1122 struct perf_probe_point *ppt)
1123{
1124 Dwarf_Die cudie, spdie, indie;
1125 Dwarf *dbg;
1126 Dwarf_Line *line;
1127 Dwarf_Addr laddr, eaddr;
1128 const char *tmp;
1129 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001130 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001131
1132 dbg = dwarf_begin(fd, DWARF_C_READ);
1133 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001134 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001135
1136 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001137 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1138 ret = -EINVAL;
1139 goto end;
1140 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001141
1142 /* Find a corresponding line */
1143 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1144 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001145 if (dwarf_lineaddr(line, &laddr) == 0 &&
1146 (Dwarf_Addr)addr == laddr &&
1147 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001148 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001149 if (tmp) {
1150 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001151 ppt->file = strdup(tmp);
1152 if (ppt->file == NULL) {
1153 ret = -ENOMEM;
1154 goto end;
1155 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001156 found = true;
1157 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001158 }
1159 }
1160
1161 /* Find a corresponding function */
1162 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1163 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001164 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001165 goto end;
1166
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001167 if (ppt->line) {
1168 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1169 &indie)) {
1170 /* addr in an inline function */
1171 tmp = dwarf_diename(&indie);
1172 if (!tmp)
1173 goto end;
1174 ret = dwarf_decl_line(&indie, &lineno);
1175 } else {
1176 if (eaddr == addr) { /* Function entry */
1177 lineno = ppt->line;
1178 ret = 0;
1179 } else
1180 ret = dwarf_decl_line(&spdie, &lineno);
1181 }
1182 if (ret == 0) {
1183 /* Make a relative line number */
1184 ppt->line -= lineno;
1185 goto found;
1186 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001187 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001188 /* We don't have a line number, let's use offset */
1189 ppt->offset = addr - (unsigned long)eaddr;
1190found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001191 ppt->function = strdup(tmp);
1192 if (ppt->function == NULL) {
1193 ret = -ENOMEM;
1194 goto end;
1195 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001196 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001197 }
1198
1199end:
1200 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001201 if (ret >= 0)
1202 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001203 return ret;
1204}
1205
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001206/* Add a line and store the src path */
1207static int line_range_add_line(const char *src, unsigned int lineno,
1208 struct line_range *lr)
1209{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001210 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001211 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001212 lr->path = strdup(src);
1213 if (lr->path == NULL)
1214 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001215 }
1216 return line_list__add_line(&lr->line_list, lineno);
1217}
1218
1219/* Search function declaration lines */
1220static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1221{
1222 struct dwarf_callback_param *param = data;
1223 struct line_finder *lf = param->data;
1224 const char *src;
1225 int lineno;
1226
1227 src = dwarf_decl_file(sp_die);
1228 if (src && strtailcmp(src, lf->fname) != 0)
1229 return DWARF_CB_OK;
1230
1231 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1232 (lf->lno_s > lineno || lf->lno_e < lineno))
1233 return DWARF_CB_OK;
1234
1235 param->retval = line_range_add_line(src, lineno, lf->lr);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001236 if (param->retval < 0)
1237 return DWARF_CB_ABORT;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001238 return DWARF_CB_OK;
1239}
1240
1241static int find_line_range_func_decl_lines(struct line_finder *lf)
1242{
1243 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1244 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1245 return param.retval;
1246}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001247
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001248/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001249static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001250{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001251 Dwarf_Lines *lines;
1252 Dwarf_Line *line;
1253 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001254 Dwarf_Addr addr;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001255 int lineno, ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001256 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001257 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001258
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001259 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001260 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1261 pr_warning("No source lines found in this CU.\n");
1262 return -ENOENT;
1263 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001264
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001265 /* Search probable lines on lines list */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001266 for (i = 0; i < nlines; i++) {
1267 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001268 if (dwarf_lineno(line, &lineno) != 0 ||
1269 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001270 continue;
1271
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001272 if (sp_die) {
1273 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001274 if (dwarf_lineaddr(line, &addr) != 0 ||
1275 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001276 continue;
1277
1278 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001279 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001280 continue;
1281 }
1282
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001283 /* TODO: Get fileno from line, but how? */
1284 src = dwarf_linesrc(line, NULL, NULL);
1285 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001286 continue;
1287
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001288 ret = line_range_add_line(src, lineno, lf->lr);
1289 if (ret < 0)
1290 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001291 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001292
1293 /*
1294 * Dwarf lines doesn't include function declarations. We have to
1295 * check functions list or given function.
1296 */
1297 if (sp_die) {
1298 src = dwarf_decl_file(sp_die);
1299 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1300 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1301 ret = line_range_add_line(src, lineno, lf->lr);
1302 } else
1303 ret = find_line_range_func_decl_lines(lf);
1304
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001305 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001306 if (ret >= 0)
1307 if (!list_empty(&lf->lr->line_list))
1308 ret = lf->found = 1;
1309 else
1310 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001311 else {
1312 free(lf->lr->path);
1313 lf->lr->path = NULL;
1314 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001315 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001316}
1317
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001318static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1319{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001320 struct dwarf_callback_param *param = data;
1321
1322 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001323 return DWARF_CB_ABORT; /* No need to find other instances */
1324}
1325
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001326/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001327static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001328{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001329 struct dwarf_callback_param *param = data;
1330 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001331 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001332
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001333 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1334 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001335 lf->fname = dwarf_decl_file(sp_die);
1336 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001337 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001338 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001339 if (lf->lno_s < 0) /* Overflow */
1340 lf->lno_s = INT_MAX;
1341 lf->lno_e = lr->offset + lr->end;
1342 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001343 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001344 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001345 lr->start = lf->lno_s;
1346 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001347 if (dwarf_func_inline(sp_die)) {
1348 struct dwarf_callback_param _param;
1349 _param.data = (void *)lf;
1350 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001351 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001352 line_range_inline_cb,
1353 &_param);
1354 param->retval = _param.retval;
1355 } else
1356 param->retval = find_line_range_by_line(sp_die, lf);
1357 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001358 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001359 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001360}
1361
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001362static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001363{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001364 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1365 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1366 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001367}
1368
1369int find_line_range(int fd, struct line_range *lr)
1370{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001371 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001372 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001373 Dwarf_Off off = 0, noff;
1374 size_t cuhl;
1375 Dwarf_Die *diep;
1376 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001377
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001378 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001379 if (!dbg) {
1380 pr_warning("No dwarf info found in the vmlinux - "
1381 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1382 return -EBADF;
1383 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001384
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001385 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001386 while (!lf.found && ret >= 0) {
1387 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001388 break;
1389
1390 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001391 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1392 if (!diep)
1393 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001394
1395 /* Check if target file is included. */
1396 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001397 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001398 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001399 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001400
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001401 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001402 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001403 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001404 else {
1405 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001406 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001407 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001408 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001409 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001410 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001411 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001412 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001413 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001414
1415 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001416}
1417