blob: c422472fe4d1c6217a828edd0f0e746c7a8045ee [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
41/* Dwarf_Die Linkage to parent Die */
42struct die_link {
43 struct die_link *parent; /* Parent die */
44 Dwarf_Die die; /* Current die */
45};
46
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040047
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040048/*
49 * Generic dwarf analysis helpers
50 */
51
52#define X86_32_MAX_REGS 8
53const char *x86_32_regs_table[X86_32_MAX_REGS] = {
54 "%ax",
55 "%cx",
56 "%dx",
57 "%bx",
58 "$stack", /* Stack address instead of %sp */
59 "%bp",
60 "%si",
61 "%di",
62};
63
64#define X86_64_MAX_REGS 16
65const char *x86_64_regs_table[X86_64_MAX_REGS] = {
66 "%ax",
67 "%dx",
68 "%cx",
69 "%bx",
70 "%si",
71 "%di",
72 "%bp",
73 "%sp",
74 "%r8",
75 "%r9",
76 "%r10",
77 "%r11",
78 "%r12",
79 "%r13",
80 "%r14",
81 "%r15",
82};
83
84/* TODO: switching by dwarf address size */
85#ifdef __x86_64__
86#define ARCH_MAX_REGS X86_64_MAX_REGS
87#define arch_regs_table x86_64_regs_table
88#else
89#define ARCH_MAX_REGS X86_32_MAX_REGS
90#define arch_regs_table x86_32_regs_table
91#endif
92
93/* Return architecture dependent register string (for kprobe-tracer) */
94static const char *get_arch_regstr(unsigned int n)
95{
96 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
97}
98
99/*
100 * Compare the tail of two strings.
101 * Return 0 if whole of either string is same as another's tail part.
102 */
103static int strtailcmp(const char *s1, const char *s2)
104{
105 int i1 = strlen(s1);
106 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -0500107 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400108 if (s1[i1] != s2[i2])
109 return s1[i1] - s2[i2];
110 }
111 return 0;
112}
113
114/* Find the fileno of the target file. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500115static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400116{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500117 Dwarf_Files *files;
118 size_t nfiles, i;
119 const char *src;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400120 int ret;
121
122 if (!fname)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500123 return -EINVAL;
124
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500125 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
126 if (ret == 0) {
127 for (i = 0; i < nfiles; i++) {
128 src = dwarf_filesrc(files, i, NULL, NULL);
129 if (strtailcmp(src, fname) == 0) {
130 ret = (int)i; /*???: +1 or not?*/
131 break;
132 }
133 }
134 if (ret)
135 pr_debug("found fno: %d\n", ret);
136 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500137 return ret;
138}
139
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500140struct __addr_die_search_param {
141 Dwarf_Addr addr;
142 Dwarf_Die *die_mem;
143};
144
145static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
146{
147 struct __addr_die_search_param *ad = data;
148
149 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
150 dwarf_haspc(fn_die, ad->addr)) {
151 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
152 return DWARF_CB_ABORT;
153 }
154 return DWARF_CB_OK;
155}
156
157/* Search a real subprogram including this line, */
158static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
159 Dwarf_Die *die_mem)
160{
161 struct __addr_die_search_param ad;
162 ad.addr = addr;
163 ad.die_mem = die_mem;
164 /* dwarf_getscopes can't find subprogram. */
165 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
166 return NULL;
167 else
168 return die_mem;
169}
170
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400171/* Compare diename and tname */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500172static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400173{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500174 const char *name;
175 name = dwarf_diename(dw_die);
176 DIE_IF(name == NULL);
177 return strcmp(tname, name);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400178}
179
180/* Check the address is in the subprogram(function). */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500181static bool die_within_subprogram(Dwarf_Die *sp_die, Dwarf_Addr addr,
182 size_t *offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400183{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500184 Dwarf_Addr epc;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400185 int ret;
186
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500187 ret = dwarf_haspc(sp_die, addr);
188 if (ret <= 0)
189 return false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400190
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500191 if (offs) {
192 ret = dwarf_entrypc(sp_die, &epc);
193 DIE_IF(ret == -1);
194 *offs = addr - epc;
195 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400196
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500197 return true;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400198}
199
200/* Get entry pc(or low pc, 1st entry of ranges) of the die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500201static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400202{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500203 Dwarf_Addr epc;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400204 int ret;
205
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500206 ret = dwarf_entrypc(dw_die, &epc);
207 DIE_IF(ret == -1);
208 return epc;
209}
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400210
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500211/* Check if the abstract origin's address or not */
212static bool die_compare_abstract_origin(Dwarf_Die *in_die, void *origin_addr)
213{
214 Dwarf_Attribute attr;
215 Dwarf_Die origin;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400216
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500217 if (!dwarf_attr(in_die, DW_AT_abstract_origin, &attr))
218 return false;
219 if (!dwarf_formref_die(&attr, &origin))
220 return false;
221
222 return origin.addr == origin_addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400223}
224
225/*
226 * Search a Die from Die tree.
227 * Note: cur_link->die should be deallocated in this function.
228 */
229static int __search_die_tree(struct die_link *cur_link,
230 int (*die_cb)(struct die_link *, void *),
231 void *data)
232{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400233 struct die_link new_link;
234 int ret;
235
236 if (!die_cb)
237 return 0;
238
239 /* Check current die */
240 while (!(ret = die_cb(cur_link, data))) {
241 /* Check child die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500242 ret = dwarf_child(&cur_link->die, &new_link.die);
243 if (ret == 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400244 new_link.parent = cur_link;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400245 ret = __search_die_tree(&new_link, die_cb, data);
246 if (ret)
247 break;
248 }
249
250 /* Move to next sibling */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500251 ret = dwarf_siblingof(&cur_link->die, &cur_link->die);
252 if (ret != 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400253 return 0;
254 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400255 return ret;
256}
257
258/* Search a die in its children's die tree */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500259static int search_die_from_children(Dwarf_Die *parent_die,
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400260 int (*die_cb)(struct die_link *, void *),
261 void *data)
262{
263 struct die_link new_link;
264 int ret;
265
266 new_link.parent = NULL;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500267 ret = dwarf_child(parent_die, &new_link.die);
268 if (ret == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400269 return __search_die_tree(&new_link, die_cb, data);
270 else
271 return 0;
272}
273
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400274
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400275/*
276 * Probe finder related functions
277 */
278
279/* Show a location */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500280static void show_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400281{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500282 unsigned int regn;
283 Dwarf_Word offs = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400284 int deref = 0, ret;
285 const char *regs;
286
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500287 /* TODO: support CFA */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400288 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500289 if (op->atom == DW_OP_fbreg) {
290 if (pf->fb_ops == NULL)
291 die("The attribute of frame base is not supported.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400292 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500293 offs = op->number;
294 op = &pf->fb_ops[0];
295 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400296
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500297 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
298 regn = op->atom - DW_OP_breg0;
299 offs += op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400300 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500301 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
302 regn = op->atom - DW_OP_reg0;
303 } else if (op->atom == DW_OP_bregx) {
304 regn = op->number;
305 offs += op->number2;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400306 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500307 } else if (op->atom == DW_OP_regx) {
308 regn = op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400309 } else
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500310 die("DW_OP %d is not supported.", op->atom);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400311
312 regs = get_arch_regstr(regn);
313 if (!regs)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500314 die("%u exceeds max register number.", regn);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400315
316 if (deref)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500317 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
318 pf->var, (uintmax_t)offs, regs);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400319 else
320 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400321 DIE_IF(ret < 0);
322 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400323}
324
325/* Show a variables in kprobe event format */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500326static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400327{
328 Dwarf_Attribute attr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500329 Dwarf_Op *expr;
330 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400331 int ret;
332
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500333 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400334 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500335 /* TODO: handle more than 1 exprs */
336 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
337 &expr, &nexpr, 1);
338 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400339 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500340
341 show_location(expr, pf);
342 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400343 return ;
344error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500345 /* TODO: Support const_value */
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400346 die("Failed to find the location of %s at this address.\n"
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500347 " Perhaps, it has been optimized out.", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400348}
349
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500350static int variable_search_cb(struct die_link *dlink, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400351{
352 struct probe_finder *pf = (struct probe_finder *)data;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500353 int tag;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400354
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500355 tag = dwarf_tag(&dlink->die);
356 DIE_IF(tag < 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400357 if ((tag == DW_TAG_formal_parameter ||
358 tag == DW_TAG_variable) &&
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500359 (die_compare_name(&dlink->die, pf->var) == 0)) {
360 show_variable(&dlink->die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400361 return 1;
362 }
363 /* TODO: Support struct members and arrays */
364 return 0;
365}
366
367/* Find a variable in a subprogram die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500368static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400369{
370 int ret;
371
372 if (!is_c_varname(pf->var)) {
373 /* Output raw parameters */
374 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400375 DIE_IF(ret < 0);
376 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400377 return ;
378 }
379
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200380 pr_debug("Searching '%s' variable in context.\n", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400381 /* Search child die for local variables and parameters. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500382 ret = search_die_from_children(sp_die, variable_search_cb, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400383 if (!ret)
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500384 die("Failed to find '%s' in this function.", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400385}
386
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400387/* Show a probe point to output buffer */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500388static void show_probe_point(Dwarf_Die *sp_die, size_t offs,
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500389 struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400390{
391 struct probe_point *pp = pf->pp;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500392 const char *name;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400393 char tmp[MAX_PROBE_BUFFER];
394 int ret, i, len;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500395 Dwarf_Attribute fb_attr;
396 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400397
398 /* Output name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500399 name = dwarf_diename(sp_die);
400 if (name) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400401 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%u", name,
402 (unsigned int)offs);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400403 /* Copy the function name if possible */
404 if (!pp->function) {
405 pp->function = strdup(name);
406 pp->offset = offs;
407 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400408 } else {
409 /* This function has no name. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500410 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
411 (uintmax_t)pf->addr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400412 if (!pp->function) {
413 /* TODO: Use _stext */
414 pp->function = strdup("");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500415 pp->offset = (size_t)pf->addr;
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400416 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400417 }
Masami Hiramatsu97698332009-10-16 20:08:18 -0400418 DIE_IF(ret < 0);
419 DIE_IF(ret >= MAX_PROBE_BUFFER);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400420 len = ret;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400421 pr_debug("Probe point found: %s\n", tmp);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400422
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500423 /* Get the frame base attribute/ops */
424 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
425 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
426 &pf->fb_ops, &nops, 1);
427 if (ret <= 0 || nops == 0)
428 pf->fb_ops = NULL;
429
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400430 /* Find each argument */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500431 /* TODO: use dwarf_cfi_addrframe */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400432 for (i = 0; i < pp->nr_args; i++) {
433 pf->var = pp->args[i];
434 pf->buf = &tmp[len];
435 pf->len = MAX_PROBE_BUFFER - len;
436 find_variable(sp_die, pf);
437 len += strlen(pf->buf);
438 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500439
440 /* *pf->fb_ops will be cached in libdw. Don't free it. */
441 pf->fb_ops = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400442
443 pp->probes[pp->found] = strdup(tmp);
444 pp->found++;
445}
446
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400447/* Find probe point from its line number */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500448static void find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400449{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500450 Dwarf_Lines *lines;
451 Dwarf_Line *line;
452 size_t nlines, i;
453 Dwarf_Addr addr, epc;
454 int lineno;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400455 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500456 Dwarf_Die *sp_die, die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400457
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500458 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
459 DIE_IF(ret != 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400460
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500461 for (i = 0; i < nlines; i++) {
462 line = dwarf_onesrcline(lines, i);
463 dwarf_lineno(line, &lineno);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400464 if (lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400465 continue;
466
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500467 /* TODO: Get fileno from line, but how? */
468 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
469 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400470
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500471 ret = dwarf_lineaddr(line, &addr);
472 DIE_IF(ret != 0);
473 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
474 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400475 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500476
477 sp_die = die_get_real_subprogram(&pf->cu_die, addr, &die_mem);
478 if (!sp_die)
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500479 die("Probe point is not found in subprograms.");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500480 dwarf_entrypc(sp_die, &epc);
481 show_probe_point(sp_die, (size_t)(addr - epc), pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400482 /* Continuing, because target line might be inlined. */
483 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400484}
485
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500486
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400487/* Search function from function name */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500488static int probe_point_search_cb(struct die_link *dlink, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400489{
490 struct probe_finder *pf = (struct probe_finder *)data;
491 struct probe_point *pp = pf->pp;
492 struct die_link *lk;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500493 size_t offs;
494 int tag;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400495 int ret;
496
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500497 tag = dwarf_tag(&dlink->die);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400498 if (tag == DW_TAG_subprogram) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500499 if (die_compare_name(&dlink->die, pp->function) == 0) {
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400500 if (pp->line) { /* Function relative line */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500501 pf->fname = dwarf_decl_file(&dlink->die);
502 dwarf_decl_line(&dlink->die, &pf->lno);
503 pf->lno += pp->line;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500504 find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400505 return 1;
506 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500507 if (dwarf_func_inline(&dlink->die)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400508 /* Inlined function, save it. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500509 pf->origin = dlink->die.addr;
Masami Hiramatsu8030c5f2009-10-27 16:42:53 -0400510 return 0; /* Continue to search */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400511 }
512 /* Get probe address */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500513 pf->addr = die_get_entrypc(&dlink->die);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400514 pf->addr += pp->offset;
515 /* TODO: Check the address in this function */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500516 show_probe_point(&dlink->die, pp->offset, pf);
Masami Hiramatsu8030c5f2009-10-27 16:42:53 -0400517 return 1; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400518 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500519 } else if (tag == DW_TAG_inlined_subroutine && pf->origin) {
520 if (die_compare_abstract_origin(&dlink->die, pf->origin)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400521 /* Get probe address */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500522 pf->addr = die_get_entrypc(&dlink->die);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400523 pf->addr += pp->offset;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500524 pr_debug("found inline addr: 0x%jx\n",
525 (uintmax_t)pf->addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400526 /* Inlined function. Get a real subprogram */
527 for (lk = dlink->parent; lk != NULL; lk = lk->parent) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500528 tag = dwarf_tag(&lk->die);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400529 if (tag == DW_TAG_subprogram &&
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500530 !dwarf_func_inline(&lk->die))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400531 goto found;
532 }
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500533 die("Failed to find real subprogram.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400534found:
535 /* Get offset from subprogram */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500536 ret = die_within_subprogram(&lk->die, pf->addr, &offs);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400537 DIE_IF(!ret);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500538 show_probe_point(&lk->die, offs, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400539 /* Continue to search */
540 }
541 }
542 return 0;
543}
544
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500545static void find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400546{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500547 search_die_from_children(&pf->cu_die, probe_point_search_cb, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400548}
549
550/* Find a probe point */
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500551int find_probe_point(int fd, struct probe_point *pp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400552{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400553 struct probe_finder pf = {.pp = pp};
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500554 int ret;
555 Dwarf_Off off, noff;
556 size_t cuhl;
557 Dwarf_Die *diep;
558 Dwarf *dbg;
559 int fno = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400560
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500561 dbg = dwarf_begin(fd, DWARF_C_READ);
562 if (!dbg)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500563 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400564
565 pp->found = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500566 off = 0;
567 /* Loop on CUs (Compilation Unit) */
568 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400569 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500570 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
571 if (!diep)
572 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400573
574 /* Check if target file is included. */
575 if (pp->file)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500576 fno = cu_find_fileno(&pf.cu_die, pp->file);
577 else
578 fno = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400579
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500580 if (!pp->file || fno) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400581 /* Save CU base address (for frame_base) */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500582 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
583 if (ret != 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400584 pf.cu_base = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400585 if (pp->function)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500586 find_probe_point_by_func(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400587 else {
588 pf.lno = pp->line;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500589 find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400590 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400591 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500592 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400593 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500594 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400595
596 return pp->found;
597}
598
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500599
600static void line_range_add_line(struct line_range *lr, unsigned int line)
601{
602 struct line_node *ln;
603 struct list_head *p;
604
605 /* Reverse search, because new line will be the last one */
606 list_for_each_entry_reverse(ln, &lr->line_list, list) {
607 if (ln->line < line) {
608 p = &ln->list;
609 goto found;
610 } else if (ln->line == line) /* Already exist */
611 return ;
612 }
613 /* List is empty, or the smallest entry */
614 p = &lr->line_list;
615found:
616 pr_debug("Debug: add a line %u\n", line);
617 ln = zalloc(sizeof(struct line_node));
618 DIE_IF(ln == NULL);
619 ln->line = line;
620 INIT_LIST_HEAD(&ln->list);
621 list_add(&ln->list, p);
622}
623
624/* Find line range from its line number */
625static void find_line_range_by_line(struct line_finder *lf)
626{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500627 Dwarf_Lines *lines;
628 Dwarf_Line *line;
629 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500630 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500631 int lineno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500632 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500633 const char *src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500634
Masami Hiramatsu3cb8bc62010-02-25 08:35:27 -0500635 INIT_LIST_HEAD(&lf->lr->line_list);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500636 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
637 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500638
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500639 for (i = 0; i < nlines; i++) {
640 line = dwarf_onesrcline(lines, i);
641 dwarf_lineno(line, &lineno);
642 if (lf->lno_s > lineno || lf->lno_e < lineno)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500643 continue;
644
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500645 /* TODO: Get fileno from line, but how? */
646 src = dwarf_linesrc(line, NULL, NULL);
647 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500648 continue;
649
650 /* Filter line in the function address range */
651 if (lf->addr_s && lf->addr_e) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500652 ret = dwarf_lineaddr(line, &addr);
653 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500654 if (lf->addr_s > addr || lf->addr_e <= addr)
655 continue;
656 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500657 /* Copy real path */
658 if (!lf->lr->path)
659 lf->lr->path = strdup(src);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500660 line_range_add_line(lf->lr, (unsigned int)lineno);
661 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500662 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500663 if (!list_empty(&lf->lr->line_list))
664 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500665 else {
666 free(lf->lr->path);
667 lf->lr->path = NULL;
668 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500669}
670
671/* Search function from function name */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500672static int line_range_search_cb(struct die_link *dlink, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500673{
674 struct line_finder *lf = (struct line_finder *)data;
675 struct line_range *lr = lf->lr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500676 int tag;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500677 int ret;
678
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500679 tag = dwarf_tag(&dlink->die);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500680 if (tag == DW_TAG_subprogram &&
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500681 die_compare_name(&dlink->die, lr->function) == 0) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500682 /* Get the address range of this function */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500683 ret = dwarf_highpc(&dlink->die, &lf->addr_e);
684 if (ret == 0)
685 ret = dwarf_lowpc(&dlink->die, &lf->addr_s);
686 if (ret != 0) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500687 lf->addr_s = 0;
688 lf->addr_e = 0;
689 }
690
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500691 lf->fname = dwarf_decl_file(&dlink->die);
692 dwarf_decl_line(&dlink->die, &lr->offset);
693 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500694 lf->lno_s = lr->offset + lr->start;
695 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500696 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500697 else
698 lf->lno_e = lr->offset + lr->end;
699 lr->start = lf->lno_s;
700 lr->end = lf->lno_e;
701 find_line_range_by_line(lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500702 return 1;
703 }
704 return 0;
705}
706
707static void find_line_range_by_func(struct line_finder *lf)
708{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500709 search_die_from_children(&lf->cu_die, line_range_search_cb, lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500710}
711
712int find_line_range(int fd, struct line_range *lr)
713{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500714 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500715 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500716 Dwarf_Off off = 0, noff;
717 size_t cuhl;
718 Dwarf_Die *diep;
719 Dwarf *dbg;
720 int fno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500721
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500722 dbg = dwarf_begin(fd, DWARF_C_READ);
723 if (!dbg)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500724 return -ENOENT;
725
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500726 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500727 while (!lf.found) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500728 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
729 if (ret != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500730 break;
731
732 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500733 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
734 if (!diep)
735 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500736
737 /* Check if target file is included. */
738 if (lr->file)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500739 fno = cu_find_fileno(&lf.cu_die, lr->file);
740 else
741 fno = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500742
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500743 if (!lr->file || fno) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500744 if (lr->function)
745 find_line_range_by_func(&lf);
746 else {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500747 lf.fname = lr->file;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500748 lf.lno_s = lr->start;
749 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500750 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500751 else
752 lf.lno_e = lr->end;
753 find_line_range_by_line(&lf);
754 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500755 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500756 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500757 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500758 pr_debug("path: %lx\n", (unsigned long)lr->path);
759 dwarf_end(dbg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500760 return lf.found;
761}
762