blob: a41035634dd8c42202028896a5815b0cc6477be7 [file] [log] [blame]
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001/*
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>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040034
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040035#include "event.h"
36#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040037#include "util.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040038#include "probe-finder.h"
39
40
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040041/*
42 * Generic dwarf analysis helpers
43 */
44
45#define X86_32_MAX_REGS 8
46const char *x86_32_regs_table[X86_32_MAX_REGS] = {
47 "%ax",
48 "%cx",
49 "%dx",
50 "%bx",
51 "$stack", /* Stack address instead of %sp */
52 "%bp",
53 "%si",
54 "%di",
55};
56
57#define X86_64_MAX_REGS 16
58const char *x86_64_regs_table[X86_64_MAX_REGS] = {
59 "%ax",
60 "%dx",
61 "%cx",
62 "%bx",
63 "%si",
64 "%di",
65 "%bp",
66 "%sp",
67 "%r8",
68 "%r9",
69 "%r10",
70 "%r11",
71 "%r12",
72 "%r13",
73 "%r14",
74 "%r15",
75};
76
77/* TODO: switching by dwarf address size */
78#ifdef __x86_64__
79#define ARCH_MAX_REGS X86_64_MAX_REGS
80#define arch_regs_table x86_64_regs_table
81#else
82#define ARCH_MAX_REGS X86_32_MAX_REGS
83#define arch_regs_table x86_32_regs_table
84#endif
85
86/* Return architecture dependent register string (for kprobe-tracer) */
87static const char *get_arch_regstr(unsigned int n)
88{
89 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
90}
91
92/*
93 * Compare the tail of two strings.
94 * Return 0 if whole of either string is same as another's tail part.
95 */
96static int strtailcmp(const char *s1, const char *s2)
97{
98 int i1 = strlen(s1);
99 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -0500100 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400101 if (s1[i1] != s2[i2])
102 return s1[i1] - s2[i2];
103 }
104 return 0;
105}
106
107/* Find the fileno of the target file. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500108static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400109{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500110 Dwarf_Files *files;
111 size_t nfiles, i;
112 const char *src;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400113 int ret;
114
115 if (!fname)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500116 return -EINVAL;
117
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500118 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
119 if (ret == 0) {
120 for (i = 0; i < nfiles; i++) {
121 src = dwarf_filesrc(files, i, NULL, NULL);
122 if (strtailcmp(src, fname) == 0) {
123 ret = (int)i; /*???: +1 or not?*/
124 break;
125 }
126 }
127 if (ret)
128 pr_debug("found fno: %d\n", ret);
129 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500130 return ret;
131}
132
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500133struct __addr_die_search_param {
134 Dwarf_Addr addr;
135 Dwarf_Die *die_mem;
136};
137
138static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
139{
140 struct __addr_die_search_param *ad = data;
141
142 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
143 dwarf_haspc(fn_die, ad->addr)) {
144 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
145 return DWARF_CB_ABORT;
146 }
147 return DWARF_CB_OK;
148}
149
150/* Search a real subprogram including this line, */
151static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
152 Dwarf_Die *die_mem)
153{
154 struct __addr_die_search_param ad;
155 ad.addr = addr;
156 ad.die_mem = die_mem;
157 /* dwarf_getscopes can't find subprogram. */
158 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
159 return NULL;
160 else
161 return die_mem;
162}
163
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500164/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
165static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
166 Dwarf_Die *die_mem)
167{
168 Dwarf_Die child_die;
169 int ret;
170
171 ret = dwarf_child(sp_die, die_mem);
172 if (ret != 0)
173 return NULL;
174
175 do {
176 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
177 dwarf_haspc(die_mem, addr))
178 return die_mem;
179
180 if (die_get_inlinefunc(die_mem, addr, &child_die)) {
181 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
182 return die_mem;
183 }
184 } while (dwarf_siblingof(die_mem, die_mem) == 0);
185
186 return NULL;
187}
188
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400189/* Compare diename and tname */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500190static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400191{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500192 const char *name;
193 name = dwarf_diename(dw_die);
194 DIE_IF(name == NULL);
195 return strcmp(tname, name);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400196}
197
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400198/* Get entry pc(or low pc, 1st entry of ranges) of the die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500199static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400200{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500201 Dwarf_Addr epc;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400202 int ret;
203
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500204 ret = dwarf_entrypc(dw_die, &epc);
205 DIE_IF(ret == -1);
206 return epc;
207}
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400208
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500209/* Get a variable die */
210static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
211 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500212{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500213 Dwarf_Die child_die;
214 int tag;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400215 int ret;
216
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500217 ret = dwarf_child(sp_die, die_mem);
218 if (ret != 0)
219 return NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400220
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500221 do {
222 tag = dwarf_tag(die_mem);
223 if ((tag == DW_TAG_formal_parameter ||
224 tag == DW_TAG_variable) &&
225 (die_compare_name(die_mem, name) == 0))
226 return die_mem;
227
228 if (die_find_variable(die_mem, name, &child_die)) {
229 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
230 return die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400231 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500232 } while (dwarf_siblingof(die_mem, die_mem) == 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400233
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500234 return NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400235}
236
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400237/*
238 * Probe finder related functions
239 */
240
241/* Show a location */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500242static void show_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400243{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500244 unsigned int regn;
245 Dwarf_Word offs = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400246 int deref = 0, ret;
247 const char *regs;
248
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500249 /* TODO: support CFA */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400250 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500251 if (op->atom == DW_OP_fbreg) {
252 if (pf->fb_ops == NULL)
253 die("The attribute of frame base is not supported.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400254 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500255 offs = op->number;
256 op = &pf->fb_ops[0];
257 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400258
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500259 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
260 regn = op->atom - DW_OP_breg0;
261 offs += op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400262 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500263 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
264 regn = op->atom - DW_OP_reg0;
265 } else if (op->atom == DW_OP_bregx) {
266 regn = op->number;
267 offs += op->number2;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400268 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500269 } else if (op->atom == DW_OP_regx) {
270 regn = op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400271 } else
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500272 die("DW_OP %d is not supported.", op->atom);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400273
274 regs = get_arch_regstr(regn);
275 if (!regs)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500276 die("%u exceeds max register number.", regn);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400277
278 if (deref)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500279 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
280 pf->var, (uintmax_t)offs, regs);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400281 else
282 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400283 DIE_IF(ret < 0);
284 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400285}
286
287/* Show a variables in kprobe event format */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500288static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400289{
290 Dwarf_Attribute attr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500291 Dwarf_Op *expr;
292 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400293 int ret;
294
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500295 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400296 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500297 /* TODO: handle more than 1 exprs */
298 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
299 &expr, &nexpr, 1);
300 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400301 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500302
303 show_location(expr, pf);
304 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400305 return ;
306error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500307 /* TODO: Support const_value */
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400308 die("Failed to find the location of %s at this address.\n"
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500309 " Perhaps, it has been optimized out.", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400310}
311
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400312/* Find a variable in a subprogram die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500313static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400314{
315 int ret;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500316 Dwarf_Die vr_die;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400317
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500318 /* TODO: Support struct members and arrays */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400319 if (!is_c_varname(pf->var)) {
320 /* Output raw parameters */
321 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400322 DIE_IF(ret < 0);
323 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400324 return ;
325 }
326
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200327 pr_debug("Searching '%s' variable in context.\n", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400328 /* Search child die for local variables and parameters. */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500329 if (!die_find_variable(sp_die, pf->var, &vr_die))
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500330 die("Failed to find '%s' in this function.", pf->var);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500331
332 show_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400333}
334
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400335/* Show a probe point to output buffer */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500336static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400337{
338 struct probe_point *pp = pf->pp;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500339 Dwarf_Addr eaddr;
340 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500341 const char *name;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400342 char tmp[MAX_PROBE_BUFFER];
343 int ret, i, len;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500344 Dwarf_Attribute fb_attr;
345 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400346
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500347 /* If no real subprogram, find a real one */
348 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
349 sp_die = die_get_real_subprogram(&pf->cu_die,
350 pf->addr, &die_mem);
351 if (!sp_die)
352 die("Probe point is not found in subprograms.");
353 }
354
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400355 /* Output name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500356 name = dwarf_diename(sp_die);
357 if (name) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500358 dwarf_entrypc(sp_die, &eaddr);
359 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
360 (unsigned long)(pf->addr - eaddr));
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400361 /* Copy the function name if possible */
362 if (!pp->function) {
363 pp->function = strdup(name);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500364 pp->offset = (size_t)(pf->addr - eaddr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400365 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400366 } else {
367 /* This function has no name. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500368 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
369 (uintmax_t)pf->addr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400370 if (!pp->function) {
371 /* TODO: Use _stext */
372 pp->function = strdup("");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500373 pp->offset = (size_t)pf->addr;
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400374 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400375 }
Masami Hiramatsu97698332009-10-16 20:08:18 -0400376 DIE_IF(ret < 0);
377 DIE_IF(ret >= MAX_PROBE_BUFFER);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400378 len = ret;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400379 pr_debug("Probe point found: %s\n", tmp);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400380
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500381 /* Get the frame base attribute/ops */
382 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
383 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
384 &pf->fb_ops, &nops, 1);
385 if (ret <= 0 || nops == 0)
386 pf->fb_ops = NULL;
387
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400388 /* Find each argument */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500389 /* TODO: use dwarf_cfi_addrframe */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400390 for (i = 0; i < pp->nr_args; i++) {
391 pf->var = pp->args[i];
392 pf->buf = &tmp[len];
393 pf->len = MAX_PROBE_BUFFER - len;
394 find_variable(sp_die, pf);
395 len += strlen(pf->buf);
396 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500397
398 /* *pf->fb_ops will be cached in libdw. Don't free it. */
399 pf->fb_ops = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400400
401 pp->probes[pp->found] = strdup(tmp);
402 pp->found++;
403}
404
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400405/* Find probe point from its line number */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500406static void find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400407{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500408 Dwarf_Lines *lines;
409 Dwarf_Line *line;
410 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500411 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500412 int lineno;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400413 int ret;
414
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500415 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
416 DIE_IF(ret != 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400417
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500418 for (i = 0; i < nlines; i++) {
419 line = dwarf_onesrcline(lines, i);
420 dwarf_lineno(line, &lineno);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400421 if (lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400422 continue;
423
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500424 /* TODO: Get fileno from line, but how? */
425 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
426 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400427
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500428 ret = dwarf_lineaddr(line, &addr);
429 DIE_IF(ret != 0);
430 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
431 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400432 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500433
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500434 show_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400435 /* Continuing, because target line might be inlined. */
436 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400437}
438
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500439static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400440{
441 struct probe_finder *pf = (struct probe_finder *)data;
442 struct probe_point *pp = pf->pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400443
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500444 /* Get probe address */
445 pf->addr = die_get_entrypc(in_die);
446 pf->addr += pp->offset;
447 pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr);
448
449 show_probe_point(in_die, pf);
450 return DWARF_CB_OK;
451}
452
453/* Search function from function name */
454static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
455{
456 struct probe_finder *pf = (struct probe_finder *)data;
457 struct probe_point *pp = pf->pp;
458
459 /* Check tag and diename */
460 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
461 die_compare_name(sp_die, pp->function) != 0)
462 return 0;
463
464 if (pp->line) { /* Function relative line */
465 pf->fname = dwarf_decl_file(sp_die);
466 dwarf_decl_line(sp_die, &pf->lno);
467 pf->lno += pp->line;
468 find_probe_point_by_line(pf);
469 } else if (!dwarf_func_inline(sp_die)) {
470 /* Real function */
471 pf->addr = die_get_entrypc(sp_die);
472 pf->addr += pp->offset;
473 /* TODO: Check the address in this function */
474 show_probe_point(sp_die, pf);
475 } else
476 /* Inlined function: search instances */
477 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
478
479 return 1; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400480}
481
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500482static void find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400483{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500484 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400485}
486
487/* Find a probe point */
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500488int find_probe_point(int fd, struct probe_point *pp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400489{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400490 struct probe_finder pf = {.pp = pp};
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500491 int ret;
492 Dwarf_Off off, noff;
493 size_t cuhl;
494 Dwarf_Die *diep;
495 Dwarf *dbg;
496 int fno = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400497
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500498 dbg = dwarf_begin(fd, DWARF_C_READ);
499 if (!dbg)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500500 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400501
502 pp->found = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500503 off = 0;
504 /* Loop on CUs (Compilation Unit) */
505 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400506 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500507 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
508 if (!diep)
509 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400510
511 /* Check if target file is included. */
512 if (pp->file)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500513 fno = cu_find_fileno(&pf.cu_die, pp->file);
514 else
515 fno = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400516
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500517 if (!pp->file || fno) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400518 /* Save CU base address (for frame_base) */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500519 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
520 if (ret != 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400521 pf.cu_base = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400522 if (pp->function)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500523 find_probe_point_by_func(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400524 else {
525 pf.lno = pp->line;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500526 find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400527 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400528 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500529 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400530 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500531 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400532
533 return pp->found;
534}
535
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500536
537static void line_range_add_line(struct line_range *lr, unsigned int line)
538{
539 struct line_node *ln;
540 struct list_head *p;
541
542 /* Reverse search, because new line will be the last one */
543 list_for_each_entry_reverse(ln, &lr->line_list, list) {
544 if (ln->line < line) {
545 p = &ln->list;
546 goto found;
547 } else if (ln->line == line) /* Already exist */
548 return ;
549 }
550 /* List is empty, or the smallest entry */
551 p = &lr->line_list;
552found:
553 pr_debug("Debug: add a line %u\n", line);
554 ln = zalloc(sizeof(struct line_node));
555 DIE_IF(ln == NULL);
556 ln->line = line;
557 INIT_LIST_HEAD(&ln->list);
558 list_add(&ln->list, p);
559}
560
561/* Find line range from its line number */
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500562static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500563{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500564 Dwarf_Lines *lines;
565 Dwarf_Line *line;
566 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500567 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500568 int lineno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500569 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500570 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500571 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500572
Masami Hiramatsu3cb8bc62010-02-25 08:35:27 -0500573 INIT_LIST_HEAD(&lf->lr->line_list);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500574 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
575 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500576
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500577 for (i = 0; i < nlines; i++) {
578 line = dwarf_onesrcline(lines, i);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500579 ret = dwarf_lineno(line, &lineno);
580 DIE_IF(ret != 0);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500581 if (lf->lno_s > lineno || lf->lno_e < lineno)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500582 continue;
583
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500584 if (sp_die) {
585 /* Address filtering 1: does sp_die include addr? */
586 ret = dwarf_lineaddr(line, &addr);
587 DIE_IF(ret != 0);
588 if (!dwarf_haspc(sp_die, addr))
589 continue;
590
591 /* Address filtering 2: No child include addr? */
592 if (die_get_inlinefunc(sp_die, addr, &die_mem))
593 continue;
594 }
595
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500596 /* TODO: Get fileno from line, but how? */
597 src = dwarf_linesrc(line, NULL, NULL);
598 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500599 continue;
600
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500601 /* Copy real path */
602 if (!lf->lr->path)
603 lf->lr->path = strdup(src);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500604 line_range_add_line(lf->lr, (unsigned int)lineno);
605 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500606 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500607 if (!list_empty(&lf->lr->line_list))
608 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500609 else {
610 free(lf->lr->path);
611 lf->lr->path = NULL;
612 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500613}
614
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500615static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
616{
617 find_line_range_by_line(in_die, (struct line_finder *)data);
618 return DWARF_CB_ABORT; /* No need to find other instances */
619}
620
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500621/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500622static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500623{
624 struct line_finder *lf = (struct line_finder *)data;
625 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500626
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500627 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
628 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500629 lf->fname = dwarf_decl_file(sp_die);
630 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500631 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500632 lf->lno_s = lr->offset + lr->start;
633 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500634 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500635 else
636 lf->lno_e = lr->offset + lr->end;
637 lr->start = lf->lno_s;
638 lr->end = lf->lno_e;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500639 if (dwarf_func_inline(sp_die))
640 dwarf_func_inline_instances(sp_die,
641 line_range_inline_cb, lf);
642 else
643 find_line_range_by_line(sp_die, lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500644 return 1;
645 }
646 return 0;
647}
648
649static void find_line_range_by_func(struct line_finder *lf)
650{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500651 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500652}
653
654int find_line_range(int fd, struct line_range *lr)
655{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500656 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500657 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500658 Dwarf_Off off = 0, noff;
659 size_t cuhl;
660 Dwarf_Die *diep;
661 Dwarf *dbg;
662 int fno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500663
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500664 dbg = dwarf_begin(fd, DWARF_C_READ);
665 if (!dbg)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500666 return -ENOENT;
667
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500668 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500669 while (!lf.found) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500670 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
671 if (ret != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500672 break;
673
674 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500675 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
676 if (!diep)
677 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500678
679 /* Check if target file is included. */
680 if (lr->file)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500681 fno = cu_find_fileno(&lf.cu_die, lr->file);
682 else
683 fno = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500684
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500685 if (!lr->file || fno) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500686 if (lr->function)
687 find_line_range_by_func(&lf);
688 else {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500689 lf.fname = lr->file;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500690 lf.lno_s = lr->start;
691 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500692 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500693 else
694 lf.lno_e = lr->end;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500695 find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500696 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500697 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500698 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500699 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500700 pr_debug("path: %lx\n", (unsigned long)lr->path);
701 dwarf_end(dbg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500702 return lf.found;
703}
704