Commit | Line | Data |
---|---|---|
4ea42b18 MH |
1 | /* |
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> | |
cd932c59 | 34 | #include <dwarf-regs.h> |
074fc0e4 | 35 | |
2a9c8c36 | 36 | #include "string.h" |
89c69c0e MH |
37 | #include "event.h" |
38 | #include "debug.h" | |
074fc0e4 | 39 | #include "util.h" |
9ed7e1b8 | 40 | #include "symbol.h" |
4ea42b18 MH |
41 | #include "probe-finder.h" |
42 | ||
4984912e MH |
43 | /* Kprobe tracer basic type is up to u64 */ |
44 | #define MAX_BASIC_TYPE_BITS 64 | |
45 | ||
4ea42b18 MH |
46 | /* |
47 | * Compare the tail of two strings. | |
48 | * Return 0 if whole of either string is same as another's tail part. | |
49 | */ | |
50 | static int strtailcmp(const char *s1, const char *s2) | |
51 | { | |
52 | int i1 = strlen(s1); | |
53 | int i2 = strlen(s2); | |
d56728b8 | 54 | while (--i1 >= 0 && --i2 >= 0) { |
4ea42b18 MH |
55 | if (s1[i1] != s2[i2]) |
56 | return s1[i1] - s2[i2]; | |
57 | } | |
58 | return 0; | |
59 | } | |
60 | ||
9ed7e1b8 CD |
61 | /* |
62 | * Find a src file from a DWARF tag path. Prepend optional source path prefix | |
63 | * and chop off leading directories that do not exist. Result is passed back as | |
64 | * a newly allocated path on success. | |
65 | * Return 0 if file was found and readable, -errno otherwise. | |
66 | */ | |
67 | static int get_real_path(const char *raw_path, char **new_path) | |
68 | { | |
69 | if (!symbol_conf.source_prefix) { | |
70 | if (access(raw_path, R_OK) == 0) { | |
71 | *new_path = strdup(raw_path); | |
72 | return 0; | |
73 | } else | |
74 | return -errno; | |
75 | } | |
76 | ||
77 | *new_path = malloc((strlen(symbol_conf.source_prefix) + | |
78 | strlen(raw_path) + 2)); | |
79 | if (!*new_path) | |
80 | return -ENOMEM; | |
81 | ||
82 | for (;;) { | |
83 | sprintf(*new_path, "%s/%s", symbol_conf.source_prefix, | |
84 | raw_path); | |
85 | ||
86 | if (access(*new_path, R_OK) == 0) | |
87 | return 0; | |
88 | ||
89 | switch (errno) { | |
90 | case ENAMETOOLONG: | |
91 | case ENOENT: | |
92 | case EROFS: | |
93 | case EFAULT: | |
94 | raw_path = strchr(++raw_path, '/'); | |
95 | if (!raw_path) { | |
96 | free(*new_path); | |
97 | *new_path = NULL; | |
98 | return -ENOENT; | |
99 | } | |
100 | continue; | |
101 | ||
102 | default: | |
103 | free(*new_path); | |
104 | *new_path = NULL; | |
105 | return -errno; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
2a9c8c36 MH |
110 | /* Line number list operations */ |
111 | ||
112 | /* Add a line to line number list */ | |
d3b63d7a | 113 | static int line_list__add_line(struct list_head *head, int line) |
2a9c8c36 MH |
114 | { |
115 | struct line_node *ln; | |
116 | struct list_head *p; | |
117 | ||
118 | /* Reverse search, because new line will be the last one */ | |
119 | list_for_each_entry_reverse(ln, head, list) { | |
120 | if (ln->line < line) { | |
121 | p = &ln->list; | |
122 | goto found; | |
123 | } else if (ln->line == line) /* Already exist */ | |
e334016f | 124 | return 1; |
2a9c8c36 MH |
125 | } |
126 | /* List is empty, or the smallest entry */ | |
127 | p = head; | |
128 | found: | |
129 | pr_debug("line list: add a line %u\n", line); | |
e334016f MH |
130 | ln = zalloc(sizeof(struct line_node)); |
131 | if (ln == NULL) | |
132 | return -ENOMEM; | |
2a9c8c36 MH |
133 | ln->line = line; |
134 | INIT_LIST_HEAD(&ln->list); | |
135 | list_add(&ln->list, p); | |
e334016f | 136 | return 0; |
2a9c8c36 MH |
137 | } |
138 | ||
139 | /* Check if the line in line number list */ | |
d3b63d7a | 140 | static int line_list__has_line(struct list_head *head, int line) |
2a9c8c36 MH |
141 | { |
142 | struct line_node *ln; | |
143 | ||
144 | /* Reverse search, because new line will be the last one */ | |
145 | list_for_each_entry(ln, head, list) | |
146 | if (ln->line == line) | |
147 | return 1; | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | /* Init line number list */ | |
153 | static void line_list__init(struct list_head *head) | |
154 | { | |
155 | INIT_LIST_HEAD(head); | |
156 | } | |
157 | ||
158 | /* Free line number list */ | |
159 | static void line_list__free(struct list_head *head) | |
160 | { | |
161 | struct line_node *ln; | |
162 | while (!list_empty(head)) { | |
163 | ln = list_first_entry(head, struct line_node, list); | |
164 | list_del(&ln->list); | |
165 | free(ln); | |
166 | } | |
167 | } | |
168 | ||
169 | /* Dwarf wrappers */ | |
170 | ||
171 | /* Find the realpath of the target file. */ | |
172 | static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname) | |
4ea42b18 | 173 | { |
804b3606 MH |
174 | Dwarf_Files *files; |
175 | size_t nfiles, i; | |
accd3cc4 | 176 | const char *src = NULL; |
4ea42b18 MH |
177 | int ret; |
178 | ||
179 | if (!fname) | |
2a9c8c36 | 180 | return NULL; |
4ea42b18 | 181 | |
804b3606 | 182 | ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); |
2a9c8c36 MH |
183 | if (ret != 0) |
184 | return NULL; | |
185 | ||
186 | for (i = 0; i < nfiles; i++) { | |
187 | src = dwarf_filesrc(files, i, NULL, NULL); | |
188 | if (strtailcmp(src, fname) == 0) | |
189 | break; | |
4ea42b18 | 190 | } |
c9e38582 MH |
191 | if (i == nfiles) |
192 | return NULL; | |
2a9c8c36 | 193 | return src; |
4ea42b18 MH |
194 | } |
195 | ||
016f262e MH |
196 | /* Compare diename and tname */ |
197 | static bool die_compare_name(Dwarf_Die *dw_die, const char *tname) | |
198 | { | |
199 | const char *name; | |
200 | name = dwarf_diename(dw_die); | |
b55a87ad | 201 | return name ? strcmp(tname, name) : -1; |
016f262e MH |
202 | } |
203 | ||
7df2f329 MH |
204 | /* Get type die, but skip qualifiers and typedef */ |
205 | static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) | |
206 | { | |
207 | Dwarf_Attribute attr; | |
208 | int tag; | |
209 | ||
210 | do { | |
211 | if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL || | |
212 | dwarf_formref_die(&attr, die_mem) == NULL) | |
213 | return NULL; | |
214 | ||
215 | tag = dwarf_tag(die_mem); | |
216 | vr_die = die_mem; | |
217 | } while (tag == DW_TAG_const_type || | |
218 | tag == DW_TAG_restrict_type || | |
219 | tag == DW_TAG_volatile_type || | |
220 | tag == DW_TAG_shared_type || | |
221 | tag == DW_TAG_typedef); | |
222 | ||
223 | return die_mem; | |
224 | } | |
225 | ||
4984912e MH |
226 | static bool die_is_signed_type(Dwarf_Die *tp_die) |
227 | { | |
228 | Dwarf_Attribute attr; | |
229 | Dwarf_Word ret; | |
230 | ||
231 | if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL || | |
232 | dwarf_formudata(&attr, &ret) != 0) | |
233 | return false; | |
234 | ||
235 | return (ret == DW_ATE_signed_char || ret == DW_ATE_signed || | |
236 | ret == DW_ATE_signed_fixed); | |
237 | } | |
238 | ||
239 | static int die_get_byte_size(Dwarf_Die *tp_die) | |
240 | { | |
241 | Dwarf_Attribute attr; | |
242 | Dwarf_Word ret; | |
243 | ||
244 | if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL || | |
245 | dwarf_formudata(&attr, &ret) != 0) | |
246 | return 0; | |
247 | ||
248 | return (int)ret; | |
249 | } | |
250 | ||
de1439d8 MH |
251 | /* Get data_member_location offset */ |
252 | static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs) | |
253 | { | |
254 | Dwarf_Attribute attr; | |
255 | Dwarf_Op *expr; | |
256 | size_t nexpr; | |
257 | int ret; | |
258 | ||
259 | if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL) | |
260 | return -ENOENT; | |
261 | ||
262 | if (dwarf_formudata(&attr, offs) != 0) { | |
263 | /* DW_AT_data_member_location should be DW_OP_plus_uconst */ | |
264 | ret = dwarf_getlocation(&attr, &expr, &nexpr); | |
265 | if (ret < 0 || nexpr == 0) | |
266 | return -ENOENT; | |
267 | ||
268 | if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) { | |
269 | pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n", | |
270 | expr[0].atom, nexpr); | |
271 | return -ENOTSUP; | |
272 | } | |
273 | *offs = (Dwarf_Word)expr[0].number; | |
274 | } | |
275 | return 0; | |
276 | } | |
277 | ||
016f262e MH |
278 | /* Return values for die_find callbacks */ |
279 | enum { | |
280 | DIE_FIND_CB_FOUND = 0, /* End of Search */ | |
281 | DIE_FIND_CB_CHILD = 1, /* Search only children */ | |
282 | DIE_FIND_CB_SIBLING = 2, /* Search only siblings */ | |
283 | DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */ | |
284 | }; | |
285 | ||
286 | /* Search a child die */ | |
287 | static Dwarf_Die *die_find_child(Dwarf_Die *rt_die, | |
288 | int (*callback)(Dwarf_Die *, void *), | |
289 | void *data, Dwarf_Die *die_mem) | |
290 | { | |
291 | Dwarf_Die child_die; | |
292 | int ret; | |
293 | ||
294 | ret = dwarf_child(rt_die, die_mem); | |
295 | if (ret != 0) | |
296 | return NULL; | |
297 | ||
298 | do { | |
299 | ret = callback(die_mem, data); | |
300 | if (ret == DIE_FIND_CB_FOUND) | |
301 | return die_mem; | |
302 | ||
303 | if ((ret & DIE_FIND_CB_CHILD) && | |
304 | die_find_child(die_mem, callback, data, &child_die)) { | |
305 | memcpy(die_mem, &child_die, sizeof(Dwarf_Die)); | |
306 | return die_mem; | |
307 | } | |
308 | } while ((ret & DIE_FIND_CB_SIBLING) && | |
309 | dwarf_siblingof(die_mem, die_mem) == 0); | |
310 | ||
311 | return NULL; | |
312 | } | |
313 | ||
804b3606 MH |
314 | struct __addr_die_search_param { |
315 | Dwarf_Addr addr; | |
316 | Dwarf_Die *die_mem; | |
317 | }; | |
318 | ||
319 | static int __die_search_func_cb(Dwarf_Die *fn_die, void *data) | |
631c9def | 320 | { |
804b3606 | 321 | struct __addr_die_search_param *ad = data; |
631c9def | 322 | |
804b3606 MH |
323 | if (dwarf_tag(fn_die) == DW_TAG_subprogram && |
324 | dwarf_haspc(fn_die, ad->addr)) { | |
325 | memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die)); | |
326 | return DWARF_CB_ABORT; | |
327 | } | |
328 | return DWARF_CB_OK; | |
329 | } | |
631c9def | 330 | |
804b3606 | 331 | /* Search a real subprogram including this line, */ |
95a3e4c4 MH |
332 | static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr, |
333 | Dwarf_Die *die_mem) | |
804b3606 MH |
334 | { |
335 | struct __addr_die_search_param ad; | |
336 | ad.addr = addr; | |
337 | ad.die_mem = die_mem; | |
338 | /* dwarf_getscopes can't find subprogram. */ | |
339 | if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0)) | |
340 | return NULL; | |
341 | else | |
342 | return die_mem; | |
631c9def MH |
343 | } |
344 | ||
016f262e MH |
345 | /* die_find callback for inline function search */ |
346 | static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data) | |
161a26b0 | 347 | { |
016f262e | 348 | Dwarf_Addr *addr = data; |
161a26b0 | 349 | |
016f262e MH |
350 | if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine && |
351 | dwarf_haspc(die_mem, *addr)) | |
352 | return DIE_FIND_CB_FOUND; | |
161a26b0 | 353 | |
016f262e | 354 | return DIE_FIND_CB_CONTINUE; |
161a26b0 MH |
355 | } |
356 | ||
016f262e MH |
357 | /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */ |
358 | static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr, | |
359 | Dwarf_Die *die_mem) | |
4ea42b18 | 360 | { |
016f262e | 361 | return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem); |
4ea42b18 MH |
362 | } |
363 | ||
016f262e | 364 | static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data) |
4ea42b18 | 365 | { |
016f262e MH |
366 | const char *name = data; |
367 | int tag; | |
4ea42b18 | 368 | |
016f262e MH |
369 | tag = dwarf_tag(die_mem); |
370 | if ((tag == DW_TAG_formal_parameter || | |
371 | tag == DW_TAG_variable) && | |
372 | (die_compare_name(die_mem, name) == 0)) | |
373 | return DIE_FIND_CB_FOUND; | |
374 | ||
375 | return DIE_FIND_CB_CONTINUE; | |
4ea42b18 MH |
376 | } |
377 | ||
016f262e | 378 | /* Find a variable called 'name' */ |
e92b85e1 MH |
379 | static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name, |
380 | Dwarf_Die *die_mem) | |
4ea42b18 | 381 | { |
016f262e MH |
382 | return die_find_child(sp_die, __die_find_variable_cb, (void *)name, |
383 | die_mem); | |
4ea42b18 MH |
384 | } |
385 | ||
7df2f329 MH |
386 | static int __die_find_member_cb(Dwarf_Die *die_mem, void *data) |
387 | { | |
388 | const char *name = data; | |
389 | ||
390 | if ((dwarf_tag(die_mem) == DW_TAG_member) && | |
391 | (die_compare_name(die_mem, name) == 0)) | |
392 | return DIE_FIND_CB_FOUND; | |
393 | ||
394 | return DIE_FIND_CB_SIBLING; | |
395 | } | |
396 | ||
397 | /* Find a member called 'name' */ | |
398 | static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name, | |
399 | Dwarf_Die *die_mem) | |
400 | { | |
401 | return die_find_child(st_die, __die_find_member_cb, (void *)name, | |
402 | die_mem); | |
403 | } | |
404 | ||
4ea42b18 MH |
405 | /* |
406 | * Probe finder related functions | |
407 | */ | |
408 | ||
409 | /* Show a location */ | |
b55a87ad | 410 | static int convert_location(Dwarf_Op *op, struct probe_finder *pf) |
4ea42b18 | 411 | { |
804b3606 MH |
412 | unsigned int regn; |
413 | Dwarf_Word offs = 0; | |
4235b045 | 414 | bool ref = false; |
4ea42b18 | 415 | const char *regs; |
4235b045 | 416 | struct kprobe_trace_arg *tvar = pf->tvar; |
4ea42b18 | 417 | |
4ea42b18 | 418 | /* If this is based on frame buffer, set the offset */ |
804b3606 | 419 | if (op->atom == DW_OP_fbreg) { |
b55a87ad MH |
420 | if (pf->fb_ops == NULL) { |
421 | pr_warning("The attribute of frame base is not " | |
422 | "supported.\n"); | |
423 | return -ENOTSUP; | |
424 | } | |
4235b045 | 425 | ref = true; |
804b3606 MH |
426 | offs = op->number; |
427 | op = &pf->fb_ops[0]; | |
428 | } | |
4ea42b18 | 429 | |
804b3606 MH |
430 | if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { |
431 | regn = op->atom - DW_OP_breg0; | |
432 | offs += op->number; | |
4235b045 | 433 | ref = true; |
804b3606 MH |
434 | } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { |
435 | regn = op->atom - DW_OP_reg0; | |
436 | } else if (op->atom == DW_OP_bregx) { | |
437 | regn = op->number; | |
438 | offs += op->number2; | |
4235b045 | 439 | ref = true; |
804b3606 MH |
440 | } else if (op->atom == DW_OP_regx) { |
441 | regn = op->number; | |
b55a87ad MH |
442 | } else { |
443 | pr_warning("DW_OP %x is not supported.\n", op->atom); | |
444 | return -ENOTSUP; | |
445 | } | |
4ea42b18 MH |
446 | |
447 | regs = get_arch_regstr(regn); | |
b55a87ad | 448 | if (!regs) { |
cd932c59 | 449 | pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn); |
b55a87ad MH |
450 | return -ERANGE; |
451 | } | |
4ea42b18 | 452 | |
02b95dad MH |
453 | tvar->value = strdup(regs); |
454 | if (tvar->value == NULL) | |
455 | return -ENOMEM; | |
456 | ||
4235b045 | 457 | if (ref) { |
e334016f MH |
458 | tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref)); |
459 | if (tvar->ref == NULL) | |
460 | return -ENOMEM; | |
4235b045 MH |
461 | tvar->ref->offset = (long)offs; |
462 | } | |
b55a87ad | 463 | return 0; |
4ea42b18 MH |
464 | } |
465 | ||
b55a87ad MH |
466 | static int convert_variable_type(Dwarf_Die *vr_die, |
467 | struct kprobe_trace_arg *targ) | |
4984912e MH |
468 | { |
469 | Dwarf_Die type; | |
470 | char buf[16]; | |
471 | int ret; | |
472 | ||
b55a87ad MH |
473 | if (die_get_real_type(vr_die, &type) == NULL) { |
474 | pr_warning("Failed to get a type information of %s.\n", | |
475 | dwarf_diename(vr_die)); | |
476 | return -ENOENT; | |
477 | } | |
4984912e MH |
478 | |
479 | ret = die_get_byte_size(&type) * 8; | |
480 | if (ret) { | |
481 | /* Check the bitwidth */ | |
482 | if (ret > MAX_BASIC_TYPE_BITS) { | |
b55a87ad MH |
483 | pr_info("%s exceeds max-bitwidth." |
484 | " Cut down to %d bits.\n", | |
485 | dwarf_diename(&type), MAX_BASIC_TYPE_BITS); | |
4984912e MH |
486 | ret = MAX_BASIC_TYPE_BITS; |
487 | } | |
488 | ||
489 | ret = snprintf(buf, 16, "%c%d", | |
490 | die_is_signed_type(&type) ? 's' : 'u', ret); | |
b55a87ad MH |
491 | if (ret < 0 || ret >= 16) { |
492 | if (ret >= 16) | |
493 | ret = -E2BIG; | |
494 | pr_warning("Failed to convert variable type: %s\n", | |
495 | strerror(-ret)); | |
496 | return ret; | |
497 | } | |
02b95dad MH |
498 | targ->type = strdup(buf); |
499 | if (targ->type == NULL) | |
500 | return -ENOMEM; | |
4984912e | 501 | } |
b55a87ad | 502 | return 0; |
4984912e MH |
503 | } |
504 | ||
b55a87ad | 505 | static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, |
7df2f329 | 506 | struct perf_probe_arg_field *field, |
4984912e MH |
507 | struct kprobe_trace_arg_ref **ref_ptr, |
508 | Dwarf_Die *die_mem) | |
7df2f329 MH |
509 | { |
510 | struct kprobe_trace_arg_ref *ref = *ref_ptr; | |
7df2f329 MH |
511 | Dwarf_Die type; |
512 | Dwarf_Word offs; | |
de1439d8 | 513 | int ret; |
7df2f329 MH |
514 | |
515 | pr_debug("converting %s in %s\n", field->name, varname); | |
b55a87ad MH |
516 | if (die_get_real_type(vr_die, &type) == NULL) { |
517 | pr_warning("Failed to get the type of %s.\n", varname); | |
518 | return -ENOENT; | |
519 | } | |
7df2f329 MH |
520 | |
521 | /* Check the pointer and dereference */ | |
522 | if (dwarf_tag(&type) == DW_TAG_pointer_type) { | |
b55a87ad MH |
523 | if (!field->ref) { |
524 | pr_err("Semantic error: %s must be referred by '->'\n", | |
525 | field->name); | |
526 | return -EINVAL; | |
527 | } | |
7df2f329 | 528 | /* Get the type pointed by this pointer */ |
b55a87ad MH |
529 | if (die_get_real_type(&type, &type) == NULL) { |
530 | pr_warning("Failed to get the type of %s.\n", varname); | |
531 | return -ENOENT; | |
532 | } | |
12e5a7ae | 533 | /* Verify it is a data structure */ |
b55a87ad MH |
534 | if (dwarf_tag(&type) != DW_TAG_structure_type) { |
535 | pr_warning("%s is not a data structure.\n", varname); | |
536 | return -EINVAL; | |
537 | } | |
12e5a7ae | 538 | |
e334016f MH |
539 | ref = zalloc(sizeof(struct kprobe_trace_arg_ref)); |
540 | if (ref == NULL) | |
541 | return -ENOMEM; | |
7df2f329 MH |
542 | if (*ref_ptr) |
543 | (*ref_ptr)->next = ref; | |
544 | else | |
545 | *ref_ptr = ref; | |
546 | } else { | |
12e5a7ae | 547 | /* Verify it is a data structure */ |
b55a87ad MH |
548 | if (dwarf_tag(&type) != DW_TAG_structure_type) { |
549 | pr_warning("%s is not a data structure.\n", varname); | |
550 | return -EINVAL; | |
551 | } | |
552 | if (field->ref) { | |
553 | pr_err("Semantic error: %s must be referred by '.'\n", | |
554 | field->name); | |
555 | return -EINVAL; | |
556 | } | |
557 | if (!ref) { | |
558 | pr_warning("Structure on a register is not " | |
559 | "supported yet.\n"); | |
560 | return -ENOTSUP; | |
561 | } | |
7df2f329 MH |
562 | } |
563 | ||
b55a87ad MH |
564 | if (die_find_member(&type, field->name, die_mem) == NULL) { |
565 | pr_warning("%s(tyep:%s) has no member %s.\n", varname, | |
566 | dwarf_diename(&type), field->name); | |
567 | return -EINVAL; | |
568 | } | |
7df2f329 MH |
569 | |
570 | /* Get the offset of the field */ | |
de1439d8 MH |
571 | ret = die_get_data_member_location(die_mem, &offs); |
572 | if (ret < 0) { | |
b55a87ad | 573 | pr_warning("Failed to get the offset of %s.\n", field->name); |
de1439d8 | 574 | return ret; |
b55a87ad | 575 | } |
7df2f329 MH |
576 | ref->offset += (long)offs; |
577 | ||
578 | /* Converting next field */ | |
579 | if (field->next) | |
b55a87ad | 580 | return convert_variable_fields(die_mem, field->name, |
de1439d8 | 581 | field->next, &ref, die_mem); |
b55a87ad MH |
582 | else |
583 | return 0; | |
7df2f329 MH |
584 | } |
585 | ||
4ea42b18 | 586 | /* Show a variables in kprobe event format */ |
b55a87ad | 587 | static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) |
4ea42b18 MH |
588 | { |
589 | Dwarf_Attribute attr; | |
4984912e | 590 | Dwarf_Die die_mem; |
804b3606 MH |
591 | Dwarf_Op *expr; |
592 | size_t nexpr; | |
4ea42b18 MH |
593 | int ret; |
594 | ||
804b3606 | 595 | if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL) |
4ea42b18 | 596 | goto error; |
804b3606 | 597 | /* TODO: handle more than 1 exprs */ |
d0cb4260 | 598 | ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1); |
804b3606 | 599 | if (ret <= 0 || nexpr == 0) |
4ea42b18 | 600 | goto error; |
804b3606 | 601 | |
b55a87ad MH |
602 | ret = convert_location(expr, pf); |
603 | if (ret == 0 && pf->pvar->field) { | |
604 | ret = convert_variable_fields(vr_die, pf->pvar->var, | |
605 | pf->pvar->field, &pf->tvar->ref, | |
606 | &die_mem); | |
4984912e MH |
607 | vr_die = &die_mem; |
608 | } | |
b55a87ad | 609 | if (ret == 0) { |
02b95dad MH |
610 | if (pf->pvar->type) { |
611 | pf->tvar->type = strdup(pf->pvar->type); | |
612 | if (pf->tvar->type == NULL) | |
613 | ret = -ENOMEM; | |
614 | } else | |
b55a87ad MH |
615 | ret = convert_variable_type(vr_die, pf->tvar); |
616 | } | |
804b3606 | 617 | /* *expr will be cached in libdw. Don't free it. */ |
b55a87ad | 618 | return ret; |
4ea42b18 | 619 | error: |
804b3606 | 620 | /* TODO: Support const_value */ |
b55a87ad MH |
621 | pr_err("Failed to find the location of %s at this address.\n" |
622 | " Perhaps, it has been optimized out.\n", pf->pvar->var); | |
623 | return -ENOENT; | |
4ea42b18 MH |
624 | } |
625 | ||
4ea42b18 | 626 | /* Find a variable in a subprogram die */ |
b55a87ad | 627 | static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf) |
4ea42b18 | 628 | { |
e92b85e1 | 629 | Dwarf_Die vr_die; |
11a1ca35 | 630 | char buf[32], *ptr; |
02b95dad | 631 | int ret; |
4ea42b18 | 632 | |
48481938 MH |
633 | /* TODO: Support arrays */ |
634 | if (pf->pvar->name) | |
02b95dad | 635 | pf->tvar->name = strdup(pf->pvar->name); |
48481938 | 636 | else { |
02b95dad MH |
637 | ret = synthesize_perf_probe_arg(pf->pvar, buf, 32); |
638 | if (ret < 0) | |
639 | return ret; | |
11a1ca35 MH |
640 | ptr = strchr(buf, ':'); /* Change type separator to _ */ |
641 | if (ptr) | |
642 | *ptr = '_'; | |
02b95dad | 643 | pf->tvar->name = strdup(buf); |
48481938 | 644 | } |
02b95dad MH |
645 | if (pf->tvar->name == NULL) |
646 | return -ENOMEM; | |
48481938 MH |
647 | |
648 | if (!is_c_varname(pf->pvar->var)) { | |
4235b045 | 649 | /* Copy raw parameters */ |
02b95dad MH |
650 | pf->tvar->value = strdup(pf->pvar->var); |
651 | if (pf->tvar->value == NULL) | |
652 | return -ENOMEM; | |
653 | else | |
654 | return 0; | |
4ea42b18 | 655 | } |
b55a87ad MH |
656 | |
657 | pr_debug("Searching '%s' variable in context.\n", | |
658 | pf->pvar->var); | |
659 | /* Search child die for local variables and parameters. */ | |
660 | if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) { | |
661 | pr_warning("Failed to find '%s' in this function.\n", | |
662 | pf->pvar->var); | |
663 | return -ENOENT; | |
664 | } | |
665 | return convert_variable(&vr_die, pf); | |
4ea42b18 MH |
666 | } |
667 | ||
4ea42b18 | 668 | /* Show a probe point to output buffer */ |
b55a87ad | 669 | static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf) |
4ea42b18 | 670 | { |
4235b045 | 671 | struct kprobe_trace_event *tev; |
e92b85e1 MH |
672 | Dwarf_Addr eaddr; |
673 | Dwarf_Die die_mem; | |
804b3606 | 674 | const char *name; |
4235b045 | 675 | int ret, i; |
804b3606 MH |
676 | Dwarf_Attribute fb_attr; |
677 | size_t nops; | |
4ea42b18 | 678 | |
ef4a3565 MH |
679 | if (pf->ntevs == pf->max_tevs) { |
680 | pr_warning("Too many( > %d) probe point found.\n", | |
681 | pf->max_tevs); | |
b55a87ad MH |
682 | return -ERANGE; |
683 | } | |
4235b045 MH |
684 | tev = &pf->tevs[pf->ntevs++]; |
685 | ||
e92b85e1 MH |
686 | /* If no real subprogram, find a real one */ |
687 | if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { | |
95a3e4c4 | 688 | sp_die = die_find_real_subprogram(&pf->cu_die, |
e92b85e1 | 689 | pf->addr, &die_mem); |
b55a87ad MH |
690 | if (!sp_die) { |
691 | pr_warning("Failed to find probe point in any " | |
692 | "functions.\n"); | |
693 | return -ENOENT; | |
694 | } | |
e92b85e1 MH |
695 | } |
696 | ||
4235b045 | 697 | /* Copy the name of probe point */ |
804b3606 MH |
698 | name = dwarf_diename(sp_die); |
699 | if (name) { | |
b55a87ad MH |
700 | if (dwarf_entrypc(sp_die, &eaddr) != 0) { |
701 | pr_warning("Failed to get entry pc of %s\n", | |
702 | dwarf_diename(sp_die)); | |
703 | return -ENOENT; | |
704 | } | |
02b95dad MH |
705 | tev->point.symbol = strdup(name); |
706 | if (tev->point.symbol == NULL) | |
707 | return -ENOMEM; | |
4235b045 MH |
708 | tev->point.offset = (unsigned long)(pf->addr - eaddr); |
709 | } else | |
4ea42b18 | 710 | /* This function has no name. */ |
4235b045 MH |
711 | tev->point.offset = (unsigned long)pf->addr; |
712 | ||
713 | pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, | |
714 | tev->point.offset); | |
4ea42b18 | 715 | |
804b3606 MH |
716 | /* Get the frame base attribute/ops */ |
717 | dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr); | |
d0cb4260 | 718 | ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); |
a34a9854 | 719 | if (ret <= 0 || nops == 0) { |
804b3606 | 720 | pf->fb_ops = NULL; |
7752f1b0 | 721 | #if _ELFUTILS_PREREQ(0, 142) |
a34a9854 MH |
722 | } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && |
723 | pf->cfi != NULL) { | |
724 | Dwarf_Frame *frame; | |
b55a87ad MH |
725 | if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 || |
726 | dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { | |
727 | pr_warning("Failed to get CFA on 0x%jx\n", | |
728 | (uintmax_t)pf->addr); | |
729 | return -ENOENT; | |
730 | } | |
7752f1b0 | 731 | #endif |
a34a9854 | 732 | } |
804b3606 | 733 | |
4ea42b18 | 734 | /* Find each argument */ |
4235b045 | 735 | tev->nargs = pf->pev->nargs; |
e334016f MH |
736 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
737 | if (tev->args == NULL) | |
738 | return -ENOMEM; | |
4235b045 MH |
739 | for (i = 0; i < pf->pev->nargs; i++) { |
740 | pf->pvar = &pf->pev->args[i]; | |
741 | pf->tvar = &tev->args[i]; | |
b55a87ad MH |
742 | ret = find_variable(sp_die, pf); |
743 | if (ret != 0) | |
744 | return ret; | |
4ea42b18 | 745 | } |
804b3606 MH |
746 | |
747 | /* *pf->fb_ops will be cached in libdw. Don't free it. */ | |
748 | pf->fb_ops = NULL; | |
b55a87ad | 749 | return 0; |
4ea42b18 MH |
750 | } |
751 | ||
4ea42b18 | 752 | /* Find probe point from its line number */ |
b55a87ad | 753 | static int find_probe_point_by_line(struct probe_finder *pf) |
4ea42b18 | 754 | { |
804b3606 MH |
755 | Dwarf_Lines *lines; |
756 | Dwarf_Line *line; | |
757 | size_t nlines, i; | |
e92b85e1 | 758 | Dwarf_Addr addr; |
804b3606 | 759 | int lineno; |
b55a87ad | 760 | int ret = 0; |
4ea42b18 | 761 | |
b55a87ad MH |
762 | if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) { |
763 | pr_warning("No source lines found in this CU.\n"); | |
764 | return -ENOENT; | |
765 | } | |
4ea42b18 | 766 | |
b55a87ad | 767 | for (i = 0; i < nlines && ret == 0; i++) { |
804b3606 | 768 | line = dwarf_onesrcline(lines, i); |
b55a87ad MH |
769 | if (dwarf_lineno(line, &lineno) != 0 || |
770 | lineno != pf->lno) | |
4ea42b18 MH |
771 | continue; |
772 | ||
804b3606 MH |
773 | /* TODO: Get fileno from line, but how? */ |
774 | if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0) | |
775 | continue; | |
b0ef0732 | 776 | |
b55a87ad MH |
777 | if (dwarf_lineaddr(line, &addr) != 0) { |
778 | pr_warning("Failed to get the address of the line.\n"); | |
779 | return -ENOENT; | |
780 | } | |
804b3606 MH |
781 | pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n", |
782 | (int)i, lineno, (uintmax_t)addr); | |
4ea42b18 | 783 | pf->addr = addr; |
804b3606 | 784 | |
b55a87ad | 785 | ret = convert_probe_point(NULL, pf); |
4ea42b18 MH |
786 | /* Continuing, because target line might be inlined. */ |
787 | } | |
b55a87ad | 788 | return ret; |
4ea42b18 MH |
789 | } |
790 | ||
2a9c8c36 MH |
791 | /* Find lines which match lazy pattern */ |
792 | static int find_lazy_match_lines(struct list_head *head, | |
793 | const char *fname, const char *pat) | |
794 | { | |
795 | char *fbuf, *p1, *p2; | |
b448c4b6 | 796 | int fd, line, nlines = -1; |
2a9c8c36 MH |
797 | struct stat st; |
798 | ||
799 | fd = open(fname, O_RDONLY); | |
b55a87ad MH |
800 | if (fd < 0) { |
801 | pr_warning("Failed to open %s: %s\n", fname, strerror(-fd)); | |
b448c4b6 | 802 | return -errno; |
b55a87ad MH |
803 | } |
804 | ||
b448c4b6 | 805 | if (fstat(fd, &st) < 0) { |
b55a87ad MH |
806 | pr_warning("Failed to get the size of %s: %s\n", |
807 | fname, strerror(errno)); | |
b448c4b6 ACM |
808 | nlines = -errno; |
809 | goto out_close; | |
b55a87ad | 810 | } |
b448c4b6 ACM |
811 | |
812 | nlines = -ENOMEM; | |
813 | fbuf = malloc(st.st_size + 2); | |
814 | if (fbuf == NULL) | |
815 | goto out_close; | |
816 | if (read(fd, fbuf, st.st_size) < 0) { | |
b55a87ad | 817 | pr_warning("Failed to read %s: %s\n", fname, strerror(errno)); |
b448c4b6 ACM |
818 | nlines = -errno; |
819 | goto out_free_fbuf; | |
b55a87ad | 820 | } |
2a9c8c36 MH |
821 | fbuf[st.st_size] = '\n'; /* Dummy line */ |
822 | fbuf[st.st_size + 1] = '\0'; | |
823 | p1 = fbuf; | |
824 | line = 1; | |
b448c4b6 | 825 | nlines = 0; |
2a9c8c36 MH |
826 | while ((p2 = strchr(p1, '\n')) != NULL) { |
827 | *p2 = '\0'; | |
828 | if (strlazymatch(p1, pat)) { | |
829 | line_list__add_line(head, line); | |
830 | nlines++; | |
831 | } | |
832 | line++; | |
833 | p1 = p2 + 1; | |
834 | } | |
b448c4b6 | 835 | out_free_fbuf: |
2a9c8c36 | 836 | free(fbuf); |
b448c4b6 ACM |
837 | out_close: |
838 | close(fd); | |
2a9c8c36 MH |
839 | return nlines; |
840 | } | |
841 | ||
842 | /* Find probe points from lazy pattern */ | |
b55a87ad | 843 | static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf) |
2a9c8c36 MH |
844 | { |
845 | Dwarf_Lines *lines; | |
846 | Dwarf_Line *line; | |
847 | size_t nlines, i; | |
848 | Dwarf_Addr addr; | |
849 | Dwarf_Die die_mem; | |
850 | int lineno; | |
b55a87ad | 851 | int ret = 0; |
2a9c8c36 MH |
852 | |
853 | if (list_empty(&pf->lcache)) { | |
854 | /* Matching lazy line pattern */ | |
855 | ret = find_lazy_match_lines(&pf->lcache, pf->fname, | |
4235b045 | 856 | pf->pev->point.lazy_line); |
b55a87ad MH |
857 | if (ret == 0) { |
858 | pr_debug("No matched lines found in %s.\n", pf->fname); | |
859 | return 0; | |
860 | } else if (ret < 0) | |
861 | return ret; | |
2a9c8c36 MH |
862 | } |
863 | ||
b55a87ad MH |
864 | if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) { |
865 | pr_warning("No source lines found in this CU.\n"); | |
866 | return -ENOENT; | |
867 | } | |
868 | ||
869 | for (i = 0; i < nlines && ret >= 0; i++) { | |
2a9c8c36 MH |
870 | line = dwarf_onesrcline(lines, i); |
871 | ||
b55a87ad MH |
872 | if (dwarf_lineno(line, &lineno) != 0 || |
873 | !line_list__has_line(&pf->lcache, lineno)) | |
2a9c8c36 MH |
874 | continue; |
875 | ||
876 | /* TODO: Get fileno from line, but how? */ | |
877 | if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0) | |
878 | continue; | |
879 | ||
b55a87ad MH |
880 | if (dwarf_lineaddr(line, &addr) != 0) { |
881 | pr_debug("Failed to get the address of line %d.\n", | |
882 | lineno); | |
883 | continue; | |
884 | } | |
2a9c8c36 MH |
885 | if (sp_die) { |
886 | /* Address filtering 1: does sp_die include addr? */ | |
887 | if (!dwarf_haspc(sp_die, addr)) | |
888 | continue; | |
889 | /* Address filtering 2: No child include addr? */ | |
95a3e4c4 | 890 | if (die_find_inlinefunc(sp_die, addr, &die_mem)) |
2a9c8c36 MH |
891 | continue; |
892 | } | |
893 | ||
894 | pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n", | |
895 | (int)i, lineno, (unsigned long long)addr); | |
896 | pf->addr = addr; | |
897 | ||
b55a87ad | 898 | ret = convert_probe_point(sp_die, pf); |
2a9c8c36 MH |
899 | /* Continuing, because target line might be inlined. */ |
900 | } | |
901 | /* TODO: deallocate lines, but how? */ | |
b55a87ad | 902 | return ret; |
2a9c8c36 MH |
903 | } |
904 | ||
b55a87ad MH |
905 | /* Callback parameter with return value */ |
906 | struct dwarf_callback_param { | |
907 | void *data; | |
908 | int retval; | |
909 | }; | |
910 | ||
e92b85e1 MH |
911 | static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) |
912 | { | |
b55a87ad MH |
913 | struct dwarf_callback_param *param = data; |
914 | struct probe_finder *pf = param->data; | |
4235b045 | 915 | struct perf_probe_point *pp = &pf->pev->point; |
b55a87ad | 916 | Dwarf_Addr addr; |
e92b85e1 | 917 | |
2a9c8c36 | 918 | if (pp->lazy_line) |
b55a87ad | 919 | param->retval = find_probe_point_lazy(in_die, pf); |
2a9c8c36 MH |
920 | else { |
921 | /* Get probe address */ | |
b55a87ad MH |
922 | if (dwarf_entrypc(in_die, &addr) != 0) { |
923 | pr_warning("Failed to get entry pc of %s.\n", | |
924 | dwarf_diename(in_die)); | |
925 | param->retval = -ENOENT; | |
926 | return DWARF_CB_ABORT; | |
927 | } | |
928 | pf->addr = addr; | |
2a9c8c36 MH |
929 | pf->addr += pp->offset; |
930 | pr_debug("found inline addr: 0x%jx\n", | |
931 | (uintmax_t)pf->addr); | |
932 | ||
b55a87ad | 933 | param->retval = convert_probe_point(in_die, pf); |
5d1ee041 MH |
934 | if (param->retval < 0) |
935 | return DWARF_CB_ABORT; | |
2a9c8c36 | 936 | } |
e92b85e1 | 937 | |
e92b85e1 MH |
938 | return DWARF_CB_OK; |
939 | } | |
804b3606 | 940 | |
4ea42b18 | 941 | /* Search function from function name */ |
e92b85e1 | 942 | static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) |
4ea42b18 | 943 | { |
b55a87ad MH |
944 | struct dwarf_callback_param *param = data; |
945 | struct probe_finder *pf = param->data; | |
4235b045 | 946 | struct perf_probe_point *pp = &pf->pev->point; |
4ea42b18 | 947 | |
e92b85e1 MH |
948 | /* Check tag and diename */ |
949 | if (dwarf_tag(sp_die) != DW_TAG_subprogram || | |
950 | die_compare_name(sp_die, pp->function) != 0) | |
b55a87ad | 951 | return DWARF_CB_OK; |
e92b85e1 | 952 | |
2a9c8c36 | 953 | pf->fname = dwarf_decl_file(sp_die); |
e92b85e1 | 954 | if (pp->line) { /* Function relative line */ |
e92b85e1 MH |
955 | dwarf_decl_line(sp_die, &pf->lno); |
956 | pf->lno += pp->line; | |
b55a87ad | 957 | param->retval = find_probe_point_by_line(pf); |
e92b85e1 MH |
958 | } else if (!dwarf_func_inline(sp_die)) { |
959 | /* Real function */ | |
2a9c8c36 | 960 | if (pp->lazy_line) |
b55a87ad | 961 | param->retval = find_probe_point_lazy(sp_die, pf); |
2a9c8c36 | 962 | else { |
b55a87ad MH |
963 | if (dwarf_entrypc(sp_die, &pf->addr) != 0) { |
964 | pr_warning("Failed to get entry pc of %s.\n", | |
965 | dwarf_diename(sp_die)); | |
966 | param->retval = -ENOENT; | |
967 | return DWARF_CB_ABORT; | |
968 | } | |
2a9c8c36 MH |
969 | pf->addr += pp->offset; |
970 | /* TODO: Check the address in this function */ | |
b55a87ad | 971 | param->retval = convert_probe_point(sp_die, pf); |
2a9c8c36 | 972 | } |
b55a87ad MH |
973 | } else { |
974 | struct dwarf_callback_param _param = {.data = (void *)pf, | |
975 | .retval = 0}; | |
e92b85e1 | 976 | /* Inlined function: search instances */ |
b55a87ad MH |
977 | dwarf_func_inline_instances(sp_die, probe_point_inline_cb, |
978 | &_param); | |
979 | param->retval = _param.retval; | |
980 | } | |
e92b85e1 | 981 | |
b55a87ad | 982 | return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */ |
4ea42b18 MH |
983 | } |
984 | ||
b55a87ad | 985 | static int find_probe_point_by_func(struct probe_finder *pf) |
4ea42b18 | 986 | { |
b55a87ad MH |
987 | struct dwarf_callback_param _param = {.data = (void *)pf, |
988 | .retval = 0}; | |
989 | dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0); | |
990 | return _param.retval; | |
4ea42b18 MH |
991 | } |
992 | ||
4235b045 MH |
993 | /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */ |
994 | int find_kprobe_trace_events(int fd, struct perf_probe_event *pev, | |
ef4a3565 | 995 | struct kprobe_trace_event **tevs, int max_tevs) |
4ea42b18 | 996 | { |
ef4a3565 | 997 | struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs}; |
4235b045 | 998 | struct perf_probe_point *pp = &pev->point; |
804b3606 MH |
999 | Dwarf_Off off, noff; |
1000 | size_t cuhl; | |
1001 | Dwarf_Die *diep; | |
1002 | Dwarf *dbg; | |
b55a87ad | 1003 | int ret = 0; |
804b3606 | 1004 | |
ef4a3565 | 1005 | pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs); |
e334016f MH |
1006 | if (pf.tevs == NULL) |
1007 | return -ENOMEM; | |
4235b045 MH |
1008 | *tevs = pf.tevs; |
1009 | pf.ntevs = 0; | |
1010 | ||
804b3606 | 1011 | dbg = dwarf_begin(fd, DWARF_C_READ); |
b55a87ad MH |
1012 | if (!dbg) { |
1013 | pr_warning("No dwarf info found in the vmlinux - " | |
1014 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
b448c4b6 ACM |
1015 | free(pf.tevs); |
1016 | *tevs = NULL; | |
b55a87ad MH |
1017 | return -EBADF; |
1018 | } | |
4ea42b18 | 1019 | |
7752f1b0 | 1020 | #if _ELFUTILS_PREREQ(0, 142) |
a34a9854 MH |
1021 | /* Get the call frame information from this dwarf */ |
1022 | pf.cfi = dwarf_getcfi(dbg); | |
7752f1b0 | 1023 | #endif |
a34a9854 | 1024 | |
804b3606 | 1025 | off = 0; |
2a9c8c36 | 1026 | line_list__init(&pf.lcache); |
804b3606 | 1027 | /* Loop on CUs (Compilation Unit) */ |
b55a87ad MH |
1028 | while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) && |
1029 | ret >= 0) { | |
4ea42b18 | 1030 | /* Get the DIE(Debugging Information Entry) of this CU */ |
804b3606 MH |
1031 | diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die); |
1032 | if (!diep) | |
1033 | continue; | |
4ea42b18 MH |
1034 | |
1035 | /* Check if target file is included. */ | |
1036 | if (pp->file) | |
2a9c8c36 | 1037 | pf.fname = cu_find_realpath(&pf.cu_die, pp->file); |
804b3606 | 1038 | else |
2a9c8c36 | 1039 | pf.fname = NULL; |
4ea42b18 | 1040 | |
2a9c8c36 | 1041 | if (!pp->file || pf.fname) { |
4ea42b18 | 1042 | if (pp->function) |
b55a87ad | 1043 | ret = find_probe_point_by_func(&pf); |
2a9c8c36 | 1044 | else if (pp->lazy_line) |
b55a87ad | 1045 | ret = find_probe_point_lazy(NULL, &pf); |
b0ef0732 MH |
1046 | else { |
1047 | pf.lno = pp->line; | |
b55a87ad | 1048 | ret = find_probe_point_by_line(&pf); |
b0ef0732 | 1049 | } |
4ea42b18 | 1050 | } |
804b3606 | 1051 | off = noff; |
4ea42b18 | 1052 | } |
2a9c8c36 | 1053 | line_list__free(&pf.lcache); |
804b3606 | 1054 | dwarf_end(dbg); |
4ea42b18 | 1055 | |
b55a87ad | 1056 | return (ret < 0) ? ret : pf.ntevs; |
4ea42b18 MH |
1057 | } |
1058 | ||
fb1587d8 MH |
1059 | /* Reverse search */ |
1060 | int find_perf_probe_point(int fd, unsigned long addr, | |
1061 | struct perf_probe_point *ppt) | |
1062 | { | |
1063 | Dwarf_Die cudie, spdie, indie; | |
1064 | Dwarf *dbg; | |
1065 | Dwarf_Line *line; | |
1066 | Dwarf_Addr laddr, eaddr; | |
1067 | const char *tmp; | |
1068 | int lineno, ret = 0; | |
b55a87ad | 1069 | bool found = false; |
fb1587d8 MH |
1070 | |
1071 | dbg = dwarf_begin(fd, DWARF_C_READ); | |
1072 | if (!dbg) | |
b55a87ad | 1073 | return -EBADF; |
fb1587d8 MH |
1074 | |
1075 | /* Find cu die */ | |
75ec5a24 MH |
1076 | if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) { |
1077 | ret = -EINVAL; | |
1078 | goto end; | |
1079 | } | |
fb1587d8 MH |
1080 | |
1081 | /* Find a corresponding line */ | |
1082 | line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr); | |
1083 | if (line) { | |
b55a87ad MH |
1084 | if (dwarf_lineaddr(line, &laddr) == 0 && |
1085 | (Dwarf_Addr)addr == laddr && | |
1086 | dwarf_lineno(line, &lineno) == 0) { | |
fb1587d8 | 1087 | tmp = dwarf_linesrc(line, NULL, NULL); |
b55a87ad MH |
1088 | if (tmp) { |
1089 | ppt->line = lineno; | |
02b95dad MH |
1090 | ppt->file = strdup(tmp); |
1091 | if (ppt->file == NULL) { | |
1092 | ret = -ENOMEM; | |
1093 | goto end; | |
1094 | } | |
b55a87ad MH |
1095 | found = true; |
1096 | } | |
fb1587d8 MH |
1097 | } |
1098 | } | |
1099 | ||
1100 | /* Find a corresponding function */ | |
1101 | if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) { | |
1102 | tmp = dwarf_diename(&spdie); | |
b55a87ad | 1103 | if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0) |
fb1587d8 MH |
1104 | goto end; |
1105 | ||
b55a87ad MH |
1106 | if (ppt->line) { |
1107 | if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, | |
1108 | &indie)) { | |
1109 | /* addr in an inline function */ | |
1110 | tmp = dwarf_diename(&indie); | |
1111 | if (!tmp) | |
1112 | goto end; | |
1113 | ret = dwarf_decl_line(&indie, &lineno); | |
1114 | } else { | |
1115 | if (eaddr == addr) { /* Function entry */ | |
1116 | lineno = ppt->line; | |
1117 | ret = 0; | |
1118 | } else | |
1119 | ret = dwarf_decl_line(&spdie, &lineno); | |
1120 | } | |
1121 | if (ret == 0) { | |
1122 | /* Make a relative line number */ | |
1123 | ppt->line -= lineno; | |
1124 | goto found; | |
1125 | } | |
fb1587d8 | 1126 | } |
b55a87ad MH |
1127 | /* We don't have a line number, let's use offset */ |
1128 | ppt->offset = addr - (unsigned long)eaddr; | |
1129 | found: | |
02b95dad MH |
1130 | ppt->function = strdup(tmp); |
1131 | if (ppt->function == NULL) { | |
1132 | ret = -ENOMEM; | |
1133 | goto end; | |
1134 | } | |
b55a87ad | 1135 | found = true; |
fb1587d8 MH |
1136 | } |
1137 | ||
1138 | end: | |
1139 | dwarf_end(dbg); | |
b55a87ad MH |
1140 | if (ret >= 0) |
1141 | ret = found ? 1 : 0; | |
fb1587d8 MH |
1142 | return ret; |
1143 | } | |
1144 | ||
f6c903f5 MH |
1145 | /* Add a line and store the src path */ |
1146 | static int line_range_add_line(const char *src, unsigned int lineno, | |
1147 | struct line_range *lr) | |
1148 | { | |
9ed7e1b8 CD |
1149 | int ret; |
1150 | ||
f6c903f5 MH |
1151 | /* Copy real path */ |
1152 | if (!lr->path) { | |
9ed7e1b8 CD |
1153 | ret = get_real_path(src, &lr->path); |
1154 | if (ret != 0) | |
1155 | return ret; | |
f6c903f5 MH |
1156 | } |
1157 | return line_list__add_line(&lr->line_list, lineno); | |
1158 | } | |
1159 | ||
1160 | /* Search function declaration lines */ | |
1161 | static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data) | |
1162 | { | |
1163 | struct dwarf_callback_param *param = data; | |
1164 | struct line_finder *lf = param->data; | |
1165 | const char *src; | |
1166 | int lineno; | |
1167 | ||
1168 | src = dwarf_decl_file(sp_die); | |
1169 | if (src && strtailcmp(src, lf->fname) != 0) | |
1170 | return DWARF_CB_OK; | |
1171 | ||
1172 | if (dwarf_decl_line(sp_die, &lineno) != 0 || | |
1173 | (lf->lno_s > lineno || lf->lno_e < lineno)) | |
1174 | return DWARF_CB_OK; | |
1175 | ||
1176 | param->retval = line_range_add_line(src, lineno, lf->lr); | |
5d1ee041 MH |
1177 | if (param->retval < 0) |
1178 | return DWARF_CB_ABORT; | |
f6c903f5 MH |
1179 | return DWARF_CB_OK; |
1180 | } | |
1181 | ||
1182 | static int find_line_range_func_decl_lines(struct line_finder *lf) | |
1183 | { | |
1184 | struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0}; | |
1185 | dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, ¶m, 0); | |
1186 | return param.retval; | |
1187 | } | |
fb1587d8 | 1188 | |
631c9def | 1189 | /* Find line range from its line number */ |
b55a87ad | 1190 | static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) |
631c9def | 1191 | { |
804b3606 MH |
1192 | Dwarf_Lines *lines; |
1193 | Dwarf_Line *line; | |
1194 | size_t nlines, i; | |
631c9def | 1195 | Dwarf_Addr addr; |
f6c903f5 | 1196 | int lineno, ret = 0; |
804b3606 | 1197 | const char *src; |
161a26b0 | 1198 | Dwarf_Die die_mem; |
631c9def | 1199 | |
2a9c8c36 | 1200 | line_list__init(&lf->lr->line_list); |
b55a87ad MH |
1201 | if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) { |
1202 | pr_warning("No source lines found in this CU.\n"); | |
1203 | return -ENOENT; | |
1204 | } | |
631c9def | 1205 | |
f6c903f5 | 1206 | /* Search probable lines on lines list */ |
804b3606 MH |
1207 | for (i = 0; i < nlines; i++) { |
1208 | line = dwarf_onesrcline(lines, i); | |
b55a87ad MH |
1209 | if (dwarf_lineno(line, &lineno) != 0 || |
1210 | (lf->lno_s > lineno || lf->lno_e < lineno)) | |
631c9def MH |
1211 | continue; |
1212 | ||
161a26b0 MH |
1213 | if (sp_die) { |
1214 | /* Address filtering 1: does sp_die include addr? */ | |
b55a87ad MH |
1215 | if (dwarf_lineaddr(line, &addr) != 0 || |
1216 | !dwarf_haspc(sp_die, addr)) | |
161a26b0 MH |
1217 | continue; |
1218 | ||
1219 | /* Address filtering 2: No child include addr? */ | |
95a3e4c4 | 1220 | if (die_find_inlinefunc(sp_die, addr, &die_mem)) |
161a26b0 MH |
1221 | continue; |
1222 | } | |
1223 | ||
804b3606 MH |
1224 | /* TODO: Get fileno from line, but how? */ |
1225 | src = dwarf_linesrc(line, NULL, NULL); | |
1226 | if (strtailcmp(src, lf->fname) != 0) | |
631c9def MH |
1227 | continue; |
1228 | ||
f6c903f5 MH |
1229 | ret = line_range_add_line(src, lineno, lf->lr); |
1230 | if (ret < 0) | |
1231 | return ret; | |
631c9def | 1232 | } |
f6c903f5 MH |
1233 | |
1234 | /* | |
1235 | * Dwarf lines doesn't include function declarations. We have to | |
1236 | * check functions list or given function. | |
1237 | */ | |
1238 | if (sp_die) { | |
1239 | src = dwarf_decl_file(sp_die); | |
1240 | if (src && dwarf_decl_line(sp_die, &lineno) == 0 && | |
1241 | (lf->lno_s <= lineno && lf->lno_e >= lineno)) | |
1242 | ret = line_range_add_line(src, lineno, lf->lr); | |
1243 | } else | |
1244 | ret = find_line_range_func_decl_lines(lf); | |
1245 | ||
804b3606 | 1246 | /* Update status */ |
f6c903f5 MH |
1247 | if (ret >= 0) |
1248 | if (!list_empty(&lf->lr->line_list)) | |
1249 | ret = lf->found = 1; | |
1250 | else | |
1251 | ret = 0; /* Lines are not found */ | |
804b3606 MH |
1252 | else { |
1253 | free(lf->lr->path); | |
1254 | lf->lr->path = NULL; | |
1255 | } | |
f6c903f5 | 1256 | return ret; |
631c9def MH |
1257 | } |
1258 | ||
161a26b0 MH |
1259 | static int line_range_inline_cb(Dwarf_Die *in_die, void *data) |
1260 | { | |
b55a87ad MH |
1261 | struct dwarf_callback_param *param = data; |
1262 | ||
1263 | param->retval = find_line_range_by_line(in_die, param->data); | |
161a26b0 MH |
1264 | return DWARF_CB_ABORT; /* No need to find other instances */ |
1265 | } | |
1266 | ||
631c9def | 1267 | /* Search function from function name */ |
e92b85e1 | 1268 | static int line_range_search_cb(Dwarf_Die *sp_die, void *data) |
631c9def | 1269 | { |
b55a87ad MH |
1270 | struct dwarf_callback_param *param = data; |
1271 | struct line_finder *lf = param->data; | |
631c9def | 1272 | struct line_range *lr = lf->lr; |
631c9def | 1273 | |
e92b85e1 MH |
1274 | if (dwarf_tag(sp_die) == DW_TAG_subprogram && |
1275 | die_compare_name(sp_die, lr->function) == 0) { | |
e92b85e1 MH |
1276 | lf->fname = dwarf_decl_file(sp_die); |
1277 | dwarf_decl_line(sp_die, &lr->offset); | |
804b3606 | 1278 | pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); |
631c9def | 1279 | lf->lno_s = lr->offset + lr->start; |
d3b63d7a MH |
1280 | if (lf->lno_s < 0) /* Overflow */ |
1281 | lf->lno_s = INT_MAX; | |
1282 | lf->lno_e = lr->offset + lr->end; | |
1283 | if (lf->lno_e < 0) /* Overflow */ | |
804b3606 | 1284 | lf->lno_e = INT_MAX; |
d3b63d7a | 1285 | pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e); |
631c9def MH |
1286 | lr->start = lf->lno_s; |
1287 | lr->end = lf->lno_e; | |
b55a87ad MH |
1288 | if (dwarf_func_inline(sp_die)) { |
1289 | struct dwarf_callback_param _param; | |
1290 | _param.data = (void *)lf; | |
1291 | _param.retval = 0; | |
161a26b0 | 1292 | dwarf_func_inline_instances(sp_die, |
b55a87ad MH |
1293 | line_range_inline_cb, |
1294 | &_param); | |
1295 | param->retval = _param.retval; | |
1296 | } else | |
1297 | param->retval = find_line_range_by_line(sp_die, lf); | |
1298 | return DWARF_CB_ABORT; | |
631c9def | 1299 | } |
b55a87ad | 1300 | return DWARF_CB_OK; |
631c9def MH |
1301 | } |
1302 | ||
b55a87ad | 1303 | static int find_line_range_by_func(struct line_finder *lf) |
631c9def | 1304 | { |
b55a87ad MH |
1305 | struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0}; |
1306 | dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0); | |
1307 | return param.retval; | |
631c9def MH |
1308 | } |
1309 | ||
1310 | int find_line_range(int fd, struct line_range *lr) | |
1311 | { | |
804b3606 | 1312 | struct line_finder lf = {.lr = lr, .found = 0}; |
b55a87ad | 1313 | int ret = 0; |
804b3606 MH |
1314 | Dwarf_Off off = 0, noff; |
1315 | size_t cuhl; | |
1316 | Dwarf_Die *diep; | |
1317 | Dwarf *dbg; | |
804b3606 MH |
1318 | |
1319 | dbg = dwarf_begin(fd, DWARF_C_READ); | |
b55a87ad MH |
1320 | if (!dbg) { |
1321 | pr_warning("No dwarf info found in the vmlinux - " | |
1322 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
1323 | return -EBADF; | |
1324 | } | |
631c9def | 1325 | |
804b3606 | 1326 | /* Loop on CUs (Compilation Unit) */ |
b55a87ad MH |
1327 | while (!lf.found && ret >= 0) { |
1328 | if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0) | |
631c9def MH |
1329 | break; |
1330 | ||
1331 | /* Get the DIE(Debugging Information Entry) of this CU */ | |
804b3606 MH |
1332 | diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die); |
1333 | if (!diep) | |
1334 | continue; | |
631c9def MH |
1335 | |
1336 | /* Check if target file is included. */ | |
1337 | if (lr->file) | |
2a9c8c36 | 1338 | lf.fname = cu_find_realpath(&lf.cu_die, lr->file); |
804b3606 | 1339 | else |
2a9c8c36 | 1340 | lf.fname = 0; |
631c9def | 1341 | |
2a9c8c36 | 1342 | if (!lr->file || lf.fname) { |
631c9def | 1343 | if (lr->function) |
b55a87ad | 1344 | ret = find_line_range_by_func(&lf); |
631c9def MH |
1345 | else { |
1346 | lf.lno_s = lr->start; | |
d3b63d7a | 1347 | lf.lno_e = lr->end; |
b55a87ad | 1348 | ret = find_line_range_by_line(NULL, &lf); |
631c9def | 1349 | } |
631c9def | 1350 | } |
804b3606 | 1351 | off = noff; |
631c9def | 1352 | } |
804b3606 MH |
1353 | pr_debug("path: %lx\n", (unsigned long)lr->path); |
1354 | dwarf_end(dbg); | |
b55a87ad MH |
1355 | |
1356 | return (ret < 0) ? ret : lf.found; | |
631c9def MH |
1357 | } |
1358 |