blob: 8a1e9e67614f415955c92f89dacec5deec24d800 [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 Hiramatsuee45b6c2014-02-06 05:32:04 +000070static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040071
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090072/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090073int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040074{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040075 int ret;
76
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090078 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090079 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 if (ret < 0) {
81 pr_debug("Failed to init symbol map.\n");
82 goto out;
83 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040084
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000085 if (host_machine || user_only) /* already initialized */
86 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030087
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000088 if (symbol_conf.vmlinux_name)
89 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90
91 host_machine = machine__new_host();
92 if (!host_machine) {
93 pr_debug("machine__new_host() failed.\n");
94 symbol__exit();
95 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090096 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040097out:
98 if (ret < 0)
99 pr_warning("Failed to init vmlinux path.\n");
100 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400101}
102
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900103void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000104{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300105 machine__delete(host_machine);
106 host_machine = NULL;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107 symbol__exit();
108}
109
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110static struct symbol *__find_kernel_function_by_name(const char *name,
111 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400112{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000113 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900114 NULL);
115}
116
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000117static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
118{
119 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
120}
121
122static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
123{
124 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
125 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300126 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000127
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300128 if (map__load(map, NULL) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000129 return NULL;
130
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300131 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000132 if (!kmap)
133 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000134 return kmap->ref_reloc_sym;
135}
136
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900137static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
138 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000139{
140 struct ref_reloc_sym *reloc_sym;
141 struct symbol *sym;
142 struct map *map;
143
144 /* ref_reloc_sym is just a label. Need a special fix*/
145 reloc_sym = kernel_get_ref_reloc_sym();
146 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900147 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000148 else {
149 sym = __find_kernel_function_by_name(name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900150 if (!sym)
151 return -ENOENT;
152 *addr = map->unmap_ip(map, sym->start) -
153 ((reloc) ? 0 : map->reloc) -
154 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000161 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300162 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300163 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900164
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300167 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900168
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900169 if (!module)
170 module = "kernel";
171
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300173 /* short_name is "[module]" */
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900174 if (strncmp(pos->dso->short_name + 1, module,
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300175 pos->dso->short_name_len - 2) == 0 &&
176 module[pos->dso->short_name_len - 2] == '\0') {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900177 return pos;
178 }
179 }
180 return NULL;
181}
182
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500183struct map *get_target_map(const char *target, bool user)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900184{
185 /* Init maps of given executable or kernel */
186 if (user)
187 return dso__new_map(target);
188 else
189 return kernel_get_module_map(target);
190}
191
192static void put_target_map(struct map *map, bool user)
193{
194 if (map && user) {
195 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300196 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900197 }
198}
199
200
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000201static int convert_exec_to_group(const char *exec, char **result)
202{
203 char *ptr1, *ptr2, *exec_copy;
204 char buf[64];
205 int ret;
206
207 exec_copy = strdup(exec);
208 if (!exec_copy)
209 return -ENOMEM;
210
211 ptr1 = basename(exec_copy);
212 if (!ptr1) {
213 ret = -EINVAL;
214 goto out;
215 }
216
217 ptr2 = strpbrk(ptr1, "-._");
218 if (ptr2)
219 *ptr2 = '\0';
220 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
221 if (ret < 0)
222 goto out;
223
224 *result = strdup(buf);
225 ret = *result ? 0 : -ENOMEM;
226
227out:
228 free(exec_copy);
229 return ret;
230}
231
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900232static void clear_perf_probe_point(struct perf_probe_point *pp)
233{
234 free(pp->file);
235 free(pp->function);
236 free(pp->lazy_line);
237}
238
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000239static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
240{
241 int i;
242
243 for (i = 0; i < ntevs; i++)
244 clear_probe_trace_event(tevs + i);
245}
246
Masami Hiramatsub0312202015-06-16 20:50:55 +0900247static bool kprobe_blacklist__listed(unsigned long address);
248static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
249{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900250 u64 etext_addr = 0;
251 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000252
Masami Hiramatsub0312202015-06-16 20:50:55 +0900253 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900254 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
255 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000256
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900257 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900258 pr_warning("%s is out of .text, skip it.\n", symbol);
259 else if (kprobe_blacklist__listed(address))
260 pr_warning("%s is blacklisted function, skip it.\n", symbol);
261 else
262 return false;
263
264 return true;
265}
266
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530267/*
268 * NOTE:
269 * '.gnu.linkonce.this_module' section of kernel module elf directly
270 * maps to 'struct module' from linux/module.h. This section contains
271 * actual module name which will be used by kernel after loading it.
272 * But, we cannot use 'struct module' here since linux/module.h is not
273 * exposed to user-space. Offset of 'name' has remained same from long
274 * time, so hardcoding it here.
275 */
276#ifdef __LP64__
277#define MOD_NAME_OFFSET 24
278#else
279#define MOD_NAME_OFFSET 12
280#endif
281
282/*
283 * @module can be module name of module file path. In case of path,
284 * inspect elf and find out what is actual module name.
285 * Caller has to free mod_name after using it.
286 */
287static char *find_module_name(const char *module)
288{
289 int fd;
290 Elf *elf;
291 GElf_Ehdr ehdr;
292 GElf_Shdr shdr;
293 Elf_Data *data;
294 Elf_Scn *sec;
295 char *mod_name = NULL;
296
297 fd = open(module, O_RDONLY);
298 if (fd < 0)
299 return NULL;
300
301 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
302 if (elf == NULL)
303 goto elf_err;
304
305 if (gelf_getehdr(elf, &ehdr) == NULL)
306 goto ret_err;
307
308 sec = elf_section_by_name(elf, &ehdr, &shdr,
309 ".gnu.linkonce.this_module", NULL);
310 if (!sec)
311 goto ret_err;
312
313 data = elf_getdata(sec, NULL);
314 if (!data || !data->d_buf)
315 goto ret_err;
316
317 mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
318
319ret_err:
320 elf_end(elf);
321elf_err:
322 close(fd);
323 return mod_name;
324}
325
Ingo Molnar89fe8082013-09-30 12:07:11 +0200326#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000327
328static int kernel_get_module_dso(const char *module, struct dso **pdso)
329{
330 struct dso *dso;
331 struct map *map;
332 const char *vmlinux_name;
333 int ret = 0;
334
335 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300336 char module_name[128];
337
338 snprintf(module_name, sizeof(module_name), "[%s]", module);
339 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
340 if (map) {
341 dso = map->dso;
342 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000343 }
344 pr_debug("Failed to find module %s.\n", module);
345 return -ENOENT;
346 }
347
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300348 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000349 dso = map->dso;
350
351 vmlinux_name = symbol_conf.vmlinux_name;
352 dso->load_errno = 0;
353 if (vmlinux_name)
354 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
355 else
356 ret = dso__load_vmlinux_path(dso, map, NULL);
357found:
358 *pdso = dso;
359 return ret;
360}
361
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900362/*
363 * Some binaries like glibc have special symbols which are on the symbol
364 * table, but not in the debuginfo. If we can find the address of the
365 * symbol from map, we can translate the address back to the probe point.
366 */
367static int find_alternative_probe_point(struct debuginfo *dinfo,
368 struct perf_probe_point *pp,
369 struct perf_probe_point *result,
370 const char *target, bool uprobes)
371{
372 struct map *map = NULL;
373 struct symbol *sym;
374 u64 address = 0;
375 int ret = -ENOENT;
376
377 /* This can work only for function-name based one */
378 if (!pp->function || pp->file)
379 return -ENOTSUP;
380
381 map = get_target_map(target, uprobes);
382 if (!map)
383 return -EINVAL;
384
385 /* Find the address of given function */
386 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900387 if (uprobes)
388 address = sym->start;
389 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900390 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900391 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900392 }
393 if (!address) {
394 ret = -ENOENT;
395 goto out;
396 }
Wang Nanf6c15622015-04-08 02:14:34 +0000397 pr_debug("Symbol %s address found : %" PRIx64 "\n",
398 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900399
400 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
401 result);
402 if (ret <= 0)
403 ret = (!ret) ? -ENOENT : ret;
404 else {
405 result->offset += pp->offset;
406 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800407 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900408 ret = 0;
409 }
410
411out:
412 put_target_map(map, uprobes);
413 return ret;
414
415}
416
417static int get_alternative_probe_event(struct debuginfo *dinfo,
418 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900419 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900420{
421 int ret;
422
423 memcpy(tmp, &pev->point, sizeof(*tmp));
424 memset(&pev->point, 0, sizeof(pev->point));
425 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900426 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900427 if (ret < 0)
428 memcpy(&pev->point, tmp, sizeof(*tmp));
429
430 return ret;
431}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000432
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900433static int get_alternative_line_range(struct debuginfo *dinfo,
434 struct line_range *lr,
435 const char *target, bool user)
436{
David Ahern6d4a4892015-03-11 10:36:20 -0400437 struct perf_probe_point pp = { .function = lr->function,
438 .file = lr->file,
439 .line = lr->start };
440 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900441 int ret, len = 0;
442
David Ahern6d4a4892015-03-11 10:36:20 -0400443 memset(&result, 0, sizeof(result));
444
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900445 if (lr->end != INT_MAX)
446 len = lr->end - lr->start;
447 ret = find_alternative_probe_point(dinfo, &pp, &result,
448 target, user);
449 if (!ret) {
450 lr->function = result.function;
451 lr->file = result.file;
452 lr->start = result.line;
453 if (lr->end != INT_MAX)
454 lr->end = lr->start + len;
455 clear_perf_probe_point(&pp);
456 }
457 return ret;
458}
459
Masami Hiramatsuff741782011-06-27 16:27:39 +0900460/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000461static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900462{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000463 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900464 char reason[STRERR_BUFSIZE];
465 struct debuginfo *ret = NULL;
466 struct dso *dso = NULL;
467 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900468
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000469 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900470 err = kernel_get_module_dso(module, &dso);
471 if (err < 0) {
472 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300473 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900474 strcpy(reason, "(unknown)");
475 } else
476 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000477 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900478 pr_err("Failed to find the path for %s: %s\n",
479 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900480 return NULL;
481 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900482 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900483 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000484 ret = debuginfo__new(path);
485 if (!ret && !silent) {
486 pr_warning("The %s file has no debug information.\n", path);
487 if (!module || !strtailcmp(path, ".ko"))
488 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
489 else
490 pr_warning("Rebuild with -g, ");
491 pr_warning("or install an appropriate debuginfo package.\n");
492 }
493 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400494}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300495
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900496/* For caching the last debuginfo */
497static struct debuginfo *debuginfo_cache;
498static char *debuginfo_cache_path;
499
500static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
501{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900502 const char *path = module;
503
504 /* If the module is NULL, it should be the kernel. */
505 if (!module)
506 path = "kernel";
507
508 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900509 goto out;
510
511 /* Copy module path */
512 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900513 debuginfo_cache_path = strdup(path);
514 if (!debuginfo_cache_path) {
515 debuginfo__delete(debuginfo_cache);
516 debuginfo_cache = NULL;
517 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900518 }
519
520 debuginfo_cache = open_debuginfo(module, silent);
521 if (!debuginfo_cache)
522 zfree(&debuginfo_cache_path);
523out:
524 return debuginfo_cache;
525}
526
527static void debuginfo_cache__exit(void)
528{
529 debuginfo__delete(debuginfo_cache);
530 debuginfo_cache = NULL;
531 zfree(&debuginfo_cache_path);
532}
533
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000534
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000535static int get_text_start_address(const char *exec, unsigned long *address)
536{
537 Elf *elf;
538 GElf_Ehdr ehdr;
539 GElf_Shdr shdr;
540 int fd, ret = -ENOENT;
541
542 fd = open(exec, O_RDONLY);
543 if (fd < 0)
544 return -errno;
545
546 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900547 if (elf == NULL) {
548 ret = -EINVAL;
549 goto out_close;
550 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000551
552 if (gelf_getehdr(elf, &ehdr) == NULL)
553 goto out;
554
555 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
556 goto out;
557
558 *address = shdr.sh_addr - shdr.sh_offset;
559 ret = 0;
560out:
561 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900562out_close:
563 close(fd);
564
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000565 return ret;
566}
567
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000568/*
569 * Convert trace point to probe point with debuginfo
570 */
571static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
572 struct perf_probe_point *pp,
573 bool is_kprobe)
574{
575 struct debuginfo *dinfo = NULL;
576 unsigned long stext = 0;
577 u64 addr = tp->address;
578 int ret = -ENOENT;
579
580 /* convert the address to dwarf address */
581 if (!is_kprobe) {
582 if (!addr) {
583 ret = -EINVAL;
584 goto error;
585 }
586 ret = get_text_start_address(tp->module, &stext);
587 if (ret < 0)
588 goto error;
589 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000590 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900591 /* If the module is given, this returns relative address */
592 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
593 false, !!tp->module);
594 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000595 goto error;
596 addr += tp->offset;
597 }
598
599 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
600 tp->module ? : "kernel");
601
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900602 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
603 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000604 ret = debuginfo__find_probe_point(dinfo,
605 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900606 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000607 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000608
609 if (ret > 0) {
610 pp->retprobe = tp->retprobe;
611 return 0;
612 }
613error:
614 pr_debug("Failed to find corresponding probes from debuginfo.\n");
615 return ret ? : -ENOENT;
616}
617
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000618static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
619 int ntevs, const char *exec)
620{
621 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000622 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000623
624 if (!exec)
625 return 0;
626
627 ret = get_text_start_address(exec, &stext);
628 if (ret < 0)
629 return ret;
630
631 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000632 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000633 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000634 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000635 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000636 ret = -ENOMEM;
637 break;
638 }
639 tevs[i].uprobes = true;
640 }
641
642 return ret;
643}
644
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900645static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
646 int ntevs, const char *module)
647{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900648 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530649 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900650
651 if (!module)
652 return 0;
653
Ravi Bangoria63a29612016-04-26 19:55:41 +0530654 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900655
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900656 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530657 tevs[i].point.module =
658 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900659 if (!tevs[i].point.module) {
660 ret = -ENOMEM;
661 break;
662 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900663 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900664
Ravi Bangoria63a29612016-04-26 19:55:41 +0530665 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900666 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900667}
668
Ravi Bangoriad8204562016-08-09 01:23:24 -0500669static int
670post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
671 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000672{
673 struct ref_reloc_sym *reloc_sym;
674 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900675 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000676
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900677 /* Skip post process if the target is an offline kernel */
678 if (symbol_conf.ignore_vmlinux_buildid)
679 return 0;
680
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000681 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000682 if (!reloc_sym) {
683 pr_warning("Relocated base symbol is not found!\n");
684 return -EINVAL;
685 }
686
687 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900688 if (!tevs[i].point.address || tevs[i].point.retprobe)
689 continue;
690 /* If we found a wrong one, mark it by NULL symbol */
691 if (kprobe_warn_out_range(tevs[i].point.symbol,
692 tevs[i].point.address)) {
693 tmp = NULL;
694 skipped++;
695 } else {
696 tmp = strdup(reloc_sym->name);
697 if (!tmp)
698 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000699 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900700 /* If we have no realname, use symbol for it */
701 if (!tevs[i].point.realname)
702 tevs[i].point.realname = tevs[i].point.symbol;
703 else
704 free(tevs[i].point.symbol);
705 tevs[i].point.symbol = tmp;
706 tevs[i].point.offset = tevs[i].point.address -
707 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000708 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900709 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000710}
711
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500712void __weak
713arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
714 int ntevs __maybe_unused)
715{
716}
717
Ravi Bangoriad8204562016-08-09 01:23:24 -0500718/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500719static int post_process_probe_trace_events(struct perf_probe_event *pev,
720 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500721 int ntevs, const char *module,
722 bool uprobe)
723{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500724 int ret;
725
Ravi Bangoriad8204562016-08-09 01:23:24 -0500726 if (uprobe)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500727 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
728 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500729 /* Currently ref_reloc_sym based probe is not for drivers */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500730 ret = add_module_to_probe_trace_events(tevs, ntevs, module);
731 else
732 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500733
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500734 if (ret >= 0)
735 arch__post_process_probe_trace_events(pev, ntevs);
736
737 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500738}
739
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300740/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530741static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900742 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300743{
744 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900745 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530746 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900747 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300748
Masami Hiramatsu44225522015-05-08 10:03:28 +0900749 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900750 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000751 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900752 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900753 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300754 return 0;
755 }
756
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000757 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900758 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900759 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900760
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900761 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900762 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900763 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900764 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900765 /*
766 * Write back to the original probe_event for
767 * setting appropriate (user given) event name
768 */
769 clear_perf_probe_point(&pev->point);
770 memcpy(&pev->point, &tmp, sizeof(tmp));
771 }
772 }
773
Masami Hiramatsuff741782011-06-27 16:27:39 +0900774 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300775
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400776 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000777 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500778 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900779 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900780 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000781 clear_probe_trace_events(*tevs, ntevs);
782 zfree(tevs);
783 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900784 if (ret != ntevs)
785 return ret < 0 ? ret : ntevs;
786 ntevs = 0;
787 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400788 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300789
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400790 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900791 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400792 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900793 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400794 }
795 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400796 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000797 if (ntevs < 0) {
798 if (ntevs == -EBADF)
799 pr_warning("Warning: No dwarf info found in the vmlinux - "
800 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400801 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900802 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400803 return 0;
804 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300805 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400806 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300807}
808
809#define LINEBUF_SIZE 256
810#define NR_ADDITIONAL_LINES 2
811
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100812static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300813{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000814 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100815 const char *color = show_num ? "" : PERF_COLOR_BLUE;
816 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300817
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100818 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300819 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
820 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100821 if (skip)
822 continue;
823 if (!prefix) {
824 prefix = show_num ? "%7d " : " ";
825 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300826 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100827 color_fprintf(stdout, color, "%s", buf);
828
829 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400830
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100831 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300832error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100833 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000834 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300835 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100836 return -1;
837 }
838 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300839}
840
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100841static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
842{
843 int rv = __show_one_line(fp, l, skip, show_num);
844 if (rv == 0) {
845 pr_warning("Source file is shorter than expected.\n");
846 rv = -1;
847 }
848 return rv;
849}
850
851#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
852#define show_one_line(f,l) _show_one_line(f,l,false,false)
853#define skip_one_line(f,l) _show_one_line(f,l,true,false)
854#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
855
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300856/*
857 * Show line-range always requires debuginfo to find source file and
858 * line number.
859 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900860static int __show_line_range(struct line_range *lr, const char *module,
861 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300862{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400863 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000864 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900865 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300866 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900867 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900868 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000869 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300870
871 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000872 dinfo = open_debuginfo(module, false);
873 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900874 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400875
Masami Hiramatsuff741782011-06-27 16:27:39 +0900876 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900877 if (!ret) { /* Not found, retry with an alternative */
878 ret = get_alternative_line_range(dinfo, lr, module, user);
879 if (!ret)
880 ret = debuginfo__find_line_range(dinfo, lr);
881 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900882 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000883 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400884 pr_warning("Specified source line is not found.\n");
885 return -ENOENT;
886 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000887 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400888 return ret;
889 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300890
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900891 /* Convert source file path */
892 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900893 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800894
895 /* Free old path when new path is assigned */
896 if (tmp != lr->path)
897 free(tmp);
898
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900899 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000900 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900901 return ret;
902 }
903
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300904 setup_pager();
905
906 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900907 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300908 lr->start - lr->offset);
909 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100910 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300911
912 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400913 if (fp == NULL) {
914 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300915 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400916 return -errno;
917 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300918 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100919 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100920 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100921 if (ret < 0)
922 goto end;
923 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300924
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300925 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000926 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100927 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100928 if (ret < 0)
929 goto end;
930 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100931 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400932 if (ret < 0)
933 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300934 }
935
936 if (lr->end == INT_MAX)
937 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100938 while (l <= lr->end) {
939 ret = show_one_line_or_eof(fp, l++ - lr->offset);
940 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100941 break;
942 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400943end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300944 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400945 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300946}
947
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000948int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000949{
950 int ret;
951
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900952 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000953 if (ret < 0)
954 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900955 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900956 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000957
958 return ret;
959}
960
Masami Hiramatsuff741782011-06-27 16:27:39 +0900961static int show_available_vars_at(struct debuginfo *dinfo,
962 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900963 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900964{
965 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900966 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900967 struct str_node *node;
968 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900969 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900970 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900971
972 buf = synthesize_perf_probe_point(&pev->point);
973 if (!buf)
974 return -EINVAL;
975 pr_debug("Searching variables at %s\n", buf);
976
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900977 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900978 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900979 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900980 if (!ret) {
981 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900982 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900983 /* Release the old probe_point */
984 clear_perf_probe_point(&tmp);
985 }
986 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900987 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000988 if (ret == 0 || ret == -ENOENT) {
989 pr_err("Failed to find the address of %s\n", buf);
990 ret = -ENOENT;
991 } else
992 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900993 goto end;
994 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000995
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900996 /* Some variables are found */
997 fprintf(stdout, "Available variables at %s\n", buf);
998 for (i = 0; i < ret; i++) {
999 vl = &vls[i];
1000 /*
1001 * A probe point might be converted to
1002 * several trace points.
1003 */
1004 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1005 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001006 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001007 nvars = 0;
1008 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001009 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001010 var = strchr(node->s, '\t') + 1;
1011 if (strfilter__compare(_filter, var)) {
1012 fprintf(stdout, "\t\t%s\n", node->s);
1013 nvars++;
1014 }
1015 }
1016 strlist__delete(vl->vars);
1017 }
1018 if (nvars == 0)
1019 fprintf(stdout, "\t\t(No matched variables)\n");
1020 }
1021 free(vls);
1022end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001023 free(buf);
1024 return ret;
1025}
1026
1027/* Show available variables on given probe point */
1028int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001029 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001030{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001031 int i, ret = 0;
1032 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001033
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001034 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001035 if (ret < 0)
1036 return ret;
1037
Masami Hiramatsu44225522015-05-08 10:03:28 +09001038 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001039 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001040 ret = -ENOENT;
1041 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001042 }
1043
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001044 setup_pager();
1045
Masami Hiramatsuff741782011-06-27 16:27:39 +09001046 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001047 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001048
1049 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001050out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001051 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001052 return ret;
1053}
1054
Ingo Molnar89fe8082013-09-30 12:07:11 +02001055#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001056
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001057static void debuginfo_cache__exit(void)
1058{
1059}
1060
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001061static int
1062find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1063 struct perf_probe_point *pp __maybe_unused,
1064 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001065{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001066 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001067}
1068
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301069static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001070 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001071{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001072 if (perf_probe_event_need_dwarf(pev)) {
1073 pr_warning("Debuginfo-analysis is not supported.\n");
1074 return -ENOSYS;
1075 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301076
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001077 return 0;
1078}
1079
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001080int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001081 const char *module __maybe_unused,
1082 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001083{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001084 pr_warning("Debuginfo-analysis is not supported.\n");
1085 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001086}
1087
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001088int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001089 int npevs __maybe_unused,
1090 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001091{
1092 pr_warning("Debuginfo-analysis is not supported.\n");
1093 return -ENOSYS;
1094}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001095#endif
1096
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001097void line_range__clear(struct line_range *lr)
1098{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001099 free(lr->function);
1100 free(lr->file);
1101 free(lr->path);
1102 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001103 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001104 memset(lr, 0, sizeof(*lr));
1105}
1106
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001107int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001108{
1109 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001110 lr->line_list = intlist__new(NULL);
1111 if (!lr->line_list)
1112 return -ENOMEM;
1113 else
1114 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001115}
1116
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001117static int parse_line_num(char **ptr, int *val, const char *what)
1118{
1119 const char *start = *ptr;
1120
1121 errno = 0;
1122 *val = strtol(*ptr, ptr, 0);
1123 if (errno || *ptr == start) {
1124 semantic_error("'%s' is not a valid number.\n", what);
1125 return -EINVAL;
1126 }
1127 return 0;
1128}
1129
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001130/* Check the name is good for event, group or function */
1131static bool is_c_func_name(const char *name)
1132{
1133 if (!isalpha(*name) && *name != '_')
1134 return false;
1135 while (*++name != '\0') {
1136 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1137 return false;
1138 }
1139 return true;
1140}
1141
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001142/*
1143 * Stuff 'lr' according to the line range described by 'arg'.
1144 * The line range syntax is described by:
1145 *
1146 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001147 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001148 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001149int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001150{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001151 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001152 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001153
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001154 if (!name)
1155 return -ENOMEM;
1156
1157 lr->start = 0;
1158 lr->end = INT_MAX;
1159
1160 range = strchr(name, ':');
1161 if (range) {
1162 *range++ = '\0';
1163
1164 err = parse_line_num(&range, &lr->start, "start line");
1165 if (err)
1166 goto err;
1167
1168 if (*range == '+' || *range == '-') {
1169 const char c = *range++;
1170
1171 err = parse_line_num(&range, &lr->end, "end line");
1172 if (err)
1173 goto err;
1174
1175 if (c == '+') {
1176 lr->end += lr->start;
1177 /*
1178 * Adjust the number of lines here.
1179 * If the number of lines == 1, the
1180 * the end of line should be equal to
1181 * the start of line.
1182 */
1183 lr->end--;
1184 }
1185 }
1186
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001187 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001188
1189 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001190 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001191 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001193 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001195 if (*range != '\0') {
1196 semantic_error("Tailing with invalid str '%s'.\n", range);
1197 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001199 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001200
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001201 file = strchr(name, '@');
1202 if (file) {
1203 *file = '\0';
1204 lr->file = strdup(++file);
1205 if (lr->file == NULL) {
1206 err = -ENOMEM;
1207 goto err;
1208 }
1209 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001210 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001211 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001212 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001213 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001214 else { /* Invalid name */
1215 semantic_error("'%s' is not a valid function name.\n", name);
1216 err = -EINVAL;
1217 goto err;
1218 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219
1220 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001221err:
1222 free(name);
1223 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001224}
1225
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001226static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1227{
1228 char *ptr;
1229
1230 ptr = strchr(*arg, ':');
1231 if (ptr) {
1232 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001233 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001234 goto ng_name;
1235 pev->group = strdup(*arg);
1236 if (!pev->group)
1237 return -ENOMEM;
1238 *arg = ptr + 1;
1239 } else
1240 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001241 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001242ng_name:
1243 semantic_error("%s is bad for event name -it must "
1244 "follow C symbol-naming rule.\n", *arg);
1245 return -EINVAL;
1246 }
1247 pev->event = strdup(*arg);
1248 if (pev->event == NULL)
1249 return -ENOMEM;
1250
1251 return 0;
1252}
1253
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001254/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001255static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001256{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001257 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001258 char *ptr, *tmp;
1259 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301260 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001261 int ret;
1262
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001263 /*
1264 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001265 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1266 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001267 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001268 */
Wang Nane59d29e2015-04-28 08:46:09 +00001269 if (!arg)
1270 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001271
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001272 /*
1273 * If the probe point starts with '%',
1274 * or starts with "sdt_" and has a ':' but no '=',
1275 * then it should be a SDT/cached probe point.
1276 */
1277 if (arg[0] == '%' ||
1278 (!strncmp(arg, "sdt_", 4) &&
1279 !!strchr(arg, ':') && !strchr(arg, '='))) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001280 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001281 if (arg[0] == '%')
1282 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001283 }
1284
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001285 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001286 if (pev->sdt) {
1287 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001288 if (*ptr != '@') {
1289 semantic_error("%s must be an SDT name.\n",
1290 arg);
1291 return -EINVAL;
1292 }
1293 /* This must be a target file name or build id */
1294 tmp = build_id_cache__complement(ptr + 1);
1295 if (tmp) {
1296 pev->target = build_id_cache__origname(tmp);
1297 free(tmp);
1298 } else
1299 pev->target = strdup(ptr + 1);
1300 if (!pev->target)
1301 return -ENOMEM;
1302 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001303 }
1304 ret = parse_perf_probe_event_name(&arg, pev);
1305 if (ret == 0) {
1306 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1307 ret = -errno;
1308 }
1309 return ret;
1310 }
1311
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001312 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001313 *ptr = '\0';
1314 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001315 ret = parse_perf_probe_event_name(&arg, pev);
1316 if (ret < 0)
1317 return ret;
1318
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001319 arg = tmp;
1320 }
1321
Naveen N. Rao3099c022015-04-28 17:35:34 +05301322 /*
1323 * Check arg is function or file name and copy it.
1324 *
1325 * We consider arg to be a file spec if and only if it satisfies
1326 * all of the below criteria::
1327 * - it does not include any of "+@%",
1328 * - it includes one of ":;", and
1329 * - it has a period '.' in the name.
1330 *
1331 * Otherwise, we consider arg to be a function specification.
1332 */
1333 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1334 /* This is a file spec if it includes a '.' before ; or : */
1335 if (memchr(arg, '.', ptr - arg))
1336 file_spec = true;
1337 }
1338
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001339 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001340 if (ptr) {
1341 nc = *ptr;
1342 *ptr++ = '\0';
1343 }
1344
Wang Nan6c6e0242015-08-26 10:57:44 +00001345 if (arg[0] == '\0')
1346 tmp = NULL;
1347 else {
1348 tmp = strdup(arg);
1349 if (tmp == NULL)
1350 return -ENOMEM;
1351 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001352
Naveen N. Rao3099c022015-04-28 17:35:34 +05301353 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001354 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001355 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001356 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001357
Wang Nanda15bd92015-08-26 10:57:45 +00001358 /*
1359 * Keep pp->function even if this is absolute address,
1360 * so it can mark whether abs_address is valid.
1361 * Which make 'perf probe lib.bin 0x0' possible.
1362 *
1363 * Note that checking length of tmp is not needed
1364 * because when we access tmp[1] we know tmp[0] is '0',
1365 * so tmp[1] should always valid (but could be '\0').
1366 */
1367 if (tmp && !strncmp(tmp, "0x", 2)) {
1368 pp->abs_address = strtoul(pp->function, &tmp, 0);
1369 if (*tmp != '\0') {
1370 semantic_error("Invalid absolute address.\n");
1371 return -EINVAL;
1372 }
1373 }
1374 }
1375
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001376 /* Parse other options */
1377 while (ptr) {
1378 arg = ptr;
1379 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001380 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001381 pp->lazy_line = strdup(arg);
1382 if (pp->lazy_line == NULL)
1383 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001384 break;
1385 }
1386 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001387 if (ptr) {
1388 nc = *ptr;
1389 *ptr++ = '\0';
1390 }
1391 switch (c) {
1392 case ':': /* Line number */
1393 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001394 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001395 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001396 " in line number.\n");
1397 return -EINVAL;
1398 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001399 break;
1400 case '+': /* Byte offset from a symbol */
1401 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001402 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001403 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404 " in offset.\n");
1405 return -EINVAL;
1406 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001407 break;
1408 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001409 if (pp->file) {
1410 semantic_error("SRC@SRC is not allowed.\n");
1411 return -EINVAL;
1412 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001413 pp->file = strdup(arg);
1414 if (pp->file == NULL)
1415 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001416 break;
1417 case '%': /* Probe places */
1418 if (strcmp(arg, "return") == 0) {
1419 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001420 } else { /* Others not supported yet */
1421 semantic_error("%%%s is not supported.\n", arg);
1422 return -ENOTSUP;
1423 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001424 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001425 default: /* Buggy case */
1426 pr_err("This program has a bug at %s:%d.\n",
1427 __FILE__, __LINE__);
1428 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001429 break;
1430 }
1431 }
1432
1433 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001434 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001435 semantic_error("Lazy pattern can't be used with"
1436 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 return -EINVAL;
1438 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001439
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001440 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001441 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001442 return -EINVAL;
1443 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001444
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001446 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001447 return -EINVAL;
1448 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001449
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001450 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001451 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001452 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001453 return -EINVAL;
1454 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001455
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001457 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001458 return -EINVAL;
1459 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001460
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001461 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001462 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001463 return -EINVAL;
1464 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001465
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001467 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001468 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001469 return -EINVAL;
1470 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001471
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001473 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1474 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001475 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001476}
1477
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001478/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001479static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001480{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001481 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001482 struct perf_probe_arg_field **fieldp;
1483
1484 pr_debug("parsing arg: %s into ", str);
1485
Masami Hiramatsu48481932010-04-12 13:16:53 -04001486 tmp = strchr(str, '=');
1487 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001488 arg->name = strndup(str, tmp - str);
1489 if (arg->name == NULL)
1490 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001491 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001492 str = tmp + 1;
1493 }
1494
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001495 tmp = strchr(str, ':');
1496 if (tmp) { /* Type setting */
1497 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001498 arg->type = strdup(tmp + 1);
1499 if (arg->type == NULL)
1500 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001501 pr_debug("type:%s ", arg->type);
1502 }
1503
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001504 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001505 if (!is_c_varname(str) || !tmp) {
1506 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001507 arg->var = strdup(str);
1508 if (arg->var == NULL)
1509 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001510 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001511 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001512 }
1513
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001514 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001515 arg->var = strndup(str, tmp - str);
1516 if (arg->var == NULL)
1517 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001518 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001519 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001520 fieldp = &arg->field;
1521
1522 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001523 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1524 if (*fieldp == NULL)
1525 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001526 if (*tmp == '[') { /* Array */
1527 str = tmp;
1528 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001529 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001530 if (*tmp != ']' || tmp == str + 1) {
1531 semantic_error("Array index must be a"
1532 " number.\n");
1533 return -EINVAL;
1534 }
1535 tmp++;
1536 if (*tmp == '\0')
1537 tmp = NULL;
1538 } else { /* Structure */
1539 if (*tmp == '.') {
1540 str = tmp + 1;
1541 (*fieldp)->ref = false;
1542 } else if (tmp[1] == '>') {
1543 str = tmp + 2;
1544 (*fieldp)->ref = true;
1545 } else {
1546 semantic_error("Argument parse error: %s\n",
1547 str);
1548 return -EINVAL;
1549 }
1550 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001551 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001552 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001553 (*fieldp)->name = strndup(str, tmp - str);
1554 if ((*fieldp)->name == NULL)
1555 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001556 if (*str != '[')
1557 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001558 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1559 fieldp = &(*fieldp)->next;
1560 }
1561 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001562 (*fieldp)->name = strdup(str);
1563 if ((*fieldp)->name == NULL)
1564 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001565 if (*str != '[')
1566 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001567 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001568
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001569 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001570 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001571 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001572 if (arg->name == NULL)
1573 return -ENOMEM;
1574 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001575 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001576}
1577
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001578/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001579int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001580{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001581 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001583
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001584 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001585 if (!argv) {
1586 pr_debug("Failed to split arguments.\n");
1587 return -ENOMEM;
1588 }
1589 if (argc - 1 > MAX_PROBE_ARGS) {
1590 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1591 ret = -ERANGE;
1592 goto out;
1593 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001594 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001595 ret = parse_perf_probe_point(argv[0], pev);
1596 if (ret < 0)
1597 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001598
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001599 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001600 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001601 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1602 if (pev->args == NULL) {
1603 ret = -ENOMEM;
1604 goto out;
1605 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001606 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1607 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1608 if (ret >= 0 &&
1609 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001610 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001611 " kretprobe.\n");
1612 ret = -EINVAL;
1613 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001614 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001615out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001616 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001617
1618 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001619}
1620
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001621/* Return true if this perf_probe_event requires debuginfo */
1622bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001623{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001624 int i;
1625
1626 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1627 return true;
1628
1629 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsuf6eb0512016-07-12 19:04:34 +09001630 if (is_c_varname(pev->args[i].var) ||
1631 !strcmp(pev->args[i].var, "$params") ||
1632 !strcmp(pev->args[i].var, "$vars"))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001633 return true;
1634
1635 return false;
1636}
1637
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301638/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001639int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001640{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301641 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001642 char pr;
1643 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001644 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001645 int ret, i, argc;
1646 char **argv;
1647
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301648 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001650 if (!argv) {
1651 pr_debug("Failed to split arguments.\n");
1652 return -ENOMEM;
1653 }
1654 if (argc < 2) {
1655 semantic_error("Too few probe arguments.\n");
1656 ret = -ERANGE;
1657 goto out;
1658 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001659
1660 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001661 argv0_str = strdup(argv[0]);
1662 if (argv0_str == NULL) {
1663 ret = -ENOMEM;
1664 goto out;
1665 }
1666 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1667 fmt2_str = strtok_r(NULL, "/", &fmt);
1668 fmt3_str = strtok_r(NULL, " \t", &fmt);
1669 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1670 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001671 semantic_error("Failed to parse event name: %s\n", argv[0]);
1672 ret = -EINVAL;
1673 goto out;
1674 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001675 pr = fmt1_str[0];
1676 tev->group = strdup(fmt2_str);
1677 tev->event = strdup(fmt3_str);
1678 if (tev->group == NULL || tev->event == NULL) {
1679 ret = -ENOMEM;
1680 goto out;
1681 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001682 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001683
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001684 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001685
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001686 /* Scan module name(if there), function name and offset */
1687 p = strchr(argv[1], ':');
1688 if (p) {
1689 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001690 if (!tp->module) {
1691 ret = -ENOMEM;
1692 goto out;
1693 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001694 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001695 p++;
1696 } else
1697 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001698 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001699 /* only the address started with 0x */
1700 if (fmt1_str[0] == '0') {
1701 /*
1702 * Fix a special case:
1703 * if address == 0, kernel reports something like:
1704 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1705 * Newer kernel may fix that, but we want to
1706 * support old kernel also.
1707 */
1708 if (strcmp(fmt1_str, "0x") == 0) {
1709 if (!argv[2] || strcmp(argv[2], "(null)")) {
1710 ret = -EINVAL;
1711 goto out;
1712 }
1713 tp->address = 0;
1714
1715 free(argv[2]);
1716 for (i = 2; argv[i + 1] != NULL; i++)
1717 argv[i] = argv[i + 1];
1718
1719 argv[i] = NULL;
1720 argc -= 1;
1721 } else
1722 tp->address = strtoul(fmt1_str, NULL, 0);
1723 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001724 /* Only the symbol-based probe has offset */
1725 tp->symbol = strdup(fmt1_str);
1726 if (tp->symbol == NULL) {
1727 ret = -ENOMEM;
1728 goto out;
1729 }
1730 fmt2_str = strtok_r(NULL, "", &fmt);
1731 if (fmt2_str == NULL)
1732 tp->offset = 0;
1733 else
1734 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001735 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001736
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001737 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301738 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001739 if (tev->args == NULL) {
1740 ret = -ENOMEM;
1741 goto out;
1742 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001743 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001744 p = strchr(argv[i + 2], '=');
1745 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001746 *p++ = '\0';
1747 else
1748 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001749 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001750 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001751 tev->args[i].value = strdup(p);
1752 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1753 ret = -ENOMEM;
1754 goto out;
1755 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001756 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001757 ret = 0;
1758out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001759 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001760 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001761 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001762}
1763
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001764/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001765char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001766{
1767 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001768 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001769 char *ret = NULL;
1770 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001771
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001772 if (strbuf_init(&buf, 64) < 0)
1773 return NULL;
1774
Masami Hiramatsu48481932010-04-12 13:16:53 -04001775 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001776 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001777 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001778 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1779 if (err)
1780 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001781
1782 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001783 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001784 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001785 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001786 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1787 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001788 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001789 if (err)
1790 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001791 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001792
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001793 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001794 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1795 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001796
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001797 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001798out:
1799 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001800 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001801}
1802
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001803/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001804char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001805{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001806 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001807 char *tmp, *ret = NULL;
1808 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001809
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001810 if (strbuf_init(&buf, 64) < 0)
1811 return NULL;
1812
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001813 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001814 if (strbuf_addstr(&buf, pp->function) < 0)
1815 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001816 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001817 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001818 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001819 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001820 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001821 err = strbuf_addstr(&buf, "%return");
1822 if (err)
1823 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001824 }
1825 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001826 tmp = pp->file;
1827 len = strlen(tmp);
1828 if (len > 30) {
1829 tmp = strchr(pp->file + len - 30, '/');
1830 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1831 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001832 err = strbuf_addf(&buf, "@%s", tmp);
1833 if (!err && !pp->function && pp->line)
1834 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001835 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001836 if (!err)
1837 ret = strbuf_detach(&buf, NULL);
1838out:
1839 strbuf_release(&buf);
1840 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001841}
1842
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001843char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1844{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001845 struct strbuf buf;
1846 char *tmp, *ret = NULL;
1847 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001848
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001849 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001850 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001851 if (pev->event)
1852 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1853 pev->event) < 0)
1854 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001856 tmp = synthesize_perf_probe_point(&pev->point);
1857 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1858 goto out;
1859 free(tmp);
1860
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001861 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001862 tmp = synthesize_perf_probe_arg(pev->args + i);
1863 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1864 goto out;
1865 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866 }
1867
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001868 ret = strbuf_detach(&buf, NULL);
1869out:
1870 strbuf_release(&buf);
1871 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001872}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301874static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001875 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001876{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001877 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001878 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301879 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001880 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001881 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001882 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001884 err = strbuf_addf(buf, "%+ld(", ref->offset);
1885 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001886}
1887
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301888static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001889 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301891 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001892 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893
1894 /* Argument name or separator */
1895 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001896 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001897 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001898 err = strbuf_addch(buf, ' ');
1899 if (err)
1900 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001901
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001902 /* Special case: @XXX */
1903 if (arg->value[0] == '@' && arg->ref)
1904 ref = ref->next;
1905
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001906 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001907 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001908 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001909 if (depth < 0)
1910 return depth;
1911 }
1912
1913 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001914 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001915 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001916 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001917 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001918
1919 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001920 while (!err && depth--)
1921 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001922
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001923 /* Print argument type */
1924 if (!err && arg->type)
1925 err = strbuf_addf(buf, ":%s", arg->type);
1926
1927 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001928}
1929
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301930char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001931{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301932 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001933 struct strbuf buf;
1934 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001935 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001936
Wang Nanda15bd92015-08-26 10:57:45 +00001937 /* Uprobes must have tp->module */
1938 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001939 return NULL;
1940
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001941 if (strbuf_init(&buf, 32) < 0)
1942 return NULL;
1943
1944 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1945 tev->group, tev->event) < 0)
1946 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001947 /*
1948 * If tp->address == 0, then this point must be a
1949 * absolute address uprobe.
1950 * try_to_find_absolute_address() should have made
1951 * tp->symbol to "0x0".
1952 */
1953 if (tev->uprobes && !tp->address) {
1954 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1955 goto error;
1956 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001957
1958 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301959 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001960 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001961 else if (!strncmp(tp->symbol, "0x", 2))
1962 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001963 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1964 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301965 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001966 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1967 tp->module ? ":" : "", tp->symbol, tp->offset);
1968 if (err)
1969 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301970
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001971 for (i = 0; i < tev->nargs; i++)
1972 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001973 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001974
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001975 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001976error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001977 strbuf_release(&buf);
1978 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001979}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001980
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001981static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1982 struct perf_probe_point *pp,
1983 bool is_kprobe)
1984{
1985 struct symbol *sym = NULL;
1986 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001987 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001988 int ret = -ENOENT;
1989
1990 if (!is_kprobe) {
1991 map = dso__new_map(tp->module);
1992 if (!map)
1993 goto out;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001994 sym = map__find_symbol(map, addr, NULL);
1995 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001996 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09001997 if (kernel_get_symbol_address_by_name(tp->symbol,
1998 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001999 goto out;
2000 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002001 if (addr) {
2002 addr += tp->offset;
2003 sym = __find_kernel_function(addr, &map);
2004 }
2005 }
Wang Nan98d3b252015-11-05 13:19:25 +00002006
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002007 if (!sym)
2008 goto out;
2009
2010 pp->retprobe = tp->retprobe;
2011 pp->offset = addr - map->unmap_ip(map, sym->start);
2012 pp->function = strdup(sym->name);
2013 ret = pp->function ? 0 : -ENOMEM;
2014
2015out:
2016 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002017 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002018 }
2019
2020 return ret;
2021}
2022
2023static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002024 struct perf_probe_point *pp,
2025 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002026{
2027 char buf[128];
2028 int ret;
2029
2030 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2031 if (!ret)
2032 return 0;
2033 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2034 if (!ret)
2035 return 0;
2036
2037 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2038
2039 if (tp->symbol) {
2040 pp->function = strdup(tp->symbol);
2041 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002042 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002043 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2044 if (ret < 0)
2045 return ret;
2046 pp->function = strdup(buf);
2047 pp->offset = 0;
2048 }
2049 if (pp->function == NULL)
2050 return -ENOMEM;
2051
2052 pp->retprobe = tp->retprobe;
2053
2054 return 0;
2055}
2056
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302057static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302058 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002059{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002060 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002061 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002062
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002063 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002064 pev->event = strdup(tev->event);
2065 pev->group = strdup(tev->group);
2066 if (pev->event == NULL || pev->group == NULL)
2067 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002068
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002069 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002070 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002071 if (ret < 0)
2072 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002073
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002074 /* Convert trace_arg to probe_arg */
2075 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002076 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2077 if (pev->args == NULL)
2078 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002079 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002080 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002081 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002082 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002083 if ((ret = strbuf_init(&buf, 32)) < 0)
2084 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002085 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2086 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002087 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002088 if (pev->args[i].name == NULL && ret >= 0)
2089 ret = -ENOMEM;
2090 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002091error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002092 if (ret < 0)
2093 clear_perf_probe_event(pev);
2094
2095 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002096}
2097
2098void clear_perf_probe_event(struct perf_probe_event *pev)
2099{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002100 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002101 int i;
2102
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002103 free(pev->event);
2104 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002105 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002106 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002107
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002108 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002109 free(pev->args[i].name);
2110 free(pev->args[i].var);
2111 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002112 field = pev->args[i].field;
2113 while (field) {
2114 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002115 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002116 free(field);
2117 field = next;
2118 }
2119 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002120 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002121 memset(pev, 0, sizeof(*pev));
2122}
2123
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002124#define strdup_or_goto(str, label) \
2125({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2126
2127static int perf_probe_point__copy(struct perf_probe_point *dst,
2128 struct perf_probe_point *src)
2129{
2130 dst->file = strdup_or_goto(src->file, out_err);
2131 dst->function = strdup_or_goto(src->function, out_err);
2132 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2133 dst->line = src->line;
2134 dst->retprobe = src->retprobe;
2135 dst->offset = src->offset;
2136 return 0;
2137
2138out_err:
2139 clear_perf_probe_point(dst);
2140 return -ENOMEM;
2141}
2142
2143static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2144 struct perf_probe_arg *src)
2145{
2146 struct perf_probe_arg_field *field, **ppfield;
2147
2148 dst->name = strdup_or_goto(src->name, out_err);
2149 dst->var = strdup_or_goto(src->var, out_err);
2150 dst->type = strdup_or_goto(src->type, out_err);
2151
2152 field = src->field;
2153 ppfield = &(dst->field);
2154 while (field) {
2155 *ppfield = zalloc(sizeof(*field));
2156 if (!*ppfield)
2157 goto out_err;
2158 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2159 (*ppfield)->index = field->index;
2160 (*ppfield)->ref = field->ref;
2161 field = field->next;
2162 ppfield = &((*ppfield)->next);
2163 }
2164 return 0;
2165out_err:
2166 return -ENOMEM;
2167}
2168
2169int perf_probe_event__copy(struct perf_probe_event *dst,
2170 struct perf_probe_event *src)
2171{
2172 int i;
2173
2174 dst->event = strdup_or_goto(src->event, out_err);
2175 dst->group = strdup_or_goto(src->group, out_err);
2176 dst->target = strdup_or_goto(src->target, out_err);
2177 dst->uprobes = src->uprobes;
2178
2179 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2180 goto out_err;
2181
2182 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2183 if (!dst->args)
2184 goto out_err;
2185 dst->nargs = src->nargs;
2186
2187 for (i = 0; i < src->nargs; i++)
2188 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2189 goto out_err;
2190 return 0;
2191
2192out_err:
2193 clear_perf_probe_event(dst);
2194 return -ENOMEM;
2195}
2196
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002197void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002198{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302199 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002200 int i;
2201
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002202 free(tev->event);
2203 free(tev->group);
2204 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002205 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002206 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002207 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002208 free(tev->args[i].name);
2209 free(tev->args[i].value);
2210 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002211 ref = tev->args[i].ref;
2212 while (ref) {
2213 next = ref->next;
2214 free(ref);
2215 ref = next;
2216 }
2217 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002218 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002219 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002220}
2221
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002222struct kprobe_blacklist_node {
2223 struct list_head list;
2224 unsigned long start;
2225 unsigned long end;
2226 char *symbol;
2227};
2228
2229static void kprobe_blacklist__delete(struct list_head *blacklist)
2230{
2231 struct kprobe_blacklist_node *node;
2232
2233 while (!list_empty(blacklist)) {
2234 node = list_first_entry(blacklist,
2235 struct kprobe_blacklist_node, list);
2236 list_del(&node->list);
2237 free(node->symbol);
2238 free(node);
2239 }
2240}
2241
2242static int kprobe_blacklist__load(struct list_head *blacklist)
2243{
2244 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002245 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002246 char buf[PATH_MAX], *p;
2247 FILE *fp;
2248 int ret;
2249
2250 if (__debugfs == NULL)
2251 return -ENOTSUP;
2252
2253 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2254 if (ret < 0)
2255 return ret;
2256
2257 fp = fopen(buf, "r");
2258 if (!fp)
2259 return -errno;
2260
2261 ret = 0;
2262 while (fgets(buf, PATH_MAX, fp)) {
2263 node = zalloc(sizeof(*node));
2264 if (!node) {
2265 ret = -ENOMEM;
2266 break;
2267 }
2268 INIT_LIST_HEAD(&node->list);
2269 list_add_tail(&node->list, blacklist);
2270 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2271 ret = -EINVAL;
2272 break;
2273 }
2274 p = strchr(buf, '\t');
2275 if (p) {
2276 p++;
2277 if (p[strlen(p) - 1] == '\n')
2278 p[strlen(p) - 1] = '\0';
2279 } else
2280 p = (char *)"unknown";
2281 node->symbol = strdup(p);
2282 if (!node->symbol) {
2283 ret = -ENOMEM;
2284 break;
2285 }
2286 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2287 node->start, node->end, node->symbol);
2288 ret++;
2289 }
2290 if (ret < 0)
2291 kprobe_blacklist__delete(blacklist);
2292 fclose(fp);
2293
2294 return ret;
2295}
2296
2297static struct kprobe_blacklist_node *
2298kprobe_blacklist__find_by_address(struct list_head *blacklist,
2299 unsigned long address)
2300{
2301 struct kprobe_blacklist_node *node;
2302
2303 list_for_each_entry(node, blacklist, list) {
2304 if (node->start <= address && address <= node->end)
2305 return node;
2306 }
2307
2308 return NULL;
2309}
2310
Masami Hiramatsub0312202015-06-16 20:50:55 +09002311static LIST_HEAD(kprobe_blacklist);
2312
2313static void kprobe_blacklist__init(void)
2314{
2315 if (!list_empty(&kprobe_blacklist))
2316 return;
2317
2318 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2319 pr_debug("No kprobe blacklist support, ignored\n");
2320}
2321
2322static void kprobe_blacklist__release(void)
2323{
2324 kprobe_blacklist__delete(&kprobe_blacklist);
2325}
2326
2327static bool kprobe_blacklist__listed(unsigned long address)
2328{
2329 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2330}
2331
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002332static int perf_probe_event__sprintf(const char *group, const char *event,
2333 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002334 const char *module,
2335 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002336{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002337 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002338 char *buf;
2339
2340 if (asprintf(&buf, "%s:%s", group, event) < 0)
2341 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002342 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002343 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002344 if (ret)
2345 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002346
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002347 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002348 buf = synthesize_perf_probe_point(&pev->point);
2349 if (!buf)
2350 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002351 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002352 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002353
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002354 if (!ret && module)
2355 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002356
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002357 if (!ret && pev->nargs > 0) {
2358 ret = strbuf_add(result, " with", 5);
2359 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002360 buf = synthesize_perf_probe_arg(&pev->args[i]);
2361 if (!buf)
2362 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002363 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002364 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002365 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002366 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002367 if (!ret)
2368 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002369
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002370 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002371}
2372
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002373/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002374int show_perf_probe_event(const char *group, const char *event,
2375 struct perf_probe_event *pev,
2376 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002377{
2378 struct strbuf buf = STRBUF_INIT;
2379 int ret;
2380
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002381 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002382 if (ret >= 0) {
2383 if (use_stdout)
2384 printf("%s\n", buf.buf);
2385 else
2386 pr_info("%s\n", buf.buf);
2387 }
2388 strbuf_release(&buf);
2389
2390 return ret;
2391}
2392
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002393static bool filter_probe_trace_event(struct probe_trace_event *tev,
2394 struct strfilter *filter)
2395{
2396 char tmp[128];
2397
2398 /* At first, check the event name itself */
2399 if (strfilter__compare(filter, tev->event))
2400 return true;
2401
2402 /* Next, check the combination of name and group */
2403 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2404 return false;
2405 return strfilter__compare(filter, tmp);
2406}
2407
2408static int __show_perf_probe_events(int fd, bool is_kprobe,
2409 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002410{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302411 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302412 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002413 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002414 struct strlist *rawlist;
2415 struct str_node *ent;
2416
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002417 memset(&tev, 0, sizeof(tev));
2418 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002419
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002420 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002421 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002422 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002423
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002424 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302425 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002426 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002427 if (!filter_probe_trace_event(&tev, filter))
2428 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302429 ret = convert_to_perf_probe_event(&tev, &pev,
2430 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002431 if (ret < 0)
2432 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002433 ret = show_perf_probe_event(pev.group, pev.event,
2434 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002435 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002436 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002437next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002438 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302439 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002440 if (ret < 0)
2441 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002442 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002443 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002444 /* Cleanup cached debuginfo if needed */
2445 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002446
2447 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002448}
2449
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302450/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002451int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302452{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002453 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302454
2455 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302456
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002457 if (probe_conf.cache)
2458 return probe_cache__show_all_caches(filter);
2459
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002460 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302461 if (ret < 0)
2462 return ret;
2463
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002464 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2465 if (ret < 0)
2466 return ret;
2467
2468 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002469 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002470 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002471 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002472 if (kp_fd > 0)
2473 close(kp_fd);
2474 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002475 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002476 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302477
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002478 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002479}
2480
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002481static int get_new_event_name(char *buf, size_t len, const char *base,
2482 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002483{
2484 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002485 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002486
Naveen N. Rao3099c022015-04-28 17:35:34 +05302487 if (*base == '.')
2488 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002489 nbase = strdup(base);
2490 if (!nbase)
2491 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302492
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002493 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2494 p = strchr(nbase, '.');
2495 if (p && p != nbase)
2496 *p = '\0';
2497
2498 /* Try no suffix number */
2499 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002500 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002501 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002502 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002503 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002504 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002505 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002506
Masami Hiramatsud761b082009-12-15 10:32:25 -05002507 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002508 pr_warning("Error: event \"%s\" already exists.\n"
2509 " Hint: Remove existing event by 'perf probe -d'\n"
2510 " or force duplicates by 'perf probe -f'\n"
2511 " or set 'force=yes' in BPF source.\n",
2512 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002513 ret = -EEXIST;
2514 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002515 }
2516
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002517 /* Try to add suffix */
2518 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002519 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002520 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002521 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002522 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002523 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002524 if (!strlist__has_entry(namelist, buf))
2525 break;
2526 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002527 if (i == MAX_EVENT_INDEX) {
2528 pr_warning("Too many events are on the same function.\n");
2529 ret = -ERANGE;
2530 }
2531
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002532out:
2533 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002534 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002535}
2536
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002537/* Warn if the current kernel's uprobe implementation is old */
2538static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2539{
2540 int i;
2541 char *buf = synthesize_probe_trace_command(tev);
2542
2543 /* Old uprobe event doesn't support memory dereference */
2544 if (!tev->uprobes || tev->nargs == 0 || !buf)
2545 goto out;
2546
2547 for (i = 0; i < tev->nargs; i++)
2548 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2549 pr_warning("Please upgrade your kernel to at least "
2550 "3.14 to have access to feature %s\n",
2551 tev->args[i].value);
2552 break;
2553 }
2554out:
2555 free(buf);
2556}
2557
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002558/* Set new name from original perf_probe_event and namelist */
2559static int probe_trace_event__set_name(struct probe_trace_event *tev,
2560 struct perf_probe_event *pev,
2561 struct strlist *namelist,
2562 bool allow_suffix)
2563{
2564 const char *event, *group;
2565 char buf[64];
2566 int ret;
2567
Masami Hiramatsubc062232016-07-01 17:03:12 +09002568 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002569 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002570 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002571 else if (tev->event)
2572 event = tev->event;
2573 else {
2574 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002575 if (pev->point.function &&
2576 (strncmp(pev->point.function, "0x", 2) != 0) &&
2577 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002578 event = pev->point.function;
2579 else
2580 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002581 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002582 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002583 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002584 else if (tev->group)
2585 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002586 else
2587 group = PERFPROBE_GROUP;
2588
2589 /* Get an unused new event name */
2590 ret = get_new_event_name(buf, 64, event,
2591 namelist, allow_suffix);
2592 if (ret < 0)
2593 return ret;
2594
2595 event = buf;
2596
2597 tev->event = strdup(event);
2598 tev->group = strdup(group);
2599 if (tev->event == NULL || tev->group == NULL)
2600 return -ENOMEM;
2601
2602 /* Add added event name to namelist */
2603 strlist__add(namelist, event);
2604 return 0;
2605}
2606
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002607static int __open_probe_file_and_namelist(bool uprobe,
2608 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002609{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002610 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002611
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002612 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002613 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002614 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002615
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002616 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002617 *namelist = probe_file__get_namelist(fd);
2618 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002619 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002620 close(fd);
2621 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002622 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002623 return fd;
2624}
2625
2626static int __add_probe_trace_events(struct perf_probe_event *pev,
2627 struct probe_trace_event *tevs,
2628 int ntevs, bool allow_suffix)
2629{
2630 int i, fd[2] = {-1, -1}, up, ret;
2631 struct probe_trace_event *tev = NULL;
2632 struct probe_cache *cache = NULL;
2633 struct strlist *namelist[2] = {NULL, NULL};
2634
2635 up = pev->uprobes ? 1 : 0;
2636 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2637 if (fd[up] < 0)
2638 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002639
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002640 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002641 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002642 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002643 up = tev->uprobes ? 1 : 0;
2644 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2645 fd[up] = __open_probe_file_and_namelist(up,
2646 &namelist[up]);
2647 if (fd[up] < 0)
2648 goto close_out;
2649 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002650 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002651 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002652 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002653
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002654 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002655 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002656 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002657 if (ret < 0)
2658 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002659
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002660 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002661 if (ret < 0)
2662 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002663
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002664 /*
2665 * Probes after the first probe which comes from same
2666 * user input are always allowed to add suffix, because
2667 * there might be several addresses corresponding to
2668 * one code line.
2669 */
2670 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002671 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002672 if (ret == -EINVAL && pev->uprobes)
2673 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002674 if (ret == 0 && probe_conf.cache) {
2675 cache = probe_cache__new(pev->target);
2676 if (!cache ||
2677 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2678 probe_cache__commit(cache) < 0)
2679 pr_warning("Failed to add event to probe cache\n");
2680 probe_cache__delete(cache);
2681 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002682
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002683close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002684 for (up = 0; up < 2; up++) {
2685 strlist__delete(namelist[up]);
2686 if (fd[up] >= 0)
2687 close(fd[up]);
2688 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002690}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002691
Wang Nan6bb536c2015-05-29 09:45:47 +00002692static int find_probe_functions(struct map *map, char *name,
2693 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002694{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002695 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002696 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002697 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002698
Wang Nan75e4a2a2015-05-15 12:14:44 +00002699 if (map__load(map, NULL) < 0)
2700 return 0;
2701
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002702 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002703 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002704 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002705 if (syms && found < probe_conf.max_probes)
2706 syms[found - 1] = sym;
2707 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002708 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002709
2710 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002711}
2712
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302713void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2714 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302715 struct map *map __maybe_unused,
2716 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302717
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002718/*
2719 * Find probe function addresses from map.
2720 * Return an error or the number of found probe_trace_event
2721 */
2722static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002723 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002724{
2725 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002726 struct ref_reloc_sym *reloc_sym = NULL;
2727 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002728 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002729 struct probe_trace_event *tev;
2730 struct perf_probe_point *pp = &pev->point;
2731 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002732 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002733 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302734 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002735
Masami Hiramatsu44225522015-05-08 10:03:28 +09002736 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002737 if (!map) {
2738 ret = -EINVAL;
2739 goto out;
2740 }
2741
Wang Nan6bb536c2015-05-29 09:45:47 +00002742 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2743 if (!syms) {
2744 ret = -ENOMEM;
2745 goto out;
2746 }
2747
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002748 /*
2749 * Load matched symbols: Since the different local symbols may have
2750 * same name but different addresses, this lists all the symbols.
2751 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002752 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002753 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002754 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002755 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002756 ret = -ENOENT;
2757 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002758 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002759 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002760 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002761 ret = -E2BIG;
2762 goto out;
2763 }
2764
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002765 /* Note that the symbols in the kmodule are not relocated */
2766 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002767 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002768 if (!reloc_sym) {
2769 pr_warning("Relocated base symbol is not found!\n");
2770 ret = -EINVAL;
2771 goto out;
2772 }
2773 }
2774
2775 /* Setup result trace-probe-events */
2776 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2777 if (!*tevs) {
2778 ret = -ENOMEM;
2779 goto out;
2780 }
2781
2782 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002783
Wang Nan6bb536c2015-05-29 09:45:47 +00002784 for (j = 0; j < num_matched_functions; j++) {
2785 sym = syms[j];
2786
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002787 tev = (*tevs) + ret;
2788 tp = &tev->point;
2789 if (ret == num_matched_functions) {
2790 pr_warning("Too many symbols are listed. Skip it.\n");
2791 break;
2792 }
2793 ret++;
2794
2795 if (pp->offset > sym->end - sym->start) {
2796 pr_warning("Offset %ld is bigger than the size of %s\n",
2797 pp->offset, sym->name);
2798 ret = -ENOENT;
2799 goto err_out;
2800 }
2801 /* Add one probe point */
2802 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002803
2804 /* Check the kprobe (not in module) is within .text */
2805 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002806 kprobe_warn_out_range(sym->name, tp->address)) {
2807 tp->symbol = NULL; /* Skip it */
2808 skipped++;
2809 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002810 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2811 tp->offset = tp->address - reloc_sym->addr;
2812 } else {
2813 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2814 tp->offset = pp->offset;
2815 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002816 tp->realname = strdup_or_goto(sym->name, nomem_out);
2817
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002818 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302819 if (pev->target) {
2820 if (pev->uprobes) {
2821 tev->point.module = strdup_or_goto(pev->target,
2822 nomem_out);
2823 } else {
2824 mod_name = find_module_name(pev->target);
2825 tev->point.module =
2826 strdup(mod_name ? mod_name : pev->target);
2827 free(mod_name);
2828 if (!tev->point.module)
2829 goto nomem_out;
2830 }
2831 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002832 tev->uprobes = pev->uprobes;
2833 tev->nargs = pev->nargs;
2834 if (tev->nargs) {
2835 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2836 tev->nargs);
2837 if (tev->args == NULL)
2838 goto nomem_out;
2839 }
2840 for (i = 0; i < tev->nargs; i++) {
2841 if (pev->args[i].name)
2842 tev->args[i].name =
2843 strdup_or_goto(pev->args[i].name,
2844 nomem_out);
2845
2846 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2847 nomem_out);
2848 if (pev->args[i].type)
2849 tev->args[i].type =
2850 strdup_or_goto(pev->args[i].type,
2851 nomem_out);
2852 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302853 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002854 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002855 if (ret == skipped) {
2856 ret = -ENOENT;
2857 goto err_out;
2858 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002859
2860out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002861 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002862 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002863 return ret;
2864
2865nomem_out:
2866 ret = -ENOMEM;
2867err_out:
2868 clear_probe_trace_events(*tevs, num_matched_functions);
2869 zfree(tevs);
2870 goto out;
2871}
2872
Wang Nanda15bd92015-08-26 10:57:45 +00002873static int try_to_find_absolute_address(struct perf_probe_event *pev,
2874 struct probe_trace_event **tevs)
2875{
2876 struct perf_probe_point *pp = &pev->point;
2877 struct probe_trace_event *tev;
2878 struct probe_trace_point *tp;
2879 int i, err;
2880
2881 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2882 return -EINVAL;
2883 if (perf_probe_event_need_dwarf(pev))
2884 return -EINVAL;
2885
2886 /*
2887 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2888 * absolute address.
2889 *
2890 * Only one tev can be generated by this.
2891 */
2892 *tevs = zalloc(sizeof(*tev));
2893 if (!*tevs)
2894 return -ENOMEM;
2895
2896 tev = *tevs;
2897 tp = &tev->point;
2898
2899 /*
2900 * Don't use tp->offset, use address directly, because
2901 * in synthesize_probe_trace_command() address cannot be
2902 * zero.
2903 */
2904 tp->address = pev->point.abs_address;
2905 tp->retprobe = pp->retprobe;
2906 tev->uprobes = pev->uprobes;
2907
2908 err = -ENOMEM;
2909 /*
2910 * Give it a '0x' leading symbol name.
2911 * In __add_probe_trace_events, a NULL symbol is interpreted as
2912 * invalud.
2913 */
2914 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2915 goto errout;
2916
2917 /* For kprobe, check range */
2918 if ((!tev->uprobes) &&
2919 (kprobe_warn_out_range(tev->point.symbol,
2920 tev->point.address))) {
2921 err = -EACCES;
2922 goto errout;
2923 }
2924
2925 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2926 goto errout;
2927
2928 if (pev->target) {
2929 tp->module = strdup(pev->target);
2930 if (!tp->module)
2931 goto errout;
2932 }
2933
2934 if (tev->group) {
2935 tev->group = strdup(pev->group);
2936 if (!tev->group)
2937 goto errout;
2938 }
2939
2940 if (pev->event) {
2941 tev->event = strdup(pev->event);
2942 if (!tev->event)
2943 goto errout;
2944 }
2945
2946 tev->nargs = pev->nargs;
2947 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2948 if (!tev->args) {
2949 err = -ENOMEM;
2950 goto errout;
2951 }
2952 for (i = 0; i < tev->nargs; i++)
2953 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2954
2955 return 1;
2956
2957errout:
2958 if (*tevs) {
2959 clear_probe_trace_events(*tevs, 1);
2960 *tevs = NULL;
2961 }
2962 return err;
2963}
2964
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002965/* Concatinate two arrays */
2966static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2967{
2968 void *ret;
2969
2970 ret = malloc(sz_a + sz_b);
2971 if (ret) {
2972 memcpy(ret, a, sz_a);
2973 memcpy(ret + sz_a, b, sz_b);
2974 }
2975 return ret;
2976}
2977
2978static int
2979concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2980 struct probe_trace_event **tevs2, int ntevs2)
2981{
2982 struct probe_trace_event *new_tevs;
2983 int ret = 0;
2984
2985 if (ntevs == 0) {
2986 *tevs = *tevs2;
2987 *ntevs = ntevs2;
2988 *tevs2 = NULL;
2989 return 0;
2990 }
2991
2992 if (*ntevs + ntevs2 > probe_conf.max_probes)
2993 ret = -E2BIG;
2994 else {
2995 /* Concatinate the array of probe_trace_event */
2996 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
2997 *tevs2, ntevs2 * sizeof(**tevs2));
2998 if (!new_tevs)
2999 ret = -ENOMEM;
3000 else {
3001 free(*tevs);
3002 *tevs = new_tevs;
3003 *ntevs += ntevs2;
3004 }
3005 }
3006 if (ret < 0)
3007 clear_probe_trace_events(*tevs2, ntevs2);
3008 zfree(tevs2);
3009
3010 return ret;
3011}
3012
3013/*
3014 * Try to find probe_trace_event from given probe caches. Return the number
3015 * of cached events found, if an error occurs return the error.
3016 */
3017static int find_cached_events(struct perf_probe_event *pev,
3018 struct probe_trace_event **tevs,
3019 const char *target)
3020{
3021 struct probe_cache *cache;
3022 struct probe_cache_entry *entry;
3023 struct probe_trace_event *tmp_tevs = NULL;
3024 int ntevs = 0;
3025 int ret = 0;
3026
3027 cache = probe_cache__new(target);
3028 /* Return 0 ("not found") if the target has no probe cache. */
3029 if (!cache)
3030 return 0;
3031
3032 for_each_probe_cache_entry(entry, cache) {
3033 /* Skip the cache entry which has no name */
3034 if (!entry->pev.event || !entry->pev.group)
3035 continue;
3036 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3037 strglobmatch(entry->pev.event, pev->event)) {
3038 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3039 if (ret > 0)
3040 ret = concat_probe_trace_events(tevs, &ntevs,
3041 &tmp_tevs, ret);
3042 if (ret < 0)
3043 break;
3044 }
3045 }
3046 probe_cache__delete(cache);
3047 if (ret < 0) {
3048 clear_probe_trace_events(*tevs, ntevs);
3049 zfree(tevs);
3050 } else {
3051 ret = ntevs;
3052 if (ntevs > 0 && target && target[0] == '/')
3053 pev->uprobes = true;
3054 }
3055
3056 return ret;
3057}
3058
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003059/* Try to find probe_trace_event from all probe caches */
3060static int find_cached_events_all(struct perf_probe_event *pev,
3061 struct probe_trace_event **tevs)
3062{
3063 struct probe_trace_event *tmp_tevs = NULL;
3064 struct strlist *bidlist;
3065 struct str_node *nd;
3066 char *pathname;
3067 int ntevs = 0;
3068 int ret;
3069
3070 /* Get the buildid list of all valid caches */
3071 bidlist = build_id_cache__list_all(true);
3072 if (!bidlist) {
3073 ret = -errno;
3074 pr_debug("Failed to get buildids: %d\n", ret);
3075 return ret;
3076 }
3077
3078 ret = 0;
3079 strlist__for_each_entry(nd, bidlist) {
3080 pathname = build_id_cache__origname(nd->s);
3081 ret = find_cached_events(pev, &tmp_tevs, pathname);
3082 /* In the case of cnt == 0, we just skip it */
3083 if (ret > 0)
3084 ret = concat_probe_trace_events(tevs, &ntevs,
3085 &tmp_tevs, ret);
3086 free(pathname);
3087 if (ret < 0)
3088 break;
3089 }
3090 strlist__delete(bidlist);
3091
3092 if (ret < 0) {
3093 clear_probe_trace_events(*tevs, ntevs);
3094 zfree(tevs);
3095 } else
3096 ret = ntevs;
3097
3098 return ret;
3099}
3100
Masami Hiramatsubc062232016-07-01 17:03:12 +09003101static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3102 struct probe_trace_event **tevs)
3103{
3104 struct probe_cache *cache;
3105 struct probe_cache_entry *entry;
3106 struct probe_trace_event *tev;
3107 struct str_node *node;
3108 int ret, i;
3109
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003110 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003111 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003112 if (!pev->target)
3113 return find_cached_events_all(pev, tevs);
3114 else
3115 return find_cached_events(pev, tevs, pev->target);
3116 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003117 cache = probe_cache__new(pev->target);
3118 if (!cache)
3119 return 0;
3120
3121 entry = probe_cache__find(cache, pev);
3122 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003123 /* SDT must be in the cache */
3124 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003125 goto out;
3126 }
3127
3128 ret = strlist__nr_entries(entry->tevlist);
3129 if (ret > probe_conf.max_probes) {
3130 pr_debug("Too many entries matched in the cache of %s\n",
3131 pev->target ? : "kernel");
3132 ret = -E2BIG;
3133 goto out;
3134 }
3135
3136 *tevs = zalloc(ret * sizeof(*tev));
3137 if (!*tevs) {
3138 ret = -ENOMEM;
3139 goto out;
3140 }
3141
3142 i = 0;
3143 strlist__for_each_entry(node, entry->tevlist) {
3144 tev = &(*tevs)[i++];
3145 ret = parse_probe_trace_command(node->s, tev);
3146 if (ret < 0)
3147 goto out;
3148 /* Set the uprobes attribute as same as original */
3149 tev->uprobes = pev->uprobes;
3150 }
3151 ret = i;
3152
3153out:
3154 probe_cache__delete(cache);
3155 return ret;
3156}
3157
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303158static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003159 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003160{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003161 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003162
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003163 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003164 /* Set group name if not given */
3165 if (!pev->uprobes) {
3166 pev->group = strdup(PERFPROBE_GROUP);
3167 ret = pev->group ? 0 : -ENOMEM;
3168 } else
3169 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003170 if (ret != 0) {
3171 pr_warning("Failed to make a group name.\n");
3172 return ret;
3173 }
3174 }
3175
Wang Nanda15bd92015-08-26 10:57:45 +00003176 ret = try_to_find_absolute_address(pev, tevs);
3177 if (ret > 0)
3178 return ret;
3179
Masami Hiramatsubc062232016-07-01 17:03:12 +09003180 /* At first, we need to lookup cache entry */
3181 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003182 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3183 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003184
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003185 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003186 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003187 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003188 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003189
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003190 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003191}
3192
Wang Nan12fae5e2015-09-04 21:16:00 +09003193int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003194{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003195 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003196
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003197 /* Loop 1: convert all events */
3198 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003199 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003200 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003201 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003202 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003203 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003204 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003205 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003206 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003207 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003208 /* This just release blacklist only if allocated */
3209 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003210
Namhyung Kim844dffa2015-09-04 21:15:59 +09003211 return 0;
3212}
3213
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003214static int show_probe_trace_event(struct probe_trace_event *tev)
3215{
3216 char *buf = synthesize_probe_trace_command(tev);
3217
3218 if (!buf) {
3219 pr_debug("Failed to synthesize probe trace event.\n");
3220 return -EINVAL;
3221 }
3222
3223 /* Showing definition always go stdout */
3224 printf("%s\n", buf);
3225 free(buf);
3226
3227 return 0;
3228}
3229
3230int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3231{
3232 struct strlist *namelist = strlist__new(NULL, NULL);
3233 struct probe_trace_event *tev;
3234 struct perf_probe_event *pev;
3235 int i, j, ret = 0;
3236
3237 if (!namelist)
3238 return -ENOMEM;
3239
3240 for (j = 0; j < npevs && !ret; j++) {
3241 pev = &pevs[j];
3242 for (i = 0; i < pev->ntevs && !ret; i++) {
3243 tev = &pev->tevs[i];
3244 /* Skip if the symbol is out of .text or blacklisted */
3245 if (!tev->point.symbol && !pev->uprobes)
3246 continue;
3247
3248 /* Set new name for tev (and update namelist) */
3249 ret = probe_trace_event__set_name(tev, pev,
3250 namelist, true);
3251 if (!ret)
3252 ret = show_probe_trace_event(tev);
3253 }
3254 }
3255 strlist__delete(namelist);
3256
3257 return ret;
3258}
3259
Wang Nan12fae5e2015-09-04 21:16:00 +09003260int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003261{
3262 int i, ret = 0;
3263
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003264 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003265 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003266 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3267 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003268 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003269 if (ret < 0)
3270 break;
3271 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003272 return ret;
3273}
3274
Wang Nan12fae5e2015-09-04 21:16:00 +09003275void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003276{
3277 int i, j;
3278
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003279 /* Loop 3: cleanup and free trace events */
3280 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003281 for (j = 0; j < pevs[i].ntevs; j++)
3282 clear_probe_trace_event(&pevs[i].tevs[j]);
3283 zfree(&pevs[i].tevs);
3284 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003285 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003286 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003287}
3288
3289int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3290{
3291 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003292
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003293 ret = init_probe_symbol_maps(pevs->uprobes);
3294 if (ret < 0)
3295 return ret;
3296
Wang Nan12fae5e2015-09-04 21:16:00 +09003297 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003298 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003299 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003300
Wang Nan12fae5e2015-09-04 21:16:00 +09003301 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003302
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003303 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003304 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003305}
3306
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003307int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003308{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003309 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003310 char *str = strfilter__string(filter);
3311
3312 if (!str)
3313 return -EINVAL;
3314
Masami Hiramatsufa282442009-12-08 17:03:23 -05003315 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003316 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3317 if (ret < 0)
3318 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303319
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003320 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003321 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303322 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003323
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003324 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003325 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003326 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003327 goto error;
3328 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003329 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303330
3331error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003332 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303333 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003334 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303335 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003336out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003337 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003338
3339 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003340}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303341
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003342int show_available_funcs(const char *target, struct strfilter *_filter,
3343 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003344{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003345 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003346 struct map *map;
3347 int ret;
3348
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003349 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003350 if (ret < 0)
3351 return ret;
3352
3353 /* Get a symbol map */
3354 if (user)
3355 map = dso__new_map(target);
3356 else
3357 map = kernel_get_module_map(target);
3358 if (!map) {
3359 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003360 return -EINVAL;
3361 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003362
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003363 ret = map__load(map, NULL);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003364 if (ret) {
3365 if (ret == -2) {
3366 char *str = strfilter__string(_filter);
3367 pr_err("Failed to find symbols matched to \"%s\"\n",
3368 str);
3369 free(str);
3370 } else
3371 pr_err("Failed to load symbols in %s\n",
3372 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003373 goto end;
3374 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003375 if (!dso__sorted_by_name(map->dso, map->type))
3376 dso__sort_by_name(map->dso, map->type);
3377
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003378 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303379 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003380
3381 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3382 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3383
3384 if (strfilter__compare(_filter, pos->sym.name))
3385 printf("%s\n", pos->sym.name);
3386 }
3387
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003388end:
3389 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03003390 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003391 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003392 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303393
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003394 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303395}
3396
Wang Nanda15bd92015-08-26 10:57:45 +00003397int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3398 struct perf_probe_arg *pvar)
3399{
3400 tvar->value = strdup(pvar->var);
3401 if (tvar->value == NULL)
3402 return -ENOMEM;
3403 if (pvar->type) {
3404 tvar->type = strdup(pvar->type);
3405 if (tvar->type == NULL)
3406 return -ENOMEM;
3407 }
3408 if (pvar->name) {
3409 tvar->name = strdup(pvar->name);
3410 if (tvar->name == NULL)
3411 return -ENOMEM;
3412 } else
3413 tvar->name = NULL;
3414 return 0;
3415}