blob: 6c7750da43d766f591817f884d8950f1d7c879f3 [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 Hiramatsu89c69c02009-10-16 20:08:10 -040036#include "event.h"
37#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040038#include "util.h"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040039#include "symbol.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 Hiramatsu6a330a32010-07-09 18:29:11 +0900146/* Get DW_AT_comp_dir (should be NULL with older gcc) */
147static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
148{
149 Dwarf_Attribute attr;
150 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
151 return NULL;
152 return dwarf_formstring(&attr);
153}
154
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400155/* Compare diename and tname */
156static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
157{
158 const char *name;
159 name = dwarf_diename(dw_die);
Masami Hiramatsu82175632010-07-09 18:29:17 +0900160 return name ? (strcmp(tname, name) == 0) : false;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400161}
162
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400163/* Get type die, but skip qualifiers and typedef */
164static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
165{
166 Dwarf_Attribute attr;
167 int tag;
168
169 do {
170 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
171 dwarf_formref_die(&attr, die_mem) == NULL)
172 return NULL;
173
174 tag = dwarf_tag(die_mem);
175 vr_die = die_mem;
176 } while (tag == DW_TAG_const_type ||
177 tag == DW_TAG_restrict_type ||
178 tag == DW_TAG_volatile_type ||
179 tag == DW_TAG_shared_type ||
180 tag == DW_TAG_typedef);
181
182 return die_mem;
183}
184
Masami Hiramatsu49849122010-04-12 13:17:15 -0400185static bool die_is_signed_type(Dwarf_Die *tp_die)
186{
187 Dwarf_Attribute attr;
188 Dwarf_Word ret;
189
190 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
191 dwarf_formudata(&attr, &ret) != 0)
192 return false;
193
194 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
195 ret == DW_ATE_signed_fixed);
196}
197
198static int die_get_byte_size(Dwarf_Die *tp_die)
199{
200 Dwarf_Attribute attr;
201 Dwarf_Word ret;
202
203 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
204 dwarf_formudata(&attr, &ret) != 0)
205 return 0;
206
207 return (int)ret;
208}
209
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300210/* Get data_member_location offset */
211static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
212{
213 Dwarf_Attribute attr;
214 Dwarf_Op *expr;
215 size_t nexpr;
216 int ret;
217
218 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
219 return -ENOENT;
220
221 if (dwarf_formudata(&attr, offs) != 0) {
222 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
223 ret = dwarf_getlocation(&attr, &expr, &nexpr);
224 if (ret < 0 || nexpr == 0)
225 return -ENOENT;
226
227 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
228 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
229 expr[0].atom, nexpr);
230 return -ENOTSUP;
231 }
232 *offs = (Dwarf_Word)expr[0].number;
233 }
234 return 0;
235}
236
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400237/* Return values for die_find callbacks */
238enum {
239 DIE_FIND_CB_FOUND = 0, /* End of Search */
240 DIE_FIND_CB_CHILD = 1, /* Search only children */
241 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
242 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
243};
244
245/* Search a child die */
246static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
247 int (*callback)(Dwarf_Die *, void *),
248 void *data, Dwarf_Die *die_mem)
249{
250 Dwarf_Die child_die;
251 int ret;
252
253 ret = dwarf_child(rt_die, die_mem);
254 if (ret != 0)
255 return NULL;
256
257 do {
258 ret = callback(die_mem, data);
259 if (ret == DIE_FIND_CB_FOUND)
260 return die_mem;
261
262 if ((ret & DIE_FIND_CB_CHILD) &&
263 die_find_child(die_mem, callback, data, &child_die)) {
264 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
265 return die_mem;
266 }
267 } while ((ret & DIE_FIND_CB_SIBLING) &&
268 dwarf_siblingof(die_mem, die_mem) == 0);
269
270 return NULL;
271}
272
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500273struct __addr_die_search_param {
274 Dwarf_Addr addr;
275 Dwarf_Die *die_mem;
276};
277
278static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
279{
280 struct __addr_die_search_param *ad = data;
281
282 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
283 dwarf_haspc(fn_die, ad->addr)) {
284 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
285 return DWARF_CB_ABORT;
286 }
287 return DWARF_CB_OK;
288}
289
290/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400291static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
292 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500293{
294 struct __addr_die_search_param ad;
295 ad.addr = addr;
296 ad.die_mem = die_mem;
297 /* dwarf_getscopes can't find subprogram. */
298 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
299 return NULL;
300 else
301 return die_mem;
302}
303
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400304/* die_find callback for inline function search */
305static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
306{
307 Dwarf_Addr *addr = data;
308
309 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
310 dwarf_haspc(die_mem, *addr))
311 return DIE_FIND_CB_FOUND;
312
313 return DIE_FIND_CB_CONTINUE;
314}
315
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500316/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400317static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
318 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500319{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400320 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500321}
322
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400323static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400324{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400325 const char *name = data;
326 int tag;
327
328 tag = dwarf_tag(die_mem);
329 if ((tag == DW_TAG_formal_parameter ||
330 tag == DW_TAG_variable) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900331 die_compare_name(die_mem, name))
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400332 return DIE_FIND_CB_FOUND;
333
334 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400335}
336
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400337/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500338static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
339 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500340{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400341 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
342 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400343}
344
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400345static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
346{
347 const char *name = data;
348
349 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900350 die_compare_name(die_mem, name))
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400351 return DIE_FIND_CB_FOUND;
352
353 return DIE_FIND_CB_SIBLING;
354}
355
356/* Find a member called 'name' */
357static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
358 Dwarf_Die *die_mem)
359{
360 return die_find_child(st_die, __die_find_member_cb, (void *)name,
361 die_mem);
362}
363
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400364/*
365 * Probe finder related functions
366 */
367
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530368static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400369{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530370 struct probe_trace_arg_ref *ref;
371 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400372 if (ref != NULL)
373 ref->offset = offs;
374 return ref;
375}
376
377/* Show a location */
378static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
379{
380 Dwarf_Attribute attr;
381 Dwarf_Op *op;
382 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500383 unsigned int regn;
384 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400385 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400386 const char *regs;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530387 struct probe_trace_arg *tvar = pf->tvar;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400388 int ret;
389
390 /* TODO: handle more than 1 exprs */
391 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
392 dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
393 nops == 0) {
394 /* TODO: Support const_value */
395 pr_err("Failed to find the location of %s at this address.\n"
396 " Perhaps, it has been optimized out.\n", pf->pvar->var);
397 return -ENOENT;
398 }
399
400 if (op->atom == DW_OP_addr) {
401 /* Static variables on memory (not stack), make @varname */
402 ret = strlen(dwarf_diename(vr_die));
403 tvar->value = zalloc(ret + 2);
404 if (tvar->value == NULL)
405 return -ENOMEM;
406 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
407 tvar->ref = alloc_trace_arg_ref((long)offs);
408 if (tvar->ref == NULL)
409 return -ENOMEM;
410 return 0;
411 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400412
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400413 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500414 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400415 if (pf->fb_ops == NULL) {
416 pr_warning("The attribute of frame base is not "
417 "supported.\n");
418 return -ENOTSUP;
419 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400420 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500421 offs = op->number;
422 op = &pf->fb_ops[0];
423 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400424
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500425 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
426 regn = op->atom - DW_OP_breg0;
427 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400428 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500429 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
430 regn = op->atom - DW_OP_reg0;
431 } else if (op->atom == DW_OP_bregx) {
432 regn = op->number;
433 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400434 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500435 } else if (op->atom == DW_OP_regx) {
436 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400437 } else {
438 pr_warning("DW_OP %x is not supported.\n", op->atom);
439 return -ENOTSUP;
440 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400441
442 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400443 if (!regs) {
Ian Munsiecd932c52010-04-20 16:58:32 +1000444 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400445 return -ERANGE;
446 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400447
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400448 tvar->value = strdup(regs);
449 if (tvar->value == NULL)
450 return -ENOMEM;
451
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400452 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400453 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400454 if (tvar->ref == NULL)
455 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400456 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400457 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400458}
459
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400460static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530461 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400462 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400463{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530464 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400465 Dwarf_Die type;
466 char buf[16];
467 int ret;
468
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400469 /* TODO: check all types */
470 if (cast && strcmp(cast, "string") != 0) {
471 /* Non string type is OK */
472 tvar->type = strdup(cast);
473 return (tvar->type == NULL) ? -ENOMEM : 0;
474 }
475
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400476 if (die_get_real_type(vr_die, &type) == NULL) {
477 pr_warning("Failed to get a type information of %s.\n",
478 dwarf_diename(vr_die));
479 return -ENOENT;
480 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400481
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400482 pr_debug("%s type is %s.\n",
483 dwarf_diename(vr_die), dwarf_diename(&type));
484
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400485 if (cast && strcmp(cast, "string") == 0) { /* String type */
486 ret = dwarf_tag(&type);
487 if (ret != DW_TAG_pointer_type &&
488 ret != DW_TAG_array_type) {
489 pr_warning("Failed to cast into string: "
490 "%s(%s) is not a pointer nor array.",
491 dwarf_diename(vr_die), dwarf_diename(&type));
492 return -EINVAL;
493 }
494 if (ret == DW_TAG_pointer_type) {
495 if (die_get_real_type(&type, &type) == NULL) {
496 pr_warning("Failed to get a type information.");
497 return -ENOENT;
498 }
499 while (*ref_ptr)
500 ref_ptr = &(*ref_ptr)->next;
501 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530502 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400503 if (*ref_ptr == NULL) {
504 pr_warning("Out of memory error\n");
505 return -ENOMEM;
506 }
507 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900508 if (!die_compare_name(&type, "char") &&
509 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400510 pr_warning("Failed to cast into string: "
511 "%s is not (unsigned) char *.",
512 dwarf_diename(vr_die));
513 return -EINVAL;
514 }
515 tvar->type = strdup(cast);
516 return (tvar->type == NULL) ? -ENOMEM : 0;
517 }
518
Masami Hiramatsu49849122010-04-12 13:17:15 -0400519 ret = die_get_byte_size(&type) * 8;
520 if (ret) {
521 /* Check the bitwidth */
522 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400523 pr_info("%s exceeds max-bitwidth."
524 " Cut down to %d bits.\n",
525 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400526 ret = MAX_BASIC_TYPE_BITS;
527 }
528
529 ret = snprintf(buf, 16, "%c%d",
530 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400531 if (ret < 0 || ret >= 16) {
532 if (ret >= 16)
533 ret = -E2BIG;
534 pr_warning("Failed to convert variable type: %s\n",
535 strerror(-ret));
536 return ret;
537 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400538 tvar->type = strdup(buf);
539 if (tvar->type == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400540 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400541 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400542 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400543}
544
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400545static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400546 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530547 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400548 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400549{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530550 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400551 Dwarf_Die type;
552 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400553 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400554
555 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400556 if (die_get_real_type(vr_die, &type) == NULL) {
557 pr_warning("Failed to get the type of %s.\n", varname);
558 return -ENOENT;
559 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400560 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
561 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400562
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400563 if (field->name[0] == '[' &&
564 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
565 if (field->next)
566 /* Save original type for next field */
567 memcpy(die_mem, &type, sizeof(*die_mem));
568 /* Get the type of this array */
569 if (die_get_real_type(&type, &type) == NULL) {
570 pr_warning("Failed to get the type of %s.\n", varname);
571 return -ENOENT;
572 }
573 pr_debug2("Array real type: (%x)\n",
574 (unsigned)dwarf_dieoffset(&type));
575 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530576 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400577 if (ref == NULL)
578 return -ENOMEM;
579 if (*ref_ptr)
580 (*ref_ptr)->next = ref;
581 else
582 *ref_ptr = ref;
583 }
584 ref->offset += die_get_byte_size(&type) * field->index;
585 if (!field->next)
586 /* Save vr_die for converting types */
587 memcpy(die_mem, vr_die, sizeof(*die_mem));
588 goto next;
589 } else if (tag == DW_TAG_pointer_type) {
590 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400591 if (!field->ref) {
592 pr_err("Semantic error: %s must be referred by '->'\n",
593 field->name);
594 return -EINVAL;
595 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400596 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400597 if (die_get_real_type(&type, &type) == NULL) {
598 pr_warning("Failed to get the type of %s.\n", varname);
599 return -ENOENT;
600 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400601 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400602 if (dwarf_tag(&type) != DW_TAG_structure_type) {
603 pr_warning("%s is not a data structure.\n", varname);
604 return -EINVAL;
605 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400606
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530607 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400608 if (ref == NULL)
609 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400610 if (*ref_ptr)
611 (*ref_ptr)->next = ref;
612 else
613 *ref_ptr = ref;
614 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400615 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400616 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400617 pr_warning("%s is not a data structure.\n", varname);
618 return -EINVAL;
619 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400620 if (field->name[0] == '[') {
621 pr_err("Semantic error: %s is not a pointor nor array.",
622 varname);
623 return -EINVAL;
624 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400625 if (field->ref) {
626 pr_err("Semantic error: %s must be referred by '.'\n",
627 field->name);
628 return -EINVAL;
629 }
630 if (!ref) {
631 pr_warning("Structure on a register is not "
632 "supported yet.\n");
633 return -ENOTSUP;
634 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400635 }
636
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400637 if (die_find_member(&type, field->name, die_mem) == NULL) {
638 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
639 dwarf_diename(&type), field->name);
640 return -EINVAL;
641 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400642
643 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300644 ret = die_get_data_member_location(die_mem, &offs);
645 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400646 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300647 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400648 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400649 ref->offset += (long)offs;
650
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400651next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400652 /* Converting next field */
653 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400654 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300655 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400656 else
657 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400658}
659
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400660/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400661static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400662{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400663 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400664 int ret;
665
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400666 pr_debug("Converting variable %s into trace event.\n",
667 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500668
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400669 ret = convert_variable_location(vr_die, pf);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400670 if (ret == 0 && pf->pvar->field) {
671 ret = convert_variable_fields(vr_die, pf->pvar->var,
672 pf->pvar->field, &pf->tvar->ref,
673 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400674 vr_die = &die_mem;
675 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400676 if (ret == 0)
677 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500678 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400679 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400680}
681
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400682/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400683static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400684{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400685 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400686 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400687 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400688
Masami Hiramatsu48481932010-04-12 13:16:53 -0400689 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400690 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400691 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400692 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
693 if (ret < 0)
694 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400695 ptr = strchr(buf, ':'); /* Change type separator to _ */
696 if (ptr)
697 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400698 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400699 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400700 if (pf->tvar->name == NULL)
701 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400702
703 if (!is_c_varname(pf->pvar->var)) {
704 /* Copy raw parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400705 pf->tvar->value = strdup(pf->pvar->var);
706 if (pf->tvar->value == NULL)
707 return -ENOMEM;
708 else
709 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400710 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400711
712 pr_debug("Searching '%s' variable in context.\n",
713 pf->pvar->var);
714 /* Search child die for local variables and parameters. */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400715 if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
716 ret = convert_variable(&vr_die, pf);
717 else {
718 /* Search upper class */
719 nscopes = dwarf_getscopes_die(sp_die, &scopes);
720 if (nscopes > 0) {
721 ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
722 0, NULL, 0, 0, &vr_die);
723 if (ret >= 0)
724 ret = convert_variable(&vr_die, pf);
725 else
726 ret = -ENOENT;
727 free(scopes);
728 } else
729 ret = -ENOENT;
730 }
731 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400732 pr_warning("Failed to find '%s' in this function.\n",
733 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400734 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400735}
736
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400737/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400738static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400739{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530740 struct probe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500741 Dwarf_Addr eaddr;
742 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500743 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400744 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500745 Dwarf_Attribute fb_attr;
746 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400747
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400748 if (pf->ntevs == pf->max_tevs) {
749 pr_warning("Too many( > %d) probe point found.\n",
750 pf->max_tevs);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400751 return -ERANGE;
752 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400753 tev = &pf->tevs[pf->ntevs++];
754
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500755 /* If no real subprogram, find a real one */
756 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400757 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500758 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400759 if (!sp_die) {
760 pr_warning("Failed to find probe point in any "
761 "functions.\n");
762 return -ENOENT;
763 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500764 }
765
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400766 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500767 name = dwarf_diename(sp_die);
768 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400769 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
770 pr_warning("Failed to get entry pc of %s\n",
771 dwarf_diename(sp_die));
772 return -ENOENT;
773 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400774 tev->point.symbol = strdup(name);
775 if (tev->point.symbol == NULL)
776 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400777 tev->point.offset = (unsigned long)(pf->addr - eaddr);
778 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400779 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400780 tev->point.offset = (unsigned long)pf->addr;
781
782 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
783 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400784
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500785 /* Get the frame base attribute/ops */
786 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400787 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400788 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500789 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400790#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400791 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
792 pf->cfi != NULL) {
793 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400794 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
795 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
796 pr_warning("Failed to get CFA on 0x%jx\n",
797 (uintmax_t)pf->addr);
798 return -ENOENT;
799 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400800#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400801 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500802
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400803 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400804 tev->nargs = pf->pev->nargs;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530805 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400806 if (tev->args == NULL)
807 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400808 for (i = 0; i < pf->pev->nargs; i++) {
809 pf->pvar = &pf->pev->args[i];
810 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400811 ret = find_variable(sp_die, pf);
812 if (ret != 0)
813 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400814 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500815
816 /* *pf->fb_ops will be cached in libdw. Don't free it. */
817 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400818 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400819}
820
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400821/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400822static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400823{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500824 Dwarf_Lines *lines;
825 Dwarf_Line *line;
826 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500827 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500828 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400829 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400830
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400831 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
832 pr_warning("No source lines found in this CU.\n");
833 return -ENOENT;
834 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400835
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400836 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500837 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400838 if (dwarf_lineno(line, &lineno) != 0 ||
839 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400840 continue;
841
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500842 /* TODO: Get fileno from line, but how? */
843 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
844 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400845
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400846 if (dwarf_lineaddr(line, &addr) != 0) {
847 pr_warning("Failed to get the address of the line.\n");
848 return -ENOENT;
849 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500850 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
851 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400852 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500853
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400854 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400855 /* Continuing, because target line might be inlined. */
856 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400857 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400858}
859
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500860/* Find lines which match lazy pattern */
861static int find_lazy_match_lines(struct list_head *head,
862 const char *fname, const char *pat)
863{
864 char *fbuf, *p1, *p2;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300865 int fd, line, nlines = -1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500866 struct stat st;
867
868 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400869 if (fd < 0) {
870 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300871 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400872 }
873
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300874 if (fstat(fd, &st) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400875 pr_warning("Failed to get the size of %s: %s\n",
876 fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300877 nlines = -errno;
878 goto out_close;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400879 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300880
881 nlines = -ENOMEM;
882 fbuf = malloc(st.st_size + 2);
883 if (fbuf == NULL)
884 goto out_close;
885 if (read(fd, fbuf, st.st_size) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400886 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300887 nlines = -errno;
888 goto out_free_fbuf;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400889 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500890 fbuf[st.st_size] = '\n'; /* Dummy line */
891 fbuf[st.st_size + 1] = '\0';
892 p1 = fbuf;
893 line = 1;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300894 nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500895 while ((p2 = strchr(p1, '\n')) != NULL) {
896 *p2 = '\0';
897 if (strlazymatch(p1, pat)) {
898 line_list__add_line(head, line);
899 nlines++;
900 }
901 line++;
902 p1 = p2 + 1;
903 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300904out_free_fbuf:
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500905 free(fbuf);
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300906out_close:
907 close(fd);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500908 return nlines;
909}
910
911/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400912static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500913{
914 Dwarf_Lines *lines;
915 Dwarf_Line *line;
916 size_t nlines, i;
917 Dwarf_Addr addr;
918 Dwarf_Die die_mem;
919 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400920 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500921
922 if (list_empty(&pf->lcache)) {
923 /* Matching lazy line pattern */
924 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400925 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400926 if (ret == 0) {
927 pr_debug("No matched lines found in %s.\n", pf->fname);
928 return 0;
929 } else if (ret < 0)
930 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500931 }
932
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400933 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
934 pr_warning("No source lines found in this CU.\n");
935 return -ENOENT;
936 }
937
938 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500939 line = dwarf_onesrcline(lines, i);
940
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400941 if (dwarf_lineno(line, &lineno) != 0 ||
942 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500943 continue;
944
945 /* TODO: Get fileno from line, but how? */
946 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
947 continue;
948
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400949 if (dwarf_lineaddr(line, &addr) != 0) {
950 pr_debug("Failed to get the address of line %d.\n",
951 lineno);
952 continue;
953 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500954 if (sp_die) {
955 /* Address filtering 1: does sp_die include addr? */
956 if (!dwarf_haspc(sp_die, addr))
957 continue;
958 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400959 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500960 continue;
961 }
962
963 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
964 (int)i, lineno, (unsigned long long)addr);
965 pf->addr = addr;
966
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400967 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500968 /* Continuing, because target line might be inlined. */
969 }
970 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400971 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500972}
973
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400974/* Callback parameter with return value */
975struct dwarf_callback_param {
976 void *data;
977 int retval;
978};
979
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500980static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400981{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400982 struct dwarf_callback_param *param = data;
983 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400984 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400985 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400986
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500987 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400988 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500989 else {
990 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400991 if (dwarf_entrypc(in_die, &addr) != 0) {
992 pr_warning("Failed to get entry pc of %s.\n",
993 dwarf_diename(in_die));
994 param->retval = -ENOENT;
995 return DWARF_CB_ABORT;
996 }
997 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500998 pf->addr += pp->offset;
999 pr_debug("found inline addr: 0x%jx\n",
1000 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001001
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001002 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001003 if (param->retval < 0)
1004 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001005 }
1006
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001007 return DWARF_CB_OK;
1008}
1009
1010/* Search function from function name */
1011static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1012{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001013 struct dwarf_callback_param *param = data;
1014 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001015 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001016
1017 /* Check tag and diename */
1018 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001019 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001020 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001021
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001022 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001023 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001024 dwarf_decl_line(sp_die, &pf->lno);
1025 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001026 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001027 } else if (!dwarf_func_inline(sp_die)) {
1028 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001029 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001030 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001031 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001032 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
1033 pr_warning("Failed to get entry pc of %s.\n",
1034 dwarf_diename(sp_die));
1035 param->retval = -ENOENT;
1036 return DWARF_CB_ABORT;
1037 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001038 pf->addr += pp->offset;
1039 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001040 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001041 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001042 } else {
1043 struct dwarf_callback_param _param = {.data = (void *)pf,
1044 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001045 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001046 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1047 &_param);
1048 param->retval = _param.retval;
1049 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001050
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001051 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001052}
1053
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001054static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001055{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001056 struct dwarf_callback_param _param = {.data = (void *)pf,
1057 .retval = 0};
1058 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1059 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001060}
1061
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301062/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1063int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1064 struct probe_trace_event **tevs, int max_tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001065{
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001066 struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001067 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001068 Dwarf_Off off, noff;
1069 size_t cuhl;
1070 Dwarf_Die *diep;
1071 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001072 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001073
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301074 pf.tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001075 if (pf.tevs == NULL)
1076 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001077 *tevs = pf.tevs;
1078 pf.ntevs = 0;
1079
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001080 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001081 if (!dbg) {
1082 pr_warning("No dwarf info found in the vmlinux - "
1083 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001084 free(pf.tevs);
1085 *tevs = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001086 return -EBADF;
1087 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001088
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001089#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001090 /* Get the call frame information from this dwarf */
1091 pf.cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001092#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001093
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001094 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001095 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001096 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001097 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1098 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001099 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001100 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
1101 if (!diep)
1102 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001103
1104 /* Check if target file is included. */
1105 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001106 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001107 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001108 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001109
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001110 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001111 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001112 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001113 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001114 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001115 else {
1116 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001117 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001118 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001119 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001120 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001121 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001122 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001123 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001124
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001125 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001126}
1127
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001128/* Reverse search */
1129int find_perf_probe_point(int fd, unsigned long addr,
1130 struct perf_probe_point *ppt)
1131{
1132 Dwarf_Die cudie, spdie, indie;
1133 Dwarf *dbg;
1134 Dwarf_Line *line;
1135 Dwarf_Addr laddr, eaddr;
1136 const char *tmp;
1137 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001138 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001139
1140 dbg = dwarf_begin(fd, DWARF_C_READ);
1141 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001142 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001143
1144 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001145 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1146 ret = -EINVAL;
1147 goto end;
1148 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001149
1150 /* Find a corresponding line */
1151 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1152 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001153 if (dwarf_lineaddr(line, &laddr) == 0 &&
1154 (Dwarf_Addr)addr == laddr &&
1155 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001156 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001157 if (tmp) {
1158 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001159 ppt->file = strdup(tmp);
1160 if (ppt->file == NULL) {
1161 ret = -ENOMEM;
1162 goto end;
1163 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001164 found = true;
1165 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001166 }
1167 }
1168
1169 /* Find a corresponding function */
1170 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1171 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001172 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001173 goto end;
1174
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001175 if (ppt->line) {
1176 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1177 &indie)) {
1178 /* addr in an inline function */
1179 tmp = dwarf_diename(&indie);
1180 if (!tmp)
1181 goto end;
1182 ret = dwarf_decl_line(&indie, &lineno);
1183 } else {
1184 if (eaddr == addr) { /* Function entry */
1185 lineno = ppt->line;
1186 ret = 0;
1187 } else
1188 ret = dwarf_decl_line(&spdie, &lineno);
1189 }
1190 if (ret == 0) {
1191 /* Make a relative line number */
1192 ppt->line -= lineno;
1193 goto found;
1194 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001195 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001196 /* We don't have a line number, let's use offset */
1197 ppt->offset = addr - (unsigned long)eaddr;
1198found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001199 ppt->function = strdup(tmp);
1200 if (ppt->function == NULL) {
1201 ret = -ENOMEM;
1202 goto end;
1203 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001204 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001205 }
1206
1207end:
1208 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001209 if (ret >= 0)
1210 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001211 return ret;
1212}
1213
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001214/* Add a line and store the src path */
1215static int line_range_add_line(const char *src, unsigned int lineno,
1216 struct line_range *lr)
1217{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001218 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001219 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001220 lr->path = strdup(src);
1221 if (lr->path == NULL)
1222 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001223 }
1224 return line_list__add_line(&lr->line_list, lineno);
1225}
1226
1227/* Search function declaration lines */
1228static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1229{
1230 struct dwarf_callback_param *param = data;
1231 struct line_finder *lf = param->data;
1232 const char *src;
1233 int lineno;
1234
1235 src = dwarf_decl_file(sp_die);
1236 if (src && strtailcmp(src, lf->fname) != 0)
1237 return DWARF_CB_OK;
1238
1239 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1240 (lf->lno_s > lineno || lf->lno_e < lineno))
1241 return DWARF_CB_OK;
1242
1243 param->retval = line_range_add_line(src, lineno, lf->lr);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001244 if (param->retval < 0)
1245 return DWARF_CB_ABORT;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001246 return DWARF_CB_OK;
1247}
1248
1249static int find_line_range_func_decl_lines(struct line_finder *lf)
1250{
1251 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1252 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1253 return param.retval;
1254}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001255
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001256/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001257static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001258{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001259 Dwarf_Lines *lines;
1260 Dwarf_Line *line;
1261 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001262 Dwarf_Addr addr;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001263 int lineno, ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001264 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001265 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001266
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001267 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001268 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1269 pr_warning("No source lines found in this CU.\n");
1270 return -ENOENT;
1271 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001272
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001273 /* Search probable lines on lines list */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001274 for (i = 0; i < nlines; i++) {
1275 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001276 if (dwarf_lineno(line, &lineno) != 0 ||
1277 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001278 continue;
1279
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001280 if (sp_die) {
1281 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001282 if (dwarf_lineaddr(line, &addr) != 0 ||
1283 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001284 continue;
1285
1286 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001287 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001288 continue;
1289 }
1290
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001291 /* TODO: Get fileno from line, but how? */
1292 src = dwarf_linesrc(line, NULL, NULL);
1293 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001294 continue;
1295
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001296 ret = line_range_add_line(src, lineno, lf->lr);
1297 if (ret < 0)
1298 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001299 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001300
1301 /*
1302 * Dwarf lines doesn't include function declarations. We have to
1303 * check functions list or given function.
1304 */
1305 if (sp_die) {
1306 src = dwarf_decl_file(sp_die);
1307 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1308 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1309 ret = line_range_add_line(src, lineno, lf->lr);
1310 } else
1311 ret = find_line_range_func_decl_lines(lf);
1312
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001313 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001314 if (ret >= 0)
1315 if (!list_empty(&lf->lr->line_list))
1316 ret = lf->found = 1;
1317 else
1318 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001319 else {
1320 free(lf->lr->path);
1321 lf->lr->path = NULL;
1322 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001323 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001324}
1325
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001326static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1327{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001328 struct dwarf_callback_param *param = data;
1329
1330 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001331 return DWARF_CB_ABORT; /* No need to find other instances */
1332}
1333
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001334/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001335static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001336{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001337 struct dwarf_callback_param *param = data;
1338 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001339 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001340
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001341 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001342 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001343 lf->fname = dwarf_decl_file(sp_die);
1344 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001345 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001346 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001347 if (lf->lno_s < 0) /* Overflow */
1348 lf->lno_s = INT_MAX;
1349 lf->lno_e = lr->offset + lr->end;
1350 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001351 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001352 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001353 lr->start = lf->lno_s;
1354 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001355 if (dwarf_func_inline(sp_die)) {
1356 struct dwarf_callback_param _param;
1357 _param.data = (void *)lf;
1358 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001359 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001360 line_range_inline_cb,
1361 &_param);
1362 param->retval = _param.retval;
1363 } else
1364 param->retval = find_line_range_by_line(sp_die, lf);
1365 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001366 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001367 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001368}
1369
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001370static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001371{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001372 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1373 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1374 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001375}
1376
1377int find_line_range(int fd, struct line_range *lr)
1378{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001379 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001380 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001381 Dwarf_Off off = 0, noff;
1382 size_t cuhl;
1383 Dwarf_Die *diep;
1384 Dwarf *dbg;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001385 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001386
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001387 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001388 if (!dbg) {
1389 pr_warning("No dwarf info found in the vmlinux - "
1390 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1391 return -EBADF;
1392 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001393
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001394 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001395 while (!lf.found && ret >= 0) {
1396 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001397 break;
1398
1399 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001400 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1401 if (!diep)
1402 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001403
1404 /* Check if target file is included. */
1405 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001406 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001407 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001408 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001409
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001410 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001411 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001412 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001413 else {
1414 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001415 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001416 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001417 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001418 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001419 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001420 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001421
1422 /* Store comp_dir */
1423 if (lf.found) {
1424 comp_dir = cu_get_comp_dir(&lf.cu_die);
1425 if (comp_dir) {
1426 lr->comp_dir = strdup(comp_dir);
1427 if (!lr->comp_dir)
1428 ret = -ENOMEM;
1429 }
1430 }
1431
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001432 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001433 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001434
1435 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001436}
1437