blob: 840f1aabbb748b5cd9ef8c8e8b3cc722067b215e [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 Hiramatsu6a330a32010-07-09 18:29:11 +0900147/* Get DW_AT_comp_dir (should be NULL with older gcc) */
148static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
149{
150 Dwarf_Attribute attr;
151 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
152 return NULL;
153 return dwarf_formstring(&attr);
154}
155
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400156/* Compare diename and tname */
157static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
158{
159 const char *name;
160 name = dwarf_diename(dw_die);
Masami Hiramatsu82175632010-07-09 18:29:17 +0900161 return name ? (strcmp(tname, name) == 0) : false;
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400162}
163
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400164/* Get type die, but skip qualifiers and typedef */
165static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
166{
167 Dwarf_Attribute attr;
168 int tag;
169
170 do {
171 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
172 dwarf_formref_die(&attr, die_mem) == NULL)
173 return NULL;
174
175 tag = dwarf_tag(die_mem);
176 vr_die = die_mem;
177 } while (tag == DW_TAG_const_type ||
178 tag == DW_TAG_restrict_type ||
179 tag == DW_TAG_volatile_type ||
180 tag == DW_TAG_shared_type ||
181 tag == DW_TAG_typedef);
182
183 return die_mem;
184}
185
Masami Hiramatsu49849122010-04-12 13:17:15 -0400186static bool die_is_signed_type(Dwarf_Die *tp_die)
187{
188 Dwarf_Attribute attr;
189 Dwarf_Word ret;
190
191 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
192 dwarf_formudata(&attr, &ret) != 0)
193 return false;
194
195 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
196 ret == DW_ATE_signed_fixed);
197}
198
199static int die_get_byte_size(Dwarf_Die *tp_die)
200{
201 Dwarf_Attribute attr;
202 Dwarf_Word ret;
203
204 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
205 dwarf_formudata(&attr, &ret) != 0)
206 return 0;
207
208 return (int)ret;
209}
210
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300211/* Get data_member_location offset */
212static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
213{
214 Dwarf_Attribute attr;
215 Dwarf_Op *expr;
216 size_t nexpr;
217 int ret;
218
219 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
220 return -ENOENT;
221
222 if (dwarf_formudata(&attr, offs) != 0) {
223 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
224 ret = dwarf_getlocation(&attr, &expr, &nexpr);
225 if (ret < 0 || nexpr == 0)
226 return -ENOENT;
227
228 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
229 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
230 expr[0].atom, nexpr);
231 return -ENOTSUP;
232 }
233 *offs = (Dwarf_Word)expr[0].number;
234 }
235 return 0;
236}
237
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400238/* Return values for die_find callbacks */
239enum {
240 DIE_FIND_CB_FOUND = 0, /* End of Search */
241 DIE_FIND_CB_CHILD = 1, /* Search only children */
242 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
243 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
244};
245
246/* Search a child die */
247static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
248 int (*callback)(Dwarf_Die *, void *),
249 void *data, Dwarf_Die *die_mem)
250{
251 Dwarf_Die child_die;
252 int ret;
253
254 ret = dwarf_child(rt_die, die_mem);
255 if (ret != 0)
256 return NULL;
257
258 do {
259 ret = callback(die_mem, data);
260 if (ret == DIE_FIND_CB_FOUND)
261 return die_mem;
262
263 if ((ret & DIE_FIND_CB_CHILD) &&
264 die_find_child(die_mem, callback, data, &child_die)) {
265 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
266 return die_mem;
267 }
268 } while ((ret & DIE_FIND_CB_SIBLING) &&
269 dwarf_siblingof(die_mem, die_mem) == 0);
270
271 return NULL;
272}
273
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500274struct __addr_die_search_param {
275 Dwarf_Addr addr;
276 Dwarf_Die *die_mem;
277};
278
279static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
280{
281 struct __addr_die_search_param *ad = data;
282
283 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
284 dwarf_haspc(fn_die, ad->addr)) {
285 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
286 return DWARF_CB_ABORT;
287 }
288 return DWARF_CB_OK;
289}
290
291/* Search a real subprogram including this line, */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400292static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
293 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500294{
295 struct __addr_die_search_param ad;
296 ad.addr = addr;
297 ad.die_mem = die_mem;
298 /* dwarf_getscopes can't find subprogram. */
299 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
300 return NULL;
301 else
302 return die_mem;
303}
304
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400305/* die_find callback for inline function search */
306static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
307{
308 Dwarf_Addr *addr = data;
309
310 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
311 dwarf_haspc(die_mem, *addr))
312 return DIE_FIND_CB_FOUND;
313
314 return DIE_FIND_CB_CONTINUE;
315}
316
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500317/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400318static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
319 Dwarf_Die *die_mem)
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500320{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400321 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500322}
323
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400324static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400325{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400326 const char *name = data;
327 int tag;
328
329 tag = dwarf_tag(die_mem);
330 if ((tag == DW_TAG_formal_parameter ||
331 tag == DW_TAG_variable) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900332 die_compare_name(die_mem, name))
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400333 return DIE_FIND_CB_FOUND;
334
335 return DIE_FIND_CB_CONTINUE;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400336}
337
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400338/* Find a variable called 'name' */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500339static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
340 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500341{
Masami Hiramatsu016f2622010-03-16 18:05:58 -0400342 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
343 die_mem);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400344}
345
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400346static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
347{
348 const char *name = data;
349
350 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
Masami Hiramatsu82175632010-07-09 18:29:17 +0900351 die_compare_name(die_mem, name))
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400352 return DIE_FIND_CB_FOUND;
353
354 return DIE_FIND_CB_SIBLING;
355}
356
357/* Find a member called 'name' */
358static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
359 Dwarf_Die *die_mem)
360{
361 return die_find_child(st_die, __die_find_member_cb, (void *)name,
362 die_mem);
363}
364
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400365/*
366 * Probe finder related functions
367 */
368
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530369static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400370{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530371 struct probe_trace_arg_ref *ref;
372 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400373 if (ref != NULL)
374 ref->offset = offs;
375 return ref;
376}
377
378/* Show a location */
379static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
380{
381 Dwarf_Attribute attr;
382 Dwarf_Op *op;
383 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500384 unsigned int regn;
385 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400386 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400387 const char *regs;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530388 struct probe_trace_arg *tvar = pf->tvar;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400389 int ret;
390
391 /* TODO: handle more than 1 exprs */
392 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
393 dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
394 nops == 0) {
395 /* TODO: Support const_value */
396 pr_err("Failed to find the location of %s at this address.\n"
397 " Perhaps, it has been optimized out.\n", pf->pvar->var);
398 return -ENOENT;
399 }
400
401 if (op->atom == DW_OP_addr) {
402 /* Static variables on memory (not stack), make @varname */
403 ret = strlen(dwarf_diename(vr_die));
404 tvar->value = zalloc(ret + 2);
405 if (tvar->value == NULL)
406 return -ENOMEM;
407 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
408 tvar->ref = alloc_trace_arg_ref((long)offs);
409 if (tvar->ref == NULL)
410 return -ENOMEM;
411 return 0;
412 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400413
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400414 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500415 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400416 if (pf->fb_ops == NULL) {
417 pr_warning("The attribute of frame base is not "
418 "supported.\n");
419 return -ENOTSUP;
420 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400421 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500422 offs = op->number;
423 op = &pf->fb_ops[0];
424 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400425
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500426 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
427 regn = op->atom - DW_OP_breg0;
428 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400429 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500430 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
431 regn = op->atom - DW_OP_reg0;
432 } else if (op->atom == DW_OP_bregx) {
433 regn = op->number;
434 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400435 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500436 } else if (op->atom == DW_OP_regx) {
437 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400438 } else {
439 pr_warning("DW_OP %x is not supported.\n", op->atom);
440 return -ENOTSUP;
441 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400442
443 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400444 if (!regs) {
Ian Munsiecd932c52010-04-20 16:58:32 +1000445 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400446 return -ERANGE;
447 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400448
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400449 tvar->value = strdup(regs);
450 if (tvar->value == NULL)
451 return -ENOMEM;
452
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400453 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400454 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400455 if (tvar->ref == NULL)
456 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400457 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400458 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400459}
460
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400461static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530462 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400463 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400464{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530465 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400466 Dwarf_Die type;
467 char buf[16];
468 int ret;
469
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400470 /* TODO: check all types */
471 if (cast && strcmp(cast, "string") != 0) {
472 /* Non string type is OK */
473 tvar->type = strdup(cast);
474 return (tvar->type == NULL) ? -ENOMEM : 0;
475 }
476
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400477 if (die_get_real_type(vr_die, &type) == NULL) {
478 pr_warning("Failed to get a type information of %s.\n",
479 dwarf_diename(vr_die));
480 return -ENOENT;
481 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400482
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400483 pr_debug("%s type is %s.\n",
484 dwarf_diename(vr_die), dwarf_diename(&type));
485
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400486 if (cast && strcmp(cast, "string") == 0) { /* String type */
487 ret = dwarf_tag(&type);
488 if (ret != DW_TAG_pointer_type &&
489 ret != DW_TAG_array_type) {
490 pr_warning("Failed to cast into string: "
491 "%s(%s) is not a pointer nor array.",
492 dwarf_diename(vr_die), dwarf_diename(&type));
493 return -EINVAL;
494 }
495 if (ret == DW_TAG_pointer_type) {
496 if (die_get_real_type(&type, &type) == NULL) {
497 pr_warning("Failed to get a type information.");
498 return -ENOENT;
499 }
500 while (*ref_ptr)
501 ref_ptr = &(*ref_ptr)->next;
502 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530503 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400504 if (*ref_ptr == NULL) {
505 pr_warning("Out of memory error\n");
506 return -ENOMEM;
507 }
508 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900509 if (!die_compare_name(&type, "char") &&
510 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400511 pr_warning("Failed to cast into string: "
512 "%s is not (unsigned) char *.",
513 dwarf_diename(vr_die));
514 return -EINVAL;
515 }
516 tvar->type = strdup(cast);
517 return (tvar->type == NULL) ? -ENOMEM : 0;
518 }
519
Masami Hiramatsu49849122010-04-12 13:17:15 -0400520 ret = die_get_byte_size(&type) * 8;
521 if (ret) {
522 /* Check the bitwidth */
523 if (ret > MAX_BASIC_TYPE_BITS) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400524 pr_info("%s exceeds max-bitwidth."
525 " Cut down to %d bits.\n",
526 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400527 ret = MAX_BASIC_TYPE_BITS;
528 }
529
530 ret = snprintf(buf, 16, "%c%d",
531 die_is_signed_type(&type) ? 's' : 'u', ret);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400532 if (ret < 0 || ret >= 16) {
533 if (ret >= 16)
534 ret = -E2BIG;
535 pr_warning("Failed to convert variable type: %s\n",
536 strerror(-ret));
537 return ret;
538 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400539 tvar->type = strdup(buf);
540 if (tvar->type == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400541 return -ENOMEM;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400542 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400543 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400544}
545
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400546static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400547 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530548 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400549 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400550{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530551 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400552 Dwarf_Die type;
553 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400554 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400555
556 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400557 if (die_get_real_type(vr_die, &type) == NULL) {
558 pr_warning("Failed to get the type of %s.\n", varname);
559 return -ENOENT;
560 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400561 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
562 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400563
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400564 if (field->name[0] == '[' &&
565 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
566 if (field->next)
567 /* Save original type for next field */
568 memcpy(die_mem, &type, sizeof(*die_mem));
569 /* Get the type of this array */
570 if (die_get_real_type(&type, &type) == NULL) {
571 pr_warning("Failed to get the type of %s.\n", varname);
572 return -ENOENT;
573 }
574 pr_debug2("Array real type: (%x)\n",
575 (unsigned)dwarf_dieoffset(&type));
576 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530577 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400578 if (ref == NULL)
579 return -ENOMEM;
580 if (*ref_ptr)
581 (*ref_ptr)->next = ref;
582 else
583 *ref_ptr = ref;
584 }
585 ref->offset += die_get_byte_size(&type) * field->index;
586 if (!field->next)
587 /* Save vr_die for converting types */
588 memcpy(die_mem, vr_die, sizeof(*die_mem));
589 goto next;
590 } else if (tag == DW_TAG_pointer_type) {
591 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400592 if (!field->ref) {
593 pr_err("Semantic error: %s must be referred by '->'\n",
594 field->name);
595 return -EINVAL;
596 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400597 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400598 if (die_get_real_type(&type, &type) == NULL) {
599 pr_warning("Failed to get the type of %s.\n", varname);
600 return -ENOENT;
601 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400602 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400603 if (dwarf_tag(&type) != DW_TAG_structure_type) {
604 pr_warning("%s is not a data structure.\n", varname);
605 return -EINVAL;
606 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400607
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530608 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400609 if (ref == NULL)
610 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400611 if (*ref_ptr)
612 (*ref_ptr)->next = ref;
613 else
614 *ref_ptr = ref;
615 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400616 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400617 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400618 pr_warning("%s is not a data structure.\n", varname);
619 return -EINVAL;
620 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400621 if (field->name[0] == '[') {
622 pr_err("Semantic error: %s is not a pointor nor array.",
623 varname);
624 return -EINVAL;
625 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400626 if (field->ref) {
627 pr_err("Semantic error: %s must be referred by '.'\n",
628 field->name);
629 return -EINVAL;
630 }
631 if (!ref) {
632 pr_warning("Structure on a register is not "
633 "supported yet.\n");
634 return -ENOTSUP;
635 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400636 }
637
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400638 if (die_find_member(&type, field->name, die_mem) == NULL) {
639 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
640 dwarf_diename(&type), field->name);
641 return -EINVAL;
642 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400643
644 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300645 ret = die_get_data_member_location(die_mem, &offs);
646 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400647 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300648 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400649 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400650 ref->offset += (long)offs;
651
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400652next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400653 /* Converting next field */
654 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400655 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300656 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400657 else
658 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400659}
660
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400661/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400662static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400663{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400664 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400665 int ret;
666
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400667 pr_debug("Converting variable %s into trace event.\n",
668 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500669
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400670 ret = convert_variable_location(vr_die, pf);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400671 if (ret == 0 && pf->pvar->field) {
672 ret = convert_variable_fields(vr_die, pf->pvar->var,
673 pf->pvar->field, &pf->tvar->ref,
674 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400675 vr_die = &die_mem;
676 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400677 if (ret == 0)
678 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500679 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400680 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400681}
682
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400683/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400684static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400685{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400686 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400687 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400688 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400689
Masami Hiramatsu48481932010-04-12 13:16:53 -0400690 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400691 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400692 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400693 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
694 if (ret < 0)
695 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400696 ptr = strchr(buf, ':'); /* Change type separator to _ */
697 if (ptr)
698 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400699 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400700 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400701 if (pf->tvar->name == NULL)
702 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400703
704 if (!is_c_varname(pf->pvar->var)) {
705 /* Copy raw parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400706 pf->tvar->value = strdup(pf->pvar->var);
707 if (pf->tvar->value == NULL)
708 return -ENOMEM;
709 else
710 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400711 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400712
713 pr_debug("Searching '%s' variable in context.\n",
714 pf->pvar->var);
715 /* Search child die for local variables and parameters. */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400716 if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
717 ret = convert_variable(&vr_die, pf);
718 else {
719 /* Search upper class */
720 nscopes = dwarf_getscopes_die(sp_die, &scopes);
721 if (nscopes > 0) {
722 ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
723 0, NULL, 0, 0, &vr_die);
724 if (ret >= 0)
725 ret = convert_variable(&vr_die, pf);
726 else
727 ret = -ENOENT;
728 free(scopes);
729 } else
730 ret = -ENOENT;
731 }
732 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400733 pr_warning("Failed to find '%s' in this function.\n",
734 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400735 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400736}
737
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400738/* Show a probe point to output buffer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400739static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400740{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530741 struct probe_trace_event *tev;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500742 Dwarf_Addr eaddr;
743 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500744 const char *name;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400745 int ret, i;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500746 Dwarf_Attribute fb_attr;
747 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400748
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400749 if (pf->ntevs == pf->max_tevs) {
750 pr_warning("Too many( > %d) probe point found.\n",
751 pf->max_tevs);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400752 return -ERANGE;
753 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400754 tev = &pf->tevs[pf->ntevs++];
755
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500756 /* If no real subprogram, find a real one */
757 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400758 sp_die = die_find_real_subprogram(&pf->cu_die,
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500759 pf->addr, &die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400760 if (!sp_die) {
761 pr_warning("Failed to find probe point in any "
762 "functions.\n");
763 return -ENOENT;
764 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500765 }
766
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400767 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500768 name = dwarf_diename(sp_die);
769 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400770 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
771 pr_warning("Failed to get entry pc of %s\n",
772 dwarf_diename(sp_die));
773 return -ENOENT;
774 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400775 tev->point.symbol = strdup(name);
776 if (tev->point.symbol == NULL)
777 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400778 tev->point.offset = (unsigned long)(pf->addr - eaddr);
779 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400780 /* This function has no name. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400781 tev->point.offset = (unsigned long)pf->addr;
782
783 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
784 tev->point.offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400785
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500786 /* Get the frame base attribute/ops */
787 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400788 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400789 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500790 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400791#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400792 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
793 pf->cfi != NULL) {
794 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400795 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
796 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
797 pr_warning("Failed to get CFA on 0x%jx\n",
798 (uintmax_t)pf->addr);
799 return -ENOENT;
800 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400801#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400802 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500803
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400804 /* Find each argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400805 tev->nargs = pf->pev->nargs;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530806 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400807 if (tev->args == NULL)
808 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400809 for (i = 0; i < pf->pev->nargs; i++) {
810 pf->pvar = &pf->pev->args[i];
811 pf->tvar = &tev->args[i];
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400812 ret = find_variable(sp_die, pf);
813 if (ret != 0)
814 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400815 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500816
817 /* *pf->fb_ops will be cached in libdw. Don't free it. */
818 pf->fb_ops = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400819 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400820}
821
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400822/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400823static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400824{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500825 Dwarf_Lines *lines;
826 Dwarf_Line *line;
827 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500828 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500829 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400830 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400831
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400832 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
833 pr_warning("No source lines found in this CU.\n");
834 return -ENOENT;
835 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400836
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400837 for (i = 0; i < nlines && ret == 0; i++) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500838 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400839 if (dwarf_lineno(line, &lineno) != 0 ||
840 lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400841 continue;
842
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500843 /* TODO: Get fileno from line, but how? */
844 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
845 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400846
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400847 if (dwarf_lineaddr(line, &addr) != 0) {
848 pr_warning("Failed to get the address of the line.\n");
849 return -ENOENT;
850 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500851 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
852 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400853 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500854
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400855 ret = convert_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400856 /* Continuing, because target line might be inlined. */
857 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400858 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400859}
860
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500861/* Find lines which match lazy pattern */
862static int find_lazy_match_lines(struct list_head *head,
863 const char *fname, const char *pat)
864{
865 char *fbuf, *p1, *p2;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300866 int fd, line, nlines = -1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500867 struct stat st;
868
869 fd = open(fname, O_RDONLY);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400870 if (fd < 0) {
871 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300872 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400873 }
874
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300875 if (fstat(fd, &st) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400876 pr_warning("Failed to get the size of %s: %s\n",
877 fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300878 nlines = -errno;
879 goto out_close;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400880 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300881
882 nlines = -ENOMEM;
883 fbuf = malloc(st.st_size + 2);
884 if (fbuf == NULL)
885 goto out_close;
886 if (read(fd, fbuf, st.st_size) < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400887 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300888 nlines = -errno;
889 goto out_free_fbuf;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400890 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500891 fbuf[st.st_size] = '\n'; /* Dummy line */
892 fbuf[st.st_size + 1] = '\0';
893 p1 = fbuf;
894 line = 1;
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300895 nlines = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500896 while ((p2 = strchr(p1, '\n')) != NULL) {
897 *p2 = '\0';
898 if (strlazymatch(p1, pat)) {
899 line_list__add_line(head, line);
900 nlines++;
901 }
902 line++;
903 p1 = p2 + 1;
904 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300905out_free_fbuf:
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500906 free(fbuf);
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300907out_close:
908 close(fd);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500909 return nlines;
910}
911
912/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400913static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500914{
915 Dwarf_Lines *lines;
916 Dwarf_Line *line;
917 size_t nlines, i;
918 Dwarf_Addr addr;
919 Dwarf_Die die_mem;
920 int lineno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400921 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500922
923 if (list_empty(&pf->lcache)) {
924 /* Matching lazy line pattern */
925 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400926 pf->pev->point.lazy_line);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400927 if (ret == 0) {
928 pr_debug("No matched lines found in %s.\n", pf->fname);
929 return 0;
930 } else if (ret < 0)
931 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500932 }
933
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400934 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
935 pr_warning("No source lines found in this CU.\n");
936 return -ENOENT;
937 }
938
939 for (i = 0; i < nlines && ret >= 0; i++) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500940 line = dwarf_onesrcline(lines, i);
941
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400942 if (dwarf_lineno(line, &lineno) != 0 ||
943 !line_list__has_line(&pf->lcache, lineno))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500944 continue;
945
946 /* TODO: Get fileno from line, but how? */
947 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
948 continue;
949
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400950 if (dwarf_lineaddr(line, &addr) != 0) {
951 pr_debug("Failed to get the address of line %d.\n",
952 lineno);
953 continue;
954 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500955 if (sp_die) {
956 /* Address filtering 1: does sp_die include addr? */
957 if (!dwarf_haspc(sp_die, addr))
958 continue;
959 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -0400960 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500961 continue;
962 }
963
964 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
965 (int)i, lineno, (unsigned long long)addr);
966 pf->addr = addr;
967
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400968 ret = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500969 /* Continuing, because target line might be inlined. */
970 }
971 /* TODO: deallocate lines, but how? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400972 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500973}
974
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400975/* Callback parameter with return value */
976struct dwarf_callback_param {
977 void *data;
978 int retval;
979};
980
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500981static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400982{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400983 struct dwarf_callback_param *param = data;
984 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400985 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400986 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400987
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500988 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400989 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500990 else {
991 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400992 if (dwarf_entrypc(in_die, &addr) != 0) {
993 pr_warning("Failed to get entry pc of %s.\n",
994 dwarf_diename(in_die));
995 param->retval = -ENOENT;
996 return DWARF_CB_ABORT;
997 }
998 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500999 pf->addr += pp->offset;
1000 pr_debug("found inline addr: 0x%jx\n",
1001 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001002
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001003 param->retval = convert_probe_point(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001004 if (param->retval < 0)
1005 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001006 }
1007
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001008 return DWARF_CB_OK;
1009}
1010
1011/* Search function from function name */
1012static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1013{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001014 struct dwarf_callback_param *param = data;
1015 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001016 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001017
1018 /* Check tag and diename */
1019 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +09001020 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001021 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001022
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001023 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001024 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001025 dwarf_decl_line(sp_die, &pf->lno);
1026 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001027 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001028 } else if (!dwarf_func_inline(sp_die)) {
1029 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001030 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001031 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001032 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001033 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
1034 pr_warning("Failed to get entry pc of %s.\n",
1035 dwarf_diename(sp_die));
1036 param->retval = -ENOENT;
1037 return DWARF_CB_ABORT;
1038 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001039 pf->addr += pp->offset;
1040 /* TODO: Check the address in this function */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001041 param->retval = convert_probe_point(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001042 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001043 } else {
1044 struct dwarf_callback_param _param = {.data = (void *)pf,
1045 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001046 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001047 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1048 &_param);
1049 param->retval = _param.retval;
1050 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001051
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001052 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001053}
1054
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001055static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001056{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001057 struct dwarf_callback_param _param = {.data = (void *)pf,
1058 .retval = 0};
1059 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1060 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001061}
1062
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301063/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1064int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1065 struct probe_trace_event **tevs, int max_tevs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001066{
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001067 struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001068 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001069 Dwarf_Off off, noff;
1070 size_t cuhl;
1071 Dwarf_Die *diep;
1072 Dwarf *dbg;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001073 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001074
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301075 pf.tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001076 if (pf.tevs == NULL)
1077 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001078 *tevs = pf.tevs;
1079 pf.ntevs = 0;
1080
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001081 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001082 if (!dbg) {
1083 pr_warning("No dwarf info found in the vmlinux - "
1084 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -03001085 free(pf.tevs);
1086 *tevs = NULL;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001087 return -EBADF;
1088 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001089
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001090#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001091 /* Get the call frame information from this dwarf */
1092 pf.cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -04001093#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -04001094
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001095 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001096 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001097 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001098 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1099 ret >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001100 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001101 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
1102 if (!diep)
1103 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001104
1105 /* Check if target file is included. */
1106 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001107 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001108 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001109 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001110
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001111 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001112 if (pp->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001113 ret = find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001114 else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001115 ret = find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001116 else {
1117 pf.lno = pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001118 ret = find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001119 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001120 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001121 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001122 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001123 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001124 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001125
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001126 return (ret < 0) ? ret : pf.ntevs;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001127}
1128
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001129/* Reverse search */
1130int find_perf_probe_point(int fd, unsigned long addr,
1131 struct perf_probe_point *ppt)
1132{
1133 Dwarf_Die cudie, spdie, indie;
1134 Dwarf *dbg;
1135 Dwarf_Line *line;
1136 Dwarf_Addr laddr, eaddr;
1137 const char *tmp;
1138 int lineno, ret = 0;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001139 bool found = false;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001140
1141 dbg = dwarf_begin(fd, DWARF_C_READ);
1142 if (!dbg)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001143 return -EBADF;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001144
1145 /* Find cu die */
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001146 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1147 ret = -EINVAL;
1148 goto end;
1149 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001150
1151 /* Find a corresponding line */
1152 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1153 if (line) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001154 if (dwarf_lineaddr(line, &laddr) == 0 &&
1155 (Dwarf_Addr)addr == laddr &&
1156 dwarf_lineno(line, &lineno) == 0) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001157 tmp = dwarf_linesrc(line, NULL, NULL);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001158 if (tmp) {
1159 ppt->line = lineno;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001160 ppt->file = strdup(tmp);
1161 if (ppt->file == NULL) {
1162 ret = -ENOMEM;
1163 goto end;
1164 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001165 found = true;
1166 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001167 }
1168 }
1169
1170 /* Find a corresponding function */
1171 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1172 tmp = dwarf_diename(&spdie);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001173 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001174 goto end;
1175
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001176 if (ppt->line) {
1177 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1178 &indie)) {
1179 /* addr in an inline function */
1180 tmp = dwarf_diename(&indie);
1181 if (!tmp)
1182 goto end;
1183 ret = dwarf_decl_line(&indie, &lineno);
1184 } else {
1185 if (eaddr == addr) { /* Function entry */
1186 lineno = ppt->line;
1187 ret = 0;
1188 } else
1189 ret = dwarf_decl_line(&spdie, &lineno);
1190 }
1191 if (ret == 0) {
1192 /* Make a relative line number */
1193 ppt->line -= lineno;
1194 goto found;
1195 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001196 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001197 /* We don't have a line number, let's use offset */
1198 ppt->offset = addr - (unsigned long)eaddr;
1199found:
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001200 ppt->function = strdup(tmp);
1201 if (ppt->function == NULL) {
1202 ret = -ENOMEM;
1203 goto end;
1204 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001205 found = true;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001206 }
1207
1208end:
1209 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001210 if (ret >= 0)
1211 ret = found ? 1 : 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001212 return ret;
1213}
1214
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001215/* Add a line and store the src path */
1216static int line_range_add_line(const char *src, unsigned int lineno,
1217 struct line_range *lr)
1218{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001219 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001220 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001221 lr->path = strdup(src);
1222 if (lr->path == NULL)
1223 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001224 }
1225 return line_list__add_line(&lr->line_list, lineno);
1226}
1227
1228/* Search function declaration lines */
1229static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1230{
1231 struct dwarf_callback_param *param = data;
1232 struct line_finder *lf = param->data;
1233 const char *src;
1234 int lineno;
1235
1236 src = dwarf_decl_file(sp_die);
1237 if (src && strtailcmp(src, lf->fname) != 0)
1238 return DWARF_CB_OK;
1239
1240 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1241 (lf->lno_s > lineno || lf->lno_e < lineno))
1242 return DWARF_CB_OK;
1243
1244 param->retval = line_range_add_line(src, lineno, lf->lr);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -04001245 if (param->retval < 0)
1246 return DWARF_CB_ABORT;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001247 return DWARF_CB_OK;
1248}
1249
1250static int find_line_range_func_decl_lines(struct line_finder *lf)
1251{
1252 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1253 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1254 return param.retval;
1255}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001256
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001257/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001258static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001259{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001260 Dwarf_Lines *lines;
1261 Dwarf_Line *line;
1262 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001263 Dwarf_Addr addr;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001264 int lineno, ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001265 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001266 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001267
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001268 line_list__init(&lf->lr->line_list);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001269 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1270 pr_warning("No source lines found in this CU.\n");
1271 return -ENOENT;
1272 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001273
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001274 /* Search probable lines on lines list */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001275 for (i = 0; i < nlines; i++) {
1276 line = dwarf_onesrcline(lines, i);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001277 if (dwarf_lineno(line, &lineno) != 0 ||
1278 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001279 continue;
1280
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001281 if (sp_die) {
1282 /* Address filtering 1: does sp_die include addr? */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001283 if (dwarf_lineaddr(line, &addr) != 0 ||
1284 !dwarf_haspc(sp_die, addr))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001285 continue;
1286
1287 /* Address filtering 2: No child include addr? */
Masami Hiramatsu95a3e4c2010-03-16 18:05:51 -04001288 if (die_find_inlinefunc(sp_die, addr, &die_mem))
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001289 continue;
1290 }
1291
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001292 /* TODO: Get fileno from line, but how? */
1293 src = dwarf_linesrc(line, NULL, NULL);
1294 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001295 continue;
1296
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001297 ret = line_range_add_line(src, lineno, lf->lr);
1298 if (ret < 0)
1299 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001300 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001301
1302 /*
1303 * Dwarf lines doesn't include function declarations. We have to
1304 * check functions list or given function.
1305 */
1306 if (sp_die) {
1307 src = dwarf_decl_file(sp_die);
1308 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1309 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1310 ret = line_range_add_line(src, lineno, lf->lr);
1311 } else
1312 ret = find_line_range_func_decl_lines(lf);
1313
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001314 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001315 if (ret >= 0)
1316 if (!list_empty(&lf->lr->line_list))
1317 ret = lf->found = 1;
1318 else
1319 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001320 else {
1321 free(lf->lr->path);
1322 lf->lr->path = NULL;
1323 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001324 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001325}
1326
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001327static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1328{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001329 struct dwarf_callback_param *param = data;
1330
1331 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001332 return DWARF_CB_ABORT; /* No need to find other instances */
1333}
1334
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001335/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001336static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001337{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001338 struct dwarf_callback_param *param = data;
1339 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001340 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001341
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001342 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001343 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001344 lf->fname = dwarf_decl_file(sp_die);
1345 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001346 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001347 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001348 if (lf->lno_s < 0) /* Overflow */
1349 lf->lno_s = INT_MAX;
1350 lf->lno_e = lr->offset + lr->end;
1351 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001352 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001353 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001354 lr->start = lf->lno_s;
1355 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001356 if (dwarf_func_inline(sp_die)) {
1357 struct dwarf_callback_param _param;
1358 _param.data = (void *)lf;
1359 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001360 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001361 line_range_inline_cb,
1362 &_param);
1363 param->retval = _param.retval;
1364 } else
1365 param->retval = find_line_range_by_line(sp_die, lf);
1366 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001367 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001368 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001369}
1370
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001371static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001372{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001373 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1374 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1375 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001376}
1377
1378int find_line_range(int fd, struct line_range *lr)
1379{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001380 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001381 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001382 Dwarf_Off off = 0, noff;
1383 size_t cuhl;
1384 Dwarf_Die *diep;
1385 Dwarf *dbg;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001386 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001387
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001388 dbg = dwarf_begin(fd, DWARF_C_READ);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001389 if (!dbg) {
1390 pr_warning("No dwarf info found in the vmlinux - "
1391 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1392 return -EBADF;
1393 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001394
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001395 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001396 while (!lf.found && ret >= 0) {
1397 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001398 break;
1399
1400 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001401 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1402 if (!diep)
1403 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001404
1405 /* Check if target file is included. */
1406 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001407 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001408 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001409 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001410
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001411 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001412 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001413 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001414 else {
1415 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001416 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001417 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001418 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001419 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001420 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001421 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001422
1423 /* Store comp_dir */
1424 if (lf.found) {
1425 comp_dir = cu_get_comp_dir(&lf.cu_die);
1426 if (comp_dir) {
1427 lr->comp_dir = strdup(comp_dir);
1428 if (!lr->comp_dir)
1429 ret = -ENOMEM;
1430 }
1431 }
1432
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001433 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001434 dwarf_end(dbg);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001435
1436 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001437}
1438