150656eecSMasami Hiramatsu /* 20e60836bSSrikar Dronamraju * probe-event.c : perf-probe definition to probe_events format converter 350656eecSMasami Hiramatsu * 450656eecSMasami Hiramatsu * Written by Masami Hiramatsu <mhiramat@redhat.com> 550656eecSMasami Hiramatsu * 650656eecSMasami Hiramatsu * This program is free software; you can redistribute it and/or modify 750656eecSMasami Hiramatsu * it under the terms of the GNU General Public License as published by 850656eecSMasami Hiramatsu * the Free Software Foundation; either version 2 of the License, or 950656eecSMasami Hiramatsu * (at your option) any later version. 1050656eecSMasami Hiramatsu * 1150656eecSMasami Hiramatsu * This program is distributed in the hope that it will be useful, 1250656eecSMasami Hiramatsu * but WITHOUT ANY WARRANTY; without even the implied warranty of 1350656eecSMasami Hiramatsu * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1450656eecSMasami Hiramatsu * GNU General Public License for more details. 1550656eecSMasami Hiramatsu * 1650656eecSMasami Hiramatsu * You should have received a copy of the GNU General Public License 1750656eecSMasami Hiramatsu * along with this program; if not, write to the Free Software 1850656eecSMasami Hiramatsu * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1950656eecSMasami Hiramatsu * 2050656eecSMasami Hiramatsu */ 2150656eecSMasami Hiramatsu 2250656eecSMasami Hiramatsu #include <sys/utsname.h> 2350656eecSMasami Hiramatsu #include <sys/types.h> 2450656eecSMasami Hiramatsu #include <sys/stat.h> 2550656eecSMasami Hiramatsu #include <fcntl.h> 2650656eecSMasami Hiramatsu #include <errno.h> 2750656eecSMasami Hiramatsu #include <stdio.h> 2850656eecSMasami Hiramatsu #include <unistd.h> 2950656eecSMasami Hiramatsu #include <stdlib.h> 3050656eecSMasami Hiramatsu #include <string.h> 314de189feSMasami Hiramatsu #include <stdarg.h> 324de189feSMasami Hiramatsu #include <limits.h> 33e80711caSMasami Hiramatsu #include <elf.h> 3450656eecSMasami Hiramatsu 3531facc5fSMasami Hiramatsu #include "util.h" 3650656eecSMasami Hiramatsu #include "event.h" 374de189feSMasami Hiramatsu #include "strlist.h" 3850656eecSMasami Hiramatsu #include "debug.h" 3972041334SMasami Hiramatsu #include "cache.h" 40631c9defSMasami Hiramatsu #include "color.h" 41e0faa8d3SMasami Hiramatsu #include "symbol.h" 42e0faa8d3SMasami Hiramatsu #include "thread.h" 434605eab3SJiri Olsa #include <api/fs/fs.h> 441d037ca1SIrina Tirdea #include "trace-event.h" /* For __maybe_unused */ 4550656eecSMasami Hiramatsu #include "probe-event.h" 464235b045SMasami Hiramatsu #include "probe-finder.h" 4792f6c72eSMasami Hiramatsu #include "probe-file.h" 48225466f1SSrikar Dronamraju #include "session.h" 4950656eecSMasami Hiramatsu 5050656eecSMasami Hiramatsu #define MAX_CMDLEN 256 5150656eecSMasami Hiramatsu #define PERFPROBE_GROUP "probe" 5250656eecSMasami Hiramatsu 53f4d7da49SMasami Hiramatsu bool probe_event_dry_run; /* Dry run flag */ 54ddb2f58fSMasami Hiramatsu struct probe_conf probe_conf; 55f4d7da49SMasami Hiramatsu 56146a1439SMasami Hiramatsu #define semantic_error(msg ...) pr_err("Semantic error :" msg) 5750656eecSMasami Hiramatsu 5892f6c72eSMasami Hiramatsu int e_snprintf(char *str, size_t size, const char *format, ...) 594de189feSMasami Hiramatsu { 604de189feSMasami Hiramatsu int ret; 614de189feSMasami Hiramatsu va_list ap; 624de189feSMasami Hiramatsu va_start(ap, format); 634de189feSMasami Hiramatsu ret = vsnprintf(str, size, format, ap); 644de189feSMasami Hiramatsu va_end(ap); 654de189feSMasami Hiramatsu if (ret >= (int)size) 664de189feSMasami Hiramatsu ret = -E2BIG; 674de189feSMasami Hiramatsu return ret; 684de189feSMasami Hiramatsu } 694de189feSMasami Hiramatsu 70ee45b6c2SMasami Hiramatsu static struct machine *host_machine; 71e0faa8d3SMasami Hiramatsu 72469b9b88SMasami Hiramatsu /* Initialize symbol maps and path of vmlinux/modules */ 739bae1e8cSNamhyung Kim int init_probe_symbol_maps(bool user_only) 74e0faa8d3SMasami Hiramatsu { 75146a1439SMasami Hiramatsu int ret; 76146a1439SMasami Hiramatsu 77e0faa8d3SMasami Hiramatsu symbol_conf.sort_by_name = true; 78680d926aSNamhyung Kim symbol_conf.allow_aliases = true; 790a7e6d1bSNamhyung Kim ret = symbol__init(NULL); 80146a1439SMasami Hiramatsu if (ret < 0) { 81146a1439SMasami Hiramatsu pr_debug("Failed to init symbol map.\n"); 82146a1439SMasami Hiramatsu goto out; 83146a1439SMasami Hiramatsu } 84e0faa8d3SMasami Hiramatsu 85ee45b6c2SMasami Hiramatsu if (host_machine || user_only) /* already initialized */ 86ee45b6c2SMasami Hiramatsu return 0; 87d28c6223SArnaldo Carvalho de Melo 88ee45b6c2SMasami Hiramatsu if (symbol_conf.vmlinux_name) 89ee45b6c2SMasami Hiramatsu pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 90ee45b6c2SMasami Hiramatsu 91ee45b6c2SMasami Hiramatsu host_machine = machine__new_host(); 92ee45b6c2SMasami Hiramatsu if (!host_machine) { 93ee45b6c2SMasami Hiramatsu pr_debug("machine__new_host() failed.\n"); 94ee45b6c2SMasami Hiramatsu symbol__exit(); 95ee45b6c2SMasami Hiramatsu ret = -1; 96469b9b88SMasami Hiramatsu } 97146a1439SMasami Hiramatsu out: 98146a1439SMasami Hiramatsu if (ret < 0) 99146a1439SMasami Hiramatsu pr_warning("Failed to init vmlinux path.\n"); 100146a1439SMasami Hiramatsu return ret; 101e0faa8d3SMasami Hiramatsu } 102e0faa8d3SMasami Hiramatsu 1039bae1e8cSNamhyung Kim void exit_probe_symbol_maps(void) 104ee45b6c2SMasami Hiramatsu { 105ee45b6c2SMasami Hiramatsu machine__delete(host_machine); 106ee45b6c2SMasami Hiramatsu host_machine = NULL; 107ee45b6c2SMasami Hiramatsu symbol__exit(); 108ee45b6c2SMasami Hiramatsu } 109ee45b6c2SMasami Hiramatsu 110469b9b88SMasami Hiramatsu static struct symbol *__find_kernel_function_by_name(const char *name, 111469b9b88SMasami Hiramatsu struct map **mapp) 112e0faa8d3SMasami Hiramatsu { 113be39db9fSArnaldo Carvalho de Melo return machine__find_kernel_function_by_name(host_machine, name, mapp); 114e0faa8d3SMasami Hiramatsu } 115469b9b88SMasami Hiramatsu 1168f33f7deSMasami Hiramatsu static struct symbol *__find_kernel_function(u64 addr, struct map **mapp) 1178f33f7deSMasami Hiramatsu { 118be39db9fSArnaldo Carvalho de Melo return machine__find_kernel_function(host_machine, addr, mapp); 1198f33f7deSMasami Hiramatsu } 1208f33f7deSMasami Hiramatsu 1218f33f7deSMasami Hiramatsu static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void) 1228f33f7deSMasami Hiramatsu { 1238f33f7deSMasami Hiramatsu /* kmap->ref_reloc_sym should be set if host_machine is initialized */ 1248f33f7deSMasami Hiramatsu struct kmap *kmap; 125a5e813c6SArnaldo Carvalho de Melo struct map *map = machine__kernel_map(host_machine); 1268f33f7deSMasami Hiramatsu 127be39db9fSArnaldo Carvalho de Melo if (map__load(map) < 0) 1288f33f7deSMasami Hiramatsu return NULL; 1298f33f7deSMasami Hiramatsu 13077e65977SArnaldo Carvalho de Melo kmap = map__kmap(map); 131ba92732eSWang Nan if (!kmap) 132ba92732eSWang Nan return NULL; 1338f33f7deSMasami Hiramatsu return kmap->ref_reloc_sym; 1348f33f7deSMasami Hiramatsu } 1358f33f7deSMasami Hiramatsu 1369b239a12SMasami Hiramatsu static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, 1379b239a12SMasami Hiramatsu bool reloc, bool reladdr) 1388f33f7deSMasami Hiramatsu { 1398f33f7deSMasami Hiramatsu struct ref_reloc_sym *reloc_sym; 1408f33f7deSMasami Hiramatsu struct symbol *sym; 1418f33f7deSMasami Hiramatsu struct map *map; 1428f33f7deSMasami Hiramatsu 1438f33f7deSMasami Hiramatsu /* ref_reloc_sym is just a label. Need a special fix*/ 1448f33f7deSMasami Hiramatsu reloc_sym = kernel_get_ref_reloc_sym(); 1458f33f7deSMasami Hiramatsu if (reloc_sym && strcmp(name, reloc_sym->name) == 0) 1469b239a12SMasami Hiramatsu *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; 1478f33f7deSMasami Hiramatsu else { 1488f33f7deSMasami Hiramatsu sym = __find_kernel_function_by_name(name, &map); 1499b239a12SMasami Hiramatsu if (!sym) 1509b239a12SMasami Hiramatsu return -ENOENT; 1519b239a12SMasami Hiramatsu *addr = map->unmap_ip(map, sym->start) - 1529b239a12SMasami Hiramatsu ((reloc) ? 0 : map->reloc) - 1539b239a12SMasami Hiramatsu ((reladdr) ? map->start : 0); 1548f33f7deSMasami Hiramatsu } 1558f33f7deSMasami Hiramatsu return 0; 1568f33f7deSMasami Hiramatsu } 1578f33f7deSMasami Hiramatsu 158e80711caSMasami Hiramatsu static struct map *kernel_get_module_map(const char *module) 159e80711caSMasami Hiramatsu { 160ee45b6c2SMasami Hiramatsu struct map_groups *grp = &host_machine->kmaps; 1611eee78aeSArnaldo Carvalho de Melo struct maps *maps = &grp->maps[MAP__FUNCTION]; 1624bb7123dSArnaldo Carvalho de Melo struct map *pos; 163e80711caSMasami Hiramatsu 16414a8fd7cSMasami Hiramatsu /* A file path -- this is an offline module */ 16514a8fd7cSMasami Hiramatsu if (module && strchr(module, '/')) 166eebc509bSMasami Hiramatsu return dso__new_map(module); 16714a8fd7cSMasami Hiramatsu 168e80711caSMasami Hiramatsu if (!module) 169e80711caSMasami Hiramatsu module = "kernel"; 170e80711caSMasami Hiramatsu 1714bb7123dSArnaldo Carvalho de Melo for (pos = maps__first(maps); pos; pos = map__next(pos)) { 172cb3f3378SKonstantin Khlebnikov /* short_name is "[module]" */ 173e80711caSMasami Hiramatsu if (strncmp(pos->dso->short_name + 1, module, 174cb3f3378SKonstantin Khlebnikov pos->dso->short_name_len - 2) == 0 && 175cb3f3378SKonstantin Khlebnikov module[pos->dso->short_name_len - 2] == '\0') { 176eebc509bSMasami Hiramatsu map__get(pos); 177e80711caSMasami Hiramatsu return pos; 178e80711caSMasami Hiramatsu } 179e80711caSMasami Hiramatsu } 180e80711caSMasami Hiramatsu return NULL; 181e80711caSMasami Hiramatsu } 182e80711caSMasami Hiramatsu 18399e608b5SRavi Bangoria struct map *get_target_map(const char *target, bool user) 1849b118acaSMasami Hiramatsu { 1859b118acaSMasami Hiramatsu /* Init maps of given executable or kernel */ 1869b118acaSMasami Hiramatsu if (user) 1879b118acaSMasami Hiramatsu return dso__new_map(target); 1889b118acaSMasami Hiramatsu else 1899b118acaSMasami Hiramatsu return kernel_get_module_map(target); 1909b118acaSMasami Hiramatsu } 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 { 2299b118acaSMasami Hiramatsu free(pp->file); 2309b118acaSMasami Hiramatsu free(pp->function); 2319b118acaSMasami Hiramatsu free(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 242b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address); 243b031220dSMasami Hiramatsu static bool kprobe_warn_out_range(const char *symbol, unsigned long address) 244b031220dSMasami Hiramatsu { 2459b239a12SMasami Hiramatsu u64 etext_addr = 0; 2469b239a12SMasami Hiramatsu int ret; 2477c31bb8cSHe Kuang 248b031220dSMasami Hiramatsu /* Get the address of _etext for checking non-probable text symbol */ 2499b239a12SMasami Hiramatsu ret = kernel_get_symbol_address_by_name("_etext", &etext_addr, 2509b239a12SMasami Hiramatsu false, false); 2517c31bb8cSHe Kuang 2529b239a12SMasami Hiramatsu if (ret == 0 && etext_addr < address) 253b031220dSMasami Hiramatsu pr_warning("%s is out of .text, skip it.\n", symbol); 254b031220dSMasami Hiramatsu else if (kprobe_blacklist__listed(address)) 255b031220dSMasami Hiramatsu pr_warning("%s is blacklisted function, skip it.\n", symbol); 256b031220dSMasami Hiramatsu else 257b031220dSMasami Hiramatsu return false; 258b031220dSMasami Hiramatsu 259b031220dSMasami Hiramatsu return true; 260b031220dSMasami Hiramatsu } 261b031220dSMasami Hiramatsu 262c61fb959SRavi Bangoria /* 263c61fb959SRavi Bangoria * @module can be module name of module file path. In case of path, 264c61fb959SRavi Bangoria * inspect elf and find out what is actual module name. 265c61fb959SRavi Bangoria * Caller has to free mod_name after using it. 266c61fb959SRavi Bangoria */ 267c61fb959SRavi Bangoria static char *find_module_name(const char *module) 268c61fb959SRavi Bangoria { 269c61fb959SRavi Bangoria int fd; 270c61fb959SRavi Bangoria Elf *elf; 271c61fb959SRavi Bangoria GElf_Ehdr ehdr; 272c61fb959SRavi Bangoria GElf_Shdr shdr; 273c61fb959SRavi Bangoria Elf_Data *data; 274c61fb959SRavi Bangoria Elf_Scn *sec; 275c61fb959SRavi Bangoria char *mod_name = NULL; 2761f2ed153SMasami Hiramatsu int name_offset; 277c61fb959SRavi Bangoria 278c61fb959SRavi Bangoria fd = open(module, O_RDONLY); 279c61fb959SRavi Bangoria if (fd < 0) 280c61fb959SRavi Bangoria return NULL; 281c61fb959SRavi Bangoria 282c61fb959SRavi Bangoria elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 283c61fb959SRavi Bangoria if (elf == NULL) 284c61fb959SRavi Bangoria goto elf_err; 285c61fb959SRavi Bangoria 286c61fb959SRavi Bangoria if (gelf_getehdr(elf, &ehdr) == NULL) 287c61fb959SRavi Bangoria goto ret_err; 288c61fb959SRavi Bangoria 289c61fb959SRavi Bangoria sec = elf_section_by_name(elf, &ehdr, &shdr, 290c61fb959SRavi Bangoria ".gnu.linkonce.this_module", NULL); 291c61fb959SRavi Bangoria if (!sec) 292c61fb959SRavi Bangoria goto ret_err; 293c61fb959SRavi Bangoria 294c61fb959SRavi Bangoria data = elf_getdata(sec, NULL); 295c61fb959SRavi Bangoria if (!data || !data->d_buf) 296c61fb959SRavi Bangoria goto ret_err; 297c61fb959SRavi Bangoria 2981f2ed153SMasami Hiramatsu /* 2991f2ed153SMasami Hiramatsu * NOTE: 3001f2ed153SMasami Hiramatsu * '.gnu.linkonce.this_module' section of kernel module elf directly 3011f2ed153SMasami Hiramatsu * maps to 'struct module' from linux/module.h. This section contains 3021f2ed153SMasami Hiramatsu * actual module name which will be used by kernel after loading it. 3031f2ed153SMasami Hiramatsu * But, we cannot use 'struct module' here since linux/module.h is not 3041f2ed153SMasami Hiramatsu * exposed to user-space. Offset of 'name' has remained same from long 3051f2ed153SMasami Hiramatsu * time, so hardcoding it here. 3061f2ed153SMasami Hiramatsu */ 3071f2ed153SMasami Hiramatsu if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 3081f2ed153SMasami Hiramatsu name_offset = 12; 3091f2ed153SMasami Hiramatsu else /* expect ELFCLASS64 by default */ 3101f2ed153SMasami Hiramatsu name_offset = 24; 3111f2ed153SMasami Hiramatsu 3121f2ed153SMasami Hiramatsu mod_name = strdup((char *)data->d_buf + name_offset); 313c61fb959SRavi Bangoria 314c61fb959SRavi Bangoria ret_err: 315c61fb959SRavi Bangoria elf_end(elf); 316c61fb959SRavi Bangoria elf_err: 317c61fb959SRavi Bangoria close(fd); 318c61fb959SRavi Bangoria return mod_name; 319c61fb959SRavi Bangoria } 320c61fb959SRavi Bangoria 32189fe808aSIngo Molnar #ifdef HAVE_DWARF_SUPPORT 32260fb7742SWang Nan 32360fb7742SWang Nan static int kernel_get_module_dso(const char *module, struct dso **pdso) 32460fb7742SWang Nan { 32560fb7742SWang Nan struct dso *dso; 32660fb7742SWang Nan struct map *map; 32760fb7742SWang Nan const char *vmlinux_name; 32860fb7742SWang Nan int ret = 0; 32960fb7742SWang Nan 33060fb7742SWang Nan if (module) { 331266fa2b2SArnaldo Carvalho de Melo char module_name[128]; 332266fa2b2SArnaldo Carvalho de Melo 333266fa2b2SArnaldo Carvalho de Melo snprintf(module_name, sizeof(module_name), "[%s]", module); 334266fa2b2SArnaldo Carvalho de Melo map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name); 335266fa2b2SArnaldo Carvalho de Melo if (map) { 336266fa2b2SArnaldo Carvalho de Melo dso = map->dso; 33760fb7742SWang Nan goto found; 33860fb7742SWang Nan } 33960fb7742SWang Nan pr_debug("Failed to find module %s.\n", module); 34060fb7742SWang Nan return -ENOENT; 34160fb7742SWang Nan } 34260fb7742SWang Nan 343a5e813c6SArnaldo Carvalho de Melo map = machine__kernel_map(host_machine); 34460fb7742SWang Nan dso = map->dso; 34560fb7742SWang Nan 34660fb7742SWang Nan vmlinux_name = symbol_conf.vmlinux_name; 34760fb7742SWang Nan dso->load_errno = 0; 34860fb7742SWang Nan if (vmlinux_name) 349be39db9fSArnaldo Carvalho de Melo ret = dso__load_vmlinux(dso, map, vmlinux_name, false); 35060fb7742SWang Nan else 351be39db9fSArnaldo Carvalho de Melo ret = dso__load_vmlinux_path(dso, map); 35260fb7742SWang Nan found: 35360fb7742SWang Nan *pdso = dso; 35460fb7742SWang Nan return ret; 35560fb7742SWang Nan } 35660fb7742SWang Nan 3579b118acaSMasami Hiramatsu /* 3589b118acaSMasami Hiramatsu * Some binaries like glibc have special symbols which are on the symbol 3599b118acaSMasami Hiramatsu * table, but not in the debuginfo. If we can find the address of the 3609b118acaSMasami Hiramatsu * symbol from map, we can translate the address back to the probe point. 3619b118acaSMasami Hiramatsu */ 3629b118acaSMasami Hiramatsu static int find_alternative_probe_point(struct debuginfo *dinfo, 3639b118acaSMasami Hiramatsu struct perf_probe_point *pp, 3649b118acaSMasami Hiramatsu struct perf_probe_point *result, 3659b118acaSMasami Hiramatsu const char *target, bool uprobes) 3669b118acaSMasami Hiramatsu { 3679b118acaSMasami Hiramatsu struct map *map = NULL; 3689b118acaSMasami Hiramatsu struct symbol *sym; 3699b118acaSMasami Hiramatsu u64 address = 0; 3709b118acaSMasami Hiramatsu int ret = -ENOENT; 3719b118acaSMasami Hiramatsu 3729b118acaSMasami Hiramatsu /* This can work only for function-name based one */ 3739b118acaSMasami Hiramatsu if (!pp->function || pp->file) 3749b118acaSMasami Hiramatsu return -ENOTSUP; 3759b118acaSMasami Hiramatsu 3769b118acaSMasami Hiramatsu map = get_target_map(target, uprobes); 3779b118acaSMasami Hiramatsu if (!map) 3789b118acaSMasami Hiramatsu return -EINVAL; 3799b118acaSMasami Hiramatsu 3809b118acaSMasami Hiramatsu /* Find the address of given function */ 3819b118acaSMasami Hiramatsu map__for_each_symbol_by_name(map, pp->function, sym) { 382e6d7c91cSMasami Hiramatsu if (uprobes) 3839b118acaSMasami Hiramatsu address = sym->start; 384e6d7c91cSMasami Hiramatsu else 3858e34189bSMasami Hiramatsu address = map->unmap_ip(map, sym->start) - map->reloc; 3869b118acaSMasami Hiramatsu break; 3879b118acaSMasami Hiramatsu } 3889b118acaSMasami Hiramatsu if (!address) { 3899b118acaSMasami Hiramatsu ret = -ENOENT; 3909b118acaSMasami Hiramatsu goto out; 3919b118acaSMasami Hiramatsu } 392f6c15621SWang Nan pr_debug("Symbol %s address found : %" PRIx64 "\n", 393f6c15621SWang Nan pp->function, address); 3949b118acaSMasami Hiramatsu 3959b118acaSMasami Hiramatsu ret = debuginfo__find_probe_point(dinfo, (unsigned long)address, 3969b118acaSMasami Hiramatsu result); 3979b118acaSMasami Hiramatsu if (ret <= 0) 3989b118acaSMasami Hiramatsu ret = (!ret) ? -ENOENT : ret; 3999b118acaSMasami Hiramatsu else { 4009b118acaSMasami Hiramatsu result->offset += pp->offset; 4019b118acaSMasami Hiramatsu result->line += pp->line; 4029d7b45c5SHe Kuang result->retprobe = pp->retprobe; 4039b118acaSMasami Hiramatsu ret = 0; 4049b118acaSMasami Hiramatsu } 4059b118acaSMasami Hiramatsu 4069b118acaSMasami Hiramatsu out: 407eebc509bSMasami Hiramatsu map__put(map); 4089b118acaSMasami Hiramatsu return ret; 4099b118acaSMasami Hiramatsu 4109b118acaSMasami Hiramatsu } 4119b118acaSMasami Hiramatsu 4129b118acaSMasami Hiramatsu static int get_alternative_probe_event(struct debuginfo *dinfo, 4139b118acaSMasami Hiramatsu struct perf_probe_event *pev, 41444225521SMasami Hiramatsu struct perf_probe_point *tmp) 4159b118acaSMasami Hiramatsu { 4169b118acaSMasami Hiramatsu int ret; 4179b118acaSMasami Hiramatsu 4189b118acaSMasami Hiramatsu memcpy(tmp, &pev->point, sizeof(*tmp)); 4199b118acaSMasami Hiramatsu memset(&pev->point, 0, sizeof(pev->point)); 4209b118acaSMasami Hiramatsu ret = find_alternative_probe_point(dinfo, tmp, &pev->point, 42144225521SMasami Hiramatsu pev->target, pev->uprobes); 4229b118acaSMasami Hiramatsu if (ret < 0) 4239b118acaSMasami Hiramatsu memcpy(&pev->point, tmp, sizeof(*tmp)); 4249b118acaSMasami Hiramatsu 4259b118acaSMasami Hiramatsu return ret; 4269b118acaSMasami Hiramatsu } 427a15ad2f5SMasami Hiramatsu 428811dd2aeSMasami Hiramatsu static int get_alternative_line_range(struct debuginfo *dinfo, 429811dd2aeSMasami Hiramatsu struct line_range *lr, 430811dd2aeSMasami Hiramatsu const char *target, bool user) 431811dd2aeSMasami Hiramatsu { 4326d4a4896SDavid Ahern struct perf_probe_point pp = { .function = lr->function, 4336d4a4896SDavid Ahern .file = lr->file, 4346d4a4896SDavid Ahern .line = lr->start }; 4356d4a4896SDavid Ahern struct perf_probe_point result; 436811dd2aeSMasami Hiramatsu int ret, len = 0; 437811dd2aeSMasami Hiramatsu 4386d4a4896SDavid Ahern memset(&result, 0, sizeof(result)); 4396d4a4896SDavid Ahern 440811dd2aeSMasami Hiramatsu if (lr->end != INT_MAX) 441811dd2aeSMasami Hiramatsu len = lr->end - lr->start; 442811dd2aeSMasami Hiramatsu ret = find_alternative_probe_point(dinfo, &pp, &result, 443811dd2aeSMasami Hiramatsu target, user); 444811dd2aeSMasami Hiramatsu if (!ret) { 445811dd2aeSMasami Hiramatsu lr->function = result.function; 446811dd2aeSMasami Hiramatsu lr->file = result.file; 447811dd2aeSMasami Hiramatsu lr->start = result.line; 448811dd2aeSMasami Hiramatsu if (lr->end != INT_MAX) 449811dd2aeSMasami Hiramatsu lr->end = lr->start + len; 450811dd2aeSMasami Hiramatsu clear_perf_probe_point(&pp); 451811dd2aeSMasami Hiramatsu } 452811dd2aeSMasami Hiramatsu return ret; 453811dd2aeSMasami Hiramatsu } 454811dd2aeSMasami Hiramatsu 455ff741783SMasami Hiramatsu /* Open new debuginfo of given module */ 45692561cb7SMasami Hiramatsu static struct debuginfo *open_debuginfo(const char *module, bool silent) 457469b9b88SMasami Hiramatsu { 458a15ad2f5SMasami Hiramatsu const char *path = module; 459419e8738SMasami Hiramatsu char reason[STRERR_BUFSIZE]; 460419e8738SMasami Hiramatsu struct debuginfo *ret = NULL; 461419e8738SMasami Hiramatsu struct dso *dso = NULL; 462419e8738SMasami Hiramatsu int err; 46314a8fd7cSMasami Hiramatsu 464a15ad2f5SMasami Hiramatsu if (!module || !strchr(module, '/')) { 465419e8738SMasami Hiramatsu err = kernel_get_module_dso(module, &dso); 466419e8738SMasami Hiramatsu if (err < 0) { 467419e8738SMasami Hiramatsu if (!dso || dso->load_errno == 0) { 468c8b5f2c9SArnaldo Carvalho de Melo if (!str_error_r(-err, reason, STRERR_BUFSIZE)) 469419e8738SMasami Hiramatsu strcpy(reason, "(unknown)"); 470419e8738SMasami Hiramatsu } else 471419e8738SMasami Hiramatsu dso__strerror_load(dso, reason, STRERR_BUFSIZE); 47292561cb7SMasami Hiramatsu if (!silent) 473419e8738SMasami Hiramatsu pr_err("Failed to find the path for %s: %s\n", 474419e8738SMasami Hiramatsu module ?: "kernel", reason); 475ff741783SMasami Hiramatsu return NULL; 476469b9b88SMasami Hiramatsu } 477419e8738SMasami Hiramatsu path = dso->long_name; 47814a8fd7cSMasami Hiramatsu } 47992561cb7SMasami Hiramatsu ret = debuginfo__new(path); 48092561cb7SMasami Hiramatsu if (!ret && !silent) { 48192561cb7SMasami Hiramatsu pr_warning("The %s file has no debug information.\n", path); 48292561cb7SMasami Hiramatsu if (!module || !strtailcmp(path, ".ko")) 48392561cb7SMasami Hiramatsu pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); 48492561cb7SMasami Hiramatsu else 48592561cb7SMasami Hiramatsu pr_warning("Rebuild with -g, "); 48692561cb7SMasami Hiramatsu pr_warning("or install an appropriate debuginfo package.\n"); 487e0faa8d3SMasami Hiramatsu } 48892561cb7SMasami Hiramatsu return ret; 48992561cb7SMasami Hiramatsu } 49092561cb7SMasami Hiramatsu 4917737af01SMasami Hiramatsu /* For caching the last debuginfo */ 4927737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache; 4937737af01SMasami Hiramatsu static char *debuginfo_cache_path; 4947737af01SMasami Hiramatsu 4957737af01SMasami Hiramatsu static struct debuginfo *debuginfo_cache__open(const char *module, bool silent) 4967737af01SMasami Hiramatsu { 49720f49859SMasami Hiramatsu const char *path = module; 49820f49859SMasami Hiramatsu 49920f49859SMasami Hiramatsu /* If the module is NULL, it should be the kernel. */ 50020f49859SMasami Hiramatsu if (!module) 50120f49859SMasami Hiramatsu path = "kernel"; 50220f49859SMasami Hiramatsu 50320f49859SMasami Hiramatsu if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path)) 5047737af01SMasami Hiramatsu goto out; 5057737af01SMasami Hiramatsu 5067737af01SMasami Hiramatsu /* Copy module path */ 5077737af01SMasami Hiramatsu free(debuginfo_cache_path); 50820f49859SMasami Hiramatsu debuginfo_cache_path = strdup(path); 5097737af01SMasami Hiramatsu if (!debuginfo_cache_path) { 5107737af01SMasami Hiramatsu debuginfo__delete(debuginfo_cache); 5117737af01SMasami Hiramatsu debuginfo_cache = NULL; 5127737af01SMasami Hiramatsu goto out; 5137737af01SMasami Hiramatsu } 5147737af01SMasami Hiramatsu 5157737af01SMasami Hiramatsu debuginfo_cache = open_debuginfo(module, silent); 5167737af01SMasami Hiramatsu if (!debuginfo_cache) 5177737af01SMasami Hiramatsu zfree(&debuginfo_cache_path); 5187737af01SMasami Hiramatsu out: 5197737af01SMasami Hiramatsu return debuginfo_cache; 5207737af01SMasami Hiramatsu } 5217737af01SMasami Hiramatsu 5227737af01SMasami Hiramatsu static void debuginfo_cache__exit(void) 5237737af01SMasami Hiramatsu { 5247737af01SMasami Hiramatsu debuginfo__delete(debuginfo_cache); 5257737af01SMasami Hiramatsu debuginfo_cache = NULL; 5267737af01SMasami Hiramatsu zfree(&debuginfo_cache_path); 5277737af01SMasami Hiramatsu } 5287737af01SMasami Hiramatsu 5294b4da7f7SMasami Hiramatsu 53099ca4233SMasami Hiramatsu static int get_text_start_address(const char *exec, unsigned long *address) 53199ca4233SMasami Hiramatsu { 53299ca4233SMasami Hiramatsu Elf *elf; 53399ca4233SMasami Hiramatsu GElf_Ehdr ehdr; 53499ca4233SMasami Hiramatsu GElf_Shdr shdr; 53599ca4233SMasami Hiramatsu int fd, ret = -ENOENT; 53699ca4233SMasami Hiramatsu 53799ca4233SMasami Hiramatsu fd = open(exec, O_RDONLY); 53899ca4233SMasami Hiramatsu if (fd < 0) 53999ca4233SMasami Hiramatsu return -errno; 54099ca4233SMasami Hiramatsu 54199ca4233SMasami Hiramatsu elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 542062d6c2aSMasami Hiramatsu if (elf == NULL) { 543062d6c2aSMasami Hiramatsu ret = -EINVAL; 544062d6c2aSMasami Hiramatsu goto out_close; 545062d6c2aSMasami Hiramatsu } 54699ca4233SMasami Hiramatsu 54799ca4233SMasami Hiramatsu if (gelf_getehdr(elf, &ehdr) == NULL) 54899ca4233SMasami Hiramatsu goto out; 54999ca4233SMasami Hiramatsu 55099ca4233SMasami Hiramatsu if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) 55199ca4233SMasami Hiramatsu goto out; 55299ca4233SMasami Hiramatsu 55399ca4233SMasami Hiramatsu *address = shdr.sh_addr - shdr.sh_offset; 55499ca4233SMasami Hiramatsu ret = 0; 55599ca4233SMasami Hiramatsu out: 55699ca4233SMasami Hiramatsu elf_end(elf); 557062d6c2aSMasami Hiramatsu out_close: 558062d6c2aSMasami Hiramatsu close(fd); 559062d6c2aSMasami Hiramatsu 56099ca4233SMasami Hiramatsu return ret; 56199ca4233SMasami Hiramatsu } 56299ca4233SMasami Hiramatsu 5635a6f6314SMasami Hiramatsu /* 5645a6f6314SMasami Hiramatsu * Convert trace point to probe point with debuginfo 5655a6f6314SMasami Hiramatsu */ 5665a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, 5675a6f6314SMasami Hiramatsu struct perf_probe_point *pp, 5685a6f6314SMasami Hiramatsu bool is_kprobe) 5695a6f6314SMasami Hiramatsu { 5705a6f6314SMasami Hiramatsu struct debuginfo *dinfo = NULL; 5715a6f6314SMasami Hiramatsu unsigned long stext = 0; 5725a6f6314SMasami Hiramatsu u64 addr = tp->address; 5735a6f6314SMasami Hiramatsu int ret = -ENOENT; 5745a6f6314SMasami Hiramatsu 5755a6f6314SMasami Hiramatsu /* convert the address to dwarf address */ 5765a6f6314SMasami Hiramatsu if (!is_kprobe) { 5775a6f6314SMasami Hiramatsu if (!addr) { 5785a6f6314SMasami Hiramatsu ret = -EINVAL; 5795a6f6314SMasami Hiramatsu goto error; 5805a6f6314SMasami Hiramatsu } 5815a6f6314SMasami Hiramatsu ret = get_text_start_address(tp->module, &stext); 5825a6f6314SMasami Hiramatsu if (ret < 0) 5835a6f6314SMasami Hiramatsu goto error; 5845a6f6314SMasami Hiramatsu addr += stext; 585e486367fSWang Nan } else if (tp->symbol) { 5869b239a12SMasami Hiramatsu /* If the module is given, this returns relative address */ 5879b239a12SMasami Hiramatsu ret = kernel_get_symbol_address_by_name(tp->symbol, &addr, 5889b239a12SMasami Hiramatsu false, !!tp->module); 5899b239a12SMasami Hiramatsu if (ret != 0) 5905a6f6314SMasami Hiramatsu goto error; 5915a6f6314SMasami Hiramatsu addr += tp->offset; 5925a6f6314SMasami Hiramatsu } 5935a6f6314SMasami Hiramatsu 5945a6f6314SMasami Hiramatsu pr_debug("try to find information at %" PRIx64 " in %s\n", addr, 5955a6f6314SMasami Hiramatsu tp->module ? : "kernel"); 5965a6f6314SMasami Hiramatsu 5977737af01SMasami Hiramatsu dinfo = debuginfo_cache__open(tp->module, verbose == 0); 5987737af01SMasami Hiramatsu if (dinfo) 5995a6f6314SMasami Hiramatsu ret = debuginfo__find_probe_point(dinfo, 6005a6f6314SMasami Hiramatsu (unsigned long)addr, pp); 6017737af01SMasami Hiramatsu else 6025a6f6314SMasami Hiramatsu ret = -ENOENT; 6035a6f6314SMasami Hiramatsu 6045a6f6314SMasami Hiramatsu if (ret > 0) { 6055a6f6314SMasami Hiramatsu pp->retprobe = tp->retprobe; 6065a6f6314SMasami Hiramatsu return 0; 6075a6f6314SMasami Hiramatsu } 6085a6f6314SMasami Hiramatsu error: 6095a6f6314SMasami Hiramatsu pr_debug("Failed to find corresponding probes from debuginfo.\n"); 6105a6f6314SMasami Hiramatsu return ret ? : -ENOENT; 6115a6f6314SMasami Hiramatsu } 6125a6f6314SMasami Hiramatsu 613*3e96dac7SMasami Hiramatsu /* Adjust symbol name and address */ 614*3e96dac7SMasami Hiramatsu static int post_process_probe_trace_point(struct probe_trace_point *tp, 615*3e96dac7SMasami Hiramatsu struct map *map, unsigned long offs) 616*3e96dac7SMasami Hiramatsu { 617*3e96dac7SMasami Hiramatsu struct symbol *sym; 618*3e96dac7SMasami Hiramatsu u64 addr = tp->address + tp->offset - offs; 619*3e96dac7SMasami Hiramatsu 620*3e96dac7SMasami Hiramatsu sym = map__find_symbol(map, addr); 621*3e96dac7SMasami Hiramatsu if (!sym) 622*3e96dac7SMasami Hiramatsu return -ENOENT; 623*3e96dac7SMasami Hiramatsu 624*3e96dac7SMasami Hiramatsu if (strcmp(sym->name, tp->symbol)) { 625*3e96dac7SMasami Hiramatsu /* If we have no realname, use symbol for it */ 626*3e96dac7SMasami Hiramatsu if (!tp->realname) 627*3e96dac7SMasami Hiramatsu tp->realname = tp->symbol; 628*3e96dac7SMasami Hiramatsu else 629*3e96dac7SMasami Hiramatsu free(tp->symbol); 630*3e96dac7SMasami Hiramatsu tp->symbol = strdup(sym->name); 631*3e96dac7SMasami Hiramatsu if (!tp->symbol) 632*3e96dac7SMasami Hiramatsu return -ENOMEM; 633*3e96dac7SMasami Hiramatsu } 634*3e96dac7SMasami Hiramatsu tp->offset = addr - sym->start; 635*3e96dac7SMasami Hiramatsu tp->address -= offs; 636*3e96dac7SMasami Hiramatsu 637*3e96dac7SMasami Hiramatsu return 0; 638*3e96dac7SMasami Hiramatsu } 639*3e96dac7SMasami Hiramatsu 6408a937a25SMasami Hiramatsu /* 6418a937a25SMasami Hiramatsu * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions 6428a937a25SMasami Hiramatsu * and generate new symbols with suffixes such as .constprop.N or .isra.N 6438a937a25SMasami Hiramatsu * etc. Since those symbols are not recorded in DWARF, we have to find 6448a937a25SMasami Hiramatsu * correct generated symbols from offline ELF binary. 6458a937a25SMasami Hiramatsu * For online kernel or uprobes we don't need this because those are 6468a937a25SMasami Hiramatsu * rebased on _text, or already a section relative address. 6478a937a25SMasami Hiramatsu */ 6488a937a25SMasami Hiramatsu static int 6498a937a25SMasami Hiramatsu post_process_offline_probe_trace_events(struct probe_trace_event *tevs, 6508a937a25SMasami Hiramatsu int ntevs, const char *pathname) 6518a937a25SMasami Hiramatsu { 6528a937a25SMasami Hiramatsu struct map *map; 6538a937a25SMasami Hiramatsu unsigned long stext = 0; 654*3e96dac7SMasami Hiramatsu int i, ret = 0; 6558a937a25SMasami Hiramatsu 6568a937a25SMasami Hiramatsu /* Prepare a map for offline binary */ 6578a937a25SMasami Hiramatsu map = dso__new_map(pathname); 6588a937a25SMasami Hiramatsu if (!map || get_text_start_address(pathname, &stext) < 0) { 6598a937a25SMasami Hiramatsu pr_warning("Failed to get ELF symbols for %s\n", pathname); 6608a937a25SMasami Hiramatsu return -EINVAL; 6618a937a25SMasami Hiramatsu } 6628a937a25SMasami Hiramatsu 6638a937a25SMasami Hiramatsu for (i = 0; i < ntevs; i++) { 664*3e96dac7SMasami Hiramatsu ret = post_process_probe_trace_point(&tevs[i].point, 665*3e96dac7SMasami Hiramatsu map, stext); 666*3e96dac7SMasami Hiramatsu if (ret < 0) 667*3e96dac7SMasami Hiramatsu break; 6688a937a25SMasami Hiramatsu } 6698a937a25SMasami Hiramatsu map__put(map); 6708a937a25SMasami Hiramatsu 671*3e96dac7SMasami Hiramatsu return ret; 6728a937a25SMasami Hiramatsu } 6738a937a25SMasami Hiramatsu 674fb7345bbSMasami Hiramatsu static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, 675fb7345bbSMasami Hiramatsu int ntevs, const char *exec) 676fb7345bbSMasami Hiramatsu { 677fb7345bbSMasami Hiramatsu int i, ret = 0; 678eb948e50SMasami Hiramatsu unsigned long stext = 0; 679fb7345bbSMasami Hiramatsu 680fb7345bbSMasami Hiramatsu if (!exec) 681fb7345bbSMasami Hiramatsu return 0; 682fb7345bbSMasami Hiramatsu 683fb7345bbSMasami Hiramatsu ret = get_text_start_address(exec, &stext); 684fb7345bbSMasami Hiramatsu if (ret < 0) 685fb7345bbSMasami Hiramatsu return ret; 686fb7345bbSMasami Hiramatsu 687fb7345bbSMasami Hiramatsu for (i = 0; i < ntevs && ret >= 0; i++) { 688981a2379SMasami Hiramatsu /* point.address is the addres of point.symbol + point.offset */ 689eb948e50SMasami Hiramatsu tevs[i].point.address -= stext; 690fb7345bbSMasami Hiramatsu tevs[i].point.module = strdup(exec); 691eb948e50SMasami Hiramatsu if (!tevs[i].point.module) { 692fb7345bbSMasami Hiramatsu ret = -ENOMEM; 693fb7345bbSMasami Hiramatsu break; 694fb7345bbSMasami Hiramatsu } 695fb7345bbSMasami Hiramatsu tevs[i].uprobes = true; 696fb7345bbSMasami Hiramatsu } 697fb7345bbSMasami Hiramatsu 698fb7345bbSMasami Hiramatsu return ret; 699fb7345bbSMasami Hiramatsu } 700fb7345bbSMasami Hiramatsu 701190b57fcSMasami Hiramatsu static int add_module_to_probe_trace_events(struct probe_trace_event *tevs, 702190b57fcSMasami Hiramatsu int ntevs, const char *module) 703190b57fcSMasami Hiramatsu { 70414a8fd7cSMasami Hiramatsu int i, ret = 0; 70563a29613SRavi Bangoria char *mod_name = NULL; 70614a8fd7cSMasami Hiramatsu 70714a8fd7cSMasami Hiramatsu if (!module) 70814a8fd7cSMasami Hiramatsu return 0; 70914a8fd7cSMasami Hiramatsu 71063a29613SRavi Bangoria mod_name = find_module_name(module); 71114a8fd7cSMasami Hiramatsu 712190b57fcSMasami Hiramatsu for (i = 0; i < ntevs; i++) { 71363a29613SRavi Bangoria tevs[i].point.module = 71463a29613SRavi Bangoria strdup(mod_name ? mod_name : module); 71514a8fd7cSMasami Hiramatsu if (!tevs[i].point.module) { 71614a8fd7cSMasami Hiramatsu ret = -ENOMEM; 71714a8fd7cSMasami Hiramatsu break; 718190b57fcSMasami Hiramatsu } 71914a8fd7cSMasami Hiramatsu } 72014a8fd7cSMasami Hiramatsu 72163a29613SRavi Bangoria free(mod_name); 72214a8fd7cSMasami Hiramatsu return ret; 723190b57fcSMasami Hiramatsu } 724190b57fcSMasami Hiramatsu 725d820456dSRavi Bangoria static int 726d820456dSRavi Bangoria post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, 727d820456dSRavi Bangoria int ntevs) 728dfef99cdSMasami Hiramatsu { 729dfef99cdSMasami Hiramatsu struct ref_reloc_sym *reloc_sym; 730dfef99cdSMasami Hiramatsu char *tmp; 7315a51fcd1SMasami Hiramatsu int i, skipped = 0; 732dfef99cdSMasami Hiramatsu 733428aff82SMasami Hiramatsu /* Skip post process if the target is an offline kernel */ 734428aff82SMasami Hiramatsu if (symbol_conf.ignore_vmlinux_buildid) 7358a937a25SMasami Hiramatsu return post_process_offline_probe_trace_events(tevs, ntevs, 7368a937a25SMasami Hiramatsu symbol_conf.vmlinux_name); 737428aff82SMasami Hiramatsu 7388f33f7deSMasami Hiramatsu reloc_sym = kernel_get_ref_reloc_sym(); 739dfef99cdSMasami Hiramatsu if (!reloc_sym) { 740dfef99cdSMasami Hiramatsu pr_warning("Relocated base symbol is not found!\n"); 741dfef99cdSMasami Hiramatsu return -EINVAL; 742dfef99cdSMasami Hiramatsu } 743dfef99cdSMasami Hiramatsu 744dfef99cdSMasami Hiramatsu for (i = 0; i < ntevs; i++) { 745b031220dSMasami Hiramatsu if (!tevs[i].point.address || tevs[i].point.retprobe) 746b031220dSMasami Hiramatsu continue; 7475a51fcd1SMasami Hiramatsu /* If we found a wrong one, mark it by NULL symbol */ 748b031220dSMasami Hiramatsu if (kprobe_warn_out_range(tevs[i].point.symbol, 749b031220dSMasami Hiramatsu tevs[i].point.address)) { 7505a51fcd1SMasami Hiramatsu tmp = NULL; 7515a51fcd1SMasami Hiramatsu skipped++; 7525a51fcd1SMasami Hiramatsu } else { 753dfef99cdSMasami Hiramatsu tmp = strdup(reloc_sym->name); 754dfef99cdSMasami Hiramatsu if (!tmp) 755dfef99cdSMasami Hiramatsu return -ENOMEM; 7565a51fcd1SMasami Hiramatsu } 7574c859351SMasami Hiramatsu /* If we have no realname, use symbol for it */ 7584c859351SMasami Hiramatsu if (!tevs[i].point.realname) 7594c859351SMasami Hiramatsu tevs[i].point.realname = tevs[i].point.symbol; 7604c859351SMasami Hiramatsu else 761dfef99cdSMasami Hiramatsu free(tevs[i].point.symbol); 762dfef99cdSMasami Hiramatsu tevs[i].point.symbol = tmp; 763dfef99cdSMasami Hiramatsu tevs[i].point.offset = tevs[i].point.address - 764dfef99cdSMasami Hiramatsu reloc_sym->unrelocated_addr; 765dfef99cdSMasami Hiramatsu } 7665a51fcd1SMasami Hiramatsu return skipped; 767dfef99cdSMasami Hiramatsu } 768dfef99cdSMasami Hiramatsu 76999e608b5SRavi Bangoria void __weak 77099e608b5SRavi Bangoria arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused, 77199e608b5SRavi Bangoria int ntevs __maybe_unused) 77299e608b5SRavi Bangoria { 77399e608b5SRavi Bangoria } 77499e608b5SRavi Bangoria 775d820456dSRavi Bangoria /* Post processing the probe events */ 77699e608b5SRavi Bangoria static int post_process_probe_trace_events(struct perf_probe_event *pev, 77799e608b5SRavi Bangoria struct probe_trace_event *tevs, 778d820456dSRavi Bangoria int ntevs, const char *module, 779d820456dSRavi Bangoria bool uprobe) 780d820456dSRavi Bangoria { 78199e608b5SRavi Bangoria int ret; 78299e608b5SRavi Bangoria 783d820456dSRavi Bangoria if (uprobe) 78499e608b5SRavi Bangoria ret = add_exec_to_probe_trace_events(tevs, ntevs, module); 78599e608b5SRavi Bangoria else if (module) 786d820456dSRavi Bangoria /* Currently ref_reloc_sym based probe is not for drivers */ 78799e608b5SRavi Bangoria ret = add_module_to_probe_trace_events(tevs, ntevs, module); 78899e608b5SRavi Bangoria else 78999e608b5SRavi Bangoria ret = post_process_kernel_probe_trace_events(tevs, ntevs); 790d820456dSRavi Bangoria 79199e608b5SRavi Bangoria if (ret >= 0) 79299e608b5SRavi Bangoria arch__post_process_probe_trace_events(pev, ntevs); 79399e608b5SRavi Bangoria 79499e608b5SRavi Bangoria return ret; 795d820456dSRavi Bangoria } 796d820456dSRavi Bangoria 7974b4da7f7SMasami Hiramatsu /* Try to find perf_probe_event with debuginfo */ 7980e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 799ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs) 8004b4da7f7SMasami Hiramatsu { 8014b4da7f7SMasami Hiramatsu bool need_dwarf = perf_probe_event_need_dwarf(pev); 8029b118acaSMasami Hiramatsu struct perf_probe_point tmp; 803225466f1SSrikar Dronamraju struct debuginfo *dinfo; 804190b57fcSMasami Hiramatsu int ntevs, ret = 0; 8054b4da7f7SMasami Hiramatsu 80644225521SMasami Hiramatsu dinfo = open_debuginfo(pev->target, !need_dwarf); 807ff741783SMasami Hiramatsu if (!dinfo) { 80892561cb7SMasami Hiramatsu if (need_dwarf) 809ff741783SMasami Hiramatsu return -ENOENT; 810ff741783SMasami Hiramatsu pr_debug("Could not open debuginfo. Try to use symbols.\n"); 8114b4da7f7SMasami Hiramatsu return 0; 8124b4da7f7SMasami Hiramatsu } 8134b4da7f7SMasami Hiramatsu 814dfef99cdSMasami Hiramatsu pr_debug("Try to find probe point from debuginfo.\n"); 815ff741783SMasami Hiramatsu /* Searching trace events corresponding to a probe event */ 816ddb2f58fSMasami Hiramatsu ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); 817ff741783SMasami Hiramatsu 8189b118acaSMasami Hiramatsu if (ntevs == 0) { /* Not found, retry with an alternative */ 81944225521SMasami Hiramatsu ret = get_alternative_probe_event(dinfo, pev, &tmp); 8209b118acaSMasami Hiramatsu if (!ret) { 821ddb2f58fSMasami Hiramatsu ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); 8229b118acaSMasami Hiramatsu /* 8239b118acaSMasami Hiramatsu * Write back to the original probe_event for 8249b118acaSMasami Hiramatsu * setting appropriate (user given) event name 8259b118acaSMasami Hiramatsu */ 8269b118acaSMasami Hiramatsu clear_perf_probe_point(&pev->point); 8279b118acaSMasami Hiramatsu memcpy(&pev->point, &tmp, sizeof(tmp)); 8289b118acaSMasami Hiramatsu } 8299b118acaSMasami Hiramatsu } 8309b118acaSMasami Hiramatsu 831ff741783SMasami Hiramatsu debuginfo__delete(dinfo); 8324b4da7f7SMasami Hiramatsu 833146a1439SMasami Hiramatsu if (ntevs > 0) { /* Succeeded to find trace events */ 834dfef99cdSMasami Hiramatsu pr_debug("Found %d probe_trace_events.\n", ntevs); 83599e608b5SRavi Bangoria ret = post_process_probe_trace_events(pev, *tevs, ntevs, 83644225521SMasami Hiramatsu pev->target, pev->uprobes); 8375a51fcd1SMasami Hiramatsu if (ret < 0 || ret == ntevs) { 838981d05adSMasami Hiramatsu clear_probe_trace_events(*tevs, ntevs); 839981d05adSMasami Hiramatsu zfree(tevs); 840981d05adSMasami Hiramatsu } 8415a51fcd1SMasami Hiramatsu if (ret != ntevs) 842190b57fcSMasami Hiramatsu return ret < 0 ? ret : ntevs; 8435a51fcd1SMasami Hiramatsu ntevs = 0; 8445a51fcd1SMasami Hiramatsu /* Fall through */ 845146a1439SMasami Hiramatsu } 8464b4da7f7SMasami Hiramatsu 847146a1439SMasami Hiramatsu if (ntevs == 0) { /* No error but failed to find probe point. */ 8480687eba7SMasami Hiramatsu pr_warning("Probe point '%s' not found.\n", 8494b4da7f7SMasami Hiramatsu synthesize_perf_probe_point(&pev->point)); 850146a1439SMasami Hiramatsu return -ENOENT; 851146a1439SMasami Hiramatsu } 852146a1439SMasami Hiramatsu /* Error path : ntevs < 0 */ 85315eca306SMasami Hiramatsu pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); 8541c0bd0e8SWang Nan if (ntevs < 0) { 8551c0bd0e8SWang Nan if (ntevs == -EBADF) 85615eca306SMasami Hiramatsu pr_warning("Warning: No dwarf info found in the vmlinux - " 85715eca306SMasami Hiramatsu "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); 85815eca306SMasami Hiramatsu if (!need_dwarf) { 8590e43e5d2SMasami Hiramatsu pr_debug("Trying to use symbols.\n"); 8604b4da7f7SMasami Hiramatsu return 0; 8614b4da7f7SMasami Hiramatsu } 86215eca306SMasami Hiramatsu } 86315eca306SMasami Hiramatsu return ntevs; 86415eca306SMasami Hiramatsu } 8654b4da7f7SMasami Hiramatsu 8664b4da7f7SMasami Hiramatsu #define LINEBUF_SIZE 256 8674b4da7f7SMasami Hiramatsu #define NR_ADDITIONAL_LINES 2 8684b4da7f7SMasami Hiramatsu 869fde52dbdSFranck Bui-Huu static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) 8704b4da7f7SMasami Hiramatsu { 8715f03cba4SMasami Hiramatsu char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; 872befe3414SFranck Bui-Huu const char *color = show_num ? "" : PERF_COLOR_BLUE; 873befe3414SFranck Bui-Huu const char *prefix = NULL; 8744b4da7f7SMasami Hiramatsu 875befe3414SFranck Bui-Huu do { 8764b4da7f7SMasami Hiramatsu if (fgets(buf, LINEBUF_SIZE, fp) == NULL) 8774b4da7f7SMasami Hiramatsu goto error; 878befe3414SFranck Bui-Huu if (skip) 879befe3414SFranck Bui-Huu continue; 880befe3414SFranck Bui-Huu if (!prefix) { 881befe3414SFranck Bui-Huu prefix = show_num ? "%7d " : " "; 882befe3414SFranck Bui-Huu color_fprintf(stdout, color, prefix, l); 8834b4da7f7SMasami Hiramatsu } 884befe3414SFranck Bui-Huu color_fprintf(stdout, color, "%s", buf); 8854b4da7f7SMasami Hiramatsu 886befe3414SFranck Bui-Huu } while (strchr(buf, '\n') == NULL); 887146a1439SMasami Hiramatsu 888fde52dbdSFranck Bui-Huu return 1; 8894b4da7f7SMasami Hiramatsu error: 890fde52dbdSFranck Bui-Huu if (ferror(fp)) { 8915f03cba4SMasami Hiramatsu pr_warning("File read error: %s\n", 892c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, sbuf, sizeof(sbuf))); 893146a1439SMasami Hiramatsu return -1; 8944b4da7f7SMasami Hiramatsu } 895fde52dbdSFranck Bui-Huu return 0; 896fde52dbdSFranck Bui-Huu } 897fde52dbdSFranck Bui-Huu 898fde52dbdSFranck Bui-Huu static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) 899fde52dbdSFranck Bui-Huu { 900fde52dbdSFranck Bui-Huu int rv = __show_one_line(fp, l, skip, show_num); 901fde52dbdSFranck Bui-Huu if (rv == 0) { 902fde52dbdSFranck Bui-Huu pr_warning("Source file is shorter than expected.\n"); 903fde52dbdSFranck Bui-Huu rv = -1; 904fde52dbdSFranck Bui-Huu } 905fde52dbdSFranck Bui-Huu return rv; 906fde52dbdSFranck Bui-Huu } 907fde52dbdSFranck Bui-Huu 908fde52dbdSFranck Bui-Huu #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) 909fde52dbdSFranck Bui-Huu #define show_one_line(f,l) _show_one_line(f,l,false,false) 910fde52dbdSFranck Bui-Huu #define skip_one_line(f,l) _show_one_line(f,l,true,false) 911fde52dbdSFranck Bui-Huu #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) 9124b4da7f7SMasami Hiramatsu 9134b4da7f7SMasami Hiramatsu /* 9144b4da7f7SMasami Hiramatsu * Show line-range always requires debuginfo to find source file and 9154b4da7f7SMasami Hiramatsu * line number. 9164b4da7f7SMasami Hiramatsu */ 917811dd2aeSMasami Hiramatsu static int __show_line_range(struct line_range *lr, const char *module, 918811dd2aeSMasami Hiramatsu bool user) 9194b4da7f7SMasami Hiramatsu { 920d3b63d7aSMasami Hiramatsu int l = 1; 9215a62257aSMasami Hiramatsu struct int_node *ln; 922ff741783SMasami Hiramatsu struct debuginfo *dinfo; 9234b4da7f7SMasami Hiramatsu FILE *fp; 924ff741783SMasami Hiramatsu int ret; 9257cf0b79eSMasami Hiramatsu char *tmp; 9265f03cba4SMasami Hiramatsu char sbuf[STRERR_BUFSIZE]; 9274b4da7f7SMasami Hiramatsu 9284b4da7f7SMasami Hiramatsu /* Search a line range */ 92992561cb7SMasami Hiramatsu dinfo = open_debuginfo(module, false); 93092561cb7SMasami Hiramatsu if (!dinfo) 931ff741783SMasami Hiramatsu return -ENOENT; 932146a1439SMasami Hiramatsu 933ff741783SMasami Hiramatsu ret = debuginfo__find_line_range(dinfo, lr); 934811dd2aeSMasami Hiramatsu if (!ret) { /* Not found, retry with an alternative */ 935811dd2aeSMasami Hiramatsu ret = get_alternative_line_range(dinfo, lr, module, user); 936811dd2aeSMasami Hiramatsu if (!ret) 937811dd2aeSMasami Hiramatsu ret = debuginfo__find_line_range(dinfo, lr); 938811dd2aeSMasami Hiramatsu } 939ff741783SMasami Hiramatsu debuginfo__delete(dinfo); 9405ee05b88SMasami Hiramatsu if (ret == 0 || ret == -ENOENT) { 941146a1439SMasami Hiramatsu pr_warning("Specified source line is not found.\n"); 942146a1439SMasami Hiramatsu return -ENOENT; 943146a1439SMasami Hiramatsu } else if (ret < 0) { 9445ee05b88SMasami Hiramatsu pr_warning("Debuginfo analysis failed.\n"); 945146a1439SMasami Hiramatsu return ret; 946146a1439SMasami Hiramatsu } 9474b4da7f7SMasami Hiramatsu 9487cf0b79eSMasami Hiramatsu /* Convert source file path */ 9497cf0b79eSMasami Hiramatsu tmp = lr->path; 9506a330a3cSMasami Hiramatsu ret = get_real_path(tmp, lr->comp_dir, &lr->path); 951a78604deSHe Kuang 952a78604deSHe Kuang /* Free old path when new path is assigned */ 953a78604deSHe Kuang if (tmp != lr->path) 954a78604deSHe Kuang free(tmp); 955a78604deSHe Kuang 9567cf0b79eSMasami Hiramatsu if (ret < 0) { 9575ee05b88SMasami Hiramatsu pr_warning("Failed to find source file path.\n"); 9587cf0b79eSMasami Hiramatsu return ret; 9597cf0b79eSMasami Hiramatsu } 9607cf0b79eSMasami Hiramatsu 9614b4da7f7SMasami Hiramatsu setup_pager(); 9624b4da7f7SMasami Hiramatsu 9634b4da7f7SMasami Hiramatsu if (lr->function) 9648737ebdeSMasami Hiramatsu fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, 9654b4da7f7SMasami Hiramatsu lr->start - lr->offset); 9664b4da7f7SMasami Hiramatsu else 96762c15fc4SFranck Bui-Huu fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); 9684b4da7f7SMasami Hiramatsu 9694b4da7f7SMasami Hiramatsu fp = fopen(lr->path, "r"); 970146a1439SMasami Hiramatsu if (fp == NULL) { 971146a1439SMasami Hiramatsu pr_warning("Failed to open %s: %s\n", lr->path, 972c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, sbuf, sizeof(sbuf))); 973146a1439SMasami Hiramatsu return -errno; 974146a1439SMasami Hiramatsu } 9754b4da7f7SMasami Hiramatsu /* Skip to starting line number */ 97644b81e92SFranck Bui-Huu while (l < lr->start) { 977fde52dbdSFranck Bui-Huu ret = skip_one_line(fp, l++); 978146a1439SMasami Hiramatsu if (ret < 0) 979146a1439SMasami Hiramatsu goto end; 98044b81e92SFranck Bui-Huu } 9814b4da7f7SMasami Hiramatsu 98210daf4d0SArnaldo Carvalho de Melo intlist__for_each_entry(ln, lr->line_list) { 9835a62257aSMasami Hiramatsu for (; ln->i > l; l++) { 984fde52dbdSFranck Bui-Huu ret = show_one_line(fp, l - lr->offset); 98544b81e92SFranck Bui-Huu if (ret < 0) 98644b81e92SFranck Bui-Huu goto end; 98744b81e92SFranck Bui-Huu } 988fde52dbdSFranck Bui-Huu ret = show_one_line_with_num(fp, l++ - lr->offset); 989146a1439SMasami Hiramatsu if (ret < 0) 990146a1439SMasami Hiramatsu goto end; 9914b4da7f7SMasami Hiramatsu } 9924b4da7f7SMasami Hiramatsu 9934b4da7f7SMasami Hiramatsu if (lr->end == INT_MAX) 9944b4da7f7SMasami Hiramatsu lr->end = l + NR_ADDITIONAL_LINES; 995fde52dbdSFranck Bui-Huu while (l <= lr->end) { 996fde52dbdSFranck Bui-Huu ret = show_one_line_or_eof(fp, l++ - lr->offset); 997fde52dbdSFranck Bui-Huu if (ret <= 0) 99844b81e92SFranck Bui-Huu break; 99944b81e92SFranck Bui-Huu } 1000146a1439SMasami Hiramatsu end: 10014b4da7f7SMasami Hiramatsu fclose(fp); 1002146a1439SMasami Hiramatsu return ret; 10034b4da7f7SMasami Hiramatsu } 10044b4da7f7SMasami Hiramatsu 10052b394bc4SMasami Hiramatsu int show_line_range(struct line_range *lr, const char *module, bool user) 1006ee45b6c2SMasami Hiramatsu { 1007ee45b6c2SMasami Hiramatsu int ret; 1008ee45b6c2SMasami Hiramatsu 10099bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(user); 1010ee45b6c2SMasami Hiramatsu if (ret < 0) 1011ee45b6c2SMasami Hiramatsu return ret; 1012811dd2aeSMasami Hiramatsu ret = __show_line_range(lr, module, user); 10139bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 1014ee45b6c2SMasami Hiramatsu 1015ee45b6c2SMasami Hiramatsu return ret; 1016ee45b6c2SMasami Hiramatsu } 1017ee45b6c2SMasami Hiramatsu 1018ff741783SMasami Hiramatsu static int show_available_vars_at(struct debuginfo *dinfo, 1019ff741783SMasami Hiramatsu struct perf_probe_event *pev, 1020ddb2f58fSMasami Hiramatsu struct strfilter *_filter) 1021cf6eb489SMasami Hiramatsu { 1022cf6eb489SMasami Hiramatsu char *buf; 1023bd09d7b5SMasami Hiramatsu int ret, i, nvars; 1024cf6eb489SMasami Hiramatsu struct str_node *node; 1025cf6eb489SMasami Hiramatsu struct variable_list *vls = NULL, *vl; 10269b118acaSMasami Hiramatsu struct perf_probe_point tmp; 1027bd09d7b5SMasami Hiramatsu const char *var; 1028cf6eb489SMasami Hiramatsu 1029cf6eb489SMasami Hiramatsu buf = synthesize_perf_probe_point(&pev->point); 1030cf6eb489SMasami Hiramatsu if (!buf) 1031cf6eb489SMasami Hiramatsu return -EINVAL; 1032cf6eb489SMasami Hiramatsu pr_debug("Searching variables at %s\n", buf); 1033cf6eb489SMasami Hiramatsu 1034ddb2f58fSMasami Hiramatsu ret = debuginfo__find_available_vars_at(dinfo, pev, &vls); 10359b118acaSMasami Hiramatsu if (!ret) { /* Not found, retry with an alternative */ 103644225521SMasami Hiramatsu ret = get_alternative_probe_event(dinfo, pev, &tmp); 10379b118acaSMasami Hiramatsu if (!ret) { 10389b118acaSMasami Hiramatsu ret = debuginfo__find_available_vars_at(dinfo, pev, 1039ddb2f58fSMasami Hiramatsu &vls); 10409b118acaSMasami Hiramatsu /* Release the old probe_point */ 10419b118acaSMasami Hiramatsu clear_perf_probe_point(&tmp); 10429b118acaSMasami Hiramatsu } 10439b118acaSMasami Hiramatsu } 1044bd09d7b5SMasami Hiramatsu if (ret <= 0) { 104569e96eaaSMasami Hiramatsu if (ret == 0 || ret == -ENOENT) { 104669e96eaaSMasami Hiramatsu pr_err("Failed to find the address of %s\n", buf); 104769e96eaaSMasami Hiramatsu ret = -ENOENT; 104869e96eaaSMasami Hiramatsu } else 104969e96eaaSMasami Hiramatsu pr_warning("Debuginfo analysis failed.\n"); 1050bd09d7b5SMasami Hiramatsu goto end; 1051bd09d7b5SMasami Hiramatsu } 105269e96eaaSMasami Hiramatsu 1053bd09d7b5SMasami Hiramatsu /* Some variables are found */ 1054cf6eb489SMasami Hiramatsu fprintf(stdout, "Available variables at %s\n", buf); 1055cf6eb489SMasami Hiramatsu for (i = 0; i < ret; i++) { 1056cf6eb489SMasami Hiramatsu vl = &vls[i]; 1057cf6eb489SMasami Hiramatsu /* 1058cf6eb489SMasami Hiramatsu * A probe point might be converted to 1059cf6eb489SMasami Hiramatsu * several trace points. 1060cf6eb489SMasami Hiramatsu */ 1061cf6eb489SMasami Hiramatsu fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, 1062cf6eb489SMasami Hiramatsu vl->point.offset); 106374cf249dSArnaldo Carvalho de Melo zfree(&vl->point.symbol); 1064bd09d7b5SMasami Hiramatsu nvars = 0; 1065cf6eb489SMasami Hiramatsu if (vl->vars) { 1066602a1f4dSArnaldo Carvalho de Melo strlist__for_each_entry(node, vl->vars) { 1067bd09d7b5SMasami Hiramatsu var = strchr(node->s, '\t') + 1; 1068bd09d7b5SMasami Hiramatsu if (strfilter__compare(_filter, var)) { 1069cf6eb489SMasami Hiramatsu fprintf(stdout, "\t\t%s\n", node->s); 1070bd09d7b5SMasami Hiramatsu nvars++; 1071bd09d7b5SMasami Hiramatsu } 1072bd09d7b5SMasami Hiramatsu } 1073cf6eb489SMasami Hiramatsu strlist__delete(vl->vars); 1074bd09d7b5SMasami Hiramatsu } 1075bd09d7b5SMasami Hiramatsu if (nvars == 0) 1076bd09d7b5SMasami Hiramatsu fprintf(stdout, "\t\t(No matched variables)\n"); 1077cf6eb489SMasami Hiramatsu } 1078cf6eb489SMasami Hiramatsu free(vls); 1079bd09d7b5SMasami Hiramatsu end: 1080cf6eb489SMasami Hiramatsu free(buf); 1081cf6eb489SMasami Hiramatsu return ret; 1082cf6eb489SMasami Hiramatsu } 1083cf6eb489SMasami Hiramatsu 1084cf6eb489SMasami Hiramatsu /* Show available variables on given probe point */ 1085cf6eb489SMasami Hiramatsu int show_available_vars(struct perf_probe_event *pevs, int npevs, 1086ddb2f58fSMasami Hiramatsu struct strfilter *_filter) 1087cf6eb489SMasami Hiramatsu { 1088ff741783SMasami Hiramatsu int i, ret = 0; 1089ff741783SMasami Hiramatsu struct debuginfo *dinfo; 1090cf6eb489SMasami Hiramatsu 10919bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(pevs->uprobes); 1092cf6eb489SMasami Hiramatsu if (ret < 0) 1093cf6eb489SMasami Hiramatsu return ret; 1094cf6eb489SMasami Hiramatsu 109544225521SMasami Hiramatsu dinfo = open_debuginfo(pevs->target, false); 1096ff741783SMasami Hiramatsu if (!dinfo) { 1097ee45b6c2SMasami Hiramatsu ret = -ENOENT; 1098ee45b6c2SMasami Hiramatsu goto out; 1099ff741783SMasami Hiramatsu } 1100ff741783SMasami Hiramatsu 1101cc446446SMasami Hiramatsu setup_pager(); 1102cc446446SMasami Hiramatsu 1103ff741783SMasami Hiramatsu for (i = 0; i < npevs && ret >= 0; i++) 1104ddb2f58fSMasami Hiramatsu ret = show_available_vars_at(dinfo, &pevs[i], _filter); 1105ff741783SMasami Hiramatsu 1106ff741783SMasami Hiramatsu debuginfo__delete(dinfo); 1107ee45b6c2SMasami Hiramatsu out: 11089bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 1109cf6eb489SMasami Hiramatsu return ret; 1110cf6eb489SMasami Hiramatsu } 1111cf6eb489SMasami Hiramatsu 111289fe808aSIngo Molnar #else /* !HAVE_DWARF_SUPPORT */ 11134b4da7f7SMasami Hiramatsu 11147737af01SMasami Hiramatsu static void debuginfo_cache__exit(void) 11157737af01SMasami Hiramatsu { 11167737af01SMasami Hiramatsu } 11177737af01SMasami Hiramatsu 11185a6f6314SMasami Hiramatsu static int 11195a6f6314SMasami Hiramatsu find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, 11205a6f6314SMasami Hiramatsu struct perf_probe_point *pp __maybe_unused, 11215a6f6314SMasami Hiramatsu bool is_kprobe __maybe_unused) 11224b4da7f7SMasami Hiramatsu { 11235a6f6314SMasami Hiramatsu return -ENOSYS; 11244b4da7f7SMasami Hiramatsu } 11254b4da7f7SMasami Hiramatsu 11260e60836bSSrikar Dronamraju static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 1127ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs __maybe_unused) 11284b4da7f7SMasami Hiramatsu { 1129146a1439SMasami Hiramatsu if (perf_probe_event_need_dwarf(pev)) { 1130146a1439SMasami Hiramatsu pr_warning("Debuginfo-analysis is not supported.\n"); 1131146a1439SMasami Hiramatsu return -ENOSYS; 1132146a1439SMasami Hiramatsu } 1133225466f1SSrikar Dronamraju 11344b4da7f7SMasami Hiramatsu return 0; 11354b4da7f7SMasami Hiramatsu } 11364b4da7f7SMasami Hiramatsu 11371d037ca1SIrina Tirdea int show_line_range(struct line_range *lr __maybe_unused, 11382b394bc4SMasami Hiramatsu const char *module __maybe_unused, 11392b394bc4SMasami Hiramatsu bool user __maybe_unused) 11404b4da7f7SMasami Hiramatsu { 1141146a1439SMasami Hiramatsu pr_warning("Debuginfo-analysis is not supported.\n"); 1142146a1439SMasami Hiramatsu return -ENOSYS; 11434b4da7f7SMasami Hiramatsu } 11444b4da7f7SMasami Hiramatsu 11451d037ca1SIrina Tirdea int show_available_vars(struct perf_probe_event *pevs __maybe_unused, 1146ddb2f58fSMasami Hiramatsu int npevs __maybe_unused, 1147ddb2f58fSMasami Hiramatsu struct strfilter *filter __maybe_unused) 1148cf6eb489SMasami Hiramatsu { 1149cf6eb489SMasami Hiramatsu pr_warning("Debuginfo-analysis is not supported.\n"); 1150cf6eb489SMasami Hiramatsu return -ENOSYS; 1151cf6eb489SMasami Hiramatsu } 1152e0faa8d3SMasami Hiramatsu #endif 1153e0faa8d3SMasami Hiramatsu 1154e53b00d3SMasami Hiramatsu void line_range__clear(struct line_range *lr) 1155e53b00d3SMasami Hiramatsu { 1156e53b00d3SMasami Hiramatsu free(lr->function); 1157e53b00d3SMasami Hiramatsu free(lr->file); 1158e53b00d3SMasami Hiramatsu free(lr->path); 1159e53b00d3SMasami Hiramatsu free(lr->comp_dir); 11605a62257aSMasami Hiramatsu intlist__delete(lr->line_list); 1161e53b00d3SMasami Hiramatsu memset(lr, 0, sizeof(*lr)); 1162e53b00d3SMasami Hiramatsu } 1163e53b00d3SMasami Hiramatsu 11645a62257aSMasami Hiramatsu int line_range__init(struct line_range *lr) 1165e53b00d3SMasami Hiramatsu { 1166e53b00d3SMasami Hiramatsu memset(lr, 0, sizeof(*lr)); 11675a62257aSMasami Hiramatsu lr->line_list = intlist__new(NULL); 11685a62257aSMasami Hiramatsu if (!lr->line_list) 11695a62257aSMasami Hiramatsu return -ENOMEM; 11705a62257aSMasami Hiramatsu else 11715a62257aSMasami Hiramatsu return 0; 1172e53b00d3SMasami Hiramatsu } 1173e53b00d3SMasami Hiramatsu 117421dd9ae5SFranck Bui-Huu static int parse_line_num(char **ptr, int *val, const char *what) 117521dd9ae5SFranck Bui-Huu { 117621dd9ae5SFranck Bui-Huu const char *start = *ptr; 117721dd9ae5SFranck Bui-Huu 117821dd9ae5SFranck Bui-Huu errno = 0; 117921dd9ae5SFranck Bui-Huu *val = strtol(*ptr, ptr, 0); 118021dd9ae5SFranck Bui-Huu if (errno || *ptr == start) { 118121dd9ae5SFranck Bui-Huu semantic_error("'%s' is not a valid number.\n", what); 118221dd9ae5SFranck Bui-Huu return -EINVAL; 118321dd9ae5SFranck Bui-Huu } 118421dd9ae5SFranck Bui-Huu return 0; 118521dd9ae5SFranck Bui-Huu } 118621dd9ae5SFranck Bui-Huu 1187573709fdSMasami Hiramatsu /* Check the name is good for event, group or function */ 1188573709fdSMasami Hiramatsu static bool is_c_func_name(const char *name) 1189573709fdSMasami Hiramatsu { 1190573709fdSMasami Hiramatsu if (!isalpha(*name) && *name != '_') 1191573709fdSMasami Hiramatsu return false; 1192573709fdSMasami Hiramatsu while (*++name != '\0') { 1193573709fdSMasami Hiramatsu if (!isalpha(*name) && !isdigit(*name) && *name != '_') 1194573709fdSMasami Hiramatsu return false; 1195573709fdSMasami Hiramatsu } 1196573709fdSMasami Hiramatsu return true; 1197573709fdSMasami Hiramatsu } 1198573709fdSMasami Hiramatsu 11999d95b580SFranck Bui-Huu /* 12009d95b580SFranck Bui-Huu * Stuff 'lr' according to the line range described by 'arg'. 12019d95b580SFranck Bui-Huu * The line range syntax is described by: 12029d95b580SFranck Bui-Huu * 12039d95b580SFranck Bui-Huu * SRC[:SLN[+NUM|-ELN]] 1204e116dfa1SMasami Hiramatsu * FNC[@SRC][:SLN[+NUM|-ELN]] 12059d95b580SFranck Bui-Huu */ 1206146a1439SMasami Hiramatsu int parse_line_range_desc(const char *arg, struct line_range *lr) 1207631c9defSMasami Hiramatsu { 1208e116dfa1SMasami Hiramatsu char *range, *file, *name = strdup(arg); 120921dd9ae5SFranck Bui-Huu int err; 12109d95b580SFranck Bui-Huu 121121dd9ae5SFranck Bui-Huu if (!name) 121221dd9ae5SFranck Bui-Huu return -ENOMEM; 121321dd9ae5SFranck Bui-Huu 121421dd9ae5SFranck Bui-Huu lr->start = 0; 121521dd9ae5SFranck Bui-Huu lr->end = INT_MAX; 121621dd9ae5SFranck Bui-Huu 121721dd9ae5SFranck Bui-Huu range = strchr(name, ':'); 121821dd9ae5SFranck Bui-Huu if (range) { 121921dd9ae5SFranck Bui-Huu *range++ = '\0'; 122021dd9ae5SFranck Bui-Huu 122121dd9ae5SFranck Bui-Huu err = parse_line_num(&range, &lr->start, "start line"); 122221dd9ae5SFranck Bui-Huu if (err) 122321dd9ae5SFranck Bui-Huu goto err; 122421dd9ae5SFranck Bui-Huu 122521dd9ae5SFranck Bui-Huu if (*range == '+' || *range == '-') { 122621dd9ae5SFranck Bui-Huu const char c = *range++; 122721dd9ae5SFranck Bui-Huu 122821dd9ae5SFranck Bui-Huu err = parse_line_num(&range, &lr->end, "end line"); 122921dd9ae5SFranck Bui-Huu if (err) 123021dd9ae5SFranck Bui-Huu goto err; 123121dd9ae5SFranck Bui-Huu 123221dd9ae5SFranck Bui-Huu if (c == '+') { 123321dd9ae5SFranck Bui-Huu lr->end += lr->start; 123421dd9ae5SFranck Bui-Huu /* 1235dda4ab34SMasami Hiramatsu * Adjust the number of lines here. 1236dda4ab34SMasami Hiramatsu * If the number of lines == 1, the 1237dda4ab34SMasami Hiramatsu * the end of line should be equal to 1238dda4ab34SMasami Hiramatsu * the start of line. 1239dda4ab34SMasami Hiramatsu */ 124021dd9ae5SFranck Bui-Huu lr->end--; 124121dd9ae5SFranck Bui-Huu } 124221dd9ae5SFranck Bui-Huu } 124321dd9ae5SFranck Bui-Huu 1244d3b63d7aSMasami Hiramatsu pr_debug("Line range is %d to %d\n", lr->start, lr->end); 124521dd9ae5SFranck Bui-Huu 124621dd9ae5SFranck Bui-Huu err = -EINVAL; 1247d3b63d7aSMasami Hiramatsu if (lr->start > lr->end) { 1248631c9defSMasami Hiramatsu semantic_error("Start line must be smaller" 1249146a1439SMasami Hiramatsu " than end line.\n"); 125021dd9ae5SFranck Bui-Huu goto err; 1251146a1439SMasami Hiramatsu } 125221dd9ae5SFranck Bui-Huu if (*range != '\0') { 125321dd9ae5SFranck Bui-Huu semantic_error("Tailing with invalid str '%s'.\n", range); 125421dd9ae5SFranck Bui-Huu goto err; 1255146a1439SMasami Hiramatsu } 1256d3b63d7aSMasami Hiramatsu } 125702b95dadSMasami Hiramatsu 1258e116dfa1SMasami Hiramatsu file = strchr(name, '@'); 1259e116dfa1SMasami Hiramatsu if (file) { 1260e116dfa1SMasami Hiramatsu *file = '\0'; 1261e116dfa1SMasami Hiramatsu lr->file = strdup(++file); 1262e116dfa1SMasami Hiramatsu if (lr->file == NULL) { 1263e116dfa1SMasami Hiramatsu err = -ENOMEM; 1264e116dfa1SMasami Hiramatsu goto err; 1265e116dfa1SMasami Hiramatsu } 1266e116dfa1SMasami Hiramatsu lr->function = name; 1267573709fdSMasami Hiramatsu } else if (strchr(name, '/') || strchr(name, '.')) 126821dd9ae5SFranck Bui-Huu lr->file = name; 1269573709fdSMasami Hiramatsu else if (is_c_func_name(name))/* We reuse it for checking funcname */ 127021dd9ae5SFranck Bui-Huu lr->function = name; 1271573709fdSMasami Hiramatsu else { /* Invalid name */ 1272573709fdSMasami Hiramatsu semantic_error("'%s' is not a valid function name.\n", name); 1273573709fdSMasami Hiramatsu err = -EINVAL; 1274573709fdSMasami Hiramatsu goto err; 1275573709fdSMasami Hiramatsu } 1276146a1439SMasami Hiramatsu 1277146a1439SMasami Hiramatsu return 0; 127821dd9ae5SFranck Bui-Huu err: 127921dd9ae5SFranck Bui-Huu free(name); 128021dd9ae5SFranck Bui-Huu return err; 1281631c9defSMasami Hiramatsu } 1282631c9defSMasami Hiramatsu 128336a009feSMasami Hiramatsu static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev) 128436a009feSMasami Hiramatsu { 128536a009feSMasami Hiramatsu char *ptr; 128636a009feSMasami Hiramatsu 128736a009feSMasami Hiramatsu ptr = strchr(*arg, ':'); 128836a009feSMasami Hiramatsu if (ptr) { 128936a009feSMasami Hiramatsu *ptr = '\0'; 129042bba263SMasami Hiramatsu if (!pev->sdt && !is_c_func_name(*arg)) 129136a009feSMasami Hiramatsu goto ng_name; 129236a009feSMasami Hiramatsu pev->group = strdup(*arg); 129336a009feSMasami Hiramatsu if (!pev->group) 129436a009feSMasami Hiramatsu return -ENOMEM; 129536a009feSMasami Hiramatsu *arg = ptr + 1; 129636a009feSMasami Hiramatsu } else 129736a009feSMasami Hiramatsu pev->group = NULL; 129842bba263SMasami Hiramatsu if (!pev->sdt && !is_c_func_name(*arg)) { 129936a009feSMasami Hiramatsu ng_name: 130036a009feSMasami Hiramatsu semantic_error("%s is bad for event name -it must " 130136a009feSMasami Hiramatsu "follow C symbol-naming rule.\n", *arg); 130236a009feSMasami Hiramatsu return -EINVAL; 130336a009feSMasami Hiramatsu } 130436a009feSMasami Hiramatsu pev->event = strdup(*arg); 130536a009feSMasami Hiramatsu if (pev->event == NULL) 130636a009feSMasami Hiramatsu return -ENOMEM; 130736a009feSMasami Hiramatsu 130836a009feSMasami Hiramatsu return 0; 130936a009feSMasami Hiramatsu } 131036a009feSMasami Hiramatsu 131150656eecSMasami Hiramatsu /* Parse probepoint definition. */ 1312146a1439SMasami Hiramatsu static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) 131350656eecSMasami Hiramatsu { 13144235b045SMasami Hiramatsu struct perf_probe_point *pp = &pev->point; 131550656eecSMasami Hiramatsu char *ptr, *tmp; 131650656eecSMasami Hiramatsu char c, nc = 0; 13173099c026SNaveen N. Rao bool file_spec = false; 131836a009feSMasami Hiramatsu int ret; 131936a009feSMasami Hiramatsu 132050656eecSMasami Hiramatsu /* 132150656eecSMasami Hiramatsu * <Syntax> 13228d993d96SMasami Hiramatsu * perf probe [GRP:][EVENT=]SRC[:LN|;PTN] 13238d993d96SMasami Hiramatsu * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] 132436a009feSMasami Hiramatsu * perf probe %[GRP:]SDT_EVENT 132550656eecSMasami Hiramatsu */ 1326e59d29e8SWang Nan if (!arg) 1327e59d29e8SWang Nan return -EINVAL; 132850656eecSMasami Hiramatsu 13297e9fca51SMasami Hiramatsu /* 13307e9fca51SMasami Hiramatsu * If the probe point starts with '%', 13317e9fca51SMasami Hiramatsu * or starts with "sdt_" and has a ':' but no '=', 13327e9fca51SMasami Hiramatsu * then it should be a SDT/cached probe point. 13337e9fca51SMasami Hiramatsu */ 13347e9fca51SMasami Hiramatsu if (arg[0] == '%' || 13357e9fca51SMasami Hiramatsu (!strncmp(arg, "sdt_", 4) && 13367e9fca51SMasami Hiramatsu !!strchr(arg, ':') && !strchr(arg, '='))) { 133736a009feSMasami Hiramatsu pev->sdt = true; 13387e9fca51SMasami Hiramatsu if (arg[0] == '%') 133936a009feSMasami Hiramatsu arg++; 134036a009feSMasami Hiramatsu } 134136a009feSMasami Hiramatsu 13422a9c8c36SMasami Hiramatsu ptr = strpbrk(arg, ";=@+%"); 134336a009feSMasami Hiramatsu if (pev->sdt) { 134436a009feSMasami Hiramatsu if (ptr) { 1345a598180aSMasami Hiramatsu if (*ptr != '@') { 1346a598180aSMasami Hiramatsu semantic_error("%s must be an SDT name.\n", 1347a598180aSMasami Hiramatsu arg); 134836a009feSMasami Hiramatsu return -EINVAL; 134936a009feSMasami Hiramatsu } 1350a598180aSMasami Hiramatsu /* This must be a target file name or build id */ 1351a598180aSMasami Hiramatsu tmp = build_id_cache__complement(ptr + 1); 1352a598180aSMasami Hiramatsu if (tmp) { 1353a598180aSMasami Hiramatsu pev->target = build_id_cache__origname(tmp); 1354a598180aSMasami Hiramatsu free(tmp); 1355a598180aSMasami Hiramatsu } else 1356a598180aSMasami Hiramatsu pev->target = strdup(ptr + 1); 1357a598180aSMasami Hiramatsu if (!pev->target) 1358a598180aSMasami Hiramatsu return -ENOMEM; 1359a598180aSMasami Hiramatsu *ptr = '\0'; 1360a598180aSMasami Hiramatsu } 136136a009feSMasami Hiramatsu ret = parse_perf_probe_event_name(&arg, pev); 136236a009feSMasami Hiramatsu if (ret == 0) { 136336a009feSMasami Hiramatsu if (asprintf(&pev->point.function, "%%%s", pev->event) < 0) 136436a009feSMasami Hiramatsu ret = -errno; 136536a009feSMasami Hiramatsu } 136636a009feSMasami Hiramatsu return ret; 136736a009feSMasami Hiramatsu } 136836a009feSMasami Hiramatsu 13692a9c8c36SMasami Hiramatsu if (ptr && *ptr == '=') { /* Event name */ 1370af663d75SMasami Hiramatsu *ptr = '\0'; 1371af663d75SMasami Hiramatsu tmp = ptr + 1; 137236a009feSMasami Hiramatsu ret = parse_perf_probe_event_name(&arg, pev); 137336a009feSMasami Hiramatsu if (ret < 0) 137436a009feSMasami Hiramatsu return ret; 137536a009feSMasami Hiramatsu 1376af663d75SMasami Hiramatsu arg = tmp; 1377af663d75SMasami Hiramatsu } 1378af663d75SMasami Hiramatsu 13793099c026SNaveen N. Rao /* 13803099c026SNaveen N. Rao * Check arg is function or file name and copy it. 13813099c026SNaveen N. Rao * 13823099c026SNaveen N. Rao * We consider arg to be a file spec if and only if it satisfies 13833099c026SNaveen N. Rao * all of the below criteria:: 13843099c026SNaveen N. Rao * - it does not include any of "+@%", 13853099c026SNaveen N. Rao * - it includes one of ":;", and 13863099c026SNaveen N. Rao * - it has a period '.' in the name. 13873099c026SNaveen N. Rao * 13883099c026SNaveen N. Rao * Otherwise, we consider arg to be a function specification. 13893099c026SNaveen N. Rao */ 13903099c026SNaveen N. Rao if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) { 13913099c026SNaveen N. Rao /* This is a file spec if it includes a '.' before ; or : */ 13923099c026SNaveen N. Rao if (memchr(arg, '.', ptr - arg)) 13933099c026SNaveen N. Rao file_spec = true; 13943099c026SNaveen N. Rao } 13953099c026SNaveen N. Rao 13962a9c8c36SMasami Hiramatsu ptr = strpbrk(arg, ";:+@%"); 139750656eecSMasami Hiramatsu if (ptr) { 139850656eecSMasami Hiramatsu nc = *ptr; 139950656eecSMasami Hiramatsu *ptr++ = '\0'; 140050656eecSMasami Hiramatsu } 140150656eecSMasami Hiramatsu 14026c6e024fSWang Nan if (arg[0] == '\0') 14036c6e024fSWang Nan tmp = NULL; 14046c6e024fSWang Nan else { 140502b95dadSMasami Hiramatsu tmp = strdup(arg); 140602b95dadSMasami Hiramatsu if (tmp == NULL) 140702b95dadSMasami Hiramatsu return -ENOMEM; 14086c6e024fSWang Nan } 140902b95dadSMasami Hiramatsu 14103099c026SNaveen N. Rao if (file_spec) 141102b95dadSMasami Hiramatsu pp->file = tmp; 1412da15bd9dSWang Nan else { 141302b95dadSMasami Hiramatsu pp->function = tmp; 141450656eecSMasami Hiramatsu 1415da15bd9dSWang Nan /* 1416da15bd9dSWang Nan * Keep pp->function even if this is absolute address, 1417da15bd9dSWang Nan * so it can mark whether abs_address is valid. 1418da15bd9dSWang Nan * Which make 'perf probe lib.bin 0x0' possible. 1419da15bd9dSWang Nan * 1420da15bd9dSWang Nan * Note that checking length of tmp is not needed 1421da15bd9dSWang Nan * because when we access tmp[1] we know tmp[0] is '0', 1422da15bd9dSWang Nan * so tmp[1] should always valid (but could be '\0'). 1423da15bd9dSWang Nan */ 1424da15bd9dSWang Nan if (tmp && !strncmp(tmp, "0x", 2)) { 1425da15bd9dSWang Nan pp->abs_address = strtoul(pp->function, &tmp, 0); 1426da15bd9dSWang Nan if (*tmp != '\0') { 1427da15bd9dSWang Nan semantic_error("Invalid absolute address.\n"); 1428da15bd9dSWang Nan return -EINVAL; 1429da15bd9dSWang Nan } 1430da15bd9dSWang Nan } 1431da15bd9dSWang Nan } 1432da15bd9dSWang Nan 143350656eecSMasami Hiramatsu /* Parse other options */ 143450656eecSMasami Hiramatsu while (ptr) { 143550656eecSMasami Hiramatsu arg = ptr; 143650656eecSMasami Hiramatsu c = nc; 14372a9c8c36SMasami Hiramatsu if (c == ';') { /* Lazy pattern must be the last part */ 143802b95dadSMasami Hiramatsu pp->lazy_line = strdup(arg); 143902b95dadSMasami Hiramatsu if (pp->lazy_line == NULL) 144002b95dadSMasami Hiramatsu return -ENOMEM; 14412a9c8c36SMasami Hiramatsu break; 14422a9c8c36SMasami Hiramatsu } 14432a9c8c36SMasami Hiramatsu ptr = strpbrk(arg, ";:+@%"); 144450656eecSMasami Hiramatsu if (ptr) { 144550656eecSMasami Hiramatsu nc = *ptr; 144650656eecSMasami Hiramatsu *ptr++ = '\0'; 144750656eecSMasami Hiramatsu } 144850656eecSMasami Hiramatsu switch (c) { 144950656eecSMasami Hiramatsu case ':': /* Line number */ 145050656eecSMasami Hiramatsu pp->line = strtoul(arg, &tmp, 0); 1451146a1439SMasami Hiramatsu if (*tmp != '\0') { 14522a9c8c36SMasami Hiramatsu semantic_error("There is non-digit char" 1453146a1439SMasami Hiramatsu " in line number.\n"); 1454146a1439SMasami Hiramatsu return -EINVAL; 1455146a1439SMasami Hiramatsu } 145650656eecSMasami Hiramatsu break; 145750656eecSMasami Hiramatsu case '+': /* Byte offset from a symbol */ 145850656eecSMasami Hiramatsu pp->offset = strtoul(arg, &tmp, 0); 1459146a1439SMasami Hiramatsu if (*tmp != '\0') { 14602a9c8c36SMasami Hiramatsu semantic_error("There is non-digit character" 1461146a1439SMasami Hiramatsu " in offset.\n"); 1462146a1439SMasami Hiramatsu return -EINVAL; 1463146a1439SMasami Hiramatsu } 146450656eecSMasami Hiramatsu break; 146550656eecSMasami Hiramatsu case '@': /* File name */ 1466146a1439SMasami Hiramatsu if (pp->file) { 1467146a1439SMasami Hiramatsu semantic_error("SRC@SRC is not allowed.\n"); 1468146a1439SMasami Hiramatsu return -EINVAL; 1469146a1439SMasami Hiramatsu } 147002b95dadSMasami Hiramatsu pp->file = strdup(arg); 147102b95dadSMasami Hiramatsu if (pp->file == NULL) 147202b95dadSMasami Hiramatsu return -ENOMEM; 147350656eecSMasami Hiramatsu break; 147450656eecSMasami Hiramatsu case '%': /* Probe places */ 147550656eecSMasami Hiramatsu if (strcmp(arg, "return") == 0) { 147650656eecSMasami Hiramatsu pp->retprobe = 1; 1477146a1439SMasami Hiramatsu } else { /* Others not supported yet */ 1478146a1439SMasami Hiramatsu semantic_error("%%%s is not supported.\n", arg); 1479146a1439SMasami Hiramatsu return -ENOTSUP; 1480146a1439SMasami Hiramatsu } 148150656eecSMasami Hiramatsu break; 1482146a1439SMasami Hiramatsu default: /* Buggy case */ 1483146a1439SMasami Hiramatsu pr_err("This program has a bug at %s:%d.\n", 1484146a1439SMasami Hiramatsu __FILE__, __LINE__); 1485146a1439SMasami Hiramatsu return -ENOTSUP; 148650656eecSMasami Hiramatsu break; 148750656eecSMasami Hiramatsu } 148850656eecSMasami Hiramatsu } 148950656eecSMasami Hiramatsu 149050656eecSMasami Hiramatsu /* Exclusion check */ 1491146a1439SMasami Hiramatsu if (pp->lazy_line && pp->line) { 14920e43e5d2SMasami Hiramatsu semantic_error("Lazy pattern can't be used with" 14930e43e5d2SMasami Hiramatsu " line number.\n"); 1494146a1439SMasami Hiramatsu return -EINVAL; 1495146a1439SMasami Hiramatsu } 14962a9c8c36SMasami Hiramatsu 1497146a1439SMasami Hiramatsu if (pp->lazy_line && pp->offset) { 14980e43e5d2SMasami Hiramatsu semantic_error("Lazy pattern can't be used with offset.\n"); 1499146a1439SMasami Hiramatsu return -EINVAL; 1500146a1439SMasami Hiramatsu } 15012a9c8c36SMasami Hiramatsu 1502146a1439SMasami Hiramatsu if (pp->line && pp->offset) { 15030e43e5d2SMasami Hiramatsu semantic_error("Offset can't be used with line number.\n"); 1504146a1439SMasami Hiramatsu return -EINVAL; 1505146a1439SMasami Hiramatsu } 150650656eecSMasami Hiramatsu 1507146a1439SMasami Hiramatsu if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { 15082a9c8c36SMasami Hiramatsu semantic_error("File always requires line number or " 15090e43e5d2SMasami Hiramatsu "lazy pattern.\n"); 1510146a1439SMasami Hiramatsu return -EINVAL; 1511146a1439SMasami Hiramatsu } 151250656eecSMasami Hiramatsu 1513146a1439SMasami Hiramatsu if (pp->offset && !pp->function) { 15140e43e5d2SMasami Hiramatsu semantic_error("Offset requires an entry function.\n"); 1515146a1439SMasami Hiramatsu return -EINVAL; 1516146a1439SMasami Hiramatsu } 151750656eecSMasami Hiramatsu 1518146a1439SMasami Hiramatsu if (pp->retprobe && !pp->function) { 15190e43e5d2SMasami Hiramatsu semantic_error("Return probe requires an entry function.\n"); 1520146a1439SMasami Hiramatsu return -EINVAL; 1521146a1439SMasami Hiramatsu } 152250656eecSMasami Hiramatsu 1523146a1439SMasami Hiramatsu if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { 15242a9c8c36SMasami Hiramatsu semantic_error("Offset/Line/Lazy pattern can't be used with " 15250e43e5d2SMasami Hiramatsu "return probe.\n"); 1526146a1439SMasami Hiramatsu return -EINVAL; 1527146a1439SMasami Hiramatsu } 152850656eecSMasami Hiramatsu 15294235b045SMasami Hiramatsu pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", 15302a9c8c36SMasami Hiramatsu pp->function, pp->file, pp->line, pp->offset, pp->retprobe, 15312a9c8c36SMasami Hiramatsu pp->lazy_line); 1532146a1439SMasami Hiramatsu return 0; 153350656eecSMasami Hiramatsu } 153450656eecSMasami Hiramatsu 15357df2f329SMasami Hiramatsu /* Parse perf-probe event argument */ 1536146a1439SMasami Hiramatsu static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) 15377df2f329SMasami Hiramatsu { 1538b2a3c12bSMasami Hiramatsu char *tmp, *goodname; 15397df2f329SMasami Hiramatsu struct perf_probe_arg_field **fieldp; 15407df2f329SMasami Hiramatsu 15417df2f329SMasami Hiramatsu pr_debug("parsing arg: %s into ", str); 15427df2f329SMasami Hiramatsu 154348481938SMasami Hiramatsu tmp = strchr(str, '='); 154448481938SMasami Hiramatsu if (tmp) { 154502b95dadSMasami Hiramatsu arg->name = strndup(str, tmp - str); 154602b95dadSMasami Hiramatsu if (arg->name == NULL) 154702b95dadSMasami Hiramatsu return -ENOMEM; 154811a1ca35SMasami Hiramatsu pr_debug("name:%s ", arg->name); 154948481938SMasami Hiramatsu str = tmp + 1; 155048481938SMasami Hiramatsu } 155148481938SMasami Hiramatsu 155211a1ca35SMasami Hiramatsu tmp = strchr(str, ':'); 155311a1ca35SMasami Hiramatsu if (tmp) { /* Type setting */ 155411a1ca35SMasami Hiramatsu *tmp = '\0'; 155502b95dadSMasami Hiramatsu arg->type = strdup(tmp + 1); 155602b95dadSMasami Hiramatsu if (arg->type == NULL) 155702b95dadSMasami Hiramatsu return -ENOMEM; 155811a1ca35SMasami Hiramatsu pr_debug("type:%s ", arg->type); 155911a1ca35SMasami Hiramatsu } 156011a1ca35SMasami Hiramatsu 1561b2a3c12bSMasami Hiramatsu tmp = strpbrk(str, "-.["); 15627df2f329SMasami Hiramatsu if (!is_c_varname(str) || !tmp) { 15637df2f329SMasami Hiramatsu /* A variable, register, symbol or special value */ 156402b95dadSMasami Hiramatsu arg->var = strdup(str); 156502b95dadSMasami Hiramatsu if (arg->var == NULL) 156602b95dadSMasami Hiramatsu return -ENOMEM; 156748481938SMasami Hiramatsu pr_debug("%s\n", arg->var); 1568146a1439SMasami Hiramatsu return 0; 15697df2f329SMasami Hiramatsu } 15707df2f329SMasami Hiramatsu 1571b2a3c12bSMasami Hiramatsu /* Structure fields or array element */ 157202b95dadSMasami Hiramatsu arg->var = strndup(str, tmp - str); 157302b95dadSMasami Hiramatsu if (arg->var == NULL) 157402b95dadSMasami Hiramatsu return -ENOMEM; 1575b2a3c12bSMasami Hiramatsu goodname = arg->var; 157648481938SMasami Hiramatsu pr_debug("%s, ", arg->var); 15777df2f329SMasami Hiramatsu fieldp = &arg->field; 15787df2f329SMasami Hiramatsu 15797df2f329SMasami Hiramatsu do { 1580e334016fSMasami Hiramatsu *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); 1581e334016fSMasami Hiramatsu if (*fieldp == NULL) 1582e334016fSMasami Hiramatsu return -ENOMEM; 1583b2a3c12bSMasami Hiramatsu if (*tmp == '[') { /* Array */ 1584b2a3c12bSMasami Hiramatsu str = tmp; 1585b2a3c12bSMasami Hiramatsu (*fieldp)->index = strtol(str + 1, &tmp, 0); 1586b2a3c12bSMasami Hiramatsu (*fieldp)->ref = true; 1587b2a3c12bSMasami Hiramatsu if (*tmp != ']' || tmp == str + 1) { 1588b2a3c12bSMasami Hiramatsu semantic_error("Array index must be a" 1589b2a3c12bSMasami Hiramatsu " number.\n"); 1590b2a3c12bSMasami Hiramatsu return -EINVAL; 1591b2a3c12bSMasami Hiramatsu } 1592b2a3c12bSMasami Hiramatsu tmp++; 1593b2a3c12bSMasami Hiramatsu if (*tmp == '\0') 1594b2a3c12bSMasami Hiramatsu tmp = NULL; 1595b2a3c12bSMasami Hiramatsu } else { /* Structure */ 15967df2f329SMasami Hiramatsu if (*tmp == '.') { 15977df2f329SMasami Hiramatsu str = tmp + 1; 15987df2f329SMasami Hiramatsu (*fieldp)->ref = false; 15997df2f329SMasami Hiramatsu } else if (tmp[1] == '>') { 16007df2f329SMasami Hiramatsu str = tmp + 2; 16017df2f329SMasami Hiramatsu (*fieldp)->ref = true; 1602146a1439SMasami Hiramatsu } else { 1603b2a3c12bSMasami Hiramatsu semantic_error("Argument parse error: %s\n", 1604b2a3c12bSMasami Hiramatsu str); 1605146a1439SMasami Hiramatsu return -EINVAL; 1606146a1439SMasami Hiramatsu } 1607b2a3c12bSMasami Hiramatsu tmp = strpbrk(str, "-.["); 1608b2a3c12bSMasami Hiramatsu } 16097df2f329SMasami Hiramatsu if (tmp) { 161002b95dadSMasami Hiramatsu (*fieldp)->name = strndup(str, tmp - str); 161102b95dadSMasami Hiramatsu if ((*fieldp)->name == NULL) 161202b95dadSMasami Hiramatsu return -ENOMEM; 1613b2a3c12bSMasami Hiramatsu if (*str != '[') 1614b2a3c12bSMasami Hiramatsu goodname = (*fieldp)->name; 16157df2f329SMasami Hiramatsu pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); 16167df2f329SMasami Hiramatsu fieldp = &(*fieldp)->next; 16177df2f329SMasami Hiramatsu } 16187df2f329SMasami Hiramatsu } while (tmp); 161902b95dadSMasami Hiramatsu (*fieldp)->name = strdup(str); 162002b95dadSMasami Hiramatsu if ((*fieldp)->name == NULL) 162102b95dadSMasami Hiramatsu return -ENOMEM; 1622b2a3c12bSMasami Hiramatsu if (*str != '[') 1623b2a3c12bSMasami Hiramatsu goodname = (*fieldp)->name; 16247df2f329SMasami Hiramatsu pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); 1625df0faf4bSMasami Hiramatsu 1626b2a3c12bSMasami Hiramatsu /* If no name is specified, set the last field name (not array index)*/ 162702b95dadSMasami Hiramatsu if (!arg->name) { 1628b2a3c12bSMasami Hiramatsu arg->name = strdup(goodname); 162902b95dadSMasami Hiramatsu if (arg->name == NULL) 163002b95dadSMasami Hiramatsu return -ENOMEM; 163102b95dadSMasami Hiramatsu } 1632146a1439SMasami Hiramatsu return 0; 16337df2f329SMasami Hiramatsu } 16347df2f329SMasami Hiramatsu 16354235b045SMasami Hiramatsu /* Parse perf-probe event command */ 1636146a1439SMasami Hiramatsu int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) 163750656eecSMasami Hiramatsu { 1638e1c01d61SMasami Hiramatsu char **argv; 1639146a1439SMasami Hiramatsu int argc, i, ret = 0; 1640fac13fd5SMasami Hiramatsu 16414235b045SMasami Hiramatsu argv = argv_split(cmd, &argc); 1642146a1439SMasami Hiramatsu if (!argv) { 1643146a1439SMasami Hiramatsu pr_debug("Failed to split arguments.\n"); 1644146a1439SMasami Hiramatsu return -ENOMEM; 1645146a1439SMasami Hiramatsu } 1646146a1439SMasami Hiramatsu if (argc - 1 > MAX_PROBE_ARGS) { 1647146a1439SMasami Hiramatsu semantic_error("Too many probe arguments (%d).\n", argc - 1); 1648146a1439SMasami Hiramatsu ret = -ERANGE; 1649146a1439SMasami Hiramatsu goto out; 1650146a1439SMasami Hiramatsu } 165150656eecSMasami Hiramatsu /* Parse probe point */ 1652146a1439SMasami Hiramatsu ret = parse_perf_probe_point(argv[0], pev); 1653146a1439SMasami Hiramatsu if (ret < 0) 1654146a1439SMasami Hiramatsu goto out; 165550656eecSMasami Hiramatsu 1656e1c01d61SMasami Hiramatsu /* Copy arguments and ensure return probe has no C argument */ 16574235b045SMasami Hiramatsu pev->nargs = argc - 1; 1658e334016fSMasami Hiramatsu pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 1659e334016fSMasami Hiramatsu if (pev->args == NULL) { 1660e334016fSMasami Hiramatsu ret = -ENOMEM; 1661e334016fSMasami Hiramatsu goto out; 1662e334016fSMasami Hiramatsu } 1663146a1439SMasami Hiramatsu for (i = 0; i < pev->nargs && ret >= 0; i++) { 1664146a1439SMasami Hiramatsu ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); 1665146a1439SMasami Hiramatsu if (ret >= 0 && 1666146a1439SMasami Hiramatsu is_c_varname(pev->args[i].var) && pev->point.retprobe) { 16674235b045SMasami Hiramatsu semantic_error("You can't specify local variable for" 1668146a1439SMasami Hiramatsu " kretprobe.\n"); 1669146a1439SMasami Hiramatsu ret = -EINVAL; 1670e1c01d61SMasami Hiramatsu } 1671146a1439SMasami Hiramatsu } 1672146a1439SMasami Hiramatsu out: 1673e1c01d61SMasami Hiramatsu argv_free(argv); 1674146a1439SMasami Hiramatsu 1675146a1439SMasami Hiramatsu return ret; 167650656eecSMasami Hiramatsu } 167750656eecSMasami Hiramatsu 1678b3f33f93SRavi Bangoria /* Returns true if *any* ARG is either C variable, $params or $vars. */ 1679b3f33f93SRavi Bangoria bool perf_probe_with_var(struct perf_probe_event *pev) 16804de189feSMasami Hiramatsu { 1681b3f33f93SRavi Bangoria int i = 0; 16824235b045SMasami Hiramatsu 16834235b045SMasami Hiramatsu for (i = 0; i < pev->nargs; i++) 1684f6eb0518SMasami Hiramatsu if (is_c_varname(pev->args[i].var) || 1685b3f33f93SRavi Bangoria !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || 1686b3f33f93SRavi Bangoria !strcmp(pev->args[i].var, PROBE_ARG_VARS)) 1687b3f33f93SRavi Bangoria return true; 1688b3f33f93SRavi Bangoria return false; 1689b3f33f93SRavi Bangoria } 1690b3f33f93SRavi Bangoria 1691b3f33f93SRavi Bangoria /* Return true if this perf_probe_event requires debuginfo */ 1692b3f33f93SRavi Bangoria bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) 1693b3f33f93SRavi Bangoria { 1694b3f33f93SRavi Bangoria if (pev->point.file || pev->point.line || pev->point.lazy_line) 1695b3f33f93SRavi Bangoria return true; 1696b3f33f93SRavi Bangoria 1697b3f33f93SRavi Bangoria if (perf_probe_with_var(pev)) 16984235b045SMasami Hiramatsu return true; 16994235b045SMasami Hiramatsu 17004235b045SMasami Hiramatsu return false; 17014235b045SMasami Hiramatsu } 17024235b045SMasami Hiramatsu 17030e60836bSSrikar Dronamraju /* Parse probe_events event into struct probe_point */ 170492f6c72eSMasami Hiramatsu int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) 17054235b045SMasami Hiramatsu { 17060e60836bSSrikar Dronamraju struct probe_trace_point *tp = &tev->point; 17074de189feSMasami Hiramatsu char pr; 17084de189feSMasami Hiramatsu char *p; 1709bcbd0040SIrina Tirdea char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; 17104de189feSMasami Hiramatsu int ret, i, argc; 17114de189feSMasami Hiramatsu char **argv; 17124de189feSMasami Hiramatsu 17130e60836bSSrikar Dronamraju pr_debug("Parsing probe_events: %s\n", cmd); 17144235b045SMasami Hiramatsu argv = argv_split(cmd, &argc); 1715146a1439SMasami Hiramatsu if (!argv) { 1716146a1439SMasami Hiramatsu pr_debug("Failed to split arguments.\n"); 1717146a1439SMasami Hiramatsu return -ENOMEM; 1718146a1439SMasami Hiramatsu } 1719146a1439SMasami Hiramatsu if (argc < 2) { 1720146a1439SMasami Hiramatsu semantic_error("Too few probe arguments.\n"); 1721146a1439SMasami Hiramatsu ret = -ERANGE; 1722146a1439SMasami Hiramatsu goto out; 1723146a1439SMasami Hiramatsu } 17244de189feSMasami Hiramatsu 17254de189feSMasami Hiramatsu /* Scan event and group name. */ 1726bcbd0040SIrina Tirdea argv0_str = strdup(argv[0]); 1727bcbd0040SIrina Tirdea if (argv0_str == NULL) { 1728bcbd0040SIrina Tirdea ret = -ENOMEM; 1729bcbd0040SIrina Tirdea goto out; 1730bcbd0040SIrina Tirdea } 1731bcbd0040SIrina Tirdea fmt1_str = strtok_r(argv0_str, ":", &fmt); 1732bcbd0040SIrina Tirdea fmt2_str = strtok_r(NULL, "/", &fmt); 1733bcbd0040SIrina Tirdea fmt3_str = strtok_r(NULL, " \t", &fmt); 1734bcbd0040SIrina Tirdea if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL 1735bcbd0040SIrina Tirdea || fmt3_str == NULL) { 1736146a1439SMasami Hiramatsu semantic_error("Failed to parse event name: %s\n", argv[0]); 1737146a1439SMasami Hiramatsu ret = -EINVAL; 1738146a1439SMasami Hiramatsu goto out; 1739146a1439SMasami Hiramatsu } 1740bcbd0040SIrina Tirdea pr = fmt1_str[0]; 1741bcbd0040SIrina Tirdea tev->group = strdup(fmt2_str); 1742bcbd0040SIrina Tirdea tev->event = strdup(fmt3_str); 1743bcbd0040SIrina Tirdea if (tev->group == NULL || tev->event == NULL) { 1744bcbd0040SIrina Tirdea ret = -ENOMEM; 1745bcbd0040SIrina Tirdea goto out; 1746bcbd0040SIrina Tirdea } 17474235b045SMasami Hiramatsu pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); 17484de189feSMasami Hiramatsu 17494235b045SMasami Hiramatsu tp->retprobe = (pr == 'r'); 17504de189feSMasami Hiramatsu 1751190b57fcSMasami Hiramatsu /* Scan module name(if there), function name and offset */ 1752190b57fcSMasami Hiramatsu p = strchr(argv[1], ':'); 1753190b57fcSMasami Hiramatsu if (p) { 1754190b57fcSMasami Hiramatsu tp->module = strndup(argv[1], p - argv[1]); 1755844faa4bSMasami Hiramatsu if (!tp->module) { 1756844faa4bSMasami Hiramatsu ret = -ENOMEM; 1757844faa4bSMasami Hiramatsu goto out; 1758844faa4bSMasami Hiramatsu } 175942bba263SMasami Hiramatsu tev->uprobes = (tp->module[0] == '/'); 1760190b57fcSMasami Hiramatsu p++; 1761190b57fcSMasami Hiramatsu } else 1762190b57fcSMasami Hiramatsu p = argv[1]; 1763bcbd0040SIrina Tirdea fmt1_str = strtok_r(p, "+", &fmt); 1764be07afe9SWang Nan /* only the address started with 0x */ 1765be07afe9SWang Nan if (fmt1_str[0] == '0') { 1766be07afe9SWang Nan /* 1767be07afe9SWang Nan * Fix a special case: 1768be07afe9SWang Nan * if address == 0, kernel reports something like: 1769be07afe9SWang Nan * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax 1770be07afe9SWang Nan * Newer kernel may fix that, but we want to 1771be07afe9SWang Nan * support old kernel also. 1772be07afe9SWang Nan */ 1773be07afe9SWang Nan if (strcmp(fmt1_str, "0x") == 0) { 1774be07afe9SWang Nan if (!argv[2] || strcmp(argv[2], "(null)")) { 1775be07afe9SWang Nan ret = -EINVAL; 1776be07afe9SWang Nan goto out; 1777be07afe9SWang Nan } 1778be07afe9SWang Nan tp->address = 0; 1779be07afe9SWang Nan 1780be07afe9SWang Nan free(argv[2]); 1781be07afe9SWang Nan for (i = 2; argv[i + 1] != NULL; i++) 1782be07afe9SWang Nan argv[i] = argv[i + 1]; 1783be07afe9SWang Nan 1784be07afe9SWang Nan argv[i] = NULL; 1785be07afe9SWang Nan argc -= 1; 1786be07afe9SWang Nan } else 17875a6f6314SMasami Hiramatsu tp->address = strtoul(fmt1_str, NULL, 0); 1788be07afe9SWang Nan } else { 17895a6f6314SMasami Hiramatsu /* Only the symbol-based probe has offset */ 1790bcbd0040SIrina Tirdea tp->symbol = strdup(fmt1_str); 1791bcbd0040SIrina Tirdea if (tp->symbol == NULL) { 1792bcbd0040SIrina Tirdea ret = -ENOMEM; 1793bcbd0040SIrina Tirdea goto out; 1794bcbd0040SIrina Tirdea } 1795bcbd0040SIrina Tirdea fmt2_str = strtok_r(NULL, "", &fmt); 1796bcbd0040SIrina Tirdea if (fmt2_str == NULL) 17974235b045SMasami Hiramatsu tp->offset = 0; 1798bcbd0040SIrina Tirdea else 1799bcbd0040SIrina Tirdea tp->offset = strtoul(fmt2_str, NULL, 10); 18005a6f6314SMasami Hiramatsu } 18014de189feSMasami Hiramatsu 18024235b045SMasami Hiramatsu tev->nargs = argc - 2; 18030e60836bSSrikar Dronamraju tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1804e334016fSMasami Hiramatsu if (tev->args == NULL) { 1805e334016fSMasami Hiramatsu ret = -ENOMEM; 1806e334016fSMasami Hiramatsu goto out; 1807e334016fSMasami Hiramatsu } 18084235b045SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) { 18094de189feSMasami Hiramatsu p = strchr(argv[i + 2], '='); 18104de189feSMasami Hiramatsu if (p) /* We don't need which register is assigned. */ 18114235b045SMasami Hiramatsu *p++ = '\0'; 18124235b045SMasami Hiramatsu else 18134235b045SMasami Hiramatsu p = argv[i + 2]; 181402b95dadSMasami Hiramatsu tev->args[i].name = strdup(argv[i + 2]); 18154235b045SMasami Hiramatsu /* TODO: parse regs and offset */ 181602b95dadSMasami Hiramatsu tev->args[i].value = strdup(p); 181702b95dadSMasami Hiramatsu if (tev->args[i].name == NULL || tev->args[i].value == NULL) { 181802b95dadSMasami Hiramatsu ret = -ENOMEM; 181902b95dadSMasami Hiramatsu goto out; 182002b95dadSMasami Hiramatsu } 18214de189feSMasami Hiramatsu } 1822146a1439SMasami Hiramatsu ret = 0; 1823146a1439SMasami Hiramatsu out: 1824bcbd0040SIrina Tirdea free(argv0_str); 18254de189feSMasami Hiramatsu argv_free(argv); 1826146a1439SMasami Hiramatsu return ret; 18274de189feSMasami Hiramatsu } 18284de189feSMasami Hiramatsu 18297df2f329SMasami Hiramatsu /* Compose only probe arg */ 1830909b0360SMasami Hiramatsu char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) 18317df2f329SMasami Hiramatsu { 18327df2f329SMasami Hiramatsu struct perf_probe_arg_field *field = pa->field; 1833909b0360SMasami Hiramatsu struct strbuf buf; 1834bf4d5f25SMasami Hiramatsu char *ret = NULL; 1835bf4d5f25SMasami Hiramatsu int err; 18367df2f329SMasami Hiramatsu 1837bf4d5f25SMasami Hiramatsu if (strbuf_init(&buf, 64) < 0) 1838bf4d5f25SMasami Hiramatsu return NULL; 1839bf4d5f25SMasami Hiramatsu 184048481938SMasami Hiramatsu if (pa->name && pa->var) 1841bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); 184248481938SMasami Hiramatsu else 1843bf4d5f25SMasami Hiramatsu err = strbuf_addstr(&buf, pa->name ?: pa->var); 1844bf4d5f25SMasami Hiramatsu if (err) 1845bf4d5f25SMasami Hiramatsu goto out; 18467df2f329SMasami Hiramatsu 18477df2f329SMasami Hiramatsu while (field) { 1848b2a3c12bSMasami Hiramatsu if (field->name[0] == '[') 1849bf4d5f25SMasami Hiramatsu err = strbuf_addstr(&buf, field->name); 1850b2a3c12bSMasami Hiramatsu else 1851bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", 1852909b0360SMasami Hiramatsu field->name); 18537df2f329SMasami Hiramatsu field = field->next; 1854bf4d5f25SMasami Hiramatsu if (err) 1855bf4d5f25SMasami Hiramatsu goto out; 18567df2f329SMasami Hiramatsu } 185711a1ca35SMasami Hiramatsu 1858909b0360SMasami Hiramatsu if (pa->type) 1859bf4d5f25SMasami Hiramatsu if (strbuf_addf(&buf, ":%s", pa->type) < 0) 1860bf4d5f25SMasami Hiramatsu goto out; 186111a1ca35SMasami Hiramatsu 1862909b0360SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 1863bf4d5f25SMasami Hiramatsu out: 1864bf4d5f25SMasami Hiramatsu strbuf_release(&buf); 1865146a1439SMasami Hiramatsu return ret; 18667df2f329SMasami Hiramatsu } 18677df2f329SMasami Hiramatsu 18684235b045SMasami Hiramatsu /* Compose only probe point (not argument) */ 1869c4ff4920SMasami Hiramatsu char *synthesize_perf_probe_point(struct perf_probe_point *pp) 187050656eecSMasami Hiramatsu { 1871909b0360SMasami Hiramatsu struct strbuf buf; 1872bf4d5f25SMasami Hiramatsu char *tmp, *ret = NULL; 1873bf4d5f25SMasami Hiramatsu int len, err = 0; 187450656eecSMasami Hiramatsu 1875bf4d5f25SMasami Hiramatsu if (strbuf_init(&buf, 64) < 0) 1876bf4d5f25SMasami Hiramatsu return NULL; 1877bf4d5f25SMasami Hiramatsu 1878909b0360SMasami Hiramatsu if (pp->function) { 1879bf4d5f25SMasami Hiramatsu if (strbuf_addstr(&buf, pp->function) < 0) 1880bf4d5f25SMasami Hiramatsu goto out; 1881909b0360SMasami Hiramatsu if (pp->offset) 1882bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "+%lu", pp->offset); 1883909b0360SMasami Hiramatsu else if (pp->line) 1884bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, ":%d", pp->line); 1885909b0360SMasami Hiramatsu else if (pp->retprobe) 1886bf4d5f25SMasami Hiramatsu err = strbuf_addstr(&buf, "%return"); 1887bf4d5f25SMasami Hiramatsu if (err) 1888bf4d5f25SMasami Hiramatsu goto out; 1889fb1587d8SMasami Hiramatsu } 1890fb1587d8SMasami Hiramatsu if (pp->file) { 189132ae2adeSFranck Bui-Huu tmp = pp->file; 189232ae2adeSFranck Bui-Huu len = strlen(tmp); 189332ae2adeSFranck Bui-Huu if (len > 30) { 189432ae2adeSFranck Bui-Huu tmp = strchr(pp->file + len - 30, '/'); 189532ae2adeSFranck Bui-Huu tmp = tmp ? tmp + 1 : pp->file + len - 30; 189632ae2adeSFranck Bui-Huu } 1897bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "@%s", tmp); 1898bf4d5f25SMasami Hiramatsu if (!err && !pp->function && pp->line) 1899bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, ":%d", pp->line); 19004de189feSMasami Hiramatsu } 1901bf4d5f25SMasami Hiramatsu if (!err) 1902bf4d5f25SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 1903bf4d5f25SMasami Hiramatsu out: 1904bf4d5f25SMasami Hiramatsu strbuf_release(&buf); 1905bf4d5f25SMasami Hiramatsu return ret; 19064235b045SMasami Hiramatsu } 19074235b045SMasami Hiramatsu 19084235b045SMasami Hiramatsu char *synthesize_perf_probe_command(struct perf_probe_event *pev) 19094235b045SMasami Hiramatsu { 1910c4ff4920SMasami Hiramatsu struct strbuf buf; 1911c4ff4920SMasami Hiramatsu char *tmp, *ret = NULL; 1912c4ff4920SMasami Hiramatsu int i; 19134235b045SMasami Hiramatsu 1914c4ff4920SMasami Hiramatsu if (strbuf_init(&buf, 64)) 19154235b045SMasami Hiramatsu return NULL; 1916c4ff4920SMasami Hiramatsu if (pev->event) 1917c4ff4920SMasami Hiramatsu if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, 1918c4ff4920SMasami Hiramatsu pev->event) < 0) 1919c4ff4920SMasami Hiramatsu goto out; 19204235b045SMasami Hiramatsu 1921c4ff4920SMasami Hiramatsu tmp = synthesize_perf_probe_point(&pev->point); 1922c4ff4920SMasami Hiramatsu if (!tmp || strbuf_addstr(&buf, tmp) < 0) 1923c4ff4920SMasami Hiramatsu goto out; 1924c4ff4920SMasami Hiramatsu free(tmp); 1925c4ff4920SMasami Hiramatsu 19264235b045SMasami Hiramatsu for (i = 0; i < pev->nargs; i++) { 1927c4ff4920SMasami Hiramatsu tmp = synthesize_perf_probe_arg(pev->args + i); 1928c4ff4920SMasami Hiramatsu if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) 1929c4ff4920SMasami Hiramatsu goto out; 1930c4ff4920SMasami Hiramatsu free(tmp); 19317ef17aafSMasami Hiramatsu } 193250656eecSMasami Hiramatsu 1933c4ff4920SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 1934c4ff4920SMasami Hiramatsu out: 1935c4ff4920SMasami Hiramatsu strbuf_release(&buf); 1936c4ff4920SMasami Hiramatsu return ret; 19374235b045SMasami Hiramatsu } 19384235b045SMasami Hiramatsu 19390e60836bSSrikar Dronamraju static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, 1940909b0360SMasami Hiramatsu struct strbuf *buf, int depth) 19417ef17aafSMasami Hiramatsu { 1942bf4d5f25SMasami Hiramatsu int err; 19434235b045SMasami Hiramatsu if (ref->next) { 19440e60836bSSrikar Dronamraju depth = __synthesize_probe_trace_arg_ref(ref->next, buf, 1945909b0360SMasami Hiramatsu depth + 1); 19464235b045SMasami Hiramatsu if (depth < 0) 19474235b045SMasami Hiramatsu return depth; 19484235b045SMasami Hiramatsu } 1949bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, "%+ld(", ref->offset); 1950bf4d5f25SMasami Hiramatsu return (err < 0) ? err : depth; 1951bf4d5f25SMasami Hiramatsu } 19524235b045SMasami Hiramatsu 19530e60836bSSrikar Dronamraju static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, 1954909b0360SMasami Hiramatsu struct strbuf *buf) 19554235b045SMasami Hiramatsu { 19560e60836bSSrikar Dronamraju struct probe_trace_arg_ref *ref = arg->ref; 1957bf4d5f25SMasami Hiramatsu int depth = 0, err; 19584235b045SMasami Hiramatsu 19594235b045SMasami Hiramatsu /* Argument name or separator */ 19604235b045SMasami Hiramatsu if (arg->name) 1961bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, " %s=", arg->name); 19624235b045SMasami Hiramatsu else 1963bf4d5f25SMasami Hiramatsu err = strbuf_addch(buf, ' '); 1964bf4d5f25SMasami Hiramatsu if (err) 1965bf4d5f25SMasami Hiramatsu return err; 19664235b045SMasami Hiramatsu 1967b7dcb857SMasami Hiramatsu /* Special case: @XXX */ 1968b7dcb857SMasami Hiramatsu if (arg->value[0] == '@' && arg->ref) 1969b7dcb857SMasami Hiramatsu ref = ref->next; 1970b7dcb857SMasami Hiramatsu 19714235b045SMasami Hiramatsu /* Dereferencing arguments */ 1972b7dcb857SMasami Hiramatsu if (ref) { 1973909b0360SMasami Hiramatsu depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); 19744235b045SMasami Hiramatsu if (depth < 0) 19754235b045SMasami Hiramatsu return depth; 19764235b045SMasami Hiramatsu } 19774235b045SMasami Hiramatsu 19784235b045SMasami Hiramatsu /* Print argument value */ 1979b7dcb857SMasami Hiramatsu if (arg->value[0] == '@' && arg->ref) 1980bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); 1981b7dcb857SMasami Hiramatsu else 1982bf4d5f25SMasami Hiramatsu err = strbuf_addstr(buf, arg->value); 19834235b045SMasami Hiramatsu 19844235b045SMasami Hiramatsu /* Closing */ 1985bf4d5f25SMasami Hiramatsu while (!err && depth--) 1986bf4d5f25SMasami Hiramatsu err = strbuf_addch(buf, ')'); 19874235b045SMasami Hiramatsu 1988bf4d5f25SMasami Hiramatsu /* Print argument type */ 1989bf4d5f25SMasami Hiramatsu if (!err && arg->type) 1990bf4d5f25SMasami Hiramatsu err = strbuf_addf(buf, ":%s", arg->type); 1991bf4d5f25SMasami Hiramatsu 1992bf4d5f25SMasami Hiramatsu return err; 19934235b045SMasami Hiramatsu } 19944235b045SMasami Hiramatsu 19950e60836bSSrikar Dronamraju char *synthesize_probe_trace_command(struct probe_trace_event *tev) 19964235b045SMasami Hiramatsu { 19970e60836bSSrikar Dronamraju struct probe_trace_point *tp = &tev->point; 1998909b0360SMasami Hiramatsu struct strbuf buf; 1999909b0360SMasami Hiramatsu char *ret = NULL; 2000bf4d5f25SMasami Hiramatsu int i, err; 2001eb948e50SMasami Hiramatsu 2002da15bd9dSWang Nan /* Uprobes must have tp->module */ 2003da15bd9dSWang Nan if (tev->uprobes && !tp->module) 2004909b0360SMasami Hiramatsu return NULL; 2005909b0360SMasami Hiramatsu 2006bf4d5f25SMasami Hiramatsu if (strbuf_init(&buf, 32) < 0) 2007bf4d5f25SMasami Hiramatsu return NULL; 2008bf4d5f25SMasami Hiramatsu 2009bf4d5f25SMasami Hiramatsu if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', 2010bf4d5f25SMasami Hiramatsu tev->group, tev->event) < 0) 2011bf4d5f25SMasami Hiramatsu goto error; 2012da15bd9dSWang Nan /* 2013da15bd9dSWang Nan * If tp->address == 0, then this point must be a 2014da15bd9dSWang Nan * absolute address uprobe. 2015da15bd9dSWang Nan * try_to_find_absolute_address() should have made 2016da15bd9dSWang Nan * tp->symbol to "0x0". 2017da15bd9dSWang Nan */ 2018da15bd9dSWang Nan if (tev->uprobes && !tp->address) { 2019da15bd9dSWang Nan if (!tp->symbol || strcmp(tp->symbol, "0x0")) 2020da15bd9dSWang Nan goto error; 2021da15bd9dSWang Nan } 2022eb948e50SMasami Hiramatsu 2023eb948e50SMasami Hiramatsu /* Use the tp->address for uprobes */ 2024225466f1SSrikar Dronamraju if (tev->uprobes) 2025bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address); 2026da15bd9dSWang Nan else if (!strncmp(tp->symbol, "0x", 2)) 2027da15bd9dSWang Nan /* Absolute address. See try_to_find_absolute_address() */ 2028bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "", 2029909b0360SMasami Hiramatsu tp->module ? ":" : "", tp->address); 2030225466f1SSrikar Dronamraju else 2031bf4d5f25SMasami Hiramatsu err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "", 2032909b0360SMasami Hiramatsu tp->module ? ":" : "", tp->symbol, tp->offset); 2033bf4d5f25SMasami Hiramatsu if (err) 2034bf4d5f25SMasami Hiramatsu goto error; 2035225466f1SSrikar Dronamraju 2036909b0360SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) 2037909b0360SMasami Hiramatsu if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0) 20384235b045SMasami Hiramatsu goto error; 20397ef17aafSMasami Hiramatsu 2040909b0360SMasami Hiramatsu ret = strbuf_detach(&buf, NULL); 204150656eecSMasami Hiramatsu error: 2042909b0360SMasami Hiramatsu strbuf_release(&buf); 2043909b0360SMasami Hiramatsu return ret; 204450656eecSMasami Hiramatsu } 204550656eecSMasami Hiramatsu 20465a6f6314SMasami Hiramatsu static int find_perf_probe_point_from_map(struct probe_trace_point *tp, 20475a6f6314SMasami Hiramatsu struct perf_probe_point *pp, 20485a6f6314SMasami Hiramatsu bool is_kprobe) 20495a6f6314SMasami Hiramatsu { 20505a6f6314SMasami Hiramatsu struct symbol *sym = NULL; 20515a6f6314SMasami Hiramatsu struct map *map; 2052e486367fSWang Nan u64 addr = tp->address; 20535a6f6314SMasami Hiramatsu int ret = -ENOENT; 20545a6f6314SMasami Hiramatsu 20555a6f6314SMasami Hiramatsu if (!is_kprobe) { 20565a6f6314SMasami Hiramatsu map = dso__new_map(tp->module); 20575a6f6314SMasami Hiramatsu if (!map) 20585a6f6314SMasami Hiramatsu goto out; 2059be39db9fSArnaldo Carvalho de Melo sym = map__find_symbol(map, addr); 20605a6f6314SMasami Hiramatsu } else { 20619b239a12SMasami Hiramatsu if (tp->symbol && !addr) { 20620a62f686SMasami Hiramatsu if (kernel_get_symbol_address_by_name(tp->symbol, 20630a62f686SMasami Hiramatsu &addr, true, false) < 0) 20649b239a12SMasami Hiramatsu goto out; 20659b239a12SMasami Hiramatsu } 20665a6f6314SMasami Hiramatsu if (addr) { 20675a6f6314SMasami Hiramatsu addr += tp->offset; 20685a6f6314SMasami Hiramatsu sym = __find_kernel_function(addr, &map); 20695a6f6314SMasami Hiramatsu } 20705a6f6314SMasami Hiramatsu } 207198d3b258SWang Nan 20725a6f6314SMasami Hiramatsu if (!sym) 20735a6f6314SMasami Hiramatsu goto out; 20745a6f6314SMasami Hiramatsu 20755a6f6314SMasami Hiramatsu pp->retprobe = tp->retprobe; 20765a6f6314SMasami Hiramatsu pp->offset = addr - map->unmap_ip(map, sym->start); 20775a6f6314SMasami Hiramatsu pp->function = strdup(sym->name); 20785a6f6314SMasami Hiramatsu ret = pp->function ? 0 : -ENOMEM; 20795a6f6314SMasami Hiramatsu 20805a6f6314SMasami Hiramatsu out: 20815a6f6314SMasami Hiramatsu if (map && !is_kprobe) { 208284c2cafaSArnaldo Carvalho de Melo map__put(map); 20835a6f6314SMasami Hiramatsu } 20845a6f6314SMasami Hiramatsu 20855a6f6314SMasami Hiramatsu return ret; 20865a6f6314SMasami Hiramatsu } 20875a6f6314SMasami Hiramatsu 20885a6f6314SMasami Hiramatsu static int convert_to_perf_probe_point(struct probe_trace_point *tp, 20895a6f6314SMasami Hiramatsu struct perf_probe_point *pp, 20905a6f6314SMasami Hiramatsu bool is_kprobe) 20915a6f6314SMasami Hiramatsu { 20925a6f6314SMasami Hiramatsu char buf[128]; 20935a6f6314SMasami Hiramatsu int ret; 20945a6f6314SMasami Hiramatsu 20955a6f6314SMasami Hiramatsu ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); 20965a6f6314SMasami Hiramatsu if (!ret) 20975a6f6314SMasami Hiramatsu return 0; 20985a6f6314SMasami Hiramatsu ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); 20995a6f6314SMasami Hiramatsu if (!ret) 21005a6f6314SMasami Hiramatsu return 0; 21015a6f6314SMasami Hiramatsu 21025a6f6314SMasami Hiramatsu pr_debug("Failed to find probe point from both of dwarf and map.\n"); 21035a6f6314SMasami Hiramatsu 21045a6f6314SMasami Hiramatsu if (tp->symbol) { 21055a6f6314SMasami Hiramatsu pp->function = strdup(tp->symbol); 21065a6f6314SMasami Hiramatsu pp->offset = tp->offset; 2107614e2fdbSWang Nan } else { 21085a6f6314SMasami Hiramatsu ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); 21095a6f6314SMasami Hiramatsu if (ret < 0) 21105a6f6314SMasami Hiramatsu return ret; 21115a6f6314SMasami Hiramatsu pp->function = strdup(buf); 21125a6f6314SMasami Hiramatsu pp->offset = 0; 21135a6f6314SMasami Hiramatsu } 21145a6f6314SMasami Hiramatsu if (pp->function == NULL) 21155a6f6314SMasami Hiramatsu return -ENOMEM; 21165a6f6314SMasami Hiramatsu 21175a6f6314SMasami Hiramatsu pp->retprobe = tp->retprobe; 21185a6f6314SMasami Hiramatsu 21195a6f6314SMasami Hiramatsu return 0; 21205a6f6314SMasami Hiramatsu } 21215a6f6314SMasami Hiramatsu 21220e60836bSSrikar Dronamraju static int convert_to_perf_probe_event(struct probe_trace_event *tev, 2123225466f1SSrikar Dronamraju struct perf_probe_event *pev, bool is_kprobe) 21244de189feSMasami Hiramatsu { 2125909b0360SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 2126146a1439SMasami Hiramatsu int i, ret; 21274de189feSMasami Hiramatsu 21284b4da7f7SMasami Hiramatsu /* Convert event/group name */ 212902b95dadSMasami Hiramatsu pev->event = strdup(tev->event); 213002b95dadSMasami Hiramatsu pev->group = strdup(tev->group); 213102b95dadSMasami Hiramatsu if (pev->event == NULL || pev->group == NULL) 213202b95dadSMasami Hiramatsu return -ENOMEM; 2133fb1587d8SMasami Hiramatsu 21344b4da7f7SMasami Hiramatsu /* Convert trace_point to probe_point */ 21355a6f6314SMasami Hiramatsu ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); 2136146a1439SMasami Hiramatsu if (ret < 0) 2137146a1439SMasami Hiramatsu return ret; 21384b4da7f7SMasami Hiramatsu 21394235b045SMasami Hiramatsu /* Convert trace_arg to probe_arg */ 21404235b045SMasami Hiramatsu pev->nargs = tev->nargs; 2141e334016fSMasami Hiramatsu pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 2142e334016fSMasami Hiramatsu if (pev->args == NULL) 2143e334016fSMasami Hiramatsu return -ENOMEM; 214402b95dadSMasami Hiramatsu for (i = 0; i < tev->nargs && ret >= 0; i++) { 21454235b045SMasami Hiramatsu if (tev->args[i].name) 214602b95dadSMasami Hiramatsu pev->args[i].name = strdup(tev->args[i].name); 21474235b045SMasami Hiramatsu else { 2148bf4d5f25SMasami Hiramatsu if ((ret = strbuf_init(&buf, 32)) < 0) 2149bf4d5f25SMasami Hiramatsu goto error; 2150909b0360SMasami Hiramatsu ret = synthesize_probe_trace_arg(&tev->args[i], &buf); 2151909b0360SMasami Hiramatsu pev->args[i].name = strbuf_detach(&buf, NULL); 215202b95dadSMasami Hiramatsu } 215302b95dadSMasami Hiramatsu if (pev->args[i].name == NULL && ret >= 0) 215402b95dadSMasami Hiramatsu ret = -ENOMEM; 21554de189feSMasami Hiramatsu } 2156bf4d5f25SMasami Hiramatsu error: 2157146a1439SMasami Hiramatsu if (ret < 0) 2158146a1439SMasami Hiramatsu clear_perf_probe_event(pev); 2159146a1439SMasami Hiramatsu 2160146a1439SMasami Hiramatsu return ret; 21614235b045SMasami Hiramatsu } 21624de189feSMasami Hiramatsu 21634235b045SMasami Hiramatsu void clear_perf_probe_event(struct perf_probe_event *pev) 21644235b045SMasami Hiramatsu { 21657df2f329SMasami Hiramatsu struct perf_probe_arg_field *field, *next; 21664235b045SMasami Hiramatsu int i; 21674de189feSMasami Hiramatsu 21684235b045SMasami Hiramatsu free(pev->event); 21694235b045SMasami Hiramatsu free(pev->group); 21707afb3fabSMasami Hiramatsu free(pev->target); 21719b118acaSMasami Hiramatsu clear_perf_probe_point(&pev->point); 2172f5385650SArnaldo Carvalho de Melo 21737df2f329SMasami Hiramatsu for (i = 0; i < pev->nargs; i++) { 21744235b045SMasami Hiramatsu free(pev->args[i].name); 217548481938SMasami Hiramatsu free(pev->args[i].var); 217611a1ca35SMasami Hiramatsu free(pev->args[i].type); 21777df2f329SMasami Hiramatsu field = pev->args[i].field; 21787df2f329SMasami Hiramatsu while (field) { 21797df2f329SMasami Hiramatsu next = field->next; 218074cf249dSArnaldo Carvalho de Melo zfree(&field->name); 21817df2f329SMasami Hiramatsu free(field); 21827df2f329SMasami Hiramatsu field = next; 21837df2f329SMasami Hiramatsu } 21847df2f329SMasami Hiramatsu } 21854235b045SMasami Hiramatsu free(pev->args); 21864235b045SMasami Hiramatsu memset(pev, 0, sizeof(*pev)); 21874235b045SMasami Hiramatsu } 21884235b045SMasami Hiramatsu 21890542bb9cSMasami Hiramatsu #define strdup_or_goto(str, label) \ 21900542bb9cSMasami Hiramatsu ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) 21910542bb9cSMasami Hiramatsu 21920542bb9cSMasami Hiramatsu static int perf_probe_point__copy(struct perf_probe_point *dst, 21930542bb9cSMasami Hiramatsu struct perf_probe_point *src) 21940542bb9cSMasami Hiramatsu { 21950542bb9cSMasami Hiramatsu dst->file = strdup_or_goto(src->file, out_err); 21960542bb9cSMasami Hiramatsu dst->function = strdup_or_goto(src->function, out_err); 21970542bb9cSMasami Hiramatsu dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); 21980542bb9cSMasami Hiramatsu dst->line = src->line; 21990542bb9cSMasami Hiramatsu dst->retprobe = src->retprobe; 22000542bb9cSMasami Hiramatsu dst->offset = src->offset; 22010542bb9cSMasami Hiramatsu return 0; 22020542bb9cSMasami Hiramatsu 22030542bb9cSMasami Hiramatsu out_err: 22040542bb9cSMasami Hiramatsu clear_perf_probe_point(dst); 22050542bb9cSMasami Hiramatsu return -ENOMEM; 22060542bb9cSMasami Hiramatsu } 22070542bb9cSMasami Hiramatsu 22080542bb9cSMasami Hiramatsu static int perf_probe_arg__copy(struct perf_probe_arg *dst, 22090542bb9cSMasami Hiramatsu struct perf_probe_arg *src) 22100542bb9cSMasami Hiramatsu { 22110542bb9cSMasami Hiramatsu struct perf_probe_arg_field *field, **ppfield; 22120542bb9cSMasami Hiramatsu 22130542bb9cSMasami Hiramatsu dst->name = strdup_or_goto(src->name, out_err); 22140542bb9cSMasami Hiramatsu dst->var = strdup_or_goto(src->var, out_err); 22150542bb9cSMasami Hiramatsu dst->type = strdup_or_goto(src->type, out_err); 22160542bb9cSMasami Hiramatsu 22170542bb9cSMasami Hiramatsu field = src->field; 22180542bb9cSMasami Hiramatsu ppfield = &(dst->field); 22190542bb9cSMasami Hiramatsu while (field) { 22200542bb9cSMasami Hiramatsu *ppfield = zalloc(sizeof(*field)); 22210542bb9cSMasami Hiramatsu if (!*ppfield) 22220542bb9cSMasami Hiramatsu goto out_err; 22230542bb9cSMasami Hiramatsu (*ppfield)->name = strdup_or_goto(field->name, out_err); 22240542bb9cSMasami Hiramatsu (*ppfield)->index = field->index; 22250542bb9cSMasami Hiramatsu (*ppfield)->ref = field->ref; 22260542bb9cSMasami Hiramatsu field = field->next; 22270542bb9cSMasami Hiramatsu ppfield = &((*ppfield)->next); 22280542bb9cSMasami Hiramatsu } 22290542bb9cSMasami Hiramatsu return 0; 22300542bb9cSMasami Hiramatsu out_err: 22310542bb9cSMasami Hiramatsu return -ENOMEM; 22320542bb9cSMasami Hiramatsu } 22330542bb9cSMasami Hiramatsu 22340542bb9cSMasami Hiramatsu int perf_probe_event__copy(struct perf_probe_event *dst, 22350542bb9cSMasami Hiramatsu struct perf_probe_event *src) 22360542bb9cSMasami Hiramatsu { 22370542bb9cSMasami Hiramatsu int i; 22380542bb9cSMasami Hiramatsu 22390542bb9cSMasami Hiramatsu dst->event = strdup_or_goto(src->event, out_err); 22400542bb9cSMasami Hiramatsu dst->group = strdup_or_goto(src->group, out_err); 22410542bb9cSMasami Hiramatsu dst->target = strdup_or_goto(src->target, out_err); 22420542bb9cSMasami Hiramatsu dst->uprobes = src->uprobes; 22430542bb9cSMasami Hiramatsu 22440542bb9cSMasami Hiramatsu if (perf_probe_point__copy(&dst->point, &src->point) < 0) 22450542bb9cSMasami Hiramatsu goto out_err; 22460542bb9cSMasami Hiramatsu 22470542bb9cSMasami Hiramatsu dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); 22480542bb9cSMasami Hiramatsu if (!dst->args) 22490542bb9cSMasami Hiramatsu goto out_err; 22500542bb9cSMasami Hiramatsu dst->nargs = src->nargs; 22510542bb9cSMasami Hiramatsu 22520542bb9cSMasami Hiramatsu for (i = 0; i < src->nargs; i++) 22530542bb9cSMasami Hiramatsu if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) 22540542bb9cSMasami Hiramatsu goto out_err; 22550542bb9cSMasami Hiramatsu return 0; 22560542bb9cSMasami Hiramatsu 22570542bb9cSMasami Hiramatsu out_err: 22580542bb9cSMasami Hiramatsu clear_perf_probe_event(dst); 22590542bb9cSMasami Hiramatsu return -ENOMEM; 22600542bb9cSMasami Hiramatsu } 22610542bb9cSMasami Hiramatsu 226292f6c72eSMasami Hiramatsu void clear_probe_trace_event(struct probe_trace_event *tev) 22634235b045SMasami Hiramatsu { 22640e60836bSSrikar Dronamraju struct probe_trace_arg_ref *ref, *next; 22654235b045SMasami Hiramatsu int i; 22664235b045SMasami Hiramatsu 22674235b045SMasami Hiramatsu free(tev->event); 22684235b045SMasami Hiramatsu free(tev->group); 22694235b045SMasami Hiramatsu free(tev->point.symbol); 22704c859351SMasami Hiramatsu free(tev->point.realname); 2271190b57fcSMasami Hiramatsu free(tev->point.module); 22724235b045SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) { 22734235b045SMasami Hiramatsu free(tev->args[i].name); 22744235b045SMasami Hiramatsu free(tev->args[i].value); 22754984912eSMasami Hiramatsu free(tev->args[i].type); 22764235b045SMasami Hiramatsu ref = tev->args[i].ref; 22774235b045SMasami Hiramatsu while (ref) { 22784235b045SMasami Hiramatsu next = ref->next; 22794235b045SMasami Hiramatsu free(ref); 22804235b045SMasami Hiramatsu ref = next; 22814235b045SMasami Hiramatsu } 22824235b045SMasami Hiramatsu } 22834235b045SMasami Hiramatsu free(tev->args); 22844235b045SMasami Hiramatsu memset(tev, 0, sizeof(*tev)); 22854de189feSMasami Hiramatsu } 22864de189feSMasami Hiramatsu 22879aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node { 22889aaf5a5fSMasami Hiramatsu struct list_head list; 22899aaf5a5fSMasami Hiramatsu unsigned long start; 22909aaf5a5fSMasami Hiramatsu unsigned long end; 22919aaf5a5fSMasami Hiramatsu char *symbol; 22929aaf5a5fSMasami Hiramatsu }; 22939aaf5a5fSMasami Hiramatsu 22949aaf5a5fSMasami Hiramatsu static void kprobe_blacklist__delete(struct list_head *blacklist) 22959aaf5a5fSMasami Hiramatsu { 22969aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node *node; 22979aaf5a5fSMasami Hiramatsu 22989aaf5a5fSMasami Hiramatsu while (!list_empty(blacklist)) { 22999aaf5a5fSMasami Hiramatsu node = list_first_entry(blacklist, 23009aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node, list); 23019aaf5a5fSMasami Hiramatsu list_del(&node->list); 23029aaf5a5fSMasami Hiramatsu free(node->symbol); 23039aaf5a5fSMasami Hiramatsu free(node); 23049aaf5a5fSMasami Hiramatsu } 23059aaf5a5fSMasami Hiramatsu } 23069aaf5a5fSMasami Hiramatsu 23079aaf5a5fSMasami Hiramatsu static int kprobe_blacklist__load(struct list_head *blacklist) 23089aaf5a5fSMasami Hiramatsu { 23099aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node *node; 23104605eab3SJiri Olsa const char *__debugfs = debugfs__mountpoint(); 23119aaf5a5fSMasami Hiramatsu char buf[PATH_MAX], *p; 23129aaf5a5fSMasami Hiramatsu FILE *fp; 23139aaf5a5fSMasami Hiramatsu int ret; 23149aaf5a5fSMasami Hiramatsu 23159aaf5a5fSMasami Hiramatsu if (__debugfs == NULL) 23169aaf5a5fSMasami Hiramatsu return -ENOTSUP; 23179aaf5a5fSMasami Hiramatsu 23189aaf5a5fSMasami Hiramatsu ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); 23199aaf5a5fSMasami Hiramatsu if (ret < 0) 23209aaf5a5fSMasami Hiramatsu return ret; 23219aaf5a5fSMasami Hiramatsu 23229aaf5a5fSMasami Hiramatsu fp = fopen(buf, "r"); 23239aaf5a5fSMasami Hiramatsu if (!fp) 23249aaf5a5fSMasami Hiramatsu return -errno; 23259aaf5a5fSMasami Hiramatsu 23269aaf5a5fSMasami Hiramatsu ret = 0; 23279aaf5a5fSMasami Hiramatsu while (fgets(buf, PATH_MAX, fp)) { 23289aaf5a5fSMasami Hiramatsu node = zalloc(sizeof(*node)); 23299aaf5a5fSMasami Hiramatsu if (!node) { 23309aaf5a5fSMasami Hiramatsu ret = -ENOMEM; 23319aaf5a5fSMasami Hiramatsu break; 23329aaf5a5fSMasami Hiramatsu } 23339aaf5a5fSMasami Hiramatsu INIT_LIST_HEAD(&node->list); 23349aaf5a5fSMasami Hiramatsu list_add_tail(&node->list, blacklist); 23359aaf5a5fSMasami Hiramatsu if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { 23369aaf5a5fSMasami Hiramatsu ret = -EINVAL; 23379aaf5a5fSMasami Hiramatsu break; 23389aaf5a5fSMasami Hiramatsu } 23399aaf5a5fSMasami Hiramatsu p = strchr(buf, '\t'); 23409aaf5a5fSMasami Hiramatsu if (p) { 23419aaf5a5fSMasami Hiramatsu p++; 23429aaf5a5fSMasami Hiramatsu if (p[strlen(p) - 1] == '\n') 23439aaf5a5fSMasami Hiramatsu p[strlen(p) - 1] = '\0'; 23449aaf5a5fSMasami Hiramatsu } else 23459aaf5a5fSMasami Hiramatsu p = (char *)"unknown"; 23469aaf5a5fSMasami Hiramatsu node->symbol = strdup(p); 23479aaf5a5fSMasami Hiramatsu if (!node->symbol) { 23489aaf5a5fSMasami Hiramatsu ret = -ENOMEM; 23499aaf5a5fSMasami Hiramatsu break; 23509aaf5a5fSMasami Hiramatsu } 23519aaf5a5fSMasami Hiramatsu pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", 23529aaf5a5fSMasami Hiramatsu node->start, node->end, node->symbol); 23539aaf5a5fSMasami Hiramatsu ret++; 23549aaf5a5fSMasami Hiramatsu } 23559aaf5a5fSMasami Hiramatsu if (ret < 0) 23569aaf5a5fSMasami Hiramatsu kprobe_blacklist__delete(blacklist); 23579aaf5a5fSMasami Hiramatsu fclose(fp); 23589aaf5a5fSMasami Hiramatsu 23599aaf5a5fSMasami Hiramatsu return ret; 23609aaf5a5fSMasami Hiramatsu } 23619aaf5a5fSMasami Hiramatsu 23629aaf5a5fSMasami Hiramatsu static struct kprobe_blacklist_node * 23639aaf5a5fSMasami Hiramatsu kprobe_blacklist__find_by_address(struct list_head *blacklist, 23649aaf5a5fSMasami Hiramatsu unsigned long address) 23659aaf5a5fSMasami Hiramatsu { 23669aaf5a5fSMasami Hiramatsu struct kprobe_blacklist_node *node; 23679aaf5a5fSMasami Hiramatsu 23689aaf5a5fSMasami Hiramatsu list_for_each_entry(node, blacklist, list) { 23699aaf5a5fSMasami Hiramatsu if (node->start <= address && address <= node->end) 23709aaf5a5fSMasami Hiramatsu return node; 23719aaf5a5fSMasami Hiramatsu } 23729aaf5a5fSMasami Hiramatsu 23739aaf5a5fSMasami Hiramatsu return NULL; 23749aaf5a5fSMasami Hiramatsu } 23759aaf5a5fSMasami Hiramatsu 2376b031220dSMasami Hiramatsu static LIST_HEAD(kprobe_blacklist); 2377b031220dSMasami Hiramatsu 2378b031220dSMasami Hiramatsu static void kprobe_blacklist__init(void) 2379b031220dSMasami Hiramatsu { 2380b031220dSMasami Hiramatsu if (!list_empty(&kprobe_blacklist)) 2381b031220dSMasami Hiramatsu return; 2382b031220dSMasami Hiramatsu 2383b031220dSMasami Hiramatsu if (kprobe_blacklist__load(&kprobe_blacklist) < 0) 2384b031220dSMasami Hiramatsu pr_debug("No kprobe blacklist support, ignored\n"); 2385b031220dSMasami Hiramatsu } 2386b031220dSMasami Hiramatsu 2387b031220dSMasami Hiramatsu static void kprobe_blacklist__release(void) 2388b031220dSMasami Hiramatsu { 2389b031220dSMasami Hiramatsu kprobe_blacklist__delete(&kprobe_blacklist); 2390b031220dSMasami Hiramatsu } 2391b031220dSMasami Hiramatsu 2392b031220dSMasami Hiramatsu static bool kprobe_blacklist__listed(unsigned long address) 2393b031220dSMasami Hiramatsu { 2394b031220dSMasami Hiramatsu return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); 2395b031220dSMasami Hiramatsu } 2396b031220dSMasami Hiramatsu 2397d350bd57SMasami Hiramatsu static int perf_probe_event__sprintf(const char *group, const char *event, 2398d350bd57SMasami Hiramatsu struct perf_probe_event *pev, 2399ba7ecb02SMasami Hiramatsu const char *module, 2400ba7ecb02SMasami Hiramatsu struct strbuf *result) 2401278498d4SMasami Hiramatsu { 2402bf4d5f25SMasami Hiramatsu int i, ret; 2403909b0360SMasami Hiramatsu char *buf; 2404909b0360SMasami Hiramatsu 2405909b0360SMasami Hiramatsu if (asprintf(&buf, "%s:%s", group, event) < 0) 2406909b0360SMasami Hiramatsu return -errno; 2407bf4d5f25SMasami Hiramatsu ret = strbuf_addf(result, " %-20s (on ", buf); 2408909b0360SMasami Hiramatsu free(buf); 2409bf4d5f25SMasami Hiramatsu if (ret) 2410bf4d5f25SMasami Hiramatsu return ret; 2411278498d4SMasami Hiramatsu 24124235b045SMasami Hiramatsu /* Synthesize only event probe point */ 2413909b0360SMasami Hiramatsu buf = synthesize_perf_probe_point(&pev->point); 2414909b0360SMasami Hiramatsu if (!buf) 2415909b0360SMasami Hiramatsu return -ENOMEM; 2416bf4d5f25SMasami Hiramatsu ret = strbuf_addstr(result, buf); 2417909b0360SMasami Hiramatsu free(buf); 24184235b045SMasami Hiramatsu 2419bf4d5f25SMasami Hiramatsu if (!ret && module) 2420bf4d5f25SMasami Hiramatsu ret = strbuf_addf(result, " in %s", module); 2421278498d4SMasami Hiramatsu 2422bf4d5f25SMasami Hiramatsu if (!ret && pev->nargs > 0) { 2423bf4d5f25SMasami Hiramatsu ret = strbuf_add(result, " with", 5); 2424bf4d5f25SMasami Hiramatsu for (i = 0; !ret && i < pev->nargs; i++) { 2425909b0360SMasami Hiramatsu buf = synthesize_perf_probe_arg(&pev->args[i]); 2426909b0360SMasami Hiramatsu if (!buf) 2427909b0360SMasami Hiramatsu return -ENOMEM; 2428bf4d5f25SMasami Hiramatsu ret = strbuf_addf(result, " %s", buf); 2429909b0360SMasami Hiramatsu free(buf); 24307df2f329SMasami Hiramatsu } 2431278498d4SMasami Hiramatsu } 2432bf4d5f25SMasami Hiramatsu if (!ret) 2433bf4d5f25SMasami Hiramatsu ret = strbuf_addch(result, ')'); 2434909b0360SMasami Hiramatsu 2435bf4d5f25SMasami Hiramatsu return ret; 2436278498d4SMasami Hiramatsu } 2437278498d4SMasami Hiramatsu 2438ba7ecb02SMasami Hiramatsu /* Show an event */ 2439b02137ccSNamhyung Kim int show_perf_probe_event(const char *group, const char *event, 2440d350bd57SMasami Hiramatsu struct perf_probe_event *pev, 2441ba7ecb02SMasami Hiramatsu const char *module, bool use_stdout) 2442ba7ecb02SMasami Hiramatsu { 2443ba7ecb02SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 2444ba7ecb02SMasami Hiramatsu int ret; 2445ba7ecb02SMasami Hiramatsu 2446d350bd57SMasami Hiramatsu ret = perf_probe_event__sprintf(group, event, pev, module, &buf); 2447ba7ecb02SMasami Hiramatsu if (ret >= 0) { 2448ba7ecb02SMasami Hiramatsu if (use_stdout) 2449ba7ecb02SMasami Hiramatsu printf("%s\n", buf.buf); 2450ba7ecb02SMasami Hiramatsu else 2451ba7ecb02SMasami Hiramatsu pr_info("%s\n", buf.buf); 2452ba7ecb02SMasami Hiramatsu } 2453ba7ecb02SMasami Hiramatsu strbuf_release(&buf); 2454ba7ecb02SMasami Hiramatsu 2455ba7ecb02SMasami Hiramatsu return ret; 2456ba7ecb02SMasami Hiramatsu } 2457ba7ecb02SMasami Hiramatsu 2458b6a89643SMasami Hiramatsu static bool filter_probe_trace_event(struct probe_trace_event *tev, 2459b6a89643SMasami Hiramatsu struct strfilter *filter) 2460b6a89643SMasami Hiramatsu { 2461b6a89643SMasami Hiramatsu char tmp[128]; 2462b6a89643SMasami Hiramatsu 2463b6a89643SMasami Hiramatsu /* At first, check the event name itself */ 2464b6a89643SMasami Hiramatsu if (strfilter__compare(filter, tev->event)) 2465b6a89643SMasami Hiramatsu return true; 2466b6a89643SMasami Hiramatsu 2467b6a89643SMasami Hiramatsu /* Next, check the combination of name and group */ 2468b6a89643SMasami Hiramatsu if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) 2469b6a89643SMasami Hiramatsu return false; 2470b6a89643SMasami Hiramatsu return strfilter__compare(filter, tmp); 2471b6a89643SMasami Hiramatsu } 2472b6a89643SMasami Hiramatsu 2473b6a89643SMasami Hiramatsu static int __show_perf_probe_events(int fd, bool is_kprobe, 2474b6a89643SMasami Hiramatsu struct strfilter *filter) 24754de189feSMasami Hiramatsu { 2476225466f1SSrikar Dronamraju int ret = 0; 24770e60836bSSrikar Dronamraju struct probe_trace_event tev; 24784235b045SMasami Hiramatsu struct perf_probe_event pev; 24794de189feSMasami Hiramatsu struct strlist *rawlist; 24804de189feSMasami Hiramatsu struct str_node *ent; 24814de189feSMasami Hiramatsu 24824235b045SMasami Hiramatsu memset(&tev, 0, sizeof(tev)); 24834235b045SMasami Hiramatsu memset(&pev, 0, sizeof(pev)); 248472041334SMasami Hiramatsu 248592f6c72eSMasami Hiramatsu rawlist = probe_file__get_rawlist(fd); 2486146a1439SMasami Hiramatsu if (!rawlist) 24876eb08660SMasami Hiramatsu return -ENOMEM; 24884de189feSMasami Hiramatsu 2489602a1f4dSArnaldo Carvalho de Melo strlist__for_each_entry(ent, rawlist) { 24900e60836bSSrikar Dronamraju ret = parse_probe_trace_command(ent->s, &tev); 2491146a1439SMasami Hiramatsu if (ret >= 0) { 2492b6a89643SMasami Hiramatsu if (!filter_probe_trace_event(&tev, filter)) 2493b6a89643SMasami Hiramatsu goto next; 2494225466f1SSrikar Dronamraju ret = convert_to_perf_probe_event(&tev, &pev, 2495225466f1SSrikar Dronamraju is_kprobe); 2496ba7ecb02SMasami Hiramatsu if (ret < 0) 2497ba7ecb02SMasami Hiramatsu goto next; 2498d350bd57SMasami Hiramatsu ret = show_perf_probe_event(pev.group, pev.event, 2499d350bd57SMasami Hiramatsu &pev, tev.point.module, 2500ba7ecb02SMasami Hiramatsu true); 2501146a1439SMasami Hiramatsu } 2502b6a89643SMasami Hiramatsu next: 25034235b045SMasami Hiramatsu clear_perf_probe_event(&pev); 25040e60836bSSrikar Dronamraju clear_probe_trace_event(&tev); 2505146a1439SMasami Hiramatsu if (ret < 0) 2506146a1439SMasami Hiramatsu break; 25074de189feSMasami Hiramatsu } 25084de189feSMasami Hiramatsu strlist__delete(rawlist); 25097737af01SMasami Hiramatsu /* Cleanup cached debuginfo if needed */ 25107737af01SMasami Hiramatsu debuginfo_cache__exit(); 2511146a1439SMasami Hiramatsu 2512146a1439SMasami Hiramatsu return ret; 25134de189feSMasami Hiramatsu } 25144de189feSMasami Hiramatsu 2515225466f1SSrikar Dronamraju /* List up current perf-probe events */ 2516b6a89643SMasami Hiramatsu int show_perf_probe_events(struct strfilter *filter) 2517225466f1SSrikar Dronamraju { 25185e45187cSMasami Hiramatsu int kp_fd, up_fd, ret; 2519225466f1SSrikar Dronamraju 2520225466f1SSrikar Dronamraju setup_pager(); 2521225466f1SSrikar Dronamraju 25221f3736c9SMasami Hiramatsu if (probe_conf.cache) 25231f3736c9SMasami Hiramatsu return probe_cache__show_all_caches(filter); 25241f3736c9SMasami Hiramatsu 25259bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(false); 2526225466f1SSrikar Dronamraju if (ret < 0) 2527225466f1SSrikar Dronamraju return ret; 2528225466f1SSrikar Dronamraju 252992f6c72eSMasami Hiramatsu ret = probe_file__open_both(&kp_fd, &up_fd, 0); 25305e45187cSMasami Hiramatsu if (ret < 0) 2531225466f1SSrikar Dronamraju return ret; 2532225466f1SSrikar Dronamraju 253392f6c72eSMasami Hiramatsu if (kp_fd >= 0) 253492f6c72eSMasami Hiramatsu ret = __show_perf_probe_events(kp_fd, true, filter); 253592f6c72eSMasami Hiramatsu if (up_fd >= 0 && ret >= 0) 253692f6c72eSMasami Hiramatsu ret = __show_perf_probe_events(up_fd, false, filter); 253792f6c72eSMasami Hiramatsu if (kp_fd > 0) 253892f6c72eSMasami Hiramatsu close(kp_fd); 253992f6c72eSMasami Hiramatsu if (up_fd > 0) 254092f6c72eSMasami Hiramatsu close(up_fd); 25419bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 2542b498ce1fSMasami Hiramatsu 2543146a1439SMasami Hiramatsu return ret; 2544f4d7da49SMasami Hiramatsu } 254550656eecSMasami Hiramatsu 2546146a1439SMasami Hiramatsu static int get_new_event_name(char *buf, size_t len, const char *base, 2547d761b08bSMasami Hiramatsu struct strlist *namelist, bool allow_suffix) 2548b498ce1fSMasami Hiramatsu { 2549b498ce1fSMasami Hiramatsu int i, ret; 2550663b1151SMasami Hiramatsu char *p, *nbase; 255117f88fcdSMasami Hiramatsu 25523099c026SNaveen N. Rao if (*base == '.') 25533099c026SNaveen N. Rao base++; 2554663b1151SMasami Hiramatsu nbase = strdup(base); 2555663b1151SMasami Hiramatsu if (!nbase) 2556663b1151SMasami Hiramatsu return -ENOMEM; 25573099c026SNaveen N. Rao 2558663b1151SMasami Hiramatsu /* Cut off the dot suffixes (e.g. .const, .isra)*/ 2559663b1151SMasami Hiramatsu p = strchr(nbase, '.'); 2560663b1151SMasami Hiramatsu if (p && p != nbase) 2561663b1151SMasami Hiramatsu *p = '\0'; 2562663b1151SMasami Hiramatsu 2563663b1151SMasami Hiramatsu /* Try no suffix number */ 2564663b1151SMasami Hiramatsu ret = e_snprintf(buf, len, "%s", nbase); 2565146a1439SMasami Hiramatsu if (ret < 0) { 25665f03cba4SMasami Hiramatsu pr_debug("snprintf() failed: %d\n", ret); 2567663b1151SMasami Hiramatsu goto out; 2568146a1439SMasami Hiramatsu } 256917f88fcdSMasami Hiramatsu if (!strlist__has_entry(namelist, buf)) 2570663b1151SMasami Hiramatsu goto out; 257117f88fcdSMasami Hiramatsu 2572d761b08bSMasami Hiramatsu if (!allow_suffix) { 257303e01f56SWang Nan pr_warning("Error: event \"%s\" already exists.\n" 257403e01f56SWang Nan " Hint: Remove existing event by 'perf probe -d'\n" 257503e01f56SWang Nan " or force duplicates by 'perf probe -f'\n" 257603e01f56SWang Nan " or set 'force=yes' in BPF source.\n", 257703e01f56SWang Nan buf); 2578663b1151SMasami Hiramatsu ret = -EEXIST; 2579663b1151SMasami Hiramatsu goto out; 2580d761b08bSMasami Hiramatsu } 2581d761b08bSMasami Hiramatsu 258217f88fcdSMasami Hiramatsu /* Try to add suffix */ 258317f88fcdSMasami Hiramatsu for (i = 1; i < MAX_EVENT_INDEX; i++) { 2584663b1151SMasami Hiramatsu ret = e_snprintf(buf, len, "%s_%d", nbase, i); 2585146a1439SMasami Hiramatsu if (ret < 0) { 25865f03cba4SMasami Hiramatsu pr_debug("snprintf() failed: %d\n", ret); 2587663b1151SMasami Hiramatsu goto out; 2588146a1439SMasami Hiramatsu } 2589b498ce1fSMasami Hiramatsu if (!strlist__has_entry(namelist, buf)) 2590b498ce1fSMasami Hiramatsu break; 2591b498ce1fSMasami Hiramatsu } 2592146a1439SMasami Hiramatsu if (i == MAX_EVENT_INDEX) { 2593146a1439SMasami Hiramatsu pr_warning("Too many events are on the same function.\n"); 2594146a1439SMasami Hiramatsu ret = -ERANGE; 2595b498ce1fSMasami Hiramatsu } 2596b498ce1fSMasami Hiramatsu 2597663b1151SMasami Hiramatsu out: 2598663b1151SMasami Hiramatsu free(nbase); 2599146a1439SMasami Hiramatsu return ret; 2600146a1439SMasami Hiramatsu } 2601146a1439SMasami Hiramatsu 260279702f61SMasami Hiramatsu /* Warn if the current kernel's uprobe implementation is old */ 260379702f61SMasami Hiramatsu static void warn_uprobe_event_compat(struct probe_trace_event *tev) 260479702f61SMasami Hiramatsu { 260579702f61SMasami Hiramatsu int i; 260679702f61SMasami Hiramatsu char *buf = synthesize_probe_trace_command(tev); 260779702f61SMasami Hiramatsu 260879702f61SMasami Hiramatsu /* Old uprobe event doesn't support memory dereference */ 260979702f61SMasami Hiramatsu if (!tev->uprobes || tev->nargs == 0 || !buf) 261079702f61SMasami Hiramatsu goto out; 261179702f61SMasami Hiramatsu 261279702f61SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) 261379702f61SMasami Hiramatsu if (strglobmatch(tev->args[i].value, "[$@+-]*")) { 261479702f61SMasami Hiramatsu pr_warning("Please upgrade your kernel to at least " 261579702f61SMasami Hiramatsu "3.14 to have access to feature %s\n", 261679702f61SMasami Hiramatsu tev->args[i].value); 261779702f61SMasami Hiramatsu break; 261879702f61SMasami Hiramatsu } 261979702f61SMasami Hiramatsu out: 262079702f61SMasami Hiramatsu free(buf); 262179702f61SMasami Hiramatsu } 262279702f61SMasami Hiramatsu 2623a3c9de62SMasami Hiramatsu /* Set new name from original perf_probe_event and namelist */ 2624a3c9de62SMasami Hiramatsu static int probe_trace_event__set_name(struct probe_trace_event *tev, 2625a3c9de62SMasami Hiramatsu struct perf_probe_event *pev, 2626a3c9de62SMasami Hiramatsu struct strlist *namelist, 2627a3c9de62SMasami Hiramatsu bool allow_suffix) 2628a3c9de62SMasami Hiramatsu { 2629a3c9de62SMasami Hiramatsu const char *event, *group; 2630a3c9de62SMasami Hiramatsu char buf[64]; 2631a3c9de62SMasami Hiramatsu int ret; 2632a3c9de62SMasami Hiramatsu 2633bc062230SMasami Hiramatsu /* If probe_event or trace_event already have the name, reuse it */ 263442bba263SMasami Hiramatsu if (pev->event && !pev->sdt) 2635a3c9de62SMasami Hiramatsu event = pev->event; 2636bc062230SMasami Hiramatsu else if (tev->event) 2637bc062230SMasami Hiramatsu event = tev->event; 2638bc062230SMasami Hiramatsu else { 2639bc062230SMasami Hiramatsu /* Or generate new one from probe point */ 2640da15bd9dSWang Nan if (pev->point.function && 2641da15bd9dSWang Nan (strncmp(pev->point.function, "0x", 2) != 0) && 2642da15bd9dSWang Nan !strisglob(pev->point.function)) 2643a3c9de62SMasami Hiramatsu event = pev->point.function; 2644a3c9de62SMasami Hiramatsu else 2645a3c9de62SMasami Hiramatsu event = tev->point.realname; 2646bc062230SMasami Hiramatsu } 264742bba263SMasami Hiramatsu if (pev->group && !pev->sdt) 2648a3c9de62SMasami Hiramatsu group = pev->group; 2649bc062230SMasami Hiramatsu else if (tev->group) 2650bc062230SMasami Hiramatsu group = tev->group; 2651a3c9de62SMasami Hiramatsu else 2652a3c9de62SMasami Hiramatsu group = PERFPROBE_GROUP; 2653a3c9de62SMasami Hiramatsu 2654a3c9de62SMasami Hiramatsu /* Get an unused new event name */ 2655a3c9de62SMasami Hiramatsu ret = get_new_event_name(buf, 64, event, 2656a3c9de62SMasami Hiramatsu namelist, allow_suffix); 2657a3c9de62SMasami Hiramatsu if (ret < 0) 2658a3c9de62SMasami Hiramatsu return ret; 2659a3c9de62SMasami Hiramatsu 2660a3c9de62SMasami Hiramatsu event = buf; 2661a3c9de62SMasami Hiramatsu 2662a3c9de62SMasami Hiramatsu tev->event = strdup(event); 2663a3c9de62SMasami Hiramatsu tev->group = strdup(group); 2664a3c9de62SMasami Hiramatsu if (tev->event == NULL || tev->group == NULL) 2665a3c9de62SMasami Hiramatsu return -ENOMEM; 2666a3c9de62SMasami Hiramatsu 2667a3c9de62SMasami Hiramatsu /* Add added event name to namelist */ 2668a3c9de62SMasami Hiramatsu strlist__add(namelist, event); 2669a3c9de62SMasami Hiramatsu return 0; 2670a3c9de62SMasami Hiramatsu } 2671a3c9de62SMasami Hiramatsu 26721de7b8bfSMasami Hiramatsu static int __open_probe_file_and_namelist(bool uprobe, 26731de7b8bfSMasami Hiramatsu struct strlist **namelist) 267450656eecSMasami Hiramatsu { 26751de7b8bfSMasami Hiramatsu int fd; 267650656eecSMasami Hiramatsu 26771de7b8bfSMasami Hiramatsu fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); 267892f6c72eSMasami Hiramatsu if (fd < 0) 2679146a1439SMasami Hiramatsu return fd; 26805e45187cSMasami Hiramatsu 2681b498ce1fSMasami Hiramatsu /* Get current event names */ 26821de7b8bfSMasami Hiramatsu *namelist = probe_file__get_namelist(fd); 26831de7b8bfSMasami Hiramatsu if (!(*namelist)) { 2684146a1439SMasami Hiramatsu pr_debug("Failed to get current event list.\n"); 26851de7b8bfSMasami Hiramatsu close(fd); 26861de7b8bfSMasami Hiramatsu return -ENOMEM; 2687146a1439SMasami Hiramatsu } 26881de7b8bfSMasami Hiramatsu return fd; 26891de7b8bfSMasami Hiramatsu } 26901de7b8bfSMasami Hiramatsu 26911de7b8bfSMasami Hiramatsu static int __add_probe_trace_events(struct perf_probe_event *pev, 26921de7b8bfSMasami Hiramatsu struct probe_trace_event *tevs, 26931de7b8bfSMasami Hiramatsu int ntevs, bool allow_suffix) 26941de7b8bfSMasami Hiramatsu { 26951de7b8bfSMasami Hiramatsu int i, fd[2] = {-1, -1}, up, ret; 26961de7b8bfSMasami Hiramatsu struct probe_trace_event *tev = NULL; 26971de7b8bfSMasami Hiramatsu struct probe_cache *cache = NULL; 26981de7b8bfSMasami Hiramatsu struct strlist *namelist[2] = {NULL, NULL}; 26991de7b8bfSMasami Hiramatsu 27001de7b8bfSMasami Hiramatsu up = pev->uprobes ? 1 : 0; 27011de7b8bfSMasami Hiramatsu fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); 27021de7b8bfSMasami Hiramatsu if (fd[up] < 0) 27031de7b8bfSMasami Hiramatsu return fd[up]; 270450656eecSMasami Hiramatsu 2705146a1439SMasami Hiramatsu ret = 0; 270602b95dadSMasami Hiramatsu for (i = 0; i < ntevs; i++) { 27074235b045SMasami Hiramatsu tev = &tevs[i]; 27081de7b8bfSMasami Hiramatsu up = tev->uprobes ? 1 : 0; 27091de7b8bfSMasami Hiramatsu if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ 27101de7b8bfSMasami Hiramatsu fd[up] = __open_probe_file_and_namelist(up, 27111de7b8bfSMasami Hiramatsu &namelist[up]); 27121de7b8bfSMasami Hiramatsu if (fd[up] < 0) 27131de7b8bfSMasami Hiramatsu goto close_out; 27141de7b8bfSMasami Hiramatsu } 2715b031220dSMasami Hiramatsu /* Skip if the symbol is out of .text or blacklisted */ 2716bc062230SMasami Hiramatsu if (!tev->point.symbol && !pev->uprobes) 27175a51fcd1SMasami Hiramatsu continue; 27189aaf5a5fSMasami Hiramatsu 2719a3c9de62SMasami Hiramatsu /* Set new name for tev (and update namelist) */ 27201de7b8bfSMasami Hiramatsu ret = probe_trace_event__set_name(tev, pev, namelist[up], 2721a3c9de62SMasami Hiramatsu allow_suffix); 2722146a1439SMasami Hiramatsu if (ret < 0) 2723146a1439SMasami Hiramatsu break; 27244235b045SMasami Hiramatsu 27251de7b8bfSMasami Hiramatsu ret = probe_file__add_event(fd[up], tev); 2726146a1439SMasami Hiramatsu if (ret < 0) 2727146a1439SMasami Hiramatsu break; 27284235b045SMasami Hiramatsu 2729d761b08bSMasami Hiramatsu /* 2730d761b08bSMasami Hiramatsu * Probes after the first probe which comes from same 2731d761b08bSMasami Hiramatsu * user input are always allowed to add suffix, because 2732d761b08bSMasami Hiramatsu * there might be several addresses corresponding to 2733d761b08bSMasami Hiramatsu * one code line. 2734d761b08bSMasami Hiramatsu */ 2735d761b08bSMasami Hiramatsu allow_suffix = true; 273650656eecSMasami Hiramatsu } 273779702f61SMasami Hiramatsu if (ret == -EINVAL && pev->uprobes) 273879702f61SMasami Hiramatsu warn_uprobe_event_compat(tev); 27392fd457a3SMasami Hiramatsu if (ret == 0 && probe_conf.cache) { 27402fd457a3SMasami Hiramatsu cache = probe_cache__new(pev->target); 27412fd457a3SMasami Hiramatsu if (!cache || 27422fd457a3SMasami Hiramatsu probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || 27432fd457a3SMasami Hiramatsu probe_cache__commit(cache) < 0) 27442fd457a3SMasami Hiramatsu pr_warning("Failed to add event to probe cache\n"); 27452fd457a3SMasami Hiramatsu probe_cache__delete(cache); 27462fd457a3SMasami Hiramatsu } 2747146a1439SMasami Hiramatsu 2748ae2cb1acSMasami Hiramatsu close_out: 27491de7b8bfSMasami Hiramatsu for (up = 0; up < 2; up++) { 27501de7b8bfSMasami Hiramatsu strlist__delete(namelist[up]); 27511de7b8bfSMasami Hiramatsu if (fd[up] >= 0) 27521de7b8bfSMasami Hiramatsu close(fd[up]); 27531de7b8bfSMasami Hiramatsu } 2754146a1439SMasami Hiramatsu return ret; 275550656eecSMasami Hiramatsu } 2756fa28244dSMasami Hiramatsu 27576bb536ccSWang Nan static int find_probe_functions(struct map *map, char *name, 27586bb536ccSWang Nan struct symbol **syms) 2759eb948e50SMasami Hiramatsu { 2760564c62a4SNamhyung Kim int found = 0; 27610a3873a8SArnaldo Carvalho de Melo struct symbol *sym; 27624c859351SMasami Hiramatsu struct rb_node *tmp; 2763564c62a4SNamhyung Kim 2764be39db9fSArnaldo Carvalho de Melo if (map__load(map) < 0) 276575e4a2a6SWang Nan return 0; 276675e4a2a6SWang Nan 27674c859351SMasami Hiramatsu map__for_each_symbol(map, sym, tmp) { 27686bb536ccSWang Nan if (strglobmatch(sym->name, name)) { 2769564c62a4SNamhyung Kim found++; 27706bb536ccSWang Nan if (syms && found < probe_conf.max_probes) 27716bb536ccSWang Nan syms[found - 1] = sym; 27726bb536ccSWang Nan } 2773eb948e50SMasami Hiramatsu } 2774564c62a4SNamhyung Kim 2775564c62a4SNamhyung Kim return found; 2776eb948e50SMasami Hiramatsu } 2777eb948e50SMasami Hiramatsu 27787b6ff0bdSNaveen N. Rao void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, 27797b6ff0bdSNaveen N. Rao struct probe_trace_event *tev __maybe_unused, 27800b3c2264SNaveen N. Rao struct map *map __maybe_unused, 27810b3c2264SNaveen N. Rao struct symbol *sym __maybe_unused) { } 27827b6ff0bdSNaveen N. Rao 2783eb948e50SMasami Hiramatsu /* 2784eb948e50SMasami Hiramatsu * Find probe function addresses from map. 2785eb948e50SMasami Hiramatsu * Return an error or the number of found probe_trace_event 2786eb948e50SMasami Hiramatsu */ 2787eb948e50SMasami Hiramatsu static int find_probe_trace_events_from_map(struct perf_probe_event *pev, 2788ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs) 2789eb948e50SMasami Hiramatsu { 2790eb948e50SMasami Hiramatsu struct map *map = NULL; 2791eb948e50SMasami Hiramatsu struct ref_reloc_sym *reloc_sym = NULL; 2792eb948e50SMasami Hiramatsu struct symbol *sym; 27936bb536ccSWang Nan struct symbol **syms = NULL; 2794eb948e50SMasami Hiramatsu struct probe_trace_event *tev; 2795eb948e50SMasami Hiramatsu struct perf_probe_point *pp = &pev->point; 2796eb948e50SMasami Hiramatsu struct probe_trace_point *tp; 2797564c62a4SNamhyung Kim int num_matched_functions; 2798b031220dSMasami Hiramatsu int ret, i, j, skipped = 0; 2799c61fb959SRavi Bangoria char *mod_name; 2800eb948e50SMasami Hiramatsu 280144225521SMasami Hiramatsu map = get_target_map(pev->target, pev->uprobes); 2802eb948e50SMasami Hiramatsu if (!map) { 2803eb948e50SMasami Hiramatsu ret = -EINVAL; 2804eb948e50SMasami Hiramatsu goto out; 2805eb948e50SMasami Hiramatsu } 2806eb948e50SMasami Hiramatsu 28076bb536ccSWang Nan syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); 28086bb536ccSWang Nan if (!syms) { 28096bb536ccSWang Nan ret = -ENOMEM; 28106bb536ccSWang Nan goto out; 28116bb536ccSWang Nan } 28126bb536ccSWang Nan 2813eb948e50SMasami Hiramatsu /* 2814eb948e50SMasami Hiramatsu * Load matched symbols: Since the different local symbols may have 2815eb948e50SMasami Hiramatsu * same name but different addresses, this lists all the symbols. 2816eb948e50SMasami Hiramatsu */ 28176bb536ccSWang Nan num_matched_functions = find_probe_functions(map, pp->function, syms); 2818564c62a4SNamhyung Kim if (num_matched_functions == 0) { 2819eb948e50SMasami Hiramatsu pr_err("Failed to find symbol %s in %s\n", pp->function, 282044225521SMasami Hiramatsu pev->target ? : "kernel"); 2821eb948e50SMasami Hiramatsu ret = -ENOENT; 2822eb948e50SMasami Hiramatsu goto out; 2823ddb2f58fSMasami Hiramatsu } else if (num_matched_functions > probe_conf.max_probes) { 2824eb948e50SMasami Hiramatsu pr_err("Too many functions matched in %s\n", 282544225521SMasami Hiramatsu pev->target ? : "kernel"); 2826eb948e50SMasami Hiramatsu ret = -E2BIG; 2827eb948e50SMasami Hiramatsu goto out; 2828eb948e50SMasami Hiramatsu } 2829eb948e50SMasami Hiramatsu 28301a8ac29cSMasami Hiramatsu /* Note that the symbols in the kmodule are not relocated */ 28311a8ac29cSMasami Hiramatsu if (!pev->uprobes && !pp->retprobe && !pev->target) { 28320560a0c4SHe Kuang reloc_sym = kernel_get_ref_reloc_sym(); 2833eb948e50SMasami Hiramatsu if (!reloc_sym) { 2834eb948e50SMasami Hiramatsu pr_warning("Relocated base symbol is not found!\n"); 2835eb948e50SMasami Hiramatsu ret = -EINVAL; 2836eb948e50SMasami Hiramatsu goto out; 2837eb948e50SMasami Hiramatsu } 2838eb948e50SMasami Hiramatsu } 2839eb948e50SMasami Hiramatsu 2840eb948e50SMasami Hiramatsu /* Setup result trace-probe-events */ 2841eb948e50SMasami Hiramatsu *tevs = zalloc(sizeof(*tev) * num_matched_functions); 2842eb948e50SMasami Hiramatsu if (!*tevs) { 2843eb948e50SMasami Hiramatsu ret = -ENOMEM; 2844eb948e50SMasami Hiramatsu goto out; 2845eb948e50SMasami Hiramatsu } 2846eb948e50SMasami Hiramatsu 2847eb948e50SMasami Hiramatsu ret = 0; 2848564c62a4SNamhyung Kim 28496bb536ccSWang Nan for (j = 0; j < num_matched_functions; j++) { 28506bb536ccSWang Nan sym = syms[j]; 28516bb536ccSWang Nan 2852eb948e50SMasami Hiramatsu tev = (*tevs) + ret; 2853eb948e50SMasami Hiramatsu tp = &tev->point; 2854eb948e50SMasami Hiramatsu if (ret == num_matched_functions) { 2855eb948e50SMasami Hiramatsu pr_warning("Too many symbols are listed. Skip it.\n"); 2856eb948e50SMasami Hiramatsu break; 2857eb948e50SMasami Hiramatsu } 2858eb948e50SMasami Hiramatsu ret++; 2859eb948e50SMasami Hiramatsu 2860eb948e50SMasami Hiramatsu if (pp->offset > sym->end - sym->start) { 2861eb948e50SMasami Hiramatsu pr_warning("Offset %ld is bigger than the size of %s\n", 2862eb948e50SMasami Hiramatsu pp->offset, sym->name); 2863eb948e50SMasami Hiramatsu ret = -ENOENT; 2864eb948e50SMasami Hiramatsu goto err_out; 2865eb948e50SMasami Hiramatsu } 2866eb948e50SMasami Hiramatsu /* Add one probe point */ 2867eb948e50SMasami Hiramatsu tp->address = map->unmap_ip(map, sym->start) + pp->offset; 28681a8ac29cSMasami Hiramatsu 28691a8ac29cSMasami Hiramatsu /* Check the kprobe (not in module) is within .text */ 28701a8ac29cSMasami Hiramatsu if (!pev->uprobes && !pev->target && 2871b031220dSMasami Hiramatsu kprobe_warn_out_range(sym->name, tp->address)) { 2872b031220dSMasami Hiramatsu tp->symbol = NULL; /* Skip it */ 2873b031220dSMasami Hiramatsu skipped++; 2874b031220dSMasami Hiramatsu } else if (reloc_sym) { 2875eb948e50SMasami Hiramatsu tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); 2876eb948e50SMasami Hiramatsu tp->offset = tp->address - reloc_sym->addr; 2877eb948e50SMasami Hiramatsu } else { 2878eb948e50SMasami Hiramatsu tp->symbol = strdup_or_goto(sym->name, nomem_out); 2879eb948e50SMasami Hiramatsu tp->offset = pp->offset; 2880eb948e50SMasami Hiramatsu } 28816bb536ccSWang Nan tp->realname = strdup_or_goto(sym->name, nomem_out); 28826bb536ccSWang Nan 2883eb948e50SMasami Hiramatsu tp->retprobe = pp->retprobe; 2884c61fb959SRavi Bangoria if (pev->target) { 2885c61fb959SRavi Bangoria if (pev->uprobes) { 288644225521SMasami Hiramatsu tev->point.module = strdup_or_goto(pev->target, 288744225521SMasami Hiramatsu nomem_out); 2888c61fb959SRavi Bangoria } else { 2889c61fb959SRavi Bangoria mod_name = find_module_name(pev->target); 2890c61fb959SRavi Bangoria tev->point.module = 2891c61fb959SRavi Bangoria strdup(mod_name ? mod_name : pev->target); 2892c61fb959SRavi Bangoria free(mod_name); 2893c61fb959SRavi Bangoria if (!tev->point.module) 2894c61fb959SRavi Bangoria goto nomem_out; 2895c61fb959SRavi Bangoria } 2896c61fb959SRavi Bangoria } 2897eb948e50SMasami Hiramatsu tev->uprobes = pev->uprobes; 2898eb948e50SMasami Hiramatsu tev->nargs = pev->nargs; 2899eb948e50SMasami Hiramatsu if (tev->nargs) { 2900eb948e50SMasami Hiramatsu tev->args = zalloc(sizeof(struct probe_trace_arg) * 2901eb948e50SMasami Hiramatsu tev->nargs); 2902eb948e50SMasami Hiramatsu if (tev->args == NULL) 2903eb948e50SMasami Hiramatsu goto nomem_out; 2904eb948e50SMasami Hiramatsu } 2905eb948e50SMasami Hiramatsu for (i = 0; i < tev->nargs; i++) { 2906eb948e50SMasami Hiramatsu if (pev->args[i].name) 2907eb948e50SMasami Hiramatsu tev->args[i].name = 2908eb948e50SMasami Hiramatsu strdup_or_goto(pev->args[i].name, 2909eb948e50SMasami Hiramatsu nomem_out); 2910eb948e50SMasami Hiramatsu 2911eb948e50SMasami Hiramatsu tev->args[i].value = strdup_or_goto(pev->args[i].var, 2912eb948e50SMasami Hiramatsu nomem_out); 2913eb948e50SMasami Hiramatsu if (pev->args[i].type) 2914eb948e50SMasami Hiramatsu tev->args[i].type = 2915eb948e50SMasami Hiramatsu strdup_or_goto(pev->args[i].type, 2916eb948e50SMasami Hiramatsu nomem_out); 2917eb948e50SMasami Hiramatsu } 29180b3c2264SNaveen N. Rao arch__fix_tev_from_maps(pev, tev, map, sym); 2919eb948e50SMasami Hiramatsu } 2920b031220dSMasami Hiramatsu if (ret == skipped) { 2921b031220dSMasami Hiramatsu ret = -ENOENT; 2922b031220dSMasami Hiramatsu goto err_out; 2923b031220dSMasami Hiramatsu } 2924eb948e50SMasami Hiramatsu 2925eb948e50SMasami Hiramatsu out: 2926eebc509bSMasami Hiramatsu map__put(map); 29276bb536ccSWang Nan free(syms); 2928eb948e50SMasami Hiramatsu return ret; 2929eb948e50SMasami Hiramatsu 2930eb948e50SMasami Hiramatsu nomem_out: 2931eb948e50SMasami Hiramatsu ret = -ENOMEM; 2932eb948e50SMasami Hiramatsu err_out: 2933eb948e50SMasami Hiramatsu clear_probe_trace_events(*tevs, num_matched_functions); 2934eb948e50SMasami Hiramatsu zfree(tevs); 2935eb948e50SMasami Hiramatsu goto out; 2936eb948e50SMasami Hiramatsu } 2937eb948e50SMasami Hiramatsu 2938da15bd9dSWang Nan static int try_to_find_absolute_address(struct perf_probe_event *pev, 2939da15bd9dSWang Nan struct probe_trace_event **tevs) 2940da15bd9dSWang Nan { 2941da15bd9dSWang Nan struct perf_probe_point *pp = &pev->point; 2942da15bd9dSWang Nan struct probe_trace_event *tev; 2943da15bd9dSWang Nan struct probe_trace_point *tp; 2944da15bd9dSWang Nan int i, err; 2945da15bd9dSWang Nan 2946da15bd9dSWang Nan if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) 2947da15bd9dSWang Nan return -EINVAL; 2948da15bd9dSWang Nan if (perf_probe_event_need_dwarf(pev)) 2949da15bd9dSWang Nan return -EINVAL; 2950da15bd9dSWang Nan 2951da15bd9dSWang Nan /* 2952da15bd9dSWang Nan * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at 2953da15bd9dSWang Nan * absolute address. 2954da15bd9dSWang Nan * 2955da15bd9dSWang Nan * Only one tev can be generated by this. 2956da15bd9dSWang Nan */ 2957da15bd9dSWang Nan *tevs = zalloc(sizeof(*tev)); 2958da15bd9dSWang Nan if (!*tevs) 2959da15bd9dSWang Nan return -ENOMEM; 2960da15bd9dSWang Nan 2961da15bd9dSWang Nan tev = *tevs; 2962da15bd9dSWang Nan tp = &tev->point; 2963da15bd9dSWang Nan 2964da15bd9dSWang Nan /* 2965da15bd9dSWang Nan * Don't use tp->offset, use address directly, because 2966da15bd9dSWang Nan * in synthesize_probe_trace_command() address cannot be 2967da15bd9dSWang Nan * zero. 2968da15bd9dSWang Nan */ 2969da15bd9dSWang Nan tp->address = pev->point.abs_address; 2970da15bd9dSWang Nan tp->retprobe = pp->retprobe; 2971da15bd9dSWang Nan tev->uprobes = pev->uprobes; 2972da15bd9dSWang Nan 2973da15bd9dSWang Nan err = -ENOMEM; 2974da15bd9dSWang Nan /* 2975da15bd9dSWang Nan * Give it a '0x' leading symbol name. 2976da15bd9dSWang Nan * In __add_probe_trace_events, a NULL symbol is interpreted as 2977da15bd9dSWang Nan * invalud. 2978da15bd9dSWang Nan */ 2979da15bd9dSWang Nan if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0) 2980da15bd9dSWang Nan goto errout; 2981da15bd9dSWang Nan 2982da15bd9dSWang Nan /* For kprobe, check range */ 2983da15bd9dSWang Nan if ((!tev->uprobes) && 2984da15bd9dSWang Nan (kprobe_warn_out_range(tev->point.symbol, 2985da15bd9dSWang Nan tev->point.address))) { 2986da15bd9dSWang Nan err = -EACCES; 2987da15bd9dSWang Nan goto errout; 2988da15bd9dSWang Nan } 2989da15bd9dSWang Nan 2990da15bd9dSWang Nan if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0) 2991da15bd9dSWang Nan goto errout; 2992da15bd9dSWang Nan 2993da15bd9dSWang Nan if (pev->target) { 2994da15bd9dSWang Nan tp->module = strdup(pev->target); 2995da15bd9dSWang Nan if (!tp->module) 2996da15bd9dSWang Nan goto errout; 2997da15bd9dSWang Nan } 2998da15bd9dSWang Nan 2999da15bd9dSWang Nan if (tev->group) { 3000da15bd9dSWang Nan tev->group = strdup(pev->group); 3001da15bd9dSWang Nan if (!tev->group) 3002da15bd9dSWang Nan goto errout; 3003da15bd9dSWang Nan } 3004da15bd9dSWang Nan 3005da15bd9dSWang Nan if (pev->event) { 3006da15bd9dSWang Nan tev->event = strdup(pev->event); 3007da15bd9dSWang Nan if (!tev->event) 3008da15bd9dSWang Nan goto errout; 3009da15bd9dSWang Nan } 3010da15bd9dSWang Nan 3011da15bd9dSWang Nan tev->nargs = pev->nargs; 3012da15bd9dSWang Nan tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 3013da15bd9dSWang Nan if (!tev->args) { 3014da15bd9dSWang Nan err = -ENOMEM; 3015da15bd9dSWang Nan goto errout; 3016da15bd9dSWang Nan } 3017da15bd9dSWang Nan for (i = 0; i < tev->nargs; i++) 3018da15bd9dSWang Nan copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); 3019da15bd9dSWang Nan 3020da15bd9dSWang Nan return 1; 3021da15bd9dSWang Nan 3022da15bd9dSWang Nan errout: 3023da15bd9dSWang Nan if (*tevs) { 3024da15bd9dSWang Nan clear_probe_trace_events(*tevs, 1); 3025da15bd9dSWang Nan *tevs = NULL; 3026da15bd9dSWang Nan } 3027da15bd9dSWang Nan return err; 3028da15bd9dSWang Nan } 3029da15bd9dSWang Nan 303042bba263SMasami Hiramatsu /* Concatinate two arrays */ 303142bba263SMasami Hiramatsu static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) 303242bba263SMasami Hiramatsu { 303342bba263SMasami Hiramatsu void *ret; 303442bba263SMasami Hiramatsu 303542bba263SMasami Hiramatsu ret = malloc(sz_a + sz_b); 303642bba263SMasami Hiramatsu if (ret) { 303742bba263SMasami Hiramatsu memcpy(ret, a, sz_a); 303842bba263SMasami Hiramatsu memcpy(ret + sz_a, b, sz_b); 303942bba263SMasami Hiramatsu } 304042bba263SMasami Hiramatsu return ret; 304142bba263SMasami Hiramatsu } 304242bba263SMasami Hiramatsu 304342bba263SMasami Hiramatsu static int 304442bba263SMasami Hiramatsu concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, 304542bba263SMasami Hiramatsu struct probe_trace_event **tevs2, int ntevs2) 304642bba263SMasami Hiramatsu { 304742bba263SMasami Hiramatsu struct probe_trace_event *new_tevs; 304842bba263SMasami Hiramatsu int ret = 0; 304942bba263SMasami Hiramatsu 305042bba263SMasami Hiramatsu if (ntevs == 0) { 305142bba263SMasami Hiramatsu *tevs = *tevs2; 305242bba263SMasami Hiramatsu *ntevs = ntevs2; 305342bba263SMasami Hiramatsu *tevs2 = NULL; 305442bba263SMasami Hiramatsu return 0; 305542bba263SMasami Hiramatsu } 305642bba263SMasami Hiramatsu 305742bba263SMasami Hiramatsu if (*ntevs + ntevs2 > probe_conf.max_probes) 305842bba263SMasami Hiramatsu ret = -E2BIG; 305942bba263SMasami Hiramatsu else { 306042bba263SMasami Hiramatsu /* Concatinate the array of probe_trace_event */ 306142bba263SMasami Hiramatsu new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), 306242bba263SMasami Hiramatsu *tevs2, ntevs2 * sizeof(**tevs2)); 306342bba263SMasami Hiramatsu if (!new_tevs) 306442bba263SMasami Hiramatsu ret = -ENOMEM; 306542bba263SMasami Hiramatsu else { 306642bba263SMasami Hiramatsu free(*tevs); 306742bba263SMasami Hiramatsu *tevs = new_tevs; 306842bba263SMasami Hiramatsu *ntevs += ntevs2; 306942bba263SMasami Hiramatsu } 307042bba263SMasami Hiramatsu } 307142bba263SMasami Hiramatsu if (ret < 0) 307242bba263SMasami Hiramatsu clear_probe_trace_events(*tevs2, ntevs2); 307342bba263SMasami Hiramatsu zfree(tevs2); 307442bba263SMasami Hiramatsu 307542bba263SMasami Hiramatsu return ret; 307642bba263SMasami Hiramatsu } 307742bba263SMasami Hiramatsu 307842bba263SMasami Hiramatsu /* 307942bba263SMasami Hiramatsu * Try to find probe_trace_event from given probe caches. Return the number 308042bba263SMasami Hiramatsu * of cached events found, if an error occurs return the error. 308142bba263SMasami Hiramatsu */ 308242bba263SMasami Hiramatsu static int find_cached_events(struct perf_probe_event *pev, 308342bba263SMasami Hiramatsu struct probe_trace_event **tevs, 308442bba263SMasami Hiramatsu const char *target) 308542bba263SMasami Hiramatsu { 308642bba263SMasami Hiramatsu struct probe_cache *cache; 308742bba263SMasami Hiramatsu struct probe_cache_entry *entry; 308842bba263SMasami Hiramatsu struct probe_trace_event *tmp_tevs = NULL; 308942bba263SMasami Hiramatsu int ntevs = 0; 309042bba263SMasami Hiramatsu int ret = 0; 309142bba263SMasami Hiramatsu 309242bba263SMasami Hiramatsu cache = probe_cache__new(target); 309342bba263SMasami Hiramatsu /* Return 0 ("not found") if the target has no probe cache. */ 309442bba263SMasami Hiramatsu if (!cache) 309542bba263SMasami Hiramatsu return 0; 309642bba263SMasami Hiramatsu 309742bba263SMasami Hiramatsu for_each_probe_cache_entry(entry, cache) { 309842bba263SMasami Hiramatsu /* Skip the cache entry which has no name */ 309942bba263SMasami Hiramatsu if (!entry->pev.event || !entry->pev.group) 310042bba263SMasami Hiramatsu continue; 310142bba263SMasami Hiramatsu if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && 310242bba263SMasami Hiramatsu strglobmatch(entry->pev.event, pev->event)) { 310342bba263SMasami Hiramatsu ret = probe_cache_entry__get_event(entry, &tmp_tevs); 310442bba263SMasami Hiramatsu if (ret > 0) 310542bba263SMasami Hiramatsu ret = concat_probe_trace_events(tevs, &ntevs, 310642bba263SMasami Hiramatsu &tmp_tevs, ret); 310742bba263SMasami Hiramatsu if (ret < 0) 310842bba263SMasami Hiramatsu break; 310942bba263SMasami Hiramatsu } 311042bba263SMasami Hiramatsu } 311142bba263SMasami Hiramatsu probe_cache__delete(cache); 311242bba263SMasami Hiramatsu if (ret < 0) { 311342bba263SMasami Hiramatsu clear_probe_trace_events(*tevs, ntevs); 311442bba263SMasami Hiramatsu zfree(tevs); 311542bba263SMasami Hiramatsu } else { 311642bba263SMasami Hiramatsu ret = ntevs; 311742bba263SMasami Hiramatsu if (ntevs > 0 && target && target[0] == '/') 311842bba263SMasami Hiramatsu pev->uprobes = true; 311942bba263SMasami Hiramatsu } 312042bba263SMasami Hiramatsu 312142bba263SMasami Hiramatsu return ret; 312242bba263SMasami Hiramatsu } 312342bba263SMasami Hiramatsu 31241de7b8bfSMasami Hiramatsu /* Try to find probe_trace_event from all probe caches */ 31251de7b8bfSMasami Hiramatsu static int find_cached_events_all(struct perf_probe_event *pev, 31261de7b8bfSMasami Hiramatsu struct probe_trace_event **tevs) 31271de7b8bfSMasami Hiramatsu { 31281de7b8bfSMasami Hiramatsu struct probe_trace_event *tmp_tevs = NULL; 31291de7b8bfSMasami Hiramatsu struct strlist *bidlist; 31301de7b8bfSMasami Hiramatsu struct str_node *nd; 31311de7b8bfSMasami Hiramatsu char *pathname; 31321de7b8bfSMasami Hiramatsu int ntevs = 0; 31331de7b8bfSMasami Hiramatsu int ret; 31341de7b8bfSMasami Hiramatsu 31351de7b8bfSMasami Hiramatsu /* Get the buildid list of all valid caches */ 31361de7b8bfSMasami Hiramatsu bidlist = build_id_cache__list_all(true); 31371de7b8bfSMasami Hiramatsu if (!bidlist) { 31381de7b8bfSMasami Hiramatsu ret = -errno; 31391de7b8bfSMasami Hiramatsu pr_debug("Failed to get buildids: %d\n", ret); 31401de7b8bfSMasami Hiramatsu return ret; 31411de7b8bfSMasami Hiramatsu } 31421de7b8bfSMasami Hiramatsu 31431de7b8bfSMasami Hiramatsu ret = 0; 31441de7b8bfSMasami Hiramatsu strlist__for_each_entry(nd, bidlist) { 31451de7b8bfSMasami Hiramatsu pathname = build_id_cache__origname(nd->s); 31461de7b8bfSMasami Hiramatsu ret = find_cached_events(pev, &tmp_tevs, pathname); 31471de7b8bfSMasami Hiramatsu /* In the case of cnt == 0, we just skip it */ 31481de7b8bfSMasami Hiramatsu if (ret > 0) 31491de7b8bfSMasami Hiramatsu ret = concat_probe_trace_events(tevs, &ntevs, 31501de7b8bfSMasami Hiramatsu &tmp_tevs, ret); 31511de7b8bfSMasami Hiramatsu free(pathname); 31521de7b8bfSMasami Hiramatsu if (ret < 0) 31531de7b8bfSMasami Hiramatsu break; 31541de7b8bfSMasami Hiramatsu } 31551de7b8bfSMasami Hiramatsu strlist__delete(bidlist); 31561de7b8bfSMasami Hiramatsu 31571de7b8bfSMasami Hiramatsu if (ret < 0) { 31581de7b8bfSMasami Hiramatsu clear_probe_trace_events(*tevs, ntevs); 31591de7b8bfSMasami Hiramatsu zfree(tevs); 31601de7b8bfSMasami Hiramatsu } else 31611de7b8bfSMasami Hiramatsu ret = ntevs; 31621de7b8bfSMasami Hiramatsu 31631de7b8bfSMasami Hiramatsu return ret; 31641de7b8bfSMasami Hiramatsu } 31651de7b8bfSMasami Hiramatsu 3166bc062230SMasami Hiramatsu static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, 3167bc062230SMasami Hiramatsu struct probe_trace_event **tevs) 3168bc062230SMasami Hiramatsu { 3169bc062230SMasami Hiramatsu struct probe_cache *cache; 3170bc062230SMasami Hiramatsu struct probe_cache_entry *entry; 3171bc062230SMasami Hiramatsu struct probe_trace_event *tev; 3172bc062230SMasami Hiramatsu struct str_node *node; 3173bc062230SMasami Hiramatsu int ret, i; 3174bc062230SMasami Hiramatsu 31751de7b8bfSMasami Hiramatsu if (pev->sdt) { 317642bba263SMasami Hiramatsu /* For SDT/cached events, we use special search functions */ 31771de7b8bfSMasami Hiramatsu if (!pev->target) 31781de7b8bfSMasami Hiramatsu return find_cached_events_all(pev, tevs); 31791de7b8bfSMasami Hiramatsu else 318042bba263SMasami Hiramatsu return find_cached_events(pev, tevs, pev->target); 31811de7b8bfSMasami Hiramatsu } 3182bc062230SMasami Hiramatsu cache = probe_cache__new(pev->target); 3183bc062230SMasami Hiramatsu if (!cache) 3184bc062230SMasami Hiramatsu return 0; 3185bc062230SMasami Hiramatsu 3186bc062230SMasami Hiramatsu entry = probe_cache__find(cache, pev); 3187bc062230SMasami Hiramatsu if (!entry) { 318836a009feSMasami Hiramatsu /* SDT must be in the cache */ 318936a009feSMasami Hiramatsu ret = pev->sdt ? -ENOENT : 0; 3190bc062230SMasami Hiramatsu goto out; 3191bc062230SMasami Hiramatsu } 3192bc062230SMasami Hiramatsu 3193bc062230SMasami Hiramatsu ret = strlist__nr_entries(entry->tevlist); 3194bc062230SMasami Hiramatsu if (ret > probe_conf.max_probes) { 3195bc062230SMasami Hiramatsu pr_debug("Too many entries matched in the cache of %s\n", 3196bc062230SMasami Hiramatsu pev->target ? : "kernel"); 3197bc062230SMasami Hiramatsu ret = -E2BIG; 3198bc062230SMasami Hiramatsu goto out; 3199bc062230SMasami Hiramatsu } 3200bc062230SMasami Hiramatsu 3201bc062230SMasami Hiramatsu *tevs = zalloc(ret * sizeof(*tev)); 3202bc062230SMasami Hiramatsu if (!*tevs) { 3203bc062230SMasami Hiramatsu ret = -ENOMEM; 3204bc062230SMasami Hiramatsu goto out; 3205bc062230SMasami Hiramatsu } 3206bc062230SMasami Hiramatsu 3207bc062230SMasami Hiramatsu i = 0; 3208bc062230SMasami Hiramatsu strlist__for_each_entry(node, entry->tevlist) { 3209bc062230SMasami Hiramatsu tev = &(*tevs)[i++]; 3210bc062230SMasami Hiramatsu ret = parse_probe_trace_command(node->s, tev); 3211bc062230SMasami Hiramatsu if (ret < 0) 3212bc062230SMasami Hiramatsu goto out; 3213bc062230SMasami Hiramatsu /* Set the uprobes attribute as same as original */ 3214bc062230SMasami Hiramatsu tev->uprobes = pev->uprobes; 3215bc062230SMasami Hiramatsu } 3216bc062230SMasami Hiramatsu ret = i; 3217bc062230SMasami Hiramatsu 3218bc062230SMasami Hiramatsu out: 3219bc062230SMasami Hiramatsu probe_cache__delete(cache); 3220bc062230SMasami Hiramatsu return ret; 3221bc062230SMasami Hiramatsu } 3222bc062230SMasami Hiramatsu 32230e60836bSSrikar Dronamraju static int convert_to_probe_trace_events(struct perf_probe_event *pev, 3224ddb2f58fSMasami Hiramatsu struct probe_trace_event **tevs) 3225e0faa8d3SMasami Hiramatsu { 3226eb948e50SMasami Hiramatsu int ret; 32274235b045SMasami Hiramatsu 322836a009feSMasami Hiramatsu if (!pev->group && !pev->sdt) { 32292a12ec13SMasami Hiramatsu /* Set group name if not given */ 32302a12ec13SMasami Hiramatsu if (!pev->uprobes) { 32312a12ec13SMasami Hiramatsu pev->group = strdup(PERFPROBE_GROUP); 32322a12ec13SMasami Hiramatsu ret = pev->group ? 0 : -ENOMEM; 32332a12ec13SMasami Hiramatsu } else 323444225521SMasami Hiramatsu ret = convert_exec_to_group(pev->target, &pev->group); 3235fb7345bbSMasami Hiramatsu if (ret != 0) { 3236fb7345bbSMasami Hiramatsu pr_warning("Failed to make a group name.\n"); 3237fb7345bbSMasami Hiramatsu return ret; 3238fb7345bbSMasami Hiramatsu } 3239fb7345bbSMasami Hiramatsu } 3240fb7345bbSMasami Hiramatsu 3241da15bd9dSWang Nan ret = try_to_find_absolute_address(pev, tevs); 3242da15bd9dSWang Nan if (ret > 0) 3243da15bd9dSWang Nan return ret; 3244da15bd9dSWang Nan 3245bc062230SMasami Hiramatsu /* At first, we need to lookup cache entry */ 3246bc062230SMasami Hiramatsu ret = find_probe_trace_events_from_cache(pev, tevs); 324736a009feSMasami Hiramatsu if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ 324836a009feSMasami Hiramatsu return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ 3249bc062230SMasami Hiramatsu 32504b4da7f7SMasami Hiramatsu /* Convert perf_probe_event with debuginfo */ 3251ddb2f58fSMasami Hiramatsu ret = try_to_find_probe_trace_events(pev, tevs); 3252e334016fSMasami Hiramatsu if (ret != 0) 3253190b57fcSMasami Hiramatsu return ret; /* Found in debuginfo or got an error */ 3254e0faa8d3SMasami Hiramatsu 3255ddb2f58fSMasami Hiramatsu return find_probe_trace_events_from_map(pev, tevs); 32564235b045SMasami Hiramatsu } 32574235b045SMasami Hiramatsu 325812fae5efSWang Nan int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) 32594235b045SMasami Hiramatsu { 3260844dffa5SNamhyung Kim int i, ret; 32614235b045SMasami Hiramatsu 32624235b045SMasami Hiramatsu /* Loop 1: convert all events */ 32634235b045SMasami Hiramatsu for (i = 0; i < npevs; i++) { 3264b031220dSMasami Hiramatsu /* Init kprobe blacklist if needed */ 326512fae5efSWang Nan if (!pevs[i].uprobes) 3266b031220dSMasami Hiramatsu kprobe_blacklist__init(); 32674235b045SMasami Hiramatsu /* Convert with or without debuginfo */ 326812fae5efSWang Nan ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); 3269146a1439SMasami Hiramatsu if (ret < 0) 3270844dffa5SNamhyung Kim return ret; 327112fae5efSWang Nan pevs[i].ntevs = ret; 32724235b045SMasami Hiramatsu } 3273b031220dSMasami Hiramatsu /* This just release blacklist only if allocated */ 3274b031220dSMasami Hiramatsu kprobe_blacklist__release(); 32754235b045SMasami Hiramatsu 3276844dffa5SNamhyung Kim return 0; 3277844dffa5SNamhyung Kim } 3278844dffa5SNamhyung Kim 32791c20b1d1SMasami Hiramatsu static int show_probe_trace_event(struct probe_trace_event *tev) 32801c20b1d1SMasami Hiramatsu { 32811c20b1d1SMasami Hiramatsu char *buf = synthesize_probe_trace_command(tev); 32821c20b1d1SMasami Hiramatsu 32831c20b1d1SMasami Hiramatsu if (!buf) { 32841c20b1d1SMasami Hiramatsu pr_debug("Failed to synthesize probe trace event.\n"); 32851c20b1d1SMasami Hiramatsu return -EINVAL; 32861c20b1d1SMasami Hiramatsu } 32871c20b1d1SMasami Hiramatsu 32881c20b1d1SMasami Hiramatsu /* Showing definition always go stdout */ 32891c20b1d1SMasami Hiramatsu printf("%s\n", buf); 32901c20b1d1SMasami Hiramatsu free(buf); 32911c20b1d1SMasami Hiramatsu 32921c20b1d1SMasami Hiramatsu return 0; 32931c20b1d1SMasami Hiramatsu } 32941c20b1d1SMasami Hiramatsu 32951c20b1d1SMasami Hiramatsu int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) 32961c20b1d1SMasami Hiramatsu { 32971c20b1d1SMasami Hiramatsu struct strlist *namelist = strlist__new(NULL, NULL); 32981c20b1d1SMasami Hiramatsu struct probe_trace_event *tev; 32991c20b1d1SMasami Hiramatsu struct perf_probe_event *pev; 33001c20b1d1SMasami Hiramatsu int i, j, ret = 0; 33011c20b1d1SMasami Hiramatsu 33021c20b1d1SMasami Hiramatsu if (!namelist) 33031c20b1d1SMasami Hiramatsu return -ENOMEM; 33041c20b1d1SMasami Hiramatsu 33051c20b1d1SMasami Hiramatsu for (j = 0; j < npevs && !ret; j++) { 33061c20b1d1SMasami Hiramatsu pev = &pevs[j]; 33071c20b1d1SMasami Hiramatsu for (i = 0; i < pev->ntevs && !ret; i++) { 33081c20b1d1SMasami Hiramatsu tev = &pev->tevs[i]; 33091c20b1d1SMasami Hiramatsu /* Skip if the symbol is out of .text or blacklisted */ 33101c20b1d1SMasami Hiramatsu if (!tev->point.symbol && !pev->uprobes) 33111c20b1d1SMasami Hiramatsu continue; 33121c20b1d1SMasami Hiramatsu 33131c20b1d1SMasami Hiramatsu /* Set new name for tev (and update namelist) */ 33141c20b1d1SMasami Hiramatsu ret = probe_trace_event__set_name(tev, pev, 33151c20b1d1SMasami Hiramatsu namelist, true); 33161c20b1d1SMasami Hiramatsu if (!ret) 33171c20b1d1SMasami Hiramatsu ret = show_probe_trace_event(tev); 33181c20b1d1SMasami Hiramatsu } 33191c20b1d1SMasami Hiramatsu } 33201c20b1d1SMasami Hiramatsu strlist__delete(namelist); 33211c20b1d1SMasami Hiramatsu 33221c20b1d1SMasami Hiramatsu return ret; 33231c20b1d1SMasami Hiramatsu } 33241c20b1d1SMasami Hiramatsu 332512fae5efSWang Nan int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3326844dffa5SNamhyung Kim { 3327844dffa5SNamhyung Kim int i, ret = 0; 3328844dffa5SNamhyung Kim 33294235b045SMasami Hiramatsu /* Loop 2: add all events */ 33308635bf6eSArnaldo Carvalho de Melo for (i = 0; i < npevs; i++) { 333112fae5efSWang Nan ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, 333212fae5efSWang Nan pevs[i].ntevs, 3333ddb2f58fSMasami Hiramatsu probe_conf.force_add); 3334fbee632dSArnaldo Carvalho de Melo if (ret < 0) 3335fbee632dSArnaldo Carvalho de Melo break; 3336fbee632dSArnaldo Carvalho de Melo } 3337844dffa5SNamhyung Kim return ret; 3338844dffa5SNamhyung Kim } 3339844dffa5SNamhyung Kim 334012fae5efSWang Nan void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3341844dffa5SNamhyung Kim { 3342844dffa5SNamhyung Kim int i, j; 3343844dffa5SNamhyung Kim 3344449e5b24SMasami Hiramatsu /* Loop 3: cleanup and free trace events */ 3345449e5b24SMasami Hiramatsu for (i = 0; i < npevs; i++) { 334612fae5efSWang Nan for (j = 0; j < pevs[i].ntevs; j++) 334712fae5efSWang Nan clear_probe_trace_event(&pevs[i].tevs[j]); 334812fae5efSWang Nan zfree(&pevs[i].tevs); 334912fae5efSWang Nan pevs[i].ntevs = 0; 3350a43aac29SNamhyung Kim clear_perf_probe_event(&pevs[i]); 3351449e5b24SMasami Hiramatsu } 3352844dffa5SNamhyung Kim } 3353844dffa5SNamhyung Kim 3354844dffa5SNamhyung Kim int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3355844dffa5SNamhyung Kim { 3356844dffa5SNamhyung Kim int ret; 3357844dffa5SNamhyung Kim 33589bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(pevs->uprobes); 33599bae1e8cSNamhyung Kim if (ret < 0) 33609bae1e8cSNamhyung Kim return ret; 33619bae1e8cSNamhyung Kim 336212fae5efSWang Nan ret = convert_perf_probe_events(pevs, npevs); 3363844dffa5SNamhyung Kim if (ret == 0) 336412fae5efSWang Nan ret = apply_perf_probe_events(pevs, npevs); 3365844dffa5SNamhyung Kim 336612fae5efSWang Nan cleanup_perf_probe_events(pevs, npevs); 3367146a1439SMasami Hiramatsu 33689bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 3369146a1439SMasami Hiramatsu return ret; 3370e0faa8d3SMasami Hiramatsu } 3371e0faa8d3SMasami Hiramatsu 3372307a464bSMasami Hiramatsu int del_perf_probe_events(struct strfilter *filter) 3373fa28244dSMasami Hiramatsu { 3374307a464bSMasami Hiramatsu int ret, ret2, ufd = -1, kfd = -1; 3375307a464bSMasami Hiramatsu char *str = strfilter__string(filter); 3376307a464bSMasami Hiramatsu 3377307a464bSMasami Hiramatsu if (!str) 3378307a464bSMasami Hiramatsu return -EINVAL; 3379307a464bSMasami Hiramatsu 3380fa28244dSMasami Hiramatsu /* Get current event names */ 338192f6c72eSMasami Hiramatsu ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); 338292f6c72eSMasami Hiramatsu if (ret < 0) 338392f6c72eSMasami Hiramatsu goto out; 3384225466f1SSrikar Dronamraju 338592f6c72eSMasami Hiramatsu ret = probe_file__del_events(kfd, filter); 3386307a464bSMasami Hiramatsu if (ret < 0 && ret != -ENOENT) 3387225466f1SSrikar Dronamraju goto error; 3388fa28244dSMasami Hiramatsu 338992f6c72eSMasami Hiramatsu ret2 = probe_file__del_events(ufd, filter); 3390dddc7ee3SMasami Hiramatsu if (ret2 < 0 && ret2 != -ENOENT) { 33916dbe31f7SMasami Hiramatsu ret = ret2; 3392dddc7ee3SMasami Hiramatsu goto error; 3393dddc7ee3SMasami Hiramatsu } 3394307a464bSMasami Hiramatsu ret = 0; 3395225466f1SSrikar Dronamraju 3396225466f1SSrikar Dronamraju error: 339792f6c72eSMasami Hiramatsu if (kfd >= 0) 3398225466f1SSrikar Dronamraju close(kfd); 339992f6c72eSMasami Hiramatsu if (ufd >= 0) 3400225466f1SSrikar Dronamraju close(ufd); 340192f6c72eSMasami Hiramatsu out: 3402307a464bSMasami Hiramatsu free(str); 3403146a1439SMasami Hiramatsu 3404146a1439SMasami Hiramatsu return ret; 3405fa28244dSMasami Hiramatsu } 3406225466f1SSrikar Dronamraju 34072df58634SMasami Hiramatsu int show_available_funcs(const char *target, struct strfilter *_filter, 34082df58634SMasami Hiramatsu bool user) 3409e80711caSMasami Hiramatsu { 3410fd227598SArnaldo Carvalho de Melo struct rb_node *nd; 34112df58634SMasami Hiramatsu struct map *map; 34122df58634SMasami Hiramatsu int ret; 34132df58634SMasami Hiramatsu 34149bae1e8cSNamhyung Kim ret = init_probe_symbol_maps(user); 34152df58634SMasami Hiramatsu if (ret < 0) 34162df58634SMasami Hiramatsu return ret; 34172df58634SMasami Hiramatsu 34182df58634SMasami Hiramatsu /* Get a symbol map */ 3419eebc509bSMasami Hiramatsu map = get_target_map(target, user); 34202df58634SMasami Hiramatsu if (!map) { 34212df58634SMasami Hiramatsu pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); 3422e80711caSMasami Hiramatsu return -EINVAL; 3423e80711caSMasami Hiramatsu } 34242df58634SMasami Hiramatsu 3425be39db9fSArnaldo Carvalho de Melo ret = map__load(map); 3426e7049342SMasami Hiramatsu if (ret) { 3427e7049342SMasami Hiramatsu if (ret == -2) { 3428e7049342SMasami Hiramatsu char *str = strfilter__string(_filter); 3429e7049342SMasami Hiramatsu pr_err("Failed to find symbols matched to \"%s\"\n", 3430e7049342SMasami Hiramatsu str); 3431e7049342SMasami Hiramatsu free(str); 3432e7049342SMasami Hiramatsu } else 3433e7049342SMasami Hiramatsu pr_err("Failed to load symbols in %s\n", 3434e7049342SMasami Hiramatsu (target) ? : "kernel"); 34352df58634SMasami Hiramatsu goto end; 34362df58634SMasami Hiramatsu } 3437e80711caSMasami Hiramatsu if (!dso__sorted_by_name(map->dso, map->type)) 3438e80711caSMasami Hiramatsu dso__sort_by_name(map->dso, map->type); 3439e80711caSMasami Hiramatsu 34402df58634SMasami Hiramatsu /* Show all (filtered) symbols */ 34412df58634SMasami Hiramatsu setup_pager(); 3442fd227598SArnaldo Carvalho de Melo 3443fd227598SArnaldo Carvalho de Melo for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) { 3444fd227598SArnaldo Carvalho de Melo struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); 3445fd227598SArnaldo Carvalho de Melo 3446fd227598SArnaldo Carvalho de Melo if (strfilter__compare(_filter, pos->sym.name)) 3447fd227598SArnaldo Carvalho de Melo printf("%s\n", pos->sym.name); 3448fd227598SArnaldo Carvalho de Melo } 3449fd227598SArnaldo Carvalho de Melo 34502df58634SMasami Hiramatsu end: 345184c2cafaSArnaldo Carvalho de Melo map__put(map); 34529bae1e8cSNamhyung Kim exit_probe_symbol_maps(); 3453225466f1SSrikar Dronamraju 34542df58634SMasami Hiramatsu return ret; 3455225466f1SSrikar Dronamraju } 3456225466f1SSrikar Dronamraju 3457da15bd9dSWang Nan int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, 3458da15bd9dSWang Nan struct perf_probe_arg *pvar) 3459da15bd9dSWang Nan { 3460da15bd9dSWang Nan tvar->value = strdup(pvar->var); 3461da15bd9dSWang Nan if (tvar->value == NULL) 3462da15bd9dSWang Nan return -ENOMEM; 3463da15bd9dSWang Nan if (pvar->type) { 3464da15bd9dSWang Nan tvar->type = strdup(pvar->type); 3465da15bd9dSWang Nan if (tvar->type == NULL) 3466da15bd9dSWang Nan return -ENOMEM; 3467da15bd9dSWang Nan } 3468da15bd9dSWang Nan if (pvar->name) { 3469da15bd9dSWang Nan tvar->name = strdup(pvar->name); 3470da15bd9dSWang Nan if (tvar->name == NULL) 3471da15bd9dSWang Nan return -ENOMEM; 3472da15bd9dSWang Nan } else 3473da15bd9dSWang Nan tvar->name = NULL; 3474da15bd9dSWang Nan return 0; 3475da15bd9dSWang Nan } 3476