blob: 7ae3dd10f147897f763ef84d233014ab5d2dfdef [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Jiri Olsa4605eab2015-09-02 09:56:43 +020043#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090047#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090054struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090058int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059{
60 int ret;
61 va_list ap;
62 va_start(ap, format);
63 ret = vsnprintf(str, size, format, ap);
64 va_end(ap);
65 if (ret >= (int)size)
66 ret = -E2BIG;
67 return ret;
68}
69
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030070static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000071static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040072
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090073/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090074int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040076 int ret;
77
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040078 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090079 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090080 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 if (ret < 0) {
82 pr_debug("Failed to init symbol map.\n");
83 goto out;
84 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040085
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000086 if (host_machine || user_only) /* already initialized */
87 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030088
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000089 if (symbol_conf.vmlinux_name)
90 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91
92 host_machine = machine__new_host();
93 if (!host_machine) {
94 pr_debug("machine__new_host() failed.\n");
95 symbol__exit();
96 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040098out:
99 if (ret < 0)
100 pr_warning("Failed to init vmlinux path.\n");
101 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400102}
103
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900104void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000105{
106 if (host_machine) {
107 machine__delete(host_machine);
108 host_machine = NULL;
109 }
110 symbol__exit();
111}
112
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900113static struct symbol *__find_kernel_function_by_name(const char *name,
114 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400115{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000116 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900117 NULL);
118}
119
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000120static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
121{
122 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
123}
124
125static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
126{
127 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
128 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300129 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000130
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300131 if (map__load(map, NULL) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000132 return NULL;
133
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300134 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000135 if (!kmap)
136 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000137 return kmap->ref_reloc_sym;
138}
139
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900140static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
141 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000142{
143 struct ref_reloc_sym *reloc_sym;
144 struct symbol *sym;
145 struct map *map;
146
147 /* ref_reloc_sym is just a label. Need a special fix*/
148 reloc_sym = kernel_get_ref_reloc_sym();
149 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900150 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000151 else {
152 sym = __find_kernel_function_by_name(name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900153 if (!sym)
154 return -ENOENT;
155 *addr = map->unmap_ip(map, sym->start) -
156 ((reloc) ? 0 : map->reloc) -
157 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000158 }
159 return 0;
160}
161
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900162static struct map *kernel_get_module_map(const char *module)
163{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000164 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300165 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300166 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900167
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900168 /* A file path -- this is an offline module */
169 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300170 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900171
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900172 if (!module)
173 module = "kernel";
174
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300175 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900176 if (strncmp(pos->dso->short_name + 1, module,
177 pos->dso->short_name_len - 2) == 0) {
178 return pos;
179 }
180 }
181 return NULL;
182}
183
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900184static struct map *get_target_map(const char *target, bool user)
185{
186 /* Init maps of given executable or kernel */
187 if (user)
188 return dso__new_map(target);
189 else
190 return kernel_get_module_map(target);
191}
192
193static void put_target_map(struct map *map, bool user)
194{
195 if (map && user) {
196 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300197 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900198 }
199}
200
201
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000202static int convert_exec_to_group(const char *exec, char **result)
203{
204 char *ptr1, *ptr2, *exec_copy;
205 char buf[64];
206 int ret;
207
208 exec_copy = strdup(exec);
209 if (!exec_copy)
210 return -ENOMEM;
211
212 ptr1 = basename(exec_copy);
213 if (!ptr1) {
214 ret = -EINVAL;
215 goto out;
216 }
217
218 ptr2 = strpbrk(ptr1, "-._");
219 if (ptr2)
220 *ptr2 = '\0';
221 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
222 if (ret < 0)
223 goto out;
224
225 *result = strdup(buf);
226 ret = *result ? 0 : -ENOMEM;
227
228out:
229 free(exec_copy);
230 return ret;
231}
232
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900233static void clear_perf_probe_point(struct perf_probe_point *pp)
234{
235 free(pp->file);
236 free(pp->function);
237 free(pp->lazy_line);
238}
239
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000240static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
241{
242 int i;
243
244 for (i = 0; i < ntevs; i++)
245 clear_probe_trace_event(tevs + i);
246}
247
Masami Hiramatsub0312202015-06-16 20:50:55 +0900248static bool kprobe_blacklist__listed(unsigned long address);
249static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
250{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900251 u64 etext_addr = 0;
252 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000253
Masami Hiramatsub0312202015-06-16 20:50:55 +0900254 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900255 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
256 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000257
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900258 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900259 pr_warning("%s is out of .text, skip it.\n", symbol);
260 else if (kprobe_blacklist__listed(address))
261 pr_warning("%s is blacklisted function, skip it.\n", symbol);
262 else
263 return false;
264
265 return true;
266}
267
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530268/*
269 * NOTE:
270 * '.gnu.linkonce.this_module' section of kernel module elf directly
271 * maps to 'struct module' from linux/module.h. This section contains
272 * actual module name which will be used by kernel after loading it.
273 * But, we cannot use 'struct module' here since linux/module.h is not
274 * exposed to user-space. Offset of 'name' has remained same from long
275 * time, so hardcoding it here.
276 */
277#ifdef __LP64__
278#define MOD_NAME_OFFSET 24
279#else
280#define MOD_NAME_OFFSET 12
281#endif
282
283/*
284 * @module can be module name of module file path. In case of path,
285 * inspect elf and find out what is actual module name.
286 * Caller has to free mod_name after using it.
287 */
288static char *find_module_name(const char *module)
289{
290 int fd;
291 Elf *elf;
292 GElf_Ehdr ehdr;
293 GElf_Shdr shdr;
294 Elf_Data *data;
295 Elf_Scn *sec;
296 char *mod_name = NULL;
297
298 fd = open(module, O_RDONLY);
299 if (fd < 0)
300 return NULL;
301
302 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
303 if (elf == NULL)
304 goto elf_err;
305
306 if (gelf_getehdr(elf, &ehdr) == NULL)
307 goto ret_err;
308
309 sec = elf_section_by_name(elf, &ehdr, &shdr,
310 ".gnu.linkonce.this_module", NULL);
311 if (!sec)
312 goto ret_err;
313
314 data = elf_getdata(sec, NULL);
315 if (!data || !data->d_buf)
316 goto ret_err;
317
318 mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
319
320ret_err:
321 elf_end(elf);
322elf_err:
323 close(fd);
324 return mod_name;
325}
326
Ingo Molnar89fe8082013-09-30 12:07:11 +0200327#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000328
329static int kernel_get_module_dso(const char *module, struct dso **pdso)
330{
331 struct dso *dso;
332 struct map *map;
333 const char *vmlinux_name;
334 int ret = 0;
335
336 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300337 char module_name[128];
338
339 snprintf(module_name, sizeof(module_name), "[%s]", module);
340 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
341 if (map) {
342 dso = map->dso;
343 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000344 }
345 pr_debug("Failed to find module %s.\n", module);
346 return -ENOENT;
347 }
348
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300349 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000350 dso = map->dso;
351
352 vmlinux_name = symbol_conf.vmlinux_name;
353 dso->load_errno = 0;
354 if (vmlinux_name)
355 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
356 else
357 ret = dso__load_vmlinux_path(dso, map, NULL);
358found:
359 *pdso = dso;
360 return ret;
361}
362
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900363/*
364 * Some binaries like glibc have special symbols which are on the symbol
365 * table, but not in the debuginfo. If we can find the address of the
366 * symbol from map, we can translate the address back to the probe point.
367 */
368static int find_alternative_probe_point(struct debuginfo *dinfo,
369 struct perf_probe_point *pp,
370 struct perf_probe_point *result,
371 const char *target, bool uprobes)
372{
373 struct map *map = NULL;
374 struct symbol *sym;
375 u64 address = 0;
376 int ret = -ENOENT;
377
378 /* This can work only for function-name based one */
379 if (!pp->function || pp->file)
380 return -ENOTSUP;
381
382 map = get_target_map(target, uprobes);
383 if (!map)
384 return -EINVAL;
385
386 /* Find the address of given function */
387 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900388 if (uprobes)
389 address = sym->start;
390 else
391 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900392 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900393 }
394 if (!address) {
395 ret = -ENOENT;
396 goto out;
397 }
Wang Nanf6c15622015-04-08 02:14:34 +0000398 pr_debug("Symbol %s address found : %" PRIx64 "\n",
399 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900400
401 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
402 result);
403 if (ret <= 0)
404 ret = (!ret) ? -ENOENT : ret;
405 else {
406 result->offset += pp->offset;
407 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800408 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900409 ret = 0;
410 }
411
412out:
413 put_target_map(map, uprobes);
414 return ret;
415
416}
417
418static int get_alternative_probe_event(struct debuginfo *dinfo,
419 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900420 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900421{
422 int ret;
423
424 memcpy(tmp, &pev->point, sizeof(*tmp));
425 memset(&pev->point, 0, sizeof(pev->point));
426 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900427 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900428 if (ret < 0)
429 memcpy(&pev->point, tmp, sizeof(*tmp));
430
431 return ret;
432}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000433
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900434static int get_alternative_line_range(struct debuginfo *dinfo,
435 struct line_range *lr,
436 const char *target, bool user)
437{
David Ahern6d4a4892015-03-11 10:36:20 -0400438 struct perf_probe_point pp = { .function = lr->function,
439 .file = lr->file,
440 .line = lr->start };
441 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900442 int ret, len = 0;
443
David Ahern6d4a4892015-03-11 10:36:20 -0400444 memset(&result, 0, sizeof(result));
445
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900446 if (lr->end != INT_MAX)
447 len = lr->end - lr->start;
448 ret = find_alternative_probe_point(dinfo, &pp, &result,
449 target, user);
450 if (!ret) {
451 lr->function = result.function;
452 lr->file = result.file;
453 lr->start = result.line;
454 if (lr->end != INT_MAX)
455 lr->end = lr->start + len;
456 clear_perf_probe_point(&pp);
457 }
458 return ret;
459}
460
Masami Hiramatsuff741782011-06-27 16:27:39 +0900461/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000462static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900463{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000464 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900465 char reason[STRERR_BUFSIZE];
466 struct debuginfo *ret = NULL;
467 struct dso *dso = NULL;
468 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900469
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000470 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900471 err = kernel_get_module_dso(module, &dso);
472 if (err < 0) {
473 if (!dso || dso->load_errno == 0) {
474 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
475 strcpy(reason, "(unknown)");
476 } else
477 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000478 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900479 pr_err("Failed to find the path for %s: %s\n",
480 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900481 return NULL;
482 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900483 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900484 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000485 ret = debuginfo__new(path);
486 if (!ret && !silent) {
487 pr_warning("The %s file has no debug information.\n", path);
488 if (!module || !strtailcmp(path, ".ko"))
489 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
490 else
491 pr_warning("Rebuild with -g, ");
492 pr_warning("or install an appropriate debuginfo package.\n");
493 }
494 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400495}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300496
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900497/* For caching the last debuginfo */
498static struct debuginfo *debuginfo_cache;
499static char *debuginfo_cache_path;
500
501static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
502{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900503 const char *path = module;
504
505 /* If the module is NULL, it should be the kernel. */
506 if (!module)
507 path = "kernel";
508
509 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900510 goto out;
511
512 /* Copy module path */
513 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900514 debuginfo_cache_path = strdup(path);
515 if (!debuginfo_cache_path) {
516 debuginfo__delete(debuginfo_cache);
517 debuginfo_cache = NULL;
518 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900519 }
520
521 debuginfo_cache = open_debuginfo(module, silent);
522 if (!debuginfo_cache)
523 zfree(&debuginfo_cache_path);
524out:
525 return debuginfo_cache;
526}
527
528static void debuginfo_cache__exit(void)
529{
530 debuginfo__delete(debuginfo_cache);
531 debuginfo_cache = NULL;
532 zfree(&debuginfo_cache_path);
533}
534
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000535
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000536static int get_text_start_address(const char *exec, unsigned long *address)
537{
538 Elf *elf;
539 GElf_Ehdr ehdr;
540 GElf_Shdr shdr;
541 int fd, ret = -ENOENT;
542
543 fd = open(exec, O_RDONLY);
544 if (fd < 0)
545 return -errno;
546
547 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900548 if (elf == NULL) {
549 ret = -EINVAL;
550 goto out_close;
551 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000552
553 if (gelf_getehdr(elf, &ehdr) == NULL)
554 goto out;
555
556 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
557 goto out;
558
559 *address = shdr.sh_addr - shdr.sh_offset;
560 ret = 0;
561out:
562 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900563out_close:
564 close(fd);
565
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000566 return ret;
567}
568
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000569/*
570 * Convert trace point to probe point with debuginfo
571 */
572static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
573 struct perf_probe_point *pp,
574 bool is_kprobe)
575{
576 struct debuginfo *dinfo = NULL;
577 unsigned long stext = 0;
578 u64 addr = tp->address;
579 int ret = -ENOENT;
580
581 /* convert the address to dwarf address */
582 if (!is_kprobe) {
583 if (!addr) {
584 ret = -EINVAL;
585 goto error;
586 }
587 ret = get_text_start_address(tp->module, &stext);
588 if (ret < 0)
589 goto error;
590 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000591 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900592 /* If the module is given, this returns relative address */
593 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
594 false, !!tp->module);
595 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000596 goto error;
597 addr += tp->offset;
598 }
599
600 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
601 tp->module ? : "kernel");
602
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900603 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
604 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000605 ret = debuginfo__find_probe_point(dinfo,
606 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900607 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000608 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000609
610 if (ret > 0) {
611 pp->retprobe = tp->retprobe;
612 return 0;
613 }
614error:
615 pr_debug("Failed to find corresponding probes from debuginfo.\n");
616 return ret ? : -ENOENT;
617}
618
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000619static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
620 int ntevs, const char *exec)
621{
622 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000623 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000624
625 if (!exec)
626 return 0;
627
628 ret = get_text_start_address(exec, &stext);
629 if (ret < 0)
630 return ret;
631
632 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000633 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000634 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000635 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000636 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000637 ret = -ENOMEM;
638 break;
639 }
640 tevs[i].uprobes = true;
641 }
642
643 return ret;
644}
645
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900646static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
647 int ntevs, const char *module)
648{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900649 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530650 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900651
652 if (!module)
653 return 0;
654
Ravi Bangoria63a29612016-04-26 19:55:41 +0530655 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900656
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900657 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530658 tevs[i].point.module =
659 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900660 if (!tevs[i].point.module) {
661 ret = -ENOMEM;
662 break;
663 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900664 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900665
Ravi Bangoria63a29612016-04-26 19:55:41 +0530666 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900667 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900668}
669
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000670/* Post processing the probe events */
671static int post_process_probe_trace_events(struct probe_trace_event *tevs,
672 int ntevs, const char *module,
673 bool uprobe)
674{
675 struct ref_reloc_sym *reloc_sym;
676 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900677 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000678
679 if (uprobe)
680 return add_exec_to_probe_trace_events(tevs, ntevs, module);
681
682 /* Note that currently ref_reloc_sym based probe is not for drivers */
683 if (module)
684 return add_module_to_probe_trace_events(tevs, ntevs, module);
685
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000686 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000687 if (!reloc_sym) {
688 pr_warning("Relocated base symbol is not found!\n");
689 return -EINVAL;
690 }
691
692 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900693 if (!tevs[i].point.address || tevs[i].point.retprobe)
694 continue;
695 /* If we found a wrong one, mark it by NULL symbol */
696 if (kprobe_warn_out_range(tevs[i].point.symbol,
697 tevs[i].point.address)) {
698 tmp = NULL;
699 skipped++;
700 } else {
701 tmp = strdup(reloc_sym->name);
702 if (!tmp)
703 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000704 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900705 /* If we have no realname, use symbol for it */
706 if (!tevs[i].point.realname)
707 tevs[i].point.realname = tevs[i].point.symbol;
708 else
709 free(tevs[i].point.symbol);
710 tevs[i].point.symbol = tmp;
711 tevs[i].point.offset = tevs[i].point.address -
712 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000713 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900714 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000715}
716
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300717/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530718static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900719 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300720{
721 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900722 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530723 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900724 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725
Masami Hiramatsu44225522015-05-08 10:03:28 +0900726 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900727 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000728 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900729 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900730 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731 return 0;
732 }
733
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000734 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900735 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900736 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900737
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900738 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900739 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900740 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900741 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900742 /*
743 * Write back to the original probe_event for
744 * setting appropriate (user given) event name
745 */
746 clear_perf_probe_point(&pev->point);
747 memcpy(&pev->point, &tmp, sizeof(tmp));
748 }
749 }
750
Masami Hiramatsuff741782011-06-27 16:27:39 +0900751 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300752
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400753 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000754 pr_debug("Found %d probe_trace_events.\n", ntevs);
755 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900756 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900757 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000758 clear_probe_trace_events(*tevs, ntevs);
759 zfree(tevs);
760 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900761 if (ret != ntevs)
762 return ret < 0 ? ret : ntevs;
763 ntevs = 0;
764 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400765 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300766
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400767 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900768 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400769 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900770 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400771 }
772 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400773 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000774 if (ntevs < 0) {
775 if (ntevs == -EBADF)
776 pr_warning("Warning: No dwarf info found in the vmlinux - "
777 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400778 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900779 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400780 return 0;
781 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300782 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400783 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784}
785
786#define LINEBUF_SIZE 256
787#define NR_ADDITIONAL_LINES 2
788
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100789static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300790{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000791 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100792 const char *color = show_num ? "" : PERF_COLOR_BLUE;
793 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300794
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100795 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300796 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
797 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100798 if (skip)
799 continue;
800 if (!prefix) {
801 prefix = show_num ? "%7d " : " ";
802 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300803 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100804 color_fprintf(stdout, color, "%s", buf);
805
806 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400807
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100808 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300809error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100810 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000811 pr_warning("File read error: %s\n",
812 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100813 return -1;
814 }
815 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816}
817
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100818static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
819{
820 int rv = __show_one_line(fp, l, skip, show_num);
821 if (rv == 0) {
822 pr_warning("Source file is shorter than expected.\n");
823 rv = -1;
824 }
825 return rv;
826}
827
828#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
829#define show_one_line(f,l) _show_one_line(f,l,false,false)
830#define skip_one_line(f,l) _show_one_line(f,l,true,false)
831#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
832
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300833/*
834 * Show line-range always requires debuginfo to find source file and
835 * line number.
836 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900837static int __show_line_range(struct line_range *lr, const char *module,
838 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300839{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400840 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000841 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900842 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300843 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900844 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900845 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000846 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300847
848 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000849 dinfo = open_debuginfo(module, false);
850 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900851 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400852
Masami Hiramatsuff741782011-06-27 16:27:39 +0900853 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900854 if (!ret) { /* Not found, retry with an alternative */
855 ret = get_alternative_line_range(dinfo, lr, module, user);
856 if (!ret)
857 ret = debuginfo__find_line_range(dinfo, lr);
858 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900859 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000860 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 pr_warning("Specified source line is not found.\n");
862 return -ENOENT;
863 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000864 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 return ret;
866 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300867
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900868 /* Convert source file path */
869 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900870 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800871
872 /* Free old path when new path is assigned */
873 if (tmp != lr->path)
874 free(tmp);
875
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900876 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000877 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900878 return ret;
879 }
880
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300881 setup_pager();
882
883 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900884 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300885 lr->start - lr->offset);
886 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100887 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300888
889 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400890 if (fp == NULL) {
891 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000892 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400893 return -errno;
894 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300895 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100896 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100897 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100898 if (ret < 0)
899 goto end;
900 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300901
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000902 intlist__for_each(ln, lr->line_list) {
903 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100904 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100905 if (ret < 0)
906 goto end;
907 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100908 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400909 if (ret < 0)
910 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300911 }
912
913 if (lr->end == INT_MAX)
914 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100915 while (l <= lr->end) {
916 ret = show_one_line_or_eof(fp, l++ - lr->offset);
917 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100918 break;
919 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400920end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300921 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400922 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300923}
924
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000925int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000926{
927 int ret;
928
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900929 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000930 if (ret < 0)
931 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900932 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900933 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000934
935 return ret;
936}
937
Masami Hiramatsuff741782011-06-27 16:27:39 +0900938static int show_available_vars_at(struct debuginfo *dinfo,
939 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900940 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900941{
942 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900943 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944 struct str_node *node;
945 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900946 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900947 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900948
949 buf = synthesize_perf_probe_point(&pev->point);
950 if (!buf)
951 return -EINVAL;
952 pr_debug("Searching variables at %s\n", buf);
953
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900954 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900955 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900956 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900957 if (!ret) {
958 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900959 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900960 /* Release the old probe_point */
961 clear_perf_probe_point(&tmp);
962 }
963 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900964 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000965 if (ret == 0 || ret == -ENOENT) {
966 pr_err("Failed to find the address of %s\n", buf);
967 ret = -ENOENT;
968 } else
969 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900970 goto end;
971 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000972
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900973 /* Some variables are found */
974 fprintf(stdout, "Available variables at %s\n", buf);
975 for (i = 0; i < ret; i++) {
976 vl = &vls[i];
977 /*
978 * A probe point might be converted to
979 * several trace points.
980 */
981 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
982 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300983 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900984 nvars = 0;
985 if (vl->vars) {
986 strlist__for_each(node, vl->vars) {
987 var = strchr(node->s, '\t') + 1;
988 if (strfilter__compare(_filter, var)) {
989 fprintf(stdout, "\t\t%s\n", node->s);
990 nvars++;
991 }
992 }
993 strlist__delete(vl->vars);
994 }
995 if (nvars == 0)
996 fprintf(stdout, "\t\t(No matched variables)\n");
997 }
998 free(vls);
999end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001000 free(buf);
1001 return ret;
1002}
1003
1004/* Show available variables on given probe point */
1005int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001006 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001007{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001008 int i, ret = 0;
1009 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001010
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001011 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001012 if (ret < 0)
1013 return ret;
1014
Masami Hiramatsu44225522015-05-08 10:03:28 +09001015 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001016 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001017 ret = -ENOENT;
1018 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001019 }
1020
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001021 setup_pager();
1022
Masami Hiramatsuff741782011-06-27 16:27:39 +09001023 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001024 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001025
1026 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001027out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001028 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001029 return ret;
1030}
1031
Ingo Molnar89fe8082013-09-30 12:07:11 +02001032#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001033
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001034static void debuginfo_cache__exit(void)
1035{
1036}
1037
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001038static int
1039find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1040 struct perf_probe_point *pp __maybe_unused,
1041 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001042{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001043 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001044}
1045
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301046static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001047 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001048{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001049 if (perf_probe_event_need_dwarf(pev)) {
1050 pr_warning("Debuginfo-analysis is not supported.\n");
1051 return -ENOSYS;
1052 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301053
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001054 return 0;
1055}
1056
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001057int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001058 const char *module __maybe_unused,
1059 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001060{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001061 pr_warning("Debuginfo-analysis is not supported.\n");
1062 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001063}
1064
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001065int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001066 int npevs __maybe_unused,
1067 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001068{
1069 pr_warning("Debuginfo-analysis is not supported.\n");
1070 return -ENOSYS;
1071}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001072#endif
1073
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001074void line_range__clear(struct line_range *lr)
1075{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001076 free(lr->function);
1077 free(lr->file);
1078 free(lr->path);
1079 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001080 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001081 memset(lr, 0, sizeof(*lr));
1082}
1083
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001084int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001085{
1086 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001087 lr->line_list = intlist__new(NULL);
1088 if (!lr->line_list)
1089 return -ENOMEM;
1090 else
1091 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001092}
1093
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001094static int parse_line_num(char **ptr, int *val, const char *what)
1095{
1096 const char *start = *ptr;
1097
1098 errno = 0;
1099 *val = strtol(*ptr, ptr, 0);
1100 if (errno || *ptr == start) {
1101 semantic_error("'%s' is not a valid number.\n", what);
1102 return -EINVAL;
1103 }
1104 return 0;
1105}
1106
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001107/* Check the name is good for event, group or function */
1108static bool is_c_func_name(const char *name)
1109{
1110 if (!isalpha(*name) && *name != '_')
1111 return false;
1112 while (*++name != '\0') {
1113 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1114 return false;
1115 }
1116 return true;
1117}
1118
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001119/*
1120 * Stuff 'lr' according to the line range described by 'arg'.
1121 * The line range syntax is described by:
1122 *
1123 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001124 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001125 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001126int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001127{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001128 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001129 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001130
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001131 if (!name)
1132 return -ENOMEM;
1133
1134 lr->start = 0;
1135 lr->end = INT_MAX;
1136
1137 range = strchr(name, ':');
1138 if (range) {
1139 *range++ = '\0';
1140
1141 err = parse_line_num(&range, &lr->start, "start line");
1142 if (err)
1143 goto err;
1144
1145 if (*range == '+' || *range == '-') {
1146 const char c = *range++;
1147
1148 err = parse_line_num(&range, &lr->end, "end line");
1149 if (err)
1150 goto err;
1151
1152 if (c == '+') {
1153 lr->end += lr->start;
1154 /*
1155 * Adjust the number of lines here.
1156 * If the number of lines == 1, the
1157 * the end of line should be equal to
1158 * the start of line.
1159 */
1160 lr->end--;
1161 }
1162 }
1163
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001164 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001165
1166 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001167 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001168 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001169 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001170 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001171 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001172 if (*range != '\0') {
1173 semantic_error("Tailing with invalid str '%s'.\n", range);
1174 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001175 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001176 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001177
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001178 file = strchr(name, '@');
1179 if (file) {
1180 *file = '\0';
1181 lr->file = strdup(++file);
1182 if (lr->file == NULL) {
1183 err = -ENOMEM;
1184 goto err;
1185 }
1186 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001187 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001188 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001189 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001190 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001191 else { /* Invalid name */
1192 semantic_error("'%s' is not a valid function name.\n", name);
1193 err = -EINVAL;
1194 goto err;
1195 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001196
1197 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001198err:
1199 free(name);
1200 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001201}
1202
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001203/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001205{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001206 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001207 char *ptr, *tmp;
1208 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301209 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001210 /*
1211 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001212 * perf probe [EVENT=]SRC[:LN|;PTN]
1213 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001214 *
1215 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001216 */
Wang Nane59d29e2015-04-28 08:46:09 +00001217 if (!arg)
1218 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001219
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001220 ptr = strpbrk(arg, ";=@+%");
1221 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001222 *ptr = '\0';
1223 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224 if (strchr(arg, ':')) {
1225 semantic_error("Group name is not supported yet.\n");
1226 return -ENOTSUP;
1227 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001228 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001229 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230 "follow C symbol-naming rule.\n", arg);
1231 return -EINVAL;
1232 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001233 pev->event = strdup(arg);
1234 if (pev->event == NULL)
1235 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001236 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001237 arg = tmp;
1238 }
1239
Naveen N. Rao3099c022015-04-28 17:35:34 +05301240 /*
1241 * Check arg is function or file name and copy it.
1242 *
1243 * We consider arg to be a file spec if and only if it satisfies
1244 * all of the below criteria::
1245 * - it does not include any of "+@%",
1246 * - it includes one of ":;", and
1247 * - it has a period '.' in the name.
1248 *
1249 * Otherwise, we consider arg to be a function specification.
1250 */
1251 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1252 /* This is a file spec if it includes a '.' before ; or : */
1253 if (memchr(arg, '.', ptr - arg))
1254 file_spec = true;
1255 }
1256
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001257 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258 if (ptr) {
1259 nc = *ptr;
1260 *ptr++ = '\0';
1261 }
1262
Wang Nan6c6e0242015-08-26 10:57:44 +00001263 if (arg[0] == '\0')
1264 tmp = NULL;
1265 else {
1266 tmp = strdup(arg);
1267 if (tmp == NULL)
1268 return -ENOMEM;
1269 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001270
Naveen N. Rao3099c022015-04-28 17:35:34 +05301271 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001272 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001273 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001274 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001275
Wang Nanda15bd92015-08-26 10:57:45 +00001276 /*
1277 * Keep pp->function even if this is absolute address,
1278 * so it can mark whether abs_address is valid.
1279 * Which make 'perf probe lib.bin 0x0' possible.
1280 *
1281 * Note that checking length of tmp is not needed
1282 * because when we access tmp[1] we know tmp[0] is '0',
1283 * so tmp[1] should always valid (but could be '\0').
1284 */
1285 if (tmp && !strncmp(tmp, "0x", 2)) {
1286 pp->abs_address = strtoul(pp->function, &tmp, 0);
1287 if (*tmp != '\0') {
1288 semantic_error("Invalid absolute address.\n");
1289 return -EINVAL;
1290 }
1291 }
1292 }
1293
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001294 /* Parse other options */
1295 while (ptr) {
1296 arg = ptr;
1297 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001298 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001299 pp->lazy_line = strdup(arg);
1300 if (pp->lazy_line == NULL)
1301 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001302 break;
1303 }
1304 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001305 if (ptr) {
1306 nc = *ptr;
1307 *ptr++ = '\0';
1308 }
1309 switch (c) {
1310 case ':': /* Line number */
1311 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001313 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001314 " in line number.\n");
1315 return -EINVAL;
1316 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001317 break;
1318 case '+': /* Byte offset from a symbol */
1319 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001320 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001321 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001322 " in offset.\n");
1323 return -EINVAL;
1324 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001325 break;
1326 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 if (pp->file) {
1328 semantic_error("SRC@SRC is not allowed.\n");
1329 return -EINVAL;
1330 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001331 pp->file = strdup(arg);
1332 if (pp->file == NULL)
1333 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001334 break;
1335 case '%': /* Probe places */
1336 if (strcmp(arg, "return") == 0) {
1337 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001338 } else { /* Others not supported yet */
1339 semantic_error("%%%s is not supported.\n", arg);
1340 return -ENOTSUP;
1341 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001342 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001343 default: /* Buggy case */
1344 pr_err("This program has a bug at %s:%d.\n",
1345 __FILE__, __LINE__);
1346 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001347 break;
1348 }
1349 }
1350
1351 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001352 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001353 semantic_error("Lazy pattern can't be used with"
1354 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001355 return -EINVAL;
1356 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001357
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001358 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001359 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 return -EINVAL;
1361 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001362
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001363 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001364 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365 return -EINVAL;
1366 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001367
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001368 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001369 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001370 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001371 return -EINVAL;
1372 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001373
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001374 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001375 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376 return -EINVAL;
1377 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001378
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001379 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001380 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381 return -EINVAL;
1382 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001383
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001384 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001385 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001386 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 return -EINVAL;
1388 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001389
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001390 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001391 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1392 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001394}
1395
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001396/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001398{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001399 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001400 struct perf_probe_arg_field **fieldp;
1401
1402 pr_debug("parsing arg: %s into ", str);
1403
Masami Hiramatsu48481932010-04-12 13:16:53 -04001404 tmp = strchr(str, '=');
1405 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001406 arg->name = strndup(str, tmp - str);
1407 if (arg->name == NULL)
1408 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001409 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001410 str = tmp + 1;
1411 }
1412
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001413 tmp = strchr(str, ':');
1414 if (tmp) { /* Type setting */
1415 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001416 arg->type = strdup(tmp + 1);
1417 if (arg->type == NULL)
1418 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001419 pr_debug("type:%s ", arg->type);
1420 }
1421
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001422 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001423 if (!is_c_varname(str) || !tmp) {
1424 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001425 arg->var = strdup(str);
1426 if (arg->var == NULL)
1427 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001428 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001430 }
1431
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001432 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001433 arg->var = strndup(str, tmp - str);
1434 if (arg->var == NULL)
1435 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001436 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001437 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001438 fieldp = &arg->field;
1439
1440 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001441 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1442 if (*fieldp == NULL)
1443 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001444 if (*tmp == '[') { /* Array */
1445 str = tmp;
1446 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001447 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001448 if (*tmp != ']' || tmp == str + 1) {
1449 semantic_error("Array index must be a"
1450 " number.\n");
1451 return -EINVAL;
1452 }
1453 tmp++;
1454 if (*tmp == '\0')
1455 tmp = NULL;
1456 } else { /* Structure */
1457 if (*tmp == '.') {
1458 str = tmp + 1;
1459 (*fieldp)->ref = false;
1460 } else if (tmp[1] == '>') {
1461 str = tmp + 2;
1462 (*fieldp)->ref = true;
1463 } else {
1464 semantic_error("Argument parse error: %s\n",
1465 str);
1466 return -EINVAL;
1467 }
1468 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001469 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001470 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001471 (*fieldp)->name = strndup(str, tmp - str);
1472 if ((*fieldp)->name == NULL)
1473 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001474 if (*str != '[')
1475 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001476 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1477 fieldp = &(*fieldp)->next;
1478 }
1479 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001480 (*fieldp)->name = strdup(str);
1481 if ((*fieldp)->name == NULL)
1482 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001483 if (*str != '[')
1484 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001485 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001486
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001487 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001488 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001489 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001490 if (arg->name == NULL)
1491 return -ENOMEM;
1492 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001493 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001494}
1495
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001496/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001497int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001498{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001499 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001500 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001501
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001502 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001503 if (!argv) {
1504 pr_debug("Failed to split arguments.\n");
1505 return -ENOMEM;
1506 }
1507 if (argc - 1 > MAX_PROBE_ARGS) {
1508 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1509 ret = -ERANGE;
1510 goto out;
1511 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001512 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001513 ret = parse_perf_probe_point(argv[0], pev);
1514 if (ret < 0)
1515 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001516
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001517 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001518 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001519 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1520 if (pev->args == NULL) {
1521 ret = -ENOMEM;
1522 goto out;
1523 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1525 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1526 if (ret >= 0 &&
1527 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001528 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 " kretprobe.\n");
1530 ret = -EINVAL;
1531 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001532 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001533out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001534 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001535
1536 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001537}
1538
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001539/* Return true if this perf_probe_event requires debuginfo */
1540bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001542 int i;
1543
1544 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1545 return true;
1546
1547 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001548 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001549 return true;
1550
1551 return false;
1552}
1553
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301554/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001555int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001556{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301557 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001558 char pr;
1559 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001560 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001561 int ret, i, argc;
1562 char **argv;
1563
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301564 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001565 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001566 if (!argv) {
1567 pr_debug("Failed to split arguments.\n");
1568 return -ENOMEM;
1569 }
1570 if (argc < 2) {
1571 semantic_error("Too few probe arguments.\n");
1572 ret = -ERANGE;
1573 goto out;
1574 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001575
1576 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001577 argv0_str = strdup(argv[0]);
1578 if (argv0_str == NULL) {
1579 ret = -ENOMEM;
1580 goto out;
1581 }
1582 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1583 fmt2_str = strtok_r(NULL, "/", &fmt);
1584 fmt3_str = strtok_r(NULL, " \t", &fmt);
1585 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1586 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001587 semantic_error("Failed to parse event name: %s\n", argv[0]);
1588 ret = -EINVAL;
1589 goto out;
1590 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001591 pr = fmt1_str[0];
1592 tev->group = strdup(fmt2_str);
1593 tev->event = strdup(fmt3_str);
1594 if (tev->group == NULL || tev->event == NULL) {
1595 ret = -ENOMEM;
1596 goto out;
1597 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001598 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001599
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001600 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001601
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001602 /* Scan module name(if there), function name and offset */
1603 p = strchr(argv[1], ':');
1604 if (p) {
1605 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001606 if (!tp->module) {
1607 ret = -ENOMEM;
1608 goto out;
1609 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001610 p++;
1611 } else
1612 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001613 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001614 /* only the address started with 0x */
1615 if (fmt1_str[0] == '0') {
1616 /*
1617 * Fix a special case:
1618 * if address == 0, kernel reports something like:
1619 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1620 * Newer kernel may fix that, but we want to
1621 * support old kernel also.
1622 */
1623 if (strcmp(fmt1_str, "0x") == 0) {
1624 if (!argv[2] || strcmp(argv[2], "(null)")) {
1625 ret = -EINVAL;
1626 goto out;
1627 }
1628 tp->address = 0;
1629
1630 free(argv[2]);
1631 for (i = 2; argv[i + 1] != NULL; i++)
1632 argv[i] = argv[i + 1];
1633
1634 argv[i] = NULL;
1635 argc -= 1;
1636 } else
1637 tp->address = strtoul(fmt1_str, NULL, 0);
1638 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001639 /* Only the symbol-based probe has offset */
1640 tp->symbol = strdup(fmt1_str);
1641 if (tp->symbol == NULL) {
1642 ret = -ENOMEM;
1643 goto out;
1644 }
1645 fmt2_str = strtok_r(NULL, "", &fmt);
1646 if (fmt2_str == NULL)
1647 tp->offset = 0;
1648 else
1649 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001650 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001651
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301653 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001654 if (tev->args == NULL) {
1655 ret = -ENOMEM;
1656 goto out;
1657 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001658 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001659 p = strchr(argv[i + 2], '=');
1660 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 *p++ = '\0';
1662 else
1663 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001664 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001665 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001666 tev->args[i].value = strdup(p);
1667 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1668 ret = -ENOMEM;
1669 goto out;
1670 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001671 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001672 ret = 0;
1673out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001674 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001675 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001676 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001677}
1678
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001679/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001680char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001681{
1682 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001683 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001684 char *ret = NULL;
1685 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001686
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001687 if (strbuf_init(&buf, 64) < 0)
1688 return NULL;
1689
Masami Hiramatsu48481932010-04-12 13:16:53 -04001690 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001691 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001692 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001693 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1694 if (err)
1695 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001696
1697 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001698 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001699 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001700 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001701 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1702 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001703 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001704 if (err)
1705 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001706 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001707
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001708 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001709 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1710 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001711
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001712 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001713out:
1714 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001715 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001716}
1717
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718/* Compose only probe point (not argument) */
1719static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001720{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001721 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001722 char *tmp, *ret = NULL;
1723 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001724
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001725 if (strbuf_init(&buf, 64) < 0)
1726 return NULL;
1727
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001728 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001729 if (strbuf_addstr(&buf, pp->function) < 0)
1730 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001731 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001732 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001733 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001734 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001735 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001736 err = strbuf_addstr(&buf, "%return");
1737 if (err)
1738 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001739 }
1740 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001741 tmp = pp->file;
1742 len = strlen(tmp);
1743 if (len > 30) {
1744 tmp = strchr(pp->file + len - 30, '/');
1745 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1746 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001747 err = strbuf_addf(&buf, "@%s", tmp);
1748 if (!err && !pp->function && pp->line)
1749 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001750 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001751 if (!err)
1752 ret = strbuf_detach(&buf, NULL);
1753out:
1754 strbuf_release(&buf);
1755 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001756}
1757
1758#if 0
1759char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1760{
1761 char *buf;
1762 int i, len, ret;
1763
1764 buf = synthesize_perf_probe_point(&pev->point);
1765 if (!buf)
1766 return NULL;
1767
1768 len = strlen(buf);
1769 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001770 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 pev->args[i].name);
1772 if (ret <= 0) {
1773 free(buf);
1774 return NULL;
1775 }
1776 len += ret;
1777 }
1778
1779 return buf;
1780}
1781#endif
1782
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301783static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001784 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001785{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001786 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001787 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301788 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001789 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001790 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001791 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001792 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001793 err = strbuf_addf(buf, "%+ld(", ref->offset);
1794 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001795}
1796
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301797static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001798 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001799{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301800 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001801 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001802
1803 /* Argument name or separator */
1804 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001805 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001806 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001807 err = strbuf_addch(buf, ' ');
1808 if (err)
1809 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001810
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001811 /* Special case: @XXX */
1812 if (arg->value[0] == '@' && arg->ref)
1813 ref = ref->next;
1814
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001815 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001816 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001817 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001818 if (depth < 0)
1819 return depth;
1820 }
1821
1822 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001823 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001824 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001825 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001826 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001827
1828 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001829 while (!err && depth--)
1830 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001831
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001832 /* Print argument type */
1833 if (!err && arg->type)
1834 err = strbuf_addf(buf, ":%s", arg->type);
1835
1836 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001837}
1838
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301839char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001840{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301841 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001842 struct strbuf buf;
1843 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001844 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001845
Wang Nanda15bd92015-08-26 10:57:45 +00001846 /* Uprobes must have tp->module */
1847 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001848 return NULL;
1849
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001850 if (strbuf_init(&buf, 32) < 0)
1851 return NULL;
1852
1853 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1854 tev->group, tev->event) < 0)
1855 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001856 /*
1857 * If tp->address == 0, then this point must be a
1858 * absolute address uprobe.
1859 * try_to_find_absolute_address() should have made
1860 * tp->symbol to "0x0".
1861 */
1862 if (tev->uprobes && !tp->address) {
1863 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1864 goto error;
1865 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001866
1867 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301868 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001869 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001870 else if (!strncmp(tp->symbol, "0x", 2))
1871 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001872 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1873 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301874 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001875 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1876 tp->module ? ":" : "", tp->symbol, tp->offset);
1877 if (err)
1878 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301879
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001880 for (i = 0; i < tev->nargs; i++)
1881 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001882 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001883
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001884 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001885error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001886 strbuf_release(&buf);
1887 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001889
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001890static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1891 struct perf_probe_point *pp,
1892 bool is_kprobe)
1893{
1894 struct symbol *sym = NULL;
1895 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001896 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001897 int ret = -ENOENT;
1898
1899 if (!is_kprobe) {
1900 map = dso__new_map(tp->module);
1901 if (!map)
1902 goto out;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001903 sym = map__find_symbol(map, addr, NULL);
1904 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001905 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09001906 if (kernel_get_symbol_address_by_name(tp->symbol,
1907 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001908 goto out;
1909 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001910 if (addr) {
1911 addr += tp->offset;
1912 sym = __find_kernel_function(addr, &map);
1913 }
1914 }
Wang Nan98d3b252015-11-05 13:19:25 +00001915
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001916 if (!sym)
1917 goto out;
1918
1919 pp->retprobe = tp->retprobe;
1920 pp->offset = addr - map->unmap_ip(map, sym->start);
1921 pp->function = strdup(sym->name);
1922 ret = pp->function ? 0 : -ENOMEM;
1923
1924out:
1925 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001926 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001927 }
1928
1929 return ret;
1930}
1931
1932static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001933 struct perf_probe_point *pp,
1934 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001935{
1936 char buf[128];
1937 int ret;
1938
1939 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1940 if (!ret)
1941 return 0;
1942 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1943 if (!ret)
1944 return 0;
1945
1946 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1947
1948 if (tp->symbol) {
1949 pp->function = strdup(tp->symbol);
1950 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00001951 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001952 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1953 if (ret < 0)
1954 return ret;
1955 pp->function = strdup(buf);
1956 pp->offset = 0;
1957 }
1958 if (pp->function == NULL)
1959 return -ENOMEM;
1960
1961 pp->retprobe = tp->retprobe;
1962
1963 return 0;
1964}
1965
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301966static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301967 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001968{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001969 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001970 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001971
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001972 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001973 pev->event = strdup(tev->event);
1974 pev->group = strdup(tev->group);
1975 if (pev->event == NULL || pev->group == NULL)
1976 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001977
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001978 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001979 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001980 if (ret < 0)
1981 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001982
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001983 /* Convert trace_arg to probe_arg */
1984 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001985 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1986 if (pev->args == NULL)
1987 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001988 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001989 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001990 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001991 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001992 if ((ret = strbuf_init(&buf, 32)) < 0)
1993 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001994 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
1995 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001996 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001997 if (pev->args[i].name == NULL && ret >= 0)
1998 ret = -ENOMEM;
1999 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002000error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002001 if (ret < 0)
2002 clear_perf_probe_event(pev);
2003
2004 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002005}
2006
2007void clear_perf_probe_event(struct perf_probe_event *pev)
2008{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002009 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002010 int i;
2011
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002012 free(pev->event);
2013 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002014 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002015 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002016
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002017 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002018 free(pev->args[i].name);
2019 free(pev->args[i].var);
2020 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002021 field = pev->args[i].field;
2022 while (field) {
2023 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002024 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002025 free(field);
2026 field = next;
2027 }
2028 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002029 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002030 memset(pev, 0, sizeof(*pev));
2031}
2032
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002033void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002034{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302035 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002036 int i;
2037
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002038 free(tev->event);
2039 free(tev->group);
2040 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002041 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002042 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002043 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002044 free(tev->args[i].name);
2045 free(tev->args[i].value);
2046 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002047 ref = tev->args[i].ref;
2048 while (ref) {
2049 next = ref->next;
2050 free(ref);
2051 ref = next;
2052 }
2053 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002054 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002055 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002056}
2057
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002058struct kprobe_blacklist_node {
2059 struct list_head list;
2060 unsigned long start;
2061 unsigned long end;
2062 char *symbol;
2063};
2064
2065static void kprobe_blacklist__delete(struct list_head *blacklist)
2066{
2067 struct kprobe_blacklist_node *node;
2068
2069 while (!list_empty(blacklist)) {
2070 node = list_first_entry(blacklist,
2071 struct kprobe_blacklist_node, list);
2072 list_del(&node->list);
2073 free(node->symbol);
2074 free(node);
2075 }
2076}
2077
2078static int kprobe_blacklist__load(struct list_head *blacklist)
2079{
2080 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002081 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002082 char buf[PATH_MAX], *p;
2083 FILE *fp;
2084 int ret;
2085
2086 if (__debugfs == NULL)
2087 return -ENOTSUP;
2088
2089 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2090 if (ret < 0)
2091 return ret;
2092
2093 fp = fopen(buf, "r");
2094 if (!fp)
2095 return -errno;
2096
2097 ret = 0;
2098 while (fgets(buf, PATH_MAX, fp)) {
2099 node = zalloc(sizeof(*node));
2100 if (!node) {
2101 ret = -ENOMEM;
2102 break;
2103 }
2104 INIT_LIST_HEAD(&node->list);
2105 list_add_tail(&node->list, blacklist);
2106 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2107 ret = -EINVAL;
2108 break;
2109 }
2110 p = strchr(buf, '\t');
2111 if (p) {
2112 p++;
2113 if (p[strlen(p) - 1] == '\n')
2114 p[strlen(p) - 1] = '\0';
2115 } else
2116 p = (char *)"unknown";
2117 node->symbol = strdup(p);
2118 if (!node->symbol) {
2119 ret = -ENOMEM;
2120 break;
2121 }
2122 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2123 node->start, node->end, node->symbol);
2124 ret++;
2125 }
2126 if (ret < 0)
2127 kprobe_blacklist__delete(blacklist);
2128 fclose(fp);
2129
2130 return ret;
2131}
2132
2133static struct kprobe_blacklist_node *
2134kprobe_blacklist__find_by_address(struct list_head *blacklist,
2135 unsigned long address)
2136{
2137 struct kprobe_blacklist_node *node;
2138
2139 list_for_each_entry(node, blacklist, list) {
2140 if (node->start <= address && address <= node->end)
2141 return node;
2142 }
2143
2144 return NULL;
2145}
2146
Masami Hiramatsub0312202015-06-16 20:50:55 +09002147static LIST_HEAD(kprobe_blacklist);
2148
2149static void kprobe_blacklist__init(void)
2150{
2151 if (!list_empty(&kprobe_blacklist))
2152 return;
2153
2154 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2155 pr_debug("No kprobe blacklist support, ignored\n");
2156}
2157
2158static void kprobe_blacklist__release(void)
2159{
2160 kprobe_blacklist__delete(&kprobe_blacklist);
2161}
2162
2163static bool kprobe_blacklist__listed(unsigned long address)
2164{
2165 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2166}
2167
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002168static int perf_probe_event__sprintf(const char *group, const char *event,
2169 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002170 const char *module,
2171 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002172{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002173 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002174 char *buf;
2175
2176 if (asprintf(&buf, "%s:%s", group, event) < 0)
2177 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002178 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002179 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002180 if (ret)
2181 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002182
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002183 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002184 buf = synthesize_perf_probe_point(&pev->point);
2185 if (!buf)
2186 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002187 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002188 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002189
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002190 if (!ret && module)
2191 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002192
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002193 if (!ret && pev->nargs > 0) {
2194 ret = strbuf_add(result, " with", 5);
2195 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002196 buf = synthesize_perf_probe_arg(&pev->args[i]);
2197 if (!buf)
2198 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002199 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002200 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002201 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002202 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002203 if (!ret)
2204 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002205
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002206 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002207}
2208
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002209/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002210int show_perf_probe_event(const char *group, const char *event,
2211 struct perf_probe_event *pev,
2212 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002213{
2214 struct strbuf buf = STRBUF_INIT;
2215 int ret;
2216
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002217 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002218 if (ret >= 0) {
2219 if (use_stdout)
2220 printf("%s\n", buf.buf);
2221 else
2222 pr_info("%s\n", buf.buf);
2223 }
2224 strbuf_release(&buf);
2225
2226 return ret;
2227}
2228
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002229static bool filter_probe_trace_event(struct probe_trace_event *tev,
2230 struct strfilter *filter)
2231{
2232 char tmp[128];
2233
2234 /* At first, check the event name itself */
2235 if (strfilter__compare(filter, tev->event))
2236 return true;
2237
2238 /* Next, check the combination of name and group */
2239 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2240 return false;
2241 return strfilter__compare(filter, tmp);
2242}
2243
2244static int __show_perf_probe_events(int fd, bool is_kprobe,
2245 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002246{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302247 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302248 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002249 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002250 struct strlist *rawlist;
2251 struct str_node *ent;
2252
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002253 memset(&tev, 0, sizeof(tev));
2254 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002255
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002256 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002257 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002258 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002259
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002260 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302261 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002262 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002263 if (!filter_probe_trace_event(&tev, filter))
2264 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302265 ret = convert_to_perf_probe_event(&tev, &pev,
2266 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002267 if (ret < 0)
2268 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002269 ret = show_perf_probe_event(pev.group, pev.event,
2270 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002271 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002273next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002274 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302275 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002276 if (ret < 0)
2277 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002278 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002279 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002280 /* Cleanup cached debuginfo if needed */
2281 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002282
2283 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002284}
2285
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302286/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002287int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302288{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002289 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302290
2291 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302292
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002293 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302294 if (ret < 0)
2295 return ret;
2296
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002297 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2298 if (ret < 0)
2299 return ret;
2300
2301 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002302 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002303 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002304 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002305 if (kp_fd > 0)
2306 close(kp_fd);
2307 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002308 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002309 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302310
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002311 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002312}
2313
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002314static int get_new_event_name(char *buf, size_t len, const char *base,
2315 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002316{
2317 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002318 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002319
Naveen N. Rao3099c022015-04-28 17:35:34 +05302320 if (*base == '.')
2321 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002322 nbase = strdup(base);
2323 if (!nbase)
2324 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302325
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002326 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2327 p = strchr(nbase, '.');
2328 if (p && p != nbase)
2329 *p = '\0';
2330
2331 /* Try no suffix number */
2332 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002333 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002334 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002335 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002336 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002337 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002338 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002339
Masami Hiramatsud761b082009-12-15 10:32:25 -05002340 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002341 pr_warning("Error: event \"%s\" already exists.\n"
2342 " Hint: Remove existing event by 'perf probe -d'\n"
2343 " or force duplicates by 'perf probe -f'\n"
2344 " or set 'force=yes' in BPF source.\n",
2345 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002346 ret = -EEXIST;
2347 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002348 }
2349
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002350 /* Try to add suffix */
2351 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002352 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002353 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002354 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002355 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002356 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002357 if (!strlist__has_entry(namelist, buf))
2358 break;
2359 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002360 if (i == MAX_EVENT_INDEX) {
2361 pr_warning("Too many events are on the same function.\n");
2362 ret = -ERANGE;
2363 }
2364
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002365out:
2366 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002367 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002368}
2369
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002370/* Warn if the current kernel's uprobe implementation is old */
2371static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2372{
2373 int i;
2374 char *buf = synthesize_probe_trace_command(tev);
2375
2376 /* Old uprobe event doesn't support memory dereference */
2377 if (!tev->uprobes || tev->nargs == 0 || !buf)
2378 goto out;
2379
2380 for (i = 0; i < tev->nargs; i++)
2381 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2382 pr_warning("Please upgrade your kernel to at least "
2383 "3.14 to have access to feature %s\n",
2384 tev->args[i].value);
2385 break;
2386 }
2387out:
2388 free(buf);
2389}
2390
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002391/* Set new name from original perf_probe_event and namelist */
2392static int probe_trace_event__set_name(struct probe_trace_event *tev,
2393 struct perf_probe_event *pev,
2394 struct strlist *namelist,
2395 bool allow_suffix)
2396{
2397 const char *event, *group;
2398 char buf[64];
2399 int ret;
2400
2401 if (pev->event)
2402 event = pev->event;
2403 else
Wang Nanda15bd92015-08-26 10:57:45 +00002404 if (pev->point.function &&
2405 (strncmp(pev->point.function, "0x", 2) != 0) &&
2406 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002407 event = pev->point.function;
2408 else
2409 event = tev->point.realname;
2410 if (pev->group)
2411 group = pev->group;
2412 else
2413 group = PERFPROBE_GROUP;
2414
2415 /* Get an unused new event name */
2416 ret = get_new_event_name(buf, 64, event,
2417 namelist, allow_suffix);
2418 if (ret < 0)
2419 return ret;
2420
2421 event = buf;
2422
2423 tev->event = strdup(event);
2424 tev->group = strdup(group);
2425 if (tev->event == NULL || tev->group == NULL)
2426 return -ENOMEM;
2427
2428 /* Add added event name to namelist */
2429 strlist__add(namelist, event);
2430 return 0;
2431}
2432
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302433static int __add_probe_trace_events(struct perf_probe_event *pev,
2434 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002435 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002436{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002437 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302438 struct probe_trace_event *tev = NULL;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002439 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002440
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002441 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2442 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002443 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002444
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002445 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002446 namelist = probe_file__get_namelist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447 if (!namelist) {
2448 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002449 ret = -ENOMEM;
2450 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002451 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002452
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002453 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002454 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002455 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002456 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002457 if (!tev->point.symbol)
2458 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002459
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002460 /* Set new name for tev (and update namelist) */
2461 ret = probe_trace_event__set_name(tev, pev, namelist,
2462 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002463 if (ret < 0)
2464 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002465
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002466 ret = probe_file__add_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002467 if (ret < 0)
2468 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002469
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002470 /*
2471 * Probes after the first probe which comes from same
2472 * user input are always allowed to add suffix, because
2473 * there might be several addresses corresponding to
2474 * one code line.
2475 */
2476 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002477 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002478 if (ret == -EINVAL && pev->uprobes)
2479 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002480
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002481 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002482close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002483 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002484 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002485}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002486
Wang Nan6bb536c2015-05-29 09:45:47 +00002487static int find_probe_functions(struct map *map, char *name,
2488 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002489{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002490 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002491 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002492 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002493
Wang Nan75e4a2a2015-05-15 12:14:44 +00002494 if (map__load(map, NULL) < 0)
2495 return 0;
2496
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002497 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002498 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002499 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002500 if (syms && found < probe_conf.max_probes)
2501 syms[found - 1] = sym;
2502 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002503 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002504
2505 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002506}
2507
2508#define strdup_or_goto(str, label) \
2509 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2510
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302511void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2512 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302513 struct map *map __maybe_unused,
2514 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302515
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002516/*
2517 * Find probe function addresses from map.
2518 * Return an error or the number of found probe_trace_event
2519 */
2520static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002521 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002522{
2523 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002524 struct ref_reloc_sym *reloc_sym = NULL;
2525 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002526 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002527 struct probe_trace_event *tev;
2528 struct perf_probe_point *pp = &pev->point;
2529 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002530 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002531 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302532 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002533
Masami Hiramatsu44225522015-05-08 10:03:28 +09002534 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002535 if (!map) {
2536 ret = -EINVAL;
2537 goto out;
2538 }
2539
Wang Nan6bb536c2015-05-29 09:45:47 +00002540 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2541 if (!syms) {
2542 ret = -ENOMEM;
2543 goto out;
2544 }
2545
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002546 /*
2547 * Load matched symbols: Since the different local symbols may have
2548 * same name but different addresses, this lists all the symbols.
2549 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002550 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002551 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002552 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002553 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002554 ret = -ENOENT;
2555 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002556 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002557 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002558 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002559 ret = -E2BIG;
2560 goto out;
2561 }
2562
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002563 /* Note that the symbols in the kmodule are not relocated */
2564 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002565 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002566 if (!reloc_sym) {
2567 pr_warning("Relocated base symbol is not found!\n");
2568 ret = -EINVAL;
2569 goto out;
2570 }
2571 }
2572
2573 /* Setup result trace-probe-events */
2574 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2575 if (!*tevs) {
2576 ret = -ENOMEM;
2577 goto out;
2578 }
2579
2580 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002581
Wang Nan6bb536c2015-05-29 09:45:47 +00002582 for (j = 0; j < num_matched_functions; j++) {
2583 sym = syms[j];
2584
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002585 tev = (*tevs) + ret;
2586 tp = &tev->point;
2587 if (ret == num_matched_functions) {
2588 pr_warning("Too many symbols are listed. Skip it.\n");
2589 break;
2590 }
2591 ret++;
2592
2593 if (pp->offset > sym->end - sym->start) {
2594 pr_warning("Offset %ld is bigger than the size of %s\n",
2595 pp->offset, sym->name);
2596 ret = -ENOENT;
2597 goto err_out;
2598 }
2599 /* Add one probe point */
2600 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002601
2602 /* Check the kprobe (not in module) is within .text */
2603 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002604 kprobe_warn_out_range(sym->name, tp->address)) {
2605 tp->symbol = NULL; /* Skip it */
2606 skipped++;
2607 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002608 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2609 tp->offset = tp->address - reloc_sym->addr;
2610 } else {
2611 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2612 tp->offset = pp->offset;
2613 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002614 tp->realname = strdup_or_goto(sym->name, nomem_out);
2615
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002616 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302617 if (pev->target) {
2618 if (pev->uprobes) {
2619 tev->point.module = strdup_or_goto(pev->target,
2620 nomem_out);
2621 } else {
2622 mod_name = find_module_name(pev->target);
2623 tev->point.module =
2624 strdup(mod_name ? mod_name : pev->target);
2625 free(mod_name);
2626 if (!tev->point.module)
2627 goto nomem_out;
2628 }
2629 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002630 tev->uprobes = pev->uprobes;
2631 tev->nargs = pev->nargs;
2632 if (tev->nargs) {
2633 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2634 tev->nargs);
2635 if (tev->args == NULL)
2636 goto nomem_out;
2637 }
2638 for (i = 0; i < tev->nargs; i++) {
2639 if (pev->args[i].name)
2640 tev->args[i].name =
2641 strdup_or_goto(pev->args[i].name,
2642 nomem_out);
2643
2644 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2645 nomem_out);
2646 if (pev->args[i].type)
2647 tev->args[i].type =
2648 strdup_or_goto(pev->args[i].type,
2649 nomem_out);
2650 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302651 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002652 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002653 if (ret == skipped) {
2654 ret = -ENOENT;
2655 goto err_out;
2656 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002657
2658out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002659 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002660 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002661 return ret;
2662
2663nomem_out:
2664 ret = -ENOMEM;
2665err_out:
2666 clear_probe_trace_events(*tevs, num_matched_functions);
2667 zfree(tevs);
2668 goto out;
2669}
2670
Wang Nanda15bd92015-08-26 10:57:45 +00002671static int try_to_find_absolute_address(struct perf_probe_event *pev,
2672 struct probe_trace_event **tevs)
2673{
2674 struct perf_probe_point *pp = &pev->point;
2675 struct probe_trace_event *tev;
2676 struct probe_trace_point *tp;
2677 int i, err;
2678
2679 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2680 return -EINVAL;
2681 if (perf_probe_event_need_dwarf(pev))
2682 return -EINVAL;
2683
2684 /*
2685 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2686 * absolute address.
2687 *
2688 * Only one tev can be generated by this.
2689 */
2690 *tevs = zalloc(sizeof(*tev));
2691 if (!*tevs)
2692 return -ENOMEM;
2693
2694 tev = *tevs;
2695 tp = &tev->point;
2696
2697 /*
2698 * Don't use tp->offset, use address directly, because
2699 * in synthesize_probe_trace_command() address cannot be
2700 * zero.
2701 */
2702 tp->address = pev->point.abs_address;
2703 tp->retprobe = pp->retprobe;
2704 tev->uprobes = pev->uprobes;
2705
2706 err = -ENOMEM;
2707 /*
2708 * Give it a '0x' leading symbol name.
2709 * In __add_probe_trace_events, a NULL symbol is interpreted as
2710 * invalud.
2711 */
2712 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2713 goto errout;
2714
2715 /* For kprobe, check range */
2716 if ((!tev->uprobes) &&
2717 (kprobe_warn_out_range(tev->point.symbol,
2718 tev->point.address))) {
2719 err = -EACCES;
2720 goto errout;
2721 }
2722
2723 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2724 goto errout;
2725
2726 if (pev->target) {
2727 tp->module = strdup(pev->target);
2728 if (!tp->module)
2729 goto errout;
2730 }
2731
2732 if (tev->group) {
2733 tev->group = strdup(pev->group);
2734 if (!tev->group)
2735 goto errout;
2736 }
2737
2738 if (pev->event) {
2739 tev->event = strdup(pev->event);
2740 if (!tev->event)
2741 goto errout;
2742 }
2743
2744 tev->nargs = pev->nargs;
2745 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2746 if (!tev->args) {
2747 err = -ENOMEM;
2748 goto errout;
2749 }
2750 for (i = 0; i < tev->nargs; i++)
2751 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2752
2753 return 1;
2754
2755errout:
2756 if (*tevs) {
2757 clear_probe_trace_events(*tevs, 1);
2758 *tevs = NULL;
2759 }
2760 return err;
2761}
2762
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302763bool __weak arch__prefers_symtab(void) { return false; }
2764
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302765static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002766 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002767{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002768 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002769
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09002770 if (!pev->group) {
2771 /* Set group name if not given */
2772 if (!pev->uprobes) {
2773 pev->group = strdup(PERFPROBE_GROUP);
2774 ret = pev->group ? 0 : -ENOMEM;
2775 } else
2776 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002777 if (ret != 0) {
2778 pr_warning("Failed to make a group name.\n");
2779 return ret;
2780 }
2781 }
2782
Wang Nanda15bd92015-08-26 10:57:45 +00002783 ret = try_to_find_absolute_address(pev, tevs);
2784 if (ret > 0)
2785 return ret;
2786
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302787 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002788 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302789 if (ret > 0)
2790 return ret; /* Found in symbol table */
2791 }
2792
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002793 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002794 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002795 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002796 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002797
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002798 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002799}
2800
Wang Nan12fae5e2015-09-04 21:16:00 +09002801int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002802{
Namhyung Kim844dffa2015-09-04 21:15:59 +09002803 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002804
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002805 /* Loop 1: convert all events */
2806 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09002807 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09002808 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09002809 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002810 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09002811 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002812 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002813 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09002814 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002815 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002816 /* This just release blacklist only if allocated */
2817 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002818
Namhyung Kim844dffa2015-09-04 21:15:59 +09002819 return 0;
2820}
2821
Wang Nan12fae5e2015-09-04 21:16:00 +09002822int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002823{
2824 int i, ret = 0;
2825
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002826 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002827 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002828 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2829 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002830 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002831 if (ret < 0)
2832 break;
2833 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002834 return ret;
2835}
2836
Wang Nan12fae5e2015-09-04 21:16:00 +09002837void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09002838{
2839 int i, j;
2840
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002841 /* Loop 3: cleanup and free trace events */
2842 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09002843 for (j = 0; j < pevs[i].ntevs; j++)
2844 clear_probe_trace_event(&pevs[i].tevs[j]);
2845 zfree(&pevs[i].tevs);
2846 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09002847 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002848 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09002849}
2850
2851int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2852{
2853 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09002854
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002855 ret = init_probe_symbol_maps(pevs->uprobes);
2856 if (ret < 0)
2857 return ret;
2858
Wang Nan12fae5e2015-09-04 21:16:00 +09002859 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002860 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09002861 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09002862
Wang Nan12fae5e2015-09-04 21:16:00 +09002863 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002864
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002865 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002866 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002867}
2868
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002869int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002870{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002871 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002872 char *str = strfilter__string(filter);
2873
2874 if (!str)
2875 return -EINVAL;
2876
Masami Hiramatsufa282442009-12-08 17:03:23 -05002877 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002878 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2879 if (ret < 0)
2880 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302881
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002882 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002883 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302884 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002885
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002886 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002887 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002888 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002889 goto error;
2890 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002891 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302892
2893error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002894 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302895 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002896 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302897 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002898out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002899 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002900
2901 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002902}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302903
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002904/* TODO: don't use a global variable for filter ... */
2905static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002906
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002907/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002908 * If a symbol corresponds to a function with global binding and
2909 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002910 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002911static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002912 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002913{
Namhyung Kime578da32015-03-06 16:31:29 +09002914 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002915 return 0;
2916 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002917}
2918
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002919int show_available_funcs(const char *target, struct strfilter *_filter,
2920 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002921{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002922 struct map *map;
2923 int ret;
2924
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002925 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002926 if (ret < 0)
2927 return ret;
2928
2929 /* Get a symbol map */
2930 if (user)
2931 map = dso__new_map(target);
2932 else
2933 map = kernel_get_module_map(target);
2934 if (!map) {
2935 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002936 return -EINVAL;
2937 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002938
2939 /* Load symbols with given filter */
2940 available_func_filter = _filter;
2941 if (map__load(map, filter_available_functions)) {
2942 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2943 goto end;
2944 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002945 if (!dso__sorted_by_name(map->dso, map->type))
2946 dso__sort_by_name(map->dso, map->type);
2947
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002948 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302949 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002950 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2951end:
2952 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002953 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002954 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002955 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302956
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002957 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302958}
2959
Wang Nanda15bd92015-08-26 10:57:45 +00002960int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2961 struct perf_probe_arg *pvar)
2962{
2963 tvar->value = strdup(pvar->var);
2964 if (tvar->value == NULL)
2965 return -ENOMEM;
2966 if (pvar->type) {
2967 tvar->type = strdup(pvar->type);
2968 if (tvar->type == NULL)
2969 return -ENOMEM;
2970 }
2971 if (pvar->name) {
2972 tvar->name = strdup(pvar->name);
2973 if (tvar->name == NULL)
2974 return -ENOMEM;
2975 } else
2976 tvar->name = NULL;
2977 return 0;
2978}