perf, tools: Support srccode output
PATCH 1/1 perf, tools: Support srccode output
From: Andi Kleen <[email protected]>
When looking at PT or brstackinsn traces with perf script it can be very useful to see the source code. This adds a simple facility to print them with perf script, if the information is available through dwarf
% perf record ...
% perf script -F insn,ip,sym,srccode
...
4004c6 main
5 for (i = 0; i < 10000000; i++)
4004cd main
5 for (i = 0; i < 10000000; i++)
4004c6 main
5 for (i = 0; i < 10000000; i++)
4004cd main
5 for (i = 0; i < 10000000; i++)
4004cd main
5 for (i = 0; i < 10000000; i++)
4004cd main
5 for (i = 0; i < 10000000; i++)
4004cd main
5 for (i = 0; i < 10000000; i++)
4004cd main
5 for (i = 0; i < 10000000; i++)
4004b3 main6 v++;
% perf record -b ...
% perf script -F insn,ip,sym,srccode,brstackinsn
...
main+22:
0000000000400543 insn: e8 ca ff ff ff # PRED
|18 f1();
f1:
0000000000400512 insn: 55
|10 {
0000000000400513 insn: 48 89 e5
0000000000400516 insn: b8 00 00 00 00
|11 f2();
000000000040051b insn: e8 d6 ff ff ff # PRED
f2:
00000000004004f6 insn: 55
|5 {
00000000004004f7 insn: 48 89 e5
00000000004004fa insn: 8b 05 2c 0b 20 00
|6 c = a / b;
0000000000400500 insn: 8b 0d 2a 0b 20 00
0000000000400506 insn: 99
0000000000400507 insn: f7 f9
0000000000400509 insn: 89 05 29 0b 20 00
000000000040050f insn: 90
|7 }
0000000000400510 insn: 5d
0000000000400511 insn: c3 # PRED
f1+14:
0000000000400520 insn: b8 00 00 00 00
|12 f2();
0000000000400525 insn: e8 cc ff ff ff # PRED
f2:
00000000004004f6 insn: 55
|5 {
00000000004004f7 insn: 48 89 e5
00000000004004fa insn: 8b 05 2c 0b 20 00|6 c = a / b;
Not supported for callchains currently, would need some layout changes there.
v2:
Limit number of cached source files
Delete bogus comment.
Directly use the line number / file from the bfd lookup code Add prefix to source to make it easier to handle for scripts Avoid redundant listing of lines
Fix include guard in srccode.h
Use a hash table for the source lookup.
--- tools/perf/Documentation/perf-script.txt | 3 +- tools/perf/builtin-script.c | 40 ++++++- tools/perf/util/Build | 1 + tools/perf/util/evsel_fprintf.c | 1 + tools/perf/util/map.c | 52 +++++++++ tools/perf/util/map.h | 16 +++ tools/perf/util/srccode.c | 186 +++++++++++++++++++++++++++++++ tools/perf/util/srccode.h | 7 ++ tools/perf/util/srcline.c | 29 +++++ tools/perf/util/srcline.h | 2 + tools/perf/util/thread.c | 2 + tools/perf/util/thread.h | 2 + 12 files changed, 339 insertions(+), 2 deletions(-) create mode 100644 tools/perf/util/srccode.c create mode 100644 tools/perf/util/srccode.h
tools/perf/Documentation/perf-script.txt +2 -1
@@ -117,7 +117,8 @@ OPTIONS Comma separated list of fields to print. Options are: comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff, srcline, period, iregs, brstack, brstacksym, flags, bpf-output, brstackinsn, - callindent, insn, insnlen. Field list can be prepended with the type, trace, sw or hw, + callindent, insn, insnlen, srccode. + Field list can be prepended with the type, trace, sw or hw, to indicate to which event type the field list applies. e.g., -F sw:comm,tid,time,ip,sym and -F trace:time,cpu,trace
tools/perf/builtin-script.c +39 -1
@@ -85,6 +85,7 @@ enum perf_output_field {PERF_OUTPUT_INSN =1U <<21 ,PERF_OUTPUT_INSNLEN =1U <<22 ,PERF_OUTPUT_BRSTACKINSN =1U <<23 , + PERF_OUTPUT_SRCCODE = 1U << 24, }; struct output_option {
@@ -115,6 +116,7 @@ struct output_option { {.str = "insn", .field = PERF_OUTPUT_INSN}, {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN}, {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN}, + {.str = "srccode", .field = PERF_OUTPUT_SRCCODE}, };/* default set to maintain compatibility with current format */
@@ -304,7 +306,7 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel, "to DSO.\n");return -EINVAL ; } -if (PRINT_FIELD (SRCLINE ) && !PRINT_FIELD (IP )) { +if ((PRINT_FIELD (SRCLINE ) ||PRINT_FIELD (SRCCODE )) && !PRINT_FIELD (IP )) {pr_err ("Display of source line number requested but sample IP is not\n" "selected. Hence, no address to lookup the source line number.\n" );return -EINVAL ;
@@ -633,6 +635,20 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,return len; } +static void print_srccode (struct thread *thread,u8 cpumode,uint64_t addr) +{ +struct addr_location al; +memset (&al,0 ,sizeof (al)); + +thread__find_addr_map (thread, cpumode,MAP__FUNCTION , addr, &al); +if (!al.map) +thread__find_addr_map (thread, cpumode,MAP__VARIABLE , + addr, &al); +if (al.map &&map__fprintf_srccode (al.map, al.addr,"" , stdout, + &thread->srccode_state)) +printf ("\n" ); +} +static void print_jump (uint64_t ip,struct branch_entry *en,struct perf_insn *x,u8 *inbuf,int len,int insn)
@@ -723,6 +739,8 @@ static void print_sample_brstackinsn(struct perf_sample *sample, br->entries[nr - 1].from, &lastsym, attr);print_jump (br->entries[nr -1 ].from, &br->entries[nr -1 ], &x, buffer, len,0 ); +if (PRINT_FIELD (SRCCODE )) +print_srccode (thread, x.cpumode, br->entries[nr -1 ].from); }/* Print all blocks */
@@ -751,12 +769,16 @@ static void print_sample_brstackinsn(struct perf_sample *sample,print_ip_sym (thread, x.cpumode, x.cpu, ip, &lastsym, attr);if (ip == end) {print_jump (ip, &br->entries[i], &x, buffer + off, len - off, insn); +if (PRINT_FIELD (SRCCODE )) +print_srccode (thread, x.cpumode, ip);break ; }else {printf ("\t%016" PRIx64"\t%s\n" , ip,dump_insn (&x, ip, buffer + off, len - off, &ilen));if (ilen ==0 )break ; +if (PRINT_FIELD (SRCCODE )) +print_srccode (thread, x.cpumode, ip); insn++; } }
@@ -787,6 +809,8 @@ static void print_sample_brstackinsn(struct perf_sample *sample,printf ("\t%016" PRIx64"\t%s\n" , sample->ip,dump_insn (&x, sample->ip, buffer, len,NULL )); +if (PRINT_FIELD (SRCCODE )) +print_srccode (thread, x.cpumode, sample->ip);return ; }for (off =0 ; off <= end - start; off += ilen) {
@@ -794,6 +818,8 @@ static void print_sample_brstackinsn(struct perf_sample *sample, dump_insn(&x, start + off, buffer + off, len - off, &ilen));if (ilen ==0 )break ; +if (PRINT_FIELD (SRCCODE )) +print_srccode (thread, x.cpumode, start + off); } }
@@ -947,6 +973,12 @@ static void print_sample_bts(struct perf_sample *sample,print_insn (sample, attr, thread, machine);printf ("\n" ); + +if (PRINT_FIELD (SRCCODE )) { +if (map__fprintf_srccode (al->map, al->addr,"" , stdout, + &thread->srccode_state)) +printf ("\n" ); + } } static struct {
@@ -1195,6 +1227,12 @@ static void process_event(struct perf_script *script,print_sample_bpf_output (sample);print_insn (sample, attr, thread, machine);printf ("\n" ); + +if (PRINT_FIELD (SRCCODE )) { +if (map__fprintf_srccode (al->map, al->addr,"" , stdout, + &thread->srccode_state)) +printf ("\n" ); + } }static struct scripting_ops *scripting_ops;
tools/perf/util/Build +1 -0
@@ -72,6 +72,7 @@ libperf-y += stat.o libperf-y += stat-shadow.o libperf-y += record.o libperf-y += srcline.o +libperf-y += srccode.o libperf-y += data.o libperf-y += tsc.o libperf-y += cloexec.o
tools/perf/util/evsel_fprintf.c +1 -0
@@ -168,6 +168,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,if (!print_oneline) printed +=fprintf (fp,"\n" ); +/* Add srccode here too? */ if (symbol_conf.bt_stop_list && node->sym && strlist__has_entry(symbol_conf.bt_stop_list,
tools/perf/util/map.c +52 -0
64 lines not on this page · raw message
tools/perf/util/map.h +16 -0
@@ -170,6 +170,22 @@ size_t map__fprintf_dsoname(struct map *map, FILE *fp);int map__fprintf_srcline (struct map *map,u64 addr,const char *prefix,FILE *fp); +struct srccode_state { +char *srcfile; +unsigned line; +}; + +static inline void srccode_state_init (struct srccode_state *state) +{ + state->srcfile =NULL ; + state->line =0 ; +} + +void srccode_state_free (struct srccode_state *state); + +int map__fprintf_srccode (struct map *map,u64 addr,const char *prefix, +FILE *fp,struct srccode_state *state); +int map__load (struct map *map);struct symbol *map__find_symbol (struct map *map,u64 addr);struct symbol *map__find_symbol_by_name (struct map *map,const char *name);
tools/perf/util/srccode.c new file +186 -0
186 lines not on this page · raw message
tools/perf/util/srccode.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef SRCCODE_H +#define SRCCODE_H 1 + +/* Result is not 0 terminated */ +char *find_sourceline (char *fn,unsigned line,int *lenp); + +#endif
tools/perf/util/srcline.c +29 -0
@@ -479,6 +479,35 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,return srcline; } +/* Returns filename and fills in line number in line */ +char *get_srcline_split (struct dso *dso,u64 addr, +bool unwind_inlines,unsigned *line) +{ +char *file =NULL ; +const char *dso_name; + +if (!dso->has_srcline) + goto out; + + dso_name =dso__name (dso); +if (dso_name ==NULL ) + goto out; + +if (!addr2line (dso_name, addr, &file, line, dso, unwind_inlines,NULL )) + goto out; + + dso->a2l_fails =0 ; +return file; + +out: +if (dso->a2l_fails && ++dso->a2l_fails >A2L_FAIL_LIMIT ) { + dso->has_srcline =0 ; +dso__free_a2l (dso); + } + +return NULL ; +} +void free_srcline (char *srcline) {if (srcline &&strcmp (srcline,SRCLINE_UNKNOWN ) !=0 )
tools/perf/util/srcline.h +2 -0
@@ -13,6 +13,8 @@ char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,char *__get_srcline (struct dso *dso,u64 addr,struct symbol *sym,bool show_sym,bool show_addr,bool unwind_inlines);void free_srcline (char *srcline); +char *get_srcline_split (struct dso *dso,u64 addr, +bool unwind_inlines,unsigned *line);#define SRCLINE_UNKNOWN ((char *) "??:0")
tools/perf/util/thread.c +2 -0
@@ -59,6 +59,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)list_add (&comm->list, &thread->comm_list);refcount_set (&thread->refcnt,1 );RB_CLEAR_NODE (&thread->rb_node); +srccode_state_init (&thread->srccode_state); }return thread;
@@ -91,6 +92,7 @@ void thread__delete(struct thread *thread)comm__free (comm); }unwind__finish_access (thread); +srccode_state_free (&thread->srccode_state);free (thread); }
tools/perf/util/thread.h +2 -0
@@ -7,6 +7,7 @@#include <unistd.h> #include <sys/types.h> #include "symbol.h" +#include "map.h" #include <strlist.h> #include <intlist.h>
@@ -34,6 +35,7 @@ struct thread {void *priv;struct thread_stack *ts; +struct srccode_state srccode_state;#ifdef HAVE_LIBUNWIND_SUPPORT void *addr_space;struct unwind_libunwind_ops *unwind_libunwind_ops;
Signature · 1 line
2.9.3
On Mon, May 08, 2017 at 01:22:05PM -0700, Andi Kleen wrote:
SNIP
Quoted 13 lines
tools/perf/util/map.c +1
-0@@ -17,6 +17,7 @@#include <linux/string.h> #include "srcline.h" #include "unwind.h" +#include "srccode.h" static void __maps__insert (struct maps *maps,struct map *map);
tools/perf/util/map.c int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, · line 421
Quoted 7 lines
tools/perf/util/map.c +4
-0@@ -414,6 +415,57 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,return ret; } +int map__fprintf_srccode (struct map *map,u64 addr, +const char *prefix,FILE *fp, +struct srccode_state *state) +{
what's the 'prefix' for? it's always passed as ""
jirka
On Mon, May 08, 2017 at 01:22:05PM -0700, Andi Kleen wrote:
SNIP
tools/perf/util/srcline.c char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, · line 484
Quoted 11 lines
tools/perf/util/srcline.c +3
-0@@ -479,6 +479,35 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,return srcline; } +/* Returns filename and fills in line number in line */ +char *get_srcline_split (struct dso *dso,u64 addr, +bool unwind_inlines,unsigned *line)
no need to pass unwind_inlines as arg if it's always true
jirka