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