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