blob: 6305f344f382f362f87519561be7901fdbdd699c [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 Hiramatsu4ea42b12009-10-08 17:17:38 -0400164/* Compare diename and tname */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500165static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400166{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500167 const char *name;
168 name = dwarf_diename(dw_die);
169 DIE_IF(name == NULL);
170 return strcmp(tname, name);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400171}
172
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400173/* Get entry pc(or low pc, 1st entry of ranges) of the die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500174static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400175{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500176 Dwarf_Addr epc;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400177 int ret;
178
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500179 ret = dwarf_entrypc(dw_die, &epc);
180 DIE_IF(ret == -1);
181 return epc;
182}
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400183
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500184/* Get a variable die */
185static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
186 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500187{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500188 Dwarf_Die child_die;
189 int tag;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400190 int ret;
191
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500192 ret = dwarf_child(sp_die, die_mem);
193 if (ret != 0)
194 return NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400195
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500196 do {
197 tag = dwarf_tag(die_mem);
198 if ((tag == DW_TAG_formal_parameter ||
199 tag == DW_TAG_variable) &&
200 (die_compare_name(die_mem, name) == 0))
201 return die_mem;
202
203 if (die_find_variable(die_mem, name, &child_die)) {
204 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
205 return die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400206 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500207 } while (dwarf_siblingof(die_mem, die_mem) == 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400208
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500209 return NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400210}
211
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400212/*
213 * Probe finder related functions
214 */
215
216/* Show a location */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500217static void show_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400218{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500219 unsigned int regn;
220 Dwarf_Word offs = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400221 int deref = 0, ret;
222 const char *regs;
223
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500224 /* TODO: support CFA */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400225 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500226 if (op->atom == DW_OP_fbreg) {
227 if (pf->fb_ops == NULL)
228 die("The attribute of frame base is not supported.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400229 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500230 offs = op->number;
231 op = &pf->fb_ops[0];
232 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400233
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500234 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
235 regn = op->atom - DW_OP_breg0;
236 offs += op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400237 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500238 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
239 regn = op->atom - DW_OP_reg0;
240 } else if (op->atom == DW_OP_bregx) {
241 regn = op->number;
242 offs += op->number2;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400243 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500244 } else if (op->atom == DW_OP_regx) {
245 regn = op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400246 } else
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500247 die("DW_OP %d is not supported.", op->atom);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400248
249 regs = get_arch_regstr(regn);
250 if (!regs)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500251 die("%u exceeds max register number.", regn);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400252
253 if (deref)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500254 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
255 pf->var, (uintmax_t)offs, regs);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400256 else
257 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400258 DIE_IF(ret < 0);
259 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400260}
261
262/* Show a variables in kprobe event format */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500263static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400264{
265 Dwarf_Attribute attr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500266 Dwarf_Op *expr;
267 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400268 int ret;
269
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500270 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400271 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500272 /* TODO: handle more than 1 exprs */
273 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
274 &expr, &nexpr, 1);
275 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400276 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500277
278 show_location(expr, pf);
279 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400280 return ;
281error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500282 /* TODO: Support const_value */
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400283 die("Failed to find the location of %s at this address.\n"
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500284 " Perhaps, it has been optimized out.", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400285}
286
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400287/* Find a variable in a subprogram die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500288static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400289{
290 int ret;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500291 Dwarf_Die vr_die;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400292
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500293 /* TODO: Support struct members and arrays */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400294 if (!is_c_varname(pf->var)) {
295 /* Output raw parameters */
296 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400297 DIE_IF(ret < 0);
298 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400299 return ;
300 }
301
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200302 pr_debug("Searching '%s' variable in context.\n", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400303 /* Search child die for local variables and parameters. */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500304 if (!die_find_variable(sp_die, pf->var, &vr_die))
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500305 die("Failed to find '%s' in this function.", pf->var);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500306
307 show_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400308}
309
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400310/* Show a probe point to output buffer */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500311static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400312{
313 struct probe_point *pp = pf->pp;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500314 Dwarf_Addr eaddr;
315 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500316 const char *name;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400317 char tmp[MAX_PROBE_BUFFER];
318 int ret, i, len;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500319 Dwarf_Attribute fb_attr;
320 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400321
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500322 /* If no real subprogram, find a real one */
323 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
324 sp_die = die_get_real_subprogram(&pf->cu_die,
325 pf->addr, &die_mem);
326 if (!sp_die)
327 die("Probe point is not found in subprograms.");
328 }
329
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400330 /* Output name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500331 name = dwarf_diename(sp_die);
332 if (name) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500333 dwarf_entrypc(sp_die, &eaddr);
334 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
335 (unsigned long)(pf->addr - eaddr));
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400336 /* Copy the function name if possible */
337 if (!pp->function) {
338 pp->function = strdup(name);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500339 pp->offset = (size_t)(pf->addr - eaddr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400340 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400341 } else {
342 /* This function has no name. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500343 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
344 (uintmax_t)pf->addr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400345 if (!pp->function) {
346 /* TODO: Use _stext */
347 pp->function = strdup("");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500348 pp->offset = (size_t)pf->addr;
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400349 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400350 }
Masami Hiramatsu97698332009-10-16 20:08:18 -0400351 DIE_IF(ret < 0);
352 DIE_IF(ret >= MAX_PROBE_BUFFER);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400353 len = ret;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400354 pr_debug("Probe point found: %s\n", tmp);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400355
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500356 /* Get the frame base attribute/ops */
357 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
358 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
359 &pf->fb_ops, &nops, 1);
360 if (ret <= 0 || nops == 0)
361 pf->fb_ops = NULL;
362
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400363 /* Find each argument */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500364 /* TODO: use dwarf_cfi_addrframe */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400365 for (i = 0; i < pp->nr_args; i++) {
366 pf->var = pp->args[i];
367 pf->buf = &tmp[len];
368 pf->len = MAX_PROBE_BUFFER - len;
369 find_variable(sp_die, pf);
370 len += strlen(pf->buf);
371 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500372
373 /* *pf->fb_ops will be cached in libdw. Don't free it. */
374 pf->fb_ops = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400375
376 pp->probes[pp->found] = strdup(tmp);
377 pp->found++;
378}
379
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400380/* Find probe point from its line number */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500381static void find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400382{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500383 Dwarf_Lines *lines;
384 Dwarf_Line *line;
385 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500386 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500387 int lineno;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400388 int ret;
389
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500390 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
391 DIE_IF(ret != 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400392
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500393 for (i = 0; i < nlines; i++) {
394 line = dwarf_onesrcline(lines, i);
395 dwarf_lineno(line, &lineno);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400396 if (lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400397 continue;
398
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500399 /* TODO: Get fileno from line, but how? */
400 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
401 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400402
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500403 ret = dwarf_lineaddr(line, &addr);
404 DIE_IF(ret != 0);
405 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
406 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400407 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500408
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500409 show_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400410 /* Continuing, because target line might be inlined. */
411 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400412}
413
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500414static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400415{
416 struct probe_finder *pf = (struct probe_finder *)data;
417 struct probe_point *pp = pf->pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400418
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500419 /* Get probe address */
420 pf->addr = die_get_entrypc(in_die);
421 pf->addr += pp->offset;
422 pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr);
423
424 show_probe_point(in_die, pf);
425 return DWARF_CB_OK;
426}
427
428/* Search function from function name */
429static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
430{
431 struct probe_finder *pf = (struct probe_finder *)data;
432 struct probe_point *pp = pf->pp;
433
434 /* Check tag and diename */
435 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
436 die_compare_name(sp_die, pp->function) != 0)
437 return 0;
438
439 if (pp->line) { /* Function relative line */
440 pf->fname = dwarf_decl_file(sp_die);
441 dwarf_decl_line(sp_die, &pf->lno);
442 pf->lno += pp->line;
443 find_probe_point_by_line(pf);
444 } else if (!dwarf_func_inline(sp_die)) {
445 /* Real function */
446 pf->addr = die_get_entrypc(sp_die);
447 pf->addr += pp->offset;
448 /* TODO: Check the address in this function */
449 show_probe_point(sp_die, pf);
450 } else
451 /* Inlined function: search instances */
452 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
453
454 return 1; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400455}
456
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500457static void find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400458{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500459 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400460}
461
462/* Find a probe point */
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500463int find_probe_point(int fd, struct probe_point *pp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400464{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400465 struct probe_finder pf = {.pp = pp};
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500466 int ret;
467 Dwarf_Off off, noff;
468 size_t cuhl;
469 Dwarf_Die *diep;
470 Dwarf *dbg;
471 int fno = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400472
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500473 dbg = dwarf_begin(fd, DWARF_C_READ);
474 if (!dbg)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500475 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400476
477 pp->found = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500478 off = 0;
479 /* Loop on CUs (Compilation Unit) */
480 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400481 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500482 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
483 if (!diep)
484 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400485
486 /* Check if target file is included. */
487 if (pp->file)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500488 fno = cu_find_fileno(&pf.cu_die, pp->file);
489 else
490 fno = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400491
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500492 if (!pp->file || fno) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400493 /* Save CU base address (for frame_base) */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500494 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
495 if (ret != 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400496 pf.cu_base = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400497 if (pp->function)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500498 find_probe_point_by_func(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400499 else {
500 pf.lno = pp->line;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500501 find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400502 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400503 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500504 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400505 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500506 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400507
508 return pp->found;
509}
510
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500511
512static void line_range_add_line(struct line_range *lr, unsigned int line)
513{
514 struct line_node *ln;
515 struct list_head *p;
516
517 /* Reverse search, because new line will be the last one */
518 list_for_each_entry_reverse(ln, &lr->line_list, list) {
519 if (ln->line < line) {
520 p = &ln->list;
521 goto found;
522 } else if (ln->line == line) /* Already exist */
523 return ;
524 }
525 /* List is empty, or the smallest entry */
526 p = &lr->line_list;
527found:
528 pr_debug("Debug: add a line %u\n", line);
529 ln = zalloc(sizeof(struct line_node));
530 DIE_IF(ln == NULL);
531 ln->line = line;
532 INIT_LIST_HEAD(&ln->list);
533 list_add(&ln->list, p);
534}
535
536/* Find line range from its line number */
537static void find_line_range_by_line(struct line_finder *lf)
538{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500539 Dwarf_Lines *lines;
540 Dwarf_Line *line;
541 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500542 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500543 int lineno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500544 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500545 const char *src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500546
Masami Hiramatsu3cb8bc62010-02-25 08:35:27 -0500547 INIT_LIST_HEAD(&lf->lr->line_list);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500548 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
549 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500550
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500551 for (i = 0; i < nlines; i++) {
552 line = dwarf_onesrcline(lines, i);
553 dwarf_lineno(line, &lineno);
554 if (lf->lno_s > lineno || lf->lno_e < lineno)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500555 continue;
556
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500557 /* TODO: Get fileno from line, but how? */
558 src = dwarf_linesrc(line, NULL, NULL);
559 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500560 continue;
561
562 /* Filter line in the function address range */
563 if (lf->addr_s && lf->addr_e) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500564 ret = dwarf_lineaddr(line, &addr);
565 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500566 if (lf->addr_s > addr || lf->addr_e <= addr)
567 continue;
568 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500569 /* Copy real path */
570 if (!lf->lr->path)
571 lf->lr->path = strdup(src);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500572 line_range_add_line(lf->lr, (unsigned int)lineno);
573 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500574 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500575 if (!list_empty(&lf->lr->line_list))
576 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500577 else {
578 free(lf->lr->path);
579 lf->lr->path = NULL;
580 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500581}
582
583/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500584static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500585{
586 struct line_finder *lf = (struct line_finder *)data;
587 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500588 int ret;
589
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500590 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
591 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500592 /* Get the address range of this function */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500593 ret = dwarf_highpc(sp_die, &lf->addr_e);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500594 if (ret == 0)
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500595 ret = dwarf_lowpc(sp_die, &lf->addr_s);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500596 if (ret != 0) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500597 lf->addr_s = 0;
598 lf->addr_e = 0;
599 }
600
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500601 lf->fname = dwarf_decl_file(sp_die);
602 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500603 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500604 lf->lno_s = lr->offset + lr->start;
605 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500606 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500607 else
608 lf->lno_e = lr->offset + lr->end;
609 lr->start = lf->lno_s;
610 lr->end = lf->lno_e;
611 find_line_range_by_line(lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500612 return 1;
613 }
614 return 0;
615}
616
617static void find_line_range_by_func(struct line_finder *lf)
618{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500619 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500620}
621
622int find_line_range(int fd, struct line_range *lr)
623{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500624 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500625 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500626 Dwarf_Off off = 0, noff;
627 size_t cuhl;
628 Dwarf_Die *diep;
629 Dwarf *dbg;
630 int fno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500631
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500632 dbg = dwarf_begin(fd, DWARF_C_READ);
633 if (!dbg)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500634 return -ENOENT;
635
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500636 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500637 while (!lf.found) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500638 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
639 if (ret != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500640 break;
641
642 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500643 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
644 if (!diep)
645 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500646
647 /* Check if target file is included. */
648 if (lr->file)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500649 fno = cu_find_fileno(&lf.cu_die, lr->file);
650 else
651 fno = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500652
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500653 if (!lr->file || fno) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500654 if (lr->function)
655 find_line_range_by_func(&lf);
656 else {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500657 lf.fname = lr->file;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500658 lf.lno_s = lr->start;
659 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500660 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500661 else
662 lf.lno_e = lr->end;
663 find_line_range_by_line(&lf);
664 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500665 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500666 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500667 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500668 pr_debug("path: %lx\n", (unsigned long)lr->path);
669 dwarf_end(dbg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500670 return lf.found;
671}
672