static struct strlist *__probe_file__get_namelist(int fd, bool include_group) { char buf[128]; struct strlist *sl, *rawlist; struct str_node *ent; struct probe_trace_event tev; int ret = 0; memset(&tev, 0, sizeof(tev)); rawlist = probe_file__get_rawlist(fd); if (!rawlist) return NULL; sl = strlist__new(NULL, NULL); strlist__for_each(ent, rawlist) { ret = parse_probe_trace_command(ent->s, &tev); if (ret < 0) break; if (include_group) { ret = e_snprintf(buf, 128, "%s:%s", tev.group, tev.event); if (ret >= 0) ret = strlist__add(sl, buf); } else ret = strlist__add(sl, tev.event); clear_probe_trace_event(&tev); if (ret < 0) break; }
/* Get raw string list of current kprobe_events or uprobe_events */ struct strlist *probe_file__get_rawlist(int fd) { int ret, idx; FILE *fp; char buf[MAX_CMDLEN]; char *p; struct strlist *sl; if (fd < 0) return NULL; sl = strlist__new(NULL, NULL); fp = fdopen(dup(fd), "r"); while (!feof(fp)) { p = fgets(buf, MAX_CMDLEN, fp); if (!p) break; idx = strlen(p) - 1; if (p[idx] == '\n') p[idx] = '\0'; ret = strlist__add(sl, buf); if (ret < 0) { pr_debug("strlist__add failed (%d)\n", ret); strlist__delete(sl); return NULL; } } fclose(fp); return sl; }
/* Get raw string list of current kprobe_events */ static struct strlist *get_trace_kprobe_event_rawlist(int fd) { int ret, idx; FILE *fp; char buf[MAX_CMDLEN]; char *p; struct strlist *sl; sl = strlist__new(true, NULL); fp = fdopen(dup(fd), "r"); while (!feof(fp)) { p = fgets(buf, MAX_CMDLEN, fp); if (!p) break; idx = strlen(p) - 1; if (p[idx] == '\n') p[idx] = '\0'; ret = strlist__add(sl, buf); if (ret < 0) die("strlist__add failed: %s", strerror(-ret)); } fclose(fp); return sl; }
static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) { struct thread_map *threads = NULL, *nt; char name[256]; int items, total_tasks = 0; struct dirent **namelist = NULL; int i, j = 0; pid_t pid, prev_pid = INT_MAX; char *end_ptr; struct str_node *pos; struct strlist *slist = strlist__new(false, pid_str); if (!slist) return NULL; strlist__for_each(pos, slist) { pid = strtol(pos->s, &end_ptr, 10); if (pid == INT_MIN || pid == INT_MAX || (*end_ptr != '\0' && *end_ptr != ',')) goto out_free_threads; if (pid == prev_pid) continue; sprintf(name, "/proc/%d/task", pid); items = scandir(name, &namelist, filter, NULL); if (items <= 0) goto out_free_threads; total_tasks += items; nt = realloc(threads, (sizeof(*threads) + sizeof(pid_t) * total_tasks)); if (nt == NULL) goto out_free_threads; threads = nt; if (threads) { for (i = 0; i < items; i++) threads->map[j++] = atoi(namelist[i]->d_name); threads->nr = total_tasks; } for (i = 0; i < items; i++) free(namelist[i]); free(namelist); if (!threads) break; }
struct thread_map *thread_map__new_by_tid_str(const char *tid_str) { struct thread_map *threads = NULL, *nt; int ntasks = 0; pid_t tid, prev_tid = INT_MAX; char *end_ptr; struct str_node *pos; struct strlist_config slist_config = { .dont_dupstr = true, }; struct strlist *slist; /* perf-stat expects threads to be generated even if tid not given */ if (!tid_str) return thread_map__new_dummy(); slist = strlist__new(tid_str, &slist_config); if (!slist) return NULL; strlist__for_each_entry(pos, slist) { tid = strtol(pos->s, &end_ptr, 10); if (tid == INT_MIN || tid == INT_MAX || (*end_ptr != '\0' && *end_ptr != ',')) goto out_free_threads; if (tid == prev_tid) continue; ntasks++; nt = thread_map__realloc(threads, ntasks); if (nt == NULL) goto out_free_threads; threads = nt; thread_map__set_pid(threads, ntasks - 1, tid); threads->nr = ntasks; } out: if (threads) atomic_set(&threads->refcnt, 1); return threads; out_free_threads: zfree(&threads); strlist__delete(slist); goto out; }
static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) { struct thread_map *threads = NULL, *nt; int ntasks = 0; pid_t tid, prev_tid = INT_MAX; char *end_ptr; struct str_node *pos; struct strlist *slist; /* */ if (!tid_str) { threads = malloc(sizeof(*threads) + sizeof(pid_t)); if (threads != NULL) { threads->map[0] = -1; threads->nr = 1; } return threads; } slist = strlist__new(false, tid_str); if (!slist) return NULL; strlist__for_each(pos, slist) { tid = strtol(pos->s, &end_ptr, 10); if (tid == INT_MIN || tid == INT_MAX || (*end_ptr != '\0' && *end_ptr != ',')) goto out_free_threads; if (tid == prev_tid) continue; ntasks++; nt = realloc(threads, sizeof(*threads) + sizeof(pid_t) * ntasks); if (nt == NULL) goto out_free_threads; threads = nt; threads->map[ntasks - 1] = tid; threads->nr = ntasks; }
static int find_probe_point_by_func(struct probe_finder *pf) { struct dwarf_callback_param _param = {.data = (void *)pf, .retval = 0}; dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0); return _param.retval; } struct pubname_callback_param { char *function; char *file; Dwarf_Die *cu_die; Dwarf_Die *sp_die; int found; }; static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) { struct pubname_callback_param *param = data; if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) { if (dwarf_tag(param->sp_die) != DW_TAG_subprogram) return DWARF_CB_OK; if (die_compare_name(param->sp_die, param->function)) { if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die)) return DWARF_CB_OK; if (param->file && strtailcmp(param->file, dwarf_decl_file(param->sp_die))) return DWARF_CB_OK; param->found = 1; return DWARF_CB_ABORT; } } return DWARF_CB_OK; } static int debuginfo__find_probes(struct debuginfo *self, struct probe_finder *pf) { struct perf_probe_point *pp = &pf->pev->point; Dwarf_Off off, noff; size_t cuhl; Dwarf_Die *diep; int ret = 0; #if _ELFUTILS_PREREQ(0, 142) pf->cfi = dwarf_getcfi(self->dbg); #endif off = 0; line_list__init(&pf->lcache); if (pp->function) { struct pubname_callback_param pubname_param = { .function = pp->function, .file = pp->file, .cu_die = &pf->cu_die, .sp_die = &pf->sp_die, .found = 0, }; struct dwarf_callback_param probe_param = { .data = pf, }; dwarf_getpubnames(self->dbg, pubname_search_cb, &pubname_param, 0); if (pubname_param.found) { ret = probe_point_search_cb(&pf->sp_die, &probe_param); if (ret) goto found; } } while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die); if (!diep) continue; if (pp->file) pf->fname = cu_find_realpath(&pf->cu_die, pp->file); else pf->fname = NULL; if (!pp->file || pf->fname) { if (pp->function) ret = find_probe_point_by_func(pf); else if (pp->lazy_line) ret = find_probe_point_lazy(NULL, pf); else { pf->lno = pp->line; ret = find_probe_point_by_line(pf); } if (ret < 0) break; } off = noff; } found: line_list__free(&pf->lcache); return ret; } static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) { struct trace_event_finder *tf = container_of(pf, struct trace_event_finder, pf); struct probe_trace_event *tev; int ret, i; if (tf->ntevs == tf->max_tevs) { pr_warning("Too many( > %d) probe point found.\n", tf->max_tevs); return -ERANGE; } tev = &tf->tevs[tf->ntevs++]; ret = convert_to_trace_point(&pf->sp_die, pf->addr, pf->pev->point.retprobe, &tev->point); if (ret < 0) return ret; pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, tev->point.offset); tev->nargs = pf->pev->nargs; tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); if (tev->args == NULL) return -ENOMEM; for (i = 0; i < pf->pev->nargs; i++) { pf->pvar = &pf->pev->args[i]; pf->tvar = &tev->args[i]; ret = find_variable(sc_die, pf); if (ret != 0) return ret; } return 0; } int debuginfo__find_trace_events(struct debuginfo *self, struct perf_probe_event *pev, struct probe_trace_event **tevs, int max_tevs) { struct trace_event_finder tf = { .pf = {.pev = pev, .callback = add_probe_trace_event}, .max_tevs = max_tevs}; int ret; *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs); if (*tevs == NULL) return -ENOMEM; tf.tevs = *tevs; tf.ntevs = 0; ret = debuginfo__find_probes(self, &tf.pf); if (ret < 0) { free(*tevs); *tevs = NULL; return ret; } return (ret < 0) ? ret : tf.ntevs; } #define MAX_VAR_LEN 64 static int collect_variables_cb(Dwarf_Die *die_mem, void *data) { struct available_var_finder *af = data; struct variable_list *vl; char buf[MAX_VAR_LEN]; int tag, ret; vl = &af->vls[af->nvls - 1]; tag = dwarf_tag(die_mem); if (tag == DW_TAG_formal_parameter || tag == DW_TAG_variable) { ret = convert_variable_location(die_mem, af->pf.addr, af->pf.fb_ops, NULL); if (ret == 0) { ret = die_get_varname(die_mem, buf, MAX_VAR_LEN); pr_debug2("Add new var: %s\n", buf); if (ret > 0) strlist__add(vl->vars, buf); } } if (af->child && dwarf_haspc(die_mem, af->pf.addr)) return DIE_FIND_CB_CONTINUE; else return DIE_FIND_CB_SIBLING; } static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf) { struct available_var_finder *af = container_of(pf, struct available_var_finder, pf); struct variable_list *vl; Dwarf_Die die_mem; int ret; if (af->nvls == af->max_vls) { pr_warning("Too many( > %d) probe point found.\n", af->max_vls); return -ERANGE; } vl = &af->vls[af->nvls++]; ret = convert_to_trace_point(&pf->sp_die, pf->addr, pf->pev->point.retprobe, &vl->point); if (ret < 0) return ret; pr_debug("Probe point found: %s+%lu\n", vl->point.symbol, vl->point.offset); vl->vars = strlist__new(true, NULL); if (vl->vars == NULL) return -ENOMEM; af->child = true; die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem); if (!af->externs) goto out; af->child = false; die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem); out: if (strlist__empty(vl->vars)) { strlist__delete(vl->vars); vl->vars = NULL; } return ret; } int debuginfo__find_available_vars_at(struct debuginfo *self, struct perf_probe_event *pev, struct variable_list **vls, int max_vls, bool externs) { struct available_var_finder af = { .pf = {.pev = pev, .callback = add_available_vars}, .max_vls = max_vls, .externs = externs}; int ret; *vls = zalloc(sizeof(struct variable_list) * max_vls); if (*vls == NULL) return -ENOMEM; af.vls = *vls; af.nvls = 0; ret = debuginfo__find_probes(self, &af.pf); if (ret < 0) { while (af.nvls--) { if (af.vls[af.nvls].point.symbol) free(af.vls[af.nvls].point.symbol); if (af.vls[af.nvls].vars) strlist__delete(af.vls[af.nvls].vars); } free(af.vls); *vls = NULL; return ret; } return (ret < 0) ? ret : af.nvls; }
static int find_probe_point_by_func(struct probe_finder *pf) { struct dwarf_callback_param _param = {.data = (void *)pf, .retval = 0}; dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0); return _param.retval; } struct pubname_callback_param { char *function; char *file; Dwarf_Die *cu_die; Dwarf_Die *sp_die; int found; }; static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) { struct pubname_callback_param *param = data; if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) { if (dwarf_tag(param->sp_die) != DW_TAG_subprogram) return DWARF_CB_OK; if (die_compare_name(param->sp_die, param->function)) { if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die)) return DWARF_CB_OK; if (param->file && strtailcmp(param->file, dwarf_decl_file(param->sp_die))) return DWARF_CB_OK; param->found = 1; return DWARF_CB_ABORT; } } return DWARF_CB_OK; } /* Find probe points from debuginfo */ static int debuginfo__find_probes(struct debuginfo *dbg, struct probe_finder *pf) { struct perf_probe_point *pp = &pf->pev->point; Dwarf_Off off, noff; size_t cuhl; Dwarf_Die *diep; int ret = 0; #if _ELFUTILS_PREREQ(0, 142) Elf *elf; GElf_Ehdr ehdr; GElf_Shdr shdr; /* Get the call frame information from this dwarf */ elf = dwarf_getelf(dbg->dbg); if (elf == NULL) return -EINVAL; if (gelf_getehdr(elf, &ehdr) == NULL) return -EINVAL; if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) && shdr.sh_type == SHT_PROGBITS) { pf->cfi = dwarf_getcfi_elf(elf); } else { pf->cfi = dwarf_getcfi(dbg->dbg); } #endif off = 0; pf->lcache = intlist__new(NULL); if (!pf->lcache) return -ENOMEM; /* Fastpath: lookup by function name from .debug_pubnames section */ if (pp->function) { struct pubname_callback_param pubname_param = { .function = pp->function, .file = pp->file, .cu_die = &pf->cu_die, .sp_die = &pf->sp_die, .found = 0, }; struct dwarf_callback_param probe_param = { .data = pf, }; dwarf_getpubnames(dbg->dbg, pubname_search_cb, &pubname_param, 0); if (pubname_param.found) { ret = probe_point_search_cb(&pf->sp_die, &probe_param); if (ret) goto found; } } /* Loop on CUs (Compilation Unit) */ while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { /* Get the DIE(Debugging Information Entry) of this CU */ diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die); if (!diep) continue; /* Check if target file is included. */ if (pp->file) pf->fname = cu_find_realpath(&pf->cu_die, pp->file); else pf->fname = NULL; if (!pp->file || pf->fname) { if (pp->function) ret = find_probe_point_by_func(pf); else if (pp->lazy_line) ret = find_probe_point_lazy(&pf->cu_die, pf); else { pf->lno = pp->line; ret = find_probe_point_by_line(pf); } if (ret < 0) break; } off = noff; } found: intlist__delete(pf->lcache); pf->lcache = NULL; return ret; } struct local_vars_finder { struct probe_finder *pf; struct perf_probe_arg *args; int max_args; int nargs; int ret; }; /* Collect available variables in this scope */ static int copy_variables_cb(Dwarf_Die *die_mem, void *data) { struct local_vars_finder *vf = data; struct probe_finder *pf = vf->pf; int tag; tag = dwarf_tag(die_mem); if (tag == DW_TAG_formal_parameter || tag == DW_TAG_variable) { if (convert_variable_location(die_mem, vf->pf->addr, vf->pf->fb_ops, &pf->sp_die, NULL) == 0) { vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem); if (vf->args[vf->nargs].var == NULL) { vf->ret = -ENOMEM; return DIE_FIND_CB_END; } pr_debug(" %s", vf->args[vf->nargs].var); vf->nargs++; } } if (dwarf_haspc(die_mem, vf->pf->addr)) return DIE_FIND_CB_CONTINUE; else return DIE_FIND_CB_SIBLING; } static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf, struct perf_probe_arg *args) { Dwarf_Die die_mem; int i; int n = 0; struct local_vars_finder vf = {.pf = pf, .args = args, .max_args = MAX_PROBE_ARGS, .ret = 0}; for (i = 0; i < pf->pev->nargs; i++) { /* var never be NULL */ if (strcmp(pf->pev->args[i].var, "$vars") == 0) { pr_debug("Expanding $vars into:"); vf.nargs = n; /* Special local variables */ die_find_child(sc_die, copy_variables_cb, (void *)&vf, &die_mem); pr_debug(" (%d)\n", vf.nargs - n); if (vf.ret < 0) return vf.ret; n = vf.nargs; } else { /* Copy normal argument */ args[n] = pf->pev->args[i]; n++; } } return n; } /* Add a found probe point into trace event list */ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) { struct trace_event_finder *tf = container_of(pf, struct trace_event_finder, pf); struct probe_trace_event *tev; struct perf_probe_arg *args; int ret, i; /* Check number of tevs */ if (tf->ntevs == tf->max_tevs) { pr_warning("Too many( > %d) probe point found.\n", tf->max_tevs); return -ERANGE; } tev = &tf->tevs[tf->ntevs++]; /* Trace point should be converted from subprogram DIE */ ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr, pf->pev->point.retprobe, &tev->point); if (ret < 0) return ret; pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, tev->point.offset); /* Expand special probe argument if exist */ args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS); if (args == NULL) return -ENOMEM; ret = expand_probe_args(sc_die, pf, args); if (ret < 0) goto end; tev->nargs = ret; tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); if (tev->args == NULL) { ret = -ENOMEM; goto end; } /* Find each argument */ for (i = 0; i < tev->nargs; i++) { pf->pvar = &args[i]; pf->tvar = &tev->args[i]; /* Variable should be found from scope DIE */ ret = find_variable(sc_die, pf); if (ret != 0) break; } end: free(args); return ret; } /* Find probe_trace_events specified by perf_probe_event from debuginfo */ int debuginfo__find_trace_events(struct debuginfo *dbg, struct perf_probe_event *pev, struct probe_trace_event **tevs, int max_tevs) { struct trace_event_finder tf = { .pf = {.pev = pev, .callback = add_probe_trace_event}, .mod = dbg->mod, .max_tevs = max_tevs}; int ret; /* Allocate result tevs array */ *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs); if (*tevs == NULL) return -ENOMEM; tf.tevs = *tevs; tf.ntevs = 0; ret = debuginfo__find_probes(dbg, &tf.pf); if (ret < 0) { zfree(tevs); return ret; } return (ret < 0) ? ret : tf.ntevs; } #define MAX_VAR_LEN 64 /* Collect available variables in this scope */ static int collect_variables_cb(Dwarf_Die *die_mem, void *data) { struct available_var_finder *af = data; struct variable_list *vl; char buf[MAX_VAR_LEN]; int tag, ret; vl = &af->vls[af->nvls - 1]; tag = dwarf_tag(die_mem); if (tag == DW_TAG_formal_parameter || tag == DW_TAG_variable) { ret = convert_variable_location(die_mem, af->pf.addr, af->pf.fb_ops, &af->pf.sp_die, NULL); if (ret == 0) { ret = die_get_varname(die_mem, buf, MAX_VAR_LEN); pr_debug2("Add new var: %s\n", buf); if (ret > 0) strlist__add(vl->vars, buf); } } if (af->child && dwarf_haspc(die_mem, af->pf.addr)) return DIE_FIND_CB_CONTINUE; else return DIE_FIND_CB_SIBLING; } /* Add a found vars into available variables list */ static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf) { struct available_var_finder *af = container_of(pf, struct available_var_finder, pf); struct variable_list *vl; Dwarf_Die die_mem; int ret; /* Check number of tevs */ if (af->nvls == af->max_vls) { pr_warning("Too many( > %d) probe point found.\n", af->max_vls); return -ERANGE; } vl = &af->vls[af->nvls++]; /* Trace point should be converted from subprogram DIE */ ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr, pf->pev->point.retprobe, &vl->point); if (ret < 0) return ret; pr_debug("Probe point found: %s+%lu\n", vl->point.symbol, vl->point.offset); /* Find local variables */ vl->vars = strlist__new(true, NULL); if (vl->vars == NULL) return -ENOMEM; af->child = true; die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem); /* Find external variables */ if (!af->externs) goto out; /* Don't need to search child DIE for externs. */ af->child = false; die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem); out: if (strlist__empty(vl->vars)) { strlist__delete(vl->vars); vl->vars = NULL; } return ret; } /* * Find available variables at given probe point * Return the number of found probe points. Return 0 if there is no * matched probe point. Return <0 if an error occurs. */ int debuginfo__find_available_vars_at(struct debuginfo *dbg, struct perf_probe_event *pev, struct variable_list **vls, int max_vls, bool externs) { struct available_var_finder af = { .pf = {.pev = pev, .callback = add_available_vars}, .mod = dbg->mod, .max_vls = max_vls, .externs = externs}; int ret; /* Allocate result vls array */ *vls = zalloc(sizeof(struct variable_list) * max_vls); if (*vls == NULL) return -ENOMEM; af.vls = *vls; af.nvls = 0; ret = debuginfo__find_probes(dbg, &af.pf); if (ret < 0) { /* Free vlist for error */ while (af.nvls--) { zfree(&af.vls[af.nvls].point.symbol); strlist__delete(af.vls[af.nvls].vars); } zfree(vls); return ret; } return (ret < 0) ? ret : af.nvls; } /* Reverse search */ int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr, struct perf_probe_point *ppt) { Dwarf_Die cudie, spdie, indie; Dwarf_Addr _addr = 0, baseaddr = 0; const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp; int baseline = 0, lineno = 0, ret = 0; /* Find cu die */ if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) { pr_warning("Failed to find debug information for address %lx\n", addr); ret = -EINVAL; goto end; } /* Find a corresponding line (filename and lineno) */ cu_find_lineinfo(&cudie, addr, &fname, &lineno); /* Don't care whether it failed or not */ /* Find a corresponding function (name, baseline and baseaddr) */ if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) { /* Get function entry information */ func = basefunc = dwarf_diename(&spdie); if (!func || dwarf_entrypc(&spdie, &baseaddr) != 0 || dwarf_decl_line(&spdie, &baseline) != 0) { lineno = 0; goto post; } fname = dwarf_decl_file(&spdie); if (addr == (unsigned long)baseaddr) { /* Function entry - Relative line number is 0 */ lineno = baseline; goto post; } /* Track down the inline functions step by step */ while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) { /* There is an inline function */ if (dwarf_entrypc(&indie, &_addr) == 0 && _addr == addr) { /* * addr is at an inline function entry. * In this case, lineno should be the call-site * line number. (overwrite lineinfo) */ lineno = die_get_call_lineno(&indie); fname = die_get_call_file(&indie); break; } else { /* * addr is in an inline function body. * Since lineno points one of the lines * of the inline function, baseline should * be the entry line of the inline function. */ tmp = dwarf_diename(&indie); if (!tmp || dwarf_decl_line(&indie, &baseline) != 0) break; func = tmp; spdie = indie; } } /* Verify the lineno and baseline are in a same file */ tmp = dwarf_decl_file(&spdie); if (!tmp || strcmp(tmp, fname) != 0) lineno = 0; } post: /* Make a relative line number or an offset */ if (lineno) ppt->line = lineno - baseline; else if (basefunc) { ppt->offset = addr - (unsigned long)baseaddr; func = basefunc; } /* Duplicate strings */ if (func) { ppt->function = strdup(func); if (ppt->function == NULL) { ret = -ENOMEM; goto end; } } if (fname) { ppt->file = strdup(fname); if (ppt->file == NULL) { zfree(&ppt->function); ret = -ENOMEM; goto end; } } end: if (ret == 0 && (fname || func)) ret = 1; /* Found a point */ return ret; } /* Add a line and store the src path */ static int line_range_add_line(const char *src, unsigned int lineno, struct line_range *lr) { /* Copy source path */ if (!lr->path) { lr->path = strdup(src); if (lr->path == NULL) return -ENOMEM; } return intlist__add(lr->line_list, lineno); } static int line_range_walk_cb(const char *fname, int lineno, Dwarf_Addr addr __maybe_unused, void *data) { struct line_finder *lf = data; int err; if ((strtailcmp(fname, lf->fname) != 0) || (lf->lno_s > lineno || lf->lno_e < lineno)) return 0; err = line_range_add_line(fname, lineno, lf->lr); if (err < 0 && err != -EEXIST) return err; return 0; } /* Find line range from its line number */ static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) { int ret; ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf); /* Update status */ if (ret >= 0) if (!intlist__empty(lf->lr->line_list)) ret = lf->found = 1; else ret = 0; /* Lines are not found */ else { zfree(&lf->lr->path); } return ret; }
int cmd_buildid_cache(int argc, const char **argv) { struct strlist *list; struct str_node *pos; int ret = 0; int ns_id = -1; bool force = false; char const *add_name_list_str = NULL, *remove_name_list_str = NULL, *purge_name_list_str = NULL, *missing_filename = NULL, *update_name_list_str = NULL, *kcore_filename = NULL; char sbuf[STRERR_BUFSIZE]; struct perf_data data = { .mode = PERF_DATA_MODE_READ, }; struct perf_session *session = NULL; struct nsinfo *nsi = NULL; const struct option buildid_cache_options[] = { OPT_STRING('a', "add", &add_name_list_str, "file list", "file(s) to add"), OPT_STRING('k', "kcore", &kcore_filename, "file", "kcore file to add"), OPT_STRING('r', "remove", &remove_name_list_str, "file list", "file(s) to remove"), OPT_STRING('p', "purge", &purge_name_list_str, "path list", "path(s) to remove (remove old caches too)"), OPT_STRING('M', "missing", &missing_filename, "file", "to find missing build ids in the cache"), OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), OPT_STRING('u', "update", &update_name_list_str, "file list", "file(s) to update"), OPT_INCR('v', "verbose", &verbose, "be more verbose"), OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"), OPT_END() }; const char * const buildid_cache_usage[] = { "perf buildid-cache [<options>]", NULL }; argc = parse_options(argc, argv, buildid_cache_options, buildid_cache_usage, 0); if (argc || (!add_name_list_str && !kcore_filename && !remove_name_list_str && !purge_name_list_str && !missing_filename && !update_name_list_str)) usage_with_options(buildid_cache_usage, buildid_cache_options); if (ns_id > 0) nsi = nsinfo__new(ns_id); if (missing_filename) { data.file.path = missing_filename; data.force = force; session = perf_session__new(&data, false, NULL); if (session == NULL) return -1; } if (symbol__init(session ? &session->header.env : NULL) < 0) goto out; setup_pager(); if (add_name_list_str) { list = strlist__new(add_name_list_str, NULL); if (list) { strlist__for_each_entry(pos, list) if (build_id_cache__add_file(pos->s, nsi)) { if (errno == EEXIST) { pr_debug("%s already in the cache\n", pos->s); continue; } pr_warning("Couldn't add %s: %s\n", pos->s, str_error_r(errno, sbuf, sizeof(sbuf))); } strlist__delete(list); } } if (remove_name_list_str) { list = strlist__new(remove_name_list_str, NULL); if (list) { strlist__for_each_entry(pos, list) if (build_id_cache__remove_file(pos->s, nsi)) { if (errno == ENOENT) { pr_debug("%s wasn't in the cache\n", pos->s); continue; } pr_warning("Couldn't remove %s: %s\n", pos->s, str_error_r(errno, sbuf, sizeof(sbuf))); } strlist__delete(list); } } if (purge_name_list_str) { list = strlist__new(purge_name_list_str, NULL); if (list) { strlist__for_each_entry(pos, list) if (build_id_cache__purge_path(pos->s, nsi)) { if (errno == ENOENT) { pr_debug("%s wasn't in the cache\n", pos->s); continue; } pr_warning("Couldn't remove %s: %s\n", pos->s, str_error_r(errno, sbuf, sizeof(sbuf))); } strlist__delete(list); } } if (missing_filename) ret = build_id_cache__fprintf_missing(session, stdout); if (update_name_list_str) { list = strlist__new(update_name_list_str, NULL); if (list) { strlist__for_each_entry(pos, list) if (build_id_cache__update_file(pos->s, nsi)) { if (errno == ENOENT) { pr_debug("%s wasn't in the cache\n", pos->s); continue; } pr_warning("Couldn't update %s: %s\n", pos->s, str_error_r(errno, sbuf, sizeof(sbuf))); } strlist__delete(list); } } if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force)) pr_warning("Couldn't add %s\n", kcore_filename); out: perf_session__delete(session); nsinfo__zput(nsi); return ret; }
static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) { struct thread_map *threads = NULL, *nt; char name[256]; int items, total_tasks = 0; struct dirent **namelist = NULL; int i, j = 0; pid_t pid, prev_pid = INT_MAX; char *end_ptr; struct str_node *pos; struct strlist_config slist_config = { .dont_dupstr = true, }; struct strlist *slist = strlist__new(pid_str, &slist_config); if (!slist) return NULL; strlist__for_each_entry(pos, slist) { pid = strtol(pos->s, &end_ptr, 10); if (pid == INT_MIN || pid == INT_MAX || (*end_ptr != '\0' && *end_ptr != ',')) goto out_free_threads; if (pid == prev_pid) continue; sprintf(name, "/proc/%d/task", pid); items = scandir(name, &namelist, filter, NULL); if (items <= 0) goto out_free_threads; total_tasks += items; nt = thread_map__realloc(threads, total_tasks); if (nt == NULL) goto out_free_namelist; threads = nt; for (i = 0; i < items; i++) { thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name)); zfree(&namelist[i]); } threads->nr = total_tasks; free(namelist); } out: strlist__delete(slist); if (threads) atomic_set(&threads->refcnt, 1); return threads; out_free_namelist: for (i = 0; i < items; i++) zfree(&namelist[i]); free(namelist); out_free_threads: zfree(&threads); goto out; }
int main(int argc, char *argv[]) { int n; struct strlist *sections; char *infile = NULL, *outfile = NULL; int fd; Elf *elf; Elf_Scn *scn; int copy_all_sections = 0; ElfCreator *ctor; sections = strlist__new(false); for (n = 1; n < argc; n++) { if (!strcmp(argv[n], "-a")) { copy_all_sections = 1; } else if (!strcmp(argv[n], "-s")) { if (n == argc-1) { fprintf(stderr, "Missing argument to -s\n"); return -1; } n++; strlist__add(sections, argv[n]); continue; } else if (!strcmp(argv[n], "-o")) { if (n == argc-1) { fprintf(stderr, "Missing argument to -o\n"); return -1; } n++; outfile = argv[n]; continue; } else if (!strcmp(argv[n], "-?") || !strcmp(argv[n], "--help") || !strcmp(argv[n], "--usage")) { printf("usage: scncopy [-s section0 [[-s section1] ... -s sectionN] | -a ] -o outfile infile\n"); return 0; } else if (n == argc-1) { infile = argv[n]; } else { fprintf(stderr, "usage: pjoc -s section 0 [[-s section1] ... -s sectionN] -o outfile infile\n"); return 1; } } if (!infile || !outfile) { fprintf(stderr, "usage: pjoc -s section 0 [[-s section1] ... -s sectionN] -o outfile infile\n"); return 1; } if (!(fd = open(infile, O_RDONLY))) { fprintf(stderr, "Could not open \"%s\" for reading: %m\n", infile); return 1; } elf_version(EV_CURRENT); if ((elf = elf_begin(fd, ELF_C_READ_MMAP_PRIVATE, NULL)) == NULL) { fprintf(stderr, "cannot get elf descriptor for \"%s\": %s\n", infile, elf_errmsg(-1)); close(fd); return 1; } if (elf_kind(elf) != ELF_K_ELF) { fprintf(stderr, "\"%s\" is not an ELF file\n", infile); err: elf_end(elf); close(fd); return 1; } if ((ctor = elfcreator_begin(outfile, elf)) == NULL) { fprintf(stderr, "could not initialize ELF creator\n"); goto err; } scn = NULL; while ((scn = elf_nextscn(elf, scn)) != NULL) { GElf_Shdr shdr_mem, *shdr; shdr = gelf_getshdr(scn, &shdr_mem); if (shdr == NULL) continue; if (!should_copy_scn(elf, shdr, sections) && !copy_all_sections) continue; elfcreator_copy_scn(ctor, elf, scn); } elfcreator_end(ctor); return 0; }
/* Search function from function name */ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) { struct dwarf_callback_param *param = data; struct probe_finder *pf = param->data; struct perf_probe_point *pp = &pf->pev->point; /* Check tag and diename */ if (dwarf_tag(sp_die) != DW_TAG_subprogram || !die_compare_name(sp_die, pp->function)) return DWARF_CB_OK; pf->fname = dwarf_decl_file(sp_die); if (pp->line) { /* Function relative line */ dwarf_decl_line(sp_die, &pf->lno); pf->lno += pp->line; param->retval = find_probe_point_by_line(pf); } else if (!dwarf_func_inline(sp_die)) { /* Real function */ if (pp->lazy_line) param->retval = find_probe_point_lazy(sp_die, pf); else { if (dwarf_entrypc(sp_die, &pf->addr) != 0) { pr_warning("Failed to get entry address of " "%s.\n", dwarf_diename(sp_die)); param->retval = -ENOENT; return DWARF_CB_ABORT; } pf->addr += pp->offset; /* TODO: Check the address in this function */ param->retval = call_probe_finder(sp_die, pf); } } else { struct dwarf_callback_param _param = {.data = (void *)pf, .retval = 0}; /* Inlined function: search instances */ dwarf_func_inline_instances(sp_die, probe_point_inline_cb, &_param); param->retval = _param.retval; } return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */ } static int find_probe_point_by_func(struct probe_finder *pf) { struct dwarf_callback_param _param = {.data = (void *)pf, .retval = 0}; dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0); return _param.retval; } /* Find probe points from debuginfo */ static int find_probes(int fd, struct probe_finder *pf) { struct perf_probe_point *pp = &pf->pev->point; Dwarf_Off off, noff; size_t cuhl; Dwarf_Die *diep; Dwarf *dbg = NULL; Dwfl *dwfl; Dwarf_Addr bias; /* Currently ignored */ int ret = 0; dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias); if (!dbg) { pr_warning("No debug information found in the vmlinux - " "please rebuild with CONFIG_DEBUG_INFO=y.\n"); return -EBADF; } #if _ELFUTILS_PREREQ(0, 142) /* Get the call frame information from this dwarf */ pf->cfi = dwarf_getcfi(dbg); #endif off = 0; line_list__init(&pf->lcache); /* Loop on CUs (Compilation Unit) */ while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { /* Get the DIE(Debugging Information Entry) of this CU */ diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die); if (!diep) continue; /* Check if target file is included. */ if (pp->file) pf->fname = cu_find_realpath(&pf->cu_die, pp->file); else pf->fname = NULL; if (!pp->file || pf->fname) { if (pp->function) ret = find_probe_point_by_func(pf); else if (pp->lazy_line) ret = find_probe_point_lazy(NULL, pf); else { pf->lno = pp->line; ret = find_probe_point_by_line(pf); } if (ret < 0) break; } off = noff; } line_list__free(&pf->lcache); if (dwfl) dwfl_end(dwfl); return ret; } /* Add a found probe point into trace event list */ static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf) { struct trace_event_finder *tf = container_of(pf, struct trace_event_finder, pf); struct probe_trace_event *tev; int ret, i; /* Check number of tevs */ if (tf->ntevs == tf->max_tevs) { pr_warning("Too many( > %d) probe point found.\n", tf->max_tevs); return -ERANGE; } tev = &tf->tevs[tf->ntevs++]; ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe, &tev->point); if (ret < 0) return ret; pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, tev->point.offset); /* Find each argument */ tev->nargs = pf->pev->nargs; tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); if (tev->args == NULL) return -ENOMEM; for (i = 0; i < pf->pev->nargs; i++) { pf->pvar = &pf->pev->args[i]; pf->tvar = &tev->args[i]; ret = find_variable(sp_die, pf); if (ret != 0) return ret; } return 0; } /* Find probe_trace_events specified by perf_probe_event from debuginfo */ int find_probe_trace_events(int fd, struct perf_probe_event *pev, struct probe_trace_event **tevs, int max_tevs) { struct trace_event_finder tf = { .pf = {.pev = pev, .callback = add_probe_trace_event}, .max_tevs = max_tevs}; int ret; /* Allocate result tevs array */ *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs); if (*tevs == NULL) return -ENOMEM; tf.tevs = *tevs; tf.ntevs = 0; ret = find_probes(fd, &tf.pf); if (ret < 0) { free(*tevs); *tevs = NULL; return ret; } return (ret < 0) ? ret : tf.ntevs; } #define MAX_VAR_LEN 64 /* Collect available variables in this scope */ static int collect_variables_cb(Dwarf_Die *die_mem, void *data) { struct available_var_finder *af = data; struct variable_list *vl; char buf[MAX_VAR_LEN]; int tag, ret; vl = &af->vls[af->nvls - 1]; tag = dwarf_tag(die_mem); if (tag == DW_TAG_formal_parameter || tag == DW_TAG_variable) { ret = convert_variable_location(die_mem, af->pf.addr, af->pf.fb_ops, NULL); if (ret == 0) { ret = die_get_varname(die_mem, buf, MAX_VAR_LEN); pr_debug2("Add new var: %s\n", buf); if (ret > 0) strlist__add(vl->vars, buf); } } if (af->child && dwarf_haspc(die_mem, af->pf.addr)) return DIE_FIND_CB_CONTINUE; else return DIE_FIND_CB_SIBLING; } /* Add a found vars into available variables list */ static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf) { struct available_var_finder *af = container_of(pf, struct available_var_finder, pf); struct variable_list *vl; Dwarf_Die die_mem, *scopes = NULL; int ret, nscopes; /* Check number of tevs */ if (af->nvls == af->max_vls) { pr_warning("Too many( > %d) probe point found.\n", af->max_vls); return -ERANGE; } vl = &af->vls[af->nvls++]; ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe, &vl->point); if (ret < 0) return ret; pr_debug("Probe point found: %s+%lu\n", vl->point.symbol, vl->point.offset); /* Find local variables */ vl->vars = strlist__new(true, NULL); if (vl->vars == NULL) return -ENOMEM; af->child = true; die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem); /* Find external variables */ if (!af->externs) goto out; /* Don't need to search child DIE for externs. */ af->child = false; nscopes = dwarf_getscopes_die(sp_die, &scopes); while (nscopes-- > 1) die_find_child(&scopes[nscopes], collect_variables_cb, (void *)af, &die_mem); if (scopes) free(scopes); out: if (strlist__empty(vl->vars)) { strlist__delete(vl->vars); vl->vars = NULL; } return ret; } /* Find available variables at given probe point */ int find_available_vars_at(int fd, struct perf_probe_event *pev, struct variable_list **vls, int max_vls, bool externs) { struct available_var_finder af = { .pf = {.pev = pev, .callback = add_available_vars}, .max_vls = max_vls, .externs = externs}; int ret; /* Allocate result vls array */ *vls = zalloc(sizeof(struct variable_list) * max_vls); if (*vls == NULL) return -ENOMEM; af.vls = *vls; af.nvls = 0; ret = find_probes(fd, &af.pf); if (ret < 0) { /* Free vlist for error */ while (af.nvls--) { if (af.vls[af.nvls].point.symbol) free(af.vls[af.nvls].point.symbol); if (af.vls[af.nvls].vars) strlist__delete(af.vls[af.nvls].vars); } free(af.vls); *vls = NULL; return ret; } return (ret < 0) ? ret : af.nvls; }