Skip to content

Commit a64489c

Browse files
Jin Yaoacmel
Jin Yao
authored andcommitted
perf report: Find the inline stack for a given address
It would be useful for perf to support a mode to query the inline stack for a given callgraph address. This would simplify finding the right code in code that does a lot of inlining. The srcline.c has contained the code which supports to translate the address to filename:line_nr. This patch just extends the function to let it support getting the inline stacks. It introduces the inline_list which will store the inline function result (filename:line_nr and funcname). If BFD lib is not supported, the result is only filename:line_nr. Signed-off-by: Yao Jin <[email protected]> Tested-by: Milian Wolff <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 5580338 commit a64489c

File tree

5 files changed

+192
-5
lines changed

5 files changed

+192
-5
lines changed

tools/perf/util/srcline.c

+162-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "util/dso.h"
88
#include "util/util.h"
99
#include "util/debug.h"
10+
#include "util/callchain.h"
1011

1112
#include "symbol.h"
1213

@@ -30,6 +31,34 @@ static const char *dso__name(struct dso *dso)
3031
return dso_name;
3132
}
3233

34+
static int inline_list__append(char *filename, char *funcname, int line_nr,
35+
struct inline_node *node, struct dso *dso)
36+
{
37+
struct inline_list *ilist;
38+
char *demangled;
39+
40+
ilist = zalloc(sizeof(*ilist));
41+
if (ilist == NULL)
42+
return -1;
43+
44+
ilist->filename = filename;
45+
ilist->line_nr = line_nr;
46+
47+
if (dso != NULL) {
48+
demangled = dso__demangle_sym(dso, 0, funcname);
49+
if (demangled == NULL) {
50+
ilist->funcname = funcname;
51+
} else {
52+
ilist->funcname = demangled;
53+
free(funcname);
54+
}
55+
}
56+
57+
list_add_tail(&ilist->list, &node->val);
58+
59+
return 0;
60+
}
61+
3362
#ifdef HAVE_LIBBFD_SUPPORT
3463

3564
/*
@@ -169,9 +198,17 @@ static void addr2line_cleanup(struct a2l_data *a2l)
169198

170199
#define MAX_INLINE_NEST 1024
171200

201+
static void inline_list__reverse(struct inline_node *node)
202+
{
203+
struct inline_list *ilist, *n;
204+
205+
list_for_each_entry_safe_reverse(ilist, n, &node->val, list)
206+
list_move_tail(&ilist->list, &node->val);
207+
}
208+
172209
static int addr2line(const char *dso_name, u64 addr,
173210
char **file, unsigned int *line, struct dso *dso,
174-
bool unwind_inlines)
211+
bool unwind_inlines, struct inline_node *node)
175212
{
176213
int ret = 0;
177214
struct a2l_data *a2l = dso->a2l;
@@ -196,8 +233,21 @@ static int addr2line(const char *dso_name, u64 addr,
196233

197234
while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
198235
&a2l->funcname, &a2l->line) &&
199-
cnt++ < MAX_INLINE_NEST)
200-
;
236+
cnt++ < MAX_INLINE_NEST) {
237+
238+
if (node != NULL) {
239+
if (inline_list__append(strdup(a2l->filename),
240+
strdup(a2l->funcname),
241+
a2l->line, node,
242+
dso) != 0)
243+
return 0;
244+
}
245+
}
246+
247+
if ((node != NULL) &&
248+
(callchain_param.order != ORDER_CALLEE)) {
249+
inline_list__reverse(node);
250+
}
201251
}
202252

203253
if (a2l->found && a2l->filename) {
@@ -223,6 +273,35 @@ void dso__free_a2l(struct dso *dso)
223273
dso->a2l = NULL;
224274
}
225275

276+
static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
277+
struct dso *dso)
278+
{
279+
char *file = NULL;
280+
unsigned int line = 0;
281+
struct inline_node *node;
282+
283+
node = zalloc(sizeof(*node));
284+
if (node == NULL) {
285+
perror("not enough memory for the inline node");
286+
return NULL;
287+
}
288+
289+
INIT_LIST_HEAD(&node->val);
290+
node->addr = addr;
291+
292+
if (!addr2line(dso_name, addr, &file, &line, dso, TRUE, node))
293+
goto out_free_inline_node;
294+
295+
if (list_empty(&node->val))
296+
goto out_free_inline_node;
297+
298+
return node;
299+
300+
out_free_inline_node:
301+
inline_node__delete(node);
302+
return NULL;
303+
}
304+
226305
#else /* HAVE_LIBBFD_SUPPORT */
227306

228307
static int filename_split(char *filename, unsigned int *line_nr)
@@ -249,7 +328,8 @@ static int filename_split(char *filename, unsigned int *line_nr)
249328
static int addr2line(const char *dso_name, u64 addr,
250329
char **file, unsigned int *line_nr,
251330
struct dso *dso __maybe_unused,
252-
bool unwind_inlines __maybe_unused)
331+
bool unwind_inlines __maybe_unused,
332+
struct inline_node *node __maybe_unused)
253333
{
254334
FILE *fp;
255335
char cmd[PATH_MAX];
@@ -288,6 +368,58 @@ void dso__free_a2l(struct dso *dso __maybe_unused)
288368
{
289369
}
290370

371+
static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
372+
struct dso *dso __maybe_unused)
373+
{
374+
FILE *fp;
375+
char cmd[PATH_MAX];
376+
struct inline_node *node;
377+
char *filename = NULL;
378+
size_t len;
379+
unsigned int line_nr = 0;
380+
381+
scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
382+
dso_name, addr);
383+
384+
fp = popen(cmd, "r");
385+
if (fp == NULL) {
386+
pr_err("popen failed for %s\n", dso_name);
387+
return NULL;
388+
}
389+
390+
node = zalloc(sizeof(*node));
391+
if (node == NULL) {
392+
perror("not enough memory for the inline node");
393+
goto out;
394+
}
395+
396+
INIT_LIST_HEAD(&node->val);
397+
node->addr = addr;
398+
399+
while (getline(&filename, &len, fp) != -1) {
400+
if (filename_split(filename, &line_nr) != 1) {
401+
free(filename);
402+
goto out;
403+
}
404+
405+
if (inline_list__append(filename, NULL, line_nr, node,
406+
NULL) != 0)
407+
goto out;
408+
409+
filename = NULL;
410+
}
411+
412+
out:
413+
pclose(fp);
414+
415+
if (list_empty(&node->val)) {
416+
inline_node__delete(node);
417+
return NULL;
418+
}
419+
420+
return node;
421+
}
422+
291423
#endif /* HAVE_LIBBFD_SUPPORT */
292424

293425
/*
@@ -311,7 +443,7 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
311443
if (dso_name == NULL)
312444
goto out;
313445

314-
if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines))
446+
if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
315447
goto out;
316448

317449
if (asprintf(&srcline, "%s:%u",
@@ -351,3 +483,28 @@ char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
351483
{
352484
return __get_srcline(dso, addr, sym, show_sym, false);
353485
}
486+
487+
struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
488+
{
489+
const char *dso_name;
490+
491+
dso_name = dso__name(dso);
492+
if (dso_name == NULL)
493+
return NULL;
494+
495+
return addr2inlines(dso_name, addr, dso);
496+
}
497+
498+
void inline_node__delete(struct inline_node *node)
499+
{
500+
struct inline_list *ilist, *tmp;
501+
502+
list_for_each_entry_safe(ilist, tmp, &node->val, list) {
503+
list_del_init(&ilist->list);
504+
zfree(&ilist->filename);
505+
zfree(&ilist->funcname);
506+
free(ilist);
507+
}
508+
509+
free(node);
510+
}

tools/perf/util/symbol-elf.c

+5
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *
390390
return 0;
391391
}
392392

393+
char *dso__demangle_sym(struct dso *dso, int kmodule, char *elf_name)
394+
{
395+
return demangle_sym(dso, kmodule, elf_name);
396+
}
397+
393398
/*
394399
* Align offset to 4 bytes as needed for note name and descriptor data.
395400
*/

tools/perf/util/symbol-minimal.c

+7
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,10 @@ int kcore_copy(const char *from_dir __maybe_unused,
373373
void symbol__elf_init(void)
374374
{
375375
}
376+
377+
char *dso__demangle_sym(struct dso *dso __maybe_unused,
378+
int kmodule __maybe_unused,
379+
char *elf_name __maybe_unused)
380+
{
381+
return NULL;
382+
}

tools/perf/util/symbol.h

+2
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
305305
int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
306306
struct map *map);
307307

308+
char *dso__demangle_sym(struct dso *dso, int kmodule, char *elf_name);
309+
308310
void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel);
309311
void symbols__insert(struct rb_root *symbols, struct symbol *sym);
310312
void symbols__fixup_duplicate(struct rb_root *symbols);

tools/perf/util/util.h

+16
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,20 @@ int is_printable_array(char *p, unsigned int len);
364364
int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
365365

366366
int unit_number__scnprintf(char *buf, size_t size, u64 n);
367+
368+
struct inline_list {
369+
char *filename;
370+
char *funcname;
371+
unsigned int line_nr;
372+
struct list_head list;
373+
};
374+
375+
struct inline_node {
376+
u64 addr;
377+
struct list_head val;
378+
};
379+
380+
struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr);
381+
void inline_node__delete(struct inline_node *node);
382+
367383
#endif /* GIT_COMPAT_UTIL_H */

0 commit comments

Comments
 (0)