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