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