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