blob: c171a243d05b721b7cfdc9b743a73cbbf75099a3 [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 Hiramatsu2a9c8c32010-02-25 08:36:12 -050035#include "string.h"
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040036#include "event.h"
37#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040038#include "util.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040039#include "probe-finder.h"
40
41
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040042/*
43 * Generic dwarf analysis helpers
44 */
45
46#define X86_32_MAX_REGS 8
47const char *x86_32_regs_table[X86_32_MAX_REGS] = {
48 "%ax",
49 "%cx",
50 "%dx",
51 "%bx",
52 "$stack", /* Stack address instead of %sp */
53 "%bp",
54 "%si",
55 "%di",
56};
57
58#define X86_64_MAX_REGS 16
59const char *x86_64_regs_table[X86_64_MAX_REGS] = {
60 "%ax",
61 "%dx",
62 "%cx",
63 "%bx",
64 "%si",
65 "%di",
66 "%bp",
67 "%sp",
68 "%r8",
69 "%r9",
70 "%r10",
71 "%r11",
72 "%r12",
73 "%r13",
74 "%r14",
75 "%r15",
76};
77
78/* TODO: switching by dwarf address size */
79#ifdef __x86_64__
80#define ARCH_MAX_REGS X86_64_MAX_REGS
81#define arch_regs_table x86_64_regs_table
82#else
83#define ARCH_MAX_REGS X86_32_MAX_REGS
84#define arch_regs_table x86_32_regs_table
85#endif
86
87/* Return architecture dependent register string (for kprobe-tracer) */
88static const char *get_arch_regstr(unsigned int n)
89{
90 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
91}
92
93/*
94 * Compare the tail of two strings.
95 * Return 0 if whole of either string is same as another's tail part.
96 */
97static int strtailcmp(const char *s1, const char *s2)
98{
99 int i1 = strlen(s1);
100 int i2 = strlen(s2);
Juha Leppanend56728b2009-12-07 12:00:40 -0500101 while (--i1 >= 0 && --i2 >= 0) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400102 if (s1[i1] != s2[i2])
103 return s1[i1] - s2[i2];
104 }
105 return 0;
106}
107
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500108/* Line number list operations */
109
110/* Add a line to line number list */
111static void line_list__add_line(struct list_head *head, unsigned int line)
112{
113 struct line_node *ln;
114 struct list_head *p;
115
116 /* Reverse search, because new line will be the last one */
117 list_for_each_entry_reverse(ln, head, list) {
118 if (ln->line < line) {
119 p = &ln->list;
120 goto found;
121 } else if (ln->line == line) /* Already exist */
122 return ;
123 }
124 /* List is empty, or the smallest entry */
125 p = head;
126found:
127 pr_debug("line list: add a line %u\n", line);
128 ln = zalloc(sizeof(struct line_node));
129 DIE_IF(ln == NULL);
130 ln->line = line;
131 INIT_LIST_HEAD(&ln->list);
132 list_add(&ln->list, p);
133}
134
135/* Check if the line in line number list */
136static int line_list__has_line(struct list_head *head, unsigned int line)
137{
138 struct line_node *ln;
139
140 /* Reverse search, because new line will be the last one */
141 list_for_each_entry(ln, head, list)
142 if (ln->line == line)
143 return 1;
144
145 return 0;
146}
147
148/* Init line number list */
149static void line_list__init(struct list_head *head)
150{
151 INIT_LIST_HEAD(head);
152}
153
154/* Free line number list */
155static void line_list__free(struct list_head *head)
156{
157 struct line_node *ln;
158 while (!list_empty(head)) {
159 ln = list_first_entry(head, struct line_node, list);
160 list_del(&ln->list);
161 free(ln);
162 }
163}
164
165/* Dwarf wrappers */
166
167/* Find the realpath of the target file. */
168static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400169{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500170 Dwarf_Files *files;
171 size_t nfiles, i;
Arnaldo Carvalho de Meloaccd3cc2010-03-05 12:51:04 -0300172 const char *src = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400173 int ret;
174
175 if (!fname)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500176 return NULL;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500177
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500178 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500179 if (ret != 0)
180 return NULL;
181
182 for (i = 0; i < nfiles; i++) {
183 src = dwarf_filesrc(files, i, NULL, NULL);
184 if (strtailcmp(src, fname) == 0)
185 break;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500186 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500187 return src;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500188}
189
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500190struct __addr_die_search_param {
191 Dwarf_Addr addr;
192 Dwarf_Die *die_mem;
193};
194
195static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
196{
197 struct __addr_die_search_param *ad = data;
198
199 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
200 dwarf_haspc(fn_die, ad->addr)) {
201 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
202 return DWARF_CB_ABORT;
203 }
204 return DWARF_CB_OK;
205}
206
207/* Search a real subprogram including this line, */
208static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
209 Dwarf_Die *die_mem)
210{
211 struct __addr_die_search_param ad;
212 ad.addr = addr;
213 ad.die_mem = die_mem;
214 /* dwarf_getscopes can't find subprogram. */
215 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
216 return NULL;
217 else
218 return die_mem;
219}
220
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500221/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
222static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
223 Dwarf_Die *die_mem)
224{
225 Dwarf_Die child_die;
226 int ret;
227
228 ret = dwarf_child(sp_die, die_mem);
229 if (ret != 0)
230 return NULL;
231
232 do {
233 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
234 dwarf_haspc(die_mem, addr))
235 return die_mem;
236
237 if (die_get_inlinefunc(die_mem, addr, &child_die)) {
238 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
239 return die_mem;
240 }
241 } while (dwarf_siblingof(die_mem, die_mem) == 0);
242
243 return NULL;
244}
245
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400246/* Compare diename and tname */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500247static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400248{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500249 const char *name;
250 name = dwarf_diename(dw_die);
251 DIE_IF(name == NULL);
252 return strcmp(tname, name);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400253}
254
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400255/* Get entry pc(or low pc, 1st entry of ranges) of the die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500256static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400257{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500258 Dwarf_Addr epc;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400259 int ret;
260
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500261 ret = dwarf_entrypc(dw_die, &epc);
262 DIE_IF(ret == -1);
263 return epc;
264}
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400265
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500266/* Get a variable die */
267static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
268 Dwarf_Die *die_mem)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500269{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500270 Dwarf_Die child_die;
271 int tag;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400272 int ret;
273
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500274 ret = dwarf_child(sp_die, die_mem);
275 if (ret != 0)
276 return NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400277
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500278 do {
279 tag = dwarf_tag(die_mem);
280 if ((tag == DW_TAG_formal_parameter ||
281 tag == DW_TAG_variable) &&
282 (die_compare_name(die_mem, name) == 0))
283 return die_mem;
284
285 if (die_find_variable(die_mem, name, &child_die)) {
286 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
287 return die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400288 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500289 } while (dwarf_siblingof(die_mem, die_mem) == 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400290
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500291 return NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400292}
293
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400294/*
295 * Probe finder related functions
296 */
297
298/* Show a location */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500299static void show_location(Dwarf_Op *op, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400300{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500301 unsigned int regn;
302 Dwarf_Word offs = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400303 int deref = 0, ret;
304 const char *regs;
305
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500306 /* TODO: support CFA */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400307 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500308 if (op->atom == DW_OP_fbreg) {
309 if (pf->fb_ops == NULL)
310 die("The attribute of frame base is not supported.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400311 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500312 offs = op->number;
313 op = &pf->fb_ops[0];
314 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400315
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500316 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
317 regn = op->atom - DW_OP_breg0;
318 offs += op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400319 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500320 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
321 regn = op->atom - DW_OP_reg0;
322 } else if (op->atom == DW_OP_bregx) {
323 regn = op->number;
324 offs += op->number2;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400325 deref = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500326 } else if (op->atom == DW_OP_regx) {
327 regn = op->number;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400328 } else
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500329 die("DW_OP %d is not supported.", op->atom);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400330
331 regs = get_arch_regstr(regn);
332 if (!regs)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500333 die("%u exceeds max register number.", regn);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400334
335 if (deref)
Masami Hiramatsu67c7ff72010-03-15 13:02:28 -0400336 ret = snprintf(pf->buf, pf->len, " %s=%+jd(%s)",
337 pf->var, (intmax_t)offs, regs);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400338 else
339 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400340 DIE_IF(ret < 0);
341 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400342}
343
344/* Show a variables in kprobe event format */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500345static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400346{
347 Dwarf_Attribute attr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500348 Dwarf_Op *expr;
349 size_t nexpr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400350 int ret;
351
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500352 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400353 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500354 /* TODO: handle more than 1 exprs */
Masami Hiramatsud0cb4260f2010-03-15 13:02:35 -0400355 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500356 if (ret <= 0 || nexpr == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400357 goto error;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500358
359 show_location(expr, pf);
360 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400361 return ;
362error:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500363 /* TODO: Support const_value */
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400364 die("Failed to find the location of %s at this address.\n"
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500365 " Perhaps, it has been optimized out.", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400366}
367
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400368/* Find a variable in a subprogram die */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500369static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400370{
371 int ret;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500372 Dwarf_Die vr_die;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400373
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500374 /* TODO: Support struct members and arrays */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400375 if (!is_c_varname(pf->var)) {
376 /* Output raw parameters */
377 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
Masami Hiramatsu97698332009-10-16 20:08:18 -0400378 DIE_IF(ret < 0);
379 DIE_IF(ret >= pf->len);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400380 return ;
381 }
382
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200383 pr_debug("Searching '%s' variable in context.\n", pf->var);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400384 /* Search child die for local variables and parameters. */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500385 if (!die_find_variable(sp_die, pf->var, &vr_die))
Masami Hiramatsubbaa46f2010-01-05 17:47:03 -0500386 die("Failed to find '%s' in this function.", pf->var);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500387
388 show_variable(&vr_die, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400389}
390
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400391/* Show a probe point to output buffer */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500392static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400393{
394 struct probe_point *pp = pf->pp;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500395 Dwarf_Addr eaddr;
396 Dwarf_Die die_mem;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500397 const char *name;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400398 char tmp[MAX_PROBE_BUFFER];
399 int ret, i, len;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500400 Dwarf_Attribute fb_attr;
401 size_t nops;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400402
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500403 /* If no real subprogram, find a real one */
404 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
405 sp_die = die_get_real_subprogram(&pf->cu_die,
406 pf->addr, &die_mem);
407 if (!sp_die)
408 die("Probe point is not found in subprograms.");
409 }
410
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400411 /* Output name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500412 name = dwarf_diename(sp_die);
413 if (name) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500414 dwarf_entrypc(sp_die, &eaddr);
415 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
416 (unsigned long)(pf->addr - eaddr));
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400417 /* Copy the function name if possible */
418 if (!pp->function) {
419 pp->function = strdup(name);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500420 pp->offset = (size_t)(pf->addr - eaddr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400421 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400422 } else {
423 /* This function has no name. */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500424 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
425 (uintmax_t)pf->addr);
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400426 if (!pp->function) {
427 /* TODO: Use _stext */
428 pp->function = strdup("");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500429 pp->offset = (size_t)pf->addr;
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400430 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400431 }
Masami Hiramatsu97698332009-10-16 20:08:18 -0400432 DIE_IF(ret < 0);
433 DIE_IF(ret >= MAX_PROBE_BUFFER);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400434 len = ret;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400435 pr_debug("Probe point found: %s\n", tmp);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400436
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500437 /* Get the frame base attribute/ops */
438 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4260f2010-03-15 13:02:35 -0400439 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500440 if (ret <= 0 || nops == 0)
441 pf->fb_ops = NULL;
442
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400443 /* Find each argument */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500444 /* TODO: use dwarf_cfi_addrframe */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400445 for (i = 0; i < pp->nr_args; i++) {
446 pf->var = pp->args[i];
447 pf->buf = &tmp[len];
448 pf->len = MAX_PROBE_BUFFER - len;
449 find_variable(sp_die, pf);
450 len += strlen(pf->buf);
451 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500452
453 /* *pf->fb_ops will be cached in libdw. Don't free it. */
454 pf->fb_ops = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400455
Masami Hiramatsu594087a2010-03-12 18:22:17 -0500456 if (pp->found == MAX_PROBES)
457 die("Too many( > %d) probe point found.\n", MAX_PROBES);
458
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400459 pp->probes[pp->found] = strdup(tmp);
460 pp->found++;
461}
462
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400463/* Find probe point from its line number */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500464static void find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400465{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500466 Dwarf_Lines *lines;
467 Dwarf_Line *line;
468 size_t nlines, i;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500469 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500470 int lineno;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400471 int ret;
472
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500473 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
474 DIE_IF(ret != 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400475
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500476 for (i = 0; i < nlines; i++) {
477 line = dwarf_onesrcline(lines, i);
478 dwarf_lineno(line, &lineno);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400479 if (lineno != pf->lno)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400480 continue;
481
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500482 /* TODO: Get fileno from line, but how? */
483 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
484 continue;
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400485
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500486 ret = dwarf_lineaddr(line, &addr);
487 DIE_IF(ret != 0);
488 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
489 (int)i, lineno, (uintmax_t)addr);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400490 pf->addr = addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500491
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500492 show_probe_point(NULL, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400493 /* Continuing, because target line might be inlined. */
494 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400495}
496
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500497/* Find lines which match lazy pattern */
498static int find_lazy_match_lines(struct list_head *head,
499 const char *fname, const char *pat)
500{
501 char *fbuf, *p1, *p2;
502 int fd, line, nlines = 0;
503 struct stat st;
504
505 fd = open(fname, O_RDONLY);
506 if (fd < 0)
507 die("failed to open %s", fname);
508 DIE_IF(fstat(fd, &st) < 0);
509 fbuf = malloc(st.st_size + 2);
510 DIE_IF(fbuf == NULL);
511 DIE_IF(read(fd, fbuf, st.st_size) < 0);
512 close(fd);
513 fbuf[st.st_size] = '\n'; /* Dummy line */
514 fbuf[st.st_size + 1] = '\0';
515 p1 = fbuf;
516 line = 1;
517 while ((p2 = strchr(p1, '\n')) != NULL) {
518 *p2 = '\0';
519 if (strlazymatch(p1, pat)) {
520 line_list__add_line(head, line);
521 nlines++;
522 }
523 line++;
524 p1 = p2 + 1;
525 }
526 free(fbuf);
527 return nlines;
528}
529
530/* Find probe points from lazy pattern */
531static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
532{
533 Dwarf_Lines *lines;
534 Dwarf_Line *line;
535 size_t nlines, i;
536 Dwarf_Addr addr;
537 Dwarf_Die die_mem;
538 int lineno;
539 int ret;
540
541 if (list_empty(&pf->lcache)) {
542 /* Matching lazy line pattern */
543 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
544 pf->pp->lazy_line);
545 if (ret <= 0)
546 die("No matched lines found in %s.", pf->fname);
547 }
548
549 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
550 DIE_IF(ret != 0);
551 for (i = 0; i < nlines; i++) {
552 line = dwarf_onesrcline(lines, i);
553
554 dwarf_lineno(line, &lineno);
555 if (!line_list__has_line(&pf->lcache, lineno))
556 continue;
557
558 /* TODO: Get fileno from line, but how? */
559 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
560 continue;
561
562 ret = dwarf_lineaddr(line, &addr);
563 DIE_IF(ret != 0);
564 if (sp_die) {
565 /* Address filtering 1: does sp_die include addr? */
566 if (!dwarf_haspc(sp_die, addr))
567 continue;
568 /* Address filtering 2: No child include addr? */
569 if (die_get_inlinefunc(sp_die, addr, &die_mem))
570 continue;
571 }
572
573 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
574 (int)i, lineno, (unsigned long long)addr);
575 pf->addr = addr;
576
577 show_probe_point(sp_die, pf);
578 /* Continuing, because target line might be inlined. */
579 }
580 /* TODO: deallocate lines, but how? */
581}
582
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500583static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400584{
585 struct probe_finder *pf = (struct probe_finder *)data;
586 struct probe_point *pp = pf->pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400587
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500588 if (pp->lazy_line)
589 find_probe_point_lazy(in_die, pf);
590 else {
591 /* Get probe address */
592 pf->addr = die_get_entrypc(in_die);
593 pf->addr += pp->offset;
594 pr_debug("found inline addr: 0x%jx\n",
595 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500596
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500597 show_probe_point(in_die, pf);
598 }
599
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500600 return DWARF_CB_OK;
601}
602
603/* Search function from function name */
604static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
605{
606 struct probe_finder *pf = (struct probe_finder *)data;
607 struct probe_point *pp = pf->pp;
608
609 /* Check tag and diename */
610 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
611 die_compare_name(sp_die, pp->function) != 0)
612 return 0;
613
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500614 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500615 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500616 dwarf_decl_line(sp_die, &pf->lno);
617 pf->lno += pp->line;
618 find_probe_point_by_line(pf);
619 } else if (!dwarf_func_inline(sp_die)) {
620 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500621 if (pp->lazy_line)
622 find_probe_point_lazy(sp_die, pf);
623 else {
624 pf->addr = die_get_entrypc(sp_die);
625 pf->addr += pp->offset;
626 /* TODO: Check the address in this function */
627 show_probe_point(sp_die, pf);
628 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500629 } else
630 /* Inlined function: search instances */
631 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
632
633 return 1; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400634}
635
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500636static void find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400637{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500638 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400639}
640
641/* Find a probe point */
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500642int find_probe_point(int fd, struct probe_point *pp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400643{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400644 struct probe_finder pf = {.pp = pp};
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500645 Dwarf_Off off, noff;
646 size_t cuhl;
647 Dwarf_Die *diep;
648 Dwarf *dbg;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400649
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500650 dbg = dwarf_begin(fd, DWARF_C_READ);
651 if (!dbg)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500652 return -ENOENT;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400653
654 pp->found = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500655 off = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500656 line_list__init(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500657 /* Loop on CUs (Compilation Unit) */
658 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400659 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500660 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
661 if (!diep)
662 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400663
664 /* Check if target file is included. */
665 if (pp->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500666 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500667 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500668 pf.fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400669
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500670 if (!pp->file || pf.fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400671 if (pp->function)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500672 find_probe_point_by_func(&pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500673 else if (pp->lazy_line)
674 find_probe_point_lazy(NULL, &pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400675 else {
676 pf.lno = pp->line;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500677 find_probe_point_by_line(&pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400678 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400679 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500680 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400681 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500682 line_list__free(&pf.lcache);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500683 dwarf_end(dbg);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400684
685 return pp->found;
686}
687
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500688/* Find line range from its line number */
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500689static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500690{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500691 Dwarf_Lines *lines;
692 Dwarf_Line *line;
693 size_t nlines, i;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500694 Dwarf_Addr addr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500695 int lineno;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500696 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500697 const char *src;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500698 Dwarf_Die die_mem;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500699
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500700 line_list__init(&lf->lr->line_list);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500701 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
702 DIE_IF(ret != 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500703
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500704 for (i = 0; i < nlines; i++) {
705 line = dwarf_onesrcline(lines, i);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500706 ret = dwarf_lineno(line, &lineno);
707 DIE_IF(ret != 0);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500708 if (lf->lno_s > lineno || lf->lno_e < lineno)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500709 continue;
710
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500711 if (sp_die) {
712 /* Address filtering 1: does sp_die include addr? */
713 ret = dwarf_lineaddr(line, &addr);
714 DIE_IF(ret != 0);
715 if (!dwarf_haspc(sp_die, addr))
716 continue;
717
718 /* Address filtering 2: No child include addr? */
719 if (die_get_inlinefunc(sp_die, addr, &die_mem))
720 continue;
721 }
722
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500723 /* TODO: Get fileno from line, but how? */
724 src = dwarf_linesrc(line, NULL, NULL);
725 if (strtailcmp(src, lf->fname) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500726 continue;
727
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500728 /* Copy real path */
729 if (!lf->lr->path)
730 lf->lr->path = strdup(src);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500731 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500732 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500733 /* Update status */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500734 if (!list_empty(&lf->lr->line_list))
735 lf->found = 1;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500736 else {
737 free(lf->lr->path);
738 lf->lr->path = NULL;
739 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500740}
741
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500742static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
743{
744 find_line_range_by_line(in_die, (struct line_finder *)data);
745 return DWARF_CB_ABORT; /* No need to find other instances */
746}
747
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500748/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500749static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500750{
751 struct line_finder *lf = (struct line_finder *)data;
752 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500753
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500754 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
755 die_compare_name(sp_die, lr->function) == 0) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500756 lf->fname = dwarf_decl_file(sp_die);
757 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500758 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500759 lf->lno_s = lr->offset + lr->start;
760 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500761 lf->lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500762 else
763 lf->lno_e = lr->offset + lr->end;
764 lr->start = lf->lno_s;
765 lr->end = lf->lno_e;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500766 if (dwarf_func_inline(sp_die))
767 dwarf_func_inline_instances(sp_die,
768 line_range_inline_cb, lf);
769 else
770 find_line_range_by_line(sp_die, lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500771 return 1;
772 }
773 return 0;
774}
775
776static void find_line_range_by_func(struct line_finder *lf)
777{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500778 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500779}
780
781int find_line_range(int fd, struct line_range *lr)
782{
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500783 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500784 int ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500785 Dwarf_Off off = 0, noff;
786 size_t cuhl;
787 Dwarf_Die *diep;
788 Dwarf *dbg;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500789
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500790 dbg = dwarf_begin(fd, DWARF_C_READ);
791 if (!dbg)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500792 return -ENOENT;
793
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500794 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500795 while (!lf.found) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500796 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
797 if (ret != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500798 break;
799
800 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500801 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
802 if (!diep)
803 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500804
805 /* Check if target file is included. */
806 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500807 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500808 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500809 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500810
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500811 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500812 if (lr->function)
813 find_line_range_by_func(&lf);
814 else {
815 lf.lno_s = lr->start;
816 if (!lr->end)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500817 lf.lno_e = INT_MAX;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500818 else
819 lf.lno_e = lr->end;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -0500820 find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500821 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500822 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500823 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500824 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500825 pr_debug("path: %lx\n", (unsigned long)lr->path);
826 dwarf_end(dbg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500827 return lf.found;
828}
829