Hi,
This patch series provides the commands 'perf bts record' and 'perf bts trace'.
These commands can record and analyze BTS (Branch Trace Store) log.
And, provide the interface to use BTS log for application developers.
BTS is a facility of Intel x86 processors, which can record the address of
'branch to/from' on every branch/jump instruction and interrupt.
This facility is very useful for developers to test their software.
For example, coverage test, execution path analysis, dynamic step count ...etc.
Also, the tools have a very big advantage that they will require no changes to
the target executable binaries.
But, there are few applications using BTS. Reasons I guess are ...
- Few people know what BTS is.
- Few people know how to use BTS on Linux box.
- It's hard to analyze the BTS log because it includes just a series of addresses.
So, I want to provide a user-friendly interface to BTS for application developers.
About new sub commands
========================
'perf bts record' provides the easy way to record bts log.
Usage is 'perf bts record <command>'. This command is just an alias to
'perf record -e branches:u -c 1 <command>'. But, new one is more simple and
more intuitive.
'perf bts trace' can parse and analyze recorded bts log and print various
information of execution path. This command can show address, pid, command name,
function+offset, file path of elf.
You can choose the printed information with option.
Example: 'perf bts trace'
function+offset
irq_return+0x0 => _start+0x0
irq_return+0x0 => _start+0x0
_start+0x3 => _dl_start+0x0
irq_return+0x0 => _dl_start+0x0
irq_return+0x0 => _dl_start+0x26
irq_return+0x0 => _dl_start+0x2d
_dl_start+0x71 => _dl_start+0x93
_dl_start+0x97 => _dl_start+0x78
...
This is the default behavior of 'perf bts trace'. It prints function+offset.
Example2: 'perf bts -cas trace'
command address function+offset
ls 0xffffffff8146fe0e irq_return+0x0 => ls 0x0000003806200b20 _start+0x0
ls 0xffffffff8146fe0e irq_return+0x0 => ls 0x0000003806200b20 _start+0x0
ls 0x0000003806200b23 _start+0x3 => ls 0x0000003806204910 _dl_start+0x0
ls 0xffffffff8146fe0e irq_return+0x0 => ls 0x0000003806204910 _dl_start+0x0
ls 0xffffffff8146fe0e irq_return+0x0 => ls 0x0000003806204936 _dl_start+0x26
ls 0xffffffff8146fe0e irq_return+0x0 => ls 0x000000380620493d _dl_start+0x2d
ls 0x0000003806204981 _dl_start+0x71 => ls 0x00000038062049a3 _dl_start+0x93
ls 0x00000038062049a7 _dl_start+0x97 => ls 0x0000003806204988 _dl_start+0x78
...
In the future, I'd like to make this more informative. For example
- source file path
- line number
- inlined function name
and more!
Thanks,
---
Akihiro Nagai (7):
perf bts trace: add print all option
perf bts trace: print function+offset
perf bts trace: print file path of the executed elf
perf bts trace: print pid and command
perf bts: Introduce new sub command 'perf bts trace'
perf: Introduce perf sub command 'bts record'
perf: add OPT_CALLBACK_DEFAULT_NOOPT
tools/perf/Documentation/perf-bts.txt | 53 ++++++
tools/perf/Makefile | 1
tools/perf/builtin-bts.c | 304 +++++++++++++++++++++++++++++++++
tools/perf/builtin.h | 1
tools/perf/perf.c | 1
tools/perf/util/parse-options.h | 4
6 files changed, 364 insertions(+), 0 deletions(-)
create mode 100644 tools/perf/Documentation/perf-bts.txt
create mode 100644 tools/perf/builtin-bts.c
--
Akihiro Nagai ([email protected])
Add new macro OPT_CALLBACK_DEFAULT_NOOPT for parse_options.
It enables to pass the default value (opt->defval) to the callback function
processing options require no argument.
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/util/parse-options.h | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index c7d72dc..abc31a1 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -119,6 +119,10 @@ struct option {
{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT }
+#define OPT_CALLBACK_DEFAULT_NOOPT(s, l, v, a, h, f, d) \
+ { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
+ .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d,\
+ .flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOARG}
/* parse_options() will filter out the processed options and leave the
* non-option argments in argv[].
Introduce the easy way to record bts log, 'perf bts record'.
This command can record the bts log while specified command is executing,
and save to the file "perf.data"
Usage:
perf bts record <tracee command>
Example:
# perf bts record ls -l
(ls -l outputs)
# perf record: Captured and wrote 4.381 MB perf.data (~191405 samples)
# ls
perf.data
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/Documentation/perf-bts.txt | 24 +++++++++++++
tools/perf/Makefile | 1 +
tools/perf/builtin-bts.c | 62 +++++++++++++++++++++++++++++++++
tools/perf/builtin.h | 1 +
tools/perf/perf.c | 1 +
5 files changed, 89 insertions(+), 0 deletions(-)
create mode 100644 tools/perf/Documentation/perf-bts.txt
create mode 100644 tools/perf/builtin-bts.c
diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
new file mode 100644
index 0000000..55a2fe6
--- /dev/null
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -0,0 +1,24 @@
+perf-bts(1)
+==============
+
+NAME
+----
+perf-bts - Record branch-trace-store log
+
+SYNOPSIS
+--------
+[verse]
+'perf bts' record <command>
+
+DESCRIPTION
+-----------
+This command can record branch-trace-store log.
+Branch-trace-store is a facility of processors. It can record
+address of branch to/from on every branch instruction and interrupt.
+
+'perf bts record <command>' records branch-trace-store log while specified
+command is executing. And, save to the file "perf.data".
+
+SEE ALSO
+--------
+linkperf:perf-record[1]
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index b3e6bc6..14de491 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -503,6 +503,7 @@ BUILTIN_OBJS += $(OUTPUT)builtin-kvm.o
BUILTIN_OBJS += $(OUTPUT)builtin-test.o
BUILTIN_OBJS += $(OUTPUT)builtin-inject.o
BUILTIN_OBJS += $(OUTPUT)builtin-trace.o
+BUILTIN_OBJS += $(OUTPUT)builtin-bts.o
PERFLIBS = $(LIB_FILE)
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
new file mode 100644
index 0000000..587cfad
--- /dev/null
+++ b/tools/perf/builtin-bts.c
@@ -0,0 +1,62 @@
+#include "builtin.h"
+#include "perf.h"
+#include "util/parse-options.h"
+
+static const char * const bts_usage[] = {
+ "perf bts record <command>",
+ NULL,
+};
+
+/* arguments to call 'perf record' */
+static const char * const record_args[] = {
+ "record",
+ "-f",
+ "-e", "branches:u",
+ "-c", "1",
+ "-d",
+};
+
+/* dummy struct option to call parse_options() */
+static const struct option bts_options[] = {
+ OPT_END()
+};
+
+static int __cmd_record(int argc, const char **argv)
+{
+ unsigned int rec_argc, i, j;
+ const char **rec_argv;
+ int rc;
+
+ /* prepare the arguments list to call 'perf record' */
+ rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+ rec_argv = calloc(rec_argc + 1, sizeof(char *));
+
+ for (i = 0; i < ARRAY_SIZE(record_args); i++)
+ rec_argv[i] = record_args[i];
+
+ for (j = 1; j < (unsigned int)argc; j++, i++)
+ rec_argv[i] = argv[j];
+
+ BUG_ON(i != rec_argc);
+
+ /* call 'perf record' */
+ rc = cmd_record(i, rec_argv, NULL);
+
+ free(rec_argv);
+ return rc;
+}
+
+int cmd_bts(int argc, const char **argv, const char *prefix __used)
+{
+ argc = parse_options(argc, argv, bts_options, bts_usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+ if (!argc)
+ usage_with_options(bts_usage, bts_options);
+
+ if (!strncmp(argv[0], "record", 6))
+ return __cmd_record(argc, argv);
+ else
+ usage_with_options(bts_usage, bts_options);
+
+ return 0;
+}
diff --git a/tools/perf/builtin.h b/tools/perf/builtin.h
index 86a93a1..9ab6430 100644
--- a/tools/perf/builtin.h
+++ b/tools/perf/builtin.h
@@ -36,5 +36,6 @@ extern int cmd_kvm(int argc, const char **argv, const char *prefix);
extern int cmd_test(int argc, const char **argv, const char *prefix);
extern int cmd_inject(int argc, const char **argv, const char *prefix);
extern int cmd_trace(int argc, const char **argv, const char *prefix);
+extern int cmd_bts(int argc, const char **argv, const char *prefix);
#endif
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 0f7bb95..5b16182 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -332,6 +332,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "test", cmd_test, 0 },
{ "inject", cmd_inject, 0 },
{ "trace", cmd_trace, 0 },
+ { "bts", cmd_bts, 0 },
};
unsigned int i;
static const char ext[] = STRIP_EXTENSION;
Introduce new sub command 'perf bts trace'.
This command can parse and print bts log recorded by
'perf bts record'.
Usage:
- First, record the bts log 'perf bts record <command>'
- Second, parse and print bts log 'perf bts trace'
Output:
0xffffffff8146fe0e => 0x0000003806200b20
0x0000003806200b23 => 0x0000003806204910
0xffffffff8146fe0e => 0x0000003806204910
0xffffffff8146fe0e => 0x0000003806204936
0xffffffff8146fe0e => 0x000000380620493d
0x0000003806204981 => 0x00000038062049a3
0x00000038062049a7 => 0x0000003806204988
...
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/Documentation/perf-bts.txt | 14 ++++++--
tools/perf/builtin-bts.c | 59 +++++++++++++++++++++++++++++++--
2 files changed, 67 insertions(+), 6 deletions(-)
diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
index 55a2fe6..5920dcc 100644
--- a/tools/perf/Documentation/perf-bts.txt
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -3,22 +3,30 @@ perf-bts(1)
NAME
----
-perf-bts - Record branch-trace-store log
+perf-bts - Record and print branch-trace-store log
SYNOPSIS
--------
[verse]
-'perf bts' record <command>
+'perf bts' [<options>] {record|trace}
DESCRIPTION
-----------
-This command can record branch-trace-store log.
+This command can record and print branch-trace-store log.
Branch-trace-store is a facility of processors. It can record
address of branch to/from on every branch instruction and interrupt.
'perf bts record <command>' records branch-trace-store log while specified
command is executing. And, save to the file "perf.data".
+'perf bts trace' parses recorded branch-trace-store log and prints it.
+
+OPTIONS
+-------
+-i::
+--input=::
+ Specify input file name to analyze.
+
SEE ALSO
--------
linkperf:perf-record[1]
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
index 587cfad..54e83af 100644
--- a/tools/perf/builtin-bts.c
+++ b/tools/perf/builtin-bts.c
@@ -1,10 +1,25 @@
#include "builtin.h"
#include "perf.h"
#include "util/parse-options.h"
+#include "util/session.h"
+#include "util/cache.h"
+#include <inttypes.h>
+
+/* format string of specifying min width to print address */
+#if __WORDSIZE == 32
+#define FMT_ADDR_WIDTH "10" /* length of "0x" + 32bit address */
+#else
+#define FMT_ADDR_WIDTH "18" /* length of "0x" + 64bit address */
+#endif
+/* format string to print address */
+#define FMT_ADDR "%#0" FMT_ADDR_WIDTH "llx"
+
+/* default input file name to analyze */
+static const char *input_name = "perf.data";
static const char * const bts_usage[] = {
- "perf bts record <command>",
- NULL,
+ "perf bts [<options>] {record|trace}",
+ NULL
};
/* arguments to call 'perf record' */
@@ -16,11 +31,45 @@ static const char * const record_args[] = {
"-d",
};
-/* dummy struct option to call parse_options() */
static const struct option bts_options[] = {
+ OPT_STRING('i', "input", &input_name, "file", "input file name"),
OPT_END()
};
+static int process_sample_event(event_t *event, struct perf_session *session)
+{
+ struct sample_data data;
+
+ memset(&data, 0, sizeof(data));
+ event__parse_sample(event, session->sample_type, &data);
+
+ /* data.ip is 'from address', data.addr is 'to address' */
+ printf(FMT_ADDR " => " FMT_ADDR "\n", data.ip, data.addr);
+
+ return 0;
+}
+
+static struct perf_event_ops event_ops = {
+ .sample = process_sample_event,
+ .ordered_samples = false,
+};
+
+static int __cmd_trace(void)
+{
+ struct perf_session *session;
+
+ session = perf_session__new(input_name, O_RDONLY, 0, false);
+ if (!session) {
+ fprintf(stderr, "failed to create perf_session.\n");
+ return -ENOMEM;
+ }
+
+ perf_session__process_events(session, &event_ops);
+ perf_session__delete(session);
+
+ return 0;
+}
+
static int __cmd_record(int argc, const char **argv)
{
unsigned int rec_argc, i, j;
@@ -55,6 +104,10 @@ int cmd_bts(int argc, const char **argv, const char *prefix __used)
if (!strncmp(argv[0], "record", 6))
return __cmd_record(argc, argv);
+ else if (!strncmp(argv[0], "trace", 5)) {
+ setup_pager();
+ return __cmd_trace();
+ }
else
usage_with_options(bts_usage, bts_options);
Provide the function of printing pid and command name to
'perf bts trace'. Users can select items to print with options.
For example,
'perf bts -p trace' prints only pid,
'perf bts -ac trace' prints address and comamnd name.
'perf bts trace' prints only address (default)
This is output sample (perf bts -ac trace):
command address
ls 0xffffffff8146fe0e => ls 0x0000003806200b20
ls 0xffffffff8146fe0e => ls 0x0000003806200b20
ls 0x0000003806200b23 => ls 0x0000003806204910
ls 0xffffffff8146fe0e => ls 0x0000003806204910
ls 0xffffffff8146fe0e => ls 0x0000003806204936
ls 0xffffffff8146fe0e => ls 0x000000380620493d
ls 0x0000003806204981 => ls 0x00000038062049a3
ls 0x00000038062049a7 => ls 0x0000003806204988
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/Documentation/perf-bts.txt | 18 ++++-
tools/perf/builtin-bts.c | 124 ++++++++++++++++++++++++++++++++-
2 files changed, 135 insertions(+), 7 deletions(-)
diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
index 5920dcc..56ddaa4 100644
--- a/tools/perf/Documentation/perf-bts.txt
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -3,7 +3,7 @@ perf-bts(1)
NAME
----
-perf-bts - Record and print branch-trace-store log
+perf-bts - Record and analyze branch-trace-store log
SYNOPSIS
--------
@@ -12,20 +12,32 @@ SYNOPSIS
DESCRIPTION
-----------
-This command can record and print branch-trace-store log.
+This command can record and analyze branch-trace-store log.
Branch-trace-store is a facility of processors. It can record
address of branch to/from on every branch instruction and interrupt.
'perf bts record <command>' records branch-trace-store log while specified
command is executing. And, save to the file "perf.data".
-'perf bts trace' parses recorded branch-trace-store log and prints it.
+'perf bts trace' analyzes recorded branch-trace-store log and prints it.
+The command can select the item to print. For example,
+ 'perf bts -a trace' : prints only address
+ 'perf bts -acp trace' : prints address, command name and pid
OPTIONS
-------
-i::
--input=::
Specify input file name to analyze.
+-a::
+--addr::
+ Print address. (default)
+-c::
+--comm::
+ Print command name.
+-p::
+--pid::
+ Print pid.
SEE ALSO
--------
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
index 54e83af..11e491b 100644
--- a/tools/perf/builtin-bts.c
+++ b/tools/perf/builtin-bts.c
@@ -3,6 +3,7 @@
#include "util/parse-options.h"
#include "util/session.h"
#include "util/cache.h"
+#include "util/trace-event.h"
#include <inttypes.h>
/* format string of specifying min width to print address */
@@ -14,6 +15,32 @@
/* format string to print address */
#define FMT_ADDR "%#0" FMT_ADDR_WIDTH "llx"
+/* printable items */
+struct exec_info {
+ u64 addr; /* recorded address by bts */
+ pid_t pid; /* tracee process pid */
+ const char *comm; /* command name */
+};
+
+#define EI_PID_UNSET -1
+
+/* flags which item print */
+#define EI_FLAG_PRINT_ADDR (1 << 0)
+#define EI_FLAG_PRINT_PID (1 << 1)
+#define EI_FLAG_PRINT_COMM (1 << 2)
+
+/* it's used when no print item specified */
+#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_ADDR
+
+/* print item flags */
+static unsigned long print_flags;
+
+#define is_flags_unset(flags) (flags == 0)
+
+/* print it when we cannnot analyze and get the information */
+#define EI_UNKNOWN_TEXT "(unknown)"
+#define EI_UNKNOWN_TEXT_LEN (sizeof(EI_UNKNOWN_TEXT))
+
/* default input file name to analyze */
static const char *input_name = "perf.data";
@@ -31,26 +58,111 @@ static const char * const record_args[] = {
"-d",
};
+/* set print flags call from parse_options() */
+static int set_print_flags(const struct option *opt, const char *str __unused,
+ int unset __unused)
+{
+ print_flags |= (unsigned long)opt->defval;
+ return 0;
+}
+
static const struct option bts_options[] = {
OPT_STRING('i', "input", &input_name, "file", "input file name"),
+ OPT_CALLBACK_DEFAULT_NOOPT('a', "addr", NULL, NULL,
+ "print address (default)", set_print_flags,
+ (void *)EI_FLAG_PRINT_ADDR),
+ OPT_CALLBACK_DEFAULT_NOOPT('p', "pid", NULL, NULL,
+ "print pid", set_print_flags,
+ (void *)EI_FLAG_PRINT_PID),
+ OPT_CALLBACK_DEFAULT_NOOPT('c', "comm", NULL, NULL,
+ "print command name", set_print_flags,
+ (void *)EI_FLAG_PRINT_COMM),
OPT_END()
};
+static void init_exec_info(struct exec_info *ei)
+{
+ memset(ei, 0, sizeof(*ei));
+ ei->pid = EI_PID_UNSET;
+}
+
+/* collect printable items to struct exec_info */
+static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
+ event_t *event, u64 addr)
+{
+ struct thread *thread;
+
+ ei->addr = addr;
+ ei->pid = event->ip.pid;
+
+ thread = perf_session__findnew(session, event->ip.pid);
+ if (!thread)
+ return;
+ ei->comm = thread->comm;
+}
+
+static void __print_exec_info(struct exec_info *ei)
+{
+ char pid[16];
+ const char *comm;
+
+ if (print_flags & EI_FLAG_PRINT_PID) {
+ if (ei->pid == EI_PID_UNSET)
+ strncpy(pid, EI_UNKNOWN_TEXT, EI_UNKNOWN_TEXT_LEN);
+ else
+ snprintf(pid, 16, "%d", ei->pid);
+ printf("%5s ", pid);
+ }
+ if (print_flags & EI_FLAG_PRINT_COMM) {
+ comm = ei->comm ? : EI_UNKNOWN_TEXT;
+ printf("%-12s ", comm);
+ }
+ if (print_flags & EI_FLAG_PRINT_ADDR)
+ printf(FMT_ADDR " ", ei->addr);
+}
+
+static void print_exec_info(struct exec_info *ei_from, struct exec_info *ei_to)
+{
+ __print_exec_info(ei_from);
+ printf("=> ");
+ __print_exec_info(ei_to);
+ printf("\n");
+}
+
+static void print_exec_info_header(void)
+{
+ if (print_flags & EI_FLAG_PRINT_PID)
+ printf("%5s ", "pid");
+ if (print_flags & EI_FLAG_PRINT_COMM)
+ printf("%-12s ", "command");
+ if (print_flags & EI_FLAG_PRINT_ADDR)
+ printf("%-" FMT_ADDR_WIDTH "s ", "address");
+ printf("\n");
+}
+
static int process_sample_event(event_t *event, struct perf_session *session)
{
struct sample_data data;
+ struct exec_info ei_from, ei_to;
memset(&data, 0, sizeof(data));
event__parse_sample(event, session->sample_type, &data);
+ init_exec_info(&ei_from);
+ init_exec_info(&ei_to);
+
/* data.ip is 'from address', data.addr is 'to address' */
- printf(FMT_ADDR " => " FMT_ADDR "\n", data.ip, data.addr);
+ fill_exec_info(&ei_from, session, event, data.ip);
+ fill_exec_info(&ei_to, session, event, data.addr);
+
+ print_exec_info(&ei_from, &ei_to);
return 0;
}
static struct perf_event_ops event_ops = {
.sample = process_sample_event,
+ .comm = event__process_comm,
.ordered_samples = false,
};
@@ -64,6 +176,12 @@ static int __cmd_trace(void)
return -ENOMEM;
}
+ /* if print flags are unset, we use default flags */
+ if (is_flags_unset(print_flags))
+ print_flags = EI_FLAG_PRINT_DEFAULT;
+
+ setup_pager();
+ print_exec_info_header();
perf_session__process_events(session, &event_ops);
perf_session__delete(session);
@@ -104,10 +222,8 @@ int cmd_bts(int argc, const char **argv, const char *prefix __used)
if (!strncmp(argv[0], "record", 6))
return __cmd_record(argc, argv);
- else if (!strncmp(argv[0], "trace", 5)) {
- setup_pager();
+ else if (!strncmp(argv[0], "trace", 5))
return __cmd_trace();
- }
else
usage_with_options(bts_usage, bts_options);
Provide the function to print file path to the executed elf.
Users can enable it with option '-e' or '--elfpath'.
For example,
'perf bts -ae trace'
This command prints address and file path of elf.
And, output is:
address elf_filepath
0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e3b20 /lib64/ld-2.12.90.so
0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e3b20 /lib64/ld-2.12.90.so
0x00007fd4038e3b23 /lib64/ld-2.12.90.so => 0x00007fd4038e7910 /lib64/ld-2.12.90.so
0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e7910 /lib64/ld-2.12.90.so
0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e7936 /lib64/ld-2.12.90.so
0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e793d /lib64/ld-2.12.90.so
0x00007fd4038e7981 /lib64/ld-2.12.90.so => 0x00007fd4038e79a3 /lib64/ld-2.12.90.so
0x00007fd4038e79a7 /lib64/ld-2.12.90.so => 0x00007fd4038e7988 /lib64/ld-2.12.90.so
...
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/Documentation/perf-bts.txt | 3 +++
tools/perf/builtin-bts.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
index 56ddaa4..acabffc 100644
--- a/tools/perf/Documentation/perf-bts.txt
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -38,6 +38,9 @@ OPTIONS
-p::
--pid::
Print pid.
+-e::
+--elfpath::
+ Print file path of executed elf.
SEE ALSO
--------
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
index 11e491b..3b2a21e 100644
--- a/tools/perf/builtin-bts.c
+++ b/tools/perf/builtin-bts.c
@@ -20,6 +20,7 @@ struct exec_info {
u64 addr; /* recorded address by bts */
pid_t pid; /* tracee process pid */
const char *comm; /* command name */
+ const char *elfpath; /* file path to elf */
};
#define EI_PID_UNSET -1
@@ -28,6 +29,7 @@ struct exec_info {
#define EI_FLAG_PRINT_ADDR (1 << 0)
#define EI_FLAG_PRINT_PID (1 << 1)
#define EI_FLAG_PRINT_COMM (1 << 2)
+#define EI_FLAG_PRINT_ELFPATH (1 << 3)
/* it's used when no print item specified */
#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_ADDR
@@ -77,6 +79,9 @@ static const struct option bts_options[] = {
OPT_CALLBACK_DEFAULT_NOOPT('c', "comm", NULL, NULL,
"print command name", set_print_flags,
(void *)EI_FLAG_PRINT_COMM),
+ OPT_CALLBACK_DEFAULT_NOOPT('e', "elfpath", NULL, NULL,
+ "print file path to elf", set_print_flags,
+ (void *)EI_FLAG_PRINT_ELFPATH),
OPT_END()
};
@@ -91,6 +96,7 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
event_t *event, u64 addr)
{
struct thread *thread;
+ struct addr_location al;
ei->addr = addr;
ei->pid = event->ip.pid;
@@ -99,12 +105,25 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
if (!thread)
return;
ei->comm = thread->comm;
+
+ /* get file path to elf */
+ memset(&al, 0, sizeof(al));
+ thread__find_addr_map(thread, session, PERF_RECORD_MISC_USER,
+ MAP__FUNCTION, event->ip.pid, addr, &al);
+ if (!al.map)
+ thread__find_addr_map(thread, session, PERF_RECORD_MISC_KERNEL,
+ MAP__FUNCTION, event->ip.pid, addr, &al);
+ if (!al.map)
+ return;
+ map__load(al.map, NULL);
+ ei->elfpath = al.map->dso->long_name;
}
static void __print_exec_info(struct exec_info *ei)
{
char pid[16];
const char *comm;
+ const char *elfpath;
if (print_flags & EI_FLAG_PRINT_PID) {
if (ei->pid == EI_PID_UNSET)
@@ -119,6 +138,10 @@ static void __print_exec_info(struct exec_info *ei)
}
if (print_flags & EI_FLAG_PRINT_ADDR)
printf(FMT_ADDR " ", ei->addr);
+ if (print_flags & EI_FLAG_PRINT_ELFPATH) {
+ elfpath = ei->elfpath ? : EI_UNKNOWN_TEXT;
+ printf("%-32s ", elfpath);
+ }
}
static void print_exec_info(struct exec_info *ei_from, struct exec_info *ei_to)
@@ -137,6 +160,8 @@ static void print_exec_info_header(void)
printf("%-12s ", "command");
if (print_flags & EI_FLAG_PRINT_ADDR)
printf("%-" FMT_ADDR_WIDTH "s ", "address");
+ if (print_flags & EI_FLAG_PRINT_ELFPATH)
+ printf("%-32s ", "elf_filepath");
printf("\n");
}
@@ -163,6 +188,7 @@ static int process_sample_event(event_t *event, struct perf_session *session)
static struct perf_event_ops event_ops = {
.sample = process_sample_event,
.comm = event__process_comm,
+ .mmap = event__process_mmap,
.ordered_samples = false,
};
@@ -180,6 +206,11 @@ static int __cmd_trace(void)
if (is_flags_unset(print_flags))
print_flags = EI_FLAG_PRINT_DEFAULT;
+ /* setup kernel maps to resolve vmlinux file path */
+ perf_session__create_kernel_maps(session);
+ if (symbol__init() < 0)
+ fprintf(stderr, "failed to initialize symbol.\n");
+
setup_pager();
print_exec_info_header();
perf_session__process_events(session, &event_ops);
For ease of use, add option printing all information '-A' or '--all'.
This option can print following information.
- pid
- command name
- address
- function+offset
- elf file path
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/Documentation/perf-bts.txt | 3 +++
tools/perf/builtin-bts.c | 6 ++++++
2 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
index 13ee862..c598dd2 100644
--- a/tools/perf/Documentation/perf-bts.txt
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -44,6 +44,9 @@ OPTIONS
-s::
--symbol::
Print function name and offset. (default)
+-A::
+--all::
+ Print all information.
SEE ALSO
--------
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
index 71c7fbe..c359f70 100644
--- a/tools/perf/builtin-bts.c
+++ b/tools/perf/builtin-bts.c
@@ -34,6 +34,9 @@ struct exec_info {
#define EI_FLAG_PRINT_ELFPATH (1 << 3)
#define EI_FLAG_PRINT_SYMBOL (1 << 4)
+/* all print flags are enabled */
+#define EI_FLAG_PRINT_ALL -1UL
+
/* it's used when no print item specified */
#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_SYMBOL
@@ -92,6 +95,9 @@ static const struct option bts_options[] = {
"print function+offset (default)",
set_print_flags,
(void *)EI_FLAG_PRINT_SYMBOL),
+ OPT_CALLBACK_DEFAULT_NOOPT('A', "all", NULL, NULL,
+ "print all items", set_print_flags,
+ (void *)EI_FLAG_PRINT_ALL),
OPT_END()
};
Provide the function to print function+offset.
And, set it as the default behavior of 'perf bts trace'.
To use this function, users can also specify the option '-s' or '--symbol'.
Example: 'perf bts -as trace'
This command prints address and function+offset.
Output sample:
address function+offset
0xffffffff8146fe0e irq_return+0x0 => 0x00007fd4038e3b20 _start+0x0
...
0x000000380661ee79 __libc_start_main+0xf9 => 0x00000000004035c3 main+0x0
0xffffffff8146ef4e irq_return+0x0 => 0x00000000004035c3 main+0x0
0x00000000004035e8 main+0x25 => 0x000000000040bca0 set_program_name+0x0
0xffffffff8146ef4e irq_return+0x0 => 0x000000000040bca0 set_program_name+0x0
0x000000000040bcae set_program_name+0xe => 0x00000000004023d0 strrchr@plt+0x0
0x00000000004023d0 strrchr@plt+0x0 => 0x00000000004023d6 strrchr@plt+0x6
...
0x0000000000403e0c main+0x849 => 0x00000000004021f0 exit@plt+0x0
0x00000000004021f0 exit@plt+0x0 => 0x00000000004021f6 exit@plt+0x6
0x00000000004021fb exit@plt+0xb => 0x00000000004020d0 _init+0x18
0x00000000004020d6 _init+0x1e => 0x00000038062149d0 _dl_runtime_resolve+0x0
...
Signed-off-by: Akihiro Nagai <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: [email protected]
---
tools/perf/Documentation/perf-bts.txt | 5 +++-
tools/perf/builtin-bts.c | 44 ++++++++++++++++++++++++++++++---
2 files changed, 44 insertions(+), 5 deletions(-)
diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
index acabffc..13ee862 100644
--- a/tools/perf/Documentation/perf-bts.txt
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -31,7 +31,7 @@ OPTIONS
Specify input file name to analyze.
-a::
--addr::
- Print address. (default)
+ Print address.
-c::
--comm::
Print command name.
@@ -41,6 +41,9 @@ OPTIONS
-e::
--elfpath::
Print file path of executed elf.
+-s::
+--symbol::
+ Print function name and offset. (default)
SEE ALSO
--------
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
index 3b2a21e..71c7fbe 100644
--- a/tools/perf/builtin-bts.c
+++ b/tools/perf/builtin-bts.c
@@ -21,6 +21,8 @@ struct exec_info {
pid_t pid; /* tracee process pid */
const char *comm; /* command name */
const char *elfpath; /* file path to elf */
+ const char *function; /* function name */
+ u64 offset; /* offset from top of the function */
};
#define EI_PID_UNSET -1
@@ -30,9 +32,10 @@ struct exec_info {
#define EI_FLAG_PRINT_PID (1 << 1)
#define EI_FLAG_PRINT_COMM (1 << 2)
#define EI_FLAG_PRINT_ELFPATH (1 << 3)
+#define EI_FLAG_PRINT_SYMBOL (1 << 4)
/* it's used when no print item specified */
-#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_ADDR
+#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_SYMBOL
/* print item flags */
static unsigned long print_flags;
@@ -43,6 +46,9 @@ static unsigned long print_flags;
#define EI_UNKNOWN_TEXT "(unknown)"
#define EI_UNKNOWN_TEXT_LEN (sizeof(EI_UNKNOWN_TEXT))
+/* 'function+offset' lengh = function + '+' + %#18llx + '\0' */
+#define symbol_buf_size(func_name) (strlen(func_name) + 1 + 18 + 1)
+
/* default input file name to analyze */
static const char *input_name = "perf.data";
@@ -71,7 +77,7 @@ static int set_print_flags(const struct option *opt, const char *str __unused,
static const struct option bts_options[] = {
OPT_STRING('i', "input", &input_name, "file", "input file name"),
OPT_CALLBACK_DEFAULT_NOOPT('a', "addr", NULL, NULL,
- "print address (default)", set_print_flags,
+ "print address", set_print_flags,
(void *)EI_FLAG_PRINT_ADDR),
OPT_CALLBACK_DEFAULT_NOOPT('p', "pid", NULL, NULL,
"print pid", set_print_flags,
@@ -82,6 +88,10 @@ static const struct option bts_options[] = {
OPT_CALLBACK_DEFAULT_NOOPT('e', "elfpath", NULL, NULL,
"print file path to elf", set_print_flags,
(void *)EI_FLAG_PRINT_ELFPATH),
+ OPT_CALLBACK_DEFAULT_NOOPT('s', "symbol", NULL, NULL,
+ "print function+offset (default)",
+ set_print_flags,
+ (void *)EI_FLAG_PRINT_SYMBOL),
OPT_END()
};
@@ -106,7 +116,7 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
return;
ei->comm = thread->comm;
- /* get file path to elf */
+ /* get file path to elf, and symbol information */
memset(&al, 0, sizeof(al));
thread__find_addr_map(thread, session, PERF_RECORD_MISC_USER,
MAP__FUNCTION, event->ip.pid, addr, &al);
@@ -115,8 +125,14 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
MAP__FUNCTION, event->ip.pid, addr, &al);
if (!al.map)
return;
- map__load(al.map, NULL);
+
+ al.addr = al.map->map_ip(al.map, addr);
+ al.sym = map__find_symbol(al.map, al.addr, NULL);
+ if (!al.sym)
+ return;
ei->elfpath = al.map->dso->long_name;
+ ei->function = al.sym->name;
+ ei->offset = al.addr - al.sym->start;
}
static void __print_exec_info(struct exec_info *ei)
@@ -124,6 +140,8 @@ static void __print_exec_info(struct exec_info *ei)
char pid[16];
const char *comm;
const char *elfpath;
+ char *symbol;
+ int symbol_len;
if (print_flags & EI_FLAG_PRINT_PID) {
if (ei->pid == EI_PID_UNSET)
@@ -138,6 +156,22 @@ static void __print_exec_info(struct exec_info *ei)
}
if (print_flags & EI_FLAG_PRINT_ADDR)
printf(FMT_ADDR " ", ei->addr);
+ if (print_flags & EI_FLAG_PRINT_SYMBOL) {
+ if (!ei->function) {
+ /* when function is unknown, offset must be unknown */
+ printf("%-32s ", EI_UNKNOWN_TEXT);
+ goto print_elfpath;
+ }
+
+ symbol_len = symbol_buf_size(ei->function);
+ symbol = malloc(symbol_len);
+ snprintf(symbol, symbol_len, "%s+0x%llx",
+ ei->function, ei->offset);
+ printf("%-32s ", symbol);
+ free(symbol);
+ }
+
+print_elfpath:
if (print_flags & EI_FLAG_PRINT_ELFPATH) {
elfpath = ei->elfpath ? : EI_UNKNOWN_TEXT;
printf("%-32s ", elfpath);
@@ -160,6 +194,8 @@ static void print_exec_info_header(void)
printf("%-12s ", "command");
if (print_flags & EI_FLAG_PRINT_ADDR)
printf("%-" FMT_ADDR_WIDTH "s ", "address");
+ if (print_flags & EI_FLAG_PRINT_SYMBOL)
+ printf("%-32s ", "function+offset");
if (print_flags & EI_FLAG_PRINT_ELFPATH)
printf("%-32s ", "elf_filepath");
printf("\n");
On Fri, 2010-12-03 at 13:00 +0900, Akihiro Nagai wrote:
> Provide the function to print function+offset.
> And, set it as the default behavior of 'perf bts trace'.
> To use this function, users can also specify the option '-s' or '--symbol'.
>
> Example: 'perf bts -as trace'
> This command prints address and function+offset.
>
> Output sample:
> address function+offset
> 0xffffffff8146fe0e irq_return+0x0 => 0x00007fd4038e3b20 _start+0x0
> ...
> 0x000000380661ee79 __libc_start_main+0xf9 => 0x00000000004035c3 main+0x0
> 0xffffffff8146ef4e irq_return+0x0 => 0x00000000004035c3 main+0x0
> 0x00000000004035e8 main+0x25 => 0x000000000040bca0 set_program_name+0x0
> 0xffffffff8146ef4e irq_return+0x0 => 0x000000000040bca0 set_program_name+0x0
> 0x000000000040bcae set_program_name+0xe => 0x00000000004023d0 strrchr@plt+0x0
> 0x00000000004023d0 strrchr@plt+0x0 => 0x00000000004023d6 strrchr@plt+0x6
> ...
> 0x0000000000403e0c main+0x849 => 0x00000000004021f0 exit@plt+0x0
> 0x00000000004021f0 exit@plt+0x0 => 0x00000000004021f6 exit@plt+0x6
> 0x00000000004021fb exit@plt+0xb => 0x00000000004020d0 _init+0x18
> 0x00000000004020d6 _init+0x1e => 0x00000038062149d0 _dl_runtime_resolve+0x0
What would be very nice output is something like a source-code browser
(tig might have something worth borrowing) and annotating the output
using the bts output.
You could for example darken the code that didn't get any coverage, and
print a percentage for each conditional statement.
That way you can easily see how your code is traversed.
(2010/12/03 12:58), Akihiro Nagai wrote:
> Add new macro OPT_CALLBACK_DEFAULT_NOOPT for parse_options.
> It enables to pass the default value (opt->defval) to the callback function
> processing options require no argument.
>
> Signed-off-by: Akihiro Nagai <[email protected]>
This could be useful for perf probe too. :)
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/util/parse-options.h | 4 ++++
> 1 files changed, 4 insertions(+), 0 deletions(-)
>
> diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
> index c7d72dc..abc31a1 100644
> --- a/tools/perf/util/parse-options.h
> +++ b/tools/perf/util/parse-options.h
> @@ -119,6 +119,10 @@ struct option {
> { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
> #define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
> { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT }
> +#define OPT_CALLBACK_DEFAULT_NOOPT(s, l, v, a, h, f, d) \
> + { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
> + .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d,\
> + .flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOARG}
>
> /* parse_options() will filter out the processed options and leave the
> * non-option argments in argv[].
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
(2010/12/03 12:59), Akihiro Nagai wrote:
> Introduce the easy way to record bts log, 'perf bts record'.
> This command can record the bts log while specified command is executing,
> and save to the file "perf.data"
Good work :)
Just update tools/perf/command-list.txt, so that perf shows
bts subcommand on its list.
>
> Usage:
> perf bts record <tracee command>
>
> Example:
> # perf bts record ls -l
> (ls -l outputs)
> # perf record: Captured and wrote 4.381 MB perf.data (~191405 samples)
> # ls
> perf.data
>
> Signed-off-by: Akihiro Nagai <[email protected]>
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/Documentation/perf-bts.txt | 24 +++++++++++++
> tools/perf/Makefile | 1 +
> tools/perf/builtin-bts.c | 62 +++++++++++++++++++++++++++++++++
> tools/perf/builtin.h | 1 +
> tools/perf/perf.c | 1 +
> 5 files changed, 89 insertions(+), 0 deletions(-)
> create mode 100644 tools/perf/Documentation/perf-bts.txt
> create mode 100644 tools/perf/builtin-bts.c
>
> diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
> new file mode 100644
> index 0000000..55a2fe6
> --- /dev/null
> +++ b/tools/perf/Documentation/perf-bts.txt
> @@ -0,0 +1,24 @@
> +perf-bts(1)
> +==============
> +
> +NAME
> +----
> +perf-bts - Record branch-trace-store log
> +
> +SYNOPSIS
> +--------
> +[verse]
> +'perf bts' record <command>
> +
> +DESCRIPTION
> +-----------
> +This command can record branch-trace-store log.
> +Branch-trace-store is a facility of processors. It can record
> +address of branch to/from on every branch instruction and interrupt.
> +
> +'perf bts record <command>' records branch-trace-store log while specified
> +command is executing. And, save to the file "perf.data".
> +
> +SEE ALSO
> +--------
> +linkperf:perf-record[1]
> diff --git a/tools/perf/Makefile b/tools/perf/Makefile
> index b3e6bc6..14de491 100644
> --- a/tools/perf/Makefile
> +++ b/tools/perf/Makefile
> @@ -503,6 +503,7 @@ BUILTIN_OBJS += $(OUTPUT)builtin-kvm.o
> BUILTIN_OBJS += $(OUTPUT)builtin-test.o
> BUILTIN_OBJS += $(OUTPUT)builtin-inject.o
> BUILTIN_OBJS += $(OUTPUT)builtin-trace.o
> +BUILTIN_OBJS += $(OUTPUT)builtin-bts.o
>
> PERFLIBS = $(LIB_FILE)
>
> diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
> new file mode 100644
> index 0000000..587cfad
> --- /dev/null
> +++ b/tools/perf/builtin-bts.c
> @@ -0,0 +1,62 @@
> +#include "builtin.h"
> +#include "perf.h"
> +#include "util/parse-options.h"
> +
> +static const char * const bts_usage[] = {
> + "perf bts record <command>",
> + NULL,
> +};
> +
> +/* arguments to call 'perf record' */
> +static const char * const record_args[] = {
> + "record",
> + "-f",
> + "-e", "branches:u",
> + "-c", "1",
> + "-d",
> +};
> +
> +/* dummy struct option to call parse_options() */
> +static const struct option bts_options[] = {
> + OPT_END()
> +};
> +
> +static int __cmd_record(int argc, const char **argv)
> +{
> + unsigned int rec_argc, i, j;
> + const char **rec_argv;
> + int rc;
> +
> + /* prepare the arguments list to call 'perf record' */
> + rec_argc = ARRAY_SIZE(record_args) + argc - 1;
> + rec_argv = calloc(rec_argc + 1, sizeof(char *));
> +
> + for (i = 0; i < ARRAY_SIZE(record_args); i++)
> + rec_argv[i] = record_args[i];
> +
> + for (j = 1; j < (unsigned int)argc; j++, i++)
> + rec_argv[i] = argv[j];
> +
> + BUG_ON(i != rec_argc);
> +
> + /* call 'perf record' */
> + rc = cmd_record(i, rec_argv, NULL);
> +
> + free(rec_argv);
> + return rc;
> +}
> +
> +int cmd_bts(int argc, const char **argv, const char *prefix __used)
> +{
> + argc = parse_options(argc, argv, bts_options, bts_usage,
> + PARSE_OPT_STOP_AT_NON_OPTION);
> + if (!argc)
> + usage_with_options(bts_usage, bts_options);
> +
> + if (!strncmp(argv[0], "record", 6))
> + return __cmd_record(argc, argv);
> + else
> + usage_with_options(bts_usage, bts_options);
> +
> + return 0;
> +}
> diff --git a/tools/perf/builtin.h b/tools/perf/builtin.h
> index 86a93a1..9ab6430 100644
> --- a/tools/perf/builtin.h
> +++ b/tools/perf/builtin.h
> @@ -36,5 +36,6 @@ extern int cmd_kvm(int argc, const char **argv, const char *prefix);
> extern int cmd_test(int argc, const char **argv, const char *prefix);
> extern int cmd_inject(int argc, const char **argv, const char *prefix);
> extern int cmd_trace(int argc, const char **argv, const char *prefix);
> +extern int cmd_bts(int argc, const char **argv, const char *prefix);
>
> #endif
> diff --git a/tools/perf/perf.c b/tools/perf/perf.c
> index 0f7bb95..5b16182 100644
> --- a/tools/perf/perf.c
> +++ b/tools/perf/perf.c
> @@ -332,6 +332,7 @@ static void handle_internal_command(int argc, const char **argv)
> { "test", cmd_test, 0 },
> { "inject", cmd_inject, 0 },
> { "trace", cmd_trace, 0 },
> + { "bts", cmd_bts, 0 },
> };
> unsigned int i;
> static const char ext[] = STRIP_EXTENSION;
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
On Fri, Dec 03, 2010 at 01:00:15PM +0900, Akihiro Nagai wrote:
> Provide the function to print function+offset.
> And, set it as the default behavior of 'perf bts trace'.
> To use this function, users can also specify the option '-s' or '--symbol'.
>
> Example: 'perf bts -as trace'
> This command prints address and function+offset.
>
> Output sample:
> address function+offset
> 0xffffffff8146fe0e irq_return+0x0 => 0x00007fd4038e3b20 _start+0x0
> ...
> 0x000000380661ee79 __libc_start_main+0xf9 => 0x00000000004035c3 main+0x0
> 0xffffffff8146ef4e irq_return+0x0 => 0x00000000004035c3 main+0x0
> 0x00000000004035e8 main+0x25 => 0x000000000040bca0 set_program_name+0x0
> 0xffffffff8146ef4e irq_return+0x0 => 0x000000000040bca0 set_program_name+0x0
> 0x000000000040bcae set_program_name+0xe => 0x00000000004023d0 strrchr@plt+0x0
> 0x00000000004023d0 strrchr@plt+0x0 => 0x00000000004023d6 strrchr@plt+0x6
> ...
> 0x0000000000403e0c main+0x849 => 0x00000000004021f0 exit@plt+0x0
> 0x00000000004021f0 exit@plt+0x0 => 0x00000000004021f6 exit@plt+0x6
> 0x00000000004021fb exit@plt+0xb => 0x00000000004020d0 _init+0x18
> 0x00000000004020d6 _init+0x1e => 0x00000038062149d0 _dl_runtime_resolve+0x0
> ...
There is another kind of mode that I suspect would be very useful: something like
a userspace function graph tracer (you can have a look into the kernel function
graph tracer we have in ftrace to get an overview).
So, the idea would be to rebuild the whole function call flow:
main() {
func1()
func2() {
func3()
}
}
This would require to deref the instructions into the dso addresses from
the bts trace, keep only the "call" and the "ret" (there would be a small arch
backend for this mode) and rebuild the whole tree of calls.
We already have all the dso address mapping API in place with perf.
I really think this could be a very useful tool. Also we can even later
expand this to the branches, as Peter suggested. But starting with calls
would a very great start already.
Thanks.
(2010/12/03 12:59), Akihiro Nagai wrote:
> Introduce new sub command 'perf bts trace'.
> This command can parse and print bts log recorded by
> 'perf bts record'.
>
> Usage:
> - First, record the bts log 'perf bts record <command>'
> - Second, parse and print bts log 'perf bts trace'
>
> Output:
> 0xffffffff8146fe0e => 0x0000003806200b20
> 0x0000003806200b23 => 0x0000003806204910
> 0xffffffff8146fe0e => 0x0000003806204910
> 0xffffffff8146fe0e => 0x0000003806204936
> 0xffffffff8146fe0e => 0x000000380620493d
> 0x0000003806204981 => 0x00000038062049a3
> 0x00000038062049a7 => 0x0000003806204988
> ...
>
> Signed-off-by: Akihiro Nagai <[email protected]>
It looks a good step.
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/Documentation/perf-bts.txt | 14 ++++++--
> tools/perf/builtin-bts.c | 59 +++++++++++++++++++++++++++++++--
> 2 files changed, 67 insertions(+), 6 deletions(-)
>
> diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
> index 55a2fe6..5920dcc 100644
> --- a/tools/perf/Documentation/perf-bts.txt
> +++ b/tools/perf/Documentation/perf-bts.txt
> @@ -3,22 +3,30 @@ perf-bts(1)
>
> NAME
> ----
> -perf-bts - Record branch-trace-store log
> +perf-bts - Record and print branch-trace-store log
>
> SYNOPSIS
> --------
> [verse]
> -'perf bts' record <command>
> +'perf bts' [<options>] {record|trace}
>
> DESCRIPTION
> -----------
> -This command can record branch-trace-store log.
> +This command can record and print branch-trace-store log.
> Branch-trace-store is a facility of processors. It can record
> address of branch to/from on every branch instruction and interrupt.
>
> 'perf bts record <command>' records branch-trace-store log while specified
> command is executing. And, save to the file "perf.data".
>
> +'perf bts trace' parses recorded branch-trace-store log and prints it.
> +
> +OPTIONS
> +-------
> +-i::
> +--input=::
> + Specify input file name to analyze.
> +
> SEE ALSO
> --------
> linkperf:perf-record[1]
> diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
> index 587cfad..54e83af 100644
> --- a/tools/perf/builtin-bts.c
> +++ b/tools/perf/builtin-bts.c
> @@ -1,10 +1,25 @@
> #include "builtin.h"
> #include "perf.h"
> #include "util/parse-options.h"
> +#include "util/session.h"
> +#include "util/cache.h"
> +#include <inttypes.h>
> +
> +/* format string of specifying min width to print address */
> +#if __WORDSIZE == 32
> +#define FMT_ADDR_WIDTH "10" /* length of "0x" + 32bit address */
> +#else
> +#define FMT_ADDR_WIDTH "18" /* length of "0x" + 64bit address */
> +#endif
> +/* format string to print address */
> +#define FMT_ADDR "%#0" FMT_ADDR_WIDTH "llx"
> +
> +/* default input file name to analyze */
> +static const char *input_name = "perf.data";
>
> static const char * const bts_usage[] = {
> - "perf bts record <command>",
> - NULL,
> + "perf bts [<options>] {record|trace}",
> + NULL
> };
>
> /* arguments to call 'perf record' */
> @@ -16,11 +31,45 @@ static const char * const record_args[] = {
> "-d",
> };
>
> -/* dummy struct option to call parse_options() */
> static const struct option bts_options[] = {
> + OPT_STRING('i', "input", &input_name, "file", "input file name"),
> OPT_END()
> };
>
> +static int process_sample_event(event_t *event, struct perf_session *session)
> +{
> + struct sample_data data;
> +
> + memset(&data, 0, sizeof(data));
> + event__parse_sample(event, session->sample_type, &data);
> +
> + /* data.ip is 'from address', data.addr is 'to address' */
> + printf(FMT_ADDR " => " FMT_ADDR "\n", data.ip, data.addr);
> +
> + return 0;
> +}
> +
> +static struct perf_event_ops event_ops = {
> + .sample = process_sample_event,
> + .ordered_samples = false,
> +};
> +
> +static int __cmd_trace(void)
> +{
> + struct perf_session *session;
> +
> + session = perf_session__new(input_name, O_RDONLY, 0, false);
> + if (!session) {
> + fprintf(stderr, "failed to create perf_session.\n");
> + return -ENOMEM;
> + }
> +
> + perf_session__process_events(session, &event_ops);
> + perf_session__delete(session);
> +
> + return 0;
> +}
> +
> static int __cmd_record(int argc, const char **argv)
> {
> unsigned int rec_argc, i, j;
> @@ -55,6 +104,10 @@ int cmd_bts(int argc, const char **argv, const char *prefix __used)
>
> if (!strncmp(argv[0], "record", 6))
> return __cmd_record(argc, argv);
> + else if (!strncmp(argv[0], "trace", 5)) {
> + setup_pager();
> + return __cmd_trace();
> + }
> else
> usage_with_options(bts_usage, bts_options);
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
(2010/12/03 12:59), Akihiro Nagai wrote:
> Provide the function of printing pid and command name to
> 'perf bts trace'. Users can select items to print with options.
> For example,
> 'perf bts -p trace' prints only pid,
> 'perf bts -ac trace' prints address and comamnd name.
> 'perf bts trace' prints only address (default)
>
> This is output sample (perf bts -ac trace):
> command address
> ls 0xffffffff8146fe0e => ls 0x0000003806200b20
> ls 0xffffffff8146fe0e => ls 0x0000003806200b20
> ls 0x0000003806200b23 => ls 0x0000003806204910
> ls 0xffffffff8146fe0e => ls 0x0000003806204910
> ls 0xffffffff8146fe0e => ls 0x0000003806204936
> ls 0xffffffff8146fe0e => ls 0x000000380620493d
> ls 0x0000003806204981 => ls 0x00000038062049a3
> ls 0x00000038062049a7 => ls 0x0000003806204988
>
> Signed-off-by: Akihiro Nagai <[email protected]>
Looks good to me :)
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/Documentation/perf-bts.txt | 18 ++++-
> tools/perf/builtin-bts.c | 124 ++++++++++++++++++++++++++++++++-
> 2 files changed, 135 insertions(+), 7 deletions(-)
>
> diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
> index 5920dcc..56ddaa4 100644
> --- a/tools/perf/Documentation/perf-bts.txt
> +++ b/tools/perf/Documentation/perf-bts.txt
> @@ -3,7 +3,7 @@ perf-bts(1)
>
> NAME
> ----
> -perf-bts - Record and print branch-trace-store log
> +perf-bts - Record and analyze branch-trace-store log
>
> SYNOPSIS
> --------
> @@ -12,20 +12,32 @@ SYNOPSIS
>
> DESCRIPTION
> -----------
> -This command can record and print branch-trace-store log.
> +This command can record and analyze branch-trace-store log.
> Branch-trace-store is a facility of processors. It can record
> address of branch to/from on every branch instruction and interrupt.
>
> 'perf bts record <command>' records branch-trace-store log while specified
> command is executing. And, save to the file "perf.data".
>
> -'perf bts trace' parses recorded branch-trace-store log and prints it.
> +'perf bts trace' analyzes recorded branch-trace-store log and prints it.
> +The command can select the item to print. For example,
> + 'perf bts -a trace' : prints only address
> + 'perf bts -acp trace' : prints address, command name and pid
>
> OPTIONS
> -------
> -i::
> --input=::
> Specify input file name to analyze.
> +-a::
> +--addr::
> + Print address. (default)
> +-c::
> +--comm::
> + Print command name.
> +-p::
> +--pid::
> + Print pid.
>
> SEE ALSO
> --------
> diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
> index 54e83af..11e491b 100644
> --- a/tools/perf/builtin-bts.c
> +++ b/tools/perf/builtin-bts.c
> @@ -3,6 +3,7 @@
> #include "util/parse-options.h"
> #include "util/session.h"
> #include "util/cache.h"
> +#include "util/trace-event.h"
> #include <inttypes.h>
>
> /* format string of specifying min width to print address */
> @@ -14,6 +15,32 @@
> /* format string to print address */
> #define FMT_ADDR "%#0" FMT_ADDR_WIDTH "llx"
>
> +/* printable items */
> +struct exec_info {
> + u64 addr; /* recorded address by bts */
> + pid_t pid; /* tracee process pid */
> + const char *comm; /* command name */
> +};
> +
> +#define EI_PID_UNSET -1
> +
> +/* flags which item print */
> +#define EI_FLAG_PRINT_ADDR (1 << 0)
> +#define EI_FLAG_PRINT_PID (1 << 1)
> +#define EI_FLAG_PRINT_COMM (1 << 2)
> +
> +/* it's used when no print item specified */
> +#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_ADDR
> +
> +/* print item flags */
> +static unsigned long print_flags;
> +
> +#define is_flags_unset(flags) (flags == 0)
> +
> +/* print it when we cannnot analyze and get the information */
> +#define EI_UNKNOWN_TEXT "(unknown)"
> +#define EI_UNKNOWN_TEXT_LEN (sizeof(EI_UNKNOWN_TEXT))
> +
> /* default input file name to analyze */
> static const char *input_name = "perf.data";
>
> @@ -31,26 +58,111 @@ static const char * const record_args[] = {
> "-d",
> };
>
> +/* set print flags call from parse_options() */
> +static int set_print_flags(const struct option *opt, const char *str __unused,
> + int unset __unused)
> +{
> + print_flags |= (unsigned long)opt->defval;
> + return 0;
> +}
> +
> static const struct option bts_options[] = {
> OPT_STRING('i', "input", &input_name, "file", "input file name"),
> + OPT_CALLBACK_DEFAULT_NOOPT('a', "addr", NULL, NULL,
> + "print address (default)", set_print_flags,
> + (void *)EI_FLAG_PRINT_ADDR),
> + OPT_CALLBACK_DEFAULT_NOOPT('p', "pid", NULL, NULL,
> + "print pid", set_print_flags,
> + (void *)EI_FLAG_PRINT_PID),
> + OPT_CALLBACK_DEFAULT_NOOPT('c', "comm", NULL, NULL,
> + "print command name", set_print_flags,
> + (void *)EI_FLAG_PRINT_COMM),
> OPT_END()
> };
>
> +static void init_exec_info(struct exec_info *ei)
> +{
> + memset(ei, 0, sizeof(*ei));
> + ei->pid = EI_PID_UNSET;
> +}
> +
> +/* collect printable items to struct exec_info */
> +static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
> + event_t *event, u64 addr)
> +{
> + struct thread *thread;
> +
> + ei->addr = addr;
> + ei->pid = event->ip.pid;
> +
> + thread = perf_session__findnew(session, event->ip.pid);
> + if (!thread)
> + return;
> + ei->comm = thread->comm;
> +}
> +
> +static void __print_exec_info(struct exec_info *ei)
> +{
> + char pid[16];
> + const char *comm;
> +
> + if (print_flags & EI_FLAG_PRINT_PID) {
> + if (ei->pid == EI_PID_UNSET)
> + strncpy(pid, EI_UNKNOWN_TEXT, EI_UNKNOWN_TEXT_LEN);
> + else
> + snprintf(pid, 16, "%d", ei->pid);
> + printf("%5s ", pid);
> + }
> + if (print_flags & EI_FLAG_PRINT_COMM) {
> + comm = ei->comm ? : EI_UNKNOWN_TEXT;
> + printf("%-12s ", comm);
> + }
> + if (print_flags & EI_FLAG_PRINT_ADDR)
> + printf(FMT_ADDR " ", ei->addr);
> +}
> +
> +static void print_exec_info(struct exec_info *ei_from, struct exec_info *ei_to)
> +{
> + __print_exec_info(ei_from);
> + printf("=> ");
> + __print_exec_info(ei_to);
> + printf("\n");
> +}
> +
> +static void print_exec_info_header(void)
> +{
> + if (print_flags & EI_FLAG_PRINT_PID)
> + printf("%5s ", "pid");
> + if (print_flags & EI_FLAG_PRINT_COMM)
> + printf("%-12s ", "command");
> + if (print_flags & EI_FLAG_PRINT_ADDR)
> + printf("%-" FMT_ADDR_WIDTH "s ", "address");
> + printf("\n");
> +}
> +
> static int process_sample_event(event_t *event, struct perf_session *session)
> {
> struct sample_data data;
> + struct exec_info ei_from, ei_to;
>
> memset(&data, 0, sizeof(data));
> event__parse_sample(event, session->sample_type, &data);
>
> + init_exec_info(&ei_from);
> + init_exec_info(&ei_to);
> +
> /* data.ip is 'from address', data.addr is 'to address' */
> - printf(FMT_ADDR " => " FMT_ADDR "\n", data.ip, data.addr);
> + fill_exec_info(&ei_from, session, event, data.ip);
> + fill_exec_info(&ei_to, session, event, data.addr);
> +
> + print_exec_info(&ei_from, &ei_to);
>
> return 0;
> }
>
> static struct perf_event_ops event_ops = {
> .sample = process_sample_event,
> + .comm = event__process_comm,
> .ordered_samples = false,
> };
>
> @@ -64,6 +176,12 @@ static int __cmd_trace(void)
> return -ENOMEM;
> }
>
> + /* if print flags are unset, we use default flags */
> + if (is_flags_unset(print_flags))
> + print_flags = EI_FLAG_PRINT_DEFAULT;
> +
> + setup_pager();
> + print_exec_info_header();
> perf_session__process_events(session, &event_ops);
> perf_session__delete(session);
>
> @@ -104,10 +222,8 @@ int cmd_bts(int argc, const char **argv, const char *prefix __used)
>
> if (!strncmp(argv[0], "record", 6))
> return __cmd_record(argc, argv);
> - else if (!strncmp(argv[0], "trace", 5)) {
> - setup_pager();
> + else if (!strncmp(argv[0], "trace", 5))
> return __cmd_trace();
> - }
> else
> usage_with_options(bts_usage, bts_options);
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
(2010/12/03 12:59), Akihiro Nagai wrote:
> Provide the function to print file path to the executed elf.
> Users can enable it with option '-e' or '--elfpath'.
> For example,
> 'perf bts -ae trace'
> This command prints address and file path of elf.
> And, output is:
>
> address elf_filepath
> 0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e3b20 /lib64/ld-2.12.90.so
> 0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e3b20 /lib64/ld-2.12.90.so
> 0x00007fd4038e3b23 /lib64/ld-2.12.90.so => 0x00007fd4038e7910 /lib64/ld-2.12.90.so
> 0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e7910 /lib64/ld-2.12.90.so
> 0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e7936 /lib64/ld-2.12.90.so
> 0xffffffff8146fe0e /lib/modules/2.6.37-rc2-tip+/build/vmlinux => 0x00007fd4038e793d /lib64/ld-2.12.90.so
> 0x00007fd4038e7981 /lib64/ld-2.12.90.so => 0x00007fd4038e79a3 /lib64/ld-2.12.90.so
> 0x00007fd4038e79a7 /lib64/ld-2.12.90.so => 0x00007fd4038e7988 /lib64/ld-2.12.90.so
> ...
>
> Signed-off-by: Akihiro Nagai <[email protected]>
Looks good to me :)
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/Documentation/perf-bts.txt | 3 +++
> tools/perf/builtin-bts.c | 31 +++++++++++++++++++++++++++++++
> 2 files changed, 34 insertions(+), 0 deletions(-)
>
> diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
> index 56ddaa4..acabffc 100644
> --- a/tools/perf/Documentation/perf-bts.txt
> +++ b/tools/perf/Documentation/perf-bts.txt
> @@ -38,6 +38,9 @@ OPTIONS
> -p::
> --pid::
> Print pid.
> +-e::
> +--elfpath::
> + Print file path of executed elf.
>
> SEE ALSO
> --------
> diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
> index 11e491b..3b2a21e 100644
> --- a/tools/perf/builtin-bts.c
> +++ b/tools/perf/builtin-bts.c
> @@ -20,6 +20,7 @@ struct exec_info {
> u64 addr; /* recorded address by bts */
> pid_t pid; /* tracee process pid */
> const char *comm; /* command name */
> + const char *elfpath; /* file path to elf */
> };
>
> #define EI_PID_UNSET -1
> @@ -28,6 +29,7 @@ struct exec_info {
> #define EI_FLAG_PRINT_ADDR (1 << 0)
> #define EI_FLAG_PRINT_PID (1 << 1)
> #define EI_FLAG_PRINT_COMM (1 << 2)
> +#define EI_FLAG_PRINT_ELFPATH (1 << 3)
>
> /* it's used when no print item specified */
> #define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_ADDR
> @@ -77,6 +79,9 @@ static const struct option bts_options[] = {
> OPT_CALLBACK_DEFAULT_NOOPT('c', "comm", NULL, NULL,
> "print command name", set_print_flags,
> (void *)EI_FLAG_PRINT_COMM),
> + OPT_CALLBACK_DEFAULT_NOOPT('e', "elfpath", NULL, NULL,
> + "print file path to elf", set_print_flags,
> + (void *)EI_FLAG_PRINT_ELFPATH),
> OPT_END()
> };
>
> @@ -91,6 +96,7 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
> event_t *event, u64 addr)
> {
> struct thread *thread;
> + struct addr_location al;
>
> ei->addr = addr;
> ei->pid = event->ip.pid;
> @@ -99,12 +105,25 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
> if (!thread)
> return;
> ei->comm = thread->comm;
> +
> + /* get file path to elf */
> + memset(&al, 0, sizeof(al));
> + thread__find_addr_map(thread, session, PERF_RECORD_MISC_USER,
> + MAP__FUNCTION, event->ip.pid, addr, &al);
> + if (!al.map)
> + thread__find_addr_map(thread, session, PERF_RECORD_MISC_KERNEL,
> + MAP__FUNCTION, event->ip.pid, addr, &al);
> + if (!al.map)
> + return;
> + map__load(al.map, NULL);
> + ei->elfpath = al.map->dso->long_name;
> }
>
> static void __print_exec_info(struct exec_info *ei)
> {
> char pid[16];
> const char *comm;
> + const char *elfpath;
>
> if (print_flags & EI_FLAG_PRINT_PID) {
> if (ei->pid == EI_PID_UNSET)
> @@ -119,6 +138,10 @@ static void __print_exec_info(struct exec_info *ei)
> }
> if (print_flags & EI_FLAG_PRINT_ADDR)
> printf(FMT_ADDR " ", ei->addr);
> + if (print_flags & EI_FLAG_PRINT_ELFPATH) {
> + elfpath = ei->elfpath ? : EI_UNKNOWN_TEXT;
> + printf("%-32s ", elfpath);
> + }
> }
>
> static void print_exec_info(struct exec_info *ei_from, struct exec_info *ei_to)
> @@ -137,6 +160,8 @@ static void print_exec_info_header(void)
> printf("%-12s ", "command");
> if (print_flags & EI_FLAG_PRINT_ADDR)
> printf("%-" FMT_ADDR_WIDTH "s ", "address");
> + if (print_flags & EI_FLAG_PRINT_ELFPATH)
> + printf("%-32s ", "elf_filepath");
> printf("\n");
> }
>
> @@ -163,6 +188,7 @@ static int process_sample_event(event_t *event, struct perf_session *session)
> static struct perf_event_ops event_ops = {
> .sample = process_sample_event,
> .comm = event__process_comm,
> + .mmap = event__process_mmap,
> .ordered_samples = false,
> };
>
> @@ -180,6 +206,11 @@ static int __cmd_trace(void)
> if (is_flags_unset(print_flags))
> print_flags = EI_FLAG_PRINT_DEFAULT;
>
> + /* setup kernel maps to resolve vmlinux file path */
> + perf_session__create_kernel_maps(session);
> + if (symbol__init() < 0)
> + fprintf(stderr, "failed to initialize symbol.\n");
> +
> setup_pager();
> print_exec_info_header();
> perf_session__process_events(session, &event_ops);
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
(2010/12/03 13:00), Akihiro Nagai wrote:
> Provide the function to print function+offset.
> And, set it as the default behavior of 'perf bts trace'.
> To use this function, users can also specify the option '-s' or '--symbol'.
>
> Example: 'perf bts -as trace'
> This command prints address and function+offset.
>
> Output sample:
> address function+offset
> 0xffffffff8146fe0e irq_return+0x0 => 0x00007fd4038e3b20 _start+0x0
> ...
> 0x000000380661ee79 __libc_start_main+0xf9 => 0x00000000004035c3 main+0x0
> 0xffffffff8146ef4e irq_return+0x0 => 0x00000000004035c3 main+0x0
> 0x00000000004035e8 main+0x25 => 0x000000000040bca0 set_program_name+0x0
> 0xffffffff8146ef4e irq_return+0x0 => 0x000000000040bca0 set_program_name+0x0
> 0x000000000040bcae set_program_name+0xe => 0x00000000004023d0 strrchr@plt+0x0
> 0x00000000004023d0 strrchr@plt+0x0 => 0x00000000004023d6 strrchr@plt+0x6
> ...
> 0x0000000000403e0c main+0x849 => 0x00000000004021f0 exit@plt+0x0
> 0x00000000004021f0 exit@plt+0x0 => 0x00000000004021f6 exit@plt+0x6
> 0x00000000004021fb exit@plt+0xb => 0x00000000004020d0 _init+0x18
> 0x00000000004020d6 _init+0x1e => 0x00000038062149d0 _dl_runtime_resolve+0x0
> ...
>
> Signed-off-by: Akihiro Nagai <[email protected]>
You might think the next step is displaying line number of the address.
Feel free to use/update probe-finder.c :)
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/Documentation/perf-bts.txt | 5 +++-
> tools/perf/builtin-bts.c | 44 ++++++++++++++++++++++++++++++---
> 2 files changed, 44 insertions(+), 5 deletions(-)
>
> diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
> index acabffc..13ee862 100644
> --- a/tools/perf/Documentation/perf-bts.txt
> +++ b/tools/perf/Documentation/perf-bts.txt
> @@ -31,7 +31,7 @@ OPTIONS
> Specify input file name to analyze.
> -a::
> --addr::
> - Print address. (default)
> + Print address.
> -c::
> --comm::
> Print command name.
> @@ -41,6 +41,9 @@ OPTIONS
> -e::
> --elfpath::
> Print file path of executed elf.
> +-s::
> +--symbol::
> + Print function name and offset. (default)
>
> SEE ALSO
> --------
> diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
> index 3b2a21e..71c7fbe 100644
> --- a/tools/perf/builtin-bts.c
> +++ b/tools/perf/builtin-bts.c
> @@ -21,6 +21,8 @@ struct exec_info {
> pid_t pid; /* tracee process pid */
> const char *comm; /* command name */
> const char *elfpath; /* file path to elf */
> + const char *function; /* function name */
> + u64 offset; /* offset from top of the function */
> };
>
> #define EI_PID_UNSET -1
> @@ -30,9 +32,10 @@ struct exec_info {
> #define EI_FLAG_PRINT_PID (1 << 1)
> #define EI_FLAG_PRINT_COMM (1 << 2)
> #define EI_FLAG_PRINT_ELFPATH (1 << 3)
> +#define EI_FLAG_PRINT_SYMBOL (1 << 4)
>
> /* it's used when no print item specified */
> -#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_ADDR
> +#define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_SYMBOL
>
> /* print item flags */
> static unsigned long print_flags;
> @@ -43,6 +46,9 @@ static unsigned long print_flags;
> #define EI_UNKNOWN_TEXT "(unknown)"
> #define EI_UNKNOWN_TEXT_LEN (sizeof(EI_UNKNOWN_TEXT))
>
> +/* 'function+offset' lengh = function + '+' + %#18llx + '\0' */
> +#define symbol_buf_size(func_name) (strlen(func_name) + 1 + 18 + 1)
> +
> /* default input file name to analyze */
> static const char *input_name = "perf.data";
>
> @@ -71,7 +77,7 @@ static int set_print_flags(const struct option *opt, const char *str __unused,
> static const struct option bts_options[] = {
> OPT_STRING('i', "input", &input_name, "file", "input file name"),
> OPT_CALLBACK_DEFAULT_NOOPT('a', "addr", NULL, NULL,
> - "print address (default)", set_print_flags,
> + "print address", set_print_flags,
> (void *)EI_FLAG_PRINT_ADDR),
> OPT_CALLBACK_DEFAULT_NOOPT('p', "pid", NULL, NULL,
> "print pid", set_print_flags,
> @@ -82,6 +88,10 @@ static const struct option bts_options[] = {
> OPT_CALLBACK_DEFAULT_NOOPT('e', "elfpath", NULL, NULL,
> "print file path to elf", set_print_flags,
> (void *)EI_FLAG_PRINT_ELFPATH),
> + OPT_CALLBACK_DEFAULT_NOOPT('s', "symbol", NULL, NULL,
> + "print function+offset (default)",
> + set_print_flags,
> + (void *)EI_FLAG_PRINT_SYMBOL),
> OPT_END()
> };
>
> @@ -106,7 +116,7 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
> return;
> ei->comm = thread->comm;
>
> - /* get file path to elf */
> + /* get file path to elf, and symbol information */
> memset(&al, 0, sizeof(al));
> thread__find_addr_map(thread, session, PERF_RECORD_MISC_USER,
> MAP__FUNCTION, event->ip.pid, addr, &al);
> @@ -115,8 +125,14 @@ static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
> MAP__FUNCTION, event->ip.pid, addr, &al);
> if (!al.map)
> return;
> - map__load(al.map, NULL);
> +
> + al.addr = al.map->map_ip(al.map, addr);
> + al.sym = map__find_symbol(al.map, al.addr, NULL);
> + if (!al.sym)
> + return;
> ei->elfpath = al.map->dso->long_name;
> + ei->function = al.sym->name;
> + ei->offset = al.addr - al.sym->start;
> }
>
> static void __print_exec_info(struct exec_info *ei)
> @@ -124,6 +140,8 @@ static void __print_exec_info(struct exec_info *ei)
> char pid[16];
> const char *comm;
> const char *elfpath;
> + char *symbol;
> + int symbol_len;
>
> if (print_flags & EI_FLAG_PRINT_PID) {
> if (ei->pid == EI_PID_UNSET)
> @@ -138,6 +156,22 @@ static void __print_exec_info(struct exec_info *ei)
> }
> if (print_flags & EI_FLAG_PRINT_ADDR)
> printf(FMT_ADDR " ", ei->addr);
> + if (print_flags & EI_FLAG_PRINT_SYMBOL) {
> + if (!ei->function) {
> + /* when function is unknown, offset must be unknown */
> + printf("%-32s ", EI_UNKNOWN_TEXT);
> + goto print_elfpath;
> + }
> +
> + symbol_len = symbol_buf_size(ei->function);
> + symbol = malloc(symbol_len);
> + snprintf(symbol, symbol_len, "%s+0x%llx",
> + ei->function, ei->offset);
> + printf("%-32s ", symbol);
> + free(symbol);
> + }
> +
> +print_elfpath:
> if (print_flags & EI_FLAG_PRINT_ELFPATH) {
> elfpath = ei->elfpath ? : EI_UNKNOWN_TEXT;
> printf("%-32s ", elfpath);
> @@ -160,6 +194,8 @@ static void print_exec_info_header(void)
> printf("%-12s ", "command");
> if (print_flags & EI_FLAG_PRINT_ADDR)
> printf("%-" FMT_ADDR_WIDTH "s ", "address");
> + if (print_flags & EI_FLAG_PRINT_SYMBOL)
> + printf("%-32s ", "function+offset");
> if (print_flags & EI_FLAG_PRINT_ELFPATH)
> printf("%-32s ", "elf_filepath");
> printf("\n");
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
(2010/12/03 13:00), Akihiro Nagai wrote:
> For ease of use, add option printing all information '-A' or '--all'.
> This option can print following information.
> - pid
> - command name
> - address
> - function+offset
> - elf file path
>
> Signed-off-by: Akihiro Nagai <[email protected]>
Looks good to me :)
Reviewed-by: Masami Hiramatsu <[email protected]>
> Cc: Masami Hiramatsu <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Paul Mackerras <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: [email protected]
> ---
>
> tools/perf/Documentation/perf-bts.txt | 3 +++
> tools/perf/builtin-bts.c | 6 ++++++
> 2 files changed, 9 insertions(+), 0 deletions(-)
>
> diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
> index 13ee862..c598dd2 100644
> --- a/tools/perf/Documentation/perf-bts.txt
> +++ b/tools/perf/Documentation/perf-bts.txt
> @@ -44,6 +44,9 @@ OPTIONS
> -s::
> --symbol::
> Print function name and offset. (default)
> +-A::
> +--all::
> + Print all information.
>
> SEE ALSO
> --------
> diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
> index 71c7fbe..c359f70 100644
> --- a/tools/perf/builtin-bts.c
> +++ b/tools/perf/builtin-bts.c
> @@ -34,6 +34,9 @@ struct exec_info {
> #define EI_FLAG_PRINT_ELFPATH (1 << 3)
> #define EI_FLAG_PRINT_SYMBOL (1 << 4)
>
> +/* all print flags are enabled */
> +#define EI_FLAG_PRINT_ALL -1UL
> +
> /* it's used when no print item specified */
> #define EI_FLAG_PRINT_DEFAULT EI_FLAG_PRINT_SYMBOL
>
> @@ -92,6 +95,9 @@ static const struct option bts_options[] = {
> "print function+offset (default)",
> set_print_flags,
> (void *)EI_FLAG_PRINT_SYMBOL),
> + OPT_CALLBACK_DEFAULT_NOOPT('A', "all", NULL, NULL,
> + "print all items", set_print_flags,
> + (void *)EI_FLAG_PRINT_ALL),
> OPT_END()
> };
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
(2010/12/03 22:00), Peter Zijlstra wrote:
> On Fri, 2010-12-03 at 13:00 +0900, Akihiro Nagai wrote:
>> Provide the function to print function+offset.
>> And, set it as the default behavior of 'perf bts trace'.
>> To use this function, users can also specify the option '-s' or '--symbol'.
>>
>> Example: 'perf bts -as trace'
>> This command prints address and function+offset.
>>
>> Output sample:
>> address function+offset
>> 0xffffffff8146fe0e irq_return+0x0 => 0x00007fd4038e3b20 _start+0x0
>> ...
>> 0x000000380661ee79 __libc_start_main+0xf9 => 0x00000000004035c3 main+0x0
>> 0xffffffff8146ef4e irq_return+0x0 => 0x00000000004035c3 main+0x0
>> 0x00000000004035e8 main+0x25 => 0x000000000040bca0 set_program_name+0x0
>> 0xffffffff8146ef4e irq_return+0x0 => 0x000000000040bca0 set_program_name+0x0
>> 0x000000000040bcae set_program_name+0xe => 0x00000000004023d0 strrchr@plt+0x0
>> 0x00000000004023d0 strrchr@plt+0x0 => 0x00000000004023d6 strrchr@plt+0x6
>> ...
>> 0x0000000000403e0c main+0x849 => 0x00000000004021f0 exit@plt+0x0
>> 0x00000000004021f0 exit@plt+0x0 => 0x00000000004021f6 exit@plt+0x6
>> 0x00000000004021fb exit@plt+0xb => 0x00000000004020d0 _init+0x18
>> 0x00000000004020d6 _init+0x1e => 0x00000038062149d0 _dl_runtime_resolve+0x0
>
>
> What would be very nice output is something like a source-code browser
> (tig might have something worth borrowing) and annotating the output
> using the bts output.
>
> You could for example darken the code that didn't get any coverage, and
> print a percentage for each conditional statement.
>
> That way you can easily see how your code is traversed.
We have developed the coverage test tool and execution path analyzer using BTS
called "Btrax". It can show the executed/unexecuted source codes with color.
Please refer this slide about details of Btrax.
http://lca2009.linux.org.au/slides/52.pdf
The purpose of 'perf bts' is to implement similar functions to Btrax.
And also, I'll try to reduce memory copies from kernel space to user,
because perf copies 3 times. But btrax does just one copy.
Please see the following benchmark results.
Test program: compile with -O0 option this source code
int main(void)
{
int i;
while(i++ < 100000000);
return 0;
}
Execution Results:
Native:
# time ./a.out
real 0m0.349s
user 0m0.346s
sys 0m0.002s
Btrax:
(enabling BTS_OFF_OS flag for this benchmark)
(kernel 2.6.30 on RHEL 5.4)
# bt_collect_log -d log -c ./a.out
real 1m19.950s
user 0m14.783s
sys 0m40.655s
(trace data size: 1,537,307,064 bytes)
perf:
# time perf bts record ./a.out
Processed 0 events and LOST 5002!
Check IO/CPU overload!
# perf record: Captured and wrote 2704.231 MB perf.data (~118149662 samples)
real 2m1.885s
user 0m7.184s
sys 0m19.194s
Bench Mark Enviroment:
Native and perf: latest -tip kernel on Fedora14
Btrax: vanilla 2.6.30 on RHEL5.4
Core2Duo E6550 @2.33GHz
Mem DDR2 4GB
best regards,
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>
(2010/12/06 11:42), Frederic Weisbecker wrote:
> On Fri, Dec 03, 2010 at 01:00:15PM +0900, Akihiro Nagai wrote:
>> Provide the function to print function+offset.
>> And, set it as the default behavior of 'perf bts trace'.
>> To use this function, users can also specify the option '-s' or '--symbol'.
>>
>> Example: 'perf bts -as trace'
>> This command prints address and function+offset.
>>
>> Output sample:
>> address function+offset
>> 0xffffffff8146fe0e irq_return+0x0 => 0x00007fd4038e3b20 _start+0x0
>> ...
>> 0x000000380661ee79 __libc_start_main+0xf9 => 0x00000000004035c3 main+0x0
>> 0xffffffff8146ef4e irq_return+0x0 => 0x00000000004035c3 main+0x0
>> 0x00000000004035e8 main+0x25 => 0x000000000040bca0 set_program_name+0x0
>> 0xffffffff8146ef4e irq_return+0x0 => 0x000000000040bca0 set_program_name+0x0
>> 0x000000000040bcae set_program_name+0xe => 0x00000000004023d0 strrchr@plt+0x0
>> 0x00000000004023d0 strrchr@plt+0x0 => 0x00000000004023d6 strrchr@plt+0x6
>> ...
>> 0x0000000000403e0c main+0x849 => 0x00000000004021f0 exit@plt+0x0
>> 0x00000000004021f0 exit@plt+0x0 => 0x00000000004021f6 exit@plt+0x6
>> 0x00000000004021fb exit@plt+0xb => 0x00000000004020d0 _init+0x18
>> 0x00000000004020d6 _init+0x1e => 0x00000038062149d0 _dl_runtime_resolve+0x0
>> ...
>
> There is another kind of mode that I suspect would be very useful: something like
> a userspace function graph tracer (you can have a look into the kernel function
> graph tracer we have in ftrace to get an overview).
>
> So, the idea would be to rebuild the whole function call flow:
>
> main() {
> func1()
> func2() {
> func3()
> }
> }
>
>
> This would require to deref the instructions into the dso addresses from
> the bts trace, keep only the "call" and the "ret" (there would be a small arch
> backend for this mode) and rebuild the whole tree of calls.
> We already have all the dso address mapping API in place with perf.
>
> I really think this could be a very useful tool. Also we can even later
> expand this to the branches, as Peter suggested. But starting with calls
> would a very great start already.
>
> Thanks.
It's very good idea!
I'll try it.
Thank you for your suggestion!
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
Commit-ID: e4e18d568b0e833c75c1f7833e1690cdde8f4d76
Gitweb: http://git.kernel.org/tip/e4e18d568b0e833c75c1f7833e1690cdde8f4d76
Author: Akihiro Nagai <[email protected]>
AuthorDate: Fri, 3 Dec 2010 12:58:53 +0900
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 6 Dec 2010 15:33:29 -0200
perf options: add OPT_CALLBACK_DEFAULT_NOOPT
Add new macro OPT_CALLBACK_DEFAULT_NOOPT for parse_options.
It enables to pass the default value (opt->defval) to the callback function
processing options require no argument.
Reviewed-by: Masami Hiramatsu <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>
Signed-off-by: Akihiro Nagai <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/parse-options.h | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index c7d72dc..abc31a1 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -119,6 +119,10 @@ struct option {
{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT }
+#define OPT_CALLBACK_DEFAULT_NOOPT(s, l, v, a, h, f, d) \
+ { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
+ .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d,\
+ .flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOARG}
/* parse_options() will filter out the processed options and leave the
* non-option argments in argv[].