Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 2 | /* |
| 3 | * probe-finder.c : C expression to kprobe event converter |
| 4 | * |
| 5 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 8 | #include <inttypes.h> |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 9 | #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 Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <stdarg.h> |
Ian Munsie | cd932c5 | 2010-04-20 16:58:32 +1000 | [diff] [blame] | 19 | #include <dwarf-regs.h> |
Masami Hiramatsu | 074fc0e | 2009-10-16 20:08:01 -0400 | [diff] [blame] | 20 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 22 | #include <linux/zalloc.h> |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 23 | #include "event.h" |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 24 | #include "dso.h" |
Masami Hiramatsu | 89c69c0 | 2009-10-16 20:08:10 -0400 | [diff] [blame] | 25 | #include "debug.h" |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 26 | #include "intlist.h" |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 27 | #include "strbuf.h" |
Arnaldo Carvalho de Melo | 8ec20b1 | 2017-04-18 10:57:25 -0300 | [diff] [blame] | 28 | #include "strlist.h" |
Chase Douglas | 9ed7e1b | 2010-06-14 15:26:30 -0400 | [diff] [blame] | 29 | #include "symbol.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 30 | #include "probe-finder.h" |
Masami Hiramatsu | 180b206 | 2016-08-18 17:58:31 +0900 | [diff] [blame] | 31 | #include "probe-file.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 32 | #include "string2.h" |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 33 | |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 34 | /* Kprobe tracer basic type is up to u64 */ |
| 35 | #define MAX_BASIC_TYPE_BITS 64 |
| 36 | |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 37 | /* Dwarf FL wrappers */ |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 38 | static char *debuginfo_path; /* Currently dummy */ |
| 39 | |
| 40 | static 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 Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 50 | /* Get a Dwarf from offline image */ |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 51 | static int debuginfo__init_offline_dwarf(struct debuginfo *dbg, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 52 | const char *path) |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 53 | { |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 54 | int fd; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 55 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 56 | fd = open(path, O_RDONLY); |
| 57 | if (fd < 0) |
| 58 | return fd; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 59 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 60 | dbg->dwfl = dwfl_begin(&offline_callbacks); |
| 61 | if (!dbg->dwfl) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 62 | goto error; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 63 | |
Masami Hiramatsu | 9135949 | 2015-10-01 01:41:28 +0900 | [diff] [blame] | 64 | dwfl_report_begin(dbg->dwfl); |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 65 | dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd); |
| 66 | if (!dbg->mod) |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 67 | goto error; |
| 68 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 69 | dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias); |
| 70 | if (!dbg->dbg) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 71 | goto error; |
| 72 | |
Masami Hiramatsu | 9135949 | 2015-10-01 01:41:28 +0900 | [diff] [blame] | 73 | dwfl_report_end(dbg->dwfl, NULL, NULL); |
| 74 | |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 75 | return 0; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 76 | error: |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 77 | if (dbg->dwfl) |
| 78 | dwfl_end(dbg->dwfl); |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 79 | else |
| 80 | close(fd); |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 81 | memset(dbg, 0, sizeof(*dbg)); |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 82 | |
| 83 | return -ENOENT; |
Masami Hiramatsu | 469b9b8 | 2010-10-21 19:13:41 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 86 | static struct debuginfo *__debuginfo__new(const char *path) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 87 | { |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 88 | struct debuginfo *dbg = zalloc(sizeof(*dbg)); |
| 89 | if (!dbg) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 90 | return NULL; |
| 91 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 92 | if (debuginfo__init_offline_dwarf(dbg, path) < 0) |
| 93 | zfree(&dbg); |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 94 | if (dbg) |
| 95 | pr_debug("Open Debuginfo file: %s\n", path); |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 96 | return dbg; |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 99 | enum 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 Hunter | 85afd35 | 2020-05-26 18:52:07 +0300 | [diff] [blame] | 104 | DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO, |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 105 | DSO_BINARY_TYPE__NOT_FOUND, |
| 106 | }; |
| 107 | |
| 108 | struct 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 Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 128 | dso__put(dso); |
Masami Hiramatsu | a15ad2f | 2014-02-06 05:32:27 +0000 | [diff] [blame] | 129 | |
| 130 | out: |
| 131 | /* if failed to open all distro debuginfo, open given binary */ |
| 132 | return dinfo ? : __debuginfo__new(path); |
| 133 | } |
| 134 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 135 | void debuginfo__delete(struct debuginfo *dbg) |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 136 | { |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 137 | if (dbg) { |
| 138 | if (dbg->dwfl) |
| 139 | dwfl_end(dbg->dwfl); |
| 140 | free(dbg); |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 144 | /* |
| 145 | * Probe finder related functions |
| 146 | */ |
| 147 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 148 | static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 149 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 150 | struct probe_trace_arg_ref *ref; |
| 151 | ref = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 152 | if (ref != NULL) |
| 153 | ref->offset = offs; |
| 154 | return ref; |
| 155 | } |
| 156 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 157 | /* |
| 158 | * Convert a location into trace_arg. |
| 159 | * If tvar == NULL, this just checks variable can be converted. |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 160 | * If fentry == true and vr_die is a parameter, do huristic search |
| 161 | * for the location fuzzed by function entry mcount. |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 162 | */ |
| 163 | static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr, |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 164 | Dwarf_Op *fb_ops, Dwarf_Die *sp_die, |
Masami Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 165 | unsigned int machine, |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 166 | struct probe_trace_arg *tvar) |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 167 | { |
| 168 | Dwarf_Attribute attr; |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 169 | Dwarf_Addr tmp = 0; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 170 | Dwarf_Op *op; |
| 171 | size_t nops; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 172 | unsigned int regn; |
| 173 | Dwarf_Word offs = 0; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 174 | bool ref = false; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 175 | const char *regs; |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 176 | int ret, ret2 = 0; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 177 | |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 178 | if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL) |
| 179 | goto static_var; |
| 180 | |
Masami Hiramatsu | 66f69b2 | 2019-11-18 17:12:40 +0900 | [diff] [blame] | 181 | /* 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 Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 192 | /* TODO: handle more than 1 exprs */ |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 193 | 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 Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 197 | 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 Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 210 | 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 Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 220 | return -ENOENT; |
| 221 | } |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 222 | found: |
| 223 | if (nops == 0) |
| 224 | /* TODO: Support const_value */ |
| 225 | return -ENOENT; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 226 | |
| 227 | if (op->atom == DW_OP_addr) { |
Masami Hiramatsu | 632941c | 2010-10-21 19:13:16 +0900 | [diff] [blame] | 228 | static_var: |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 229 | if (!tvar) |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 230 | return ret2; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 231 | /* 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 Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 240 | return ret2; |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 241 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 242 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 243 | /* If this is based on frame buffer, set the offset */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 244 | if (op->atom == DW_OP_fbreg) { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 245 | if (fb_ops == NULL) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 246 | return -ENOTSUP; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 247 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 248 | offs = op->number; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 249 | op = &fb_ops[0]; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 250 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 251 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 252 | if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { |
| 253 | regn = op->atom - DW_OP_breg0; |
| 254 | offs += op->number; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 255 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 256 | } 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 Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 261 | ref = true; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 262 | } else if (op->atom == DW_OP_regx) { |
| 263 | regn = op->number; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 264 | } else { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 265 | pr_debug("DW_OP %x is not supported.\n", op->atom); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 266 | return -ENOTSUP; |
| 267 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 268 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 269 | if (!tvar) |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 270 | return ret2; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 271 | |
Masami Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 272 | regs = get_dwarf_regstr(regn, machine); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 273 | if (!regs) { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 274 | /* This should be a bug in DWARF or this tool */ |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 275 | pr_warning("Mapping for the register number %u " |
| 276 | "missing on this architecture.\n", regn); |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 277 | return -ENOTSUP; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 278 | } |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 279 | |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 280 | tvar->value = strdup(regs); |
| 281 | if (tvar->value == NULL) |
| 282 | return -ENOMEM; |
| 283 | |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 284 | if (ref) { |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 285 | tvar->ref = alloc_trace_arg_ref((long)offs); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 286 | if (tvar->ref == NULL) |
| 287 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 288 | } |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 289 | return ret2; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 292 | #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long)) |
| 293 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 294 | static int convert_variable_type(Dwarf_Die *vr_die, |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 295 | struct probe_trace_arg *tvar, |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 296 | const char *cast, bool user_access) |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 297 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 298 | struct probe_trace_arg_ref **ref_ptr = &tvar->ref; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 299 | Dwarf_Die type; |
| 300 | char buf[16]; |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 301 | char sbuf[STRERR_BUFSIZE]; |
Masami Hiramatsu | bcfc082 | 2011-06-27 16:27:21 +0900 | [diff] [blame] | 302 | int bsize, boffs, total; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 303 | int ret; |
Masami Hiramatsu | 9254378 | 2016-08-18 17:58:47 +0900 | [diff] [blame] | 304 | char prefix; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 305 | |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 306 | /* TODO: check all types */ |
Thomas Richter | 1873f15 | 2020-01-20 14:20:10 +0100 | [diff] [blame] | 307 | if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "ustring") && |
| 308 | strcmp(cast, "x") != 0 && |
Naohiro Aota | 19f00b0 | 2016-08-09 11:40:08 +0900 | [diff] [blame] | 309 | strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) { |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 310 | /* Non string type is OK */ |
Masami Hiramatsu | 9254378 | 2016-08-18 17:58:47 +0900 | [diff] [blame] | 311 | /* and respect signedness/hexadecimal cast */ |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 312 | tvar->type = strdup(cast); |
| 313 | return (tvar->type == NULL) ? -ENOMEM : 0; |
| 314 | } |
| 315 | |
Masami Hiramatsu | bcfc082 | 2011-06-27 16:27:21 +0900 | [diff] [blame] | 316 | bsize = dwarf_bitsize(vr_die); |
| 317 | if (bsize > 0) { |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 318 | /* This is a bitfield */ |
Masami Hiramatsu | bcfc082 | 2011-06-27 16:27:21 +0900 | [diff] [blame] | 319 | 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 Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 325 | goto formatted; |
| 326 | } |
| 327 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 328 | 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 Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 333 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 334 | pr_debug("%s type is %s.\n", |
| 335 | dwarf_diename(vr_die), dwarf_diename(&type)); |
| 336 | |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 337 | if (cast && (!strcmp(cast, "string") || !strcmp(cast, "ustring"))) { |
| 338 | /* String type */ |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 339 | 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 Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 343 | "%s(%s) is not a pointer nor array.\n", |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 344 | dwarf_diename(vr_die), dwarf_diename(&type)); |
| 345 | return -EINVAL; |
| 346 | } |
Hyeoncheol Lee | 7ce28b5 | 2012-09-11 16:57:28 +0900 | [diff] [blame] | 347 | if (die_get_real_type(&type, &type) == NULL) { |
| 348 | pr_warning("Failed to get a type" |
| 349 | " information.\n"); |
| 350 | return -ENOENT; |
| 351 | } |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 352 | if (ret == DW_TAG_pointer_type) { |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 353 | while (*ref_ptr) |
| 354 | ref_ptr = &(*ref_ptr)->next; |
| 355 | /* Add new reference with offset +0 */ |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 356 | *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 357 | if (*ref_ptr == NULL) { |
| 358 | pr_warning("Out of memory error\n"); |
| 359 | return -ENOMEM; |
| 360 | } |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 361 | (*ref_ptr)->user_access = user_access; |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 362 | } |
Masami Hiramatsu | 8217563 | 2010-07-09 18:29:17 +0900 | [diff] [blame] | 363 | if (!die_compare_name(&type, "char") && |
| 364 | !die_compare_name(&type, "unsigned char")) { |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 365 | pr_warning("Failed to cast into string: " |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 366 | "%s is not (unsigned) char *.\n", |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 367 | dwarf_diename(vr_die)); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | tvar->type = strdup(cast); |
| 371 | return (tvar->type == NULL) ? -ENOMEM : 0; |
| 372 | } |
| 373 | |
Naohiro Aota | 19f00b0 | 2016-08-09 11:40:08 +0900 | [diff] [blame] | 374 | if (cast && (strcmp(cast, "u") == 0)) |
Masami Hiramatsu | 9254378 | 2016-08-18 17:58:47 +0900 | [diff] [blame] | 375 | prefix = 'u'; |
Naohiro Aota | 19f00b0 | 2016-08-09 11:40:08 +0900 | [diff] [blame] | 376 | else if (cast && (strcmp(cast, "s") == 0)) |
Masami Hiramatsu | 9254378 | 2016-08-18 17:58:47 +0900 | [diff] [blame] | 377 | prefix = 's'; |
| 378 | else if (cast && (strcmp(cast, "x") == 0) && |
| 379 | probe_type_is_available(PROBE_TYPE_X)) |
| 380 | prefix = 'x'; |
Naohiro Aota | 19f00b0 | 2016-08-09 11:40:08 +0900 | [diff] [blame] | 381 | else |
Masami Hiramatsu | 9880ce4 | 2016-08-18 17:59:07 +0900 | [diff] [blame] | 382 | prefix = die_is_signed_type(&type) ? 's' : |
| 383 | probe_type_is_available(PROBE_TYPE_X) ? 'x' : 'u'; |
Naohiro Aota | 19f00b0 | 2016-08-09 11:40:08 +0900 | [diff] [blame] | 384 | |
Masami Hiramatsu | bcfc082 | 2011-06-27 16:27:21 +0900 | [diff] [blame] | 385 | ret = dwarf_bytesize(&type); |
| 386 | if (ret <= 0) |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 387 | /* No size ... try to use default type */ |
| 388 | return 0; |
Masami Hiramatsu | bcfc082 | 2011-06-27 16:27:21 +0900 | [diff] [blame] | 389 | ret = BYTES_TO_BITS(ret); |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 390 | |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 391 | /* 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 Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 396 | } |
Masami Hiramatsu | 9254378 | 2016-08-18 17:58:47 +0900 | [diff] [blame] | 397 | ret = snprintf(buf, 16, "%c%d", prefix, ret); |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 398 | |
| 399 | formatted: |
| 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 Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 404 | str_error_r(-ret, sbuf, sizeof(sbuf))); |
Masami Hiramatsu | 124bb83 | 2011-02-04 21:52:11 +0900 | [diff] [blame] | 405 | return ret; |
| 406 | } |
| 407 | tvar->type = strdup(buf); |
| 408 | if (tvar->type == NULL) |
| 409 | return -ENOMEM; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 410 | return 0; |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 413 | static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 414 | struct perf_probe_arg_field *field, |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 415 | struct probe_trace_arg_ref **ref_ptr, |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 416 | Dwarf_Die *die_mem, bool user_access) |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 417 | { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 418 | struct probe_trace_arg_ref *ref = *ref_ptr; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 419 | Dwarf_Die type; |
| 420 | Dwarf_Word offs; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 421 | int ret, tag; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 422 | |
| 423 | pr_debug("converting %s in %s\n", field->name, varname); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 424 | 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 Hiramatsu | d046179 | 2018-03-17 21:52:25 +0900 | [diff] [blame] | 428 | pr_debug2("Var real type: %s (%x)\n", dwarf_diename(&type), |
| 429 | (unsigned)dwarf_dieoffset(&type)); |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 430 | tag = dwarf_tag(&type); |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 431 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 432 | if (field->name[0] == '[' && |
| 433 | (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { |
Masami Hiramatsu | d046179 | 2018-03-17 21:52:25 +0900 | [diff] [blame] | 434 | /* Save original type for next field or type */ |
| 435 | memcpy(die_mem, &type, sizeof(*die_mem)); |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 436 | /* 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 Hiramatsu | d046179 | 2018-03-17 21:52:25 +0900 | [diff] [blame] | 441 | pr_debug2("Array real type: %s (%x)\n", dwarf_diename(&type), |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 442 | (unsigned)dwarf_dieoffset(&type)); |
| 443 | if (tag == DW_TAG_pointer_type) { |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 444 | ref = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 445 | if (ref == NULL) |
| 446 | return -ENOMEM; |
| 447 | if (*ref_ptr) |
| 448 | (*ref_ptr)->next = ref; |
| 449 | else |
| 450 | *ref_ptr = ref; |
| 451 | } |
Masami Hiramatsu | bcfc082 | 2011-06-27 16:27:21 +0900 | [diff] [blame] | 452 | ref->offset += dwarf_bytesize(&type) * field->index; |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 453 | ref->user_access = user_access; |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 454 | goto next; |
| 455 | } else if (tag == DW_TAG_pointer_type) { |
| 456 | /* Check the pointer and dereference */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 457 | if (!field->ref) { |
| 458 | pr_err("Semantic error: %s must be referred by '->'\n", |
| 459 | field->name); |
| 460 | return -EINVAL; |
| 461 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 462 | /* Get the type pointed by this pointer */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 463 | 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 Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 467 | /* Verify it is a data structure */ |
Hyeoncheol Lee | 7b0295b | 2012-09-12 16:57:45 +0900 | [diff] [blame] | 468 | tag = dwarf_tag(&type); |
| 469 | if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { |
Masahiro Yamada | 03440c4 | 2017-02-27 14:28:49 -0800 | [diff] [blame] | 470 | pr_warning("%s is not a data structure nor a union.\n", |
Hyeoncheol Lee | 7b0295b | 2012-09-12 16:57:45 +0900 | [diff] [blame] | 471 | varname); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 472 | return -EINVAL; |
| 473 | } |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 474 | |
Srikar Dronamraju | 0e60836 | 2010-07-29 19:43:51 +0530 | [diff] [blame] | 475 | ref = zalloc(sizeof(struct probe_trace_arg_ref)); |
Masami Hiramatsu | e334016f1 | 2010-04-12 13:17:49 -0400 | [diff] [blame] | 476 | if (ref == NULL) |
| 477 | return -ENOMEM; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 478 | if (*ref_ptr) |
| 479 | (*ref_ptr)->next = ref; |
| 480 | else |
| 481 | *ref_ptr = ref; |
| 482 | } else { |
Masami Hiramatsu | 12e5a7a | 2010-04-02 12:50:53 -0400 | [diff] [blame] | 483 | /* Verify it is a data structure */ |
Hyeoncheol Lee | 7b0295b | 2012-09-12 16:57:45 +0900 | [diff] [blame] | 484 | if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { |
Masahiro Yamada | 03440c4 | 2017-02-27 14:28:49 -0800 | [diff] [blame] | 485 | pr_warning("%s is not a data structure nor a union.\n", |
Hyeoncheol Lee | 7b0295b | 2012-09-12 16:57:45 +0900 | [diff] [blame] | 486 | varname); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 487 | return -EINVAL; |
| 488 | } |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 489 | if (field->name[0] == '[') { |
Masanari Iida | d939be3 | 2015-02-27 23:52:31 +0900 | [diff] [blame] | 490 | pr_err("Semantic error: %s is not a pointer" |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 491 | " nor array.\n", varname); |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 492 | return -EINVAL; |
| 493 | } |
Masami Hiramatsu | c727383 | 2015-04-02 16:33:12 +0900 | [diff] [blame] | 494 | /* While prcessing unnamed field, we don't care about this */ |
| 495 | if (field->ref && dwarf_diename(vr_die)) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 496 | 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 Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 507 | if (die_find_member(&type, field->name, die_mem) == NULL) { |
Arnaldo Carvalho de Melo | 9ef0438 | 2013-10-24 17:36:31 -0300 | [diff] [blame] | 508 | pr_warning("%s(type:%s) has no member %s.\n", varname, |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 509 | dwarf_diename(&type), field->name); |
| 510 | return -EINVAL; |
| 511 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 512 | |
| 513 | /* Get the offset of the field */ |
Hyeoncheol Lee | 7b0295b | 2012-09-12 16:57:45 +0900 | [diff] [blame] | 514 | 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 Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 523 | } |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 524 | ref->offset += (long)offs; |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 525 | ref->user_access = user_access; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 526 | |
Masami Hiramatsu | c727383 | 2015-04-02 16:33:12 +0900 | [diff] [blame] | 527 | /* 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 Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 530 | &ref, die_mem, user_access); |
Masami Hiramatsu | c727383 | 2015-04-02 16:33:12 +0900 | [diff] [blame] | 531 | |
Masami Hiramatsu | b2a3c12 | 2010-05-19 15:57:42 -0400 | [diff] [blame] | 532 | next: |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 533 | /* Converting next field */ |
| 534 | if (field->next) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 535 | return convert_variable_fields(die_mem, field->name, |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 536 | field->next, &ref, die_mem, user_access); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 537 | else |
| 538 | return 0; |
Masami Hiramatsu | 7df2f32 | 2010-03-16 18:06:26 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 541 | static 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 Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 549 | /* Show a variables in kprobe event format */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 550 | static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 551 | { |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 552 | Dwarf_Die die_mem; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 553 | int ret; |
| 554 | |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 555 | pr_debug("Converting variable %s into trace event.\n", |
| 556 | dwarf_diename(vr_die)); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 557 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 558 | ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops, |
Masami Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 559 | &pf->sp_die, pf->machine, pf->tvar); |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 560 | if (ret == -ENOENT && pf->skip_empty_arg) |
| 561 | /* This can be found in other place. skip it */ |
| 562 | return 0; |
He Kuang | 7d5eaba | 2015-05-11 09:25:04 +0000 | [diff] [blame] | 563 | if (ret == -ENOENT || ret == -EINVAL) { |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 564 | print_var_not_found(pf->pvar->var); |
He Kuang | 7d5eaba | 2015-05-11 09:25:04 +0000 | [diff] [blame] | 565 | } else if (ret == -ENOTSUP) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 566 | pr_err("Sorry, we don't support this variable location yet.\n"); |
Masami Hiramatsu | 0c188a0 | 2014-05-29 19:52:32 +0900 | [diff] [blame] | 567 | else if (ret == 0 && pf->pvar->field) { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 568 | ret = convert_variable_fields(vr_die, pf->pvar->var, |
| 569 | pf->pvar->field, &pf->tvar->ref, |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 570 | &die_mem, pf->pvar->user_access); |
Masami Hiramatsu | 4984912 | 2010-04-12 13:17:15 -0400 | [diff] [blame] | 571 | vr_die = &die_mem; |
| 572 | } |
Masami Hiramatsu | 73317b9 | 2010-05-19 15:57:35 -0400 | [diff] [blame] | 573 | if (ret == 0) |
Masami Hiramatsu | 1e032f7 | 2019-05-15 14:39:05 +0900 | [diff] [blame] | 574 | ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type, |
| 575 | pf->pvar->user_access); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 576 | /* *expr will be cached in libdw. Don't free it. */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 577 | return ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 580 | /* Find a variable in a scope DIE */ |
| 581 | static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 582 | { |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 583 | Dwarf_Die vr_die; |
Masami Hiramatsu | 909b036 | 2016-04-28 03:37:14 +0900 | [diff] [blame] | 584 | char *buf, *ptr; |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 585 | int ret = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 586 | |
Wang Nan | da15bd9 | 2015-08-26 10:57:45 +0000 | [diff] [blame] | 587 | /* Copy raw parameters */ |
| 588 | if (!is_c_varname(pf->pvar->var)) |
| 589 | return copy_to_probe_trace_arg(pf->tvar, pf->pvar); |
Masami Hiramatsu | 367e94c | 2010-08-27 20:38:59 +0900 | [diff] [blame] | 590 | |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 591 | if (pf->pvar->name) |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 592 | pf->tvar->name = strdup(pf->pvar->name); |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 593 | else { |
Masami Hiramatsu | 909b036 | 2016-04-28 03:37:14 +0900 | [diff] [blame] | 594 | buf = synthesize_perf_probe_arg(pf->pvar); |
| 595 | if (!buf) |
| 596 | return -ENOMEM; |
Masami Hiramatsu | 11a1ca3 | 2010-04-12 13:17:22 -0400 | [diff] [blame] | 597 | ptr = strchr(buf, ':'); /* Change type separator to _ */ |
| 598 | if (ptr) |
| 599 | *ptr = '_'; |
Masami Hiramatsu | 909b036 | 2016-04-28 03:37:14 +0900 | [diff] [blame] | 600 | pf->tvar->name = buf; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 601 | } |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 602 | if (pf->tvar->name == NULL) |
| 603 | return -ENOMEM; |
Masami Hiramatsu | 4848193 | 2010-04-12 13:16:53 -0400 | [diff] [blame] | 604 | |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 605 | pr_debug("Searching '%s' variable in context.\n", pf->pvar->var); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 606 | /* Search child die for local variables and parameters. */ |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 607 | if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) { |
| 608 | /* Search again in global variables */ |
He Kuang | d13855e | 2015-04-25 16:08:58 +0800 | [diff] [blame] | 609 | if (!die_find_variable_at(&pf->cu_die, pf->pvar->var, |
| 610 | 0, &vr_die)) { |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 611 | if (pf->skip_empty_arg) |
| 612 | return 0; |
Masami Hiramatsu | 36d789a | 2014-06-06 07:13:45 +0000 | [diff] [blame] | 613 | pr_warning("Failed to find '%s' in this function.\n", |
| 614 | pf->pvar->var); |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 615 | ret = -ENOENT; |
He Kuang | d13855e | 2015-04-25 16:08:58 +0800 | [diff] [blame] | 616 | } |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 617 | } |
Masami Hiramatsu | f66fedc | 2011-08-20 14:39:23 +0900 | [diff] [blame] | 618 | if (ret >= 0) |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 619 | ret = convert_variable(&vr_die, pf); |
| 620 | |
Masami Hiramatsu | b7dcb85 | 2010-05-19 15:57:49 -0400 | [diff] [blame] | 621 | return ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 624 | /* Convert subprogram DIE to trace point */ |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 625 | static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod, |
| 626 | Dwarf_Addr paddr, bool retprobe, |
Masami Hiramatsu | 6cca13b | 2015-10-01 01:41:37 +0900 | [diff] [blame] | 627 | const char *function, |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 628 | struct probe_trace_point *tp) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 629 | { |
Masami Hiramatsu | 07d3698 | 2019-10-25 17:46:25 +0900 | [diff] [blame] | 630 | Dwarf_Addr eaddr; |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 631 | GElf_Sym sym; |
| 632 | const char *symbol; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 633 | |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 634 | /* Verify the address is correct */ |
Masami Hiramatsu | 07d3698 | 2019-10-25 17:46:25 +0900 | [diff] [blame] | 635 | if (!dwarf_haspc(sp_die, paddr)) { |
| 636 | pr_warning("Specified offset is out of %s\n", |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 637 | dwarf_diename(sp_die)); |
| 638 | return -EINVAL; |
| 639 | } |
| 640 | |
Masami Hiramatsu | 1efde27 | 2020-02-28 00:42:01 +0900 | [diff] [blame] | 641 | 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 Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 649 | if (!symbol) { |
Masami Hiramatsu | 07d3698 | 2019-10-25 17:46:25 +0900 | [diff] [blame] | 650 | pr_warning("Failed to find symbol at 0x%lx\n", |
| 651 | (unsigned long)paddr); |
| 652 | return -ENOENT; |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 653 | } |
Masami Hiramatsu | 07d3698 | 2019-10-25 17:46:25 +0900 | [diff] [blame] | 654 | |
Masami Hiramatsu | 664fee3 | 2014-09-17 08:41:01 +0000 | [diff] [blame] | 655 | tp->offset = (unsigned long)(paddr - eaddr); |
Masami Hiramatsu | fb7345b | 2013-12-26 05:41:53 +0000 | [diff] [blame] | 656 | tp->address = (unsigned long)paddr; |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 657 | tp->symbol = strdup(symbol); |
| 658 | if (!tp->symbol) |
| 659 | return -ENOMEM; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 660 | |
Masami Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 661 | /* Return probe must be on the head of a subprogram */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 662 | if (retprobe) { |
| 663 | if (eaddr != paddr) { |
Masami Hiramatsu | 6cca13b | 2015-10-01 01:41:37 +0900 | [diff] [blame] | 664 | 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 Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 668 | return -EINVAL; |
| 669 | } |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 670 | tp->retprobe = true; |
Masami Hiramatsu | 04ddd04 | 2010-08-27 20:38:53 +0900 | [diff] [blame] | 671 | } |
| 672 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 676 | /* Call probe_finder callback with scope DIE */ |
| 677 | static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 678 | { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 679 | Dwarf_Attribute fb_attr; |
Masami Hiramatsu | 4d3b162 | 2015-11-25 19:34:32 +0900 | [diff] [blame] | 680 | Dwarf_Frame *frame = NULL; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 681 | size_t nops; |
| 682 | int ret; |
| 683 | |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 684 | 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 Hiramatsu | 0dbb1ca | 2012-04-23 12:24:36 +0900 | [diff] [blame] | 690 | if (!die_is_func_def(sc_die)) { |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 691 | if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) { |
Naveen N. Rao | d4c537e | 2015-04-30 17:12:31 +0530 | [diff] [blame] | 692 | 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 701 | } |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 702 | } else |
| 703 | memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die)); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 704 | |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 705 | /* Get the frame base attribute/ops from subprogram */ |
| 706 | dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr); |
Masami Hiramatsu | d0cb426 | 2010-03-15 13:02:35 -0400 | [diff] [blame] | 707 | ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 708 | if (ret <= 0 || nops == 0) { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 709 | pf->fb_ops = NULL; |
Masami Hiramatsu | 7752f1b | 2010-05-10 13:12:07 -0400 | [diff] [blame] | 710 | #if _ELFUTILS_PREREQ(0, 142) |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 711 | } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && |
Hemant Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 712 | (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 Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 715 | dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 716 | pr_warning("Failed to get call frame on 0x%jx\n", |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 717 | (uintmax_t)pf->addr); |
Masami Hiramatsu | 4d3b162 | 2015-11-25 19:34:32 +0900 | [diff] [blame] | 718 | free(frame); |
| 719 | return -ENOENT; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 720 | } |
Masami Hiramatsu | 7752f1b | 2010-05-10 13:12:07 -0400 | [diff] [blame] | 721 | #endif |
Masami Hiramatsu | a34a985 | 2010-04-12 13:17:29 -0400 | [diff] [blame] | 722 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 723 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 724 | /* Call finder's callback handler */ |
Masami Hiramatsu | 4d3b162 | 2015-11-25 19:34:32 +0900 | [diff] [blame] | 725 | ret = pf->callback(sc_die, pf); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 726 | |
Masami Hiramatsu | 4d3b162 | 2015-11-25 19:34:32 +0900 | [diff] [blame] | 727 | /* Since *pf->fb_ops can be a part of frame. we should free it here. */ |
| 728 | free(frame); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 729 | pf->fb_ops = NULL; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 730 | |
| 731 | return ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 732 | } |
| 733 | |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 734 | struct 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 | |
| 743 | static 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 Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 757 | if (die_match_name(fn_die, fsp->function)) { |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 758 | 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 Hiramatsu | c701636 | 2019-11-05 09:16:49 +0900 | [diff] [blame] | 775 | /* Return innermost DIE */ |
| 776 | static 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 Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 785 | /* Find an appropriate scope fits to given conditions */ |
| 786 | static 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 Hiramatsu | c701636 | 2019-11-05 09:16:49 +0900 | [diff] [blame] | 796 | int ret; |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 797 | |
Masami Hiramatsu | c701636 | 2019-11-05 09:16:49 +0900 | [diff] [blame] | 798 | 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 Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 803 | |
| 804 | return fsp.found ? die_mem : NULL; |
| 805 | } |
| 806 | |
Masami Hiramatsu | 1ae5d88 | 2019-11-18 17:12:00 +0900 | [diff] [blame] | 807 | static 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 King | 358f98e | 2019-11-21 09:26:23 +0000 | [diff] [blame] | 822 | pr_warning("This line is sharing the address with other lines.\n"); |
Masami Hiramatsu | 1ae5d88 | 2019-11-18 17:12:00 +0900 | [diff] [blame] | 823 | |
| 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 Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 840 | static int probe_point_line_walker(const char *fname, int lineno, |
| 841 | Dwarf_Addr addr, void *data) |
| 842 | { |
| 843 | struct probe_finder *pf = data; |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 844 | Dwarf_Die *sc_die, die_mem; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 845 | int ret; |
| 846 | |
| 847 | if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0) |
| 848 | return 0; |
| 849 | |
Masami Hiramatsu | 1ae5d88 | 2019-11-18 17:12:00 +0900 | [diff] [blame] | 850 | if (verify_representive_line(pf, fname, lineno, addr)) |
| 851 | return -ENOENT; |
| 852 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 853 | pf->addr = addr; |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 854 | 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 Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 861 | |
| 862 | /* Continue if no error, because the line will be in inline function */ |
Arnaldo Carvalho de Melo | fbee632 | 2011-02-21 13:23:57 -0300 | [diff] [blame] | 863 | return ret < 0 ? ret : 0; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 864 | } |
| 865 | |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 866 | /* Find probe point from its line number */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 867 | static int find_probe_point_by_line(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 868 | { |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 869 | return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf); |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 870 | } |
| 871 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 872 | /* Find lines which match lazy pattern */ |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 873 | static int find_lazy_match_lines(struct intlist *list, |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 874 | const char *fname, const char *pat) |
| 875 | { |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 876 | FILE *fp; |
| 877 | char *line = NULL; |
| 878 | size_t line_len; |
| 879 | ssize_t len; |
| 880 | int count = 0, linenum = 1; |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 881 | char sbuf[STRERR_BUFSIZE]; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 882 | |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 883 | fp = fopen(fname, "r"); |
| 884 | if (!fp) { |
Masami Hiramatsu | 5f03cba | 2014-08-14 02:22:34 +0000 | [diff] [blame] | 885 | pr_warning("Failed to open %s: %s\n", fname, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 886 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | b448c4b | 2010-05-18 23:04:28 -0300 | [diff] [blame] | 887 | return -errno; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 888 | } |
| 889 | |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 890 | 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 Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 896 | intlist__add(list, linenum); |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 897 | count++; |
| 898 | } |
| 899 | linenum++; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 900 | } |
Arnaldo Carvalho de Melo | b448c4b | 2010-05-18 23:04:28 -0300 | [diff] [blame] | 901 | |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 902 | 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 Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 910 | } |
| 911 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 912 | static int probe_point_lazy_walker(const char *fname, int lineno, |
| 913 | Dwarf_Addr addr, void *data) |
| 914 | { |
| 915 | struct probe_finder *pf = data; |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 916 | Dwarf_Die *sc_die, die_mem; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 917 | int ret; |
| 918 | |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 919 | if (!intlist__has_entry(pf->lcache, lineno) || |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 920 | 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 Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 926 | 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 Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 934 | |
| 935 | /* |
| 936 | * Continue if no error, because the lazy pattern will match |
| 937 | * to other lines |
| 938 | */ |
Ingo Molnar | 5e814dd | 2011-03-15 20:51:09 +0100 | [diff] [blame] | 939 | return ret < 0 ? ret : 0; |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 940 | } |
| 941 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 942 | /* Find probe points from lazy pattern */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 943 | static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 944 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 945 | int ret = 0; |
Naohiro Aota | 09ed897 | 2015-03-13 14:18:40 +0900 | [diff] [blame] | 946 | char *fpath; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 947 | |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 948 | if (intlist__empty(pf->lcache)) { |
Naohiro Aota | 09ed897 | 2015-03-13 14:18:40 +0900 | [diff] [blame] | 949 | 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 Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 958 | /* Matching lazy line pattern */ |
Naohiro Aota | 09ed897 | 2015-03-13 14:18:40 +0900 | [diff] [blame] | 959 | ret = find_lazy_match_lines(pf->lcache, fpath, |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 960 | pf->pev->point.lazy_line); |
Naohiro Aota | 09ed897 | 2015-03-13 14:18:40 +0900 | [diff] [blame] | 961 | free(fpath); |
Franck Bui-Huu | f50c216 | 2011-01-13 11:18:30 +0100 | [diff] [blame] | 962 | if (ret <= 0) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 963 | return ret; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 964 | } |
| 965 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 966 | return die_walk_lines(sp_die, probe_point_lazy_walker, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 967 | } |
| 968 | |
Ravi Bangoria | e47392b | 2016-08-03 14:28:45 +0530 | [diff] [blame] | 969 | static 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 Bangoria | 6243b9d | 2016-08-30 14:09:37 +0530 | [diff] [blame] | 978 | if (die_is_optimized_target(&pf->cu_die)) |
Ravi Bangoria | e47392b | 2016-08-03 14:28:45 +0530 | [diff] [blame] | 979 | 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 Bangoria | 6243b9d | 2016-08-30 14:09:37 +0530 | [diff] [blame] | 998 | die_skip_prologue(sp_die, &pf->cu_die, &pf->addr); |
Ravi Bangoria | e47392b | 2016-08-03 14:28:45 +0530 | [diff] [blame] | 999 | } |
| 1000 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1001 | static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1002 | { |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1003 | struct probe_finder *pf = data; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1004 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1005 | Dwarf_Addr addr; |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1006 | int ret; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1007 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1008 | if (pp->lazy_line) |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1009 | ret = find_probe_point_lazy(in_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1010 | else { |
| 1011 | /* Get probe address */ |
Masami Hiramatsu | eb6933b | 2019-10-25 17:46:43 +0900 | [diff] [blame] | 1012 | if (die_entrypc(in_die, &addr) != 0) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1013 | pr_warning("Failed to get entry address of %s.\n", |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1014 | dwarf_diename(in_die)); |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1015 | return -ENOENT; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1016 | } |
Masami Hiramatsu | 0ad45b3 | 2016-09-24 00:35:07 +0900 | [diff] [blame] | 1017 | if (addr == 0) { |
| 1018 | pr_debug("%s has no valid entry address. skipped.\n", |
| 1019 | dwarf_diename(in_die)); |
| 1020 | return -ENOENT; |
| 1021 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1022 | pf->addr = addr; |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1023 | pf->addr += pp->offset; |
| 1024 | pr_debug("found inline addr: 0x%jx\n", |
| 1025 | (uintmax_t)pf->addr); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1026 | |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1027 | ret = call_probe_finder(in_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1028 | } |
| 1029 | |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1030 | return ret; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1031 | } |
| 1032 | |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1033 | /* Callback parameter with return value for libdw */ |
| 1034 | struct dwarf_callback_param { |
| 1035 | void *data; |
| 1036 | int retval; |
| 1037 | }; |
| 1038 | |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1039 | /* Search function from function name */ |
| 1040 | static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) |
| 1041 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1042 | struct dwarf_callback_param *param = data; |
| 1043 | struct probe_finder *pf = param->data; |
Masami Hiramatsu | 4235b04 | 2010-03-16 18:06:12 -0400 | [diff] [blame] | 1044 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1045 | |
| 1046 | /* Check tag and diename */ |
Masami Hiramatsu | 0dbb1ca | 2012-04-23 12:24:36 +0900 | [diff] [blame] | 1047 | if (!die_is_func_def(sp_die) || |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1048 | !die_match_name(sp_die, pp->function)) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1049 | return DWARF_CB_OK; |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1050 | |
Masami Hiramatsu | 7d21635 | 2011-03-30 18:25:41 +0900 | [diff] [blame] | 1051 | /* Check declared file */ |
| 1052 | if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die))) |
| 1053 | return DWARF_CB_OK; |
| 1054 | |
Masami Hiramatsu | f8da4b5 | 2016-09-24 00:34:57 +0900 | [diff] [blame] | 1055 | pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die), |
| 1056 | (unsigned long)dwarf_dieoffset(sp_die)); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1057 | pf->fname = dwarf_decl_file(sp_die); |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1058 | if (pp->line) { /* Function relative line */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1059 | dwarf_decl_line(sp_die, &pf->lno); |
| 1060 | pf->lno += pp->line; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1061 | param->retval = find_probe_point_by_line(pf); |
Masami Hiramatsu | e1ecbbc | 2015-01-30 18:37:44 +0900 | [diff] [blame] | 1062 | } else if (die_is_func_instance(sp_die)) { |
| 1063 | /* Instances always have the entry address */ |
Masami Hiramatsu | 5d16dbc | 2019-10-25 17:46:34 +0900 | [diff] [blame] | 1064 | die_entrypc(sp_die, &pf->addr); |
Masami Hiramatsu | 0ad45b3 | 2016-09-24 00:35:07 +0900 | [diff] [blame] | 1065 | /* 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 Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1070 | /* Real function */ |
Masami Hiramatsu | 0ad45b3 | 2016-09-24 00:35:07 +0900 | [diff] [blame] | 1071 | } else if (pp->lazy_line) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1072 | param->retval = find_probe_point_lazy(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1073 | else { |
Ravi Bangoria | e47392b | 2016-08-03 14:28:45 +0530 | [diff] [blame] | 1074 | skip_prologue(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1075 | pf->addr += pp->offset; |
| 1076 | /* TODO: Check the address in this function */ |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1077 | param->retval = call_probe_finder(sp_die, pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1078 | } |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1079 | } else if (!probe_conf.no_inlines) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1080 | /* Inlined function: search instances */ |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1081 | param->retval = die_walk_instances(sp_die, |
| 1082 | probe_point_inline_cb, (void *)pf); |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1083 | /* This could be a non-existed inline definition */ |
Masami Hiramatsu | f8da4b5 | 2016-09-24 00:34:57 +0900 | [diff] [blame] | 1084 | if (param->retval == -ENOENT) |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1085 | 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 Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1093 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1094 | return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */ |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1095 | } |
| 1096 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1097 | static int find_probe_point_by_func(struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1098 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1099 | 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 Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1103 | } |
| 1104 | |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1105 | struct pubname_callback_param { |
| 1106 | char *function; |
| 1107 | char *file; |
| 1108 | Dwarf_Die *cu_die; |
| 1109 | Dwarf_Die *sp_die; |
| 1110 | int found; |
| 1111 | }; |
| 1112 | |
| 1113 | static 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 Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1121 | if (die_match_name(param->sp_die, param->function)) { |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1122 | 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 Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 1137 | static int debuginfo__find_probe_location(struct debuginfo *dbg, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1138 | struct probe_finder *pf) |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1139 | { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1140 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1141 | Dwarf_Off off, noff; |
| 1142 | size_t cuhl; |
| 1143 | Dwarf_Die *diep; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1144 | int ret = 0; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1145 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1146 | off = 0; |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 1147 | pf->lcache = intlist__new(NULL); |
| 1148 | if (!pf->lcache) |
| 1149 | return -ENOMEM; |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1150 | |
| 1151 | /* Fastpath: lookup by function name from .debug_pubnames section */ |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1152 | if (pp->function && !strisglob(pp->function)) { |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1153 | 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 Ming | 2b348a7 | 2011-04-29 08:41:57 +0000 | [diff] [blame] | 1158 | .found = 0, |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1159 | }; |
| 1160 | struct dwarf_callback_param probe_param = { |
| 1161 | .data = pf, |
| 1162 | }; |
| 1163 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1164 | dwarf_getpubnames(dbg->dbg, pubname_search_cb, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1165 | &pubname_param, 0); |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1166 | 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 Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1173 | /* Loop on CUs (Compilation Unit) */ |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1174 | while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1175 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1176 | diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1177 | if (!diep) |
| 1178 | continue; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1179 | |
| 1180 | /* Check if target file is included. */ |
| 1181 | if (pp->file) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1182 | pf->fname = cu_find_realpath(&pf->cu_die, pp->file); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1183 | else |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1184 | pf->fname = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1185 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1186 | if (!pp->file || pf->fname) { |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1187 | if (pp->function) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1188 | ret = find_probe_point_by_func(pf); |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1189 | else if (pp->lazy_line) |
He Kuang | f19e80c | 2015-04-13 19:41:30 +0800 | [diff] [blame] | 1190 | ret = find_probe_point_lazy(&pf->cu_die, pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 1191 | else { |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1192 | pf->lno = pp->line; |
| 1193 | ret = find_probe_point_by_line(pf); |
Masami Hiramatsu | b0ef073 | 2009-10-27 16:43:19 -0400 | [diff] [blame] | 1194 | } |
Arnaldo Carvalho de Melo | 8635bf6 | 2011-02-22 06:56:18 -0300 | [diff] [blame] | 1195 | if (ret < 0) |
Arnaldo Carvalho de Melo | fbee632 | 2011-02-21 13:23:57 -0300 | [diff] [blame] | 1196 | break; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1197 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1198 | off = noff; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1199 | } |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1200 | |
| 1201 | found: |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 1202 | intlist__delete(pf->lcache); |
| 1203 | pf->lcache = NULL; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1204 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1205 | return ret; |
| 1206 | } |
| 1207 | |
Hemant Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 1208 | /* Find probe points from debuginfo */ |
| 1209 | static int debuginfo__find_probes(struct debuginfo *dbg, |
| 1210 | struct probe_finder *pf) |
| 1211 | { |
| 1212 | int ret = 0; |
Hemant Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 1213 | Elf *elf; |
| 1214 | GElf_Ehdr ehdr; |
Hemant Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 1215 | |
| 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 Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 1227 | pf->machine = ehdr.e_machine; |
Hemant Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 1228 | |
Masami Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 1229 | #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 Kumar | 270bde1 | 2016-02-02 20:56:46 +0530 | [diff] [blame] | 1239 | #endif |
| 1240 | |
| 1241 | ret = debuginfo__find_probe_location(dbg, pf); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1245 | struct local_vars_finder { |
| 1246 | struct probe_finder *pf; |
| 1247 | struct perf_probe_arg *args; |
Masami Hiramatsu | f8bffbf | 2015-05-06 21:46:53 +0900 | [diff] [blame] | 1248 | bool vars; |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1249 | int max_args; |
| 1250 | int nargs; |
| 1251 | int ret; |
| 1252 | }; |
| 1253 | |
| 1254 | /* Collect available variables in this scope */ |
| 1255 | static int copy_variables_cb(Dwarf_Die *die_mem, void *data) |
| 1256 | { |
| 1257 | struct local_vars_finder *vf = data; |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 1258 | struct probe_finder *pf = vf->pf; |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1259 | int tag; |
| 1260 | |
| 1261 | tag = dwarf_tag(die_mem); |
| 1262 | if (tag == DW_TAG_formal_parameter || |
Masami Hiramatsu | f8bffbf | 2015-05-06 21:46:53 +0900 | [diff] [blame] | 1263 | (tag == DW_TAG_variable && vf->vars)) { |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1264 | if (convert_variable_location(die_mem, vf->pf->addr, |
Masami Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 1265 | vf->pf->fb_ops, &pf->sp_die, |
Masami Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 1266 | pf->machine, NULL) == 0) { |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1267 | 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 | |
| 1283 | static 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 Hiramatsu | f8bffbf | 2015-05-06 21:46:53 +0900 | [diff] [blame] | 1289 | struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false, |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1290 | .max_args = MAX_PROBE_ARGS, .ret = 0}; |
| 1291 | |
| 1292 | for (i = 0; i < pf->pev->nargs; i++) { |
| 1293 | /* var never be NULL */ |
Masami Hiramatsu | f8bffbf | 2015-05-06 21:46:53 +0900 | [diff] [blame] | 1294 | 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 Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1297 | /* Copy normal argument */ |
| 1298 | args[n] = pf->pev->args[i]; |
| 1299 | n++; |
Masami Hiramatsu | f8bffbf | 2015-05-06 21:46:53 +0900 | [diff] [blame] | 1300 | continue; |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1301 | } |
Masami Hiramatsu | f8bffbf | 2015-05-06 21:46:53 +0900 | [diff] [blame] | 1302 | 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 Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1311 | } |
| 1312 | return n; |
| 1313 | } |
| 1314 | |
Masami Hiramatsu | 1a375ae | 2019-09-19 12:41:10 +0900 | [diff] [blame] | 1315 | static 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1326 | /* Add a found probe point into trace event list */ |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 1327 | static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1328 | { |
| 1329 | struct trace_event_finder *tf = |
| 1330 | container_of(pf, struct trace_event_finder, pf); |
Masami Hiramatsu | 6cca13b | 2015-10-01 01:41:37 +0900 | [diff] [blame] | 1331 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1332 | struct probe_trace_event *tev; |
Wang Nan | 092b1f0 | 2015-11-13 12:29:11 +0000 | [diff] [blame] | 1333 | struct perf_probe_arg *args = NULL; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1334 | int ret, i; |
| 1335 | |
Masami Hiramatsu | 1a375ae | 2019-09-19 12:41:10 +0900 | [diff] [blame] | 1336 | /* |
| 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1344 | /* 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 Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 1352 | /* Trace point should be converted from subprogram DIE */ |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 1353 | ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr, |
Masami Hiramatsu | 6cca13b | 2015-10-01 01:41:37 +0900 | [diff] [blame] | 1354 | pp->retprobe, pp->function, &tev->point); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1355 | if (ret < 0) |
Wang Nan | 092b1f0 | 2015-11-13 12:29:11 +0000 | [diff] [blame] | 1356 | goto end; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1357 | |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1358 | tev->point.realname = strdup(dwarf_diename(sc_die)); |
Wang Nan | 092b1f0 | 2015-11-13 12:29:11 +0000 | [diff] [blame] | 1359 | if (!tev->point.realname) { |
| 1360 | ret = -ENOMEM; |
| 1361 | goto end; |
| 1362 | } |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1363 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1364 | pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, |
| 1365 | tev->point.offset); |
| 1366 | |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1367 | /* Expand special probe argument if exist */ |
| 1368 | args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS); |
Wang Nan | 092b1f0 | 2015-11-13 12:29:11 +0000 | [diff] [blame] | 1369 | if (args == NULL) { |
| 1370 | ret = -ENOMEM; |
| 1371 | goto end; |
| 1372 | } |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1373 | |
| 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1388 | pf->tvar = &tev->args[i]; |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 1389 | /* Variable should be found from scope DIE */ |
| 1390 | ret = find_variable(sc_die, pf); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1391 | if (ret != 0) |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1392 | break; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1393 | } |
| 1394 | |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1395 | end: |
Wang Nan | 092b1f0 | 2015-11-13 12:29:11 +0000 | [diff] [blame] | 1396 | if (ret) { |
| 1397 | clear_probe_trace_event(tev); |
| 1398 | tf->ntevs--; |
| 1399 | } |
Masami Hiramatsu | 7969ec7 | 2013-10-11 16:10:23 +0900 | [diff] [blame] | 1400 | free(args); |
| 1401 | return ret; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1402 | } |
| 1403 | |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 1404 | static 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 Hiramatsu | 11fd3eb | 2020-07-10 22:11:13 +0900 | [diff] [blame] | 1411 | if (!ntevs) |
| 1412 | return -ENOENT; |
| 1413 | |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 1414 | 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1445 | /* Find probe_trace_events specified by perf_probe_event from debuginfo */ |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1446 | int debuginfo__find_trace_events(struct debuginfo *dbg, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1447 | struct perf_probe_event *pev, |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1448 | struct probe_trace_event **tevs) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1449 | { |
| 1450 | struct trace_event_finder tf = { |
| 1451 | .pf = {.pev = pev, .callback = add_probe_trace_event}, |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1452 | .max_tevs = probe_conf.max_probes, .mod = dbg->mod}; |
Masami Hiramatsu | 0196e78 | 2015-11-13 12:29:10 +0000 | [diff] [blame] | 1453 | int ret, i; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1454 | |
| 1455 | /* Allocate result tevs array */ |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1456 | *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1457 | if (*tevs == NULL) |
| 1458 | return -ENOMEM; |
| 1459 | |
| 1460 | tf.tevs = *tevs; |
| 1461 | tf.ntevs = 0; |
| 1462 | |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 1463 | if (pev->nargs != 0 && immediate_value_is_supported()) |
| 1464 | tf.pf.skip_empty_arg = true; |
| 1465 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1466 | ret = debuginfo__find_probes(dbg, &tf.pf); |
Masami Hiramatsu | cb40273 | 2019-11-18 17:12:49 +0900 | [diff] [blame] | 1467 | if (ret >= 0 && tf.pf.skip_empty_arg) |
| 1468 | ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs); |
| 1469 | |
Masami Hiramatsu | 12d572e | 2020-07-10 22:11:23 +0900 | [diff] [blame] | 1470 | if (ret < 0 || tf.ntevs == 0) { |
Masami Hiramatsu | 0196e78 | 2015-11-13 12:29:10 +0000 | [diff] [blame] | 1471 | for (i = 0; i < tf.ntevs; i++) |
| 1472 | clear_probe_trace_event(&tf.tevs[i]); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1473 | zfree(tevs); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1474 | return ret; |
| 1475 | } |
| 1476 | |
| 1477 | return (ret < 0) ? ret : tf.ntevs; |
| 1478 | } |
| 1479 | |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1480 | /* Collect available variables in this scope */ |
| 1481 | static int collect_variables_cb(Dwarf_Die *die_mem, void *data) |
| 1482 | { |
| 1483 | struct available_var_finder *af = data; |
| 1484 | struct variable_list *vl; |
Masami Hiramatsu | bf4d5f2 | 2016-05-10 14:47:07 +0900 | [diff] [blame] | 1485 | struct strbuf buf = STRBUF_INIT; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1486 | 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 Hiramatsu | 3d918a1 | 2013-10-11 16:10:26 +0900 | [diff] [blame] | 1494 | af->pf.fb_ops, &af->pf.sp_die, |
Masami Hiramatsu | 293d5b4 | 2016-08-26 01:24:57 +0900 | [diff] [blame] | 1495 | af->pf.machine, NULL); |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 1496 | if (ret == 0 || ret == -ERANGE) { |
| 1497 | int ret2; |
| 1498 | bool externs = !af->child; |
He Kuang | fb9596d | 2015-05-11 09:25:02 +0000 | [diff] [blame] | 1499 | |
Masami Hiramatsu | bf4d5f2 | 2016-05-10 14:47:07 +0900 | [diff] [blame] | 1500 | if (strbuf_init(&buf, 64) < 0) |
| 1501 | goto error; |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (probe_conf.show_location_range) { |
Masami Hiramatsu | bf4d5f2 | 2016-05-10 14:47:07 +0900 | [diff] [blame] | 1504 | 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 Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | ret2 = die_get_varname(die_mem, &buf); |
| 1514 | |
| 1515 | if (!ret2 && probe_conf.show_location_range && |
| 1516 | !externs) { |
Masami Hiramatsu | bf4d5f2 | 2016-05-10 14:47:07 +0900 | [diff] [blame] | 1517 | if (strbuf_addch(&buf, '\t') < 0) |
| 1518 | goto error; |
He Kuang | 349e8d2 | 2015-05-11 09:25:03 +0000 | [diff] [blame] | 1519 | 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 Kuang | fb9596d | 2015-05-11 09:25:02 +0000 | [diff] [blame] | 1525 | strlist__add(vl->vars, |
| 1526 | strbuf_detach(&buf, NULL)); |
Masami Hiramatsu | bf4d5f2 | 2016-05-10 14:47:07 +0900 | [diff] [blame] | 1527 | } |
| 1528 | strbuf_release(&buf); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1532 | if (af->child && dwarf_haspc(die_mem, af->pf.addr)) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1533 | return DIE_FIND_CB_CONTINUE; |
| 1534 | else |
| 1535 | return DIE_FIND_CB_SIBLING; |
Masami Hiramatsu | bf4d5f2 | 2016-05-10 14:47:07 +0900 | [diff] [blame] | 1536 | error: |
| 1537 | strbuf_release(&buf); |
| 1538 | pr_debug("Error in strbuf\n"); |
| 1539 | return DIE_FIND_CB_END; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1540 | } |
| 1541 | |
Masami Hiramatsu | dee36a2 | 2019-10-30 16:09:49 +0900 | [diff] [blame] | 1542 | static 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1554 | /* Add a found vars into available variables list */ |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 1555 | static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1556 | { |
| 1557 | struct available_var_finder *af = |
| 1558 | container_of(pf, struct available_var_finder, pf); |
Masami Hiramatsu | 6cca13b | 2015-10-01 01:41:37 +0900 | [diff] [blame] | 1559 | struct perf_probe_point *pp = &pf->pev->point; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1560 | struct variable_list *vl; |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 1561 | Dwarf_Die die_mem; |
| 1562 | int ret; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1563 | |
Masami Hiramatsu | dee36a2 | 2019-10-30 16:09:49 +0900 | [diff] [blame] | 1564 | /* |
| 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 Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1572 | /* 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 Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 1579 | /* Trace point should be converted from subprogram DIE */ |
Masami Hiramatsu | 576b523 | 2013-09-25 22:16:16 +0900 | [diff] [blame] | 1580 | ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr, |
Masami Hiramatsu | 6cca13b | 2015-10-01 01:41:37 +0900 | [diff] [blame] | 1581 | pp->retprobe, pp->function, &vl->point); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1582 | 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 Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 1589 | vl->vars = strlist__new(NULL, NULL); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1590 | if (vl->vars == NULL) |
| 1591 | return -ENOMEM; |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1592 | af->child = true; |
Masami Hiramatsu | 221d061 | 2011-08-11 20:02:59 +0900 | [diff] [blame] | 1593 | die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1594 | |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1595 | /* Find external variables */ |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1596 | if (!probe_conf.show_ext_vars) |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1597 | goto out; |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1598 | /* Don't need to search child DIE for external vars. */ |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1599 | af->child = false; |
Masami Hiramatsu | f182e3e | 2011-08-11 20:03:05 +0900 | [diff] [blame] | 1600 | die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem); |
Masami Hiramatsu | fb8c5a5 | 2010-10-21 19:13:35 +0900 | [diff] [blame] | 1601 | |
| 1602 | out: |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1603 | if (strlist__empty(vl->vars)) { |
| 1604 | strlist__delete(vl->vars); |
| 1605 | vl->vars = NULL; |
| 1606 | } |
| 1607 | |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Masami Hiramatsu | 69e96ea | 2014-06-06 07:13:59 +0000 | [diff] [blame] | 1611 | /* |
| 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 Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1616 | int debuginfo__find_available_vars_at(struct debuginfo *dbg, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1617 | struct perf_probe_event *pev, |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1618 | struct variable_list **vls) |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1619 | { |
| 1620 | struct available_var_finder af = { |
| 1621 | .pf = {.pev = pev, .callback = add_available_vars}, |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1622 | .mod = dbg->mod, |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1623 | .max_vls = probe_conf.max_probes}; |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1624 | int ret; |
| 1625 | |
| 1626 | /* Allocate result vls array */ |
Masami Hiramatsu | ddb2f58 | 2015-05-08 10:03:31 +0900 | [diff] [blame] | 1627 | *vls = zalloc(sizeof(struct variable_list) * af.max_vls); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1628 | if (*vls == NULL) |
| 1629 | return -ENOMEM; |
| 1630 | |
| 1631 | af.vls = *vls; |
| 1632 | af.nvls = 0; |
| 1633 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1634 | ret = debuginfo__find_probes(dbg, &af.pf); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1635 | if (ret < 0) { |
| 1636 | /* Free vlist for error */ |
| 1637 | while (af.nvls--) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 1638 | zfree(&af.vls[af.nvls].point.symbol); |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 1639 | strlist__delete(af.vls[af.nvls].vars); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1640 | } |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1641 | zfree(vls); |
Masami Hiramatsu | cf6eb48 | 2010-10-21 19:13:23 +0900 | [diff] [blame] | 1642 | return ret; |
| 1643 | } |
| 1644 | |
| 1645 | return (ret < 0) ? ret : af.nvls; |
Masami Hiramatsu | 4ea42b1 | 2009-10-08 17:17:38 -0400 | [diff] [blame] | 1646 | } |
| 1647 | |
Masami Hiramatsu | 9b239a1 | 2015-10-01 01:41:33 +0900 | [diff] [blame] | 1648 | /* For the kernel module, we need a special code to get a DIE */ |
Masami Hiramatsu | 613f050 | 2017-01-11 15:01:57 +0900 | [diff] [blame] | 1649 | int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs, |
| 1650 | bool adjust_offset) |
Masami Hiramatsu | 9b239a1 | 2015-10-01 01:41:33 +0900 | [diff] [blame] | 1651 | { |
| 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 Hiramatsu | 613f050 | 2017-01-11 15:01:57 +0900 | [diff] [blame] | 1679 | if (adjust_offset) |
| 1680 | *offs -= shdr->sh_offset; |
Masami Hiramatsu | 9b239a1 | 2015-10-01 01:41:33 +0900 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1686 | /* Reverse search */ |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1687 | int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1688 | struct perf_probe_point *ppt) |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1689 | { |
| 1690 | Dwarf_Die cudie, spdie, indie; |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1691 | Dwarf_Addr _addr = 0, baseaddr = 0; |
| 1692 | const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp; |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1693 | int baseline = 0, lineno = 0, ret = 0; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1694 | |
Masami Hiramatsu | d2d4edb | 2017-01-11 14:59:38 +0900 | [diff] [blame] | 1695 | /* We always need to relocate the address for aranges */ |
Masami Hiramatsu | 613f050 | 2017-01-11 15:01:57 +0900 | [diff] [blame] | 1696 | if (debuginfo__get_text_offset(dbg, &baseaddr, false) == 0) |
Masami Hiramatsu | d2d4edb | 2017-01-11 14:59:38 +0900 | [diff] [blame] | 1697 | addr += baseaddr; |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1698 | /* Find cu die */ |
Masami Hiramatsu | 0104fe6 | 2015-03-02 21:49:46 +0900 | [diff] [blame] | 1699 | if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) { |
Masami Hiramatsu | 0e43e5d | 2010-12-17 22:12:11 +0900 | [diff] [blame] | 1700 | pr_warning("Failed to find debug information for address %lx\n", |
| 1701 | addr); |
Masami Hiramatsu | 75ec5a2 | 2010-04-02 12:50:59 -0400 | [diff] [blame] | 1702 | ret = -EINVAL; |
| 1703 | goto end; |
| 1704 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1705 | |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1706 | /* 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 Hiramatsu | e0d153c | 2011-06-27 16:27:27 +0900 | [diff] [blame] | 1711 | if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) { |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1712 | /* Get function entry information */ |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1713 | func = basefunc = dwarf_diename(&spdie); |
| 1714 | if (!func || |
Masami Hiramatsu | 3895534 | 2019-10-25 17:46:52 +0900 | [diff] [blame] | 1715 | die_entrypc(&spdie, &baseaddr) != 0 || |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1716 | dwarf_decl_line(&spdie, &baseline) != 0) { |
| 1717 | lineno = 0; |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1718 | goto post; |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1719 | } |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1720 | |
Masami Hiramatsu | 1b286bd | 2013-10-11 12:23:17 +0000 | [diff] [blame] | 1721 | fname = dwarf_decl_file(&spdie); |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1722 | if (addr == (unsigned long)baseaddr) { |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1723 | /* Function entry - Relative line number is 0 */ |
| 1724 | lineno = baseline; |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1725 | 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 Hiramatsu | 3895534 | 2019-10-25 17:46:52 +0900 | [diff] [blame] | 1732 | if (die_entrypc(&indie, &_addr) == 0 && |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1733 | _addr == addr) { |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1734 | /* |
| 1735 | * addr is at an inline function entry. |
| 1736 | * In this case, lineno should be the call-site |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1737 | * line number. (overwrite lineinfo) |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1738 | */ |
| 1739 | lineno = die_get_call_lineno(&indie); |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1740 | fname = die_get_call_file(&indie); |
| 1741 | break; |
| 1742 | } else { |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1743 | /* |
| 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 Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1750 | if (!tmp || |
| 1751 | dwarf_decl_line(&indie, &baseline) != 0) |
| 1752 | break; |
| 1753 | func = tmp; |
| 1754 | spdie = indie; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1755 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1756 | } |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1757 | /* 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 Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1761 | } |
| 1762 | |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1763 | post: |
| 1764 | /* Make a relative line number or an offset */ |
| 1765 | if (lineno) |
| 1766 | ppt->line = lineno - baseline; |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1767 | else if (basefunc) { |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1768 | ppt->offset = addr - (unsigned long)baseaddr; |
Masami Hiramatsu | e08cfd4 | 2013-09-30 18:21:44 +0900 | [diff] [blame] | 1769 | func = basefunc; |
| 1770 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1771 | |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1772 | /* Duplicate strings */ |
| 1773 | if (func) { |
| 1774 | ppt->function = strdup(func); |
Masami Hiramatsu | 02b95da | 2010-04-12 13:17:56 -0400 | [diff] [blame] | 1775 | if (ppt->function == NULL) { |
| 1776 | ret = -ENOMEM; |
| 1777 | goto end; |
| 1778 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1779 | } |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1780 | if (fname) { |
| 1781 | ppt->file = strdup(fname); |
| 1782 | if (ppt->file == NULL) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1783 | zfree(&ppt->function); |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1784 | ret = -ENOMEM; |
| 1785 | goto end; |
| 1786 | } |
| 1787 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1788 | end: |
Masami Hiramatsu | 1d46ea2 | 2011-03-30 18:26:05 +0900 | [diff] [blame] | 1789 | if (ret == 0 && (fname || func)) |
| 1790 | ret = 1; /* Found a point */ |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1791 | return ret; |
| 1792 | } |
| 1793 | |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1794 | /* Add a line and store the src path */ |
| 1795 | static int line_range_add_line(const char *src, unsigned int lineno, |
| 1796 | struct line_range *lr) |
| 1797 | { |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 1798 | /* Copy source path */ |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1799 | if (!lr->path) { |
Masami Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 1800 | lr->path = strdup(src); |
| 1801 | if (lr->path == NULL) |
| 1802 | return -ENOMEM; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1803 | } |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 1804 | return intlist__add(lr->line_list, lineno); |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1805 | } |
| 1806 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1807 | static int line_range_walk_cb(const char *fname, int lineno, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1808 | Dwarf_Addr addr __maybe_unused, |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1809 | void *data) |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1810 | { |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1811 | struct line_finder *lf = data; |
Masami Hiramatsu | 499144c | 2019-11-18 17:12:10 +0900 | [diff] [blame] | 1812 | const char *__fname; |
| 1813 | int __lineno; |
Namhyung Kim | 202c7c1 | 2014-04-01 13:47:57 +0900 | [diff] [blame] | 1814 | int err; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1815 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1816 | if ((strtailcmp(fname, lf->fname) != 0) || |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1817 | (lf->lno_s > lineno || lf->lno_e < lineno)) |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1818 | return 0; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1819 | |
Masami Hiramatsu | 499144c | 2019-11-18 17:12:10 +0900 | [diff] [blame] | 1820 | /* 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 Kim | 202c7c1 | 2014-04-01 13:47:57 +0900 | [diff] [blame] | 1825 | err = line_range_add_line(fname, lineno, lf->lr); |
| 1826 | if (err < 0 && err != -EEXIST) |
| 1827 | return err; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1828 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1829 | return 0; |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1830 | } |
Masami Hiramatsu | fb1587d | 2010-03-16 18:06:19 -0400 | [diff] [blame] | 1831 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1832 | /* Find line range from its line number */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1833 | static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1834 | { |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1835 | int ret; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1836 | |
Masami Hiramatsu | 4cc9cec | 2011-01-13 21:45:58 +0900 | [diff] [blame] | 1837 | ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf); |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1838 | |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1839 | /* Update status */ |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1840 | if (ret >= 0) |
Masami Hiramatsu | 5a62257 | 2014-02-06 05:32:09 +0000 | [diff] [blame] | 1841 | if (!intlist__empty(lf->lr->line_list)) |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1842 | ret = lf->found = 1; |
| 1843 | else |
| 1844 | ret = 0; /* Lines are not found */ |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1845 | else { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1846 | zfree(&lf->lr->path); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1847 | } |
Masami Hiramatsu | f6c903f | 2010-04-14 18:40:07 -0400 | [diff] [blame] | 1848 | return ret; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1849 | } |
| 1850 | |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 1851 | static int line_range_inline_cb(Dwarf_Die *in_die, void *data) |
| 1852 | { |
Masami Hiramatsu | 182c228 | 2014-04-02 14:48:31 +0900 | [diff] [blame] | 1853 | int ret = find_line_range_by_line(in_die, data); |
Masami Hiramatsu | 36c0c58 | 2011-08-11 20:02:47 +0900 | [diff] [blame] | 1854 | |
| 1855 | /* |
| 1856 | * We have to check all instances of inlined function, because |
| 1857 | * some execution paths can be optimized out depends on the |
Masami Hiramatsu | 182c228 | 2014-04-02 14:48:31 +0900 | [diff] [blame] | 1858 | * function argument of instances. However, if an error occurs, |
| 1859 | * it should be handled by the caller. |
Masami Hiramatsu | 36c0c58 | 2011-08-11 20:02:47 +0900 | [diff] [blame] | 1860 | */ |
Masami Hiramatsu | 182c228 | 2014-04-02 14:48:31 +0900 | [diff] [blame] | 1861 | return ret < 0 ? ret : 0; |
Masami Hiramatsu | 161a26b | 2010-02-25 08:35:57 -0500 | [diff] [blame] | 1862 | } |
| 1863 | |
Masami Hiramatsu | 0dbb1ca | 2012-04-23 12:24:36 +0900 | [diff] [blame] | 1864 | /* Search function definition from function name */ |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1865 | static int line_range_search_cb(Dwarf_Die *sp_die, void *data) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1866 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1867 | struct dwarf_callback_param *param = data; |
| 1868 | struct line_finder *lf = param->data; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1869 | struct line_range *lr = lf->lr; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1870 | |
Masami Hiramatsu | 7d21635 | 2011-03-30 18:25:41 +0900 | [diff] [blame] | 1871 | /* Check declared file */ |
| 1872 | if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die))) |
| 1873 | return DWARF_CB_OK; |
| 1874 | |
Masami Hiramatsu | 0dbb1ca | 2012-04-23 12:24:36 +0900 | [diff] [blame] | 1875 | if (die_is_func_def(sp_die) && |
Masami Hiramatsu | 4c85935 | 2015-05-08 10:03:35 +0900 | [diff] [blame] | 1876 | die_match_name(sp_die, lr->function)) { |
Masami Hiramatsu | e92b85e | 2010-02-25 08:35:50 -0500 | [diff] [blame] | 1877 | lf->fname = dwarf_decl_file(sp_die); |
| 1878 | dwarf_decl_line(sp_die, &lr->offset); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1879 | pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1880 | lf->lno_s = lr->offset + lr->start; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1881 | 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 Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1885 | lf->lno_e = INT_MAX; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1886 | pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1887 | lr->start = lf->lno_s; |
| 1888 | lr->end = lf->lno_e; |
Masami Hiramatsu | e1ecbbc | 2015-01-30 18:37:44 +0900 | [diff] [blame] | 1889 | if (!die_is_func_instance(sp_die)) |
Masami Hiramatsu | db0d2c6 | 2011-08-11 20:03:11 +0900 | [diff] [blame] | 1890 | param->retval = die_walk_instances(sp_die, |
| 1891 | line_range_inline_cb, lf); |
| 1892 | else |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1893 | param->retval = find_line_range_by_line(sp_die, lf); |
| 1894 | return DWARF_CB_ABORT; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1895 | } |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1896 | return DWARF_CB_OK; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1897 | } |
| 1898 | |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1899 | static int find_line_range_by_func(struct line_finder *lf) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1900 | { |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1901 | struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0}; |
| 1902 | dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0); |
| 1903 | return param.retval; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1904 | } |
| 1905 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1906 | int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1907 | { |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1908 | struct line_finder lf = {.lr = lr, .found = 0}; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1909 | int ret = 0; |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1910 | Dwarf_Off off = 0, noff; |
| 1911 | size_t cuhl; |
| 1912 | Dwarf_Die *diep; |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 1913 | const char *comp_dir; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1914 | |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1915 | /* 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 Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1923 | dwarf_getpubnames(dbg->dbg, pubname_search_cb, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1924 | &pubname_param, 0); |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1925 | 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 Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1932 | /* Loop on CUs (Compilation Unit) */ |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1933 | while (!lf.found && ret >= 0) { |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1934 | if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, |
Masami Hiramatsu | ff74178 | 2011-06-27 16:27:39 +0900 | [diff] [blame] | 1935 | NULL, NULL, NULL) != 0) |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1936 | break; |
| 1937 | |
| 1938 | /* Get the DIE(Debugging Information Entry) of this CU */ |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1939 | diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1940 | if (!diep) |
| 1941 | continue; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1942 | |
| 1943 | /* Check if target file is included. */ |
| 1944 | if (lr->file) |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1945 | lf.fname = cu_find_realpath(&lf.cu_die, lr->file); |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1946 | else |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1947 | lf.fname = 0; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1948 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 1949 | if (!lr->file || lf.fname) { |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1950 | if (lr->function) |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1951 | ret = find_line_range_by_func(&lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1952 | else { |
| 1953 | lf.lno_s = lr->start; |
Masami Hiramatsu | d3b63d7 | 2010-04-14 18:39:42 -0400 | [diff] [blame] | 1954 | lf.lno_e = lr->end; |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1955 | ret = find_line_range_by_line(NULL, &lf); |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1956 | } |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1957 | } |
Masami Hiramatsu | 804b360 | 2010-02-25 08:35:42 -0500 | [diff] [blame] | 1958 | off = noff; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1959 | } |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 1960 | |
Lin Ming | cd25f8b | 2011-03-25 16:27:48 +0800 | [diff] [blame] | 1961 | found: |
Masami Hiramatsu | 6a330a3 | 2010-07-09 18:29:11 +0900 | [diff] [blame] | 1962 | /* 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 Hiramatsu | 7cf0b79 | 2010-07-09 18:28:59 +0900 | [diff] [blame] | 1972 | pr_debug("path: %s\n", lr->path); |
Masami Hiramatsu | b55a87a | 2010-04-12 13:17:35 -0400 | [diff] [blame] | 1973 | return (ret < 0) ? ret : lf.found; |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 1974 | } |
| 1975 | |
Naohiro Aota | 09ed897 | 2015-03-13 14:18:40 +0900 | [diff] [blame] | 1976 | /* |
| 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 | */ |
| 1982 | int 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 | } |