blob: 659024342e9acba291ef59d12d19dd9675ebfcf2 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04002/*
3 * probe-finder.c : C expression to kprobe event converter
4 *
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04006 */
7
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03008#include <inttypes.h>
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04009#include <sys/utsname.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <errno.h>
14#include <stdio.h>
15#include <unistd.h>
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040016#include <stdlib.h>
17#include <string.h>
18#include <stdarg.h>
Ian Munsiecd932c52010-04-20 16:58:32 +100019#include <dwarf-regs.h>
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040020
Masami Hiramatsu124bb832011-02-04 21:52:11 +090021#include <linux/bitops.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030022#include <linux/zalloc.h>
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040023#include "event.h"
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +000024#include "dso.h"
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040025#include "debug.h"
Masami Hiramatsu5a622572014-02-06 05:32:09 +000026#include "intlist.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030027#include "strbuf.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030028#include "strlist.h"
Chase Douglas9ed7e1b2010-06-14 15:26:30 -040029#include "symbol.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040030#include "probe-finder.h"
Masami Hiramatsu180b2062016-08-18 17:58:31 +090031#include "probe-file.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030032#include "string2.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040033
Masami Hiramatsu49849122010-04-12 13:17:15 -040034/* Kprobe tracer basic type is up to u64 */
35#define MAX_BASIC_TYPE_BITS 64
36
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090037/* Dwarf FL wrappers */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090038static char *debuginfo_path; /* Currently dummy */
39
40static const Dwfl_Callbacks offline_callbacks = {
41 .find_debuginfo = dwfl_standard_find_debuginfo,
42 .debuginfo_path = &debuginfo_path,
43
44 .section_address = dwfl_offline_section_address,
45
46 /* We use this table for core files too. */
47 .find_elf = dwfl_build_id_find_elf,
48};
49
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090050/* Get a Dwarf from offline image */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030051static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
Masami Hiramatsuff741782011-06-27 16:27:39 +090052 const char *path)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090053{
Masami Hiramatsuff741782011-06-27 16:27:39 +090054 int fd;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090055
Masami Hiramatsuff741782011-06-27 16:27:39 +090056 fd = open(path, O_RDONLY);
57 if (fd < 0)
58 return fd;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090059
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030060 dbg->dwfl = dwfl_begin(&offline_callbacks);
61 if (!dbg->dwfl)
Masami Hiramatsuff741782011-06-27 16:27:39 +090062 goto error;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090063
Masami Hiramatsu91359492015-10-01 01:41:28 +090064 dwfl_report_begin(dbg->dwfl);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030065 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
66 if (!dbg->mod)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090067 goto error;
68
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030069 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
70 if (!dbg->dbg)
Masami Hiramatsuff741782011-06-27 16:27:39 +090071 goto error;
72
Masami Hiramatsu91359492015-10-01 01:41:28 +090073 dwfl_report_end(dbg->dwfl, NULL, NULL);
74
Masami Hiramatsuff741782011-06-27 16:27:39 +090075 return 0;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090076error:
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030077 if (dbg->dwfl)
78 dwfl_end(dbg->dwfl);
Masami Hiramatsuff741782011-06-27 16:27:39 +090079 else
80 close(fd);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030081 memset(dbg, 0, sizeof(*dbg));
Masami Hiramatsuff741782011-06-27 16:27:39 +090082
83 return -ENOENT;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090084}
85
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +000086static struct debuginfo *__debuginfo__new(const char *path)
Masami Hiramatsuff741782011-06-27 16:27:39 +090087{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030088 struct debuginfo *dbg = zalloc(sizeof(*dbg));
89 if (!dbg)
Masami Hiramatsuff741782011-06-27 16:27:39 +090090 return NULL;
91
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030092 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
93 zfree(&dbg);
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +000094 if (dbg)
95 pr_debug("Open Debuginfo file: %s\n", path);
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -030096 return dbg;
Masami Hiramatsuff741782011-06-27 16:27:39 +090097}
98
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +000099enum dso_binary_type distro_dwarf_types[] = {
100 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
101 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
102 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
103 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
Adrian Hunter85afd352020-05-26 18:52:07 +0300104 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000105 DSO_BINARY_TYPE__NOT_FOUND,
106};
107
108struct debuginfo *debuginfo__new(const char *path)
109{
110 enum dso_binary_type *type;
111 char buf[PATH_MAX], nil = '\0';
112 struct dso *dso;
113 struct debuginfo *dinfo = NULL;
114
115 /* Try to open distro debuginfo files */
116 dso = dso__new(path);
117 if (!dso)
118 goto out;
119
120 for (type = distro_dwarf_types;
121 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
122 type++) {
123 if (dso__read_binary_type_filename(dso, *type, &nil,
124 buf, PATH_MAX) < 0)
125 continue;
126 dinfo = __debuginfo__new(buf);
127 }
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300128 dso__put(dso);
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000129
130out:
131 /* if failed to open all distro debuginfo, open given binary */
132 return dinfo ? : __debuginfo__new(path);
133}
134
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300135void debuginfo__delete(struct debuginfo *dbg)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900136{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300137 if (dbg) {
138 if (dbg->dwfl)
139 dwfl_end(dbg->dwfl);
140 free(dbg);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900141 }
142}
143
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400144/*
145 * Probe finder related functions
146 */
147
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530148static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400149{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530150 struct probe_trace_arg_ref *ref;
151 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400152 if (ref != NULL)
153 ref->offset = offs;
154 return ref;
155}
156
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900157/*
158 * Convert a location into trace_arg.
159 * If tvar == NULL, this just checks variable can be converted.
Masami Hiramatsu3d918a12013-10-11 16:10:26 +0900160 * If fentry == true and vr_die is a parameter, do huristic search
161 * for the location fuzzed by function entry mcount.
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900162 */
163static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
Masami Hiramatsu3d918a12013-10-11 16:10:26 +0900164 Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
Masami Hiramatsu293d5b42016-08-26 01:24:57 +0900165 unsigned int machine,
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900166 struct probe_trace_arg *tvar)
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400167{
168 Dwarf_Attribute attr;
Masami Hiramatsu3d918a12013-10-11 16:10:26 +0900169 Dwarf_Addr tmp = 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400170 Dwarf_Op *op;
171 size_t nops;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500172 unsigned int regn;
173 Dwarf_Word offs = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400174 bool ref = false;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400175 const char *regs;
He Kuang349e8d22015-05-11 09:25:03 +0000176 int ret, ret2 = 0;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400177
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900178 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
179 goto static_var;
180
Masami Hiramatsu66f69b22019-11-18 17:12:40 +0900181 /* Constant value */
182 if (dwarf_attr(vr_die, DW_AT_const_value, &attr) &&
183 immediate_value_is_supported()) {
184 Dwarf_Sword snum;
185
186 dwarf_formsdata(&attr, &snum);
187 ret = asprintf(&tvar->value, "\\%ld", (long)snum);
188
189 return ret < 0 ? -ENOMEM : 0;
190 }
191
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400192 /* TODO: handle more than 1 exprs */
Masami Hiramatsu3d918a12013-10-11 16:10:26 +0900193 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
194 return -EINVAL; /* Broken DIE ? */
195 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
196 ret = dwarf_entrypc(sp_die, &tmp);
He Kuang349e8d22015-05-11 09:25:03 +0000197 if (ret)
198 return -ENOENT;
199
200 if (probe_conf.show_location_range &&
201 (dwarf_tag(vr_die) == DW_TAG_variable)) {
202 ret2 = -ERANGE;
203 } else if (addr != tmp ||
204 dwarf_tag(vr_die) != DW_TAG_formal_parameter) {
205 return -ENOENT;
206 }
207
208 ret = dwarf_highpc(sp_die, &tmp);
209 if (ret)
Masami Hiramatsu3d918a12013-10-11 16:10:26 +0900210 return -ENOENT;
211 /*
212 * This is fuzzed by fentry mcount. We try to find the
213 * parameter location at the earliest address.
214 */
215 for (addr += 1; addr <= tmp; addr++) {
216 if (dwarf_getlocation_addr(&attr, addr, &op,
217 &nops, 1) > 0)
218 goto found;
219 }
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400220 return -ENOENT;
221 }
Masami Hiramatsu3d918a12013-10-11 16:10:26 +0900222found:
223 if (nops == 0)
224 /* TODO: Support const_value */
225 return -ENOENT;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400226
227 if (op->atom == DW_OP_addr) {
Masami Hiramatsu632941c2010-10-21 19:13:16 +0900228static_var:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900229 if (!tvar)
He Kuang349e8d22015-05-11 09:25:03 +0000230 return ret2;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400231 /* Static variables on memory (not stack), make @varname */
232 ret = strlen(dwarf_diename(vr_die));
233 tvar->value = zalloc(ret + 2);
234 if (tvar->value == NULL)
235 return -ENOMEM;
236 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
237 tvar->ref = alloc_trace_arg_ref((long)offs);
238 if (tvar->ref == NULL)
239 return -ENOMEM;
He Kuang349e8d22015-05-11 09:25:03 +0000240 return ret2;
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400241 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400242
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400243 /* If this is based on frame buffer, set the offset */
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500244 if (op->atom == DW_OP_fbreg) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900245 if (fb_ops == NULL)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400246 return -ENOTSUP;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400247 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500248 offs = op->number;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900249 op = &fb_ops[0];
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500250 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400251
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500252 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
253 regn = op->atom - DW_OP_breg0;
254 offs += op->number;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400255 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500256 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
257 regn = op->atom - DW_OP_reg0;
258 } else if (op->atom == DW_OP_bregx) {
259 regn = op->number;
260 offs += op->number2;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400261 ref = true;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500262 } else if (op->atom == DW_OP_regx) {
263 regn = op->number;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400264 } else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900265 pr_debug("DW_OP %x is not supported.\n", op->atom);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400266 return -ENOTSUP;
267 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400268
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900269 if (!tvar)
He Kuang349e8d22015-05-11 09:25:03 +0000270 return ret2;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900271
Masami Hiramatsu293d5b42016-08-26 01:24:57 +0900272 regs = get_dwarf_regstr(regn, machine);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400273 if (!regs) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900274 /* This should be a bug in DWARF or this tool */
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900275 pr_warning("Mapping for the register number %u "
276 "missing on this architecture.\n", regn);
He Kuang349e8d22015-05-11 09:25:03 +0000277 return -ENOTSUP;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400278 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400279
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400280 tvar->value = strdup(regs);
281 if (tvar->value == NULL)
282 return -ENOMEM;
283
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400284 if (ref) {
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400285 tvar->ref = alloc_trace_arg_ref((long)offs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400286 if (tvar->ref == NULL)
287 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400288 }
He Kuang349e8d22015-05-11 09:25:03 +0000289 return ret2;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400290}
291
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900292#define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
293
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400294static int convert_variable_type(Dwarf_Die *vr_die,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530295 struct probe_trace_arg *tvar,
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900296 const char *cast, bool user_access)
Masami Hiramatsu49849122010-04-12 13:17:15 -0400297{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530298 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400299 Dwarf_Die type;
300 char buf[16];
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000301 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900302 int bsize, boffs, total;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400303 int ret;
Masami Hiramatsu92543782016-08-18 17:58:47 +0900304 char prefix;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400305
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400306 /* TODO: check all types */
Thomas Richter1873f152020-01-20 14:20:10 +0100307 if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "ustring") &&
308 strcmp(cast, "x") != 0 &&
Naohiro Aota19f00b02016-08-09 11:40:08 +0900309 strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400310 /* Non string type is OK */
Masami Hiramatsu92543782016-08-18 17:58:47 +0900311 /* and respect signedness/hexadecimal cast */
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400312 tvar->type = strdup(cast);
313 return (tvar->type == NULL) ? -ENOMEM : 0;
314 }
315
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900316 bsize = dwarf_bitsize(vr_die);
317 if (bsize > 0) {
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900318 /* This is a bitfield */
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900319 boffs = dwarf_bitoffset(vr_die);
320 total = dwarf_bytesize(vr_die);
321 if (boffs < 0 || total < 0)
322 return -ENOENT;
323 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
324 BYTES_TO_BITS(total));
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900325 goto formatted;
326 }
327
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400328 if (die_get_real_type(vr_die, &type) == NULL) {
329 pr_warning("Failed to get a type information of %s.\n",
330 dwarf_diename(vr_die));
331 return -ENOENT;
332 }
Masami Hiramatsu49849122010-04-12 13:17:15 -0400333
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400334 pr_debug("%s type is %s.\n",
335 dwarf_diename(vr_die), dwarf_diename(&type));
336
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900337 if (cast && (!strcmp(cast, "string") || !strcmp(cast, "ustring"))) {
338 /* String type */
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400339 ret = dwarf_tag(&type);
340 if (ret != DW_TAG_pointer_type &&
341 ret != DW_TAG_array_type) {
342 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900343 "%s(%s) is not a pointer nor array.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400344 dwarf_diename(vr_die), dwarf_diename(&type));
345 return -EINVAL;
346 }
Hyeoncheol Lee7ce28b52012-09-11 16:57:28 +0900347 if (die_get_real_type(&type, &type) == NULL) {
348 pr_warning("Failed to get a type"
349 " information.\n");
350 return -ENOENT;
351 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400352 if (ret == DW_TAG_pointer_type) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400353 while (*ref_ptr)
354 ref_ptr = &(*ref_ptr)->next;
355 /* Add new reference with offset +0 */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530356 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400357 if (*ref_ptr == NULL) {
358 pr_warning("Out of memory error\n");
359 return -ENOMEM;
360 }
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900361 (*ref_ptr)->user_access = user_access;
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400362 }
Masami Hiramatsu82175632010-07-09 18:29:17 +0900363 if (!die_compare_name(&type, "char") &&
364 !die_compare_name(&type, "unsigned char")) {
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400365 pr_warning("Failed to cast into string: "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900366 "%s is not (unsigned) char *.\n",
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400367 dwarf_diename(vr_die));
368 return -EINVAL;
369 }
370 tvar->type = strdup(cast);
371 return (tvar->type == NULL) ? -ENOMEM : 0;
372 }
373
Naohiro Aota19f00b02016-08-09 11:40:08 +0900374 if (cast && (strcmp(cast, "u") == 0))
Masami Hiramatsu92543782016-08-18 17:58:47 +0900375 prefix = 'u';
Naohiro Aota19f00b02016-08-09 11:40:08 +0900376 else if (cast && (strcmp(cast, "s") == 0))
Masami Hiramatsu92543782016-08-18 17:58:47 +0900377 prefix = 's';
378 else if (cast && (strcmp(cast, "x") == 0) &&
379 probe_type_is_available(PROBE_TYPE_X))
380 prefix = 'x';
Naohiro Aota19f00b02016-08-09 11:40:08 +0900381 else
Masami Hiramatsu9880ce42016-08-18 17:59:07 +0900382 prefix = die_is_signed_type(&type) ? 's' :
383 probe_type_is_available(PROBE_TYPE_X) ? 'x' : 'u';
Naohiro Aota19f00b02016-08-09 11:40:08 +0900384
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900385 ret = dwarf_bytesize(&type);
386 if (ret <= 0)
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900387 /* No size ... try to use default type */
388 return 0;
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900389 ret = BYTES_TO_BITS(ret);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400390
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900391 /* Check the bitwidth */
392 if (ret > MAX_BASIC_TYPE_BITS) {
393 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
394 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
395 ret = MAX_BASIC_TYPE_BITS;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400396 }
Masami Hiramatsu92543782016-08-18 17:58:47 +0900397 ret = snprintf(buf, 16, "%c%d", prefix, ret);
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900398
399formatted:
400 if (ret < 0 || ret >= 16) {
401 if (ret >= 16)
402 ret = -E2BIG;
403 pr_warning("Failed to convert variable type: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300404 str_error_r(-ret, sbuf, sizeof(sbuf)));
Masami Hiramatsu124bb832011-02-04 21:52:11 +0900405 return ret;
406 }
407 tvar->type = strdup(buf);
408 if (tvar->type == NULL)
409 return -ENOMEM;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400410 return 0;
Masami Hiramatsu49849122010-04-12 13:17:15 -0400411}
412
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400413static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400414 struct perf_probe_arg_field *field,
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530415 struct probe_trace_arg_ref **ref_ptr,
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900416 Dwarf_Die *die_mem, bool user_access)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400417{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530418 struct probe_trace_arg_ref *ref = *ref_ptr;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400419 Dwarf_Die type;
420 Dwarf_Word offs;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400421 int ret, tag;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400422
423 pr_debug("converting %s in %s\n", field->name, varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400424 if (die_get_real_type(vr_die, &type) == NULL) {
425 pr_warning("Failed to get the type of %s.\n", varname);
426 return -ENOENT;
427 }
Masami Hiramatsud0461792018-03-17 21:52:25 +0900428 pr_debug2("Var real type: %s (%x)\n", dwarf_diename(&type),
429 (unsigned)dwarf_dieoffset(&type));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400430 tag = dwarf_tag(&type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400431
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400432 if (field->name[0] == '[' &&
433 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
Masami Hiramatsud0461792018-03-17 21:52:25 +0900434 /* Save original type for next field or type */
435 memcpy(die_mem, &type, sizeof(*die_mem));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400436 /* Get the type of this array */
437 if (die_get_real_type(&type, &type) == NULL) {
438 pr_warning("Failed to get the type of %s.\n", varname);
439 return -ENOENT;
440 }
Masami Hiramatsud0461792018-03-17 21:52:25 +0900441 pr_debug2("Array real type: %s (%x)\n", dwarf_diename(&type),
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400442 (unsigned)dwarf_dieoffset(&type));
443 if (tag == DW_TAG_pointer_type) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530444 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400445 if (ref == NULL)
446 return -ENOMEM;
447 if (*ref_ptr)
448 (*ref_ptr)->next = ref;
449 else
450 *ref_ptr = ref;
451 }
Masami Hiramatsubcfc0822011-06-27 16:27:21 +0900452 ref->offset += dwarf_bytesize(&type) * field->index;
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900453 ref->user_access = user_access;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400454 goto next;
455 } else if (tag == DW_TAG_pointer_type) {
456 /* Check the pointer and dereference */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400457 if (!field->ref) {
458 pr_err("Semantic error: %s must be referred by '->'\n",
459 field->name);
460 return -EINVAL;
461 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400462 /* Get the type pointed by this pointer */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400463 if (die_get_real_type(&type, &type) == NULL) {
464 pr_warning("Failed to get the type of %s.\n", varname);
465 return -ENOENT;
466 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400467 /* Verify it is a data structure */
Hyeoncheol Lee7b0295b2012-09-12 16:57:45 +0900468 tag = dwarf_tag(&type);
469 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
Masahiro Yamada03440c42017-02-27 14:28:49 -0800470 pr_warning("%s is not a data structure nor a union.\n",
Hyeoncheol Lee7b0295b2012-09-12 16:57:45 +0900471 varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400472 return -EINVAL;
473 }
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400474
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530475 ref = zalloc(sizeof(struct probe_trace_arg_ref));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400476 if (ref == NULL)
477 return -ENOMEM;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400478 if (*ref_ptr)
479 (*ref_ptr)->next = ref;
480 else
481 *ref_ptr = ref;
482 } else {
Masami Hiramatsu12e5a7a2010-04-02 12:50:53 -0400483 /* Verify it is a data structure */
Hyeoncheol Lee7b0295b2012-09-12 16:57:45 +0900484 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
Masahiro Yamada03440c42017-02-27 14:28:49 -0800485 pr_warning("%s is not a data structure nor a union.\n",
Hyeoncheol Lee7b0295b2012-09-12 16:57:45 +0900486 varname);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400487 return -EINVAL;
488 }
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400489 if (field->name[0] == '[') {
Masanari Iidad939be32015-02-27 23:52:31 +0900490 pr_err("Semantic error: %s is not a pointer"
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900491 " nor array.\n", varname);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400492 return -EINVAL;
493 }
Masami Hiramatsuc7273832015-04-02 16:33:12 +0900494 /* While prcessing unnamed field, we don't care about this */
495 if (field->ref && dwarf_diename(vr_die)) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400496 pr_err("Semantic error: %s must be referred by '.'\n",
497 field->name);
498 return -EINVAL;
499 }
500 if (!ref) {
501 pr_warning("Structure on a register is not "
502 "supported yet.\n");
503 return -ENOTSUP;
504 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400505 }
506
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400507 if (die_find_member(&type, field->name, die_mem) == NULL) {
Arnaldo Carvalho de Melo9ef04382013-10-24 17:36:31 -0300508 pr_warning("%s(type:%s) has no member %s.\n", varname,
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400509 dwarf_diename(&type), field->name);
510 return -EINVAL;
511 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400512
513 /* Get the offset of the field */
Hyeoncheol Lee7b0295b2012-09-12 16:57:45 +0900514 if (tag == DW_TAG_union_type) {
515 offs = 0;
516 } else {
517 ret = die_get_data_member_location(die_mem, &offs);
518 if (ret < 0) {
519 pr_warning("Failed to get the offset of %s.\n",
520 field->name);
521 return ret;
522 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400523 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400524 ref->offset += (long)offs;
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900525 ref->user_access = user_access;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400526
Masami Hiramatsuc7273832015-04-02 16:33:12 +0900527 /* If this member is unnamed, we need to reuse this field */
528 if (!dwarf_diename(die_mem))
529 return convert_variable_fields(die_mem, varname, field,
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900530 &ref, die_mem, user_access);
Masami Hiramatsuc7273832015-04-02 16:33:12 +0900531
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400532next:
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400533 /* Converting next field */
534 if (field->next)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400535 return convert_variable_fields(die_mem, field->name,
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900536 field->next, &ref, die_mem, user_access);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400537 else
538 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400539}
540
Masami Hiramatsucb402732019-11-18 17:12:49 +0900541static void print_var_not_found(const char *varname)
542{
543 pr_err("Failed to find the location of the '%s' variable at this address.\n"
544 " Perhaps it has been optimized out.\n"
545 " Use -V with the --range option to show '%s' location range.\n",
546 varname, varname);
547}
548
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400549/* Show a variables in kprobe event format */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400550static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400551{
Masami Hiramatsu49849122010-04-12 13:17:15 -0400552 Dwarf_Die die_mem;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400553 int ret;
554
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400555 pr_debug("Converting variable %s into trace event.\n",
556 dwarf_diename(vr_die));
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500557
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900558 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
Masami Hiramatsu293d5b42016-08-26 01:24:57 +0900559 &pf->sp_die, pf->machine, pf->tvar);
Masami Hiramatsucb402732019-11-18 17:12:49 +0900560 if (ret == -ENOENT && pf->skip_empty_arg)
561 /* This can be found in other place. skip it */
562 return 0;
He Kuang7d5eaba2015-05-11 09:25:04 +0000563 if (ret == -ENOENT || ret == -EINVAL) {
Masami Hiramatsucb402732019-11-18 17:12:49 +0900564 print_var_not_found(pf->pvar->var);
He Kuang7d5eaba2015-05-11 09:25:04 +0000565 } else if (ret == -ENOTSUP)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900566 pr_err("Sorry, we don't support this variable location yet.\n");
Masami Hiramatsu0c188a02014-05-29 19:52:32 +0900567 else if (ret == 0 && pf->pvar->field) {
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400568 ret = convert_variable_fields(vr_die, pf->pvar->var,
569 pf->pvar->field, &pf->tvar->ref,
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900570 &die_mem, pf->pvar->user_access);
Masami Hiramatsu49849122010-04-12 13:17:15 -0400571 vr_die = &die_mem;
572 }
Masami Hiramatsu73317b92010-05-19 15:57:35 -0400573 if (ret == 0)
Masami Hiramatsu1e032f72019-05-15 14:39:05 +0900574 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type,
575 pf->pvar->user_access);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500576 /* *expr will be cached in libdw. Don't free it. */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400577 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400578}
579
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900580/* Find a variable in a scope DIE */
581static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400582{
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +0900583 Dwarf_Die vr_die;
Masami Hiramatsu909b0362016-04-28 03:37:14 +0900584 char *buf, *ptr;
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +0900585 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400586
Wang Nanda15bd92015-08-26 10:57:45 +0000587 /* Copy raw parameters */
588 if (!is_c_varname(pf->pvar->var))
589 return copy_to_probe_trace_arg(pf->tvar, pf->pvar);
Masami Hiramatsu367e94c2010-08-27 20:38:59 +0900590
Masami Hiramatsu48481932010-04-12 13:16:53 -0400591 if (pf->pvar->name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400592 pf->tvar->name = strdup(pf->pvar->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400593 else {
Masami Hiramatsu909b0362016-04-28 03:37:14 +0900594 buf = synthesize_perf_probe_arg(pf->pvar);
595 if (!buf)
596 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400597 ptr = strchr(buf, ':'); /* Change type separator to _ */
598 if (ptr)
599 *ptr = '_';
Masami Hiramatsu909b0362016-04-28 03:37:14 +0900600 pf->tvar->name = buf;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400601 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400602 if (pf->tvar->name == NULL)
603 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400604
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +0900605 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400606 /* Search child die for local variables and parameters. */
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +0900607 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
608 /* Search again in global variables */
He Kuangd13855e2015-04-25 16:08:58 +0800609 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var,
610 0, &vr_die)) {
Masami Hiramatsucb402732019-11-18 17:12:49 +0900611 if (pf->skip_empty_arg)
612 return 0;
Masami Hiramatsu36d789a2014-06-06 07:13:45 +0000613 pr_warning("Failed to find '%s' in this function.\n",
614 pf->pvar->var);
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +0900615 ret = -ENOENT;
He Kuangd13855e2015-04-25 16:08:58 +0800616 }
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400617 }
Masami Hiramatsuf66fedc2011-08-20 14:39:23 +0900618 if (ret >= 0)
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +0900619 ret = convert_variable(&vr_die, pf);
620
Masami Hiramatsub7dcb852010-05-19 15:57:49 -0400621 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400622}
623
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900624/* Convert subprogram DIE to trace point */
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900625static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
626 Dwarf_Addr paddr, bool retprobe,
Masami Hiramatsu6cca13b2015-10-01 01:41:37 +0900627 const char *function,
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900628 struct probe_trace_point *tp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400629{
Masami Hiramatsu07d36982019-10-25 17:46:25 +0900630 Dwarf_Addr eaddr;
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900631 GElf_Sym sym;
632 const char *symbol;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -0500633
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900634 /* Verify the address is correct */
Masami Hiramatsu07d36982019-10-25 17:46:25 +0900635 if (!dwarf_haspc(sp_die, paddr)) {
636 pr_warning("Specified offset is out of %s\n",
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900637 dwarf_diename(sp_die));
638 return -EINVAL;
639 }
640
Masami Hiramatsu1efde272020-02-28 00:42:01 +0900641 if (dwarf_entrypc(sp_die, &eaddr) == 0) {
642 /* If the DIE has entrypc, use it. */
643 symbol = dwarf_diename(sp_die);
644 } else {
645 /* Try to get actual symbol name and address from symtab */
646 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
647 eaddr = sym.st_value;
648 }
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900649 if (!symbol) {
Masami Hiramatsu07d36982019-10-25 17:46:25 +0900650 pr_warning("Failed to find symbol at 0x%lx\n",
651 (unsigned long)paddr);
652 return -ENOENT;
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900653 }
Masami Hiramatsu07d36982019-10-25 17:46:25 +0900654
Masami Hiramatsu664fee32014-09-17 08:41:01 +0000655 tp->offset = (unsigned long)(paddr - eaddr);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000656 tp->address = (unsigned long)paddr;
Masami Hiramatsu576b5232013-09-25 22:16:16 +0900657 tp->symbol = strdup(symbol);
658 if (!tp->symbol)
659 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400660
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900661 /* Return probe must be on the head of a subprogram */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900662 if (retprobe) {
663 if (eaddr != paddr) {
Masami Hiramatsu6cca13b2015-10-01 01:41:37 +0900664 pr_warning("Failed to find \"%s%%return\",\n"
665 " because %s is an inlined function and"
666 " has no return point.\n", function,
667 function);
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900668 return -EINVAL;
669 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900670 tp->retprobe = true;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +0900671 }
672
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900673 return 0;
674}
675
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900676/* Call probe_finder callback with scope DIE */
677static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900678{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900679 Dwarf_Attribute fb_attr;
Masami Hiramatsu4d3b1622015-11-25 19:34:32 +0900680 Dwarf_Frame *frame = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900681 size_t nops;
682 int ret;
683
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900684 if (!sc_die) {
685 pr_err("Caller must pass a scope DIE. Program error.\n");
686 return -EINVAL;
687 }
688
689 /* If not a real subprogram, find a real one */
Masami Hiramatsu0dbb1ca2012-04-23 12:24:36 +0900690 if (!die_is_func_def(sc_die)) {
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900691 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
Naveen N. Raod4c537e2015-04-30 17:12:31 +0530692 if (die_find_tailfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
693 pr_warning("Ignoring tail call from %s\n",
694 dwarf_diename(&pf->sp_die));
695 return 0;
696 } else {
697 pr_warning("Failed to find probe point in any "
698 "functions.\n");
699 return -ENOENT;
700 }
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900701 }
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900702 } else
703 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400704
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900705 /* Get the frame base attribute/ops from subprogram */
706 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
Masami Hiramatsud0cb4262010-03-15 13:02:35 -0400707 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400708 if (ret <= 0 || nops == 0) {
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500709 pf->fb_ops = NULL;
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400710#if _ELFUTILS_PREREQ(0, 142)
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400711 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
Hemant Kumar270bde12016-02-02 20:56:46 +0530712 (pf->cfi_eh != NULL || pf->cfi_dbg != NULL)) {
713 if ((dwarf_cfi_addrframe(pf->cfi_eh, pf->addr, &frame) != 0 &&
714 (dwarf_cfi_addrframe(pf->cfi_dbg, pf->addr, &frame) != 0)) ||
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400715 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900716 pr_warning("Failed to get call frame on 0x%jx\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400717 (uintmax_t)pf->addr);
Masami Hiramatsu4d3b1622015-11-25 19:34:32 +0900718 free(frame);
719 return -ENOENT;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400720 }
Masami Hiramatsu7752f1b2010-05-10 13:12:07 -0400721#endif
Masami Hiramatsua34a9852010-04-12 13:17:29 -0400722 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500723
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900724 /* Call finder's callback handler */
Masami Hiramatsu4d3b1622015-11-25 19:34:32 +0900725 ret = pf->callback(sc_die, pf);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500726
Masami Hiramatsu4d3b1622015-11-25 19:34:32 +0900727 /* Since *pf->fb_ops can be a part of frame. we should free it here. */
728 free(frame);
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500729 pf->fb_ops = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900730
731 return ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400732}
733
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900734struct find_scope_param {
735 const char *function;
736 const char *file;
737 int line;
738 int diff;
739 Dwarf_Die *die_mem;
740 bool found;
741};
742
743static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
744{
745 struct find_scope_param *fsp = data;
746 const char *file;
747 int lno;
748
749 /* Skip if declared file name does not match */
750 if (fsp->file) {
751 file = dwarf_decl_file(fn_die);
752 if (!file || strcmp(fsp->file, file) != 0)
753 return 0;
754 }
755 /* If the function name is given, that's what user expects */
756 if (fsp->function) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +0900757 if (die_match_name(fn_die, fsp->function)) {
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900758 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
759 fsp->found = true;
760 return 1;
761 }
762 } else {
763 /* With the line number, find the nearest declared DIE */
764 dwarf_decl_line(fn_die, &lno);
765 if (lno < fsp->line && fsp->diff > fsp->line - lno) {
766 /* Keep a candidate and continue */
767 fsp->diff = fsp->line - lno;
768 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
769 fsp->found = true;
770 }
771 }
772 return 0;
773}
774
Masami Hiramatsuc7016362019-11-05 09:16:49 +0900775/* Return innermost DIE */
776static int find_inner_scope_cb(Dwarf_Die *fn_die, void *data)
777{
778 struct find_scope_param *fsp = data;
779
780 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
781 fsp->found = true;
782 return 1;
783}
784
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900785/* Find an appropriate scope fits to given conditions */
786static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
787{
788 struct find_scope_param fsp = {
789 .function = pf->pev->point.function,
790 .file = pf->fname,
791 .line = pf->lno,
792 .diff = INT_MAX,
793 .die_mem = die_mem,
794 .found = false,
795 };
Masami Hiramatsuc7016362019-11-05 09:16:49 +0900796 int ret;
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900797
Masami Hiramatsuc7016362019-11-05 09:16:49 +0900798 ret = cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb,
799 &fsp);
800 if (!ret && !fsp.found)
801 cu_walk_functions_at(&pf->cu_die, pf->addr,
802 find_inner_scope_cb, &fsp);
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900803
804 return fsp.found ? die_mem : NULL;
805}
806
Masami Hiramatsu1ae5d882019-11-18 17:12:00 +0900807static int verify_representive_line(struct probe_finder *pf, const char *fname,
808 int lineno, Dwarf_Addr addr)
809{
810 const char *__fname, *__func = NULL;
811 Dwarf_Die die_mem;
812 int __lineno;
813
814 /* Verify line number and address by reverse search */
815 if (cu_find_lineinfo(&pf->cu_die, addr, &__fname, &__lineno) < 0)
816 return 0;
817
818 pr_debug2("Reversed line: %s:%d\n", __fname, __lineno);
819 if (strcmp(fname, __fname) || lineno == __lineno)
820 return 0;
821
Colin Ian King358f98e2019-11-21 09:26:23 +0000822 pr_warning("This line is sharing the address with other lines.\n");
Masami Hiramatsu1ae5d882019-11-18 17:12:00 +0900823
824 if (pf->pev->point.function) {
825 /* Find best match function name and lines */
826 pf->addr = addr;
827 if (find_best_scope(pf, &die_mem)
828 && die_match_name(&die_mem, pf->pev->point.function)
829 && dwarf_decl_line(&die_mem, &lineno) == 0) {
830 __func = dwarf_diename(&die_mem);
831 __lineno -= lineno;
832 }
833 }
834 pr_warning("Please try to probe at %s:%d instead.\n",
835 __func ? : __fname, __lineno);
836
837 return -ENOENT;
838}
839
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900840static int probe_point_line_walker(const char *fname, int lineno,
841 Dwarf_Addr addr, void *data)
842{
843 struct probe_finder *pf = data;
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900844 Dwarf_Die *sc_die, die_mem;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900845 int ret;
846
847 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
848 return 0;
849
Masami Hiramatsu1ae5d882019-11-18 17:12:00 +0900850 if (verify_representive_line(pf, fname, lineno, addr))
851 return -ENOENT;
852
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900853 pf->addr = addr;
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900854 sc_die = find_best_scope(pf, &die_mem);
855 if (!sc_die) {
856 pr_warning("Failed to find scope of probe point.\n");
857 return -ENOENT;
858 }
859
860 ret = call_probe_finder(sc_die, pf);
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900861
862 /* Continue if no error, because the line will be in inline function */
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -0300863 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900864}
865
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400866/* Find probe point from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400867static int find_probe_point_by_line(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400868{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900869 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400870}
871
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500872/* Find lines which match lazy pattern */
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000873static int find_lazy_match_lines(struct intlist *list,
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500874 const char *fname, const char *pat)
875{
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100876 FILE *fp;
877 char *line = NULL;
878 size_t line_len;
879 ssize_t len;
880 int count = 0, linenum = 1;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000881 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500882
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100883 fp = fopen(fname, "r");
884 if (!fp) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000885 pr_warning("Failed to open %s: %s\n", fname,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300886 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300887 return -errno;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400888 }
889
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100890 while ((len = getline(&line, &line_len, fp)) > 0) {
891
892 if (line[len - 1] == '\n')
893 line[len - 1] = '\0';
894
895 if (strlazymatch(line, pat)) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000896 intlist__add(list, linenum);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100897 count++;
898 }
899 linenum++;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400900 }
Arnaldo Carvalho de Melob448c4b2010-05-18 23:04:28 -0300901
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100902 if (ferror(fp))
903 count = -errno;
904 free(line);
905 fclose(fp);
906
907 if (count == 0)
908 pr_debug("No matched lines found in %s.\n", fname);
909 return count;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500910}
911
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900912static int probe_point_lazy_walker(const char *fname, int lineno,
913 Dwarf_Addr addr, void *data)
914{
915 struct probe_finder *pf = data;
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900916 Dwarf_Die *sc_die, die_mem;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900917 int ret;
918
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000919 if (!intlist__has_entry(pf->lcache, lineno) ||
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900920 strtailcmp(fname, pf->fname) != 0)
921 return 0;
922
923 pr_debug("Probe line found: line:%d addr:0x%llx\n",
924 lineno, (unsigned long long)addr);
925 pf->addr = addr;
Masami Hiramatsu221d0612011-08-11 20:02:59 +0900926 pf->lno = lineno;
927 sc_die = find_best_scope(pf, &die_mem);
928 if (!sc_die) {
929 pr_warning("Failed to find scope of probe point.\n");
930 return -ENOENT;
931 }
932
933 ret = call_probe_finder(sc_die, pf);
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900934
935 /*
936 * Continue if no error, because the lazy pattern will match
937 * to other lines
938 */
Ingo Molnar5e814dd2011-03-15 20:51:09 +0100939 return ret < 0 ? ret : 0;
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900940}
941
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500942/* Find probe points from lazy pattern */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400943static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500944{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400945 int ret = 0;
Naohiro Aota09ed8972015-03-13 14:18:40 +0900946 char *fpath;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500947
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000948 if (intlist__empty(pf->lcache)) {
Naohiro Aota09ed8972015-03-13 14:18:40 +0900949 const char *comp_dir;
950
951 comp_dir = cu_get_comp_dir(&pf->cu_die);
952 ret = get_real_path(pf->fname, comp_dir, &fpath);
953 if (ret < 0) {
954 pr_warning("Failed to find source file path.\n");
955 return ret;
956 }
957
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500958 /* Matching lazy line pattern */
Naohiro Aota09ed8972015-03-13 14:18:40 +0900959 ret = find_lazy_match_lines(pf->lcache, fpath,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400960 pf->pev->point.lazy_line);
Naohiro Aota09ed8972015-03-13 14:18:40 +0900961 free(fpath);
Franck Bui-Huuf50c2162011-01-13 11:18:30 +0100962 if (ret <= 0)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -0400963 return ret;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500964 }
965
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +0900966 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500967}
968
Ravi Bangoriae47392b2016-08-03 14:28:45 +0530969static void skip_prologue(Dwarf_Die *sp_die, struct probe_finder *pf)
970{
971 struct perf_probe_point *pp = &pf->pev->point;
972
973 /* Not uprobe? */
974 if (!pf->pev->uprobes)
975 return;
976
977 /* Compiled with optimization? */
Ravi Bangoria6243b9d2016-08-30 14:09:37 +0530978 if (die_is_optimized_target(&pf->cu_die))
Ravi Bangoriae47392b2016-08-03 14:28:45 +0530979 return;
980
981 /* Don't know entrypc? */
982 if (!pf->addr)
983 return;
984
985 /* Only FUNC and FUNC@SRC are eligible. */
986 if (!pp->function || pp->line || pp->retprobe || pp->lazy_line ||
987 pp->offset || pp->abs_address)
988 return;
989
990 /* Not interested in func parameter? */
991 if (!perf_probe_with_var(pf->pev))
992 return;
993
994 pr_info("Target program is compiled without optimization. Skipping prologue.\n"
995 "Probe on address 0x%" PRIx64 " to force probing at the function entry.\n\n",
996 pf->addr);
997
Ravi Bangoria6243b9d2016-08-30 14:09:37 +0530998 die_skip_prologue(sp_die, &pf->cu_die, &pf->addr);
Ravi Bangoriae47392b2016-08-03 14:28:45 +0530999}
1000
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001001static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001002{
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001003 struct probe_finder *pf = data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001004 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001005 Dwarf_Addr addr;
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001006 int ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001007
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001008 if (pp->lazy_line)
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001009 ret = find_probe_point_lazy(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001010 else {
1011 /* Get probe address */
Masami Hiramatsueb6933b2019-10-25 17:46:43 +09001012 if (die_entrypc(in_die, &addr) != 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001013 pr_warning("Failed to get entry address of %s.\n",
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001014 dwarf_diename(in_die));
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001015 return -ENOENT;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001016 }
Masami Hiramatsu0ad45b32016-09-24 00:35:07 +09001017 if (addr == 0) {
1018 pr_debug("%s has no valid entry address. skipped.\n",
1019 dwarf_diename(in_die));
1020 return -ENOENT;
1021 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001022 pf->addr = addr;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001023 pf->addr += pp->offset;
1024 pr_debug("found inline addr: 0x%jx\n",
1025 (uintmax_t)pf->addr);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001026
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001027 ret = call_probe_finder(in_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001028 }
1029
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001030 return ret;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001031}
1032
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001033/* Callback parameter with return value for libdw */
1034struct dwarf_callback_param {
1035 void *data;
1036 int retval;
1037};
1038
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001039/* Search function from function name */
1040static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1041{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001042 struct dwarf_callback_param *param = data;
1043 struct probe_finder *pf = param->data;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001044 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001045
1046 /* Check tag and diename */
Masami Hiramatsu0dbb1ca2012-04-23 12:24:36 +09001047 if (!die_is_func_def(sp_die) ||
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001048 !die_match_name(sp_die, pp->function))
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001049 return DWARF_CB_OK;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001050
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001051 /* Check declared file */
1052 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1053 return DWARF_CB_OK;
1054
Masami Hiramatsuf8da4b52016-09-24 00:34:57 +09001055 pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die),
1056 (unsigned long)dwarf_dieoffset(sp_die));
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001057 pf->fname = dwarf_decl_file(sp_die);
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001058 if (pp->line) { /* Function relative line */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001059 dwarf_decl_line(sp_die, &pf->lno);
1060 pf->lno += pp->line;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001061 param->retval = find_probe_point_by_line(pf);
Masami Hiramatsue1ecbbc2015-01-30 18:37:44 +09001062 } else if (die_is_func_instance(sp_die)) {
1063 /* Instances always have the entry address */
Masami Hiramatsu5d16dbc2019-10-25 17:46:34 +09001064 die_entrypc(sp_die, &pf->addr);
Masami Hiramatsu0ad45b32016-09-24 00:35:07 +09001065 /* But in some case the entry address is 0 */
1066 if (pf->addr == 0) {
1067 pr_debug("%s has no entry PC. Skipped\n",
1068 dwarf_diename(sp_die));
1069 param->retval = 0;
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001070 /* Real function */
Masami Hiramatsu0ad45b32016-09-24 00:35:07 +09001071 } else if (pp->lazy_line)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001072 param->retval = find_probe_point_lazy(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001073 else {
Ravi Bangoriae47392b2016-08-03 14:28:45 +05301074 skip_prologue(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001075 pf->addr += pp->offset;
1076 /* TODO: Check the address in this function */
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001077 param->retval = call_probe_finder(sp_die, pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001078 }
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001079 } else if (!probe_conf.no_inlines) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001080 /* Inlined function: search instances */
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001081 param->retval = die_walk_instances(sp_die,
1082 probe_point_inline_cb, (void *)pf);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001083 /* This could be a non-existed inline definition */
Masami Hiramatsuf8da4b52016-09-24 00:34:57 +09001084 if (param->retval == -ENOENT)
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001085 param->retval = 0;
1086 }
1087
1088 /* We need to find other candidates */
1089 if (strisglob(pp->function) && param->retval >= 0) {
1090 param->retval = 0; /* We have to clear the result */
1091 return DWARF_CB_OK;
1092 }
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001093
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001094 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001095}
1096
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001097static int find_probe_point_by_func(struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001098{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001099 struct dwarf_callback_param _param = {.data = (void *)pf,
1100 .retval = 0};
1101 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1102 return _param.retval;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001103}
1104
Lin Mingcd25f8b2011-03-25 16:27:48 +08001105struct pubname_callback_param {
1106 char *function;
1107 char *file;
1108 Dwarf_Die *cu_die;
1109 Dwarf_Die *sp_die;
1110 int found;
1111};
1112
1113static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1114{
1115 struct pubname_callback_param *param = data;
1116
1117 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1118 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1119 return DWARF_CB_OK;
1120
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001121 if (die_match_name(param->sp_die, param->function)) {
Lin Mingcd25f8b2011-03-25 16:27:48 +08001122 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1123 return DWARF_CB_OK;
1124
1125 if (param->file &&
1126 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1127 return DWARF_CB_OK;
1128
1129 param->found = 1;
1130 return DWARF_CB_ABORT;
1131 }
1132 }
1133
1134 return DWARF_CB_OK;
1135}
1136
Hemant Kumar270bde12016-02-02 20:56:46 +05301137static int debuginfo__find_probe_location(struct debuginfo *dbg,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001138 struct probe_finder *pf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001139{
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001140 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001141 Dwarf_Off off, noff;
1142 size_t cuhl;
1143 Dwarf_Die *diep;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001144 int ret = 0;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001145
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001146 off = 0;
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001147 pf->lcache = intlist__new(NULL);
1148 if (!pf->lcache)
1149 return -ENOMEM;
Lin Mingcd25f8b2011-03-25 16:27:48 +08001150
1151 /* Fastpath: lookup by function name from .debug_pubnames section */
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001152 if (pp->function && !strisglob(pp->function)) {
Lin Mingcd25f8b2011-03-25 16:27:48 +08001153 struct pubname_callback_param pubname_param = {
1154 .function = pp->function,
1155 .file = pp->file,
1156 .cu_die = &pf->cu_die,
1157 .sp_die = &pf->sp_die,
Lin Ming2b348a72011-04-29 08:41:57 +00001158 .found = 0,
Lin Mingcd25f8b2011-03-25 16:27:48 +08001159 };
1160 struct dwarf_callback_param probe_param = {
1161 .data = pf,
1162 };
1163
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001164 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001165 &pubname_param, 0);
Lin Mingcd25f8b2011-03-25 16:27:48 +08001166 if (pubname_param.found) {
1167 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1168 if (ret)
1169 goto found;
1170 }
1171 }
1172
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001173 /* Loop on CUs (Compilation Unit) */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001174 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001175 /* Get the DIE(Debugging Information Entry) of this CU */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001176 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001177 if (!diep)
1178 continue;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001179
1180 /* Check if target file is included. */
1181 if (pp->file)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001182 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001183 else
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001184 pf->fname = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001185
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001186 if (!pp->file || pf->fname) {
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001187 if (pp->function)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001188 ret = find_probe_point_by_func(pf);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001189 else if (pp->lazy_line)
He Kuangf19e80c2015-04-13 19:41:30 +08001190 ret = find_probe_point_lazy(&pf->cu_die, pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001191 else {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001192 pf->lno = pp->line;
1193 ret = find_probe_point_by_line(pf);
Masami Hiramatsub0ef0732009-10-27 16:43:19 -04001194 }
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001195 if (ret < 0)
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001196 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001197 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001198 off = noff;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001199 }
Lin Mingcd25f8b2011-03-25 16:27:48 +08001200
1201found:
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001202 intlist__delete(pf->lcache);
1203 pf->lcache = NULL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001204
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001205 return ret;
1206}
1207
Hemant Kumar270bde12016-02-02 20:56:46 +05301208/* Find probe points from debuginfo */
1209static int debuginfo__find_probes(struct debuginfo *dbg,
1210 struct probe_finder *pf)
1211{
1212 int ret = 0;
Hemant Kumar270bde12016-02-02 20:56:46 +05301213 Elf *elf;
1214 GElf_Ehdr ehdr;
Hemant Kumar270bde12016-02-02 20:56:46 +05301215
1216 if (pf->cfi_eh || pf->cfi_dbg)
1217 return debuginfo__find_probe_location(dbg, pf);
1218
1219 /* Get the call frame information from this dwarf */
1220 elf = dwarf_getelf(dbg->dbg);
1221 if (elf == NULL)
1222 return -EINVAL;
1223
1224 if (gelf_getehdr(elf, &ehdr) == NULL)
1225 return -EINVAL;
1226
Masami Hiramatsu293d5b42016-08-26 01:24:57 +09001227 pf->machine = ehdr.e_machine;
Hemant Kumar270bde12016-02-02 20:56:46 +05301228
Masami Hiramatsu293d5b42016-08-26 01:24:57 +09001229#if _ELFUTILS_PREREQ(0, 142)
1230 do {
1231 GElf_Shdr shdr;
1232
1233 if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) &&
1234 shdr.sh_type == SHT_PROGBITS)
1235 pf->cfi_eh = dwarf_getcfi_elf(elf);
1236
1237 pf->cfi_dbg = dwarf_getcfi(dbg->dbg);
1238 } while (0);
Hemant Kumar270bde12016-02-02 20:56:46 +05301239#endif
1240
1241 ret = debuginfo__find_probe_location(dbg, pf);
1242 return ret;
1243}
1244
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001245struct local_vars_finder {
1246 struct probe_finder *pf;
1247 struct perf_probe_arg *args;
Masami Hiramatsuf8bffbf2015-05-06 21:46:53 +09001248 bool vars;
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001249 int max_args;
1250 int nargs;
1251 int ret;
1252};
1253
1254/* Collect available variables in this scope */
1255static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
1256{
1257 struct local_vars_finder *vf = data;
Masami Hiramatsu3d918a12013-10-11 16:10:26 +09001258 struct probe_finder *pf = vf->pf;
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001259 int tag;
1260
1261 tag = dwarf_tag(die_mem);
1262 if (tag == DW_TAG_formal_parameter ||
Masami Hiramatsuf8bffbf2015-05-06 21:46:53 +09001263 (tag == DW_TAG_variable && vf->vars)) {
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001264 if (convert_variable_location(die_mem, vf->pf->addr,
Masami Hiramatsu3d918a12013-10-11 16:10:26 +09001265 vf->pf->fb_ops, &pf->sp_die,
Masami Hiramatsu293d5b42016-08-26 01:24:57 +09001266 pf->machine, NULL) == 0) {
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001267 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
1268 if (vf->args[vf->nargs].var == NULL) {
1269 vf->ret = -ENOMEM;
1270 return DIE_FIND_CB_END;
1271 }
1272 pr_debug(" %s", vf->args[vf->nargs].var);
1273 vf->nargs++;
1274 }
1275 }
1276
1277 if (dwarf_haspc(die_mem, vf->pf->addr))
1278 return DIE_FIND_CB_CONTINUE;
1279 else
1280 return DIE_FIND_CB_SIBLING;
1281}
1282
1283static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
1284 struct perf_probe_arg *args)
1285{
1286 Dwarf_Die die_mem;
1287 int i;
1288 int n = 0;
Masami Hiramatsuf8bffbf2015-05-06 21:46:53 +09001289 struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false,
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001290 .max_args = MAX_PROBE_ARGS, .ret = 0};
1291
1292 for (i = 0; i < pf->pev->nargs; i++) {
1293 /* var never be NULL */
Masami Hiramatsuf8bffbf2015-05-06 21:46:53 +09001294 if (strcmp(pf->pev->args[i].var, PROBE_ARG_VARS) == 0)
1295 vf.vars = true;
1296 else if (strcmp(pf->pev->args[i].var, PROBE_ARG_PARAMS) != 0) {
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001297 /* Copy normal argument */
1298 args[n] = pf->pev->args[i];
1299 n++;
Masami Hiramatsuf8bffbf2015-05-06 21:46:53 +09001300 continue;
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001301 }
Masami Hiramatsuf8bffbf2015-05-06 21:46:53 +09001302 pr_debug("Expanding %s into:", pf->pev->args[i].var);
1303 vf.nargs = n;
1304 /* Special local variables */
1305 die_find_child(sc_die, copy_variables_cb, (void *)&vf,
1306 &die_mem);
1307 pr_debug(" (%d)\n", vf.nargs - n);
1308 if (vf.ret < 0)
1309 return vf.ret;
1310 n = vf.nargs;
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001311 }
1312 return n;
1313}
1314
Masami Hiramatsu1a375ae2019-09-19 12:41:10 +09001315static bool trace_event_finder_overlap(struct trace_event_finder *tf)
1316{
1317 int i;
1318
1319 for (i = 0; i < tf->ntevs; i++) {
1320 if (tf->pf.addr == tf->tevs[i].point.address)
1321 return true;
1322 }
1323 return false;
1324}
1325
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001326/* Add a found probe point into trace event list */
Masami Hiramatsu221d0612011-08-11 20:02:59 +09001327static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001328{
1329 struct trace_event_finder *tf =
1330 container_of(pf, struct trace_event_finder, pf);
Masami Hiramatsu6cca13b2015-10-01 01:41:37 +09001331 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001332 struct probe_trace_event *tev;
Wang Nan092b1f02015-11-13 12:29:11 +00001333 struct perf_probe_arg *args = NULL;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001334 int ret, i;
1335
Masami Hiramatsu1a375ae2019-09-19 12:41:10 +09001336 /*
1337 * For some reason (e.g. different column assigned to same address)
1338 * This callback can be called with the address which already passed.
1339 * Ignore it first.
1340 */
1341 if (trace_event_finder_overlap(tf))
1342 return 0;
1343
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001344 /* Check number of tevs */
1345 if (tf->ntevs == tf->max_tevs) {
1346 pr_warning("Too many( > %d) probe point found.\n",
1347 tf->max_tevs);
1348 return -ERANGE;
1349 }
1350 tev = &tf->tevs[tf->ntevs++];
1351
Masami Hiramatsu221d0612011-08-11 20:02:59 +09001352 /* Trace point should be converted from subprogram DIE */
Masami Hiramatsu576b5232013-09-25 22:16:16 +09001353 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
Masami Hiramatsu6cca13b2015-10-01 01:41:37 +09001354 pp->retprobe, pp->function, &tev->point);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001355 if (ret < 0)
Wang Nan092b1f02015-11-13 12:29:11 +00001356 goto end;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001357
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001358 tev->point.realname = strdup(dwarf_diename(sc_die));
Wang Nan092b1f02015-11-13 12:29:11 +00001359 if (!tev->point.realname) {
1360 ret = -ENOMEM;
1361 goto end;
1362 }
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001363
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001364 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1365 tev->point.offset);
1366
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001367 /* Expand special probe argument if exist */
1368 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
Wang Nan092b1f02015-11-13 12:29:11 +00001369 if (args == NULL) {
1370 ret = -ENOMEM;
1371 goto end;
1372 }
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001373
1374 ret = expand_probe_args(sc_die, pf, args);
1375 if (ret < 0)
1376 goto end;
1377
1378 tev->nargs = ret;
1379 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1380 if (tev->args == NULL) {
1381 ret = -ENOMEM;
1382 goto end;
1383 }
1384
1385 /* Find each argument */
1386 for (i = 0; i < tev->nargs; i++) {
1387 pf->pvar = &args[i];
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001388 pf->tvar = &tev->args[i];
Masami Hiramatsu221d0612011-08-11 20:02:59 +09001389 /* Variable should be found from scope DIE */
1390 ret = find_variable(sc_die, pf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001391 if (ret != 0)
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001392 break;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001393 }
1394
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001395end:
Wang Nan092b1f02015-11-13 12:29:11 +00001396 if (ret) {
1397 clear_probe_trace_event(tev);
1398 tf->ntevs--;
1399 }
Masami Hiramatsu7969ec72013-10-11 16:10:23 +09001400 free(args);
1401 return ret;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001402}
1403
Masami Hiramatsucb402732019-11-18 17:12:49 +09001404static int fill_empty_trace_arg(struct perf_probe_event *pev,
1405 struct probe_trace_event *tevs, int ntevs)
1406{
1407 char **valp;
1408 char *type;
1409 int i, j, ret;
1410
Masami Hiramatsu11fd3eb2020-07-10 22:11:13 +09001411 if (!ntevs)
1412 return -ENOENT;
1413
Masami Hiramatsucb402732019-11-18 17:12:49 +09001414 for (i = 0; i < pev->nargs; i++) {
1415 type = NULL;
1416 for (j = 0; j < ntevs; j++) {
1417 if (tevs[j].args[i].value) {
1418 type = tevs[j].args[i].type;
1419 break;
1420 }
1421 }
1422 if (j == ntevs) {
1423 print_var_not_found(pev->args[i].var);
1424 return -ENOENT;
1425 }
1426 for (j = 0; j < ntevs; j++) {
1427 valp = &tevs[j].args[i].value;
1428 if (*valp)
1429 continue;
1430
1431 ret = asprintf(valp, "\\%lx", probe_conf.magic_num);
1432 if (ret < 0)
1433 return -ENOMEM;
1434 /* Note that type can be NULL */
1435 if (type) {
1436 tevs[j].args[i].type = strdup(type);
1437 if (!tevs[j].args[i].type)
1438 return -ENOMEM;
1439 }
1440 }
1441 }
1442 return 0;
1443}
1444
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001445/* Find probe_trace_events specified by perf_probe_event from debuginfo */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001446int debuginfo__find_trace_events(struct debuginfo *dbg,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001447 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001448 struct probe_trace_event **tevs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001449{
1450 struct trace_event_finder tf = {
1451 .pf = {.pev = pev, .callback = add_probe_trace_event},
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001452 .max_tevs = probe_conf.max_probes, .mod = dbg->mod};
Masami Hiramatsu0196e782015-11-13 12:29:10 +00001453 int ret, i;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001454
1455 /* Allocate result tevs array */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001456 *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001457 if (*tevs == NULL)
1458 return -ENOMEM;
1459
1460 tf.tevs = *tevs;
1461 tf.ntevs = 0;
1462
Masami Hiramatsucb402732019-11-18 17:12:49 +09001463 if (pev->nargs != 0 && immediate_value_is_supported())
1464 tf.pf.skip_empty_arg = true;
1465
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001466 ret = debuginfo__find_probes(dbg, &tf.pf);
Masami Hiramatsucb402732019-11-18 17:12:49 +09001467 if (ret >= 0 && tf.pf.skip_empty_arg)
1468 ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
1469
Masami Hiramatsu12d572e2020-07-10 22:11:23 +09001470 if (ret < 0 || tf.ntevs == 0) {
Masami Hiramatsu0196e782015-11-13 12:29:10 +00001471 for (i = 0; i < tf.ntevs; i++)
1472 clear_probe_trace_event(&tf.tevs[i]);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001473 zfree(tevs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001474 return ret;
1475 }
1476
1477 return (ret < 0) ? ret : tf.ntevs;
1478}
1479
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001480/* Collect available variables in this scope */
1481static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1482{
1483 struct available_var_finder *af = data;
1484 struct variable_list *vl;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001485 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001486 int tag, ret;
1487
1488 vl = &af->vls[af->nvls - 1];
1489
1490 tag = dwarf_tag(die_mem);
1491 if (tag == DW_TAG_formal_parameter ||
1492 tag == DW_TAG_variable) {
1493 ret = convert_variable_location(die_mem, af->pf.addr,
Masami Hiramatsu3d918a12013-10-11 16:10:26 +09001494 af->pf.fb_ops, &af->pf.sp_die,
Masami Hiramatsu293d5b42016-08-26 01:24:57 +09001495 af->pf.machine, NULL);
He Kuang349e8d22015-05-11 09:25:03 +00001496 if (ret == 0 || ret == -ERANGE) {
1497 int ret2;
1498 bool externs = !af->child;
He Kuangfb9596d2015-05-11 09:25:02 +00001499
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001500 if (strbuf_init(&buf, 64) < 0)
1501 goto error;
He Kuang349e8d22015-05-11 09:25:03 +00001502
1503 if (probe_conf.show_location_range) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001504 if (!externs)
1505 ret2 = strbuf_add(&buf,
1506 ret ? "[INV]\t" : "[VAL]\t", 6);
1507 else
1508 ret2 = strbuf_add(&buf, "[EXT]\t", 6);
1509 if (ret2)
1510 goto error;
He Kuang349e8d22015-05-11 09:25:03 +00001511 }
1512
1513 ret2 = die_get_varname(die_mem, &buf);
1514
1515 if (!ret2 && probe_conf.show_location_range &&
1516 !externs) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001517 if (strbuf_addch(&buf, '\t') < 0)
1518 goto error;
He Kuang349e8d22015-05-11 09:25:03 +00001519 ret2 = die_get_var_range(&af->pf.sp_die,
1520 die_mem, &buf);
1521 }
1522
1523 pr_debug("Add new var: %s\n", buf.buf);
1524 if (ret2 == 0) {
He Kuangfb9596d2015-05-11 09:25:02 +00001525 strlist__add(vl->vars,
1526 strbuf_detach(&buf, NULL));
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001527 }
1528 strbuf_release(&buf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001529 }
1530 }
1531
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001532 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001533 return DIE_FIND_CB_CONTINUE;
1534 else
1535 return DIE_FIND_CB_SIBLING;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001536error:
1537 strbuf_release(&buf);
1538 pr_debug("Error in strbuf\n");
1539 return DIE_FIND_CB_END;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001540}
1541
Masami Hiramatsudee36a22019-10-30 16:09:49 +09001542static bool available_var_finder_overlap(struct available_var_finder *af)
1543{
1544 int i;
1545
1546 for (i = 0; i < af->nvls; i++) {
1547 if (af->pf.addr == af->vls[i].point.address)
1548 return true;
1549 }
1550 return false;
1551
1552}
1553
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001554/* Add a found vars into available variables list */
Masami Hiramatsu221d0612011-08-11 20:02:59 +09001555static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001556{
1557 struct available_var_finder *af =
1558 container_of(pf, struct available_var_finder, pf);
Masami Hiramatsu6cca13b2015-10-01 01:41:37 +09001559 struct perf_probe_point *pp = &pf->pev->point;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001560 struct variable_list *vl;
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +09001561 Dwarf_Die die_mem;
1562 int ret;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001563
Masami Hiramatsudee36a22019-10-30 16:09:49 +09001564 /*
1565 * For some reason (e.g. different column assigned to same address),
1566 * this callback can be called with the address which already passed.
1567 * Ignore it first.
1568 */
1569 if (available_var_finder_overlap(af))
1570 return 0;
1571
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001572 /* Check number of tevs */
1573 if (af->nvls == af->max_vls) {
1574 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1575 return -ERANGE;
1576 }
1577 vl = &af->vls[af->nvls++];
1578
Masami Hiramatsu221d0612011-08-11 20:02:59 +09001579 /* Trace point should be converted from subprogram DIE */
Masami Hiramatsu576b5232013-09-25 22:16:16 +09001580 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
Masami Hiramatsu6cca13b2015-10-01 01:41:37 +09001581 pp->retprobe, pp->function, &vl->point);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001582 if (ret < 0)
1583 return ret;
1584
1585 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1586 vl->point.offset);
1587
1588 /* Find local variables */
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03001589 vl->vars = strlist__new(NULL, NULL);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001590 if (vl->vars == NULL)
1591 return -ENOMEM;
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001592 af->child = true;
Masami Hiramatsu221d0612011-08-11 20:02:59 +09001593 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001594
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001595 /* Find external variables */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001596 if (!probe_conf.show_ext_vars)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001597 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001598 /* Don't need to search child DIE for external vars. */
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001599 af->child = false;
Masami Hiramatsuf182e3e2011-08-11 20:03:05 +09001600 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +09001601
1602out:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001603 if (strlist__empty(vl->vars)) {
1604 strlist__delete(vl->vars);
1605 vl->vars = NULL;
1606 }
1607
1608 return ret;
1609}
1610
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001611/*
1612 * Find available variables at given probe point
1613 * Return the number of found probe points. Return 0 if there is no
1614 * matched probe point. Return <0 if an error occurs.
1615 */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001616int debuginfo__find_available_vars_at(struct debuginfo *dbg,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001617 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001618 struct variable_list **vls)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001619{
1620 struct available_var_finder af = {
1621 .pf = {.pev = pev, .callback = add_available_vars},
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001622 .mod = dbg->mod,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001623 .max_vls = probe_conf.max_probes};
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001624 int ret;
1625
1626 /* Allocate result vls array */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001627 *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001628 if (*vls == NULL)
1629 return -ENOMEM;
1630
1631 af.vls = *vls;
1632 af.nvls = 0;
1633
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001634 ret = debuginfo__find_probes(dbg, &af.pf);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001635 if (ret < 0) {
1636 /* Free vlist for error */
1637 while (af.nvls--) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001638 zfree(&af.vls[af.nvls].point.symbol);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001639 strlist__delete(af.vls[af.nvls].vars);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001640 }
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001641 zfree(vls);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001642 return ret;
1643 }
1644
1645 return (ret < 0) ? ret : af.nvls;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001646}
1647
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001648/* For the kernel module, we need a special code to get a DIE */
Masami Hiramatsu613f0502017-01-11 15:01:57 +09001649int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
1650 bool adjust_offset)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001651{
1652 int n, i;
1653 Elf32_Word shndx;
1654 Elf_Scn *scn;
1655 Elf *elf;
1656 GElf_Shdr mem, *shdr;
1657 const char *p;
1658
1659 elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
1660 if (!elf)
1661 return -EINVAL;
1662
1663 /* Get the number of relocations */
1664 n = dwfl_module_relocations(dbg->mod);
1665 if (n < 0)
1666 return -ENOENT;
1667 /* Search the relocation related .text section */
1668 for (i = 0; i < n; i++) {
1669 p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
1670 if (strcmp(p, ".text") == 0) {
1671 /* OK, get the section header */
1672 scn = elf_getscn(elf, shndx);
1673 if (!scn)
1674 return -ENOENT;
1675 shdr = gelf_getshdr(scn, &mem);
1676 if (!shdr)
1677 return -ENOENT;
1678 *offs = shdr->sh_addr;
Masami Hiramatsu613f0502017-01-11 15:01:57 +09001679 if (adjust_offset)
1680 *offs -= shdr->sh_offset;
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001681 }
1682 }
1683 return 0;
1684}
1685
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001686/* Reverse search */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001687int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001688 struct perf_probe_point *ppt)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001689{
1690 Dwarf_Die cudie, spdie, indie;
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001691 Dwarf_Addr _addr = 0, baseaddr = 0;
1692 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001693 int baseline = 0, lineno = 0, ret = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001694
Masami Hiramatsud2d4edb2017-01-11 14:59:38 +09001695 /* We always need to relocate the address for aranges */
Masami Hiramatsu613f0502017-01-11 15:01:57 +09001696 if (debuginfo__get_text_offset(dbg, &baseaddr, false) == 0)
Masami Hiramatsud2d4edb2017-01-11 14:59:38 +09001697 addr += baseaddr;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001698 /* Find cu die */
Masami Hiramatsu0104fe62015-03-02 21:49:46 +09001699 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001700 pr_warning("Failed to find debug information for address %lx\n",
1701 addr);
Masami Hiramatsu75ec5a22010-04-02 12:50:59 -04001702 ret = -EINVAL;
1703 goto end;
1704 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001705
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001706 /* Find a corresponding line (filename and lineno) */
1707 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1708 /* Don't care whether it failed or not */
1709
1710 /* Find a corresponding function (name, baseline and baseaddr) */
Masami Hiramatsue0d153c2011-06-27 16:27:27 +09001711 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001712 /* Get function entry information */
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001713 func = basefunc = dwarf_diename(&spdie);
1714 if (!func ||
Masami Hiramatsu38955342019-10-25 17:46:52 +09001715 die_entrypc(&spdie, &baseaddr) != 0 ||
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001716 dwarf_decl_line(&spdie, &baseline) != 0) {
1717 lineno = 0;
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001718 goto post;
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001719 }
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001720
Masami Hiramatsu1b286bd2013-10-11 12:23:17 +00001721 fname = dwarf_decl_file(&spdie);
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001722 if (addr == (unsigned long)baseaddr) {
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001723 /* Function entry - Relative line number is 0 */
1724 lineno = baseline;
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001725 goto post;
1726 }
1727
1728 /* Track down the inline functions step by step */
1729 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
1730 &indie)) {
1731 /* There is an inline function */
Masami Hiramatsu38955342019-10-25 17:46:52 +09001732 if (die_entrypc(&indie, &_addr) == 0 &&
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001733 _addr == addr) {
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001734 /*
1735 * addr is at an inline function entry.
1736 * In this case, lineno should be the call-site
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001737 * line number. (overwrite lineinfo)
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001738 */
1739 lineno = die_get_call_lineno(&indie);
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001740 fname = die_get_call_file(&indie);
1741 break;
1742 } else {
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001743 /*
1744 * addr is in an inline function body.
1745 * Since lineno points one of the lines
1746 * of the inline function, baseline should
1747 * be the entry line of the inline function.
1748 */
1749 tmp = dwarf_diename(&indie);
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001750 if (!tmp ||
1751 dwarf_decl_line(&indie, &baseline) != 0)
1752 break;
1753 func = tmp;
1754 spdie = indie;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001755 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001756 }
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001757 /* Verify the lineno and baseline are in a same file */
1758 tmp = dwarf_decl_file(&spdie);
1759 if (!tmp || strcmp(tmp, fname) != 0)
1760 lineno = 0;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001761 }
1762
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001763post:
1764 /* Make a relative line number or an offset */
1765 if (lineno)
1766 ppt->line = lineno - baseline;
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001767 else if (basefunc) {
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001768 ppt->offset = addr - (unsigned long)baseaddr;
Masami Hiramatsue08cfd42013-09-30 18:21:44 +09001769 func = basefunc;
1770 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001771
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001772 /* Duplicate strings */
1773 if (func) {
1774 ppt->function = strdup(func);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001775 if (ppt->function == NULL) {
1776 ret = -ENOMEM;
1777 goto end;
1778 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001779 }
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001780 if (fname) {
1781 ppt->file = strdup(fname);
1782 if (ppt->file == NULL) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001783 zfree(&ppt->function);
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001784 ret = -ENOMEM;
1785 goto end;
1786 }
1787 }
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001788end:
Masami Hiramatsu1d46ea22011-03-30 18:26:05 +09001789 if (ret == 0 && (fname || func))
1790 ret = 1; /* Found a point */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001791 return ret;
1792}
1793
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001794/* Add a line and store the src path */
1795static int line_range_add_line(const char *src, unsigned int lineno,
1796 struct line_range *lr)
1797{
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001798 /* Copy source path */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001799 if (!lr->path) {
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001800 lr->path = strdup(src);
1801 if (lr->path == NULL)
1802 return -ENOMEM;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001803 }
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001804 return intlist__add(lr->line_list, lineno);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001805}
1806
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001807static int line_range_walk_cb(const char *fname, int lineno,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001808 Dwarf_Addr addr __maybe_unused,
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001809 void *data)
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001810{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001811 struct line_finder *lf = data;
Masami Hiramatsu499144c2019-11-18 17:12:10 +09001812 const char *__fname;
1813 int __lineno;
Namhyung Kim202c7c12014-04-01 13:47:57 +09001814 int err;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001815
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001816 if ((strtailcmp(fname, lf->fname) != 0) ||
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001817 (lf->lno_s > lineno || lf->lno_e < lineno))
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001818 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001819
Masami Hiramatsu499144c2019-11-18 17:12:10 +09001820 /* Make sure this line can be reversable */
1821 if (cu_find_lineinfo(&lf->cu_die, addr, &__fname, &__lineno) > 0
1822 && (lineno != __lineno || strcmp(fname, __fname)))
1823 return 0;
1824
Namhyung Kim202c7c12014-04-01 13:47:57 +09001825 err = line_range_add_line(fname, lineno, lf->lr);
1826 if (err < 0 && err != -EEXIST)
1827 return err;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001828
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001829 return 0;
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001830}
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001831
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001832/* Find line range from its line number */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001833static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001834{
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001835 int ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001836
Masami Hiramatsu4cc9cec2011-01-13 21:45:58 +09001837 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001838
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001839 /* Update status */
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001840 if (ret >= 0)
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001841 if (!intlist__empty(lf->lr->line_list))
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001842 ret = lf->found = 1;
1843 else
1844 ret = 0; /* Lines are not found */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001845 else {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001846 zfree(&lf->lr->path);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001847 }
Masami Hiramatsuf6c903f2010-04-14 18:40:07 -04001848 return ret;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001849}
1850
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001851static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1852{
Masami Hiramatsu182c2282014-04-02 14:48:31 +09001853 int ret = find_line_range_by_line(in_die, data);
Masami Hiramatsu36c0c582011-08-11 20:02:47 +09001854
1855 /*
1856 * We have to check all instances of inlined function, because
1857 * some execution paths can be optimized out depends on the
Masami Hiramatsu182c2282014-04-02 14:48:31 +09001858 * function argument of instances. However, if an error occurs,
1859 * it should be handled by the caller.
Masami Hiramatsu36c0c582011-08-11 20:02:47 +09001860 */
Masami Hiramatsu182c2282014-04-02 14:48:31 +09001861 return ret < 0 ? ret : 0;
Masami Hiramatsu161a26b2010-02-25 08:35:57 -05001862}
1863
Masami Hiramatsu0dbb1ca2012-04-23 12:24:36 +09001864/* Search function definition from function name */
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001865static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001866{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001867 struct dwarf_callback_param *param = data;
1868 struct line_finder *lf = param->data;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001869 struct line_range *lr = lf->lr;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001870
Masami Hiramatsu7d216352011-03-30 18:25:41 +09001871 /* Check declared file */
1872 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1873 return DWARF_CB_OK;
1874
Masami Hiramatsu0dbb1ca2012-04-23 12:24:36 +09001875 if (die_is_func_def(sp_die) &&
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001876 die_match_name(sp_die, lr->function)) {
Masami Hiramatsue92b85e2010-02-25 08:35:50 -05001877 lf->fname = dwarf_decl_file(sp_die);
1878 dwarf_decl_line(sp_die, &lr->offset);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001879 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001880 lf->lno_s = lr->offset + lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001881 if (lf->lno_s < 0) /* Overflow */
1882 lf->lno_s = INT_MAX;
1883 lf->lno_e = lr->offset + lr->end;
1884 if (lf->lno_e < 0) /* Overflow */
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001885 lf->lno_e = INT_MAX;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001886 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001887 lr->start = lf->lno_s;
1888 lr->end = lf->lno_e;
Masami Hiramatsue1ecbbc2015-01-30 18:37:44 +09001889 if (!die_is_func_instance(sp_die))
Masami Hiramatsudb0d2c62011-08-11 20:03:11 +09001890 param->retval = die_walk_instances(sp_die,
1891 line_range_inline_cb, lf);
1892 else
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001893 param->retval = find_line_range_by_line(sp_die, lf);
1894 return DWARF_CB_ABORT;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001895 }
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001896 return DWARF_CB_OK;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001897}
1898
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001899static int find_line_range_by_func(struct line_finder *lf)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001900{
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001901 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1902 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1903 return param.retval;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001904}
1905
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001906int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001907{
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001908 struct line_finder lf = {.lr = lr, .found = 0};
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001909 int ret = 0;
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001910 Dwarf_Off off = 0, noff;
1911 size_t cuhl;
1912 Dwarf_Die *diep;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001913 const char *comp_dir;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001914
Lin Mingcd25f8b2011-03-25 16:27:48 +08001915 /* Fastpath: lookup by function name from .debug_pubnames section */
1916 if (lr->function) {
1917 struct pubname_callback_param pubname_param = {
1918 .function = lr->function, .file = lr->file,
1919 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1920 struct dwarf_callback_param line_range_param = {
1921 .data = (void *)&lf, .retval = 0};
1922
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001923 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001924 &pubname_param, 0);
Lin Mingcd25f8b2011-03-25 16:27:48 +08001925 if (pubname_param.found) {
1926 line_range_search_cb(&lf.sp_die, &line_range_param);
1927 if (lf.found)
1928 goto found;
1929 }
1930 }
1931
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001932 /* Loop on CUs (Compilation Unit) */
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001933 while (!lf.found && ret >= 0) {
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001934 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
Masami Hiramatsuff741782011-06-27 16:27:39 +09001935 NULL, NULL, NULL) != 0)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001936 break;
1937
1938 /* Get the DIE(Debugging Information Entry) of this CU */
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001939 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001940 if (!diep)
1941 continue;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001942
1943 /* Check if target file is included. */
1944 if (lr->file)
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001945 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001946 else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001947 lf.fname = 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001948
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001949 if (!lr->file || lf.fname) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001950 if (lr->function)
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001951 ret = find_line_range_by_func(&lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001952 else {
1953 lf.lno_s = lr->start;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001954 lf.lno_e = lr->end;
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001955 ret = find_line_range_by_line(NULL, &lf);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001956 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001957 }
Masami Hiramatsu804b3602010-02-25 08:35:42 -05001958 off = noff;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001959 }
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001960
Lin Mingcd25f8b2011-03-25 16:27:48 +08001961found:
Masami Hiramatsu6a330a32010-07-09 18:29:11 +09001962 /* Store comp_dir */
1963 if (lf.found) {
1964 comp_dir = cu_get_comp_dir(&lf.cu_die);
1965 if (comp_dir) {
1966 lr->comp_dir = strdup(comp_dir);
1967 if (!lr->comp_dir)
1968 ret = -ENOMEM;
1969 }
1970 }
1971
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +09001972 pr_debug("path: %s\n", lr->path);
Masami Hiramatsub55a87a2010-04-12 13:17:35 -04001973 return (ret < 0) ? ret : lf.found;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001974}
1975
Naohiro Aota09ed8972015-03-13 14:18:40 +09001976/*
1977 * Find a src file from a DWARF tag path. Prepend optional source path prefix
1978 * and chop off leading directories that do not exist. Result is passed back as
1979 * a newly allocated path on success.
1980 * Return 0 if file was found and readable, -errno otherwise.
1981 */
1982int get_real_path(const char *raw_path, const char *comp_dir,
1983 char **new_path)
1984{
1985 const char *prefix = symbol_conf.source_prefix;
1986
1987 if (!prefix) {
1988 if (raw_path[0] != '/' && comp_dir)
1989 /* If not an absolute path, try to use comp_dir */
1990 prefix = comp_dir;
1991 else {
1992 if (access(raw_path, R_OK) == 0) {
1993 *new_path = strdup(raw_path);
1994 return *new_path ? 0 : -ENOMEM;
1995 } else
1996 return -errno;
1997 }
1998 }
1999
2000 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
2001 if (!*new_path)
2002 return -ENOMEM;
2003
2004 for (;;) {
2005 sprintf(*new_path, "%s/%s", prefix, raw_path);
2006
2007 if (access(*new_path, R_OK) == 0)
2008 return 0;
2009
2010 if (!symbol_conf.source_prefix) {
2011 /* In case of searching comp_dir, don't retry */
2012 zfree(new_path);
2013 return -errno;
2014 }
2015
2016 switch (errno) {
2017 case ENAMETOOLONG:
2018 case ENOENT:
2019 case EROFS:
2020 case EFAULT:
2021 raw_path = strchr(++raw_path, '/');
2022 if (!raw_path) {
2023 zfree(new_path);
2024 return -ENOENT;
2025 }
2026 continue;
2027
2028 default:
2029 zfree(new_path);
2030 return -errno;
2031 }
2032 }
2033}