11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 250656eecSMasami Hiramatsu /* 30e60836bSSrikar Dronamraju * probe-event.c : perf-probe definition to probe_events format converter 450656eecSMasami Hiramatsu * 550656eecSMasami Hiramatsu * Written by Masami Hiramatsu <mhiramat@redhat.com> 650656eecSMasami Hiramatsu */ 750656eecSMasami Hiramatsu 8fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 950656eecSMasami Hiramatsu #include <sys/utsname.h> 1050656eecSMasami Hiramatsu #include <sys/types.h> 1150656eecSMasami Hiramatsu #include <sys/stat.h> 1250656eecSMasami Hiramatsu #include <fcntl.h> 1350656eecSMasami Hiramatsu #include <errno.h> 1450656eecSMasami Hiramatsu #include <stdio.h> 1550656eecSMasami Hiramatsu #include <unistd.h> 1650656eecSMasami Hiramatsu #include <stdlib.h> 1750656eecSMasami Hiramatsu #include <string.h> 184de189feSMasami Hiramatsu #include <stdarg.h> 194de189feSMasami Hiramatsu #include <limits.h> 20e80711caSMasami Hiramatsu #include <elf.h> 2150656eecSMasami Hiramatsu 224a3cec84SArnaldo Carvalho de Melo #include "build-id.h" 2350656eecSMasami Hiramatsu #include "event.h" 2440f3b2d2SArnaldo Carvalho de Melo #include "namespaces.h" 254de189feSMasami Hiramatsu #include "strlist.h" 268ec20b17SArnaldo Carvalho de Melo #include "strfilter.h" 2750656eecSMasami Hiramatsu #include "debug.h" 284a3cec84SArnaldo Carvalho de Melo #include "dso.h" 29631c9defSMasami Hiramatsu #include "color.h" 301101f69aSArnaldo Carvalho de Melo #include "map.h" 31c54d241bSArnaldo Carvalho de Melo #include "maps.h" 32e0faa8d3SMasami Hiramatsu #include "symbol.h" 334605eab3SJiri Olsa #include <api/fs/fs.h> 341d037ca1SIrina Tirdea #include "trace-event.h" /* For __maybe_unused */ 3550656eecSMasami Hiramatsu #include "probe-event.h" 364235b045SMasami Hiramatsu #include "probe-finder.h" 3792f6c72eSMasami Hiramatsu #include "probe-file.h" 38225466f1SSrikar Dronamraju #include "session.h" 39a067558eSArnaldo Carvalho de Melo #include "string2.h" 40fa0d9846SArnaldo Carvalho de Melo #include "strbuf.h" 4150656eecSMasami Hiramatsu 42fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h> 433052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h> 447f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 453d689ed6SArnaldo Carvalho de Melo 467cd5738dSMasami Hiramatsu #ifdef HAVE_DEBUGINFOD_SUPPORT 477cd5738dSMasami Hiramatsu #include <elfutils/debuginfod.h> 487cd5738dSMasami Hiramatsu #endif 497cd5738dSMasami Hiramatsu 5050656eecSMasami Hiramatsu #define PERFPROBE_GROUP "probe" 5150656eecSMasami Hiramatsu 52f4d7da49SMasami Hiramatsu bool probe_event_dry_run; /* Dry run flag */ 53cb402730SMasami Hiramatsu struct probe_conf probe_conf = { .magic_num = DEFAULT_PROBE_MAGIC_NUM }; 54f4d7da49SMasami Hiramatsu 55146a1439SMasami Hiramatsu #define semantic_error(msg ...) pr_err("Semantic error :" msg) 5650656eecSMasami Hiramatsu 5792f6c72eSMasami Hiramatsu int e_snprintf(char *str, size_t size, const char *format, ...) 584de189feSMasami Hiramatsu { 594de189feSMasami Hiramatsu int ret; 604de189feSMasami Hiramatsu va_list ap; 614de189feSMasami Hiramatsu va_start(ap, format); 624de189feSMasami Hiramatsu ret = vsnprintf(str, size, format, ap); 634de189feSMasami Hiramatsu va_end(ap); 644de189feSMasami Hiramatsu if (ret >= (int)size) 654de189feSMasami Hiramatsu ret = -E2BIG; 664de189feSMasami Hiramatsu return ret; 674de189feSMasami Hiramatsu } 684de189feSMasami Hiramatsu 69ee45b6c2SMasami Hiramatsu static struct machine *host_machine; 70e0faa8d3SMasami Hiramatsu 71469b9b88SMasami Hiramatsu /* Initialize symbol maps and path of vmlinux/modules */ 729bae1e8cSNamhyung Kim int init_probe_symbol_maps(bool user_only) 73e0faa8d3SMasami Hiramatsu { 74146a1439SMasami Hiramatsu int ret; 75146a1439SMasami Hiramatsu 76e0faa8d3SMasami Hiramatsu symbol_conf.sort_by_name = true; 77680d926aSNamhyung Kim symbol_conf.allow_aliases = true; 780a7e6d1bSNamhyung Kim ret = symbol__init(NULL); 79146a1439SMasami Hiramatsu if (ret < 0) { 80146a1439SMasami Hiramatsu pr_debug("Failed to init symbol map.\n"); 81146a1439SMasami Hiramatsu goto out; 82146a1439SMasami Hiramatsu } 83e0faa8d3SMasami Hiramatsu 84ee45b6c2SMasami Hiramatsu if (host_machine || user_only) /* already initialized */ 85ee45b6c2SMasami Hiramatsu return 0; 86d28c6223SArnaldo Carvalho de Melo 87ee45b6c2SMasami Hiramatsu if (symbol_conf.vmlinux_name) 88ee45b6c2SMasami Hiramatsu pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 89ee45b6c2SMasami Hiramatsu 90ee45b6c2SMasami Hiramatsu host_machine = machine__new_host(); 91ee45b6c2SMasami Hiramatsu if (!host_machine) { 92ee45b6c2SMasami Hiramatsu pr_debug("machine__new_host() failed.\n"); 93ee45b6c2SMasami Hiramatsu symbol__exit(); 94ee45b6c2SMasami Hiramatsu ret = -1; 95469b9b88SMasami Hiramatsu } 96146a1439SMasami Hiramatsu out: 97146a1439SMasami Hiramatsu if (ret < 0) 98146a1439SMasami Hiramatsu pr_warning("Failed to init vmlinux path.\n"); 99146a1439SMasami Hiramatsu return ret; 100e0faa8d3SMasami Hiramatsu } 101e0faa8d3SMasami Hiramatsu 1029bae1e8cSNamhyung Kim void exit_probe_symbol_maps(void) 103ee45b6c2SMasami Hiramatsu { 104ee45b6c2SMasami Hiramatsu machine__delete(host_machine); 105ee45b6c2SMasami Hiramatsu host_machine = NULL; 106ee45b6c2SMasami Hiramatsu symbol__exit(); 107ee45b6c2SMasami Hiramatsu } 108ee45b6c2SMasami Hiramatsu 10980526491SMasami Hiramatsu static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap) 1108f33f7deSMasami Hiramatsu { 1118f33f7deSMasami Hiramatsu struct kmap *kmap; 112a5e813c6SArnaldo Carvalho de Melo struct map *map = machine__kernel_map(host_machine); 1138f33f7deSMasami Hiramatsu 114be39db9fSArnaldo Carvalho de Melo if (map__load(map) < 0) 1158f33f7deSMasami Hiramatsu return NULL; 1168f33f7deSMasami Hiramatsu 11777e65977SArnaldo Carvalho de Melo kmap = map__kmap(map); 118ba92732eSWang Nan if (!kmap) 119ba92732eSWang Nan return NULL; 12080526491SMasami Hiramatsu 12180526491SMasami Hiramatsu if (pmap) 12280526491SMasami Hiramatsu *pmap = map; 12380526491SMasami Hiramatsu 1248f33f7deSMasami Hiramatsu return kmap->ref_reloc_sym; 1258f33f7deSMasami Hiramatsu } 1268f33f7deSMasami Hiramatsu 1279b239a12SMasami Hiramatsu static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, 1289b239a12SMasami Hiramatsu bool reloc, bool reladdr) 1298f33f7deSMasami Hiramatsu { 1308f33f7deSMasami Hiramatsu struct ref_reloc_sym *reloc_sym; 1318f33f7deSMasami Hiramatsu struct symbol *sym; 1328f33f7deSMasami Hiramatsu struct map *map; 1338f33f7deSMasami Hiramatsu 1348f33f7deSMasami Hiramatsu /* ref_reloc_sym is just a label. Need a special fix*/ 135ac7a75d1SMasami Hiramatsu reloc_sym = kernel_get_ref_reloc_sym(&map); 1368f33f7deSMasami Hiramatsu if (reloc_sym && strcmp(name, reloc_sym->name) == 0) 137ac7a75d1SMasami Hiramatsu *addr = (!map->reloc || reloc) ? reloc_sym->addr : 138ac7a75d1SMasami Hiramatsu reloc_sym->unrelocated_addr; 1398f33f7deSMasami Hiramatsu else { 140107cad95SArnaldo Carvalho de Melo sym = machine__find_kernel_symbol_by_name(host_machine, name, &map); 1419b239a12SMasami Hiramatsu if (!sym) 1429b239a12SMasami Hiramatsu return -ENOENT; 1439b239a12SMasami Hiramatsu *addr = map->unmap_ip(map, sym->start) - 1449b239a12SMasami Hiramatsu ((reloc) ? 0 : map->reloc) - 1459b239a12SMasami Hiramatsu ((reladdr) ? map->start : 0); 1468f33f7deSMasami Hiramatsu } 1478f33f7deSMasami Hiramatsu return 0; 1488f33f7deSMasami Hiramatsu } 1498f33f7deSMasami Hiramatsu 150e80711caSMasami Hiramatsu static struct map *kernel_get_module_map(const char *module) 151e80711caSMasami Hiramatsu { 15268a74186SArnaldo Carvalho de Melo struct maps *maps = machine__kernel_maps(host_machine); 1534bb7123dSArnaldo Carvalho de Melo struct map *pos; 154e80711caSMasami Hiramatsu 15514a8fd7cSMasami Hiramatsu /* A file path -- this is an offline module */ 15614a8fd7cSMasami Hiramatsu if (module && strchr(module, '/')) 157eebc509bSMasami Hiramatsu return dso__new_map(module); 15814a8fd7cSMasami Hiramatsu 159eaeffeb9SAdrian Hunter if (!module) { 160eaeffeb9SAdrian Hunter pos = machine__kernel_map(host_machine); 161eaeffeb9SAdrian Hunter return map__get(pos); 162eaeffeb9SAdrian Hunter } 163e80711caSMasami Hiramatsu 1648efc4f05SArnaldo Carvalho de Melo maps__for_each_entry(maps, pos) { 165cb3f3378SKonstantin Khlebnikov /* short_name is "[module]" */ 166e80711caSMasami Hiramatsu if (strncmp(pos->dso->short_name + 1, module, 167cb3f3378SKonstantin Khlebnikov pos->dso->short_name_len - 2) == 0 && 168cb3f3378SKonstantin Khlebnikov module[pos->dso->short_name_len - 2] == '\0') { 169f622df5eSArnaldo Carvalho de Melo return map__get(pos); 170e80711caSMasami Hiramatsu } 171e80711caSMasami Hiramatsu } 172e80711caSMasami Hiramatsu return NULL; 173e80711caSMasami Hiramatsu } 174e80711caSMasami Hiramatsu 175544abd44SKrister Johansen struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) 1769b118acaSMasami Hiramatsu { 1779b118acaSMasami Hiramatsu /* Init maps of given executable or kernel */ 178544abd44SKrister Johansen if (user) { 179544abd44SKrister Johansen struct map *map; 180544abd44SKrister Johansen 181544abd44SKrister Johansen map = dso__new_map(target); 182dedeb4beSRiccardo Mancini if (map && map->dso) { 183dedeb4beSRiccardo Mancini nsinfo__put(map->dso->nsinfo); 184544abd44SKrister Johansen map->dso->nsinfo = nsinfo__get(nsi); 185dedeb4beSRiccardo Mancini } 186544abd44SKrister Johansen return map; 187544abd44SKrister Johansen } else { 1889b118acaSMasami Hiramatsu return kernel_get_module_map(target); 1899b118acaSMasami Hiramatsu } 190544abd44SKrister Johansen } 1919b118acaSMasami Hiramatsu 192fb7345bbSMasami Hiramatsu static int convert_exec_to_group(const char *exec, char **result) 193fb7345bbSMasami Hiramatsu { 194fb7345bbSMasami Hiramatsu char *ptr1, *ptr2, *exec_copy; 195fb7345bbSMasami Hiramatsu char buf[64]; 196fb7345bbSMasami Hiramatsu int ret; 197fb7345bbSMasami Hiramatsu 198fb7345bbSMasami Hiramatsu exec_copy = strdup(exec); 199fb7345bbSMasami Hiramatsu if (!exec_copy) 200fb7345bbSMasami Hiramatsu return -ENOMEM; 201fb7345bbSMasami Hiramatsu 202fb7345bbSMasami Hiramatsu ptr1 = basename(exec_copy); 203fb7345bbSMasami Hiramatsu if (!ptr1) { 204fb7345bbSMasami Hiramatsu ret = -EINVAL; 205fb7345bbSMasami Hiramatsu goto out; 206fb7345bbSMasami Hiramatsu } 207fb7345bbSMasami Hiramatsu 208ead1a574SColin Ian King for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) { 20935726d3aSMasami Hiramatsu if (!isalnum(*ptr2) && *ptr2 != '_') { 210fb7345bbSMasami Hiramatsu *ptr2 = '\0'; 21135726d3aSMasami Hiramatsu break; 21235726d3aSMasami Hiramatsu } 21335726d3aSMasami Hiramatsu } 21435726d3aSMasami Hiramatsu 215fb7345bbSMasami Hiramatsu ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); 216fb7345bbSMasami Hiramatsu if (ret < 0) 217fb7345bbSMasami Hiramatsu goto out; 218fb7345bbSMasami Hiramatsu 219fb7345bbSMasami Hiramatsu *result = strdup(buf); 220fb7345bbSMasami Hiramatsu ret = *result ? 0 : -ENOMEM; 221fb7345bbSMasami Hiramatsu 222fb7345bbSMasami Hiramatsu out: 223fb7345bbSMasami Hiramatsu free(exec_copy); 224fb7345bbSMasami Hiramatsu return ret; 225fb7345bbSMasami Hiramatsu } 226fb7345bbSMasami Hiramatsu 2279b118acaSMasami Hiramatsu static void clear_perf_probe_point(struct perf_probe_point *pp) 2289b118acaSMasami Hiramatsu { 229d8f9da24SArnaldo Carvalho de Melo zfree(&pp->file); 230d8f9da24SArnaldo Carvalho de Melo zfree(&pp->function); 231d8f9da24SArnaldo Carvalho de Melo zfree(&pp->lazy_line); 2329b118acaSMasami Hiramatsu } 2339b118acaSMasami Hiramatsu 234eb948e50SMasami Hiramatsu static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) 235eb948e50SMasami Hiramatsu { 236eb948e50SMasami Hiramatsu int i; 237eb948e50SMasami Hiramatsu 238eb948e50SMasami Hiramatsu for (i = 0; i < ntevs; i++) 239eb948e50SMasami Hiramatsu clear_probe_trace_event(tevs + i); 240eb948e50SMasami Hiramatsu } 241eb948e50SMasami Hiramatsu 24222a66551SYang Jihong static bool kprobe_blacklist__listed(u64 address); 24322a66551SYang Jihong static bool kprobe_warn_out_range(const char *symbol, u64 address) 244b031220dSMasami Hiramatsu { 2452ae5d0d7SMasami Hiramatsu struct map *map; 2462ae5d0d7SMasami Hiramatsu bool ret = false; 2477c31bb8cSHe Kuang 2482ae5d0d7SMasami Hiramatsu map = kernel_get_module_map(NULL); 2492ae5d0d7SMasami Hiramatsu if (map) { 2502ae5d0d7SMasami Hiramatsu ret = address <= map->start || map->end < address; 2512ae5d0d7SMasami Hiramatsu if (ret) 252b031220dSMasami Hiramatsu pr_warning("%s is out of .text, skip it.\n", symbol); 2532ae5d0d7SMasami Hiramatsu map__put(map); 2542ae5d0d7SMasami Hiramatsu } 2552ae5d0d7SMasami Hiramatsu if (!ret && kprobe_blacklist__listed(address)) { 256b031220dSMasami Hiramatsu pr_warning("%s is blacklisted function, skip it.\n", symbol); 2572ae5d0d7SMasami Hiramatsu ret = true; 2582ae5d0d7SMasami Hiramatsu } 259b031220dSMasami Hiramatsu 2602ae5d0d7SMasami Hiramatsu return ret; 261b031220dSMasami Hiramatsu } 262b031220dSMasami Hiramatsu 263c61fb959SRavi Bangoria /* 264c61fb959SRavi Bangoria * @module can be module name of module file path. In case of path, 265c61fb959SRavi Bangoria * inspect elf and find out what is actual module name. 266c61fb959SRavi Bangoria * Caller has to free mod_name after using it. 267c61fb959SRavi Bangoria */ 268c61fb959SRavi Bangoria static char *find_module_name(const char *module) 269c61fb959SRavi Bangoria { 270c61fb959SRavi Bangoria int fd; 271c61fb959SRavi Bangoria Elf *elf; 272c61fb959SRavi Bangoria GElf_Ehdr ehdr; 273c61fb959SRavi Bangoria GElf_Shdr shdr; 274c61fb959SRavi Bangoria Elf_Data *data; 275c61fb959SRavi Bangoria Elf_Scn *sec; 276c61fb959SRavi Bangoria char *mod_name = NULL; 2771f2ed153SMasami Hiramatsu int name_offset; 278c61fb959SRavi Bangoria 279c61fb959SRavi Bangoria fd = open(module, O_RDONLY); 280c61fb959SRavi Bangoria if (fd < 0) 281c61fb959SRavi Bangoria return NULL; 282c61fb959SRavi Bangoria 283c61fb959SRavi Bangoria elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 284c61fb959SRavi Bangoria if (elf == NULL) 285c61fb959SRavi Bangoria goto elf_err; 286c61fb959SRavi Bangoria 287c61fb959SRavi Bangoria if (gelf_getehdr(elf, &ehdr) == NULL) 288c61fb959SRavi Bangoria goto ret_err; 289c61fb959SRavi Bangoria 290c61fb959SRavi Bangoria sec = elf_section_by_name(elf, &ehdr, &shdr, 291c61fb959SRavi Bangoria ".gnu.linkonce.this_module", NULL); 292c61fb959SRavi Bangoria if (!sec) 293c61fb959SRavi Bangoria goto ret_err; 294c61fb959SRavi Bangoria 295c61fb959SRavi Bangoria data = elf_getdata(sec, NULL); 296c61fb959SRavi Bangoria if (!data || !data->d_buf) 297c61fb959SRavi Bangoria goto ret_err; 298c61fb959SRavi Bangoria 2991f2ed153SMasami Hiramatsu /* 3001f2ed153SMasami Hiramatsu * NOTE: 3011f2ed153SMasami Hiramatsu * '.gnu.linkonce.this_module' section of kernel module elf directly 3021f2ed153SMasami Hiramatsu * maps to 'struct module' from linux/module.h. This section contains 3031f2ed153SMasami Hiramatsu * actual module name which will be used by kernel after loading it. 3041f2ed153SMasami Hiramatsu * But, we cannot use 'struct module' here since linux/module.h is not 3051f2ed153SMasami Hiramatsu * exposed to user-space. Offset of 'name' has remained same from long 3061f2ed153SMasami Hiramatsu * time, so hardcoding it here. 3071f2ed153SMasami Hiramatsu */ 3081f2ed153SMasami Hiramatsu if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 3091f2ed153SMasami Hiramatsu name_offset = 12; 3101f2ed153SMasami Hiramatsu else /* expect ELFCLASS64 by default */ 3111f2ed153SMasami Hiramatsu name_offset = 24; 3121f2ed153SMasami Hiramatsu 3131f2ed153SMasami Hiramatsu mod_name = strdup((char *)data->d_buf + name_offset); 314c61fb959SRavi Bangoria 315c61fb959SRavi Bangoria ret_err: 316c61fb959SRavi Bangoria elf_end(elf); 317c61fb959SRavi Bangoria elf_err: 318c61fb959SRavi Bangoria close(fd); 319c61fb959SRavi Bangoria return mod_name; 320c61fb959SRavi Bangoria } 321c61fb959SRavi Bangoria 32289fe808aSIngo Molnar #ifdef HAVE_DWARF_SUPPORT 32360fb7742SWang Nan 32460fb7742SWang Nan static int kernel_get_module_dso(const char *module, struct dso **pdso) 32560fb7742SWang Nan { 32660fb7742SWang Nan struct dso *dso; 32760fb7742SWang Nan struct map *map; 32860fb7742SWang Nan const char *vmlinux_name; 32960fb7742SWang Nan int ret = 0; 33060fb7742SWang Nan 33160fb7742SWang Nan if (module) { 332266fa2b2SArnaldo Carvalho de Melo char module_name[128]; 333266fa2b2SArnaldo Carvalho de Melo 334266fa2b2SArnaldo Carvalho de Melo snprintf(module_name, sizeof(module_name), "[%s]", module); 3351a97cee6SIan Rogers map = maps__find_by_name(machine__kernel_maps(host_machine), module_name); 336266fa2b2SArnaldo Carvalho de Melo if (map) { 337266fa2b2SArnaldo Carvalho de Melo dso = map->dso; 33860fb7742SWang Nan goto found; 33960fb7742SWang Nan } 34060fb7742SWang Nan pr_debug("Failed to find module %s.\n", module); 34160fb7742SWang Nan return -ENOENT; 34260fb7742SWang Nan } 34360fb7742SWang Nan 344a5e813c6SArnaldo Carvalho de Melo map = machine__kernel_map(host_machine); 34560fb7742SWang Nan dso = map->dso; 3467cd5738dSMasami Hiramatsu if (!dso->has_build_id) 3477cd5738dSMasami Hiramatsu dso__read_running_kernel_build_id(dso, host_machine); 34860fb7742SWang Nan 34960fb7742SWang Nan vmlinux_name = symbol_conf.vmlinux_name; 35060fb7742SWang Nan dso->load_errno = 0; 35160fb7742SWang Nan if (vmlinux_name) 352be39db9fSArnaldo Carvalho de Melo ret = dso__load_vmlinux(dso, map, vmlinux_name, false); 35360fb7742SWang Nan else 354be39db9fSArnaldo Carvalho de Melo ret = dso__load_vmlinux_path(dso, map); 35560fb7742SWang Nan found: 35660fb7742SWang Nan *pdso = dso; 35760fb7742SWang Nan return ret; 35860fb7742SWang Nan } 35960fb7742SWang Nan 3609b118acaSMasami Hiramatsu /* 3619b118acaSMasami Hiramatsu * Some binaries like glibc have special symbols which are on the symbol 3629b118acaSMasami Hiramatsu * table, but not in the debuginfo. If we can find the address of the 3639b118acaSMasami Hiramatsu * symbol from map, we can translate the address back to the probe point. 3649b118acaSMasami Hiramatsu */ 3659b118acaSMasami Hiramatsu static int find_alternative_probe_point(struct debuginfo *dinfo, 3669b118acaSMasami Hiramatsu struct perf_probe_point *pp, 3679b118acaSMasami Hiramatsu struct perf_probe_point *result, 368544abd44SKrister Johansen const char *target, struct nsinfo *nsi, 369544abd44SKrister Johansen bool uprobes) 3709b118acaSMasami Hiramatsu { 3719b118acaSMasami Hiramatsu struct map *map = NULL; 3729b118acaSMasami Hiramatsu struct symbol *sym; 3739b118acaSMasami Hiramatsu u64 address = 0; 3749b118acaSMasami Hiramatsu int ret = -ENOENT; 3759b118acaSMasami Hiramatsu 3769b118acaSMasami Hiramatsu /* This can work only for function-name based one */ 3779b118acaSMasami Hiramatsu if (!pp->function || pp->file) 3789b118acaSMasami Hiramatsu return -ENOTSUP; 3799b118acaSMasami Hiramatsu 380544abd44SKrister Johansen map = get_target_map(target, nsi, uprobes); 3819b118acaSMasami Hiramatsu if (!map) 3829b118acaSMasami Hiramatsu return -EINVAL; 3839b118acaSMasami Hiramatsu 3849b118acaSMasami Hiramatsu /* Find the address of given function */ 3859b118acaSMasami Hiramatsu map__for_each_symbol_by_name(map, pp->function, sym) { 3863de2bf9dSMasami Hiramatsu if (uprobes) { 3879b118acaSMasami Hiramatsu address = sym->start; 3883de2bf9dSMasami Hiramatsu if (sym->type == STT_GNU_IFUNC) 3893de2bf9dSMasami Hiramatsu pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n" 3903de2bf9dSMasami Hiramatsu "Consider identifying the final function used at run time and set the probe directly on that.\n", 3913de2bf9dSMasami Hiramatsu pp->function); 3923de2bf9dSMasami Hiramatsu } else 3938e34189bSMasami Hiramatsu address = map->unmap_ip(map, sym->start) - map->reloc; 3949b118acaSMasami Hiramatsu break; 3959b118acaSMasami Hiramatsu } 3969b118acaSMasami Hiramatsu if (!address) { 3979b118acaSMasami Hiramatsu ret = -ENOENT; 3989b118acaSMasami Hiramatsu goto out; 3999b118acaSMasami Hiramatsu } 400f6c15621SWang Nan pr_debug("Symbol %s address found : %" PRIx64 "\n", 401f6c15621SWang Nan pp->function, address); 4029b118acaSMasami Hiramatsu 40322a66551SYang Jihong ret = debuginfo__find_probe_point(dinfo, address, result); 4049b118acaSMasami Hiramatsu if (ret <= 0) 4059b118acaSMasami Hiramatsu ret = (!ret) ? -ENOENT : ret; 4069b118acaSMasami Hiramatsu else { 4079b118acaSMasami Hiramatsu result->offset += pp->offset; 4089b118acaSMasami Hiramatsu result->line += pp->line; 4099d7b45c5SHe Kuang result->retprobe = pp->retprobe; 4109b118acaSMasami Hiramatsu ret = 0; 4119b118acaSMasami Hiramatsu } 4129b118acaSMasami Hiramatsu 4139b118acaSMasami Hiramatsu out: 414eebc509bSMasami Hiramatsu map__put(map); 4159b118acaSMasami Hiramatsu return ret; 4169b118acaSMasami Hiramatsu 4179b118acaSMasami Hiramatsu } 4189b118acaSMasami Hiramatsu 4199b118acaSMasami Hiramatsu static int get_alternative_probe_event(struct debuginfo *dinfo, 4209b118acaSMasami Hiramatsu struct perf_probe_event *pev, 42144225521SMasami Hiramatsu struct perf_probe_point *tmp) 4229b118acaSMasami Hiramatsu { 4239b118acaSMasami Hiramatsu int ret; 4249b118acaSMasami Hiramatsu 4259b118acaSMasami Hiramatsu memcpy(tmp, &pev->point, sizeof(*tmp)); 4269b118acaSMasami Hiramatsu memset(&pev->point, 0, sizeof(pev->point)); 427544abd44SKrister Johansen ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target, 428544abd44SKrister Johansen pev->nsi, pev->uprobes); 4299b118acaSMasami Hiramatsu if (ret < 0) 4309b118acaSMasami Hiramatsu memcpy(&pev->point, tmp, sizeof(*tmp)); 4319b118acaSMasami Hiramatsu 4329b118acaSMasami Hiramatsu return ret; 4339b118acaSMasami Hiramatsu } 434a15ad2f5SMasami Hiramatsu 435811dd2aeSMasami Hiramatsu static int get_alternative_line_range(struct debuginfo *dinfo, 436811dd2aeSMasami Hiramatsu struct line_range *lr, 437811dd2aeSMasami Hiramatsu const char *target, bool user) 438811dd2aeSMasami Hiramatsu { 4396d4a4896SDavid Ahern struct perf_probe_point pp = { .function = lr->function, 4406d4a4896SDavid Ahern .file = lr->file, 4416d4a4896SDavid Ahern .line = lr->start }; 4426d4a4896SDavid Ahern struct perf_probe_point result; 443811dd2aeSMasami Hiramatsu int ret, len = 0; 444811dd2aeSMasami Hiramatsu 4456d4a4896SDavid Ahern memset(&result, 0, sizeof(result)); 4466d4a4896SDavid Ahern 447811dd2aeSMasami Hiramatsu if (lr->end != INT_MAX) 448811dd2aeSMasami Hiramatsu len = lr->end - lr->start; 449811dd2aeSMasami Hiramatsu ret = find_alternative_probe_point(dinfo, &pp, &result, 450544abd44SKrister Johansen target, NULL, user); 451811dd2aeSMasami Hiramatsu if (!ret) { 452811dd2aeSMasami Hiramatsu lr->function = result.function; 453811dd2aeSMasami Hiramatsu lr->file = result.file; 454811dd2aeSMasami Hiramatsu lr->start = result.line; 455811dd2aeSMasami Hiramatsu if (lr->end != INT_MAX) 456811dd2aeSMasami Hiramatsu lr->end = lr->start + len; 457811dd2aeSMasami Hiramatsu clear_perf_probe_point(&pp); 458811dd2aeSMasami Hiramatsu } 459811dd2aeSMasami Hiramatsu return ret; 460811dd2aeSMasami Hiramatsu } 461811dd2aeSMasami Hiramatsu 4627cd5738dSMasami Hiramatsu #ifdef HAVE_DEBUGINFOD_SUPPORT 4637cd5738dSMasami Hiramatsu static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi, 4647cd5738dSMasami Hiramatsu bool silent) 4657cd5738dSMasami Hiramatsu { 4667cd5738dSMasami Hiramatsu debuginfod_client *c = debuginfod_begin(); 4677cd5738dSMasami Hiramatsu char sbuild_id[SBUILD_ID_SIZE + 1]; 4687cd5738dSMasami Hiramatsu struct debuginfo *ret = NULL; 4697cd5738dSMasami Hiramatsu struct nscookie nsc; 4707cd5738dSMasami Hiramatsu char *path; 4717cd5738dSMasami Hiramatsu int fd; 4727cd5738dSMasami Hiramatsu 4737cd5738dSMasami Hiramatsu if (!c) 4747cd5738dSMasami Hiramatsu return NULL; 4757cd5738dSMasami Hiramatsu 476bf541169SJiri Olsa build_id__sprintf(&dso->bid, sbuild_id); 4777cd5738dSMasami Hiramatsu fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id, 4787cd5738dSMasami Hiramatsu 0, &path); 4797cd5738dSMasami Hiramatsu if (fd >= 0) 4807cd5738dSMasami Hiramatsu close(fd); 4817cd5738dSMasami Hiramatsu debuginfod_end(c); 4827cd5738dSMasami Hiramatsu if (fd < 0) { 4837cd5738dSMasami Hiramatsu if (!silent) 4847cd5738dSMasami Hiramatsu pr_debug("Failed to find debuginfo in debuginfod.\n"); 4857cd5738dSMasami Hiramatsu return NULL; 4867cd5738dSMasami Hiramatsu } 4877cd5738dSMasami Hiramatsu if (!silent) 4887cd5738dSMasami Hiramatsu pr_debug("Load debuginfo from debuginfod (%s)\n", path); 4897cd5738dSMasami Hiramatsu 4907cd5738dSMasami Hiramatsu nsinfo__mountns_enter(nsi, &nsc); 4917cd5738dSMasami Hiramatsu ret = debuginfo__new((const char *)path); 4927cd5738dSMasami Hiramatsu nsinfo__mountns_exit(&nsc); 4937cd5738dSMasami Hiramatsu return ret; 4947cd5738dSMasami Hiramatsu } 4957cd5738dSMasami Hiramatsu #else 4967cd5738dSMasami Hiramatsu static inline 4977cd5738dSMasami Hiramatsu struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused, 4987cd5738dSMasami Hiramatsu struct nsinfo *nsi __maybe_unused, 4997cd5738dSMasami Hiramatsu bool silent __maybe_unused) 5007cd5738dSMasami Hiramatsu { 5017cd5738dSMasami Hiramatsu return NULL; 5027cd5738dSMasami Hiramatsu } 5037cd5738dSMasami Hiramatsu #endif 5047cd5738dSMasami Hiramatsu 505ff741783SMasami Hiramatsu /* Open new debuginfo of given module */ 506544abd44SKrister Johansen static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi, 507544abd44SKrister Johansen bool silent) 508469b9b88SMasami Hiramatsu { 509a15ad2f5SMasami Hiramatsu const char *path = module; 510419e8738SMasami Hiramatsu char reason[STRERR_BUFSIZE]; 511419e8738SMasami Hiramatsu struct debuginfo *ret = NULL; 512419e8738SMasami Hiramatsu struct dso *dso = NULL; 513544abd44SKrister Johansen struct nscookie nsc; 514419e8738SMasami Hiramatsu int err; 51514a8fd7cSMasami Hiramatsu 516a15ad2f5SMasami Hiramatsu if (!module || !strchr(module, '/')) { 517419e8738SMasami Hiramatsu err = kernel_get_module_dso(module, &dso); 518419e8738SMasami Hiramatsu if (err < 0) { 519419e8738SMasami Hiramatsu if (!dso || dso->load_errno == 0) { 520c8b5f2c9SArnaldo Carvalho de Melo if (!str_error_r(-err, reason, STRERR_BUFSIZE)) 521419e8738SMasami Hiramatsu strcpy(reason, "(unknown)"); 522419e8738SMasami Hiramatsu } else 523419e8738SMasami Hiramatsu dso__strerror_load(dso, reason, STRERR_BUFSIZE); 5247cd5738dSMasami Hiramatsu if (dso) 5257cd5738dSMasami Hiramatsu ret = open_from_debuginfod(dso, nsi, silent); 5267cd5738dSMasami Hiramatsu if (ret) 5277cd5738dSMasami Hiramatsu return ret; 5284d6101f5SArnaldo Carvalho de Melo if (!silent) { 5294d6101f5SArnaldo Carvalho de Melo if (module) 5304d6101f5SArnaldo Carvalho de Melo pr_err("Module %s is not loaded, please specify its full path name.\n", module); 5314d6101f5SArnaldo Carvalho de Melo else 5324d6101f5SArnaldo Carvalho de Melo pr_err("Failed to find the path for the kernel: %s\n", reason); 5334d6101f5SArnaldo Carvalho de Melo } 534ff741783SMasami Hiramatsu return NULL; 535469b9b88SMasami Hiramatsu } 536419e8738SMasami Hiramatsu path = dso->long_name; 53714a8fd7cSMasami Hiramatsu } 538544abd44SKrister Johansen nsinfo__mountns_enter(nsi, &nsc); 53992561cb7SMasami Hiramatsu ret = debuginfo__new(path); 54092561cb7SMasami Hiramatsu if (!ret && !silent) { 54192561cb7SMasami Hiramatsu pr_warning("The %s file has no debug information.\n", path); 54292561cb7SMasami Hiramatsu if (!module || !strtailcmp(path, ".ko")) 54392561cb7SMasami Hiramatsu pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); 54492561cb7SMasami Hiramatsu else 54592561cb7SMasami Hiramatsu pr_warning("Rebuild with -g, "); 54692561cb7SMasami Hiramatsu pr_warning("or install an appropriate debuginfo package.\n"); 547e0faa8d3SMasami Hiramatsu } 548544abd44SKrister Johansen nsinfo__mountns_exit(&nsc); 54992561cb7SMasami Hiramatsu return ret; 55092561cb7SMasami Hiramatsu } 55192561cb7SMasami Hiramatsu 5527737af01SMasami Hiramatsu /* For caching the last debuginfo */ 5537737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache; 5547737af01SMasami Hiramatsu static char *debuginfo_cache_path; 5557737af01SMasami Hiramatsu 5567737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache__open(const char *module, bool silent) 5577737af01SMasami Hiramatsu { 55820f49859SMasami Hiramatsu const char *path = module; 55920f49859SMasami Hiramatsu 56020f49859SMasami Hiramatsu /* If the module is NULL, it should be the kernel. */ 56120f49859SMasami Hiramatsu if (!module) 56220f49859SMasami Hiramatsu path = "kernel"; 56320f49859SMasami Hiramatsu 56420f49859SMasami Hiramatsu if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path)) 5657737af01SMasami Hiramatsu goto out; 5667737af01SMasami Hiramatsu 5677737af01SMasami Hiramatsu /* Copy module path */ 5687737af01SMasami Hiramatsu free(debuginfo_cache_path); 56920f49859SMasami Hiramatsu debuginfo_cache_path = strdup(path); 5707737af01SMasami Hiramatsu if (!debuginfo_cache_path) { 5717737af01SMasami Hiramatsu debuginfo__delete(debuginfo_cache); 5727737af01SMasami Hiramatsu debuginfo_cache = NULL; 5737737af01SMasami Hiramatsu goto out; 5747737af01SMasami Hiramatsu } 5757737af01SMasami Hiramatsu 576544abd44SKrister Johansen debuginfo_cache = open_debuginfo(module, NULL, silent); 5777737af01SMasami Hiramatsu if (!debuginfo_cache) 5787737af01SMasami Hiramatsu zfree(&debuginfo_cache_path); 5797737af01SMasami Hiramatsu out: 5807737af01SMasami Hiramatsu return debuginfo_cache; 5817737af01SMasami Hiramatsu } 5827737af01SMasami Hiramatsu 5837737af01SMasami Hiramatsu static void debuginfo_cache__exit(void) 5847737af01SMasami Hiramatsu { 5857737af01SMasami Hiramatsu debuginfo__delete(debuginfo_cache); 5867737af01SMasami Hiramatsu debuginfo_cache = NULL; 5877737af01SMasami Hiramatsu zfree(&debuginfo_cache_path); 5887737af01SMasami Hiramatsu } 5897737af01SMasami Hiramatsu 5904b4da7f7SMasami Hiramatsu 59122a66551SYang Jihong static int get_text_start_address(const char *exec, u64 *address, 592544abd44SKrister Johansen struct nsinfo *nsi) 59399ca4233SMasami Hiramatsu { 59499ca4233SMasami Hiramatsu Elf *elf; 59599ca4233SMasami Hiramatsu GElf_Ehdr ehdr; 59699ca4233SMasami Hiramatsu GElf_Shdr shdr; 59799ca4233SMasami Hiramatsu int fd, ret = -ENOENT; 598544abd44SKrister Johansen struct nscookie nsc; 59999ca4233SMasami Hiramatsu 600544abd44SKrister Johansen nsinfo__mountns_enter(nsi, &nsc); 60199ca4233SMasami Hiramatsu fd = open(exec, O_RDONLY); 602544abd44SKrister Johansen nsinfo__mountns_exit(&nsc); 60399ca4233SMasami Hiramatsu if (fd < 0) 60499ca4233SMasami Hiramatsu return -errno; 60599ca4233SMasami Hiramatsu 60699ca4233SMasami Hiramatsu elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 607062d6c2aSMasami Hiramatsu if (elf == NULL) { 608062d6c2aSMasami Hiramatsu ret = -EINVAL; 609062d6c2aSMasami Hiramatsu goto out_close; 610062d6c2aSMasami Hiramatsu } 61199ca4233SMasami Hiramatsu 61299ca4233SMasami Hiramatsu if (gelf_getehdr(elf, &ehdr) == NULL) 61399ca4233SMasami Hiramatsu goto out; 61499ca4233SMasami Hiramatsu 61599ca4233SMasami Hiramatsu if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) 61699ca4233SMasami Hiramatsu goto out; 61799ca4233SMasami Hiramatsu 61899ca4233SMasami Hiramatsu *address = shdr.sh_addr - shdr.sh_offset; 61999ca4233SMasami Hiramatsu ret = 0; 62099ca4233SMasami Hiramatsu out: 62199ca4233SMasami Hiramatsu elf_end(elf); 622062d6c2aSMasami Hiramatsu out_close: 623062d6c2aSMasami Hiramatsu close(fd); 624062d6c2aSMasami Hiramatsu 62599ca4233SMasami Hiramatsu return ret; 62699ca4233SMasami Hiramatsu } 62799ca4233SMasami Hiramatsu 6285a6f6314SMasami Hiramatsu /* 6295a6f6314SMasami Hiramatsu * Convert trace point to probe point with debuginfo 6305a6f6314SMasami Hiramatsu */ 6315a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, 6325a6f6314SMasami Hiramatsu struct perf_probe_point *pp, 6335a6f6314SMasami Hiramatsu bool is_kprobe) 6345a6f6314SMasami Hiramatsu { 6355a6f6314SMasami Hiramatsu struct debuginfo *dinfo = NULL; 63622a66551SYang Jihong u64 stext = 0; 6375a6f6314SMasami Hiramatsu u64 addr = tp->address; 6385a6f6314SMasami Hiramatsu int ret = -ENOENT; 6395a6f6314SMasami Hiramatsu 6405a6f6314SMasami Hiramatsu /* convert the address to dwarf address */ 6415a6f6314SMasami Hiramatsu if (!is_kprobe) { 6425a6f6314SMasami Hiramatsu if (!addr) { 6435a6f6314SMasami Hiramatsu ret = -EINVAL; 6445a6f6314SMasami Hiramatsu goto error; 6455a6f6314SMasami Hiramatsu } 646544abd44SKrister Johansen ret = get_text_start_address(tp->module, &stext, NULL); 6475a6f6314SMasami Hiramatsu if (ret < 0) 6485a6f6314SMasami Hiramatsu goto error; 6495a6f6314SMasami Hiramatsu addr += stext; 650e486367fSWang Nan } else if (tp->symbol) { 6519b239a12SMasami Hiramatsu /* If the module is given, this returns relative address */ 6529b239a12SMasami Hiramatsu ret = kernel_get_symbol_address_by_name(tp->symbol, &addr, 6539b239a12SMasami Hiramatsu false, !!tp->module); 6549b239a12SMasami Hiramatsu if (ret != 0) 6555a6f6314SMasami Hiramatsu goto error; 6565a6f6314SMasami Hiramatsu addr += tp->offset; 6575a6f6314SMasami Hiramatsu } 6585a6f6314SMasami Hiramatsu 6595a6f6314SMasami Hiramatsu pr_debug("try to find information at %" PRIx64 " in %s\n", addr, 6605a6f6314SMasami Hiramatsu tp->module ? : "kernel"); 6615a6f6314SMasami Hiramatsu 662bb963e16SNamhyung Kim dinfo = debuginfo_cache__open(tp->module, verbose <= 0); 6637737af01SMasami Hiramatsu if (dinfo) 66422a66551SYang Jihong ret = debuginfo__find_probe_point(dinfo, addr, pp); 6657737af01SMasami Hiramatsu else 6665a6f6314SMasami Hiramatsu ret = -ENOENT; 6675a6f6314SMasami Hiramatsu 6685a6f6314SMasami Hiramatsu if (ret > 0) { 6695a6f6314SMasami Hiramatsu pp->retprobe = tp->retprobe; 6705a6f6314SMasami Hiramatsu return 0; 6715a6f6314SMasami Hiramatsu } 6725a6f6314SMasami Hiramatsu error: 6735a6f6314SMasami Hiramatsu pr_debug("Failed to find corresponding probes from debuginfo.\n"); 6745a6f6314SMasami Hiramatsu return ret ? : -ENOENT; 6755a6f6314SMasami Hiramatsu } 6765a6f6314SMasami Hiramatsu 6773e96dac7SMasami Hiramatsu /* Adjust symbol name and address */ 6783e96dac7SMasami Hiramatsu static int post_process_probe_trace_point(struct probe_trace_point *tp, 67922a66551SYang Jihong struct map *map, u64 offs) 6803e96dac7SMasami Hiramatsu { 6813e96dac7SMasami Hiramatsu struct symbol *sym; 6827598f8bcSBjörn Töpel u64 addr = tp->address - offs; 6833e96dac7SMasami Hiramatsu 6843e96dac7SMasami Hiramatsu sym = map__find_symbol(map, addr); 685f338de22SMasami Hiramatsu if (!sym) { 686f338de22SMasami Hiramatsu /* 687f338de22SMasami Hiramatsu * If the address is in the inittext section, map can not 688f338de22SMasami Hiramatsu * find it. Ignore it if we are probing offline kernel. 689f338de22SMasami Hiramatsu */ 690f338de22SMasami Hiramatsu return (symbol_conf.ignore_vmlinux_buildid) ? 0 : -ENOENT; 691f338de22SMasami Hiramatsu } 6923e96dac7SMasami Hiramatsu 6933e96dac7SMasami Hiramatsu if (strcmp(sym->name, tp->symbol)) { 6943e96dac7SMasami Hiramatsu /* If we have no realname, use symbol for it */ 6953e96dac7SMasami Hiramatsu if (!tp->realname) 6963e96dac7SMasami Hiramatsu tp->realname = tp->symbol; 6973e96dac7SMasami Hiramatsu else 6983e96dac7SMasami Hiramatsu free(tp->symbol); 6993e96dac7SMasami Hiramatsu tp->symbol = strdup(sym->name); 7003e96dac7SMasami Hiramatsu if (!tp->symbol) 7013e96dac7SMasami Hiramatsu return -ENOMEM; 7023e96dac7SMasami Hiramatsu } 7033e96dac7SMasami Hiramatsu tp->offset = addr - sym->start; 7043e96dac7SMasami Hiramatsu tp->address -= offs; 7053e96dac7SMasami Hiramatsu 7063e96dac7SMasami Hiramatsu return 0; 7073e96dac7SMasami Hiramatsu } 7083e96dac7SMasami Hiramatsu 7098a937a25SMasami Hiramatsu /* 7108a937a25SMasami Hiramatsu * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions 7118a937a25SMasami Hiramatsu * and generate new symbols with suffixes such as .constprop.N or .isra.N 7128a937a25SMasami Hiramatsu * etc. Since those symbols are not recorded in DWARF, we have to find 7138a937a25SMasami Hiramatsu * correct generated symbols from offline ELF binary. 7148a937a25SMasami Hiramatsu * For online kernel or uprobes we don't need this because those are 7158a937a25SMasami Hiramatsu * rebased on _text, or already a section relative address. 7168a937a25SMasami Hiramatsu */ 7178a937a25SMasami Hiramatsu static int 7188a937a25SMasami Hiramatsu post_process_offline_probe_trace_events(struct probe_trace_event *tevs, 7198a937a25SMasami Hiramatsu int ntevs, const char *pathname) 7208a937a25SMasami Hiramatsu { 7218a937a25SMasami Hiramatsu struct map *map; 72222a66551SYang Jihong u64 stext = 0; 7233e96dac7SMasami Hiramatsu int i, ret = 0; 7248a937a25SMasami Hiramatsu 7258a937a25SMasami Hiramatsu /* Prepare a map for offline binary */ 7268a937a25SMasami Hiramatsu map = dso__new_map(pathname); 727544abd44SKrister Johansen if (!map || get_text_start_address(pathname, &stext, NULL) < 0) { 7288a937a25SMasami Hiramatsu pr_warning("Failed to get ELF symbols for %s\n", pathname); 7298a937a25SMasami Hiramatsu return -EINVAL; 7308a937a25SMasami Hiramatsu } 7318a937a25SMasami Hiramatsu 7328a937a25SMasami Hiramatsu for (i = 0; i < ntevs; i++) { 7333e96dac7SMasami Hiramatsu ret = post_process_probe_trace_point(&tevs[i].point, 7343e96dac7SMasami Hiramatsu map, stext); 7353e96dac7SMasami Hiramatsu if (ret < 0) 7363e96dac7SMasami Hiramatsu break; 7378a937a25SMasami Hiramatsu } 7388a937a25SMasami Hiramatsu map__put(map); 7398a937a25SMasami Hiramatsu 7403e96dac7SMasami Hiramatsu return ret; 7418a937a25SMasami Hiramatsu } 7428a937a25SMasami Hiramatsu 743fb7345bbSMasami Hiramatsu static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, 744544abd44SKrister Johansen int ntevs, const char *exec, 745544abd44SKrister Johansen struct nsinfo *nsi) 746fb7345bbSMasami Hiramatsu { 747fb7345bbSMasami Hiramatsu int i, ret = 0; 74822a66551SYang Jihong u64 stext = 0; 749fb7345bbSMasami Hiramatsu 750fb7345bbSMasami Hiramatsu if (!exec) 751fb7345bbSMasami Hiramatsu return 0; 752fb7345bbSMasami Hiramatsu 753544abd44SKrister Johansen ret = get_text_start_address(exec, &stext, nsi); 754fb7345bbSMasami Hiramatsu if (ret < 0) 755fb7345bbSMasami Hiramatsu return ret; 756fb7345bbSMasami Hiramatsu 757fb7345bbSMasami Hiramatsu for (i = 0; i < ntevs && ret >= 0; i++) { 758adba1634SIngo Molnar /* point.address is the address of point.symbol + point.offset */ 759eb948e50SMasami Hiramatsu tevs[i].point.address -= stext; 760fb7345bbSMasami Hiramatsu tevs[i].point.module = strdup(exec); 761eb948e50SMasami Hiramatsu if (!tevs[i].point.module) { 762fb7345bbSMasami Hiramatsu ret = -ENOMEM; 763fb7345bbSMasami Hiramatsu break; 764fb7345bbSMasami Hiramatsu } 765fb7345bbSMasami Hiramatsu tevs[i].uprobes = true; 766fb7345bbSMasami Hiramatsu } 767fb7345bbSMasami Hiramatsu 768fb7345bbSMasami Hiramatsu return ret; 769fb7345bbSMasami Hiramatsu } 770fb7345bbSMasami Hiramatsu 771613f050dSMasami Hiramatsu static int 772613f050dSMasami Hiramatsu post_process_module_probe_trace_events(struct probe_trace_event *tevs, 773613f050dSMasami Hiramatsu int ntevs, const char *module, 774613f050dSMasami Hiramatsu struct debuginfo *dinfo) 775190b57fcSMasami Hiramatsu { 776613f050dSMasami Hiramatsu Dwarf_Addr text_offs = 0; 77714a8fd7cSMasami Hiramatsu int i, ret = 0; 77863a29613SRavi Bangoria char *mod_name = NULL; 779613f050dSMasami Hiramatsu struct map *map; 78014a8fd7cSMasami Hiramatsu 78114a8fd7cSMasami Hiramatsu if (!module) 78214a8fd7cSMasami Hiramatsu return 0; 78314a8fd7cSMasami Hiramatsu 784544abd44SKrister Johansen map = get_target_map(module, NULL, false); 785613f050dSMasami Hiramatsu if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) { 786613f050dSMasami Hiramatsu pr_warning("Failed to get ELF symbols for %s\n", module); 787613f050dSMasami Hiramatsu return -EINVAL; 788613f050dSMasami Hiramatsu } 78914a8fd7cSMasami Hiramatsu 790613f050dSMasami Hiramatsu mod_name = find_module_name(module); 791190b57fcSMasami Hiramatsu for (i = 0; i < ntevs; i++) { 792613f050dSMasami Hiramatsu ret = post_process_probe_trace_point(&tevs[i].point, 79322a66551SYang Jihong map, text_offs); 794613f050dSMasami Hiramatsu if (ret < 0) 795613f050dSMasami Hiramatsu break; 79663a29613SRavi Bangoria tevs[i].point.module = 79763a29613SRavi Bangoria strdup(mod_name ? mod_name : module); 79814a8fd7cSMasami Hiramatsu if (!tevs[i].point.module) { 79914a8fd7cSMasami Hiramatsu ret = -ENOMEM; 80014a8fd7cSMasami Hiramatsu break; 801190b57fcSMasami Hiramatsu } 80214a8fd7cSMasami Hiramatsu } 80314a8fd7cSMasami Hiramatsu 80463a29613SRavi Bangoria free(mod_name); 805613f050dSMasami Hiramatsu map__put(map); 806613f050dSMasami Hiramatsu 80714a8fd7cSMasami Hiramatsu return ret; 808190b57fcSMasami Hiramatsu } 809190b57fcSMasami Hiramatsu 810d820456dSRavi Bangoria static int 811d820456dSRavi Bangoria post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, 812d820456dSRavi Bangoria int ntevs) 813dfef99cdSMasami Hiramatsu { 814dfef99cdSMasami Hiramatsu struct ref_reloc_sym *reloc_sym; 81580526491SMasami Hiramatsu struct map *map; 816dfef99cdSMasami Hiramatsu char *tmp; 8175a51fcd1SMasami Hiramatsu int i, skipped = 0; 818dfef99cdSMasami Hiramatsu 819428aff82SMasami Hiramatsu /* Skip post process if the target is an offline kernel */ 820428aff82SMasami Hiramatsu if (symbol_conf.ignore_vmlinux_buildid) 8218a937a25SMasami Hiramatsu return post_process_offline_probe_trace_events(tevs, ntevs, 8228a937a25SMasami Hiramatsu symbol_conf.vmlinux_name); 823428aff82SMasami Hiramatsu 82480526491SMasami Hiramatsu reloc_sym = kernel_get_ref_reloc_sym(&map); 825dfef99cdSMasami Hiramatsu if (!reloc_sym) { 82641ca1d1eSRavi Bangoria pr_warning("Relocated base symbol is not found! " 82741ca1d1eSRavi Bangoria "Check /proc/sys/kernel/kptr_restrict\n" 82841ca1d1eSRavi Bangoria "and /proc/sys/kernel/perf_event_paranoid. " 82941ca1d1eSRavi Bangoria "Or run as privileged perf user.\n\n"); 830dfef99cdSMasami Hiramatsu return -EINVAL; 831dfef99cdSMasami Hiramatsu } 832dfef99cdSMasami Hiramatsu 833dfef99cdSMasami Hiramatsu for (i = 0; i < ntevs; i++) { 8347ab31d94SNaveen N. Rao if (!tevs[i].point.address) 8357ab31d94SNaveen N. Rao continue; 8367ab31d94SNaveen N. Rao if (tevs[i].point.retprobe && !kretprobe_offset_is_supported()) 837b031220dSMasami Hiramatsu continue; 83880526491SMasami Hiramatsu /* 83980526491SMasami Hiramatsu * If we found a wrong one, mark it by NULL symbol. 84080526491SMasami Hiramatsu * Since addresses in debuginfo is same as objdump, we need 84180526491SMasami Hiramatsu * to convert it to addresses on memory. 84280526491SMasami Hiramatsu */ 843b031220dSMasami Hiramatsu if (kprobe_warn_out_range(tevs[i].point.symbol, 84480526491SMasami Hiramatsu map__objdump_2mem(map, tevs[i].point.address))) { 8455a51fcd1SMasami Hiramatsu tmp = NULL; 8465a51fcd1SMasami Hiramatsu skipped++; 8475a51fcd1SMasami Hiramatsu } else { 848dfef99cdSMasami Hiramatsu tmp = strdup(reloc_sym->name); 849dfef99cdSMasami Hiramatsu if (!tmp) 850dfef99cdSMasami Hiramatsu return -ENOMEM; 8515a51fcd1SMasami Hiramatsu } 8524c859351SMasami Hiramatsu /* If we have no realname, use symbol for it */ 8534c859351SMasami Hiramatsu if (!tevs[i].point.realname) 8544c859351SMasami Hiramatsu tevs[i].point.realname = tevs[i].point.symbol; 8554c859351SMasami Hiramatsu else 856dfef99cdSMasami Hiramatsu free(tevs[i].point.symbol); 857dfef99cdSMasami Hiramatsu tevs[i].point.symbol = tmp; 858dfef99cdSMasami Hiramatsu tevs[i].point.offset = tevs[i].point.address - 859ac7a75d1SMasami Hiramatsu (map->reloc ? reloc_sym->unrelocated_addr : 860ac7a75d1SMasami Hiramatsu reloc_sym->addr); 861dfef99cdSMasami Hiramatsu } 8625a51fcd1SMasami Hiramatsu return skipped; 863dfef99cdSMasami Hiramatsu } 864dfef99cdSMasami Hiramatsu 86599e608b5SRavi Bangoria void __weak 86699e608b5SRavi Bangoria arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused, 86799e608b5SRavi Bangoria int ntevs __maybe_unused) 86899e608b5SRavi Bangoria { 86999e608b5SRavi Bangoria } 87099e608b5SRavi Bangoria 871d820456dSRavi Bangoria /* Post processing the probe events */ 87299e608b5SRavi Bangoria static int post_process_probe_trace_events(struct perf_probe_event *pev, 87399e608b5SRavi Bangoria struct probe_trace_event *tevs, 874d820456dSRavi Bangoria int ntevs, const char *module, 875613f050dSMasami Hiramatsu bool uprobe, struct debuginfo *dinfo) 876d820456dSRavi Bangoria { 87799e608b5SRavi Bangoria int ret; 87899e608b5SRavi Bangoria 879d820456dSRavi Bangoria if (uprobe) 880544abd44SKrister Johansen ret = add_exec_to_probe_trace_events(tevs, ntevs, module, 881544abd44SKrister Johansen pev->nsi); 88299e608b5SRavi Bangoria else if (module) 883d820456dSRavi Bangoria /* Currently ref_reloc_sym based probe is not for drivers */ 884613f050dSMasami Hiramatsu ret = post_process_module_probe_trace_events(tevs, ntevs, 885613f050dSMasami Hiramatsu module, dinfo); 88699e608b5SRavi Bangoria else 88799e608b5SRavi Bangoria ret = post_process_kernel_probe_trace_events(tevs, ntevs); 888d820456dSRavi Bangoria 88999e608b5SRavi Bangoria if (ret >= 0) 89099e608b5SRavi Bangoria arch__post_process_probe_trace_events(pev, ntevs); 89199e608b5SRavi Bangoria 89299e608b5SRavi Bangoria return ret; 893d820456dSRavi Bangoria } 894d820456dSRavi Bangoria 8954b4da7f7SMasami Hiramatsu /* Try to find perf_probe_event with debuginfo */ 8960e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 897ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs) 8984b4da7f7SMasami Hiramatsu { 8994b4da7f7SMasami Hiramatsu bool need_dwarf = perf_probe_event_need_dwarf(pev); 9009b118acaSMasami Hiramatsu struct perf_probe_point tmp; 901225466f1SSrikar Dronamraju struct debuginfo *dinfo; 902190b57fcSMasami Hiramatsu int ntevs, ret = 0; 9034b4da7f7SMasami Hiramatsu 904105f75ebSJianlin Lv /* Workaround for gcc #98776 issue. 905105f75ebSJianlin Lv * Perf failed to add kretprobe event with debuginfo of vmlinux which is 906105f75ebSJianlin Lv * compiled by gcc with -fpatchable-function-entry option enabled. The 907105f75ebSJianlin Lv * same issue with kernel module. The retprobe doesn`t need debuginfo. 908105f75ebSJianlin Lv * This workaround solution use map to query the probe function address 909105f75ebSJianlin Lv * for retprobe event. 910105f75ebSJianlin Lv */ 911105f75ebSJianlin Lv if (pev->point.retprobe) 912105f75ebSJianlin Lv return 0; 913105f75ebSJianlin Lv 914544abd44SKrister Johansen dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf); 915ff741783SMasami Hiramatsu if (!dinfo) { 91692561cb7SMasami Hiramatsu if (need_dwarf) 917ff741783SMasami Hiramatsu return -ENOENT; 918ff741783SMasami Hiramatsu pr_debug("Could not open debuginfo. Try to use symbols.\n"); 9194b4da7f7SMasami Hiramatsu return 0; 9204b4da7f7SMasami Hiramatsu } 9214b4da7f7SMasami Hiramatsu 922dfef99cdSMasami Hiramatsu pr_debug("Try to find probe point from debuginfo.\n"); 923ff741783SMasami Hiramatsu /* Searching trace events corresponding to a probe event */ 924ddb2f58fSMasami Hiramatsu ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); 925ff741783SMasami Hiramatsu 9269b118acaSMasami Hiramatsu if (ntevs == 0) { /* Not found, retry with an alternative */ 92744225521SMasami Hiramatsu ret = get_alternative_probe_event(dinfo, pev, &tmp); 9289b118acaSMasami Hiramatsu if (!ret) { 929ddb2f58fSMasami Hiramatsu ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); 9309b118acaSMasami Hiramatsu /* 9319b118acaSMasami Hiramatsu * Write back to the original probe_event for 9329b118acaSMasami Hiramatsu * setting appropriate (user given) event name 9339b118acaSMasami Hiramatsu */ 9349b118acaSMasami Hiramatsu clear_perf_probe_point(&pev->point); 9359b118acaSMasami Hiramatsu memcpy(&pev->point, &tmp, sizeof(tmp)); 9369b118acaSMasami Hiramatsu } 9379b118acaSMasami Hiramatsu } 9389b118acaSMasami Hiramatsu 939146a1439SMasami Hiramatsu if (ntevs > 0) { /* Succeeded to find trace events */ 940dfef99cdSMasami Hiramatsu pr_debug("Found %d probe_trace_events.\n", ntevs); 94199e608b5SRavi Bangoria ret = post_process_probe_trace_events(pev, *tevs, ntevs, 942613f050dSMasami Hiramatsu pev->target, pev->uprobes, dinfo); 9435a51fcd1SMasami Hiramatsu if (ret < 0 || ret == ntevs) { 944613f050dSMasami Hiramatsu pr_debug("Post processing failed or all events are skipped. (%d)\n", ret); 945981d05adSMasami Hiramatsu clear_probe_trace_events(*tevs, ntevs); 946981d05adSMasami Hiramatsu zfree(tevs); 9475a51fcd1SMasami Hiramatsu ntevs = 0; 948146a1439SMasami Hiramatsu } 949613f050dSMasami Hiramatsu } 950613f050dSMasami Hiramatsu 951613f050dSMasami Hiramatsu debuginfo__delete(dinfo); 9524b4da7f7SMasami Hiramatsu 953146a1439SMasami Hiramatsu if (ntevs == 0) { /* No error but failed to find probe point. */ 9540687eba7SMasami Hiramatsu pr_warning("Probe point '%s' not found.\n", 9554b4da7f7SMasami Hiramatsu synthesize_perf_probe_point(&pev->point)); 956146a1439SMasami Hiramatsu return -ENOENT; 957613f050dSMasami Hiramatsu } else if (ntevs < 0) { 958146a1439SMasami Hiramatsu /* Error path : ntevs < 0 */ 95915eca306SMasami Hiramatsu pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); 9601c0bd0e8SWang Nan if (ntevs == -EBADF) 96115eca306SMasami Hiramatsu pr_warning("Warning: No dwarf info found in the vmlinux - " 96215eca306SMasami Hiramatsu "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); 96315eca306SMasami Hiramatsu if (!need_dwarf) { 9640e43e5d2SMasami Hiramatsu pr_debug("Trying to use symbols.\n"); 9654b4da7f7SMasami Hiramatsu return 0; 9664b4da7f7SMasami Hiramatsu } 96715eca306SMasami Hiramatsu } 96815eca306SMasami Hiramatsu return ntevs; 96915eca306SMasami Hiramatsu } 9704b4da7f7SMasami Hiramatsu 9714b4da7f7SMasami Hiramatsu #define LINEBUF_SIZE 256 9724b4da7f7SMasami Hiramatsu #define NR_ADDITIONAL_LINES 2 9734b4da7f7SMasami Hiramatsu 974fde52dbdSFranck Bui-Huu static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) 9754b4da7f7SMasami Hiramatsu { 9765f03cba4SMasami Hiramatsu char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; 977befe3414SFranck Bui-Huu const char *color = show_num ? "" : PERF_COLOR_BLUE; 978befe3414SFranck Bui-Huu const char *prefix = NULL; 9794b4da7f7SMasami Hiramatsu 980befe3414SFranck Bui-Huu do { 9814b4da7f7SMasami Hiramatsu if (fgets(buf, LINEBUF_SIZE, fp) == NULL) 9824b4da7f7SMasami Hiramatsu goto error; 983befe3414SFranck Bui-Huu if (skip) 984befe3414SFranck Bui-Huu continue; 985befe3414SFranck Bui-Huu if (!prefix) { 986befe3414SFranck Bui-Huu prefix = show_num ? "%7d " : " "; 987befe3414SFranck Bui-Huu color_fprintf(stdout, color, prefix, l); 9884b4da7f7SMasami Hiramatsu } 989befe3414SFranck Bui-Huu color_fprintf(stdout, color, "%s", buf); 9904b4da7f7SMasami Hiramatsu 991befe3414SFranck Bui-Huu } while (strchr(buf, '\n') == NULL); 992146a1439SMasami Hiramatsu 993fde52dbdSFranck Bui-Huu return 1; 9944b4da7f7SMasami Hiramatsu error: 995fde52dbdSFranck Bui-Huu if (ferror(fp)) { 9965f03cba4SMasami Hiramatsu pr_warning("File read error: %s\n", 997c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, sbuf, sizeof(sbuf))); 998146a1439SMasami Hiramatsu return -1; 9994b4da7f7SMasami Hiramatsu } 1000fde52dbdSFranck Bui-Huu return 0; 1001fde52dbdSFranck Bui-Huu } 1002fde52dbdSFranck Bui-Huu 1003fde52dbdSFranck Bui-Huu static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) 1004fde52dbdSFranck Bui-Huu { 1005fde52dbdSFranck Bui-Huu int rv = __show_one_line(fp, l, skip, show_num); 1006fde52dbdSFranck Bui-Huu if (rv == 0) { 1007fde52dbdSFranck Bui-Huu pr_warning("Source file is shorter than expected.\n"); 1008fde52dbdSFranck Bui-Huu rv = -1; 1009fde52dbdSFranck Bui-Huu } 1010fde52dbdSFranck Bui-Huu return rv; 1011fde52dbdSFranck Bui-Huu } 1012fde52dbdSFranck Bui-Huu 1013fde52dbdSFranck Bui-Huu #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) 1014fde52dbdSFranck Bui-Huu #define show_one_line(f,l) _show_one_line(f,l,false,false) 1015fde52dbdSFranck Bui-Huu #define skip_one_line(f,l) _show_one_line(f,l,true,false) 1016fde52dbdSFranck Bui-Huu #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) 10174b4da7f7SMasami Hiramatsu 10184b4da7f7SMasami Hiramatsu /* 10194b4da7f7SMasami Hiramatsu * Show line-range always requires debuginfo to find source file and 10204b4da7f7SMasami Hiramatsu * line number. 10214b4da7f7SMasami Hiramatsu */ 1022811dd2aeSMasami Hiramatsu static int __show_line_range(struct line_range *lr, const char *module, 1023811dd2aeSMasami Hiramatsu bool user) 10244b4da7f7SMasami Hiramatsu { 1025bf541169SJiri Olsa struct build_id bid; 1026d3b63d7aSMasami Hiramatsu int l = 1; 10275a62257aSMasami Hiramatsu struct int_node *ln; 1028ff741783SMasami Hiramatsu struct debuginfo *dinfo; 10294b4da7f7SMasami Hiramatsu FILE *fp; 1030ff741783SMasami Hiramatsu int ret; 10317cf0b79eSMasami Hiramatsu char *tmp; 10325f03cba4SMasami Hiramatsu char sbuf[STRERR_BUFSIZE]; 10337cd5738dSMasami Hiramatsu char sbuild_id[SBUILD_ID_SIZE] = ""; 10344b4da7f7SMasami Hiramatsu 10354b4da7f7SMasami Hiramatsu /* Search a line range */ 1036544abd44SKrister Johansen dinfo = open_debuginfo(module, NULL, false); 103792561cb7SMasami Hiramatsu if (!dinfo) 1038ff741783SMasami Hiramatsu return -ENOENT; 1039146a1439SMasami Hiramatsu 1040ff741783SMasami Hiramatsu ret = debuginfo__find_line_range(dinfo, lr); 1041811dd2aeSMasami Hiramatsu if (!ret) { /* Not found, retry with an alternative */ 1042811dd2aeSMasami Hiramatsu ret = get_alternative_line_range(dinfo, lr, module, user); 1043811dd2aeSMasami Hiramatsu if (!ret) 1044811dd2aeSMasami Hiramatsu ret = debuginfo__find_line_range(dinfo, lr); 1045811dd2aeSMasami Hiramatsu } 1046bf541169SJiri Olsa if (dinfo->build_id) { 1047bf541169SJiri Olsa build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE); 1048bf541169SJiri Olsa build_id__sprintf(&bid, sbuild_id); 1049bf541169SJiri Olsa } 1050ff741783SMasami Hiramatsu debuginfo__delete(dinfo); 10515ee05b88SMasami Hiramatsu if (ret == 0 || ret == -ENOENT) { 1052146a1439SMasami Hiramatsu pr_warning("Specified source line is not found.\n"); 1053146a1439SMasami Hiramatsu return -ENOENT; 1054146a1439SMasami Hiramatsu } else if (ret < 0) { 10555ee05b88SMasami Hiramatsu pr_warning("Debuginfo analysis failed.\n"); 1056146a1439SMasami Hiramatsu return ret; 1057146a1439SMasami Hiramatsu } 10584b4da7f7SMasami Hiramatsu 10597cf0b79eSMasami Hiramatsu /* Convert source file path */ 10607cf0b79eSMasami Hiramatsu tmp = lr->path; 10617cd5738dSMasami Hiramatsu ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path); 1062a78604deSHe Kuang 1063a78604deSHe Kuang /* Free old path when new path is assigned */ 1064a78604deSHe Kuang if (tmp != lr->path) 1065a78604deSHe Kuang free(tmp); 1066a78604deSHe Kuang 10677cf0b79eSMasami Hiramatsu if (ret < 0) { 10685ee05b88SMasami Hiramatsu pr_warning("Failed to find source file path.\n"); 10697cf0b79eSMasami Hiramatsu return ret; 10707cf0b79eSMasami Hiramatsu } 10717cf0b79eSMasami Hiramatsu 10724b4da7f7SMasami Hiramatsu setup_pager(); 10734b4da7f7SMasami Hiramatsu 10744b4da7f7SMasami Hiramatsu if (lr->function) 10758737ebdeSMasami Hiramatsu fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, 10764b4da7f7SMasami Hiramatsu lr->start - lr->offset); 10774b4da7f7SMasami Hiramatsu else 107862c15fc4SFranck Bui-Huu fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); 10794b4da7f7SMasami Hiramatsu 10804b4da7f7SMasami Hiramatsu fp = fopen(lr->path, "r"); 1081146a1439SMasami Hiramatsu if (fp == NULL) { 1082146a1439SMasami Hiramatsu pr_warning("Failed to open %s: %s\n", lr->path, 1083c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, sbuf, sizeof(sbuf))); 1084146a1439SMasami Hiramatsu return -errno; 1085146a1439SMasami Hiramatsu } 10864b4da7f7SMasami Hiramatsu /* Skip to starting line number */ 108744b81e92SFranck Bui-Huu while (l < lr->start) { 1088fde52dbdSFranck Bui-Huu ret = skip_one_line(fp, l++); 1089146a1439SMasami Hiramatsu if (ret < 0) 1090146a1439SMasami Hiramatsu goto end; 109144b81e92SFranck Bui-Huu } 10924b4da7f7SMasami Hiramatsu 109310daf4d0SArnaldo Carvalho de Melo intlist__for_each_entry(ln, lr->line_list) { 109494253393SJin Yao for (; ln->i > (unsigned long)l; l++) { 1095fde52dbdSFranck Bui-Huu ret = show_one_line(fp, l - lr->offset); 109644b81e92SFranck Bui-Huu if (ret < 0) 109744b81e92SFranck Bui-Huu goto end; 109844b81e92SFranck Bui-Huu } 1099fde52dbdSFranck Bui-Huu ret = show_one_line_with_num(fp, l++ - lr->offset); 1100146a1439SMasami Hiramatsu if (ret < 0) 1101146a1439SMasami Hiramatsu goto end; 11024b4da7f7SMasami Hiramatsu } 11034b4da7f7SMasami Hiramatsu 11044b4da7f7SMasami Hiramatsu if (lr->end == INT_MAX) 11054b4da7f7SMasami Hiramatsu lr->end = l + NR_ADDITIONAL_LINES; 1106fde52dbdSFranck Bui-Huu while (l <= lr->end) { 1107fde52dbdSFranck Bui-Huu ret = show_one_line_or_eof(fp, l++ - lr->offset); 1108fde52dbdSFranck Bui-Huu if (ret <= 0) 110944b81e92SFranck Bui-Huu break; 111044b81e92SFranck Bui-Huu } 1111146a1439SMasami Hiramatsu end: 11124b4da7f7SMasami Hiramatsu fclose(fp); 1113146a1439SMasami Hiramatsu return ret; 11144b4da7f7SMasami Hiramatsu } 11154b4da7f7SMasami Hiramatsu 1116544abd44SKrister Johansen int show_line_range(struct line_range *lr, const char *module, 1117544abd44SKrister Johansen struct nsinfo *nsi, bool user) 1118ee45b6c2SMasami Hiramatsu { 1119ee45b6c2SMasami Hiramatsu int ret; 1120544abd44SKrister Johansen struct nscookie nsc; 1121ee45b6c2SMasami Hiramatsu 11229bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(user); 1123ee45b6c2SMasami Hiramatsu if (ret < 0) 1124ee45b6c2SMasami Hiramatsu return ret; 1125544abd44SKrister Johansen nsinfo__mountns_enter(nsi, &nsc); 1126811dd2aeSMasami Hiramatsu ret = __show_line_range(lr, module, user); 1127544abd44SKrister Johansen nsinfo__mountns_exit(&nsc); 11289bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 1129ee45b6c2SMasami Hiramatsu 1130ee45b6c2SMasami Hiramatsu return ret; 1131ee45b6c2SMasami Hiramatsu } 1132ee45b6c2SMasami Hiramatsu 1133ff741783SMasami Hiramatsu static int show_available_vars_at(struct debuginfo *dinfo, 1134ff741783SMasami Hiramatsu struct perf_probe_event *pev, 1135ddb2f58fSMasami Hiramatsu struct strfilter *_filter) 1136cf6eb489SMasami Hiramatsu { 1137cf6eb489SMasami Hiramatsu char *buf; 1138bd09d7b5SMasami Hiramatsu int ret, i, nvars; 1139cf6eb489SMasami Hiramatsu struct str_node *node; 1140cf6eb489SMasami Hiramatsu struct variable_list *vls = NULL, *vl; 11419b118acaSMasami Hiramatsu struct perf_probe_point tmp; 1142bd09d7b5SMasami Hiramatsu const char *var; 1143cf6eb489SMasami Hiramatsu 1144cf6eb489SMasami Hiramatsu buf = synthesize_perf_probe_point(&pev->point); 1145cf6eb489SMasami Hiramatsu if (!buf) 1146cf6eb489SMasami Hiramatsu return -EINVAL; 1147cf6eb489SMasami Hiramatsu pr_debug("Searching variables at %s\n", buf); 1148cf6eb489SMasami Hiramatsu 1149ddb2f58fSMasami Hiramatsu ret = debuginfo__find_available_vars_at(dinfo, pev, &vls); 11509b118acaSMasami Hiramatsu if (!ret) { /* Not found, retry with an alternative */ 115144225521SMasami Hiramatsu ret = get_alternative_probe_event(dinfo, pev, &tmp); 11529b118acaSMasami Hiramatsu if (!ret) { 11539b118acaSMasami Hiramatsu ret = debuginfo__find_available_vars_at(dinfo, pev, 1154ddb2f58fSMasami Hiramatsu &vls); 11559b118acaSMasami Hiramatsu /* Release the old probe_point */ 11569b118acaSMasami Hiramatsu clear_perf_probe_point(&tmp); 11579b118acaSMasami Hiramatsu } 11589b118acaSMasami Hiramatsu } 1159bd09d7b5SMasami Hiramatsu if (ret <= 0) { 116069e96eaaSMasami Hiramatsu if (ret == 0 || ret == -ENOENT) { 116169e96eaaSMasami Hiramatsu pr_err("Failed to find the address of %s\n", buf); 116269e96eaaSMasami Hiramatsu ret = -ENOENT; 116369e96eaaSMasami Hiramatsu } else 116469e96eaaSMasami Hiramatsu pr_warning("Debuginfo analysis failed.\n"); 1165bd09d7b5SMasami Hiramatsu goto end; 1166bd09d7b5SMasami Hiramatsu } 116769e96eaaSMasami Hiramatsu 1168bd09d7b5SMasami Hiramatsu /* Some variables are found */ 1169cf6eb489SMasami Hiramatsu fprintf(stdout, "Available variables at %s\n", buf); 1170cf6eb489SMasami Hiramatsu for (i = 0; i < ret; i++) { 1171cf6eb489SMasami Hiramatsu vl = &vls[i]; 1172cf6eb489SMasami Hiramatsu /* 1173cf6eb489SMasami Hiramatsu * A probe point might be converted to 1174cf6eb489SMasami Hiramatsu * several trace points. 1175cf6eb489SMasami Hiramatsu */ 1176cf6eb489SMasami Hiramatsu fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, 1177cf6eb489SMasami Hiramatsu vl->point.offset); 117874cf249dSArnaldo Carvalho de Melo zfree(&vl->point.symbol); 1179bd09d7b5SMasami Hiramatsu nvars = 0; 1180cf6eb489SMasami Hiramatsu if (vl->vars) { 1181602a1f4dSArnaldo Carvalho de Melo strlist__for_each_entry(node, vl->vars) { 1182bd09d7b5SMasami Hiramatsu var = strchr(node->s, '\t') + 1; 1183bd09d7b5SMasami Hiramatsu if (strfilter__compare(_filter, var)) { 1184cf6eb489SMasami Hiramatsu fprintf(stdout, "\t\t%s\n", node->s); 1185bd09d7b5SMasami Hiramatsu nvars++; 1186bd09d7b5SMasami Hiramatsu } 1187bd09d7b5SMasami Hiramatsu } 1188cf6eb489SMasami Hiramatsu strlist__delete(vl->vars); 1189bd09d7b5SMasami Hiramatsu } 1190bd09d7b5SMasami Hiramatsu if (nvars == 0) 1191bd09d7b5SMasami Hiramatsu fprintf(stdout, "\t\t(No matched variables)\n"); 1192cf6eb489SMasami Hiramatsu } 1193cf6eb489SMasami Hiramatsu free(vls); 1194bd09d7b5SMasami Hiramatsu end: 1195cf6eb489SMasami Hiramatsu free(buf); 1196cf6eb489SMasami Hiramatsu return ret; 1197cf6eb489SMasami Hiramatsu } 1198cf6eb489SMasami Hiramatsu 1199cf6eb489SMasami Hiramatsu /* Show available variables on given probe point */ 1200cf6eb489SMasami Hiramatsu int show_available_vars(struct perf_probe_event *pevs, int npevs, 1201ddb2f58fSMasami Hiramatsu struct strfilter *_filter) 1202cf6eb489SMasami Hiramatsu { 1203ff741783SMasami Hiramatsu int i, ret = 0; 1204ff741783SMasami Hiramatsu struct debuginfo *dinfo; 1205cf6eb489SMasami Hiramatsu 12069bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(pevs->uprobes); 1207cf6eb489SMasami Hiramatsu if (ret < 0) 1208cf6eb489SMasami Hiramatsu return ret; 1209cf6eb489SMasami Hiramatsu 1210544abd44SKrister Johansen dinfo = open_debuginfo(pevs->target, pevs->nsi, false); 1211ff741783SMasami Hiramatsu if (!dinfo) { 1212ee45b6c2SMasami Hiramatsu ret = -ENOENT; 1213ee45b6c2SMasami Hiramatsu goto out; 1214ff741783SMasami Hiramatsu } 1215ff741783SMasami Hiramatsu 1216cc446446SMasami Hiramatsu setup_pager(); 1217cc446446SMasami Hiramatsu 1218ff741783SMasami Hiramatsu for (i = 0; i < npevs && ret >= 0; i++) 1219ddb2f58fSMasami Hiramatsu ret = show_available_vars_at(dinfo, &pevs[i], _filter); 1220ff741783SMasami Hiramatsu 1221ff741783SMasami Hiramatsu debuginfo__delete(dinfo); 1222ee45b6c2SMasami Hiramatsu out: 12239bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 1224cf6eb489SMasami Hiramatsu return ret; 1225cf6eb489SMasami Hiramatsu } 1226cf6eb489SMasami Hiramatsu 122789fe808aSIngo Molnar #else /* !HAVE_DWARF_SUPPORT */ 12284b4da7f7SMasami Hiramatsu 12297737af01SMasami Hiramatsu static void debuginfo_cache__exit(void) 12307737af01SMasami Hiramatsu { 12317737af01SMasami Hiramatsu } 12327737af01SMasami Hiramatsu 12335a6f6314SMasami Hiramatsu static int 12345a6f6314SMasami Hiramatsu find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, 12355a6f6314SMasami Hiramatsu struct perf_probe_point *pp __maybe_unused, 12365a6f6314SMasami Hiramatsu bool is_kprobe __maybe_unused) 12374b4da7f7SMasami Hiramatsu { 12385a6f6314SMasami Hiramatsu return -ENOSYS; 12394b4da7f7SMasami Hiramatsu } 12404b4da7f7SMasami Hiramatsu 12410e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 1242ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs __maybe_unused) 12434b4da7f7SMasami Hiramatsu { 1244146a1439SMasami Hiramatsu if (perf_probe_event_need_dwarf(pev)) { 1245146a1439SMasami Hiramatsu pr_warning("Debuginfo-analysis is not supported.\n"); 1246146a1439SMasami Hiramatsu return -ENOSYS; 1247146a1439SMasami Hiramatsu } 1248225466f1SSrikar Dronamraju 12494b4da7f7SMasami Hiramatsu return 0; 12504b4da7f7SMasami Hiramatsu } 12514b4da7f7SMasami Hiramatsu 12521d037ca1SIrina Tirdea int show_line_range(struct line_range *lr __maybe_unused, 12532b394bc4SMasami Hiramatsu const char *module __maybe_unused, 1254544abd44SKrister Johansen struct nsinfo *nsi __maybe_unused, 12552b394bc4SMasami Hiramatsu bool user __maybe_unused) 12564b4da7f7SMasami Hiramatsu { 1257146a1439SMasami Hiramatsu pr_warning("Debuginfo-analysis is not supported.\n"); 1258146a1439SMasami Hiramatsu return -ENOSYS; 12594b4da7f7SMasami Hiramatsu } 12604b4da7f7SMasami Hiramatsu 12611d037ca1SIrina Tirdea int show_available_vars(struct perf_probe_event *pevs __maybe_unused, 1262ddb2f58fSMasami Hiramatsu int npevs __maybe_unused, 1263ddb2f58fSMasami Hiramatsu struct strfilter *filter __maybe_unused) 1264cf6eb489SMasami Hiramatsu { 1265cf6eb489SMasami Hiramatsu pr_warning("Debuginfo-analysis is not supported.\n"); 1266cf6eb489SMasami Hiramatsu return -ENOSYS; 1267cf6eb489SMasami Hiramatsu } 1268e0faa8d3SMasami Hiramatsu #endif 1269e0faa8d3SMasami Hiramatsu 1270e53b00d3SMasami Hiramatsu void line_range__clear(struct line_range *lr) 1271e53b00d3SMasami Hiramatsu { 1272d8f9da24SArnaldo Carvalho de Melo zfree(&lr->function); 1273d8f9da24SArnaldo Carvalho de Melo zfree(&lr->file); 1274d8f9da24SArnaldo Carvalho de Melo zfree(&lr->path); 1275d8f9da24SArnaldo Carvalho de Melo zfree(&lr->comp_dir); 12765a62257aSMasami Hiramatsu intlist__delete(lr->line_list); 1277e53b00d3SMasami Hiramatsu } 1278e53b00d3SMasami Hiramatsu 12795a62257aSMasami Hiramatsu int line_range__init(struct line_range *lr) 1280e53b00d3SMasami Hiramatsu { 1281e53b00d3SMasami Hiramatsu memset(lr, 0, sizeof(*lr)); 12825a62257aSMasami Hiramatsu lr->line_list = intlist__new(NULL); 12835a62257aSMasami Hiramatsu if (!lr->line_list) 12845a62257aSMasami Hiramatsu return -ENOMEM; 12855a62257aSMasami Hiramatsu else 12865a62257aSMasami Hiramatsu return 0; 1287e53b00d3SMasami Hiramatsu } 1288e53b00d3SMasami Hiramatsu 128921dd9ae5SFranck Bui-Huu static int parse_line_num(char **ptr, int *val, const char *what) 129021dd9ae5SFranck Bui-Huu { 129121dd9ae5SFranck Bui-Huu const char *start = *ptr; 129221dd9ae5SFranck Bui-Huu 129321dd9ae5SFranck Bui-Huu errno = 0; 129421dd9ae5SFranck Bui-Huu *val = strtol(*ptr, ptr, 0); 129521dd9ae5SFranck Bui-Huu if (errno || *ptr == start) { 129621dd9ae5SFranck Bui-Huu semantic_error("'%s' is not a valid number.\n", what); 129721dd9ae5SFranck Bui-Huu return -EINVAL; 129821dd9ae5SFranck Bui-Huu } 129921dd9ae5SFranck Bui-Huu return 0; 130021dd9ae5SFranck Bui-Huu } 130121dd9ae5SFranck Bui-Huu 1302573709fdSMasami Hiramatsu /* Check the name is good for event, group or function */ 1303573709fdSMasami Hiramatsu static bool is_c_func_name(const char *name) 1304573709fdSMasami Hiramatsu { 1305573709fdSMasami Hiramatsu if (!isalpha(*name) && *name != '_') 1306573709fdSMasami Hiramatsu return false; 1307573709fdSMasami Hiramatsu while (*++name != '\0') { 1308573709fdSMasami Hiramatsu if (!isalpha(*name) && !isdigit(*name) && *name != '_') 1309573709fdSMasami Hiramatsu return false; 1310573709fdSMasami Hiramatsu } 1311573709fdSMasami Hiramatsu return true; 1312573709fdSMasami Hiramatsu } 1313573709fdSMasami Hiramatsu 13149d95b580SFranck Bui-Huu /* 13159d95b580SFranck Bui-Huu * Stuff 'lr' according to the line range described by 'arg'. 13169d95b580SFranck Bui-Huu * The line range syntax is described by: 13179d95b580SFranck Bui-Huu * 13189d95b580SFranck Bui-Huu * SRC[:SLN[+NUM|-ELN]] 1319e116dfa1SMasami Hiramatsu * FNC[@SRC][:SLN[+NUM|-ELN]] 13209d95b580SFranck Bui-Huu */ 1321146a1439SMasami Hiramatsu int parse_line_range_desc(const char *arg, struct line_range *lr) 1322631c9defSMasami Hiramatsu { 1323e116dfa1SMasami Hiramatsu char *range, *file, *name = strdup(arg); 132421dd9ae5SFranck Bui-Huu int err; 13259d95b580SFranck Bui-Huu 132621dd9ae5SFranck Bui-Huu if (!name) 132721dd9ae5SFranck Bui-Huu return -ENOMEM; 132821dd9ae5SFranck Bui-Huu 132921dd9ae5SFranck Bui-Huu lr->start = 0; 133021dd9ae5SFranck Bui-Huu lr->end = INT_MAX; 133121dd9ae5SFranck Bui-Huu 133221dd9ae5SFranck Bui-Huu range = strchr(name, ':'); 133321dd9ae5SFranck Bui-Huu if (range) { 133421dd9ae5SFranck Bui-Huu *range++ = '\0'; 133521dd9ae5SFranck Bui-Huu 133621dd9ae5SFranck Bui-Huu err = parse_line_num(&range, &lr->start, "start line"); 133721dd9ae5SFranck Bui-Huu if (err) 133821dd9ae5SFranck Bui-Huu goto err; 133921dd9ae5SFranck Bui-Huu 134021dd9ae5SFranck Bui-Huu if (*range == '+' || *range == '-') { 134121dd9ae5SFranck Bui-Huu const char c = *range++; 134221dd9ae5SFranck Bui-Huu 134321dd9ae5SFranck Bui-Huu err = parse_line_num(&range, &lr->end, "end line"); 134421dd9ae5SFranck Bui-Huu if (err) 134521dd9ae5SFranck Bui-Huu goto err; 134621dd9ae5SFranck Bui-Huu 134721dd9ae5SFranck Bui-Huu if (c == '+') { 134821dd9ae5SFranck Bui-Huu lr->end += lr->start; 134921dd9ae5SFranck Bui-Huu /* 1350dda4ab34SMasami Hiramatsu * Adjust the number of lines here. 1351dda4ab34SMasami Hiramatsu * If the number of lines == 1, the 1352c69d33ebSJason Wang * end of line should be equal to 1353dda4ab34SMasami Hiramatsu * the start of line. 1354dda4ab34SMasami Hiramatsu */ 135521dd9ae5SFranck Bui-Huu lr->end--; 135621dd9ae5SFranck Bui-Huu } 135721dd9ae5SFranck Bui-Huu } 135821dd9ae5SFranck Bui-Huu 1359d3b63d7aSMasami Hiramatsu pr_debug("Line range is %d to %d\n", lr->start, lr->end); 136021dd9ae5SFranck Bui-Huu 136121dd9ae5SFranck Bui-Huu err = -EINVAL; 1362d3b63d7aSMasami Hiramatsu if (lr->start > lr->end) { 1363631c9defSMasami Hiramatsu semantic_error("Start line must be smaller" 1364146a1439SMasami Hiramatsu " than end line.\n"); 136521dd9ae5SFranck Bui-Huu goto err; 1366146a1439SMasami Hiramatsu } 136721dd9ae5SFranck Bui-Huu if (*range != '\0') { 136821dd9ae5SFranck Bui-Huu semantic_error("Tailing with invalid str '%s'.\n", range); 136921dd9ae5SFranck Bui-Huu goto err; 1370146a1439SMasami Hiramatsu } 1371d3b63d7aSMasami Hiramatsu } 137202b95dadSMasami Hiramatsu 1373e116dfa1SMasami Hiramatsu file = strchr(name, '@'); 1374e116dfa1SMasami Hiramatsu if (file) { 1375e116dfa1SMasami Hiramatsu *file = '\0'; 1376e116dfa1SMasami Hiramatsu lr->file = strdup(++file); 1377e116dfa1SMasami Hiramatsu if (lr->file == NULL) { 1378e116dfa1SMasami Hiramatsu err = -ENOMEM; 1379e116dfa1SMasami Hiramatsu goto err; 1380e116dfa1SMasami Hiramatsu } 1381e116dfa1SMasami Hiramatsu lr->function = name; 1382573709fdSMasami Hiramatsu } else if (strchr(name, '/') || strchr(name, '.')) 138321dd9ae5SFranck Bui-Huu lr->file = name; 1384573709fdSMasami Hiramatsu else if (is_c_func_name(name))/* We reuse it for checking funcname */ 138521dd9ae5SFranck Bui-Huu lr->function = name; 1386573709fdSMasami Hiramatsu else { /* Invalid name */ 1387573709fdSMasami Hiramatsu semantic_error("'%s' is not a valid function name.\n", name); 1388573709fdSMasami Hiramatsu err = -EINVAL; 1389573709fdSMasami Hiramatsu goto err; 1390573709fdSMasami Hiramatsu } 1391146a1439SMasami Hiramatsu 1392146a1439SMasami Hiramatsu return 0; 139321dd9ae5SFranck Bui-Huu err: 139421dd9ae5SFranck Bui-Huu free(name); 139521dd9ae5SFranck Bui-Huu return err; 1396631c9defSMasami Hiramatsu } 1397631c9defSMasami Hiramatsu 139836a009feSMasami Hiramatsu static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev) 139936a009feSMasami Hiramatsu { 140036a009feSMasami Hiramatsu char *ptr; 140136a009feSMasami Hiramatsu 1402c588d158SMasami Hiramatsu ptr = strpbrk_esc(*arg, ":"); 140336a009feSMasami Hiramatsu if (ptr) { 140436a009feSMasami Hiramatsu *ptr = '\0'; 140542bba263SMasami Hiramatsu if (!pev->sdt && !is_c_func_name(*arg)) 140636a009feSMasami Hiramatsu goto ng_name; 1407c588d158SMasami Hiramatsu pev->group = strdup_esc(*arg); 140836a009feSMasami Hiramatsu if (!pev->group) 140936a009feSMasami Hiramatsu return -ENOMEM; 141036a009feSMasami Hiramatsu *arg = ptr + 1; 141136a009feSMasami Hiramatsu } else 141236a009feSMasami Hiramatsu pev->group = NULL; 1413c588d158SMasami Hiramatsu 1414c588d158SMasami Hiramatsu pev->event = strdup_esc(*arg); 1415c588d158SMasami Hiramatsu if (pev->event == NULL) 1416c588d158SMasami Hiramatsu return -ENOMEM; 1417c588d158SMasami Hiramatsu 1418c588d158SMasami Hiramatsu if (!pev->sdt && !is_c_func_name(pev->event)) { 1419c588d158SMasami Hiramatsu zfree(&pev->event); 142036a009feSMasami Hiramatsu ng_name: 1421c588d158SMasami Hiramatsu zfree(&pev->group); 142236a009feSMasami Hiramatsu semantic_error("%s is bad for event name -it must " 142336a009feSMasami Hiramatsu "follow C symbol-naming rule.\n", *arg); 142436a009feSMasami Hiramatsu return -EINVAL; 142536a009feSMasami Hiramatsu } 142636a009feSMasami Hiramatsu return 0; 142736a009feSMasami Hiramatsu } 142836a009feSMasami Hiramatsu 142950656eecSMasami Hiramatsu /* Parse probepoint definition. */ 1430146a1439SMasami Hiramatsu static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) 143150656eecSMasami Hiramatsu { 14324235b045SMasami Hiramatsu struct perf_probe_point *pp = &pev->point; 143350656eecSMasami Hiramatsu char *ptr, *tmp; 143450656eecSMasami Hiramatsu char c, nc = 0; 14353099c026SNaveen N. Rao bool file_spec = false; 143636a009feSMasami Hiramatsu int ret; 143736a009feSMasami Hiramatsu 143850656eecSMasami Hiramatsu /* 143950656eecSMasami Hiramatsu * <Syntax> 14408d993d96SMasami Hiramatsu * perf probe [GRP:][EVENT=]SRC[:LN|;PTN] 14418d993d96SMasami Hiramatsu * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] 144236a009feSMasami Hiramatsu * perf probe %[GRP:]SDT_EVENT 144350656eecSMasami Hiramatsu */ 1444e59d29e8SWang Nan if (!arg) 1445e59d29e8SWang Nan return -EINVAL; 144650656eecSMasami Hiramatsu 1447af9100adSRavi Bangoria if (is_sdt_event(arg)) { 144836a009feSMasami Hiramatsu pev->sdt = true; 14497e9fca51SMasami Hiramatsu if (arg[0] == '%') 145036a009feSMasami Hiramatsu arg++; 145136a009feSMasami Hiramatsu } 145236a009feSMasami Hiramatsu 1453c588d158SMasami Hiramatsu ptr = strpbrk_esc(arg, ";=@+%"); 145436a009feSMasami Hiramatsu if (pev->sdt) { 145536a009feSMasami Hiramatsu if (ptr) { 1456a598180aSMasami Hiramatsu if (*ptr != '@') { 1457a598180aSMasami Hiramatsu semantic_error("%s must be an SDT name.\n", 1458a598180aSMasami Hiramatsu arg); 145936a009feSMasami Hiramatsu return -EINVAL; 146036a009feSMasami Hiramatsu } 1461a598180aSMasami Hiramatsu /* This must be a target file name or build id */ 1462a598180aSMasami Hiramatsu tmp = build_id_cache__complement(ptr + 1); 1463a598180aSMasami Hiramatsu if (tmp) { 1464a598180aSMasami Hiramatsu pev->target = build_id_cache__origname(tmp); 1465a598180aSMasami Hiramatsu free(tmp); 1466a598180aSMasami Hiramatsu } else 1467c588d158SMasami Hiramatsu pev->target = strdup_esc(ptr + 1); 1468a598180aSMasami Hiramatsu if (!pev->target) 1469a598180aSMasami Hiramatsu return -ENOMEM; 1470a598180aSMasami Hiramatsu *ptr = '\0'; 1471a598180aSMasami Hiramatsu } 147236a009feSMasami Hiramatsu ret = parse_perf_probe_event_name(&arg, pev); 147336a009feSMasami Hiramatsu if (ret == 0) { 147436a009feSMasami Hiramatsu if (asprintf(&pev->point.function, "%%%s", pev->event) < 0) 147536a009feSMasami Hiramatsu ret = -errno; 147636a009feSMasami Hiramatsu } 147736a009feSMasami Hiramatsu return ret; 147836a009feSMasami Hiramatsu } 147936a009feSMasami Hiramatsu 14802a9c8c36SMasami Hiramatsu if (ptr && *ptr == '=') { /* Event name */ 1481af663d75SMasami Hiramatsu *ptr = '\0'; 1482af663d75SMasami Hiramatsu tmp = ptr + 1; 148336a009feSMasami Hiramatsu ret = parse_perf_probe_event_name(&arg, pev); 148436a009feSMasami Hiramatsu if (ret < 0) 148536a009feSMasami Hiramatsu return ret; 148636a009feSMasami Hiramatsu 1487af663d75SMasami Hiramatsu arg = tmp; 1488af663d75SMasami Hiramatsu } 1489af663d75SMasami Hiramatsu 14903099c026SNaveen N. Rao /* 14913099c026SNaveen N. Rao * Check arg is function or file name and copy it. 14923099c026SNaveen N. Rao * 14933099c026SNaveen N. Rao * We consider arg to be a file spec if and only if it satisfies 14943099c026SNaveen N. Rao * all of the below criteria:: 14953099c026SNaveen N. Rao * - it does not include any of "+@%", 14963099c026SNaveen N. Rao * - it includes one of ":;", and 14973099c026SNaveen N. Rao * - it has a period '.' in the name. 14983099c026SNaveen N. Rao * 14993099c026SNaveen N. Rao * Otherwise, we consider arg to be a function specification. 15003099c026SNaveen N. Rao */ 1501c588d158SMasami Hiramatsu if (!strpbrk_esc(arg, "+@%")) { 1502c588d158SMasami Hiramatsu ptr = strpbrk_esc(arg, ";:"); 15033099c026SNaveen N. Rao /* This is a file spec if it includes a '.' before ; or : */ 1504c588d158SMasami Hiramatsu if (ptr && memchr(arg, '.', ptr - arg)) 15053099c026SNaveen N. Rao file_spec = true; 15063099c026SNaveen N. Rao } 15073099c026SNaveen N. Rao 1508c588d158SMasami Hiramatsu ptr = strpbrk_esc(arg, ";:+@%"); 150950656eecSMasami Hiramatsu if (ptr) { 151050656eecSMasami Hiramatsu nc = *ptr; 151150656eecSMasami Hiramatsu *ptr++ = '\0'; 151250656eecSMasami Hiramatsu } 151350656eecSMasami Hiramatsu 15146c6e024fSWang Nan if (arg[0] == '\0') 15156c6e024fSWang Nan tmp = NULL; 15166c6e024fSWang Nan else { 1517c588d158SMasami Hiramatsu tmp = strdup_esc(arg); 151802b95dadSMasami Hiramatsu if (tmp == NULL) 151902b95dadSMasami Hiramatsu return -ENOMEM; 15206c6e024fSWang Nan } 152102b95dadSMasami Hiramatsu 15223099c026SNaveen N. Rao if (file_spec) 152302b95dadSMasami Hiramatsu pp->file = tmp; 1524da15bd9dSWang Nan else { 152502b95dadSMasami Hiramatsu pp->function = tmp; 152650656eecSMasami Hiramatsu 1527da15bd9dSWang Nan /* 1528da15bd9dSWang Nan * Keep pp->function even if this is absolute address, 1529da15bd9dSWang Nan * so it can mark whether abs_address is valid. 1530da15bd9dSWang Nan * Which make 'perf probe lib.bin 0x0' possible. 1531da15bd9dSWang Nan * 1532da15bd9dSWang Nan * Note that checking length of tmp is not needed 1533da15bd9dSWang Nan * because when we access tmp[1] we know tmp[0] is '0', 1534da15bd9dSWang Nan * so tmp[1] should always valid (but could be '\0'). 1535da15bd9dSWang Nan */ 1536da15bd9dSWang Nan if (tmp && !strncmp(tmp, "0x", 2)) { 153722a66551SYang Jihong pp->abs_address = strtoull(pp->function, &tmp, 0); 1538da15bd9dSWang Nan if (*tmp != '\0') { 1539da15bd9dSWang Nan semantic_error("Invalid absolute address.\n"); 1540da15bd9dSWang Nan return -EINVAL; 1541da15bd9dSWang Nan } 1542da15bd9dSWang Nan } 1543da15bd9dSWang Nan } 1544da15bd9dSWang Nan 154550656eecSMasami Hiramatsu /* Parse other options */ 154650656eecSMasami Hiramatsu while (ptr) { 154750656eecSMasami Hiramatsu arg = ptr; 154850656eecSMasami Hiramatsu c = nc; 15492a9c8c36SMasami Hiramatsu if (c == ';') { /* Lazy pattern must be the last part */ 1550c588d158SMasami Hiramatsu pp->lazy_line = strdup(arg); /* let leave escapes */ 155102b95dadSMasami Hiramatsu if (pp->lazy_line == NULL) 155202b95dadSMasami Hiramatsu return -ENOMEM; 15532a9c8c36SMasami Hiramatsu break; 15542a9c8c36SMasami Hiramatsu } 1555c588d158SMasami Hiramatsu ptr = strpbrk_esc(arg, ";:+@%"); 155650656eecSMasami Hiramatsu if (ptr) { 155750656eecSMasami Hiramatsu nc = *ptr; 155850656eecSMasami Hiramatsu *ptr++ = '\0'; 155950656eecSMasami Hiramatsu } 156050656eecSMasami Hiramatsu switch (c) { 156150656eecSMasami Hiramatsu case ':': /* Line number */ 156250656eecSMasami Hiramatsu pp->line = strtoul(arg, &tmp, 0); 1563146a1439SMasami Hiramatsu if (*tmp != '\0') { 15642a9c8c36SMasami Hiramatsu semantic_error("There is non-digit char" 1565146a1439SMasami Hiramatsu " in line number.\n"); 1566146a1439SMasami Hiramatsu return -EINVAL; 1567146a1439SMasami Hiramatsu } 156850656eecSMasami Hiramatsu break; 156950656eecSMasami Hiramatsu case '+': /* Byte offset from a symbol */ 157050656eecSMasami Hiramatsu pp->offset = strtoul(arg, &tmp, 0); 1571146a1439SMasami Hiramatsu if (*tmp != '\0') { 15722a9c8c36SMasami Hiramatsu semantic_error("There is non-digit character" 1573146a1439SMasami Hiramatsu " in offset.\n"); 1574146a1439SMasami Hiramatsu return -EINVAL; 1575146a1439SMasami Hiramatsu } 157650656eecSMasami Hiramatsu break; 157750656eecSMasami Hiramatsu case '@': /* File name */ 1578146a1439SMasami Hiramatsu if (pp->file) { 1579146a1439SMasami Hiramatsu semantic_error("SRC@SRC is not allowed.\n"); 1580146a1439SMasami Hiramatsu return -EINVAL; 1581146a1439SMasami Hiramatsu } 1582c588d158SMasami Hiramatsu pp->file = strdup_esc(arg); 158302b95dadSMasami Hiramatsu if (pp->file == NULL) 158402b95dadSMasami Hiramatsu return -ENOMEM; 158550656eecSMasami Hiramatsu break; 158650656eecSMasami Hiramatsu case '%': /* Probe places */ 158750656eecSMasami Hiramatsu if (strcmp(arg, "return") == 0) { 158850656eecSMasami Hiramatsu pp->retprobe = 1; 1589146a1439SMasami Hiramatsu } else { /* Others not supported yet */ 1590146a1439SMasami Hiramatsu semantic_error("%%%s is not supported.\n", arg); 1591146a1439SMasami Hiramatsu return -ENOTSUP; 1592146a1439SMasami Hiramatsu } 159350656eecSMasami Hiramatsu break; 1594146a1439SMasami Hiramatsu default: /* Buggy case */ 1595146a1439SMasami Hiramatsu pr_err("This program has a bug at %s:%d.\n", 1596146a1439SMasami Hiramatsu __FILE__, __LINE__); 1597146a1439SMasami Hiramatsu return -ENOTSUP; 159850656eecSMasami Hiramatsu break; 159950656eecSMasami Hiramatsu } 160050656eecSMasami Hiramatsu } 160150656eecSMasami Hiramatsu 160250656eecSMasami Hiramatsu /* Exclusion check */ 1603146a1439SMasami Hiramatsu if (pp->lazy_line && pp->line) { 16040e43e5d2SMasami Hiramatsu semantic_error("Lazy pattern can't be used with" 16050e43e5d2SMasami Hiramatsu " line number.\n"); 1606146a1439SMasami Hiramatsu return -EINVAL; 1607146a1439SMasami Hiramatsu } 16082a9c8c36SMasami Hiramatsu 1609146a1439SMasami Hiramatsu if (pp->lazy_line && pp->offset) { 16100e43e5d2SMasami Hiramatsu semantic_error("Lazy pattern can't be used with offset.\n"); 1611146a1439SMasami Hiramatsu return -EINVAL; 1612146a1439SMasami Hiramatsu } 16132a9c8c36SMasami Hiramatsu 1614146a1439SMasami Hiramatsu if (pp->line && pp->offset) { 16150e43e5d2SMasami Hiramatsu semantic_error("Offset can't be used with line number.\n"); 1616146a1439SMasami Hiramatsu return -EINVAL; 1617146a1439SMasami Hiramatsu } 161850656eecSMasami Hiramatsu 1619146a1439SMasami Hiramatsu if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { 16202a9c8c36SMasami Hiramatsu semantic_error("File always requires line number or " 16210e43e5d2SMasami Hiramatsu "lazy pattern.\n"); 1622146a1439SMasami Hiramatsu return -EINVAL; 1623146a1439SMasami Hiramatsu } 162450656eecSMasami Hiramatsu 1625146a1439SMasami Hiramatsu if (pp->offset && !pp->function) { 16260e43e5d2SMasami Hiramatsu semantic_error("Offset requires an entry function.\n"); 1627146a1439SMasami Hiramatsu return -EINVAL; 1628146a1439SMasami Hiramatsu } 162950656eecSMasami Hiramatsu 1630146a1439SMasami Hiramatsu if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { 16312a9c8c36SMasami Hiramatsu semantic_error("Offset/Line/Lazy pattern can't be used with " 16320e43e5d2SMasami Hiramatsu "return probe.\n"); 1633146a1439SMasami Hiramatsu return -EINVAL; 1634146a1439SMasami Hiramatsu } 163550656eecSMasami Hiramatsu 16364235b045SMasami Hiramatsu pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", 16372a9c8c36SMasami Hiramatsu pp->function, pp->file, pp->line, pp->offset, pp->retprobe, 16382a9c8c36SMasami Hiramatsu pp->lazy_line); 1639146a1439SMasami Hiramatsu return 0; 164050656eecSMasami Hiramatsu } 164150656eecSMasami Hiramatsu 16427df2f329SMasami Hiramatsu /* Parse perf-probe event argument */ 1643146a1439SMasami Hiramatsu static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) 16447df2f329SMasami Hiramatsu { 1645b2a3c12bSMasami Hiramatsu char *tmp, *goodname; 16467df2f329SMasami Hiramatsu struct perf_probe_arg_field **fieldp; 16477df2f329SMasami Hiramatsu 16487df2f329SMasami Hiramatsu pr_debug("parsing arg: %s into ", str); 16497df2f329SMasami Hiramatsu 165048481938SMasami Hiramatsu tmp = strchr(str, '='); 165148481938SMasami Hiramatsu if (tmp) { 165202b95dadSMasami Hiramatsu arg->name = strndup(str, tmp - str); 165302b95dadSMasami Hiramatsu if (arg->name == NULL) 165402b95dadSMasami Hiramatsu return -ENOMEM; 165511a1ca35SMasami Hiramatsu pr_debug("name:%s ", arg->name); 165648481938SMasami Hiramatsu str = tmp + 1; 165748481938SMasami Hiramatsu } 165848481938SMasami Hiramatsu 16591e032f7cSMasami Hiramatsu tmp = strchr(str, '@'); 16609256c303SSumanth Korikkar if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */ 16611e032f7cSMasami Hiramatsu if (!user_access_is_supported()) { 16621e032f7cSMasami Hiramatsu semantic_error("ftrace does not support user access\n"); 16631e032f7cSMasami Hiramatsu return -EINVAL; 16641e032f7cSMasami Hiramatsu } 16651e032f7cSMasami Hiramatsu *tmp = '\0'; 16661e032f7cSMasami Hiramatsu arg->user_access = true; 16671e032f7cSMasami Hiramatsu pr_debug("user_access "); 16681e032f7cSMasami Hiramatsu } 16691e032f7cSMasami Hiramatsu 167011a1ca35SMasami Hiramatsu tmp = strchr(str, ':'); 167111a1ca35SMasami Hiramatsu if (tmp) { /* Type setting */ 167211a1ca35SMasami Hiramatsu *tmp = '\0'; 167302b95dadSMasami Hiramatsu arg->type = strdup(tmp + 1); 167402b95dadSMasami Hiramatsu if (arg->type == NULL) 167502b95dadSMasami Hiramatsu return -ENOMEM; 167611a1ca35SMasami Hiramatsu pr_debug("type:%s ", arg->type); 167711a1ca35SMasami Hiramatsu } 167811a1ca35SMasami Hiramatsu 1679b2a3c12bSMasami Hiramatsu tmp = strpbrk(str, "-.["); 16807df2f329SMasami Hiramatsu if (!is_c_varname(str) || !tmp) { 16817df2f329SMasami Hiramatsu /* A variable, register, symbol or special value */ 168202b95dadSMasami Hiramatsu arg->var = strdup(str); 168302b95dadSMasami Hiramatsu if (arg->var == NULL) 168402b95dadSMasami Hiramatsu return -ENOMEM; 168548481938SMasami Hiramatsu pr_debug("%s\n", arg->var); 1686146a1439SMasami Hiramatsu return 0; 16877df2f329SMasami Hiramatsu } 16887df2f329SMasami Hiramatsu 1689b2a3c12bSMasami Hiramatsu /* Structure fields or array element */ 169002b95dadSMasami Hiramatsu arg->var = strndup(str, tmp - str); 169102b95dadSMasami Hiramatsu if (arg->var == NULL) 169202b95dadSMasami Hiramatsu return -ENOMEM; 1693b2a3c12bSMasami Hiramatsu goodname = arg->var; 169448481938SMasami Hiramatsu pr_debug("%s, ", arg->var); 16957df2f329SMasami Hiramatsu fieldp = &arg->field; 16967df2f329SMasami Hiramatsu 16977df2f329SMasami Hiramatsu do { 1698e334016fSMasami Hiramatsu *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); 1699e334016fSMasami Hiramatsu if (*fieldp == NULL) 1700e334016fSMasami Hiramatsu return -ENOMEM; 1701b2a3c12bSMasami Hiramatsu if (*tmp == '[') { /* Array */ 1702b2a3c12bSMasami Hiramatsu str = tmp; 1703b2a3c12bSMasami Hiramatsu (*fieldp)->index = strtol(str + 1, &tmp, 0); 1704b2a3c12bSMasami Hiramatsu (*fieldp)->ref = true; 1705b2a3c12bSMasami Hiramatsu if (*tmp != ']' || tmp == str + 1) { 1706b2a3c12bSMasami Hiramatsu semantic_error("Array index must be a" 1707b2a3c12bSMasami Hiramatsu " number.\n"); 1708b2a3c12bSMasami Hiramatsu return -EINVAL; 1709b2a3c12bSMasami Hiramatsu } 1710b2a3c12bSMasami Hiramatsu tmp++; 1711b2a3c12bSMasami Hiramatsu if (*tmp == '\0') 1712b2a3c12bSMasami Hiramatsu tmp = NULL; 1713b2a3c12bSMasami Hiramatsu } else { /* Structure */ 17147df2f329SMasami Hiramatsu if (*tmp == '.') { 17157df2f329SMasami Hiramatsu str = tmp + 1; 17167df2f329SMasami Hiramatsu (*fieldp)->ref = false; 17177df2f329SMasami Hiramatsu } else if (tmp[1] == '>') { 17187df2f329SMasami Hiramatsu str = tmp + 2; 17197df2f329SMasami Hiramatsu (*fieldp)->ref = true; 1720146a1439SMasami Hiramatsu } else { 1721b2a3c12bSMasami Hiramatsu semantic_error("Argument parse error: %s\n", 1722b2a3c12bSMasami Hiramatsu str); 1723146a1439SMasami Hiramatsu return -EINVAL; 1724146a1439SMasami Hiramatsu } 1725b2a3c12bSMasami Hiramatsu tmp = strpbrk(str, "-.["); 1726b2a3c12bSMasami Hiramatsu } 17277df2f329SMasami Hiramatsu if (tmp) { 172802b95dadSMasami Hiramatsu (*fieldp)->name = strndup(str, tmp - str); 172902b95dadSMasami Hiramatsu if ((*fieldp)->name == NULL) 173002b95dadSMasami Hiramatsu return -ENOMEM; 1731b2a3c12bSMasami Hiramatsu if (*str != '[') 1732b2a3c12bSMasami Hiramatsu goodname = (*fieldp)->name; 17337df2f329SMasami Hiramatsu pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); 17347df2f329SMasami Hiramatsu fieldp = &(*fieldp)->next; 17357df2f329SMasami Hiramatsu } 17367df2f329SMasami Hiramatsu } while (tmp); 173702b95dadSMasami Hiramatsu (*fieldp)->name = strdup(str); 173802b95dadSMasami Hiramatsu if ((*fieldp)->name == NULL) 173902b95dadSMasami Hiramatsu return -ENOMEM; 1740b2a3c12bSMasami Hiramatsu if (*str != '[') 1741b2a3c12bSMasami Hiramatsu goodname = (*fieldp)->name; 17427df2f329SMasami Hiramatsu pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); 1743df0faf4bSMasami Hiramatsu 1744b2a3c12bSMasami Hiramatsu /* If no name is specified, set the last field name (not array index)*/ 174502b95dadSMasami Hiramatsu if (!arg->name) { 1746b2a3c12bSMasami Hiramatsu arg->name = strdup(goodname); 174702b95dadSMasami Hiramatsu if (arg->name == NULL) 174802b95dadSMasami Hiramatsu return -ENOMEM; 174902b95dadSMasami Hiramatsu } 1750146a1439SMasami Hiramatsu return 0; 17517df2f329SMasami Hiramatsu } 17527df2f329SMasami Hiramatsu 17534235b045SMasami Hiramatsu /* Parse perf-probe event command */ 1754146a1439SMasami Hiramatsu int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) 175550656eecSMasami Hiramatsu { 1756e1c01d61SMasami Hiramatsu char **argv; 1757146a1439SMasami Hiramatsu int argc, i, ret = 0; 1758fac13fd5SMasami Hiramatsu 17594235b045SMasami Hiramatsu argv = argv_split(cmd, &argc); 1760146a1439SMasami Hiramatsu if (!argv) { 1761146a1439SMasami Hiramatsu pr_debug("Failed to split arguments.\n"); 1762146a1439SMasami Hiramatsu return -ENOMEM; 1763146a1439SMasami Hiramatsu } 1764146a1439SMasami Hiramatsu if (argc - 1 > MAX_PROBE_ARGS) { 1765146a1439SMasami Hiramatsu semantic_error("Too many probe arguments (%d).\n", argc - 1); 1766146a1439SMasami Hiramatsu ret = -ERANGE; 1767146a1439SMasami Hiramatsu goto out; 1768146a1439SMasami Hiramatsu } 176950656eecSMasami Hiramatsu /* Parse probe point */ 1770146a1439SMasami Hiramatsu ret = parse_perf_probe_point(argv[0], pev); 1771146a1439SMasami Hiramatsu if (ret < 0) 1772146a1439SMasami Hiramatsu goto out; 177350656eecSMasami Hiramatsu 177415354d54SMasami Hiramatsu /* Generate event name if needed */ 177515354d54SMasami Hiramatsu if (!pev->event && pev->point.function && pev->point.line 177615354d54SMasami Hiramatsu && !pev->point.lazy_line && !pev->point.offset) { 177715354d54SMasami Hiramatsu if (asprintf(&pev->event, "%s_L%d", pev->point.function, 1778*4bf6dcaaSChristophe JAILLET pev->point.line) < 0) { 1779*4bf6dcaaSChristophe JAILLET ret = -ENOMEM; 1780*4bf6dcaaSChristophe JAILLET goto out; 1781*4bf6dcaaSChristophe JAILLET } 178215354d54SMasami Hiramatsu } 178315354d54SMasami Hiramatsu 1784e1c01d61SMasami Hiramatsu /* Copy arguments and ensure return probe has no C argument */ 17854235b045SMasami Hiramatsu pev->nargs = argc - 1; 1786e334016fSMasami Hiramatsu pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 1787e334016fSMasami Hiramatsu if (pev->args == NULL) { 1788e334016fSMasami Hiramatsu ret = -ENOMEM; 1789e334016fSMasami Hiramatsu goto out; 1790e334016fSMasami Hiramatsu } 1791146a1439SMasami Hiramatsu for (i = 0; i < pev->nargs && ret >= 0; i++) { 1792146a1439SMasami Hiramatsu ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); 1793146a1439SMasami Hiramatsu if (ret >= 0 && 1794146a1439SMasami Hiramatsu is_c_varname(pev->args[i].var) && pev->point.retprobe) { 17954235b045SMasami Hiramatsu semantic_error("You can't specify local variable for" 1796146a1439SMasami Hiramatsu " kretprobe.\n"); 1797146a1439SMasami Hiramatsu ret = -EINVAL; 1798e1c01d61SMasami Hiramatsu } 1799146a1439SMasami Hiramatsu } 1800146a1439SMasami Hiramatsu out: 1801e1c01d61SMasami Hiramatsu argv_free(argv); 1802146a1439SMasami Hiramatsu 1803146a1439SMasami Hiramatsu return ret; 180450656eecSMasami Hiramatsu } 180550656eecSMasami Hiramatsu 1806b3f33f93SRavi Bangoria /* Returns true if *any* ARG is either C variable, $params or $vars. */ 1807b3f33f93SRavi Bangoria bool perf_probe_with_var(struct perf_probe_event *pev) 18084de189feSMasami Hiramatsu { 1809b3f33f93SRavi Bangoria int i = 0; 18104235b045SMasami Hiramatsu 18114235b045SMasami Hiramatsu for (i = 0; i < pev->nargs; i++) 1812f6eb0518SMasami Hiramatsu if (is_c_varname(pev->args[i].var) || 1813b3f33f93SRavi Bangoria !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || 1814b3f33f93SRavi Bangoria !strcmp(pev->args[i].var, PROBE_ARG_VARS)) 1815b3f33f93SRavi Bangoria return true; 1816b3f33f93SRavi Bangoria return false; 1817b3f33f93SRavi Bangoria } 1818b3f33f93SRavi Bangoria 1819b3f33f93SRavi Bangoria /* Return true if this perf_probe_event requires debuginfo */ 1820b3f33f93SRavi Bangoria bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) 1821b3f33f93SRavi Bangoria { 1822b3f33f93SRavi Bangoria if (pev->point.file || pev->point.line || pev->point.lazy_line) 1823b3f33f93SRavi Bangoria return true; 1824b3f33f93SRavi Bangoria 1825b3f33f93SRavi Bangoria if (perf_probe_with_var(pev)) 18264235b045SMasami Hiramatsu return true; 18274235b045SMasami Hiramatsu 18284235b045SMasami Hiramatsu return false; 18294235b045SMasami Hiramatsu } 18304235b045SMasami Hiramatsu 18310e60836bSSrikar Dronamraju /* Parse probe_events event into struct probe_point */ 183292f6c72eSMasami Hiramatsu int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) 18334235b045SMasami Hiramatsu { 18340e60836bSSrikar Dronamraju struct probe_trace_point *tp = &tev->point; 18354de189feSMasami Hiramatsu char pr; 18364de189feSMasami Hiramatsu char *p; 1837bcbd0040SIrina Tirdea char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; 18384de189feSMasami Hiramatsu int ret, i, argc; 18394de189feSMasami Hiramatsu char **argv; 18404de189feSMasami Hiramatsu 18410e60836bSSrikar Dronamraju pr_debug("Parsing probe_events: %s\n", cmd); 18424235b045SMasami Hiramatsu argv = argv_split(cmd, &argc); 1843146a1439SMasami Hiramatsu if (!argv) { 1844146a1439SMasami Hiramatsu pr_debug("Failed to split arguments.\n"); 1845146a1439SMasami Hiramatsu return -ENOMEM; 1846146a1439SMasami Hiramatsu } 1847146a1439SMasami Hiramatsu if (argc < 2) { 1848146a1439SMasami Hiramatsu semantic_error("Too few probe arguments.\n"); 1849146a1439SMasami Hiramatsu ret = -ERANGE; 1850146a1439SMasami Hiramatsu goto out; 1851146a1439SMasami Hiramatsu } 18524de189feSMasami Hiramatsu 18534de189feSMasami Hiramatsu /* Scan event and group name. */ 1854bcbd0040SIrina Tirdea argv0_str = strdup(argv[0]); 1855bcbd0040SIrina Tirdea if (argv0_str == NULL) { 1856bcbd0040SIrina Tirdea ret = -ENOMEM; 1857bcbd0040SIrina Tirdea goto out; 1858bcbd0040SIrina Tirdea } 1859bcbd0040SIrina Tirdea fmt1_str = strtok_r(argv0_str, ":", &fmt); 1860bcbd0040SIrina Tirdea fmt2_str = strtok_r(NULL, "/", &fmt); 1861bcbd0040SIrina Tirdea fmt3_str = strtok_r(NULL, " \t", &fmt); 1862c6aab66aSMasami Hiramatsu if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) { 1863146a1439SMasami Hiramatsu semantic_error("Failed to parse event name: %s\n", argv[0]); 1864146a1439SMasami Hiramatsu ret = -EINVAL; 1865146a1439SMasami Hiramatsu goto out; 1866146a1439SMasami Hiramatsu } 1867bcbd0040SIrina Tirdea pr = fmt1_str[0]; 1868bcbd0040SIrina Tirdea tev->group = strdup(fmt2_str); 1869bcbd0040SIrina Tirdea tev->event = strdup(fmt3_str); 1870bcbd0040SIrina Tirdea if (tev->group == NULL || tev->event == NULL) { 1871bcbd0040SIrina Tirdea ret = -ENOMEM; 1872bcbd0040SIrina Tirdea goto out; 1873bcbd0040SIrina Tirdea } 18744235b045SMasami Hiramatsu pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); 18754de189feSMasami Hiramatsu 18764235b045SMasami Hiramatsu tp->retprobe = (pr == 'r'); 18774de189feSMasami Hiramatsu 1878190b57fcSMasami Hiramatsu /* Scan module name(if there), function name and offset */ 1879190b57fcSMasami Hiramatsu p = strchr(argv[1], ':'); 1880190b57fcSMasami Hiramatsu if (p) { 1881190b57fcSMasami Hiramatsu tp->module = strndup(argv[1], p - argv[1]); 1882844faa4bSMasami Hiramatsu if (!tp->module) { 1883844faa4bSMasami Hiramatsu ret = -ENOMEM; 1884844faa4bSMasami Hiramatsu goto out; 1885844faa4bSMasami Hiramatsu } 188642bba263SMasami Hiramatsu tev->uprobes = (tp->module[0] == '/'); 1887190b57fcSMasami Hiramatsu p++; 1888190b57fcSMasami Hiramatsu } else 1889190b57fcSMasami Hiramatsu p = argv[1]; 1890bcbd0040SIrina Tirdea fmt1_str = strtok_r(p, "+", &fmt); 1891be07afe9SWang Nan /* only the address started with 0x */ 1892be07afe9SWang Nan if (fmt1_str[0] == '0') { 1893be07afe9SWang Nan /* 1894be07afe9SWang Nan * Fix a special case: 1895be07afe9SWang Nan * if address == 0, kernel reports something like: 1896be07afe9SWang Nan * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax 1897be07afe9SWang Nan * Newer kernel may fix that, but we want to 1898be07afe9SWang Nan * support old kernel also. 1899be07afe9SWang Nan */ 1900be07afe9SWang Nan if (strcmp(fmt1_str, "0x") == 0) { 1901be07afe9SWang Nan if (!argv[2] || strcmp(argv[2], "(null)")) { 1902be07afe9SWang Nan ret = -EINVAL; 1903be07afe9SWang Nan goto out; 1904be07afe9SWang Nan } 1905be07afe9SWang Nan tp->address = 0; 1906be07afe9SWang Nan 1907be07afe9SWang Nan free(argv[2]); 1908be07afe9SWang Nan for (i = 2; argv[i + 1] != NULL; i++) 1909be07afe9SWang Nan argv[i] = argv[i + 1]; 1910be07afe9SWang Nan 1911be07afe9SWang Nan argv[i] = NULL; 1912be07afe9SWang Nan argc -= 1; 1913be07afe9SWang Nan } else 191422a66551SYang Jihong tp->address = strtoull(fmt1_str, NULL, 0); 1915be07afe9SWang Nan } else { 19165a6f6314SMasami Hiramatsu /* Only the symbol-based probe has offset */ 1917bcbd0040SIrina Tirdea tp->symbol = strdup(fmt1_str); 1918bcbd0040SIrina Tirdea if (tp->symbol == NULL) { 1919bcbd0040SIrina Tirdea ret = -ENOMEM; 1920bcbd0040SIrina Tirdea goto out; 1921bcbd0040SIrina Tirdea } 1922bcbd0040SIrina Tirdea fmt2_str = strtok_r(NULL, "", &fmt); 1923bcbd0040SIrina Tirdea if (fmt2_str == NULL) 19244235b045SMasami Hiramatsu tp->offset = 0; 1925bcbd0040SIrina Tirdea else 1926bcbd0040SIrina Tirdea tp->offset = strtoul(fmt2_str, NULL, 10); 19275a6f6314SMasami Hiramatsu } 19284de189feSMasami Hiramatsu 19295a5e3d3cSRavi Bangoria if (tev->uprobes) { 19305a5e3d3cSRavi Bangoria fmt2_str = strchr(p, '('); 19315a5e3d3cSRavi Bangoria if (fmt2_str) 19325a5e3d3cSRavi Bangoria tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0); 19335a5e3d3cSRavi Bangoria } 19345a5e3d3cSRavi Bangoria 19354235b045SMasami Hiramatsu tev->nargs = argc - 2; 19360e60836bSSrikar Dronamraju tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1937e334016fSMasami Hiramatsu if (tev->args == NULL) { 1938e334016fSMasami Hiramatsu ret = -ENOMEM; 1939e334016fSMasami Hiramatsu goto out; 1940e334016fSMasami Hiramatsu } 19414235b045SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) { 19424de189feSMasami Hiramatsu p = strchr(argv[i + 2], '='); 19434de189feSMasami Hiramatsu if (p) /* We don't need which register is assigned. */ 19444235b045SMasami Hiramatsu *p++ = '\0'; 19454235b045SMasami Hiramatsu else 19464235b045SMasami Hiramatsu p = argv[i + 2]; 194702b95dadSMasami Hiramatsu tev->args[i].name = strdup(argv[i + 2]); 19484235b045SMasami Hiramatsu /* TODO: parse regs and offset */ 194902b95dadSMasami Hiramatsu tev->args[i].value = strdup(p); 195002b95dadSMasami Hiramatsu if (tev->args[i].name == NULL || tev->args[i].value == NULL) { 195102b95dadSMasami Hiramatsu ret = -ENOMEM; 195202b95dadSMasami Hiramatsu goto out; 195302b95dadSMasami Hiramatsu } 19544de189feSMasami Hiramatsu } 1955146a1439SMasami Hiramatsu ret = 0; 1956146a1439SMasami Hiramatsu out: 1957bcbd0040SIrina Tirdea free(argv0_str); 19584de189feSMasami Hiramatsu argv_free(argv); 1959146a1439SMasami Hiramatsu return ret; 19604de189feSMasami Hiramatsu } 19614de189feSMasami Hiramatsu 19627df2f329SMasami Hiramatsu /* Compose only probe arg */ 1963909b0360SMasami Hiramatsu char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) 19647df2f329SMasami Hiramatsu { 19657df2f329SMasami Hiramatsu struct perf_probe_arg_field *field = pa->field; 1966909b0360SMasami Hiramatsu struct strbuf buf; 1967bf4d5f25SMasami Hiramatsu char *ret = NULL; 1968bf4d5f25SMasami Hiramatsu int err; 19697df2f329SMasami Hiramatsu 1970bf4d5f25SMasami Hiramatsu if (strbuf_init(&buf, 64) < 0) 1971bf4d5f25SMasami Hiramatsu return NULL; 1972bf4d5f25SMasami Hiramatsu 197348481938SMasami Hiramatsu if (pa->name && pa->var) 1974bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); 197548481938SMasami Hiramatsu else 1976bf4d5f25SMasami Hiramatsu err = strbuf_addstr(&buf, pa->name ?: pa->var); 1977bf4d5f25SMasami Hiramatsu if (err) 1978bf4d5f25SMasami Hiramatsu goto out; 19797df2f329SMasami Hiramatsu 19807df2f329SMasami Hiramatsu while (field) { 1981b2a3c12bSMasami Hiramatsu if (field->name[0] == '[') 1982bf4d5f25SMasami Hiramatsu err = strbuf_addstr(&buf, field->name); 1983b2a3c12bSMasami Hiramatsu else 1984bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", 1985909b0360SMasami Hiramatsu field->name); 19867df2f329SMasami Hiramatsu field = field->next; 1987bf4d5f25SMasami Hiramatsu if (err) 1988bf4d5f25SMasami Hiramatsu goto out; 19897df2f329SMasami Hiramatsu } 199011a1ca35SMasami Hiramatsu 1991909b0360SMasami Hiramatsu if (pa->type) 1992bf4d5f25SMasami Hiramatsu if (strbuf_addf(&buf, ":%s", pa->type) < 0) 1993bf4d5f25SMasami Hiramatsu goto out; 199411a1ca35SMasami Hiramatsu 1995909b0360SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 1996bf4d5f25SMasami Hiramatsu out: 1997bf4d5f25SMasami Hiramatsu strbuf_release(&buf); 1998146a1439SMasami Hiramatsu return ret; 19997df2f329SMasami Hiramatsu } 20007df2f329SMasami Hiramatsu 20014235b045SMasami Hiramatsu /* Compose only probe point (not argument) */ 2002c4ff4920SMasami Hiramatsu char *synthesize_perf_probe_point(struct perf_probe_point *pp) 200350656eecSMasami Hiramatsu { 2004909b0360SMasami Hiramatsu struct strbuf buf; 2005bf4d5f25SMasami Hiramatsu char *tmp, *ret = NULL; 2006bf4d5f25SMasami Hiramatsu int len, err = 0; 200750656eecSMasami Hiramatsu 2008bf4d5f25SMasami Hiramatsu if (strbuf_init(&buf, 64) < 0) 2009bf4d5f25SMasami Hiramatsu return NULL; 2010bf4d5f25SMasami Hiramatsu 2011909b0360SMasami Hiramatsu if (pp->function) { 2012bf4d5f25SMasami Hiramatsu if (strbuf_addstr(&buf, pp->function) < 0) 2013bf4d5f25SMasami Hiramatsu goto out; 2014909b0360SMasami Hiramatsu if (pp->offset) 2015bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "+%lu", pp->offset); 2016909b0360SMasami Hiramatsu else if (pp->line) 2017bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, ":%d", pp->line); 2018909b0360SMasami Hiramatsu else if (pp->retprobe) 2019bf4d5f25SMasami Hiramatsu err = strbuf_addstr(&buf, "%return"); 2020bf4d5f25SMasami Hiramatsu if (err) 2021bf4d5f25SMasami Hiramatsu goto out; 2022fb1587d8SMasami Hiramatsu } 2023fb1587d8SMasami Hiramatsu if (pp->file) { 202432ae2adeSFranck Bui-Huu tmp = pp->file; 202532ae2adeSFranck Bui-Huu len = strlen(tmp); 202632ae2adeSFranck Bui-Huu if (len > 30) { 202732ae2adeSFranck Bui-Huu tmp = strchr(pp->file + len - 30, '/'); 202832ae2adeSFranck Bui-Huu tmp = tmp ? tmp + 1 : pp->file + len - 30; 202932ae2adeSFranck Bui-Huu } 2030bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "@%s", tmp); 2031bf4d5f25SMasami Hiramatsu if (!err && !pp->function && pp->line) 2032bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, ":%d", pp->line); 20334de189feSMasami Hiramatsu } 2034bf4d5f25SMasami Hiramatsu if (!err) 2035bf4d5f25SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 2036bf4d5f25SMasami Hiramatsu out: 2037bf4d5f25SMasami Hiramatsu strbuf_release(&buf); 2038bf4d5f25SMasami Hiramatsu return ret; 20394235b045SMasami Hiramatsu } 20404235b045SMasami Hiramatsu 20414235b045SMasami Hiramatsu char *synthesize_perf_probe_command(struct perf_probe_event *pev) 20424235b045SMasami Hiramatsu { 2043c4ff4920SMasami Hiramatsu struct strbuf buf; 2044c4ff4920SMasami Hiramatsu char *tmp, *ret = NULL; 2045c4ff4920SMasami Hiramatsu int i; 20464235b045SMasami Hiramatsu 2047c4ff4920SMasami Hiramatsu if (strbuf_init(&buf, 64)) 20484235b045SMasami Hiramatsu return NULL; 2049c4ff4920SMasami Hiramatsu if (pev->event) 2050c4ff4920SMasami Hiramatsu if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, 2051c4ff4920SMasami Hiramatsu pev->event) < 0) 2052c4ff4920SMasami Hiramatsu goto out; 20534235b045SMasami Hiramatsu 2054c4ff4920SMasami Hiramatsu tmp = synthesize_perf_probe_point(&pev->point); 2055c4ff4920SMasami Hiramatsu if (!tmp || strbuf_addstr(&buf, tmp) < 0) 2056c4ff4920SMasami Hiramatsu goto out; 2057c4ff4920SMasami Hiramatsu free(tmp); 2058c4ff4920SMasami Hiramatsu 20594235b045SMasami Hiramatsu for (i = 0; i < pev->nargs; i++) { 2060c4ff4920SMasami Hiramatsu tmp = synthesize_perf_probe_arg(pev->args + i); 2061c4ff4920SMasami Hiramatsu if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) 2062c4ff4920SMasami Hiramatsu goto out; 2063c4ff4920SMasami Hiramatsu free(tmp); 20647ef17aafSMasami Hiramatsu } 206550656eecSMasami Hiramatsu 2066c4ff4920SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 2067c4ff4920SMasami Hiramatsu out: 2068c4ff4920SMasami Hiramatsu strbuf_release(&buf); 2069c4ff4920SMasami Hiramatsu return ret; 20704235b045SMasami Hiramatsu } 20714235b045SMasami Hiramatsu 20720e60836bSSrikar Dronamraju static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, 2073909b0360SMasami Hiramatsu struct strbuf *buf, int depth) 20747ef17aafSMasami Hiramatsu { 2075bf4d5f25SMasami Hiramatsu int err; 20764235b045SMasami Hiramatsu if (ref->next) { 20770e60836bSSrikar Dronamraju depth = __synthesize_probe_trace_arg_ref(ref->next, buf, 2078909b0360SMasami Hiramatsu depth + 1); 20794235b045SMasami Hiramatsu if (depth < 0) 20804235b045SMasami Hiramatsu return depth; 20814235b045SMasami Hiramatsu } 20829256c303SSumanth Korikkar if (ref->user_access) 20839256c303SSumanth Korikkar err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset); 20849256c303SSumanth Korikkar else 2085bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, "%+ld(", ref->offset); 2086bf4d5f25SMasami Hiramatsu return (err < 0) ? err : depth; 2087bf4d5f25SMasami Hiramatsu } 20884235b045SMasami Hiramatsu 20890e60836bSSrikar Dronamraju static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, 2090909b0360SMasami Hiramatsu struct strbuf *buf) 20914235b045SMasami Hiramatsu { 20920e60836bSSrikar Dronamraju struct probe_trace_arg_ref *ref = arg->ref; 2093bf4d5f25SMasami Hiramatsu int depth = 0, err; 20944235b045SMasami Hiramatsu 20954235b045SMasami Hiramatsu /* Argument name or separator */ 20964235b045SMasami Hiramatsu if (arg->name) 2097bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, " %s=", arg->name); 20984235b045SMasami Hiramatsu else 2099bf4d5f25SMasami Hiramatsu err = strbuf_addch(buf, ' '); 2100bf4d5f25SMasami Hiramatsu if (err) 2101bf4d5f25SMasami Hiramatsu return err; 21024235b045SMasami Hiramatsu 2103b7dcb857SMasami Hiramatsu /* Special case: @XXX */ 2104b7dcb857SMasami Hiramatsu if (arg->value[0] == '@' && arg->ref) 2105b7dcb857SMasami Hiramatsu ref = ref->next; 2106b7dcb857SMasami Hiramatsu 21074235b045SMasami Hiramatsu /* Dereferencing arguments */ 2108b7dcb857SMasami Hiramatsu if (ref) { 2109909b0360SMasami Hiramatsu depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); 21104235b045SMasami Hiramatsu if (depth < 0) 21114235b045SMasami Hiramatsu return depth; 21124235b045SMasami Hiramatsu } 21134235b045SMasami Hiramatsu 21144235b045SMasami Hiramatsu /* Print argument value */ 2115b7dcb857SMasami Hiramatsu if (arg->value[0] == '@' && arg->ref) 2116bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); 2117b7dcb857SMasami Hiramatsu else 2118bf4d5f25SMasami Hiramatsu err = strbuf_addstr(buf, arg->value); 21194235b045SMasami Hiramatsu 21204235b045SMasami Hiramatsu /* Closing */ 2121bf4d5f25SMasami Hiramatsu while (!err && depth--) 2122bf4d5f25SMasami Hiramatsu err = strbuf_addch(buf, ')'); 21234235b045SMasami Hiramatsu 2124bf4d5f25SMasami Hiramatsu /* Print argument type */ 2125bf4d5f25SMasami Hiramatsu if (!err && arg->type) 2126bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, ":%s", arg->type); 2127bf4d5f25SMasami Hiramatsu 2128bf4d5f25SMasami Hiramatsu return err; 21294235b045SMasami Hiramatsu } 21304235b045SMasami Hiramatsu 21315a5e3d3cSRavi Bangoria static int 2132d26ea481SMasami Hiramatsu synthesize_probe_trace_args(struct probe_trace_event *tev, struct strbuf *buf) 21335a5e3d3cSRavi Bangoria { 2134d26ea481SMasami Hiramatsu int i, ret = 0; 2135d26ea481SMasami Hiramatsu 2136d26ea481SMasami Hiramatsu for (i = 0; i < tev->nargs && ret >= 0; i++) 2137d26ea481SMasami Hiramatsu ret = synthesize_probe_trace_arg(&tev->args[i], buf); 2138d26ea481SMasami Hiramatsu 2139d26ea481SMasami Hiramatsu return ret; 2140d26ea481SMasami Hiramatsu } 2141d26ea481SMasami Hiramatsu 2142d26ea481SMasami Hiramatsu static int 2143d26ea481SMasami Hiramatsu synthesize_uprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf) 2144d26ea481SMasami Hiramatsu { 21455a5e3d3cSRavi Bangoria int err; 21465a5e3d3cSRavi Bangoria 2147d26ea481SMasami Hiramatsu /* Uprobes must have tp->module */ 2148d26ea481SMasami Hiramatsu if (!tp->module) 2149d26ea481SMasami Hiramatsu return -EINVAL; 2150d26ea481SMasami Hiramatsu /* 2151d26ea481SMasami Hiramatsu * If tp->address == 0, then this point must be a 2152d26ea481SMasami Hiramatsu * absolute address uprobe. 2153d26ea481SMasami Hiramatsu * try_to_find_absolute_address() should have made 2154d26ea481SMasami Hiramatsu * tp->symbol to "0x0". 2155d26ea481SMasami Hiramatsu */ 2156d26ea481SMasami Hiramatsu if (!tp->address && (!tp->symbol || strcmp(tp->symbol, "0x0"))) 2157d26ea481SMasami Hiramatsu return -EINVAL; 2158d26ea481SMasami Hiramatsu 2159d26ea481SMasami Hiramatsu /* Use the tp->address for uprobes */ 216022a66551SYang Jihong err = strbuf_addf(buf, "%s:0x%" PRIx64, tp->module, tp->address); 21615a5e3d3cSRavi Bangoria 21625a5e3d3cSRavi Bangoria if (err >= 0 && tp->ref_ctr_offset) { 21635a5e3d3cSRavi Bangoria if (!uprobe_ref_ctr_is_supported()) 2164d26ea481SMasami Hiramatsu return -EINVAL; 21655a5e3d3cSRavi Bangoria err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset); 21665a5e3d3cSRavi Bangoria } 2167d26ea481SMasami Hiramatsu return err >= 0 ? 0 : err; 2168d26ea481SMasami Hiramatsu } 2169d26ea481SMasami Hiramatsu 2170d26ea481SMasami Hiramatsu static int 2171d26ea481SMasami Hiramatsu synthesize_kprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf) 2172d26ea481SMasami Hiramatsu { 2173d26ea481SMasami Hiramatsu if (!strncmp(tp->symbol, "0x", 2)) { 2174d26ea481SMasami Hiramatsu /* Absolute address. See try_to_find_absolute_address() */ 217522a66551SYang Jihong return strbuf_addf(buf, "%s%s0x%" PRIx64, tp->module ?: "", 2176d26ea481SMasami Hiramatsu tp->module ? ":" : "", tp->address); 2177d26ea481SMasami Hiramatsu } else { 2178d26ea481SMasami Hiramatsu return strbuf_addf(buf, "%s%s%s+%lu", tp->module ?: "", 2179d26ea481SMasami Hiramatsu tp->module ? ":" : "", tp->symbol, tp->offset); 2180d26ea481SMasami Hiramatsu } 21815a5e3d3cSRavi Bangoria } 21825a5e3d3cSRavi Bangoria 21830e60836bSSrikar Dronamraju char *synthesize_probe_trace_command(struct probe_trace_event *tev) 21844235b045SMasami Hiramatsu { 21850e60836bSSrikar Dronamraju struct probe_trace_point *tp = &tev->point; 2186909b0360SMasami Hiramatsu struct strbuf buf; 2187909b0360SMasami Hiramatsu char *ret = NULL; 2188d26ea481SMasami Hiramatsu int err; 2189909b0360SMasami Hiramatsu 2190bf4d5f25SMasami Hiramatsu if (strbuf_init(&buf, 32) < 0) 2191bf4d5f25SMasami Hiramatsu return NULL; 2192bf4d5f25SMasami Hiramatsu 2193bf4d5f25SMasami Hiramatsu if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', 2194bf4d5f25SMasami Hiramatsu tev->group, tev->event) < 0) 2195bf4d5f25SMasami Hiramatsu goto error; 2196eb948e50SMasami Hiramatsu 2197d26ea481SMasami Hiramatsu if (tev->uprobes) 2198d26ea481SMasami Hiramatsu err = synthesize_uprobe_trace_def(tp, &buf); 2199d26ea481SMasami Hiramatsu else 2200d26ea481SMasami Hiramatsu err = synthesize_kprobe_trace_def(tp, &buf); 22015a5e3d3cSRavi Bangoria 2202d26ea481SMasami Hiramatsu if (err >= 0) 2203d26ea481SMasami Hiramatsu err = synthesize_probe_trace_args(tev, &buf); 2204225466f1SSrikar Dronamraju 2205d26ea481SMasami Hiramatsu if (err >= 0) 2206909b0360SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 220750656eecSMasami Hiramatsu error: 2208909b0360SMasami Hiramatsu strbuf_release(&buf); 2209909b0360SMasami Hiramatsu return ret; 221050656eecSMasami Hiramatsu } 221150656eecSMasami Hiramatsu 22125a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_map(struct probe_trace_point *tp, 22135a6f6314SMasami Hiramatsu struct perf_probe_point *pp, 22145a6f6314SMasami Hiramatsu bool is_kprobe) 22155a6f6314SMasami Hiramatsu { 22165a6f6314SMasami Hiramatsu struct symbol *sym = NULL; 22178a2efd6dSArnaldo Carvalho de Melo struct map *map = NULL; 2218e486367fSWang Nan u64 addr = tp->address; 22195a6f6314SMasami Hiramatsu int ret = -ENOENT; 22205a6f6314SMasami Hiramatsu 22215a6f6314SMasami Hiramatsu if (!is_kprobe) { 22225a6f6314SMasami Hiramatsu map = dso__new_map(tp->module); 22235a6f6314SMasami Hiramatsu if (!map) 22245a6f6314SMasami Hiramatsu goto out; 2225be39db9fSArnaldo Carvalho de Melo sym = map__find_symbol(map, addr); 22265a6f6314SMasami Hiramatsu } else { 22279b239a12SMasami Hiramatsu if (tp->symbol && !addr) { 22280a62f686SMasami Hiramatsu if (kernel_get_symbol_address_by_name(tp->symbol, 22290a62f686SMasami Hiramatsu &addr, true, false) < 0) 22309b239a12SMasami Hiramatsu goto out; 22319b239a12SMasami Hiramatsu } 22325a6f6314SMasami Hiramatsu if (addr) { 22335a6f6314SMasami Hiramatsu addr += tp->offset; 2234107cad95SArnaldo Carvalho de Melo sym = machine__find_kernel_symbol(host_machine, addr, &map); 22355a6f6314SMasami Hiramatsu } 22365a6f6314SMasami Hiramatsu } 223798d3b258SWang Nan 22385a6f6314SMasami Hiramatsu if (!sym) 22395a6f6314SMasami Hiramatsu goto out; 22405a6f6314SMasami Hiramatsu 22415a6f6314SMasami Hiramatsu pp->retprobe = tp->retprobe; 22425a6f6314SMasami Hiramatsu pp->offset = addr - map->unmap_ip(map, sym->start); 22435a6f6314SMasami Hiramatsu pp->function = strdup(sym->name); 22445a6f6314SMasami Hiramatsu ret = pp->function ? 0 : -ENOMEM; 22455a6f6314SMasami Hiramatsu 22465a6f6314SMasami Hiramatsu out: 22475a6f6314SMasami Hiramatsu if (map && !is_kprobe) { 224884c2cafaSArnaldo Carvalho de Melo map__put(map); 22495a6f6314SMasami Hiramatsu } 22505a6f6314SMasami Hiramatsu 22515a6f6314SMasami Hiramatsu return ret; 22525a6f6314SMasami Hiramatsu } 22535a6f6314SMasami Hiramatsu 22545a6f6314SMasami Hiramatsu static int convert_to_perf_probe_point(struct probe_trace_point *tp, 22555a6f6314SMasami Hiramatsu struct perf_probe_point *pp, 22565a6f6314SMasami Hiramatsu bool is_kprobe) 22575a6f6314SMasami Hiramatsu { 22585a6f6314SMasami Hiramatsu char buf[128]; 22595a6f6314SMasami Hiramatsu int ret; 22605a6f6314SMasami Hiramatsu 22615a6f6314SMasami Hiramatsu ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); 22625a6f6314SMasami Hiramatsu if (!ret) 22635a6f6314SMasami Hiramatsu return 0; 22645a6f6314SMasami Hiramatsu ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); 22655a6f6314SMasami Hiramatsu if (!ret) 22665a6f6314SMasami Hiramatsu return 0; 22675a6f6314SMasami Hiramatsu 22685a6f6314SMasami Hiramatsu pr_debug("Failed to find probe point from both of dwarf and map.\n"); 22695a6f6314SMasami Hiramatsu 22705a6f6314SMasami Hiramatsu if (tp->symbol) { 22715a6f6314SMasami Hiramatsu pp->function = strdup(tp->symbol); 22725a6f6314SMasami Hiramatsu pp->offset = tp->offset; 2273614e2fdbSWang Nan } else { 227422a66551SYang Jihong ret = e_snprintf(buf, 128, "0x%" PRIx64, tp->address); 22755a6f6314SMasami Hiramatsu if (ret < 0) 22765a6f6314SMasami Hiramatsu return ret; 22775a6f6314SMasami Hiramatsu pp->function = strdup(buf); 22785a6f6314SMasami Hiramatsu pp->offset = 0; 22795a6f6314SMasami Hiramatsu } 22805a6f6314SMasami Hiramatsu if (pp->function == NULL) 22815a6f6314SMasami Hiramatsu return -ENOMEM; 22825a6f6314SMasami Hiramatsu 22835a6f6314SMasami Hiramatsu pp->retprobe = tp->retprobe; 22845a6f6314SMasami Hiramatsu 22855a6f6314SMasami Hiramatsu return 0; 22865a6f6314SMasami Hiramatsu } 22875a6f6314SMasami Hiramatsu 22880e60836bSSrikar Dronamraju static int convert_to_perf_probe_event(struct probe_trace_event *tev, 2289225466f1SSrikar Dronamraju struct perf_probe_event *pev, bool is_kprobe) 22904de189feSMasami Hiramatsu { 2291909b0360SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 2292146a1439SMasami Hiramatsu int i, ret; 22934de189feSMasami Hiramatsu 22944b4da7f7SMasami Hiramatsu /* Convert event/group name */ 229502b95dadSMasami Hiramatsu pev->event = strdup(tev->event); 229602b95dadSMasami Hiramatsu pev->group = strdup(tev->group); 229702b95dadSMasami Hiramatsu if (pev->event == NULL || pev->group == NULL) 229802b95dadSMasami Hiramatsu return -ENOMEM; 2299fb1587d8SMasami Hiramatsu 23004b4da7f7SMasami Hiramatsu /* Convert trace_point to probe_point */ 23015a6f6314SMasami Hiramatsu ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); 2302146a1439SMasami Hiramatsu if (ret < 0) 2303146a1439SMasami Hiramatsu return ret; 23044b4da7f7SMasami Hiramatsu 23054235b045SMasami Hiramatsu /* Convert trace_arg to probe_arg */ 23064235b045SMasami Hiramatsu pev->nargs = tev->nargs; 2307e334016fSMasami Hiramatsu pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 2308e334016fSMasami Hiramatsu if (pev->args == NULL) 2309e334016fSMasami Hiramatsu return -ENOMEM; 231002b95dadSMasami Hiramatsu for (i = 0; i < tev->nargs && ret >= 0; i++) { 23114235b045SMasami Hiramatsu if (tev->args[i].name) 231202b95dadSMasami Hiramatsu pev->args[i].name = strdup(tev->args[i].name); 23134235b045SMasami Hiramatsu else { 2314bf4d5f25SMasami Hiramatsu if ((ret = strbuf_init(&buf, 32)) < 0) 2315bf4d5f25SMasami Hiramatsu goto error; 2316909b0360SMasami Hiramatsu ret = synthesize_probe_trace_arg(&tev->args[i], &buf); 2317909b0360SMasami Hiramatsu pev->args[i].name = strbuf_detach(&buf, NULL); 231802b95dadSMasami Hiramatsu } 231902b95dadSMasami Hiramatsu if (pev->args[i].name == NULL && ret >= 0) 232002b95dadSMasami Hiramatsu ret = -ENOMEM; 23214de189feSMasami Hiramatsu } 2322bf4d5f25SMasami Hiramatsu error: 2323146a1439SMasami Hiramatsu if (ret < 0) 2324146a1439SMasami Hiramatsu clear_perf_probe_event(pev); 2325146a1439SMasami Hiramatsu 2326146a1439SMasami Hiramatsu return ret; 23274235b045SMasami Hiramatsu } 23284de189feSMasami Hiramatsu 23294235b045SMasami Hiramatsu void clear_perf_probe_event(struct perf_probe_event *pev) 23304235b045SMasami Hiramatsu { 23317df2f329SMasami Hiramatsu struct perf_probe_arg_field *field, *next; 23324235b045SMasami Hiramatsu int i; 23334de189feSMasami Hiramatsu 2334d8f9da24SArnaldo Carvalho de Melo zfree(&pev->event); 2335d8f9da24SArnaldo Carvalho de Melo zfree(&pev->group); 2336d8f9da24SArnaldo Carvalho de Melo zfree(&pev->target); 23379b118acaSMasami Hiramatsu clear_perf_probe_point(&pev->point); 2338f5385650SArnaldo Carvalho de Melo 23397df2f329SMasami Hiramatsu for (i = 0; i < pev->nargs; i++) { 2340d8f9da24SArnaldo Carvalho de Melo zfree(&pev->args[i].name); 2341d8f9da24SArnaldo Carvalho de Melo zfree(&pev->args[i].var); 2342d8f9da24SArnaldo Carvalho de Melo zfree(&pev->args[i].type); 23437df2f329SMasami Hiramatsu field = pev->args[i].field; 23447df2f329SMasami Hiramatsu while (field) { 23457df2f329SMasami Hiramatsu next = field->next; 234674cf249dSArnaldo Carvalho de Melo zfree(&field->name); 23477df2f329SMasami Hiramatsu free(field); 23487df2f329SMasami Hiramatsu field = next; 23497df2f329SMasami Hiramatsu } 23507df2f329SMasami Hiramatsu } 2351df8350edSArnaldo Carvalho de Melo pev->nargs = 0; 2352d8f9da24SArnaldo Carvalho de Melo zfree(&pev->args); 23534235b045SMasami Hiramatsu } 23544235b045SMasami Hiramatsu 23550542bb9cSMasami Hiramatsu #define strdup_or_goto(str, label) \ 23560542bb9cSMasami Hiramatsu ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) 23570542bb9cSMasami Hiramatsu 23580542bb9cSMasami Hiramatsu static int perf_probe_point__copy(struct perf_probe_point *dst, 23590542bb9cSMasami Hiramatsu struct perf_probe_point *src) 23600542bb9cSMasami Hiramatsu { 23610542bb9cSMasami Hiramatsu dst->file = strdup_or_goto(src->file, out_err); 23620542bb9cSMasami Hiramatsu dst->function = strdup_or_goto(src->function, out_err); 23630542bb9cSMasami Hiramatsu dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); 23640542bb9cSMasami Hiramatsu dst->line = src->line; 23650542bb9cSMasami Hiramatsu dst->retprobe = src->retprobe; 23660542bb9cSMasami Hiramatsu dst->offset = src->offset; 23670542bb9cSMasami Hiramatsu return 0; 23680542bb9cSMasami Hiramatsu 23690542bb9cSMasami Hiramatsu out_err: 23700542bb9cSMasami Hiramatsu clear_perf_probe_point(dst); 23710542bb9cSMasami Hiramatsu return -ENOMEM; 23720542bb9cSMasami Hiramatsu } 23730542bb9cSMasami Hiramatsu 23740542bb9cSMasami Hiramatsu static int perf_probe_arg__copy(struct perf_probe_arg *dst, 23750542bb9cSMasami Hiramatsu struct perf_probe_arg *src) 23760542bb9cSMasami Hiramatsu { 23770542bb9cSMasami Hiramatsu struct perf_probe_arg_field *field, **ppfield; 23780542bb9cSMasami Hiramatsu 23790542bb9cSMasami Hiramatsu dst->name = strdup_or_goto(src->name, out_err); 23800542bb9cSMasami Hiramatsu dst->var = strdup_or_goto(src->var, out_err); 23810542bb9cSMasami Hiramatsu dst->type = strdup_or_goto(src->type, out_err); 23820542bb9cSMasami Hiramatsu 23830542bb9cSMasami Hiramatsu field = src->field; 23840542bb9cSMasami Hiramatsu ppfield = &(dst->field); 23850542bb9cSMasami Hiramatsu while (field) { 23860542bb9cSMasami Hiramatsu *ppfield = zalloc(sizeof(*field)); 23870542bb9cSMasami Hiramatsu if (!*ppfield) 23880542bb9cSMasami Hiramatsu goto out_err; 23890542bb9cSMasami Hiramatsu (*ppfield)->name = strdup_or_goto(field->name, out_err); 23900542bb9cSMasami Hiramatsu (*ppfield)->index = field->index; 23910542bb9cSMasami Hiramatsu (*ppfield)->ref = field->ref; 23920542bb9cSMasami Hiramatsu field = field->next; 23930542bb9cSMasami Hiramatsu ppfield = &((*ppfield)->next); 23940542bb9cSMasami Hiramatsu } 23950542bb9cSMasami Hiramatsu return 0; 23960542bb9cSMasami Hiramatsu out_err: 23970542bb9cSMasami Hiramatsu return -ENOMEM; 23980542bb9cSMasami Hiramatsu } 23990542bb9cSMasami Hiramatsu 24000542bb9cSMasami Hiramatsu int perf_probe_event__copy(struct perf_probe_event *dst, 24010542bb9cSMasami Hiramatsu struct perf_probe_event *src) 24020542bb9cSMasami Hiramatsu { 24030542bb9cSMasami Hiramatsu int i; 24040542bb9cSMasami Hiramatsu 24050542bb9cSMasami Hiramatsu dst->event = strdup_or_goto(src->event, out_err); 24060542bb9cSMasami Hiramatsu dst->group = strdup_or_goto(src->group, out_err); 24070542bb9cSMasami Hiramatsu dst->target = strdup_or_goto(src->target, out_err); 24080542bb9cSMasami Hiramatsu dst->uprobes = src->uprobes; 24090542bb9cSMasami Hiramatsu 24100542bb9cSMasami Hiramatsu if (perf_probe_point__copy(&dst->point, &src->point) < 0) 24110542bb9cSMasami Hiramatsu goto out_err; 24120542bb9cSMasami Hiramatsu 24130542bb9cSMasami Hiramatsu dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); 24140542bb9cSMasami Hiramatsu if (!dst->args) 24150542bb9cSMasami Hiramatsu goto out_err; 24160542bb9cSMasami Hiramatsu dst->nargs = src->nargs; 24170542bb9cSMasami Hiramatsu 24180542bb9cSMasami Hiramatsu for (i = 0; i < src->nargs; i++) 24190542bb9cSMasami Hiramatsu if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) 24200542bb9cSMasami Hiramatsu goto out_err; 24210542bb9cSMasami Hiramatsu return 0; 24220542bb9cSMasami Hiramatsu 24230542bb9cSMasami Hiramatsu out_err: 24240542bb9cSMasami Hiramatsu clear_perf_probe_event(dst); 24250542bb9cSMasami Hiramatsu return -ENOMEM; 24260542bb9cSMasami Hiramatsu } 24270542bb9cSMasami Hiramatsu 242892f6c72eSMasami Hiramatsu void clear_probe_trace_event(struct probe_trace_event *tev) 24294235b045SMasami Hiramatsu { 24300e60836bSSrikar Dronamraju struct probe_trace_arg_ref *ref, *next; 24314235b045SMasami Hiramatsu int i; 24324235b045SMasami Hiramatsu 2433d8f9da24SArnaldo Carvalho de Melo zfree(&tev->event); 2434d8f9da24SArnaldo Carvalho de Melo zfree(&tev->group); 2435d8f9da24SArnaldo Carvalho de Melo zfree(&tev->point.symbol); 2436d8f9da24SArnaldo Carvalho de Melo zfree(&tev->point.realname); 2437d8f9da24SArnaldo Carvalho de Melo zfree(&tev->point.module); 24384235b045SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) { 2439d8f9da24SArnaldo Carvalho de Melo zfree(&tev->args[i].name); 2440d8f9da24SArnaldo Carvalho de Melo zfree(&tev->args[i].value); 2441d8f9da24SArnaldo Carvalho de Melo zfree(&tev->args[i].type); 24424235b045SMasami Hiramatsu ref = tev->args[i].ref; 24434235b045SMasami Hiramatsu while (ref) { 24444235b045SMasami Hiramatsu next = ref->next; 24454235b045SMasami Hiramatsu free(ref); 24464235b045SMasami Hiramatsu ref = next; 24474235b045SMasami Hiramatsu } 24484235b045SMasami Hiramatsu } 2449d8f9da24SArnaldo Carvalho de Melo zfree(&tev->args); 24509e6124d9SMasami Hiramatsu tev->nargs = 0; 24514de189feSMasami Hiramatsu } 24524de189feSMasami Hiramatsu 24539aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node { 24549aaf5a5fSMasami Hiramatsu struct list_head list; 245522a66551SYang Jihong u64 start; 245622a66551SYang Jihong u64 end; 24579aaf5a5fSMasami Hiramatsu char *symbol; 24589aaf5a5fSMasami Hiramatsu }; 24599aaf5a5fSMasami Hiramatsu 24609aaf5a5fSMasami Hiramatsu static void kprobe_blacklist__delete(struct list_head *blacklist) 24619aaf5a5fSMasami Hiramatsu { 24629aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node *node; 24639aaf5a5fSMasami Hiramatsu 24649aaf5a5fSMasami Hiramatsu while (!list_empty(blacklist)) { 24659aaf5a5fSMasami Hiramatsu node = list_first_entry(blacklist, 24669aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node, list); 2467e56fbc9dSArnaldo Carvalho de Melo list_del_init(&node->list); 2468d8f9da24SArnaldo Carvalho de Melo zfree(&node->symbol); 24699aaf5a5fSMasami Hiramatsu free(node); 24709aaf5a5fSMasami Hiramatsu } 24719aaf5a5fSMasami Hiramatsu } 24729aaf5a5fSMasami Hiramatsu 24739aaf5a5fSMasami Hiramatsu static int kprobe_blacklist__load(struct list_head *blacklist) 24749aaf5a5fSMasami Hiramatsu { 24759aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node *node; 24764605eab3SJiri Olsa const char *__debugfs = debugfs__mountpoint(); 24779aaf5a5fSMasami Hiramatsu char buf[PATH_MAX], *p; 24789aaf5a5fSMasami Hiramatsu FILE *fp; 24799aaf5a5fSMasami Hiramatsu int ret; 24809aaf5a5fSMasami Hiramatsu 24819aaf5a5fSMasami Hiramatsu if (__debugfs == NULL) 24829aaf5a5fSMasami Hiramatsu return -ENOTSUP; 24839aaf5a5fSMasami Hiramatsu 24849aaf5a5fSMasami Hiramatsu ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); 24859aaf5a5fSMasami Hiramatsu if (ret < 0) 24869aaf5a5fSMasami Hiramatsu return ret; 24879aaf5a5fSMasami Hiramatsu 24889aaf5a5fSMasami Hiramatsu fp = fopen(buf, "r"); 24899aaf5a5fSMasami Hiramatsu if (!fp) 24909aaf5a5fSMasami Hiramatsu return -errno; 24919aaf5a5fSMasami Hiramatsu 24929aaf5a5fSMasami Hiramatsu ret = 0; 24939aaf5a5fSMasami Hiramatsu while (fgets(buf, PATH_MAX, fp)) { 24949aaf5a5fSMasami Hiramatsu node = zalloc(sizeof(*node)); 24959aaf5a5fSMasami Hiramatsu if (!node) { 24969aaf5a5fSMasami Hiramatsu ret = -ENOMEM; 24979aaf5a5fSMasami Hiramatsu break; 24989aaf5a5fSMasami Hiramatsu } 24999aaf5a5fSMasami Hiramatsu INIT_LIST_HEAD(&node->list); 25009aaf5a5fSMasami Hiramatsu list_add_tail(&node->list, blacklist); 250122a66551SYang Jihong if (sscanf(buf, "0x%" PRIx64 "-0x%" PRIx64, &node->start, &node->end) != 2) { 25029aaf5a5fSMasami Hiramatsu ret = -EINVAL; 25039aaf5a5fSMasami Hiramatsu break; 25049aaf5a5fSMasami Hiramatsu } 25059aaf5a5fSMasami Hiramatsu p = strchr(buf, '\t'); 25069aaf5a5fSMasami Hiramatsu if (p) { 25079aaf5a5fSMasami Hiramatsu p++; 25089aaf5a5fSMasami Hiramatsu if (p[strlen(p) - 1] == '\n') 25099aaf5a5fSMasami Hiramatsu p[strlen(p) - 1] = '\0'; 25109aaf5a5fSMasami Hiramatsu } else 25119aaf5a5fSMasami Hiramatsu p = (char *)"unknown"; 25129aaf5a5fSMasami Hiramatsu node->symbol = strdup(p); 25139aaf5a5fSMasami Hiramatsu if (!node->symbol) { 25149aaf5a5fSMasami Hiramatsu ret = -ENOMEM; 25159aaf5a5fSMasami Hiramatsu break; 25169aaf5a5fSMasami Hiramatsu } 251722a66551SYang Jihong pr_debug2("Blacklist: 0x%" PRIx64 "-0x%" PRIx64 ", %s\n", 25189aaf5a5fSMasami Hiramatsu node->start, node->end, node->symbol); 25199aaf5a5fSMasami Hiramatsu ret++; 25209aaf5a5fSMasami Hiramatsu } 25219aaf5a5fSMasami Hiramatsu if (ret < 0) 25229aaf5a5fSMasami Hiramatsu kprobe_blacklist__delete(blacklist); 25239aaf5a5fSMasami Hiramatsu fclose(fp); 25249aaf5a5fSMasami Hiramatsu 25259aaf5a5fSMasami Hiramatsu return ret; 25269aaf5a5fSMasami Hiramatsu } 25279aaf5a5fSMasami Hiramatsu 25289aaf5a5fSMasami Hiramatsu static struct kprobe_blacklist_node * 252922a66551SYang Jihong kprobe_blacklist__find_by_address(struct list_head *blacklist, u64 address) 25309aaf5a5fSMasami Hiramatsu { 25319aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node *node; 25329aaf5a5fSMasami Hiramatsu 25339aaf5a5fSMasami Hiramatsu list_for_each_entry(node, blacklist, list) { 25342c29461eSLi Bin if (node->start <= address && address < node->end) 25359aaf5a5fSMasami Hiramatsu return node; 25369aaf5a5fSMasami Hiramatsu } 25379aaf5a5fSMasami Hiramatsu 25389aaf5a5fSMasami Hiramatsu return NULL; 25399aaf5a5fSMasami Hiramatsu } 25409aaf5a5fSMasami Hiramatsu 2541b031220dSMasami Hiramatsu static LIST_HEAD(kprobe_blacklist); 2542b031220dSMasami Hiramatsu 2543b031220dSMasami Hiramatsu static void kprobe_blacklist__init(void) 2544b031220dSMasami Hiramatsu { 2545b031220dSMasami Hiramatsu if (!list_empty(&kprobe_blacklist)) 2546b031220dSMasami Hiramatsu return; 2547b031220dSMasami Hiramatsu 2548b031220dSMasami Hiramatsu if (kprobe_blacklist__load(&kprobe_blacklist) < 0) 2549b031220dSMasami Hiramatsu pr_debug("No kprobe blacklist support, ignored\n"); 2550b031220dSMasami Hiramatsu } 2551b031220dSMasami Hiramatsu 2552b031220dSMasami Hiramatsu static void kprobe_blacklist__release(void) 2553b031220dSMasami Hiramatsu { 2554b031220dSMasami Hiramatsu kprobe_blacklist__delete(&kprobe_blacklist); 2555b031220dSMasami Hiramatsu } 2556b031220dSMasami Hiramatsu 255722a66551SYang Jihong static bool kprobe_blacklist__listed(u64 address) 2558b031220dSMasami Hiramatsu { 2559b031220dSMasami Hiramatsu return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); 2560b031220dSMasami Hiramatsu } 2561b031220dSMasami Hiramatsu 2562d350bd57SMasami Hiramatsu static int perf_probe_event__sprintf(const char *group, const char *event, 2563d350bd57SMasami Hiramatsu struct perf_probe_event *pev, 2564ba7ecb02SMasami Hiramatsu const char *module, 2565ba7ecb02SMasami Hiramatsu struct strbuf *result) 2566278498d4SMasami Hiramatsu { 2567bf4d5f25SMasami Hiramatsu int i, ret; 2568909b0360SMasami Hiramatsu char *buf; 2569909b0360SMasami Hiramatsu 2570909b0360SMasami Hiramatsu if (asprintf(&buf, "%s:%s", group, event) < 0) 2571909b0360SMasami Hiramatsu return -errno; 2572bf4d5f25SMasami Hiramatsu ret = strbuf_addf(result, " %-20s (on ", buf); 2573909b0360SMasami Hiramatsu free(buf); 2574bf4d5f25SMasami Hiramatsu if (ret) 2575bf4d5f25SMasami Hiramatsu return ret; 2576278498d4SMasami Hiramatsu 25774235b045SMasami Hiramatsu /* Synthesize only event probe point */ 2578909b0360SMasami Hiramatsu buf = synthesize_perf_probe_point(&pev->point); 2579909b0360SMasami Hiramatsu if (!buf) 2580909b0360SMasami Hiramatsu return -ENOMEM; 2581bf4d5f25SMasami Hiramatsu ret = strbuf_addstr(result, buf); 2582909b0360SMasami Hiramatsu free(buf); 25834235b045SMasami Hiramatsu 2584bf4d5f25SMasami Hiramatsu if (!ret && module) 2585bf4d5f25SMasami Hiramatsu ret = strbuf_addf(result, " in %s", module); 2586278498d4SMasami Hiramatsu 2587bf4d5f25SMasami Hiramatsu if (!ret && pev->nargs > 0) { 2588bf4d5f25SMasami Hiramatsu ret = strbuf_add(result, " with", 5); 2589bf4d5f25SMasami Hiramatsu for (i = 0; !ret && i < pev->nargs; i++) { 2590909b0360SMasami Hiramatsu buf = synthesize_perf_probe_arg(&pev->args[i]); 2591909b0360SMasami Hiramatsu if (!buf) 2592909b0360SMasami Hiramatsu return -ENOMEM; 2593bf4d5f25SMasami Hiramatsu ret = strbuf_addf(result, " %s", buf); 2594909b0360SMasami Hiramatsu free(buf); 25957df2f329SMasami Hiramatsu } 2596278498d4SMasami Hiramatsu } 2597bf4d5f25SMasami Hiramatsu if (!ret) 2598bf4d5f25SMasami Hiramatsu ret = strbuf_addch(result, ')'); 2599909b0360SMasami Hiramatsu 2600bf4d5f25SMasami Hiramatsu return ret; 2601278498d4SMasami Hiramatsu } 2602278498d4SMasami Hiramatsu 2603ba7ecb02SMasami Hiramatsu /* Show an event */ 2604b02137ccSNamhyung Kim int show_perf_probe_event(const char *group, const char *event, 2605d350bd57SMasami Hiramatsu struct perf_probe_event *pev, 2606ba7ecb02SMasami Hiramatsu const char *module, bool use_stdout) 2607ba7ecb02SMasami Hiramatsu { 2608ba7ecb02SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 2609ba7ecb02SMasami Hiramatsu int ret; 2610ba7ecb02SMasami Hiramatsu 2611d350bd57SMasami Hiramatsu ret = perf_probe_event__sprintf(group, event, pev, module, &buf); 2612ba7ecb02SMasami Hiramatsu if (ret >= 0) { 2613ba7ecb02SMasami Hiramatsu if (use_stdout) 2614ba7ecb02SMasami Hiramatsu printf("%s\n", buf.buf); 2615ba7ecb02SMasami Hiramatsu else 2616ba7ecb02SMasami Hiramatsu pr_info("%s\n", buf.buf); 2617ba7ecb02SMasami Hiramatsu } 2618ba7ecb02SMasami Hiramatsu strbuf_release(&buf); 2619ba7ecb02SMasami Hiramatsu 2620ba7ecb02SMasami Hiramatsu return ret; 2621ba7ecb02SMasami Hiramatsu } 2622ba7ecb02SMasami Hiramatsu 2623b6a89643SMasami Hiramatsu static bool filter_probe_trace_event(struct probe_trace_event *tev, 2624b6a89643SMasami Hiramatsu struct strfilter *filter) 2625b6a89643SMasami Hiramatsu { 2626b6a89643SMasami Hiramatsu char tmp[128]; 2627b6a89643SMasami Hiramatsu 2628b6a89643SMasami Hiramatsu /* At first, check the event name itself */ 2629b6a89643SMasami Hiramatsu if (strfilter__compare(filter, tev->event)) 2630b6a89643SMasami Hiramatsu return true; 2631b6a89643SMasami Hiramatsu 2632b6a89643SMasami Hiramatsu /* Next, check the combination of name and group */ 2633b6a89643SMasami Hiramatsu if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) 2634b6a89643SMasami Hiramatsu return false; 2635b6a89643SMasami Hiramatsu return strfilter__compare(filter, tmp); 2636b6a89643SMasami Hiramatsu } 2637b6a89643SMasami Hiramatsu 2638b6a89643SMasami Hiramatsu static int __show_perf_probe_events(int fd, bool is_kprobe, 2639b6a89643SMasami Hiramatsu struct strfilter *filter) 26404de189feSMasami Hiramatsu { 2641225466f1SSrikar Dronamraju int ret = 0; 26420e60836bSSrikar Dronamraju struct probe_trace_event tev; 26434235b045SMasami Hiramatsu struct perf_probe_event pev; 26444de189feSMasami Hiramatsu struct strlist *rawlist; 26454de189feSMasami Hiramatsu struct str_node *ent; 26464de189feSMasami Hiramatsu 26474235b045SMasami Hiramatsu memset(&tev, 0, sizeof(tev)); 26484235b045SMasami Hiramatsu memset(&pev, 0, sizeof(pev)); 264972041334SMasami Hiramatsu 265092f6c72eSMasami Hiramatsu rawlist = probe_file__get_rawlist(fd); 2651146a1439SMasami Hiramatsu if (!rawlist) 26526eb08660SMasami Hiramatsu return -ENOMEM; 26534de189feSMasami Hiramatsu 2654602a1f4dSArnaldo Carvalho de Melo strlist__for_each_entry(ent, rawlist) { 26550e60836bSSrikar Dronamraju ret = parse_probe_trace_command(ent->s, &tev); 2656146a1439SMasami Hiramatsu if (ret >= 0) { 2657b6a89643SMasami Hiramatsu if (!filter_probe_trace_event(&tev, filter)) 2658b6a89643SMasami Hiramatsu goto next; 2659225466f1SSrikar Dronamraju ret = convert_to_perf_probe_event(&tev, &pev, 2660225466f1SSrikar Dronamraju is_kprobe); 2661ba7ecb02SMasami Hiramatsu if (ret < 0) 2662ba7ecb02SMasami Hiramatsu goto next; 2663d350bd57SMasami Hiramatsu ret = show_perf_probe_event(pev.group, pev.event, 2664d350bd57SMasami Hiramatsu &pev, tev.point.module, 2665ba7ecb02SMasami Hiramatsu true); 2666146a1439SMasami Hiramatsu } 2667b6a89643SMasami Hiramatsu next: 26684235b045SMasami Hiramatsu clear_perf_probe_event(&pev); 26690e60836bSSrikar Dronamraju clear_probe_trace_event(&tev); 2670146a1439SMasami Hiramatsu if (ret < 0) 2671146a1439SMasami Hiramatsu break; 26724de189feSMasami Hiramatsu } 26734de189feSMasami Hiramatsu strlist__delete(rawlist); 26747737af01SMasami Hiramatsu /* Cleanup cached debuginfo if needed */ 26757737af01SMasami Hiramatsu debuginfo_cache__exit(); 2676146a1439SMasami Hiramatsu 2677146a1439SMasami Hiramatsu return ret; 26784de189feSMasami Hiramatsu } 26794de189feSMasami Hiramatsu 2680225466f1SSrikar Dronamraju /* List up current perf-probe events */ 2681b6a89643SMasami Hiramatsu int show_perf_probe_events(struct strfilter *filter) 2682225466f1SSrikar Dronamraju { 26835e45187cSMasami Hiramatsu int kp_fd, up_fd, ret; 2684225466f1SSrikar Dronamraju 2685225466f1SSrikar Dronamraju setup_pager(); 2686225466f1SSrikar Dronamraju 26871f3736c9SMasami Hiramatsu if (probe_conf.cache) 26881f3736c9SMasami Hiramatsu return probe_cache__show_all_caches(filter); 26891f3736c9SMasami Hiramatsu 26909bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(false); 2691225466f1SSrikar Dronamraju if (ret < 0) 2692225466f1SSrikar Dronamraju return ret; 2693225466f1SSrikar Dronamraju 269492f6c72eSMasami Hiramatsu ret = probe_file__open_both(&kp_fd, &up_fd, 0); 26955e45187cSMasami Hiramatsu if (ret < 0) 2696225466f1SSrikar Dronamraju return ret; 2697225466f1SSrikar Dronamraju 269892f6c72eSMasami Hiramatsu if (kp_fd >= 0) 269992f6c72eSMasami Hiramatsu ret = __show_perf_probe_events(kp_fd, true, filter); 270092f6c72eSMasami Hiramatsu if (up_fd >= 0 && ret >= 0) 270192f6c72eSMasami Hiramatsu ret = __show_perf_probe_events(up_fd, false, filter); 270292f6c72eSMasami Hiramatsu if (kp_fd > 0) 270392f6c72eSMasami Hiramatsu close(kp_fd); 270492f6c72eSMasami Hiramatsu if (up_fd > 0) 270592f6c72eSMasami Hiramatsu close(up_fd); 27069bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 2707b498ce1fSMasami Hiramatsu 2708146a1439SMasami Hiramatsu return ret; 2709f4d7da49SMasami Hiramatsu } 271050656eecSMasami Hiramatsu 2711146a1439SMasami Hiramatsu static int get_new_event_name(char *buf, size_t len, const char *base, 2712e63c625aSMasami Hiramatsu struct strlist *namelist, bool ret_event, 2713e63c625aSMasami Hiramatsu bool allow_suffix) 2714b498ce1fSMasami Hiramatsu { 2715b498ce1fSMasami Hiramatsu int i, ret; 2716663b1151SMasami Hiramatsu char *p, *nbase; 271717f88fcdSMasami Hiramatsu 27183099c026SNaveen N. Rao if (*base == '.') 27193099c026SNaveen N. Rao base++; 2720663b1151SMasami Hiramatsu nbase = strdup(base); 2721663b1151SMasami Hiramatsu if (!nbase) 2722663b1151SMasami Hiramatsu return -ENOMEM; 27233099c026SNaveen N. Rao 2724a3110cd9SMasami Hiramatsu /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */ 2725a3110cd9SMasami Hiramatsu p = strpbrk(nbase, ".@"); 2726663b1151SMasami Hiramatsu if (p && p != nbase) 2727663b1151SMasami Hiramatsu *p = '\0'; 2728663b1151SMasami Hiramatsu 2729663b1151SMasami Hiramatsu /* Try no suffix number */ 2730e63c625aSMasami Hiramatsu ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : ""); 2731146a1439SMasami Hiramatsu if (ret < 0) { 27325f03cba4SMasami Hiramatsu pr_debug("snprintf() failed: %d\n", ret); 2733663b1151SMasami Hiramatsu goto out; 2734146a1439SMasami Hiramatsu } 273517f88fcdSMasami Hiramatsu if (!strlist__has_entry(namelist, buf)) 2736663b1151SMasami Hiramatsu goto out; 273717f88fcdSMasami Hiramatsu 2738d761b08bSMasami Hiramatsu if (!allow_suffix) { 273903e01f56SWang Nan pr_warning("Error: event \"%s\" already exists.\n" 274003e01f56SWang Nan " Hint: Remove existing event by 'perf probe -d'\n" 274103e01f56SWang Nan " or force duplicates by 'perf probe -f'\n" 274203e01f56SWang Nan " or set 'force=yes' in BPF source.\n", 274303e01f56SWang Nan buf); 2744663b1151SMasami Hiramatsu ret = -EEXIST; 2745663b1151SMasami Hiramatsu goto out; 2746d761b08bSMasami Hiramatsu } 2747d761b08bSMasami Hiramatsu 274817f88fcdSMasami Hiramatsu /* Try to add suffix */ 274917f88fcdSMasami Hiramatsu for (i = 1; i < MAX_EVENT_INDEX; i++) { 2750663b1151SMasami Hiramatsu ret = e_snprintf(buf, len, "%s_%d", nbase, i); 2751146a1439SMasami Hiramatsu if (ret < 0) { 27525f03cba4SMasami Hiramatsu pr_debug("snprintf() failed: %d\n", ret); 2753663b1151SMasami Hiramatsu goto out; 2754146a1439SMasami Hiramatsu } 2755b498ce1fSMasami Hiramatsu if (!strlist__has_entry(namelist, buf)) 2756b498ce1fSMasami Hiramatsu break; 2757b498ce1fSMasami Hiramatsu } 2758146a1439SMasami Hiramatsu if (i == MAX_EVENT_INDEX) { 2759146a1439SMasami Hiramatsu pr_warning("Too many events are on the same function.\n"); 2760146a1439SMasami Hiramatsu ret = -ERANGE; 2761b498ce1fSMasami Hiramatsu } 2762b498ce1fSMasami Hiramatsu 2763663b1151SMasami Hiramatsu out: 2764663b1151SMasami Hiramatsu free(nbase); 27659f5c6d87SMasami Hiramatsu 27669f5c6d87SMasami Hiramatsu /* Final validation */ 27679f5c6d87SMasami Hiramatsu if (ret >= 0 && !is_c_func_name(buf)) { 27689f5c6d87SMasami Hiramatsu pr_warning("Internal error: \"%s\" is an invalid event name.\n", 27699f5c6d87SMasami Hiramatsu buf); 27709f5c6d87SMasami Hiramatsu ret = -EINVAL; 27719f5c6d87SMasami Hiramatsu } 27729f5c6d87SMasami Hiramatsu 2773146a1439SMasami Hiramatsu return ret; 2774146a1439SMasami Hiramatsu } 2775146a1439SMasami Hiramatsu 277679702f61SMasami Hiramatsu /* Warn if the current kernel's uprobe implementation is old */ 277779702f61SMasami Hiramatsu static void warn_uprobe_event_compat(struct probe_trace_event *tev) 277879702f61SMasami Hiramatsu { 277979702f61SMasami Hiramatsu int i; 278079702f61SMasami Hiramatsu char *buf = synthesize_probe_trace_command(tev); 27815a5e3d3cSRavi Bangoria struct probe_trace_point *tp = &tev->point; 27825a5e3d3cSRavi Bangoria 27835a5e3d3cSRavi Bangoria if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) { 27845a5e3d3cSRavi Bangoria pr_warning("A semaphore is associated with %s:%s and " 27855a5e3d3cSRavi Bangoria "seems your kernel doesn't support it.\n", 27865a5e3d3cSRavi Bangoria tev->group, tev->event); 27875a5e3d3cSRavi Bangoria } 278879702f61SMasami Hiramatsu 278979702f61SMasami Hiramatsu /* Old uprobe event doesn't support memory dereference */ 279079702f61SMasami Hiramatsu if (!tev->uprobes || tev->nargs == 0 || !buf) 279179702f61SMasami Hiramatsu goto out; 279279702f61SMasami Hiramatsu 279379702f61SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) 279479702f61SMasami Hiramatsu if (strglobmatch(tev->args[i].value, "[$@+-]*")) { 279579702f61SMasami Hiramatsu pr_warning("Please upgrade your kernel to at least " 279679702f61SMasami Hiramatsu "3.14 to have access to feature %s\n", 279779702f61SMasami Hiramatsu tev->args[i].value); 279879702f61SMasami Hiramatsu break; 279979702f61SMasami Hiramatsu } 280079702f61SMasami Hiramatsu out: 280179702f61SMasami Hiramatsu free(buf); 280279702f61SMasami Hiramatsu } 280379702f61SMasami Hiramatsu 2804a3c9de62SMasami Hiramatsu /* Set new name from original perf_probe_event and namelist */ 2805a3c9de62SMasami Hiramatsu static int probe_trace_event__set_name(struct probe_trace_event *tev, 2806a3c9de62SMasami Hiramatsu struct perf_probe_event *pev, 2807a3c9de62SMasami Hiramatsu struct strlist *namelist, 2808a3c9de62SMasami Hiramatsu bool allow_suffix) 2809a3c9de62SMasami Hiramatsu { 2810a3c9de62SMasami Hiramatsu const char *event, *group; 2811a3c9de62SMasami Hiramatsu char buf[64]; 2812a3c9de62SMasami Hiramatsu int ret; 2813a3c9de62SMasami Hiramatsu 2814bc062230SMasami Hiramatsu /* If probe_event or trace_event already have the name, reuse it */ 281542bba263SMasami Hiramatsu if (pev->event && !pev->sdt) 2816a3c9de62SMasami Hiramatsu event = pev->event; 2817bc062230SMasami Hiramatsu else if (tev->event) 2818bc062230SMasami Hiramatsu event = tev->event; 2819bc062230SMasami Hiramatsu else { 2820bc062230SMasami Hiramatsu /* Or generate new one from probe point */ 2821da15bd9dSWang Nan if (pev->point.function && 2822da15bd9dSWang Nan (strncmp(pev->point.function, "0x", 2) != 0) && 2823da15bd9dSWang Nan !strisglob(pev->point.function)) 2824a3c9de62SMasami Hiramatsu event = pev->point.function; 2825a3c9de62SMasami Hiramatsu else 2826a3c9de62SMasami Hiramatsu event = tev->point.realname; 2827bc062230SMasami Hiramatsu } 282842bba263SMasami Hiramatsu if (pev->group && !pev->sdt) 2829a3c9de62SMasami Hiramatsu group = pev->group; 2830bc062230SMasami Hiramatsu else if (tev->group) 2831bc062230SMasami Hiramatsu group = tev->group; 2832a3c9de62SMasami Hiramatsu else 2833a3c9de62SMasami Hiramatsu group = PERFPROBE_GROUP; 2834a3c9de62SMasami Hiramatsu 2835a3c9de62SMasami Hiramatsu /* Get an unused new event name */ 2836e63c625aSMasami Hiramatsu ret = get_new_event_name(buf, 64, event, namelist, 2837e63c625aSMasami Hiramatsu tev->point.retprobe, allow_suffix); 2838a3c9de62SMasami Hiramatsu if (ret < 0) 2839a3c9de62SMasami Hiramatsu return ret; 2840a3c9de62SMasami Hiramatsu 2841a3c9de62SMasami Hiramatsu event = buf; 2842a3c9de62SMasami Hiramatsu 2843a3c9de62SMasami Hiramatsu tev->event = strdup(event); 2844a3c9de62SMasami Hiramatsu tev->group = strdup(group); 2845a3c9de62SMasami Hiramatsu if (tev->event == NULL || tev->group == NULL) 2846a3c9de62SMasami Hiramatsu return -ENOMEM; 2847a3c9de62SMasami Hiramatsu 284872363540SMasami Hiramatsu /* 284972363540SMasami Hiramatsu * Add new event name to namelist if multiprobe event is NOT 285072363540SMasami Hiramatsu * supported, since we have to use new event name for following 285172363540SMasami Hiramatsu * probes in that case. 285272363540SMasami Hiramatsu */ 285372363540SMasami Hiramatsu if (!multiprobe_event_is_supported()) 2854a3c9de62SMasami Hiramatsu strlist__add(namelist, event); 2855a3c9de62SMasami Hiramatsu return 0; 2856a3c9de62SMasami Hiramatsu } 2857a3c9de62SMasami Hiramatsu 28581de7b8bfSMasami Hiramatsu static int __open_probe_file_and_namelist(bool uprobe, 28591de7b8bfSMasami Hiramatsu struct strlist **namelist) 286050656eecSMasami Hiramatsu { 28611de7b8bfSMasami Hiramatsu int fd; 286250656eecSMasami Hiramatsu 28631de7b8bfSMasami Hiramatsu fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); 286492f6c72eSMasami Hiramatsu if (fd < 0) 2865146a1439SMasami Hiramatsu return fd; 28665e45187cSMasami Hiramatsu 2867b498ce1fSMasami Hiramatsu /* Get current event names */ 28681de7b8bfSMasami Hiramatsu *namelist = probe_file__get_namelist(fd); 28691de7b8bfSMasami Hiramatsu if (!(*namelist)) { 2870146a1439SMasami Hiramatsu pr_debug("Failed to get current event list.\n"); 28711de7b8bfSMasami Hiramatsu close(fd); 28721de7b8bfSMasami Hiramatsu return -ENOMEM; 2873146a1439SMasami Hiramatsu } 28741de7b8bfSMasami Hiramatsu return fd; 28751de7b8bfSMasami Hiramatsu } 28761de7b8bfSMasami Hiramatsu 28771de7b8bfSMasami Hiramatsu static int __add_probe_trace_events(struct perf_probe_event *pev, 28781de7b8bfSMasami Hiramatsu struct probe_trace_event *tevs, 28791de7b8bfSMasami Hiramatsu int ntevs, bool allow_suffix) 28801de7b8bfSMasami Hiramatsu { 28811de7b8bfSMasami Hiramatsu int i, fd[2] = {-1, -1}, up, ret; 28821de7b8bfSMasami Hiramatsu struct probe_trace_event *tev = NULL; 28831de7b8bfSMasami Hiramatsu struct probe_cache *cache = NULL; 28841de7b8bfSMasami Hiramatsu struct strlist *namelist[2] = {NULL, NULL}; 2885544abd44SKrister Johansen struct nscookie nsc; 28861de7b8bfSMasami Hiramatsu 28871de7b8bfSMasami Hiramatsu up = pev->uprobes ? 1 : 0; 28881de7b8bfSMasami Hiramatsu fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); 28891de7b8bfSMasami Hiramatsu if (fd[up] < 0) 28901de7b8bfSMasami Hiramatsu return fd[up]; 289150656eecSMasami Hiramatsu 2892146a1439SMasami Hiramatsu ret = 0; 289302b95dadSMasami Hiramatsu for (i = 0; i < ntevs; i++) { 28944235b045SMasami Hiramatsu tev = &tevs[i]; 28951de7b8bfSMasami Hiramatsu up = tev->uprobes ? 1 : 0; 28961de7b8bfSMasami Hiramatsu if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ 28971de7b8bfSMasami Hiramatsu fd[up] = __open_probe_file_and_namelist(up, 28981de7b8bfSMasami Hiramatsu &namelist[up]); 28991de7b8bfSMasami Hiramatsu if (fd[up] < 0) 29001de7b8bfSMasami Hiramatsu goto close_out; 29011de7b8bfSMasami Hiramatsu } 2902b031220dSMasami Hiramatsu /* Skip if the symbol is out of .text or blacklisted */ 2903bc062230SMasami Hiramatsu if (!tev->point.symbol && !pev->uprobes) 29045a51fcd1SMasami Hiramatsu continue; 29059aaf5a5fSMasami Hiramatsu 2906a3c9de62SMasami Hiramatsu /* Set new name for tev (and update namelist) */ 29071de7b8bfSMasami Hiramatsu ret = probe_trace_event__set_name(tev, pev, namelist[up], 2908a3c9de62SMasami Hiramatsu allow_suffix); 2909146a1439SMasami Hiramatsu if (ret < 0) 2910146a1439SMasami Hiramatsu break; 29114235b045SMasami Hiramatsu 2912544abd44SKrister Johansen nsinfo__mountns_enter(pev->nsi, &nsc); 29131de7b8bfSMasami Hiramatsu ret = probe_file__add_event(fd[up], tev); 2914544abd44SKrister Johansen nsinfo__mountns_exit(&nsc); 2915146a1439SMasami Hiramatsu if (ret < 0) 2916146a1439SMasami Hiramatsu break; 29174235b045SMasami Hiramatsu 2918d761b08bSMasami Hiramatsu /* 2919d761b08bSMasami Hiramatsu * Probes after the first probe which comes from same 2920d761b08bSMasami Hiramatsu * user input are always allowed to add suffix, because 2921d761b08bSMasami Hiramatsu * there might be several addresses corresponding to 2922d761b08bSMasami Hiramatsu * one code line. 2923d761b08bSMasami Hiramatsu */ 2924d761b08bSMasami Hiramatsu allow_suffix = true; 292550656eecSMasami Hiramatsu } 292679702f61SMasami Hiramatsu if (ret == -EINVAL && pev->uprobes) 292779702f61SMasami Hiramatsu warn_uprobe_event_compat(tev); 29282fd457a3SMasami Hiramatsu if (ret == 0 && probe_conf.cache) { 2929f045b8c4SKrister Johansen cache = probe_cache__new(pev->target, pev->nsi); 29302fd457a3SMasami Hiramatsu if (!cache || 29312fd457a3SMasami Hiramatsu probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || 29322fd457a3SMasami Hiramatsu probe_cache__commit(cache) < 0) 29332fd457a3SMasami Hiramatsu pr_warning("Failed to add event to probe cache\n"); 29342fd457a3SMasami Hiramatsu probe_cache__delete(cache); 29352fd457a3SMasami Hiramatsu } 2936146a1439SMasami Hiramatsu 2937ae2cb1acSMasami Hiramatsu close_out: 29381de7b8bfSMasami Hiramatsu for (up = 0; up < 2; up++) { 29391de7b8bfSMasami Hiramatsu strlist__delete(namelist[up]); 29401de7b8bfSMasami Hiramatsu if (fd[up] >= 0) 29411de7b8bfSMasami Hiramatsu close(fd[up]); 29421de7b8bfSMasami Hiramatsu } 2943146a1439SMasami Hiramatsu return ret; 294450656eecSMasami Hiramatsu } 2945fa28244dSMasami Hiramatsu 29466bb536ccSWang Nan static int find_probe_functions(struct map *map, char *name, 29476bb536ccSWang Nan struct symbol **syms) 2948eb948e50SMasami Hiramatsu { 2949564c62a4SNamhyung Kim int found = 0; 29500a3873a8SArnaldo Carvalho de Melo struct symbol *sym; 29514c859351SMasami Hiramatsu struct rb_node *tmp; 29524b3a2716SMasami Hiramatsu const char *norm, *ver; 29534b3a2716SMasami Hiramatsu char *buf = NULL; 2954c588d158SMasami Hiramatsu bool cut_version = true; 2955564c62a4SNamhyung Kim 2956be39db9fSArnaldo Carvalho de Melo if (map__load(map) < 0) 2957f4f1c429SMasami Hiramatsu return -EACCES; /* Possible permission error to load symbols */ 295875e4a2a6SWang Nan 2959c588d158SMasami Hiramatsu /* If user gives a version, don't cut off the version from symbols */ 2960c588d158SMasami Hiramatsu if (strchr(name, '@')) 2961c588d158SMasami Hiramatsu cut_version = false; 2962c588d158SMasami Hiramatsu 29634c859351SMasami Hiramatsu map__for_each_symbol(map, sym, tmp) { 29644b3a2716SMasami Hiramatsu norm = arch__normalize_symbol_name(sym->name); 29654b3a2716SMasami Hiramatsu if (!norm) 29664b3a2716SMasami Hiramatsu continue; 29674b3a2716SMasami Hiramatsu 2968c588d158SMasami Hiramatsu if (cut_version) { 29694b3a2716SMasami Hiramatsu /* We don't care about default symbol or not */ 29704b3a2716SMasami Hiramatsu ver = strchr(norm, '@'); 29714b3a2716SMasami Hiramatsu if (ver) { 29724b3a2716SMasami Hiramatsu buf = strndup(norm, ver - norm); 29734b3a2716SMasami Hiramatsu if (!buf) 29744b3a2716SMasami Hiramatsu return -ENOMEM; 29754b3a2716SMasami Hiramatsu norm = buf; 29764b3a2716SMasami Hiramatsu } 2977c588d158SMasami Hiramatsu } 2978c588d158SMasami Hiramatsu 29794b3a2716SMasami Hiramatsu if (strglobmatch(norm, name)) { 2980564c62a4SNamhyung Kim found++; 29816bb536ccSWang Nan if (syms && found < probe_conf.max_probes) 29826bb536ccSWang Nan syms[found - 1] = sym; 29836bb536ccSWang Nan } 29844b3a2716SMasami Hiramatsu if (buf) 29854b3a2716SMasami Hiramatsu zfree(&buf); 2986eb948e50SMasami Hiramatsu } 2987564c62a4SNamhyung Kim 2988564c62a4SNamhyung Kim return found; 2989eb948e50SMasami Hiramatsu } 2990eb948e50SMasami Hiramatsu 29917b6ff0bdSNaveen N. Rao void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, 29927b6ff0bdSNaveen N. Rao struct probe_trace_event *tev __maybe_unused, 29930b3c2264SNaveen N. Rao struct map *map __maybe_unused, 29940b3c2264SNaveen N. Rao struct symbol *sym __maybe_unused) { } 29957b6ff0bdSNaveen N. Rao 2996f4f1c429SMasami Hiramatsu 2997f4f1c429SMasami Hiramatsu static void pr_kallsyms_access_error(void) 2998f4f1c429SMasami Hiramatsu { 2999f4f1c429SMasami Hiramatsu pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n" 3000f4f1c429SMasami Hiramatsu "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n" 3001f4f1c429SMasami Hiramatsu "kernel symbol addresses even if you are a superuser. Please change\n" 3002f4f1c429SMasami Hiramatsu "it to '1'. If kptr_restrict is '1', the superuser can read the\n" 3003f4f1c429SMasami Hiramatsu "symbol addresses.\n" 3004f4f1c429SMasami Hiramatsu "In that case, please run this command again with sudo.\n"); 3005f4f1c429SMasami Hiramatsu } 3006f4f1c429SMasami Hiramatsu 3007eb948e50SMasami Hiramatsu /* 3008eb948e50SMasami Hiramatsu * Find probe function addresses from map. 3009eb948e50SMasami Hiramatsu * Return an error or the number of found probe_trace_event 3010eb948e50SMasami Hiramatsu */ 3011eb948e50SMasami Hiramatsu static int find_probe_trace_events_from_map(struct perf_probe_event *pev, 3012ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs) 3013eb948e50SMasami Hiramatsu { 3014eb948e50SMasami Hiramatsu struct map *map = NULL; 3015eb948e50SMasami Hiramatsu struct ref_reloc_sym *reloc_sym = NULL; 3016eb948e50SMasami Hiramatsu struct symbol *sym; 30176bb536ccSWang Nan struct symbol **syms = NULL; 3018eb948e50SMasami Hiramatsu struct probe_trace_event *tev; 3019eb948e50SMasami Hiramatsu struct perf_probe_point *pp = &pev->point; 3020eb948e50SMasami Hiramatsu struct probe_trace_point *tp; 3021564c62a4SNamhyung Kim int num_matched_functions; 3022b031220dSMasami Hiramatsu int ret, i, j, skipped = 0; 3023c61fb959SRavi Bangoria char *mod_name; 3024eb948e50SMasami Hiramatsu 3025544abd44SKrister Johansen map = get_target_map(pev->target, pev->nsi, pev->uprobes); 3026eb948e50SMasami Hiramatsu if (!map) { 3027eb948e50SMasami Hiramatsu ret = -EINVAL; 3028eb948e50SMasami Hiramatsu goto out; 3029eb948e50SMasami Hiramatsu } 3030eb948e50SMasami Hiramatsu 30316bb536ccSWang Nan syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); 30326bb536ccSWang Nan if (!syms) { 30336bb536ccSWang Nan ret = -ENOMEM; 30346bb536ccSWang Nan goto out; 30356bb536ccSWang Nan } 30366bb536ccSWang Nan 3037eb948e50SMasami Hiramatsu /* 3038eb948e50SMasami Hiramatsu * Load matched symbols: Since the different local symbols may have 3039eb948e50SMasami Hiramatsu * same name but different addresses, this lists all the symbols. 3040eb948e50SMasami Hiramatsu */ 30416bb536ccSWang Nan num_matched_functions = find_probe_functions(map, pp->function, syms); 30424b3a2716SMasami Hiramatsu if (num_matched_functions <= 0) { 3043f4f1c429SMasami Hiramatsu if (num_matched_functions == -EACCES) { 3044f4f1c429SMasami Hiramatsu pr_err("Failed to load symbols from %s\n", 3045f4f1c429SMasami Hiramatsu pev->target ?: "/proc/kallsyms"); 3046f4f1c429SMasami Hiramatsu if (pev->target) 3047f4f1c429SMasami Hiramatsu pr_err("Please ensure the file is not stripped.\n"); 3048f4f1c429SMasami Hiramatsu else 3049f4f1c429SMasami Hiramatsu pr_kallsyms_access_error(); 3050f4f1c429SMasami Hiramatsu } else 3051eb948e50SMasami Hiramatsu pr_err("Failed to find symbol %s in %s\n", pp->function, 305244225521SMasami Hiramatsu pev->target ? : "kernel"); 3053eb948e50SMasami Hiramatsu ret = -ENOENT; 3054eb948e50SMasami Hiramatsu goto out; 3055ddb2f58fSMasami Hiramatsu } else if (num_matched_functions > probe_conf.max_probes) { 3056eb948e50SMasami Hiramatsu pr_err("Too many functions matched in %s\n", 305744225521SMasami Hiramatsu pev->target ? : "kernel"); 3058eb948e50SMasami Hiramatsu ret = -E2BIG; 3059eb948e50SMasami Hiramatsu goto out; 3060eb948e50SMasami Hiramatsu } 3061eb948e50SMasami Hiramatsu 30621a8ac29cSMasami Hiramatsu /* Note that the symbols in the kmodule are not relocated */ 30637ab31d94SNaveen N. Rao if (!pev->uprobes && !pev->target && 30647ab31d94SNaveen N. Rao (!pp->retprobe || kretprobe_offset_is_supported())) { 306580526491SMasami Hiramatsu reloc_sym = kernel_get_ref_reloc_sym(NULL); 3066eb948e50SMasami Hiramatsu if (!reloc_sym) { 306741ca1d1eSRavi Bangoria pr_warning("Relocated base symbol is not found! " 306841ca1d1eSRavi Bangoria "Check /proc/sys/kernel/kptr_restrict\n" 306941ca1d1eSRavi Bangoria "and /proc/sys/kernel/perf_event_paranoid. " 307041ca1d1eSRavi Bangoria "Or run as privileged perf user.\n\n"); 3071eb948e50SMasami Hiramatsu ret = -EINVAL; 3072eb948e50SMasami Hiramatsu goto out; 3073eb948e50SMasami Hiramatsu } 3074eb948e50SMasami Hiramatsu } 3075eb948e50SMasami Hiramatsu 3076eb948e50SMasami Hiramatsu /* Setup result trace-probe-events */ 3077eb948e50SMasami Hiramatsu *tevs = zalloc(sizeof(*tev) * num_matched_functions); 3078eb948e50SMasami Hiramatsu if (!*tevs) { 3079eb948e50SMasami Hiramatsu ret = -ENOMEM; 3080eb948e50SMasami Hiramatsu goto out; 3081eb948e50SMasami Hiramatsu } 3082eb948e50SMasami Hiramatsu 3083eb948e50SMasami Hiramatsu ret = 0; 3084564c62a4SNamhyung Kim 30856bb536ccSWang Nan for (j = 0; j < num_matched_functions; j++) { 30866bb536ccSWang Nan sym = syms[j]; 30876bb536ccSWang Nan 30884624f199SZechuan Chen if (sym->type != STT_FUNC) 30894624f199SZechuan Chen continue; 30904624f199SZechuan Chen 309126bbf45fSMasami Hiramatsu /* There can be duplicated symbols in the map */ 309226bbf45fSMasami Hiramatsu for (i = 0; i < j; i++) 309326bbf45fSMasami Hiramatsu if (sym->start == syms[i]->start) { 309426bbf45fSMasami Hiramatsu pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n", 309526bbf45fSMasami Hiramatsu sym->name, sym->start); 309626bbf45fSMasami Hiramatsu break; 309726bbf45fSMasami Hiramatsu } 309826bbf45fSMasami Hiramatsu if (i != j) 309926bbf45fSMasami Hiramatsu continue; 310026bbf45fSMasami Hiramatsu 3101eb948e50SMasami Hiramatsu tev = (*tevs) + ret; 3102eb948e50SMasami Hiramatsu tp = &tev->point; 3103eb948e50SMasami Hiramatsu if (ret == num_matched_functions) { 3104eb948e50SMasami Hiramatsu pr_warning("Too many symbols are listed. Skip it.\n"); 3105eb948e50SMasami Hiramatsu break; 3106eb948e50SMasami Hiramatsu } 3107eb948e50SMasami Hiramatsu ret++; 3108eb948e50SMasami Hiramatsu 3109eb948e50SMasami Hiramatsu if (pp->offset > sym->end - sym->start) { 3110eb948e50SMasami Hiramatsu pr_warning("Offset %ld is bigger than the size of %s\n", 3111eb948e50SMasami Hiramatsu pp->offset, sym->name); 3112eb948e50SMasami Hiramatsu ret = -ENOENT; 3113eb948e50SMasami Hiramatsu goto err_out; 3114eb948e50SMasami Hiramatsu } 3115eb948e50SMasami Hiramatsu /* Add one probe point */ 3116eb948e50SMasami Hiramatsu tp->address = map->unmap_ip(map, sym->start) + pp->offset; 31171a8ac29cSMasami Hiramatsu 31181a8ac29cSMasami Hiramatsu /* Check the kprobe (not in module) is within .text */ 31191a8ac29cSMasami Hiramatsu if (!pev->uprobes && !pev->target && 3120b031220dSMasami Hiramatsu kprobe_warn_out_range(sym->name, tp->address)) { 3121b031220dSMasami Hiramatsu tp->symbol = NULL; /* Skip it */ 3122b031220dSMasami Hiramatsu skipped++; 3123b031220dSMasami Hiramatsu } else if (reloc_sym) { 3124eb948e50SMasami Hiramatsu tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); 3125eb948e50SMasami Hiramatsu tp->offset = tp->address - reloc_sym->addr; 3126eb948e50SMasami Hiramatsu } else { 3127eb948e50SMasami Hiramatsu tp->symbol = strdup_or_goto(sym->name, nomem_out); 3128eb948e50SMasami Hiramatsu tp->offset = pp->offset; 3129eb948e50SMasami Hiramatsu } 31306bb536ccSWang Nan tp->realname = strdup_or_goto(sym->name, nomem_out); 31316bb536ccSWang Nan 3132eb948e50SMasami Hiramatsu tp->retprobe = pp->retprobe; 3133c61fb959SRavi Bangoria if (pev->target) { 3134c61fb959SRavi Bangoria if (pev->uprobes) { 313544225521SMasami Hiramatsu tev->point.module = strdup_or_goto(pev->target, 313644225521SMasami Hiramatsu nomem_out); 3137c61fb959SRavi Bangoria } else { 3138c61fb959SRavi Bangoria mod_name = find_module_name(pev->target); 3139c61fb959SRavi Bangoria tev->point.module = 3140c61fb959SRavi Bangoria strdup(mod_name ? mod_name : pev->target); 3141c61fb959SRavi Bangoria free(mod_name); 3142c61fb959SRavi Bangoria if (!tev->point.module) 3143c61fb959SRavi Bangoria goto nomem_out; 3144c61fb959SRavi Bangoria } 3145c61fb959SRavi Bangoria } 3146eb948e50SMasami Hiramatsu tev->uprobes = pev->uprobes; 3147eb948e50SMasami Hiramatsu tev->nargs = pev->nargs; 3148eb948e50SMasami Hiramatsu if (tev->nargs) { 3149eb948e50SMasami Hiramatsu tev->args = zalloc(sizeof(struct probe_trace_arg) * 3150eb948e50SMasami Hiramatsu tev->nargs); 3151eb948e50SMasami Hiramatsu if (tev->args == NULL) 3152eb948e50SMasami Hiramatsu goto nomem_out; 3153eb948e50SMasami Hiramatsu } 3154eb948e50SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) { 3155eb948e50SMasami Hiramatsu if (pev->args[i].name) 3156eb948e50SMasami Hiramatsu tev->args[i].name = 3157eb948e50SMasami Hiramatsu strdup_or_goto(pev->args[i].name, 3158eb948e50SMasami Hiramatsu nomem_out); 3159eb948e50SMasami Hiramatsu 3160eb948e50SMasami Hiramatsu tev->args[i].value = strdup_or_goto(pev->args[i].var, 3161eb948e50SMasami Hiramatsu nomem_out); 3162eb948e50SMasami Hiramatsu if (pev->args[i].type) 3163eb948e50SMasami Hiramatsu tev->args[i].type = 3164eb948e50SMasami Hiramatsu strdup_or_goto(pev->args[i].type, 3165eb948e50SMasami Hiramatsu nomem_out); 3166eb948e50SMasami Hiramatsu } 31670b3c2264SNaveen N. Rao arch__fix_tev_from_maps(pev, tev, map, sym); 3168eb948e50SMasami Hiramatsu } 3169b031220dSMasami Hiramatsu if (ret == skipped) { 3170b031220dSMasami Hiramatsu ret = -ENOENT; 3171b031220dSMasami Hiramatsu goto err_out; 3172b031220dSMasami Hiramatsu } 3173eb948e50SMasami Hiramatsu 3174eb948e50SMasami Hiramatsu out: 3175eebc509bSMasami Hiramatsu map__put(map); 31766bb536ccSWang Nan free(syms); 3177eb948e50SMasami Hiramatsu return ret; 3178eb948e50SMasami Hiramatsu 3179eb948e50SMasami Hiramatsu nomem_out: 3180eb948e50SMasami Hiramatsu ret = -ENOMEM; 3181eb948e50SMasami Hiramatsu err_out: 3182eb948e50SMasami Hiramatsu clear_probe_trace_events(*tevs, num_matched_functions); 3183eb948e50SMasami Hiramatsu zfree(tevs); 3184eb948e50SMasami Hiramatsu goto out; 3185eb948e50SMasami Hiramatsu } 3186eb948e50SMasami Hiramatsu 3187da15bd9dSWang Nan static int try_to_find_absolute_address(struct perf_probe_event *pev, 3188da15bd9dSWang Nan struct probe_trace_event **tevs) 3189da15bd9dSWang Nan { 3190da15bd9dSWang Nan struct perf_probe_point *pp = &pev->point; 3191da15bd9dSWang Nan struct probe_trace_event *tev; 3192da15bd9dSWang Nan struct probe_trace_point *tp; 3193da15bd9dSWang Nan int i, err; 3194da15bd9dSWang Nan 3195da15bd9dSWang Nan if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) 3196da15bd9dSWang Nan return -EINVAL; 3197da15bd9dSWang Nan if (perf_probe_event_need_dwarf(pev)) 3198da15bd9dSWang Nan return -EINVAL; 3199da15bd9dSWang Nan 3200da15bd9dSWang Nan /* 3201da15bd9dSWang Nan * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at 3202da15bd9dSWang Nan * absolute address. 3203da15bd9dSWang Nan * 3204da15bd9dSWang Nan * Only one tev can be generated by this. 3205da15bd9dSWang Nan */ 3206da15bd9dSWang Nan *tevs = zalloc(sizeof(*tev)); 3207da15bd9dSWang Nan if (!*tevs) 3208da15bd9dSWang Nan return -ENOMEM; 3209da15bd9dSWang Nan 3210da15bd9dSWang Nan tev = *tevs; 3211da15bd9dSWang Nan tp = &tev->point; 3212da15bd9dSWang Nan 3213da15bd9dSWang Nan /* 3214da15bd9dSWang Nan * Don't use tp->offset, use address directly, because 3215da15bd9dSWang Nan * in synthesize_probe_trace_command() address cannot be 3216da15bd9dSWang Nan * zero. 3217da15bd9dSWang Nan */ 3218da15bd9dSWang Nan tp->address = pev->point.abs_address; 3219da15bd9dSWang Nan tp->retprobe = pp->retprobe; 3220da15bd9dSWang Nan tev->uprobes = pev->uprobes; 3221da15bd9dSWang Nan 3222da15bd9dSWang Nan err = -ENOMEM; 3223da15bd9dSWang Nan /* 3224da15bd9dSWang Nan * Give it a '0x' leading symbol name. 3225da15bd9dSWang Nan * In __add_probe_trace_events, a NULL symbol is interpreted as 3226adba1634SIngo Molnar * invalid. 3227da15bd9dSWang Nan */ 322822a66551SYang Jihong if (asprintf(&tp->symbol, "0x%" PRIx64, tp->address) < 0) 3229da15bd9dSWang Nan goto errout; 3230da15bd9dSWang Nan 3231da15bd9dSWang Nan /* For kprobe, check range */ 3232da15bd9dSWang Nan if ((!tev->uprobes) && 3233da15bd9dSWang Nan (kprobe_warn_out_range(tev->point.symbol, 3234da15bd9dSWang Nan tev->point.address))) { 3235da15bd9dSWang Nan err = -EACCES; 3236da15bd9dSWang Nan goto errout; 3237da15bd9dSWang Nan } 3238da15bd9dSWang Nan 323922a66551SYang Jihong if (asprintf(&tp->realname, "abs_%" PRIx64, tp->address) < 0) 3240da15bd9dSWang Nan goto errout; 3241da15bd9dSWang Nan 3242da15bd9dSWang Nan if (pev->target) { 3243da15bd9dSWang Nan tp->module = strdup(pev->target); 3244da15bd9dSWang Nan if (!tp->module) 3245da15bd9dSWang Nan goto errout; 3246da15bd9dSWang Nan } 3247da15bd9dSWang Nan 3248da15bd9dSWang Nan if (tev->group) { 3249da15bd9dSWang Nan tev->group = strdup(pev->group); 3250da15bd9dSWang Nan if (!tev->group) 3251da15bd9dSWang Nan goto errout; 3252da15bd9dSWang Nan } 3253da15bd9dSWang Nan 3254da15bd9dSWang Nan if (pev->event) { 3255da15bd9dSWang Nan tev->event = strdup(pev->event); 3256da15bd9dSWang Nan if (!tev->event) 3257da15bd9dSWang Nan goto errout; 3258da15bd9dSWang Nan } 3259da15bd9dSWang Nan 3260da15bd9dSWang Nan tev->nargs = pev->nargs; 3261da15bd9dSWang Nan tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 3262d1d0e29cSMarkus Elfring if (!tev->args) 3263da15bd9dSWang Nan goto errout; 3264d1d0e29cSMarkus Elfring 3265da15bd9dSWang Nan for (i = 0; i < tev->nargs; i++) 3266da15bd9dSWang Nan copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); 3267da15bd9dSWang Nan 3268da15bd9dSWang Nan return 1; 3269da15bd9dSWang Nan 3270da15bd9dSWang Nan errout: 3271da15bd9dSWang Nan clear_probe_trace_events(*tevs, 1); 3272da15bd9dSWang Nan *tevs = NULL; 3273da15bd9dSWang Nan return err; 3274da15bd9dSWang Nan } 3275da15bd9dSWang Nan 32764d39c89fSIngo Molnar /* Concatenate two arrays */ 327742bba263SMasami Hiramatsu static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) 327842bba263SMasami Hiramatsu { 327942bba263SMasami Hiramatsu void *ret; 328042bba263SMasami Hiramatsu 328142bba263SMasami Hiramatsu ret = malloc(sz_a + sz_b); 328242bba263SMasami Hiramatsu if (ret) { 328342bba263SMasami Hiramatsu memcpy(ret, a, sz_a); 328442bba263SMasami Hiramatsu memcpy(ret + sz_a, b, sz_b); 328542bba263SMasami Hiramatsu } 328642bba263SMasami Hiramatsu return ret; 328742bba263SMasami Hiramatsu } 328842bba263SMasami Hiramatsu 328942bba263SMasami Hiramatsu static int 329042bba263SMasami Hiramatsu concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, 329142bba263SMasami Hiramatsu struct probe_trace_event **tevs2, int ntevs2) 329242bba263SMasami Hiramatsu { 329342bba263SMasami Hiramatsu struct probe_trace_event *new_tevs; 329442bba263SMasami Hiramatsu int ret = 0; 329542bba263SMasami Hiramatsu 3296f0a30dcaSRavi Bangoria if (*ntevs == 0) { 329742bba263SMasami Hiramatsu *tevs = *tevs2; 329842bba263SMasami Hiramatsu *ntevs = ntevs2; 329942bba263SMasami Hiramatsu *tevs2 = NULL; 330042bba263SMasami Hiramatsu return 0; 330142bba263SMasami Hiramatsu } 330242bba263SMasami Hiramatsu 330342bba263SMasami Hiramatsu if (*ntevs + ntevs2 > probe_conf.max_probes) 330442bba263SMasami Hiramatsu ret = -E2BIG; 330542bba263SMasami Hiramatsu else { 33064d39c89fSIngo Molnar /* Concatenate the array of probe_trace_event */ 330742bba263SMasami Hiramatsu new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), 330842bba263SMasami Hiramatsu *tevs2, ntevs2 * sizeof(**tevs2)); 330942bba263SMasami Hiramatsu if (!new_tevs) 331042bba263SMasami Hiramatsu ret = -ENOMEM; 331142bba263SMasami Hiramatsu else { 331242bba263SMasami Hiramatsu free(*tevs); 331342bba263SMasami Hiramatsu *tevs = new_tevs; 331442bba263SMasami Hiramatsu *ntevs += ntevs2; 331542bba263SMasami Hiramatsu } 331642bba263SMasami Hiramatsu } 331742bba263SMasami Hiramatsu if (ret < 0) 331842bba263SMasami Hiramatsu clear_probe_trace_events(*tevs2, ntevs2); 331942bba263SMasami Hiramatsu zfree(tevs2); 332042bba263SMasami Hiramatsu 332142bba263SMasami Hiramatsu return ret; 332242bba263SMasami Hiramatsu } 332342bba263SMasami Hiramatsu 332442bba263SMasami Hiramatsu /* 332542bba263SMasami Hiramatsu * Try to find probe_trace_event from given probe caches. Return the number 332642bba263SMasami Hiramatsu * of cached events found, if an error occurs return the error. 332742bba263SMasami Hiramatsu */ 332842bba263SMasami Hiramatsu static int find_cached_events(struct perf_probe_event *pev, 332942bba263SMasami Hiramatsu struct probe_trace_event **tevs, 333042bba263SMasami Hiramatsu const char *target) 333142bba263SMasami Hiramatsu { 333242bba263SMasami Hiramatsu struct probe_cache *cache; 333342bba263SMasami Hiramatsu struct probe_cache_entry *entry; 333442bba263SMasami Hiramatsu struct probe_trace_event *tmp_tevs = NULL; 333542bba263SMasami Hiramatsu int ntevs = 0; 333642bba263SMasami Hiramatsu int ret = 0; 333742bba263SMasami Hiramatsu 3338f045b8c4SKrister Johansen cache = probe_cache__new(target, pev->nsi); 333942bba263SMasami Hiramatsu /* Return 0 ("not found") if the target has no probe cache. */ 334042bba263SMasami Hiramatsu if (!cache) 334142bba263SMasami Hiramatsu return 0; 334242bba263SMasami Hiramatsu 334342bba263SMasami Hiramatsu for_each_probe_cache_entry(entry, cache) { 334442bba263SMasami Hiramatsu /* Skip the cache entry which has no name */ 334542bba263SMasami Hiramatsu if (!entry->pev.event || !entry->pev.group) 334642bba263SMasami Hiramatsu continue; 334742bba263SMasami Hiramatsu if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && 334842bba263SMasami Hiramatsu strglobmatch(entry->pev.event, pev->event)) { 334942bba263SMasami Hiramatsu ret = probe_cache_entry__get_event(entry, &tmp_tevs); 335042bba263SMasami Hiramatsu if (ret > 0) 335142bba263SMasami Hiramatsu ret = concat_probe_trace_events(tevs, &ntevs, 335242bba263SMasami Hiramatsu &tmp_tevs, ret); 335342bba263SMasami Hiramatsu if (ret < 0) 335442bba263SMasami Hiramatsu break; 335542bba263SMasami Hiramatsu } 335642bba263SMasami Hiramatsu } 335742bba263SMasami Hiramatsu probe_cache__delete(cache); 335842bba263SMasami Hiramatsu if (ret < 0) { 335942bba263SMasami Hiramatsu clear_probe_trace_events(*tevs, ntevs); 336042bba263SMasami Hiramatsu zfree(tevs); 336142bba263SMasami Hiramatsu } else { 336242bba263SMasami Hiramatsu ret = ntevs; 336342bba263SMasami Hiramatsu if (ntevs > 0 && target && target[0] == '/') 336442bba263SMasami Hiramatsu pev->uprobes = true; 336542bba263SMasami Hiramatsu } 336642bba263SMasami Hiramatsu 336742bba263SMasami Hiramatsu return ret; 336842bba263SMasami Hiramatsu } 336942bba263SMasami Hiramatsu 33701de7b8bfSMasami Hiramatsu /* Try to find probe_trace_event from all probe caches */ 33711de7b8bfSMasami Hiramatsu static int find_cached_events_all(struct perf_probe_event *pev, 33721de7b8bfSMasami Hiramatsu struct probe_trace_event **tevs) 33731de7b8bfSMasami Hiramatsu { 33741de7b8bfSMasami Hiramatsu struct probe_trace_event *tmp_tevs = NULL; 33751de7b8bfSMasami Hiramatsu struct strlist *bidlist; 33761de7b8bfSMasami Hiramatsu struct str_node *nd; 33771de7b8bfSMasami Hiramatsu char *pathname; 33781de7b8bfSMasami Hiramatsu int ntevs = 0; 33791de7b8bfSMasami Hiramatsu int ret; 33801de7b8bfSMasami Hiramatsu 33811de7b8bfSMasami Hiramatsu /* Get the buildid list of all valid caches */ 33821de7b8bfSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 33831de7b8bfSMasami Hiramatsu if (!bidlist) { 33841de7b8bfSMasami Hiramatsu ret = -errno; 33851de7b8bfSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", ret); 33861de7b8bfSMasami Hiramatsu return ret; 33871de7b8bfSMasami Hiramatsu } 33881de7b8bfSMasami Hiramatsu 33891de7b8bfSMasami Hiramatsu ret = 0; 33901de7b8bfSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 33911de7b8bfSMasami Hiramatsu pathname = build_id_cache__origname(nd->s); 33921de7b8bfSMasami Hiramatsu ret = find_cached_events(pev, &tmp_tevs, pathname); 33931de7b8bfSMasami Hiramatsu /* In the case of cnt == 0, we just skip it */ 33941de7b8bfSMasami Hiramatsu if (ret > 0) 33951de7b8bfSMasami Hiramatsu ret = concat_probe_trace_events(tevs, &ntevs, 33961de7b8bfSMasami Hiramatsu &tmp_tevs, ret); 33971de7b8bfSMasami Hiramatsu free(pathname); 33981de7b8bfSMasami Hiramatsu if (ret < 0) 33991de7b8bfSMasami Hiramatsu break; 34001de7b8bfSMasami Hiramatsu } 34011de7b8bfSMasami Hiramatsu strlist__delete(bidlist); 34021de7b8bfSMasami Hiramatsu 34031de7b8bfSMasami Hiramatsu if (ret < 0) { 34041de7b8bfSMasami Hiramatsu clear_probe_trace_events(*tevs, ntevs); 34051de7b8bfSMasami Hiramatsu zfree(tevs); 34061de7b8bfSMasami Hiramatsu } else 34071de7b8bfSMasami Hiramatsu ret = ntevs; 34081de7b8bfSMasami Hiramatsu 34091de7b8bfSMasami Hiramatsu return ret; 34101de7b8bfSMasami Hiramatsu } 34111de7b8bfSMasami Hiramatsu 3412bc062230SMasami Hiramatsu static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, 3413bc062230SMasami Hiramatsu struct probe_trace_event **tevs) 3414bc062230SMasami Hiramatsu { 3415bc062230SMasami Hiramatsu struct probe_cache *cache; 3416bc062230SMasami Hiramatsu struct probe_cache_entry *entry; 3417bc062230SMasami Hiramatsu struct probe_trace_event *tev; 3418bc062230SMasami Hiramatsu struct str_node *node; 3419bc062230SMasami Hiramatsu int ret, i; 3420bc062230SMasami Hiramatsu 34211de7b8bfSMasami Hiramatsu if (pev->sdt) { 342242bba263SMasami Hiramatsu /* For SDT/cached events, we use special search functions */ 34231de7b8bfSMasami Hiramatsu if (!pev->target) 34241de7b8bfSMasami Hiramatsu return find_cached_events_all(pev, tevs); 34251de7b8bfSMasami Hiramatsu else 342642bba263SMasami Hiramatsu return find_cached_events(pev, tevs, pev->target); 34271de7b8bfSMasami Hiramatsu } 3428f045b8c4SKrister Johansen cache = probe_cache__new(pev->target, pev->nsi); 3429bc062230SMasami Hiramatsu if (!cache) 3430bc062230SMasami Hiramatsu return 0; 3431bc062230SMasami Hiramatsu 3432bc062230SMasami Hiramatsu entry = probe_cache__find(cache, pev); 3433bc062230SMasami Hiramatsu if (!entry) { 343436a009feSMasami Hiramatsu /* SDT must be in the cache */ 343536a009feSMasami Hiramatsu ret = pev->sdt ? -ENOENT : 0; 3436bc062230SMasami Hiramatsu goto out; 3437bc062230SMasami Hiramatsu } 3438bc062230SMasami Hiramatsu 3439bc062230SMasami Hiramatsu ret = strlist__nr_entries(entry->tevlist); 3440bc062230SMasami Hiramatsu if (ret > probe_conf.max_probes) { 3441bc062230SMasami Hiramatsu pr_debug("Too many entries matched in the cache of %s\n", 3442bc062230SMasami Hiramatsu pev->target ? : "kernel"); 3443bc062230SMasami Hiramatsu ret = -E2BIG; 3444bc062230SMasami Hiramatsu goto out; 3445bc062230SMasami Hiramatsu } 3446bc062230SMasami Hiramatsu 3447bc062230SMasami Hiramatsu *tevs = zalloc(ret * sizeof(*tev)); 3448bc062230SMasami Hiramatsu if (!*tevs) { 3449bc062230SMasami Hiramatsu ret = -ENOMEM; 3450bc062230SMasami Hiramatsu goto out; 3451bc062230SMasami Hiramatsu } 3452bc062230SMasami Hiramatsu 3453bc062230SMasami Hiramatsu i = 0; 3454bc062230SMasami Hiramatsu strlist__for_each_entry(node, entry->tevlist) { 3455bc062230SMasami Hiramatsu tev = &(*tevs)[i++]; 3456bc062230SMasami Hiramatsu ret = parse_probe_trace_command(node->s, tev); 3457bc062230SMasami Hiramatsu if (ret < 0) 3458bc062230SMasami Hiramatsu goto out; 3459bc062230SMasami Hiramatsu /* Set the uprobes attribute as same as original */ 3460bc062230SMasami Hiramatsu tev->uprobes = pev->uprobes; 3461bc062230SMasami Hiramatsu } 3462bc062230SMasami Hiramatsu ret = i; 3463bc062230SMasami Hiramatsu 3464bc062230SMasami Hiramatsu out: 3465bc062230SMasami Hiramatsu probe_cache__delete(cache); 3466bc062230SMasami Hiramatsu return ret; 3467bc062230SMasami Hiramatsu } 3468bc062230SMasami Hiramatsu 34690e60836bSSrikar Dronamraju static int convert_to_probe_trace_events(struct perf_probe_event *pev, 3470ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs) 3471e0faa8d3SMasami Hiramatsu { 3472eb948e50SMasami Hiramatsu int ret; 34734235b045SMasami Hiramatsu 347436a009feSMasami Hiramatsu if (!pev->group && !pev->sdt) { 34752a12ec13SMasami Hiramatsu /* Set group name if not given */ 34762a12ec13SMasami Hiramatsu if (!pev->uprobes) { 34772a12ec13SMasami Hiramatsu pev->group = strdup(PERFPROBE_GROUP); 34782a12ec13SMasami Hiramatsu ret = pev->group ? 0 : -ENOMEM; 34792a12ec13SMasami Hiramatsu } else 348044225521SMasami Hiramatsu ret = convert_exec_to_group(pev->target, &pev->group); 3481fb7345bbSMasami Hiramatsu if (ret != 0) { 3482fb7345bbSMasami Hiramatsu pr_warning("Failed to make a group name.\n"); 3483fb7345bbSMasami Hiramatsu return ret; 3484fb7345bbSMasami Hiramatsu } 3485fb7345bbSMasami Hiramatsu } 3486fb7345bbSMasami Hiramatsu 3487da15bd9dSWang Nan ret = try_to_find_absolute_address(pev, tevs); 3488da15bd9dSWang Nan if (ret > 0) 3489da15bd9dSWang Nan return ret; 3490da15bd9dSWang Nan 3491bc062230SMasami Hiramatsu /* At first, we need to lookup cache entry */ 3492bc062230SMasami Hiramatsu ret = find_probe_trace_events_from_cache(pev, tevs); 349336a009feSMasami Hiramatsu if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ 349436a009feSMasami Hiramatsu return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ 3495bc062230SMasami Hiramatsu 34964b4da7f7SMasami Hiramatsu /* Convert perf_probe_event with debuginfo */ 3497ddb2f58fSMasami Hiramatsu ret = try_to_find_probe_trace_events(pev, tevs); 3498e334016fSMasami Hiramatsu if (ret != 0) 3499190b57fcSMasami Hiramatsu return ret; /* Found in debuginfo or got an error */ 3500e0faa8d3SMasami Hiramatsu 3501ddb2f58fSMasami Hiramatsu return find_probe_trace_events_from_map(pev, tevs); 35024235b045SMasami Hiramatsu } 35034235b045SMasami Hiramatsu 350412fae5efSWang Nan int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) 35054235b045SMasami Hiramatsu { 3506844dffa5SNamhyung Kim int i, ret; 35074235b045SMasami Hiramatsu 35084235b045SMasami Hiramatsu /* Loop 1: convert all events */ 35094235b045SMasami Hiramatsu for (i = 0; i < npevs; i++) { 3510b031220dSMasami Hiramatsu /* Init kprobe blacklist if needed */ 351112fae5efSWang Nan if (!pevs[i].uprobes) 3512b031220dSMasami Hiramatsu kprobe_blacklist__init(); 35134235b045SMasami Hiramatsu /* Convert with or without debuginfo */ 351412fae5efSWang Nan ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); 3515146a1439SMasami Hiramatsu if (ret < 0) 3516844dffa5SNamhyung Kim return ret; 351712fae5efSWang Nan pevs[i].ntevs = ret; 35184235b045SMasami Hiramatsu } 3519b031220dSMasami Hiramatsu /* This just release blacklist only if allocated */ 3520b031220dSMasami Hiramatsu kprobe_blacklist__release(); 35214235b045SMasami Hiramatsu 3522844dffa5SNamhyung Kim return 0; 3523844dffa5SNamhyung Kim } 3524844dffa5SNamhyung Kim 35251c20b1d1SMasami Hiramatsu static int show_probe_trace_event(struct probe_trace_event *tev) 35261c20b1d1SMasami Hiramatsu { 35271c20b1d1SMasami Hiramatsu char *buf = synthesize_probe_trace_command(tev); 35281c20b1d1SMasami Hiramatsu 35291c20b1d1SMasami Hiramatsu if (!buf) { 35301c20b1d1SMasami Hiramatsu pr_debug("Failed to synthesize probe trace event.\n"); 35311c20b1d1SMasami Hiramatsu return -EINVAL; 35321c20b1d1SMasami Hiramatsu } 35331c20b1d1SMasami Hiramatsu 35341c20b1d1SMasami Hiramatsu /* Showing definition always go stdout */ 35351c20b1d1SMasami Hiramatsu printf("%s\n", buf); 35361c20b1d1SMasami Hiramatsu free(buf); 35371c20b1d1SMasami Hiramatsu 35381c20b1d1SMasami Hiramatsu return 0; 35391c20b1d1SMasami Hiramatsu } 35401c20b1d1SMasami Hiramatsu 35411c20b1d1SMasami Hiramatsu int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) 35421c20b1d1SMasami Hiramatsu { 35431c20b1d1SMasami Hiramatsu struct strlist *namelist = strlist__new(NULL, NULL); 35441c20b1d1SMasami Hiramatsu struct probe_trace_event *tev; 35451c20b1d1SMasami Hiramatsu struct perf_probe_event *pev; 35461c20b1d1SMasami Hiramatsu int i, j, ret = 0; 35471c20b1d1SMasami Hiramatsu 35481c20b1d1SMasami Hiramatsu if (!namelist) 35491c20b1d1SMasami Hiramatsu return -ENOMEM; 35501c20b1d1SMasami Hiramatsu 35511c20b1d1SMasami Hiramatsu for (j = 0; j < npevs && !ret; j++) { 35521c20b1d1SMasami Hiramatsu pev = &pevs[j]; 35531c20b1d1SMasami Hiramatsu for (i = 0; i < pev->ntevs && !ret; i++) { 35541c20b1d1SMasami Hiramatsu tev = &pev->tevs[i]; 35551c20b1d1SMasami Hiramatsu /* Skip if the symbol is out of .text or blacklisted */ 35561c20b1d1SMasami Hiramatsu if (!tev->point.symbol && !pev->uprobes) 35571c20b1d1SMasami Hiramatsu continue; 35581c20b1d1SMasami Hiramatsu 35591c20b1d1SMasami Hiramatsu /* Set new name for tev (and update namelist) */ 35601c20b1d1SMasami Hiramatsu ret = probe_trace_event__set_name(tev, pev, 35611c20b1d1SMasami Hiramatsu namelist, true); 35621c20b1d1SMasami Hiramatsu if (!ret) 35631c20b1d1SMasami Hiramatsu ret = show_probe_trace_event(tev); 35641c20b1d1SMasami Hiramatsu } 35651c20b1d1SMasami Hiramatsu } 35661c20b1d1SMasami Hiramatsu strlist__delete(namelist); 35671c20b1d1SMasami Hiramatsu 35681c20b1d1SMasami Hiramatsu return ret; 35691c20b1d1SMasami Hiramatsu } 35701c20b1d1SMasami Hiramatsu 357145237f98SMasami Hiramatsu static int show_bootconfig_event(struct probe_trace_event *tev) 357245237f98SMasami Hiramatsu { 357345237f98SMasami Hiramatsu struct probe_trace_point *tp = &tev->point; 357445237f98SMasami Hiramatsu struct strbuf buf; 357545237f98SMasami Hiramatsu char *ret = NULL; 357645237f98SMasami Hiramatsu int err; 357745237f98SMasami Hiramatsu 357845237f98SMasami Hiramatsu if (strbuf_init(&buf, 32) < 0) 357945237f98SMasami Hiramatsu return -ENOMEM; 358045237f98SMasami Hiramatsu 358145237f98SMasami Hiramatsu err = synthesize_kprobe_trace_def(tp, &buf); 358245237f98SMasami Hiramatsu if (err >= 0) 358345237f98SMasami Hiramatsu err = synthesize_probe_trace_args(tev, &buf); 358445237f98SMasami Hiramatsu if (err >= 0) 358545237f98SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 358645237f98SMasami Hiramatsu strbuf_release(&buf); 358745237f98SMasami Hiramatsu 358845237f98SMasami Hiramatsu if (ret) { 358945237f98SMasami Hiramatsu printf("'%s'", ret); 359045237f98SMasami Hiramatsu free(ret); 359145237f98SMasami Hiramatsu } 359245237f98SMasami Hiramatsu 359345237f98SMasami Hiramatsu return err; 359445237f98SMasami Hiramatsu } 359545237f98SMasami Hiramatsu 359645237f98SMasami Hiramatsu int show_bootconfig_events(struct perf_probe_event *pevs, int npevs) 359745237f98SMasami Hiramatsu { 359845237f98SMasami Hiramatsu struct strlist *namelist = strlist__new(NULL, NULL); 359945237f98SMasami Hiramatsu struct probe_trace_event *tev; 360045237f98SMasami Hiramatsu struct perf_probe_event *pev; 360145237f98SMasami Hiramatsu char *cur_name = NULL; 360245237f98SMasami Hiramatsu int i, j, ret = 0; 360345237f98SMasami Hiramatsu 360445237f98SMasami Hiramatsu if (!namelist) 360545237f98SMasami Hiramatsu return -ENOMEM; 360645237f98SMasami Hiramatsu 360745237f98SMasami Hiramatsu for (j = 0; j < npevs && !ret; j++) { 360845237f98SMasami Hiramatsu pev = &pevs[j]; 360945237f98SMasami Hiramatsu if (pev->group && strcmp(pev->group, "probe")) 361045237f98SMasami Hiramatsu pr_warning("WARN: Group name %s is ignored\n", pev->group); 361145237f98SMasami Hiramatsu if (pev->uprobes) { 361245237f98SMasami Hiramatsu pr_warning("ERROR: Bootconfig doesn't support uprobes\n"); 361345237f98SMasami Hiramatsu ret = -EINVAL; 361445237f98SMasami Hiramatsu break; 361545237f98SMasami Hiramatsu } 361645237f98SMasami Hiramatsu for (i = 0; i < pev->ntevs && !ret; i++) { 361745237f98SMasami Hiramatsu tev = &pev->tevs[i]; 361845237f98SMasami Hiramatsu /* Skip if the symbol is out of .text or blacklisted */ 361945237f98SMasami Hiramatsu if (!tev->point.symbol && !pev->uprobes) 362045237f98SMasami Hiramatsu continue; 362145237f98SMasami Hiramatsu 362245237f98SMasami Hiramatsu /* Set new name for tev (and update namelist) */ 362345237f98SMasami Hiramatsu ret = probe_trace_event__set_name(tev, pev, 362445237f98SMasami Hiramatsu namelist, true); 362545237f98SMasami Hiramatsu if (ret) 362645237f98SMasami Hiramatsu break; 362745237f98SMasami Hiramatsu 362845237f98SMasami Hiramatsu if (!cur_name || strcmp(cur_name, tev->event)) { 362945237f98SMasami Hiramatsu printf("%sftrace.event.kprobes.%s.probe = ", 363045237f98SMasami Hiramatsu cur_name ? "\n" : "", tev->event); 363145237f98SMasami Hiramatsu cur_name = tev->event; 363245237f98SMasami Hiramatsu } else 363345237f98SMasami Hiramatsu printf(", "); 363445237f98SMasami Hiramatsu ret = show_bootconfig_event(tev); 363545237f98SMasami Hiramatsu } 363645237f98SMasami Hiramatsu } 363745237f98SMasami Hiramatsu printf("\n"); 363845237f98SMasami Hiramatsu strlist__delete(namelist); 363945237f98SMasami Hiramatsu 364045237f98SMasami Hiramatsu return ret; 364145237f98SMasami Hiramatsu } 364245237f98SMasami Hiramatsu 364312fae5efSWang Nan int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3644844dffa5SNamhyung Kim { 3645844dffa5SNamhyung Kim int i, ret = 0; 3646844dffa5SNamhyung Kim 36474235b045SMasami Hiramatsu /* Loop 2: add all events */ 36488635bf6eSArnaldo Carvalho de Melo for (i = 0; i < npevs; i++) { 364912fae5efSWang Nan ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, 365012fae5efSWang Nan pevs[i].ntevs, 3651ddb2f58fSMasami Hiramatsu probe_conf.force_add); 3652fbee632dSArnaldo Carvalho de Melo if (ret < 0) 3653fbee632dSArnaldo Carvalho de Melo break; 3654fbee632dSArnaldo Carvalho de Melo } 3655844dffa5SNamhyung Kim return ret; 3656844dffa5SNamhyung Kim } 3657844dffa5SNamhyung Kim 365812fae5efSWang Nan void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3659844dffa5SNamhyung Kim { 3660844dffa5SNamhyung Kim int i, j; 3661544abd44SKrister Johansen struct perf_probe_event *pev; 3662844dffa5SNamhyung Kim 3663449e5b24SMasami Hiramatsu /* Loop 3: cleanup and free trace events */ 3664449e5b24SMasami Hiramatsu for (i = 0; i < npevs; i++) { 3665544abd44SKrister Johansen pev = &pevs[i]; 366612fae5efSWang Nan for (j = 0; j < pevs[i].ntevs; j++) 366712fae5efSWang Nan clear_probe_trace_event(&pevs[i].tevs[j]); 366812fae5efSWang Nan zfree(&pevs[i].tevs); 366912fae5efSWang Nan pevs[i].ntevs = 0; 3670544abd44SKrister Johansen nsinfo__zput(pev->nsi); 3671a43aac29SNamhyung Kim clear_perf_probe_event(&pevs[i]); 3672449e5b24SMasami Hiramatsu } 3673844dffa5SNamhyung Kim } 3674844dffa5SNamhyung Kim 3675844dffa5SNamhyung Kim int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3676844dffa5SNamhyung Kim { 3677844dffa5SNamhyung Kim int ret; 3678844dffa5SNamhyung Kim 36799bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(pevs->uprobes); 36809bae1e8cSNamhyung Kim if (ret < 0) 36819bae1e8cSNamhyung Kim return ret; 36829bae1e8cSNamhyung Kim 368312fae5efSWang Nan ret = convert_perf_probe_events(pevs, npevs); 3684844dffa5SNamhyung Kim if (ret == 0) 368512fae5efSWang Nan ret = apply_perf_probe_events(pevs, npevs); 3686844dffa5SNamhyung Kim 368712fae5efSWang Nan cleanup_perf_probe_events(pevs, npevs); 3688146a1439SMasami Hiramatsu 36899bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 3690146a1439SMasami Hiramatsu return ret; 3691e0faa8d3SMasami Hiramatsu } 3692e0faa8d3SMasami Hiramatsu 3693307a464bSMasami Hiramatsu int del_perf_probe_events(struct strfilter *filter) 3694fa28244dSMasami Hiramatsu { 3695307a464bSMasami Hiramatsu int ret, ret2, ufd = -1, kfd = -1; 3696307a464bSMasami Hiramatsu char *str = strfilter__string(filter); 3697307a464bSMasami Hiramatsu 3698307a464bSMasami Hiramatsu if (!str) 3699307a464bSMasami Hiramatsu return -EINVAL; 3700307a464bSMasami Hiramatsu 3701fa28244dSMasami Hiramatsu /* Get current event names */ 370292f6c72eSMasami Hiramatsu ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); 370392f6c72eSMasami Hiramatsu if (ret < 0) 370492f6c72eSMasami Hiramatsu goto out; 3705225466f1SSrikar Dronamraju 370692f6c72eSMasami Hiramatsu ret = probe_file__del_events(kfd, filter); 3707307a464bSMasami Hiramatsu if (ret < 0 && ret != -ENOENT) 3708225466f1SSrikar Dronamraju goto error; 3709fa28244dSMasami Hiramatsu 371092f6c72eSMasami Hiramatsu ret2 = probe_file__del_events(ufd, filter); 3711dddc7ee3SMasami Hiramatsu if (ret2 < 0 && ret2 != -ENOENT) { 37126dbe31f7SMasami Hiramatsu ret = ret2; 3713dddc7ee3SMasami Hiramatsu goto error; 3714dddc7ee3SMasami Hiramatsu } 3715307a464bSMasami Hiramatsu ret = 0; 3716225466f1SSrikar Dronamraju 3717225466f1SSrikar Dronamraju error: 371892f6c72eSMasami Hiramatsu if (kfd >= 0) 3719225466f1SSrikar Dronamraju close(kfd); 372092f6c72eSMasami Hiramatsu if (ufd >= 0) 3721225466f1SSrikar Dronamraju close(ufd); 372292f6c72eSMasami Hiramatsu out: 3723307a464bSMasami Hiramatsu free(str); 3724146a1439SMasami Hiramatsu 3725146a1439SMasami Hiramatsu return ret; 3726fa28244dSMasami Hiramatsu } 3727225466f1SSrikar Dronamraju 3728544abd44SKrister Johansen int show_available_funcs(const char *target, struct nsinfo *nsi, 3729544abd44SKrister Johansen struct strfilter *_filter, bool user) 3730e80711caSMasami Hiramatsu { 3731fd227598SArnaldo Carvalho de Melo struct rb_node *nd; 37322df58634SMasami Hiramatsu struct map *map; 37332df58634SMasami Hiramatsu int ret; 37342df58634SMasami Hiramatsu 37359bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(user); 37362df58634SMasami Hiramatsu if (ret < 0) 37372df58634SMasami Hiramatsu return ret; 37382df58634SMasami Hiramatsu 37392df58634SMasami Hiramatsu /* Get a symbol map */ 3740544abd44SKrister Johansen map = get_target_map(target, nsi, user); 37412df58634SMasami Hiramatsu if (!map) { 37422df58634SMasami Hiramatsu pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); 3743e80711caSMasami Hiramatsu return -EINVAL; 3744e80711caSMasami Hiramatsu } 37452df58634SMasami Hiramatsu 3746be39db9fSArnaldo Carvalho de Melo ret = map__load(map); 3747e7049342SMasami Hiramatsu if (ret) { 3748e7049342SMasami Hiramatsu if (ret == -2) { 3749e7049342SMasami Hiramatsu char *str = strfilter__string(_filter); 3750e7049342SMasami Hiramatsu pr_err("Failed to find symbols matched to \"%s\"\n", 3751e7049342SMasami Hiramatsu str); 3752e7049342SMasami Hiramatsu free(str); 3753e7049342SMasami Hiramatsu } else 3754e7049342SMasami Hiramatsu pr_err("Failed to load symbols in %s\n", 3755e7049342SMasami Hiramatsu (target) ? : "kernel"); 37562df58634SMasami Hiramatsu goto end; 37572df58634SMasami Hiramatsu } 37583183f8caSArnaldo Carvalho de Melo if (!dso__sorted_by_name(map->dso)) 37593183f8caSArnaldo Carvalho de Melo dso__sort_by_name(map->dso); 3760e80711caSMasami Hiramatsu 37612df58634SMasami Hiramatsu /* Show all (filtered) symbols */ 37622df58634SMasami Hiramatsu setup_pager(); 3763fd227598SArnaldo Carvalho de Melo 37647137ff50SDavidlohr Bueso for (nd = rb_first_cached(&map->dso->symbol_names); nd; 37657137ff50SDavidlohr Bueso nd = rb_next(nd)) { 3766fd227598SArnaldo Carvalho de Melo struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); 3767fd227598SArnaldo Carvalho de Melo 3768fd227598SArnaldo Carvalho de Melo if (strfilter__compare(_filter, pos->sym.name)) 3769fd227598SArnaldo Carvalho de Melo printf("%s\n", pos->sym.name); 3770fd227598SArnaldo Carvalho de Melo } 37712df58634SMasami Hiramatsu end: 377284c2cafaSArnaldo Carvalho de Melo map__put(map); 37739bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 3774225466f1SSrikar Dronamraju 37752df58634SMasami Hiramatsu return ret; 3776225466f1SSrikar Dronamraju } 3777225466f1SSrikar Dronamraju 3778da15bd9dSWang Nan int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, 3779da15bd9dSWang Nan struct perf_probe_arg *pvar) 3780da15bd9dSWang Nan { 3781da15bd9dSWang Nan tvar->value = strdup(pvar->var); 3782da15bd9dSWang Nan if (tvar->value == NULL) 3783da15bd9dSWang Nan return -ENOMEM; 3784da15bd9dSWang Nan if (pvar->type) { 3785da15bd9dSWang Nan tvar->type = strdup(pvar->type); 3786da15bd9dSWang Nan if (tvar->type == NULL) 3787da15bd9dSWang Nan return -ENOMEM; 3788da15bd9dSWang Nan } 3789da15bd9dSWang Nan if (pvar->name) { 3790da15bd9dSWang Nan tvar->name = strdup(pvar->name); 3791da15bd9dSWang Nan if (tvar->name == NULL) 3792da15bd9dSWang Nan return -ENOMEM; 3793da15bd9dSWang Nan } else 3794da15bd9dSWang Nan tvar->name = NULL; 3795da15bd9dSWang Nan return 0; 3796da15bd9dSWang Nan } 3797