blob: 53d219bddb48fda9d46c74fce75bc03b39dcf79c [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>
Ian Munsiecd932c52010-04-20 16:58:32 +100034#include <dwarf-regs.h>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040035
Masami Hiramatsu124bb832011-02-04 21:52:11 +090036#include <linux/bitops.h>
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040037#include "event.h"
38#include "debug.h"
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040039#include "util.h"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040040#include "symbol.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040041#include "probe-finder.h"
42
Masami Hiramatsu49849122010-04-12 13:17:15 -040043/* Kprobe tracer basic type is up to u64 */
44#define MAX_BASIC_TYPE_BITS 64
45
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050046/* Line number list operations */
47
48/* Add a line to line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040049static int line_list__add_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050050{
51 struct line_node *ln;
52 struct list_head *p;
53
54 /* Reverse search, because new line will be the last one */
55 list_for_each_entry_reverse(ln, head, list) {
56 if (ln->line < line) {
57 p = &ln->list;
58 goto found;
59 } else if (ln->line == line) /* Already exist */
Masami Hiramatsue334016f12010-04-12 13:17:49 -040060 return 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050061 }
62 /* List is empty, or the smallest entry */
63 p = head;
64found:
65 pr_debug("line list: add a line %u\n", line);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040066 ln = zalloc(sizeof(struct line_node));
67 if (ln == NULL)
68 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050069 ln->line = line;
70 INIT_LIST_HEAD(&ln->list);
71 list_add(&ln->list, p);
Masami Hiramatsue334016f12010-04-12 13:17:49 -040072 return 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050073}
74
75/* Check if the line in line number list */
Masami Hiramatsud3b63d72010-04-14 18:39:42 -040076static int line_list__has_line(struct list_head *head, int line)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -050077{
78 struct line_node *ln;
79
80 /* Reverse search, because new line will be the last one */
81 list_for_each_entry(ln, head, list)
82 if (ln->line == line)
83 return 1;
84
85 return 0;
86}
87
88/* Init line number list */
89static void line_list__init(struct list_head *head)
90{
91 INIT_LIST_HEAD(head);
92}
93
94/* Free line number list */
95static void line_list__free(struct list_head *head)
96{
97 struct line_node *ln;
98 while (!list_empty(head)) {
99 ln = list_first_entry(head, struct line_node, list);
100 list_del(&ln->list);
101 free(ln);
102 }
103}
104
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900105/* Dwarf FL wrappers */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900106static char *debuginfo_path; /* Currently dummy */
107
108static const Dwfl_Callbacks offline_callbacks = {
109 .find_debuginfo = dwfl_standard_find_debuginfo,
110 .debuginfo_path = &debuginfo_path,
111
112 .section_address = dwfl_offline_section_address,
113
114 /* We use this table for core files too. */
115 .find_elf = dwfl_build_id_find_elf,
116};
117
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900118/* Get a Dwarf from offline image */
119static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
120{
121 Dwfl_Module *mod;
122 Dwarf *dbg = NULL;
123
124 if (!dwflp)
125 return NULL;
126
127 *dwflp = dwfl_begin(&offline_callbacks);
128 if (!*dwflp)
129 return NULL;
130
131 mod = dwfl_report_offline(*dwflp, "", "", fd);
132 if (!mod)
133 goto error;
134
135 dbg = dwfl_module_getdwarf(mod, bias);
136 if (!dbg) {
137error:
138 dwfl_end(*dwflp);
139 *dwflp = NULL;
140 }
141 return dbg;
142}
143
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900144#if _ELFUTILS_PREREQ(0, 148)
145/* This method is buggy if elfutils is older than 0.148 */
146static int __linux_kernel_find_elf(Dwfl_Module *mod,
147 void **userdata,
148 const char *module_name,
149 Dwarf_Addr base,
150 char **file_name, Elf **elfp)
151{
152 int fd;
153 const char *path = kernel_get_module_path(module_name);
154
155 pr_debug2("Use file %s for %s\n", path, module_name);
156 if (path) {
157 fd = open(path, O_RDONLY);
158 if (fd >= 0) {
159 *file_name = strdup(path);
160 return fd;
161 }
162 }
163 /* If failed, try to call standard method */
164 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
165 file_name, elfp);
166}
167
168static const Dwfl_Callbacks kernel_callbacks = {
169 .find_debuginfo = dwfl_standard_find_debuginfo,
170 .debuginfo_path = &debuginfo_path,
171
172 .find_elf = __linux_kernel_find_elf,
173 .section_address = dwfl_linux_kernel_module_section_address,
174};
175
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900176/* Get a Dwarf from live kernel image */
177static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
178 Dwarf_Addr *bias)
179{
180 Dwarf *dbg;
181
182 if (!dwflp)
183 return NULL;
184
185 *dwflp = dwfl_begin(&kernel_callbacks);
186 if (!*dwflp)
187 return NULL;
188
189 /* Load the kernel dwarves: Don't care the result here */
190 dwfl_linux_kernel_report_kernel(*dwflp);
191 dwfl_linux_kernel_report_modules(*dwflp);
192
193 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
194 /* Here, check whether we could get a real dwarf */
195 if (!dbg) {
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900196 pr_debug("Failed to find kernel dwarf at %lx\n",
197 (unsigned long)addr);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900198 dwfl_end(*dwflp);
199 *dwflp = NULL;
200 }
201 return dbg;
202}
Masami Hiramatsu3b4694d2010-12-17 22:12:18 +0900203#else
204/* With older elfutils, this just support kernel module... */
205static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
206 Dwarf_Addr *bias)
207{
208 int fd;
209 const char *path = kernel_get_module_path("kernel");
210
211 if (!path) {
212 pr_err("Failed to find vmlinux path\n");
213 return NULL;
214 }
215
216 pr_debug2("Use file %s for debuginfo\n", path);
217 fd = open(path, O_RDONLY);
218 if (fd < 0)
219 return NULL;
220
221 return dwfl_init_offline_dwarf(fd, dwflp, bias);
222}
223#endif
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900224
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400225/*
226 * Probe finder related functions
227 */
228
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530229static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400230{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530231 struct probe_trace_arg_ref *ref;
232 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400233 if (ref != NULL)
234 ref->offset = offs;
235 return ref;
236}
237
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900238/*
239 * Convert a location into trace_arg.
240 * If tvar == NULL, this just checks variable can be converted.
241 */
242static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
243 Dwarf_Op *fb_ops,
244 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400245{
246 Dwarf_Attribute attr;
247 Dwarf_Op *op;
248 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500249 unsigned int regn;
250 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400251 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400252 const char *regs;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400253 int ret;
254
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900255 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
256 goto static_var;
257
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400258 /* TODO: handle more than 1 exprs */
259 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900260 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400261 nops == 0) {
262 /* TODO: Support const_value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400263 return -ENOENT;
264 }
265
266 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900267static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900268 if (!tvar)
269 return 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400270 /* Static variables on memory (not stack), make @varname */
271 ret = strlen(dwarf_diename(vr_die));
272 tvar->value = zalloc(ret + 2);
273 if (tvar->value == NULL)
274 return -ENOMEM;
275 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
276 tvar->ref = alloc_trace_arg_ref((long)offs);
277 if (tvar->ref == NULL)
278 return -ENOMEM;
279 return 0;
280 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400281
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400282 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500283 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900284 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400285 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400286 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500287 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900288 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500289 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400290
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500291 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
292 regn = op->atom - DW_OP_breg0;
293 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400294 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500295 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
296 regn = op->atom - DW_OP_reg0;
297 } else if (op->atom == DW_OP_bregx) {
298 regn = op->number;
299 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400300 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500301 } else if (op->atom == DW_OP_regx) {
302 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400303 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900304 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400305 return -ENOTSUP;
306 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400307
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900308 if (!tvar)
309 return 0;
310
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400311 regs = get_arch_regstr(regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400312 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900313 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900314 pr_warning("Mapping for the register number %u "
315 "missing on this architecture.\n", regn);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400316 return -ERANGE;
317 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400318
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400319 tvar->value = strdup(regs);
320 if (tvar->value == NULL)
321 return -ENOMEM;
322
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400323 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400324 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400325 if (tvar->ref == NULL)
326 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400327 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400328 return 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400329}
330
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900331#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
332
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400333static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530334 struct probe_trace_arg *tvar,
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400335 const char *cast)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400336{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530337 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400338 Dwarf_Die type;
339 char buf[16];
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900340 int bsize, boffs, total;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400341 int ret;
342
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400343 /* TODO: check all types */
344 if (cast && strcmp(cast, "string") != 0) {
345 /* Non string type is OK */
346 tvar->type = strdup(cast);
347 return (tvar->type == NULL) ? -ENOMEM : 0;
348 }
349
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900350 bsize = dwarf_bitsize(vr_die);
351 if (bsize > 0) {
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900352 /* This is a bitfield */
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900353 boffs = dwarf_bitoffset(vr_die);
354 total = dwarf_bytesize(vr_die);
355 if (boffs < 0 || total < 0)
356 return -ENOENT;
357 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
358 BYTES_TO_BITS(total));
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900359 goto formatted;
360 }
361
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400362 if (die_get_real_type(vr_die, &type) == NULL) {
363 pr_warning("Failed to get a type information of %s.\n",
364 dwarf_diename(vr_die));
365 return -ENOENT;
366 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400367
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400368 pr_debug("%s type is %s.\n",
369 dwarf_diename(vr_die), dwarf_diename(&type));
370
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400371 if (cast && strcmp(cast, "string") == 0) { /* String type */
372 ret = dwarf_tag(&type);
373 if (ret != DW_TAG_pointer_type &&
374 ret != DW_TAG_array_type) {
375 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900376 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400377 dwarf_diename(vr_die), dwarf_diename(&type));
378 return -EINVAL;
379 }
380 if (ret == DW_TAG_pointer_type) {
381 if (die_get_real_type(&type, &type) == NULL) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900382 pr_warning("Failed to get a type"
383 " information.\n");
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400384 return -ENOENT;
385 }
386 while (*ref_ptr)
387 ref_ptr = &(*ref_ptr)->next;
388 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530389 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400390 if (*ref_ptr == NULL) {
391 pr_warning("Out of memory error\n");
392 return -ENOMEM;
393 }
394 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900395 if (!die_compare_name(&type, "char") &&
396 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400397 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900398 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400399 dwarf_diename(vr_die));
400 return -EINVAL;
401 }
402 tvar->type = strdup(cast);
403 return (tvar->type == NULL) ? -ENOMEM : 0;
404 }
405
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900406 ret = dwarf_bytesize(&type);
407 if (ret <= 0)
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900408 /* No size ... try to use default type */
409 return 0;
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900410 ret = BYTES_TO_BITS(ret);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400411
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900412 /* Check the bitwidth */
413 if (ret > MAX_BASIC_TYPE_BITS) {
414 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
415 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
416 ret = MAX_BASIC_TYPE_BITS;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400417 }
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900418 ret = snprintf(buf, 16, "%c%d",
419 die_is_signed_type(&type) ? 's' : 'u', ret);
420
421formatted:
422 if (ret < 0 || ret >= 16) {
423 if (ret >= 16)
424 ret = -E2BIG;
425 pr_warning("Failed to convert variable type: %s\n",
426 strerror(-ret));
427 return ret;
428 }
429 tvar->type = strdup(buf);
430 if (tvar->type == NULL)
431 return -ENOMEM;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400432 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400433}
434
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400435static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400436 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530437 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu49849122010-04-12 13:17:15 -0400438 Dwarf_Die *die_mem)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400439{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530440 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400441 Dwarf_Die type;
442 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400443 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400444
445 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400446 if (die_get_real_type(vr_die, &type) == NULL) {
447 pr_warning("Failed to get the type of %s.\n", varname);
448 return -ENOENT;
449 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400450 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
451 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400452
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400453 if (field->name[0] == '[' &&
454 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
455 if (field->next)
456 /* Save original type for next field */
457 memcpy(die_mem, &type, sizeof(*die_mem));
458 /* Get the type of this array */
459 if (die_get_real_type(&type, &type) == NULL) {
460 pr_warning("Failed to get the type of %s.\n", varname);
461 return -ENOENT;
462 }
463 pr_debug2("Array real type: (%x)\n",
464 (unsigned)dwarf_dieoffset(&type));
465 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530466 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400467 if (ref == NULL)
468 return -ENOMEM;
469 if (*ref_ptr)
470 (*ref_ptr)->next = ref;
471 else
472 *ref_ptr = ref;
473 }
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900474 ref->offset += dwarf_bytesize(&type) * field->index;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400475 if (!field->next)
476 /* Save vr_die for converting types */
477 memcpy(die_mem, vr_die, sizeof(*die_mem));
478 goto next;
479 } else if (tag == DW_TAG_pointer_type) {
480 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400481 if (!field->ref) {
482 pr_err("Semantic error: %s must be referred by '->'\n",
483 field->name);
484 return -EINVAL;
485 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400486 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400487 if (die_get_real_type(&type, &type) == NULL) {
488 pr_warning("Failed to get the type of %s.\n", varname);
489 return -ENOENT;
490 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400491 /* Verify it is a data structure */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400492 if (dwarf_tag(&type) != DW_TAG_structure_type) {
493 pr_warning("%s is not a data structure.\n", varname);
494 return -EINVAL;
495 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400496
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530497 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400498 if (ref == NULL)
499 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400500 if (*ref_ptr)
501 (*ref_ptr)->next = ref;
502 else
503 *ref_ptr = ref;
504 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400505 /* Verify it is a data structure */
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400506 if (tag != DW_TAG_structure_type) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400507 pr_warning("%s is not a data structure.\n", varname);
508 return -EINVAL;
509 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400510 if (field->name[0] == '[') {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900511 pr_err("Semantic error: %s is not a pointor"
512 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400513 return -EINVAL;
514 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400515 if (field->ref) {
516 pr_err("Semantic error: %s must be referred by '.'\n",
517 field->name);
518 return -EINVAL;
519 }
520 if (!ref) {
521 pr_warning("Structure on a register is not "
522 "supported yet.\n");
523 return -ENOTSUP;
524 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400525 }
526
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400527 if (die_find_member(&type, field->name, die_mem) == NULL) {
528 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
529 dwarf_diename(&type), field->name);
530 return -EINVAL;
531 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400532
533 /* Get the offset of the field */
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300534 ret = die_get_data_member_location(die_mem, &offs);
535 if (ret < 0) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400536 pr_warning("Failed to get the offset of %s.\n", field->name);
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300537 return ret;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400538 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400539 ref->offset += (long)offs;
540
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400541next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400542 /* Converting next field */
543 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400544 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsude1439d2010-04-14 17:44:00 -0300545 field->next, &ref, die_mem);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400546 else
547 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400548}
549
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400550/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400551static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400552{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400553 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400554 int ret;
555
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400556 pr_debug("Converting variable %s into trace event.\n",
557 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500558
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900559 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
560 pf->tvar);
561 if (ret == -ENOENT)
562 pr_err("Failed to find the location of %s at this address.\n"
563 " Perhaps, it has been optimized out.\n", pf->pvar->var);
564 else if (ret == -ENOTSUP)
565 pr_err("Sorry, we don't support this variable location yet.\n");
566 else if (pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400567 ret = convert_variable_fields(vr_die, pf->pvar->var,
568 pf->pvar->field, &pf->tvar->ref,
569 &die_mem);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400570 vr_die = &die_mem;
571 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400572 if (ret == 0)
573 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500574 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400575 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400576}
577
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400578/* Find a variable in a subprogram die */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400579static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400580{
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400581 Dwarf_Die vr_die, *scopes;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400582 char buf[32], *ptr;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400583 int ret, nscopes;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400584
Masami Hiramatsu367e94c2010-08-27 20:38:59 +0900585 if (!is_c_varname(pf->pvar->var)) {
586 /* Copy raw parameters */
587 pf->tvar->value = strdup(pf->pvar->var);
588 if (pf->tvar->value == NULL)
589 return -ENOMEM;
590 if (pf->pvar->type) {
591 pf->tvar->type = strdup(pf->pvar->type);
592 if (pf->tvar->type == NULL)
593 return -ENOMEM;
594 }
595 if (pf->pvar->name) {
596 pf->tvar->name = strdup(pf->pvar->name);
597 if (pf->tvar->name == NULL)
598 return -ENOMEM;
599 } else
600 pf->tvar->name = NULL;
601 return 0;
602 }
603
Masami Hiramatsu48481932010-04-12 13:16:53 -0400604 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400605 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400606 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400607 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
608 if (ret < 0)
609 return ret;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400610 ptr = strchr(buf, ':'); /* Change type separator to _ */
611 if (ptr)
612 *ptr = '_';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400613 pf->tvar->name = strdup(buf);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400614 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400615 if (pf->tvar->name == NULL)
616 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400617
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400618 pr_debug("Searching '%s' variable in context.\n",
619 pf->pvar->var);
620 /* Search child die for local variables and parameters. */
Masami Hiramatsu378eeaa2010-10-21 19:13:09 +0900621 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400622 ret = convert_variable(&vr_die, pf);
623 else {
624 /* Search upper class */
625 nscopes = dwarf_getscopes_die(sp_die, &scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900626 while (nscopes-- > 1) {
627 pr_debug("Searching variables in %s\n",
628 dwarf_diename(&scopes[nscopes]));
629 /* We should check this scope, so give dummy address */
630 if (die_find_variable_at(&scopes[nscopes],
631 pf->pvar->var, 0,
632 &vr_die)) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400633 ret = convert_variable(&vr_die, pf);
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900634 goto found;
635 }
636 }
637 if (scopes)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400638 free(scopes);
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900639 ret = -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400640 }
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900641found:
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400642 if (ret < 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400643 pr_warning("Failed to find '%s' in this function.\n",
644 pf->pvar->var);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400645 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400646}
647
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900648/* Convert subprogram DIE to trace point */
649static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
650 bool retprobe, struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400651{
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500652 Dwarf_Addr eaddr;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500653 const char *name;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500654
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400655 /* Copy the name of probe point */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500656 name = dwarf_diename(sp_die);
657 if (name) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400658 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900659 pr_warning("Failed to get entry address of %s\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400660 dwarf_diename(sp_die));
661 return -ENOENT;
662 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900663 tp->symbol = strdup(name);
664 if (tp->symbol == NULL)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400665 return -ENOMEM;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900666 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400667 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400668 /* This function has no name. */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900669 tp->offset = (unsigned long)paddr;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400670
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900671 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900672 if (retprobe) {
673 if (eaddr != paddr) {
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900674 pr_warning("Return probe must be on the head of"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900675 " a real function.\n");
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900676 return -EINVAL;
677 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900678 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900679 }
680
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900681 return 0;
682}
683
684/* Call probe_finder callback with real subprogram DIE */
685static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
686{
687 Dwarf_Die die_mem;
688 Dwarf_Attribute fb_attr;
689 size_t nops;
690 int ret;
691
692 /* If no real subprogram, find a real one */
693 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
Masami Hiramatsue0d153c2011-06-27 16:27:27 +0900694 sp_die = die_find_realfunc(&pf->cu_die, pf->addr, &die_mem);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900695 if (!sp_die) {
696 pr_warning("Failed to find probe point in any "
697 "functions.\n");
698 return -ENOENT;
699 }
700 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400701
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500702 /* Get the frame base attribute/ops */
703 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4260f2010-03-15 13:02:35 -0400704 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400705 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500706 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400707#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400708 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
709 pf->cfi != NULL) {
710 Dwarf_Frame *frame;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400711 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
712 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900713 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400714 (uintmax_t)pf->addr);
715 return -ENOENT;
716 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400717#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400718 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500719
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900720 /* Call finder's callback handler */
721 ret = pf->callback(sp_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500722
723 /* *pf->fb_ops will be cached in libdw. Don't free it. */
724 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900725
726 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400727}
728
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900729static int probe_point_line_walker(const char *fname, int lineno,
730 Dwarf_Addr addr, void *data)
731{
732 struct probe_finder *pf = data;
733 int ret;
734
735 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
736 return 0;
737
738 pf->addr = addr;
739 ret = call_probe_finder(NULL, pf);
740
741 /* Continue if no error, because the line will be in inline function */
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -0300742 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900743}
744
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400745/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400746static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400747{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900748 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400749}
750
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500751/* Find lines which match lazy pattern */
752static int find_lazy_match_lines(struct list_head *head,
753 const char *fname, const char *pat)
754{
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100755 FILE *fp;
756 char *line = NULL;
757 size_t line_len;
758 ssize_t len;
759 int count = 0, linenum = 1;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500760
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100761 fp = fopen(fname, "r");
762 if (!fp) {
763 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300764 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400765 }
766
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100767 while ((len = getline(&line, &line_len, fp)) > 0) {
768
769 if (line[len - 1] == '\n')
770 line[len - 1] = '\0';
771
772 if (strlazymatch(line, pat)) {
773 line_list__add_line(head, linenum);
774 count++;
775 }
776 linenum++;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400777 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300778
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100779 if (ferror(fp))
780 count = -errno;
781 free(line);
782 fclose(fp);
783
784 if (count == 0)
785 pr_debug("No matched lines found in %s.\n", fname);
786 return count;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500787}
788
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900789static int probe_point_lazy_walker(const char *fname, int lineno,
790 Dwarf_Addr addr, void *data)
791{
792 struct probe_finder *pf = data;
793 int ret;
794
795 if (!line_list__has_line(&pf->lcache, lineno) ||
796 strtailcmp(fname, pf->fname) != 0)
797 return 0;
798
799 pr_debug("Probe line found: line:%d addr:0x%llx\n",
800 lineno, (unsigned long long)addr);
801 pf->addr = addr;
802 ret = call_probe_finder(NULL, pf);
803
804 /*
805 * Continue if no error, because the lazy pattern will match
806 * to other lines
807 */
Ingo Molnar5e814dd2011-03-15 20:51:09 +0100808 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900809}
810
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500811/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400812static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500813{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400814 int ret = 0;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500815
816 if (list_empty(&pf->lcache)) {
817 /* Matching lazy line pattern */
818 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400819 pf->pev->point.lazy_line);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100820 if (ret <= 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400821 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500822 }
823
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900824 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500825}
826
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400827/* Callback parameter with return value */
828struct dwarf_callback_param {
829 void *data;
830 int retval;
831};
832
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500833static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400834{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400835 struct dwarf_callback_param *param = data;
836 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400837 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400838 Dwarf_Addr addr;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400839
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500840 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400841 param->retval = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500842 else {
843 /* Get probe address */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400844 if (dwarf_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900845 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400846 dwarf_diename(in_die));
847 param->retval = -ENOENT;
848 return DWARF_CB_ABORT;
849 }
850 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500851 pf->addr += pp->offset;
852 pr_debug("found inline addr: 0x%jx\n",
853 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500854
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900855 param->retval = call_probe_finder(in_die, pf);
Masami Hiramatsu5d1ee042010-04-21 15:56:32 -0400856 if (param->retval < 0)
857 return DWARF_CB_ABORT;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500858 }
859
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500860 return DWARF_CB_OK;
861}
862
863/* Search function from function name */
864static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
865{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400866 struct dwarf_callback_param *param = data;
867 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400868 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500869
870 /* Check tag and diename */
871 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
Masami Hiramatsu82175632010-07-09 18:29:17 +0900872 !die_compare_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400873 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500874
Masami Hiramatsu7d216352011-03-30 18:25:41 +0900875 /* Check declared file */
876 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
877 return DWARF_CB_OK;
878
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500879 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500880 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500881 dwarf_decl_line(sp_die, &pf->lno);
882 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400883 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500884 } else if (!dwarf_func_inline(sp_die)) {
885 /* Real function */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500886 if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400887 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500888 else {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400889 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900890 pr_warning("Failed to get entry address of "
891 "%s.\n", dwarf_diename(sp_die));
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400892 param->retval = -ENOENT;
893 return DWARF_CB_ABORT;
894 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500895 pf->addr += pp->offset;
896 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900897 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500898 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400899 } else {
900 struct dwarf_callback_param _param = {.data = (void *)pf,
901 .retval = 0};
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500902 /* Inlined function: search instances */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400903 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
904 &_param);
905 param->retval = _param.retval;
906 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500907
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400908 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400909}
910
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400911static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400912{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400913 struct dwarf_callback_param _param = {.data = (void *)pf,
914 .retval = 0};
915 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
916 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400917}
918
Lin Mingcd25f8b2011-03-25 16:27:48 +0800919struct pubname_callback_param {
920 char *function;
921 char *file;
922 Dwarf_Die *cu_die;
923 Dwarf_Die *sp_die;
924 int found;
925};
926
927static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
928{
929 struct pubname_callback_param *param = data;
930
931 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
932 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
933 return DWARF_CB_OK;
934
935 if (die_compare_name(param->sp_die, param->function)) {
936 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
937 return DWARF_CB_OK;
938
939 if (param->file &&
940 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
941 return DWARF_CB_OK;
942
943 param->found = 1;
944 return DWARF_CB_ABORT;
945 }
946 }
947
948 return DWARF_CB_OK;
949}
950
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900951/* Find probe points from debuginfo */
952static int find_probes(int fd, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400953{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900954 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500955 Dwarf_Off off, noff;
956 size_t cuhl;
957 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900958 Dwarf *dbg = NULL;
959 Dwfl *dwfl;
960 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400961 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400962
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900963 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400964 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900965 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400966 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +0900967 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400968 return -EBADF;
969 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400970
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400971#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400972 /* Get the call frame information from this dwarf */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900973 pf->cfi = dwarf_getcfi(dbg);
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400974#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400975
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500976 off = 0;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900977 line_list__init(&pf->lcache);
Lin Mingcd25f8b2011-03-25 16:27:48 +0800978
979 /* Fastpath: lookup by function name from .debug_pubnames section */
980 if (pp->function) {
981 struct pubname_callback_param pubname_param = {
982 .function = pp->function,
983 .file = pp->file,
984 .cu_die = &pf->cu_die,
985 .sp_die = &pf->sp_die,
Lin Ming2b348a72011-04-29 08:41:57 +0000986 .found = 0,
Lin Mingcd25f8b2011-03-25 16:27:48 +0800987 };
988 struct dwarf_callback_param probe_param = {
989 .data = pf,
990 };
991
992 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
993 if (pubname_param.found) {
994 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
995 if (ret)
996 goto found;
997 }
998 }
999
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001000 /* Loop on CUs (Compilation Unit) */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001001 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001002 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001003 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001004 if (!diep)
1005 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001006
1007 /* Check if target file is included. */
1008 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001009 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001010 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001011 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001012
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001013 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001014 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001015 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001016 else if (pp->lazy_line)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001017 ret = find_probe_point_lazy(NULL, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001018 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001019 pf->lno = pp->line;
1020 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001021 }
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001022 if (ret < 0)
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001023 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001024 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001025 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001026 }
Lin Mingcd25f8b2011-03-25 16:27:48 +08001027
1028found:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001029 line_list__free(&pf->lcache);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001030 if (dwfl)
1031 dwfl_end(dwfl);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001032
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001033 return ret;
1034}
1035
1036/* Add a found probe point into trace event list */
1037static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1038{
1039 struct trace_event_finder *tf =
1040 container_of(pf, struct trace_event_finder, pf);
1041 struct probe_trace_event *tev;
1042 int ret, i;
1043
1044 /* Check number of tevs */
1045 if (tf->ntevs == tf->max_tevs) {
1046 pr_warning("Too many( > %d) probe point found.\n",
1047 tf->max_tevs);
1048 return -ERANGE;
1049 }
1050 tev = &tf->tevs[tf->ntevs++];
1051
1052 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1053 &tev->point);
1054 if (ret < 0)
1055 return ret;
1056
1057 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1058 tev->point.offset);
1059
1060 /* Find each argument */
1061 tev->nargs = pf->pev->nargs;
1062 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1063 if (tev->args == NULL)
1064 return -ENOMEM;
1065 for (i = 0; i < pf->pev->nargs; i++) {
1066 pf->pvar = &pf->pev->args[i];
1067 pf->tvar = &tev->args[i];
1068 ret = find_variable(sp_die, pf);
1069 if (ret != 0)
1070 return ret;
1071 }
1072
1073 return 0;
1074}
1075
1076/* Find probe_trace_events specified by perf_probe_event from debuginfo */
1077int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1078 struct probe_trace_event **tevs, int max_tevs)
1079{
1080 struct trace_event_finder tf = {
1081 .pf = {.pev = pev, .callback = add_probe_trace_event},
1082 .max_tevs = max_tevs};
1083 int ret;
1084
1085 /* Allocate result tevs array */
1086 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1087 if (*tevs == NULL)
1088 return -ENOMEM;
1089
1090 tf.tevs = *tevs;
1091 tf.ntevs = 0;
1092
1093 ret = find_probes(fd, &tf.pf);
1094 if (ret < 0) {
1095 free(*tevs);
1096 *tevs = NULL;
1097 return ret;
1098 }
1099
1100 return (ret < 0) ? ret : tf.ntevs;
1101}
1102
1103#define MAX_VAR_LEN 64
1104
1105/* Collect available variables in this scope */
1106static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1107{
1108 struct available_var_finder *af = data;
1109 struct variable_list *vl;
1110 char buf[MAX_VAR_LEN];
1111 int tag, ret;
1112
1113 vl = &af->vls[af->nvls - 1];
1114
1115 tag = dwarf_tag(die_mem);
1116 if (tag == DW_TAG_formal_parameter ||
1117 tag == DW_TAG_variable) {
1118 ret = convert_variable_location(die_mem, af->pf.addr,
1119 af->pf.fb_ops, NULL);
1120 if (ret == 0) {
1121 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001122 pr_debug2("Add new var: %s\n", buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001123 if (ret > 0)
1124 strlist__add(vl->vars, buf);
1125 }
1126 }
1127
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001128 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001129 return DIE_FIND_CB_CONTINUE;
1130 else
1131 return DIE_FIND_CB_SIBLING;
1132}
1133
1134/* Add a found vars into available variables list */
1135static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1136{
1137 struct available_var_finder *af =
1138 container_of(pf, struct available_var_finder, pf);
1139 struct variable_list *vl;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001140 Dwarf_Die die_mem, *scopes = NULL;
1141 int ret, nscopes;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001142
1143 /* Check number of tevs */
1144 if (af->nvls == af->max_vls) {
1145 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1146 return -ERANGE;
1147 }
1148 vl = &af->vls[af->nvls++];
1149
1150 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1151 &vl->point);
1152 if (ret < 0)
1153 return ret;
1154
1155 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1156 vl->point.offset);
1157
1158 /* Find local variables */
1159 vl->vars = strlist__new(true, NULL);
1160 if (vl->vars == NULL)
1161 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001162 af->child = true;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001163 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1164
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001165 /* Find external variables */
1166 if (!af->externs)
1167 goto out;
1168 /* Don't need to search child DIE for externs. */
1169 af->child = false;
1170 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1171 while (nscopes-- > 1)
1172 die_find_child(&scopes[nscopes], collect_variables_cb,
1173 (void *)af, &die_mem);
1174 if (scopes)
1175 free(scopes);
1176
1177out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001178 if (strlist__empty(vl->vars)) {
1179 strlist__delete(vl->vars);
1180 vl->vars = NULL;
1181 }
1182
1183 return ret;
1184}
1185
1186/* Find available variables at given probe point */
1187int find_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001188 struct variable_list **vls, int max_vls,
1189 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001190{
1191 struct available_var_finder af = {
1192 .pf = {.pev = pev, .callback = add_available_vars},
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001193 .max_vls = max_vls, .externs = externs};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001194 int ret;
1195
1196 /* Allocate result vls array */
1197 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1198 if (*vls == NULL)
1199 return -ENOMEM;
1200
1201 af.vls = *vls;
1202 af.nvls = 0;
1203
1204 ret = find_probes(fd, &af.pf);
1205 if (ret < 0) {
1206 /* Free vlist for error */
1207 while (af.nvls--) {
1208 if (af.vls[af.nvls].point.symbol)
1209 free(af.vls[af.nvls].point.symbol);
1210 if (af.vls[af.nvls].vars)
1211 strlist__delete(af.vls[af.nvls].vars);
1212 }
1213 free(af.vls);
1214 *vls = NULL;
1215 return ret;
1216 }
1217
1218 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001219}
1220
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001221/* Reverse search */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001222int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001223{
1224 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001225 Dwarf *dbg = NULL;
1226 Dwfl *dwfl = NULL;
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001227 Dwarf_Addr _addr, baseaddr, bias = 0;
1228 const char *fname = NULL, *func = NULL, *tmp;
1229 int baseline = 0, lineno = 0, ret = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001230
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001231 /* Open the live linux kernel */
1232 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1233 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001234 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001235 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1236 ret = -EINVAL;
1237 goto end;
1238 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001239
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001240 /* Adjust address with bias */
1241 addr += bias;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001242 /* Find cu die */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001243 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001244 pr_warning("Failed to find debug information for address %lx\n",
1245 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001246 ret = -EINVAL;
1247 goto end;
1248 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001249
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001250 /* Find a corresponding line (filename and lineno) */
1251 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1252 /* Don't care whether it failed or not */
1253
1254 /* Find a corresponding function (name, baseline and baseaddr) */
Masami Hiramatsue0d153c2011-06-27 16:27:27 +09001255 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001256 /* Get function entry information */
1257 tmp = dwarf_diename(&spdie);
1258 if (!tmp ||
1259 dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1260 dwarf_decl_line(&spdie, &baseline) != 0)
1261 goto post;
1262 func = tmp;
1263
1264 if (addr == (unsigned long)baseaddr)
1265 /* Function entry - Relative line number is 0 */
1266 lineno = baseline;
1267 else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1268 &indie)) {
1269 if (dwarf_entrypc(&indie, &_addr) == 0 &&
1270 _addr == addr)
1271 /*
1272 * addr is at an inline function entry.
1273 * In this case, lineno should be the call-site
1274 * line number.
1275 */
1276 lineno = die_get_call_lineno(&indie);
1277 else {
1278 /*
1279 * addr is in an inline function body.
1280 * Since lineno points one of the lines
1281 * of the inline function, baseline should
1282 * be the entry line of the inline function.
1283 */
1284 tmp = dwarf_diename(&indie);
1285 if (tmp &&
1286 dwarf_decl_line(&spdie, &baseline) == 0)
1287 func = tmp;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001288 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001289 }
1290 }
1291
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001292post:
1293 /* Make a relative line number or an offset */
1294 if (lineno)
1295 ppt->line = lineno - baseline;
1296 else if (func)
1297 ppt->offset = addr - (unsigned long)baseaddr;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001298
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001299 /* Duplicate strings */
1300 if (func) {
1301 ppt->function = strdup(func);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001302 if (ppt->function == NULL) {
1303 ret = -ENOMEM;
1304 goto end;
1305 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001306 }
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001307 if (fname) {
1308 ppt->file = strdup(fname);
1309 if (ppt->file == NULL) {
1310 if (ppt->function) {
1311 free(ppt->function);
1312 ppt->function = NULL;
1313 }
1314 ret = -ENOMEM;
1315 goto end;
1316 }
1317 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001318end:
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001319 if (dwfl)
1320 dwfl_end(dwfl);
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001321 if (ret == 0 && (fname || func))
1322 ret = 1; /* Found a point */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001323 return ret;
1324}
1325
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001326/* Add a line and store the src path */
1327static int line_range_add_line(const char *src, unsigned int lineno,
1328 struct line_range *lr)
1329{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001330 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001331 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001332 lr->path = strdup(src);
1333 if (lr->path == NULL)
1334 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001335 }
1336 return line_list__add_line(&lr->line_list, lineno);
1337}
1338
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001339static int line_range_walk_cb(const char *fname, int lineno,
1340 Dwarf_Addr addr __used,
1341 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001342{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001343 struct line_finder *lf = data;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001344
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001345 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001346 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001347 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001348
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001349 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1350 return -EINVAL;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001351
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001352 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001353}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001354
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001355/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001356static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001357{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001358 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001359
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001360 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001361
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001362 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001363 if (ret >= 0)
1364 if (!list_empty(&lf->lr->line_list))
1365 ret = lf->found = 1;
1366 else
1367 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001368 else {
1369 free(lf->lr->path);
1370 lf->lr->path = NULL;
1371 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001372 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001373}
1374
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001375static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1376{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001377 struct dwarf_callback_param *param = data;
1378
1379 param->retval = find_line_range_by_line(in_die, param->data);
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001380 return DWARF_CB_ABORT; /* No need to find other instances */
1381}
1382
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001383/* Search function from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001384static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001385{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001386 struct dwarf_callback_param *param = data;
1387 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001388 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001389
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001390 /* Check declared file */
1391 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1392 return DWARF_CB_OK;
1393
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001394 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
Masami Hiramatsu82175632010-07-09 18:29:17 +09001395 die_compare_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001396 lf->fname = dwarf_decl_file(sp_die);
1397 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001398 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001399 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001400 if (lf->lno_s < 0) /* Overflow */
1401 lf->lno_s = INT_MAX;
1402 lf->lno_e = lr->offset + lr->end;
1403 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001404 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001405 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001406 lr->start = lf->lno_s;
1407 lr->end = lf->lno_e;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001408 if (dwarf_func_inline(sp_die)) {
1409 struct dwarf_callback_param _param;
1410 _param.data = (void *)lf;
1411 _param.retval = 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001412 dwarf_func_inline_instances(sp_die,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001413 line_range_inline_cb,
1414 &_param);
1415 param->retval = _param.retval;
1416 } else
1417 param->retval = find_line_range_by_line(sp_die, lf);
1418 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001419 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001420 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001421}
1422
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001423static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001424{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001425 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1426 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1427 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001428}
1429
1430int find_line_range(int fd, struct line_range *lr)
1431{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001432 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001433 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001434 Dwarf_Off off = 0, noff;
1435 size_t cuhl;
1436 Dwarf_Die *diep;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001437 Dwarf *dbg = NULL;
1438 Dwfl *dwfl;
1439 Dwarf_Addr bias; /* Currently ignored */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001440 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001441
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001442 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001443 if (!dbg) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001444 pr_warning("No debug information found in the vmlinux - "
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001445 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsuf0c48012011-03-30 18:25:47 +09001446 close(fd); /* Without dwfl_end(), fd isn't closed. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001447 return -EBADF;
1448 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001449
Lin Mingcd25f8b2011-03-25 16:27:48 +08001450 /* Fastpath: lookup by function name from .debug_pubnames section */
1451 if (lr->function) {
1452 struct pubname_callback_param pubname_param = {
1453 .function = lr->function, .file = lr->file,
1454 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1455 struct dwarf_callback_param line_range_param = {
1456 .data = (void *)&lf, .retval = 0};
1457
1458 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1459 if (pubname_param.found) {
1460 line_range_search_cb(&lf.sp_die, &line_range_param);
1461 if (lf.found)
1462 goto found;
1463 }
1464 }
1465
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001466 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001467 while (!lf.found && ret >= 0) {
1468 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001469 break;
1470
1471 /* Get the DIE(Debugging Information Entry) of this CU */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001472 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1473 if (!diep)
1474 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001475
1476 /* Check if target file is included. */
1477 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001478 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001479 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001480 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001481
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001482 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001483 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001484 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001485 else {
1486 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001487 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001488 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001489 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001490 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001491 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001492 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001493
Lin Mingcd25f8b2011-03-25 16:27:48 +08001494found:
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001495 /* Store comp_dir */
1496 if (lf.found) {
1497 comp_dir = cu_get_comp_dir(&lf.cu_die);
1498 if (comp_dir) {
1499 lr->comp_dir = strdup(comp_dir);
1500 if (!lr->comp_dir)
1501 ret = -ENOMEM;
1502 }
1503 }
1504
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001505 pr_debug("path: %s\n", lr->path);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001506 dwfl_end(dwfl);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001507 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001508}
1509