2019-05-29 14:18:02 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2014-10-30 14:09:42 +00:00
|
|
|
/*
|
|
|
|
* thread-stack.c: Synthesize a thread's stack using call / return events
|
|
|
|
* Copyright (c) 2014, Intel Corporation.
|
|
|
|
*/
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
#include <linux/rbtree.h>
|
|
|
|
#include <linux/list.h>
|
2018-12-21 12:06:19 +00:00
|
|
|
#include <linux/log2.h>
|
2019-07-04 14:32:27 +00:00
|
|
|
#include <linux/zalloc.h>
|
2017-04-18 13:46:11 +00:00
|
|
|
#include <errno.h>
|
2019-07-04 14:21:24 +00:00
|
|
|
#include <stdlib.h>
|
2019-08-29 19:18:59 +00:00
|
|
|
#include <string.h>
|
2014-10-30 14:09:42 +00:00
|
|
|
#include "thread.h"
|
|
|
|
#include "event.h"
|
2014-10-30 14:09:45 +00:00
|
|
|
#include "machine.h"
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
#include "env.h"
|
2014-10-30 14:09:42 +00:00
|
|
|
#include "debug.h"
|
2014-10-30 14:09:45 +00:00
|
|
|
#include "symbol.h"
|
|
|
|
#include "comm.h"
|
2016-04-28 08:19:07 +00:00
|
|
|
#include "call-path.h"
|
2014-10-30 14:09:42 +00:00
|
|
|
#include "thread-stack.h"
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
#define STACK_GROWTH 2048
|
|
|
|
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
/*
|
|
|
|
* State of retpoline detection.
|
|
|
|
*
|
|
|
|
* RETPOLINE_NONE: no retpoline detection
|
|
|
|
* X86_RETPOLINE_POSSIBLE: x86 retpoline possible
|
|
|
|
* X86_RETPOLINE_DETECTED: x86 retpoline detected
|
|
|
|
*/
|
|
|
|
enum retpoline_state_t {
|
|
|
|
RETPOLINE_NONE,
|
|
|
|
X86_RETPOLINE_POSSIBLE,
|
|
|
|
X86_RETPOLINE_DETECTED,
|
|
|
|
};
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
/**
|
|
|
|
* struct thread_stack_entry - thread stack entry.
|
|
|
|
* @ret_addr: return address
|
|
|
|
* @timestamp: timestamp (if known)
|
|
|
|
* @ref: external reference (e.g. db_id of sample)
|
|
|
|
* @branch_count: the branch count when the entry was created
|
2019-05-20 11:37:19 +00:00
|
|
|
* @insn_count: the instruction count when the entry was created
|
|
|
|
* @cyc_count the cycle count when the entry was created
|
2019-02-28 13:00:24 +00:00
|
|
|
* @db_id: id used for db-export
|
2014-10-30 14:09:45 +00:00
|
|
|
* @cp: call path
|
|
|
|
* @no_call: a 'call' was not seen
|
2018-09-20 13:00:45 +00:00
|
|
|
* @trace_end: a 'call' but trace ended
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
* @non_call: a branch but not a 'call' to the start of a different symbol
|
2014-10-30 14:09:45 +00:00
|
|
|
*/
|
2014-10-30 14:09:42 +00:00
|
|
|
struct thread_stack_entry {
|
|
|
|
u64 ret_addr;
|
2014-10-30 14:09:45 +00:00
|
|
|
u64 timestamp;
|
|
|
|
u64 ref;
|
|
|
|
u64 branch_count;
|
2019-05-20 11:37:19 +00:00
|
|
|
u64 insn_count;
|
|
|
|
u64 cyc_count;
|
2019-02-28 13:00:24 +00:00
|
|
|
u64 db_id;
|
2014-10-30 14:09:45 +00:00
|
|
|
struct call_path *cp;
|
|
|
|
bool no_call;
|
2018-09-20 13:00:45 +00:00
|
|
|
bool trace_end;
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
bool non_call;
|
2014-10-30 14:09:42 +00:00
|
|
|
};
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
/**
|
|
|
|
* struct thread_stack - thread stack constructed from 'call' and 'return'
|
|
|
|
* branch samples.
|
|
|
|
* @stack: array that holds the stack
|
|
|
|
* @cnt: number of entries in the stack
|
|
|
|
* @sz: current maximum stack size
|
|
|
|
* @trace_nr: current trace number
|
|
|
|
* @branch_count: running branch count
|
2019-05-20 11:37:19 +00:00
|
|
|
* @insn_count: running instruction count
|
|
|
|
* @cyc_count running cycle count
|
2014-10-30 14:09:45 +00:00
|
|
|
* @kernel_start: kernel start address
|
|
|
|
* @last_time: last timestamp
|
|
|
|
* @crp: call/return processor
|
|
|
|
* @comm: current comm
|
2018-12-21 12:06:16 +00:00
|
|
|
* @arr_sz: size of array if this is the first element of an array
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
* @rstate: used to detect retpolines
|
2014-10-30 14:09:45 +00:00
|
|
|
*/
|
2014-10-30 14:09:42 +00:00
|
|
|
struct thread_stack {
|
|
|
|
struct thread_stack_entry *stack;
|
|
|
|
size_t cnt;
|
|
|
|
size_t sz;
|
|
|
|
u64 trace_nr;
|
2014-10-30 14:09:45 +00:00
|
|
|
u64 branch_count;
|
2019-05-20 11:37:19 +00:00
|
|
|
u64 insn_count;
|
|
|
|
u64 cyc_count;
|
2014-10-30 14:09:45 +00:00
|
|
|
u64 kernel_start;
|
|
|
|
u64 last_time;
|
|
|
|
struct call_return_processor *crp;
|
|
|
|
struct comm *comm;
|
2018-12-21 12:06:16 +00:00
|
|
|
unsigned int arr_sz;
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
enum retpoline_state_t rstate;
|
2014-10-30 14:09:42 +00:00
|
|
|
};
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
/*
|
|
|
|
* Assume pid == tid == 0 identifies the idle task as defined by
|
|
|
|
* perf_session__register_idle_thread(). The idle task is really 1 task per cpu,
|
|
|
|
* and therefore requires a stack for each cpu.
|
|
|
|
*/
|
|
|
|
static inline bool thread_stack__per_cpu(struct thread *thread)
|
|
|
|
{
|
|
|
|
return !(thread->tid || thread->pid_);
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:09:42 +00:00
|
|
|
static int thread_stack__grow(struct thread_stack *ts)
|
|
|
|
{
|
|
|
|
struct thread_stack_entry *new_stack;
|
|
|
|
size_t sz, new_sz;
|
|
|
|
|
|
|
|
new_sz = ts->sz + STACK_GROWTH;
|
|
|
|
sz = new_sz * sizeof(struct thread_stack_entry);
|
|
|
|
|
|
|
|
new_stack = realloc(ts->stack, sz);
|
|
|
|
if (!new_stack)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ts->stack = new_stack;
|
|
|
|
ts->sz = new_sz;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:17 +00:00
|
|
|
static int thread_stack__init(struct thread_stack *ts, struct thread *thread,
|
|
|
|
struct call_return_processor *crp)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = thread_stack__grow(ts);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2019-11-26 01:07:43 +00:00
|
|
|
if (thread->maps && thread->maps->machine) {
|
|
|
|
struct machine *machine = thread->maps->machine;
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
const char *arch = perf_env__arch(machine->env);
|
|
|
|
|
|
|
|
ts->kernel_start = machine__kernel_start(machine);
|
|
|
|
if (!strcmp(arch, "x86"))
|
|
|
|
ts->rstate = X86_RETPOLINE_POSSIBLE;
|
|
|
|
} else {
|
2018-12-21 12:06:17 +00:00
|
|
|
ts->kernel_start = 1ULL << 63;
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
}
|
2018-12-21 12:06:17 +00:00
|
|
|
ts->crp = crp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
static struct thread_stack *thread_stack__new(struct thread *thread, int cpu,
|
2014-10-30 14:09:45 +00:00
|
|
|
struct call_return_processor *crp)
|
2014-10-30 14:09:42 +00:00
|
|
|
{
|
2018-12-21 12:06:18 +00:00
|
|
|
struct thread_stack *ts = thread->ts, *new_ts;
|
|
|
|
unsigned int old_sz = ts ? ts->arr_sz : 0;
|
|
|
|
unsigned int new_sz = 1;
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
if (thread_stack__per_cpu(thread) && cpu > 0)
|
|
|
|
new_sz = roundup_pow_of_two(cpu + 1);
|
|
|
|
|
2018-12-21 12:06:18 +00:00
|
|
|
if (!ts || new_sz > old_sz) {
|
|
|
|
new_ts = calloc(new_sz, sizeof(*ts));
|
|
|
|
if (!new_ts)
|
|
|
|
return NULL;
|
|
|
|
if (ts)
|
|
|
|
memcpy(new_ts, ts, old_sz * sizeof(*ts));
|
|
|
|
new_ts->arr_sz = new_sz;
|
|
|
|
zfree(&thread->ts);
|
|
|
|
thread->ts = new_ts;
|
|
|
|
ts = new_ts;
|
2014-10-30 14:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
if (thread_stack__per_cpu(thread) && cpu > 0 &&
|
|
|
|
(unsigned int)cpu < ts->arr_sz)
|
|
|
|
ts += cpu;
|
|
|
|
|
2018-12-21 12:06:18 +00:00
|
|
|
if (!ts->stack &&
|
|
|
|
thread_stack__init(ts, thread, crp))
|
|
|
|
return NULL;
|
2018-12-21 12:06:15 +00:00
|
|
|
|
2014-10-30 14:09:42 +00:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
static struct thread_stack *thread__cpu_stack(struct thread *thread, int cpu)
|
2018-12-21 12:06:15 +00:00
|
|
|
{
|
2018-12-21 12:06:19 +00:00
|
|
|
struct thread_stack *ts = thread->ts;
|
|
|
|
|
|
|
|
if (cpu < 0)
|
|
|
|
cpu = 0;
|
|
|
|
|
|
|
|
if (!ts || (unsigned int)cpu >= ts->arr_sz)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ts += cpu;
|
|
|
|
|
|
|
|
if (!ts->stack)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct thread_stack *thread__stack(struct thread *thread,
|
|
|
|
int cpu)
|
|
|
|
{
|
|
|
|
if (!thread)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (thread_stack__per_cpu(thread))
|
|
|
|
return thread__cpu_stack(thread, cpu);
|
|
|
|
|
|
|
|
return thread->ts;
|
2018-12-21 12:06:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 13:00:45 +00:00
|
|
|
static int thread_stack__push(struct thread_stack *ts, u64 ret_addr,
|
|
|
|
bool trace_end)
|
2014-10-30 14:09:42 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (ts->cnt == ts->sz) {
|
|
|
|
err = thread_stack__grow(ts);
|
|
|
|
if (err) {
|
|
|
|
pr_warning("Out of memory: discarding thread stack\n");
|
|
|
|
ts->cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:00:45 +00:00
|
|
|
ts->stack[ts->cnt].trace_end = trace_end;
|
2014-10-30 14:09:42 +00:00
|
|
|
ts->stack[ts->cnt++].ret_addr = ret_addr;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_stack__pop(struct thread_stack *ts, u64 ret_addr)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In some cases there may be functions which are not seen to return.
|
|
|
|
* For example when setjmp / longjmp has been used. Or the perf context
|
|
|
|
* switch in the kernel which doesn't stop and start tracing in exactly
|
|
|
|
* the same code path. When that happens the return address will be
|
|
|
|
* further down the stack. If the return address is not found at all,
|
|
|
|
* we assume the opposite (i.e. this is a return for a call that wasn't
|
|
|
|
* seen for some reason) and leave the stack alone.
|
|
|
|
*/
|
|
|
|
for (i = ts->cnt; i; ) {
|
|
|
|
if (ts->stack[--i].ret_addr == ret_addr) {
|
|
|
|
ts->cnt = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:00:45 +00:00
|
|
|
static void thread_stack__pop_trace_end(struct thread_stack *ts)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = ts->cnt; i; ) {
|
|
|
|
if (ts->stack[--i].trace_end)
|
|
|
|
ts->cnt = i;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
static bool thread_stack__in_kernel(struct thread_stack *ts)
|
|
|
|
{
|
|
|
|
if (!ts->cnt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ts->stack[ts->cnt - 1].cp->in_kernel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_stack__call_return(struct thread *thread,
|
|
|
|
struct thread_stack *ts, size_t idx,
|
|
|
|
u64 timestamp, u64 ref, bool no_return)
|
|
|
|
{
|
|
|
|
struct call_return_processor *crp = ts->crp;
|
|
|
|
struct thread_stack_entry *tse;
|
|
|
|
struct call_return cr = {
|
|
|
|
.thread = thread,
|
|
|
|
.comm = ts->comm,
|
|
|
|
.db_id = 0,
|
|
|
|
};
|
2019-02-28 13:00:24 +00:00
|
|
|
u64 *parent_db_id;
|
2014-10-30 14:09:45 +00:00
|
|
|
|
|
|
|
tse = &ts->stack[idx];
|
|
|
|
cr.cp = tse->cp;
|
|
|
|
cr.call_time = tse->timestamp;
|
|
|
|
cr.return_time = timestamp;
|
|
|
|
cr.branch_count = ts->branch_count - tse->branch_count;
|
2019-05-20 11:37:19 +00:00
|
|
|
cr.insn_count = ts->insn_count - tse->insn_count;
|
|
|
|
cr.cyc_count = ts->cyc_count - tse->cyc_count;
|
2019-02-28 13:00:24 +00:00
|
|
|
cr.db_id = tse->db_id;
|
2014-10-30 14:09:45 +00:00
|
|
|
cr.call_ref = tse->ref;
|
|
|
|
cr.return_ref = ref;
|
|
|
|
if (tse->no_call)
|
|
|
|
cr.flags |= CALL_RETURN_NO_CALL;
|
|
|
|
if (no_return)
|
|
|
|
cr.flags |= CALL_RETURN_NO_RETURN;
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
if (tse->non_call)
|
|
|
|
cr.flags |= CALL_RETURN_NON_CALL;
|
2014-10-30 14:09:45 +00:00
|
|
|
|
2019-02-28 13:00:24 +00:00
|
|
|
/*
|
|
|
|
* The parent db_id must be assigned before exporting the child. Note
|
|
|
|
* it is not possible to export the parent first because its information
|
|
|
|
* is not yet complete because its 'return' has not yet been processed.
|
|
|
|
*/
|
|
|
|
parent_db_id = idx ? &(tse - 1)->db_id : NULL;
|
|
|
|
|
|
|
|
return crp->process(&cr, parent_db_id, crp->data);
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 13:33:30 +00:00
|
|
|
static int __thread_stack__flush(struct thread *thread, struct thread_stack *ts)
|
2014-10-30 14:09:45 +00:00
|
|
|
{
|
|
|
|
struct call_return_processor *crp = ts->crp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!crp) {
|
|
|
|
ts->cnt = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ts->cnt) {
|
|
|
|
err = thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
ts->last_time, 0, true);
|
|
|
|
if (err) {
|
|
|
|
pr_err("Error flushing thread stack!\n");
|
|
|
|
ts->cnt = 0;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-29 13:33:30 +00:00
|
|
|
int thread_stack__flush(struct thread *thread)
|
|
|
|
{
|
2018-12-21 12:06:15 +00:00
|
|
|
struct thread_stack *ts = thread->ts;
|
2018-12-21 12:06:16 +00:00
|
|
|
unsigned int pos;
|
|
|
|
int err = 0;
|
2018-12-21 12:06:15 +00:00
|
|
|
|
2018-12-21 12:06:16 +00:00
|
|
|
if (ts) {
|
|
|
|
for (pos = 0; pos < ts->arr_sz; pos++) {
|
|
|
|
int ret = __thread_stack__flush(thread, ts + pos);
|
2015-05-29 13:33:30 +00:00
|
|
|
|
2018-12-21 12:06:16 +00:00
|
|
|
if (ret)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2015-05-29 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
int thread_stack__event(struct thread *thread, int cpu, u32 flags, u64 from_ip,
|
2014-10-30 14:09:42 +00:00
|
|
|
u64 to_ip, u16 insn_len, u64 trace_nr)
|
|
|
|
{
|
2018-12-21 12:06:19 +00:00
|
|
|
struct thread_stack *ts = thread__stack(thread, cpu);
|
2018-12-21 12:06:15 +00:00
|
|
|
|
2014-10-30 14:09:42 +00:00
|
|
|
if (!thread)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-21 12:06:15 +00:00
|
|
|
if (!ts) {
|
2018-12-21 12:06:19 +00:00
|
|
|
ts = thread_stack__new(thread, cpu, NULL);
|
2018-12-21 12:06:15 +00:00
|
|
|
if (!ts) {
|
2014-10-30 14:09:42 +00:00
|
|
|
pr_warning("Out of memory: no thread stack\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2018-12-21 12:06:15 +00:00
|
|
|
ts->trace_nr = trace_nr;
|
2014-10-30 14:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When the trace is discontinuous, the trace_nr changes. In that case
|
|
|
|
* the stack might be completely invalid. Better to report nothing than
|
2014-10-30 14:09:45 +00:00
|
|
|
* to report something misleading, so flush the stack.
|
2014-10-30 14:09:42 +00:00
|
|
|
*/
|
2018-12-21 12:06:15 +00:00
|
|
|
if (trace_nr != ts->trace_nr) {
|
|
|
|
if (ts->trace_nr)
|
|
|
|
__thread_stack__flush(thread, ts);
|
|
|
|
ts->trace_nr = trace_nr;
|
2014-10-30 14:09:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
/* Stop here if thread_stack__process() is in use */
|
2018-12-21 12:06:15 +00:00
|
|
|
if (ts->crp)
|
2014-10-30 14:09:45 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-10-30 14:09:42 +00:00
|
|
|
if (flags & PERF_IP_FLAG_CALL) {
|
|
|
|
u64 ret_addr;
|
|
|
|
|
|
|
|
if (!to_ip)
|
|
|
|
return 0;
|
|
|
|
ret_addr = from_ip + insn_len;
|
|
|
|
if (ret_addr == to_ip)
|
|
|
|
return 0; /* Zero-length calls are excluded */
|
2018-12-21 12:06:15 +00:00
|
|
|
return thread_stack__push(ts, ret_addr,
|
2018-09-20 13:00:45 +00:00
|
|
|
flags & PERF_IP_FLAG_TRACE_END);
|
|
|
|
} else if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
|
|
|
|
/*
|
|
|
|
* If the caller did not change the trace number (which would
|
|
|
|
* have flushed the stack) then try to make sense of the stack.
|
|
|
|
* Possibly, tracing began after returning to the current
|
|
|
|
* address, so try to pop that. Also, do not expect a call made
|
|
|
|
* when the trace ended, to return, so pop that.
|
|
|
|
*/
|
2018-12-21 12:06:15 +00:00
|
|
|
thread_stack__pop(ts, to_ip);
|
|
|
|
thread_stack__pop_trace_end(ts);
|
2018-09-20 13:00:45 +00:00
|
|
|
} else if ((flags & PERF_IP_FLAG_RETURN) && from_ip) {
|
2018-12-21 12:06:15 +00:00
|
|
|
thread_stack__pop(ts, to_ip);
|
2014-10-30 14:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
void thread_stack__set_trace_nr(struct thread *thread, int cpu, u64 trace_nr)
|
2014-10-30 14:09:45 +00:00
|
|
|
{
|
2018-12-21 12:06:19 +00:00
|
|
|
struct thread_stack *ts = thread__stack(thread, cpu);
|
2018-12-21 12:06:15 +00:00
|
|
|
|
|
|
|
if (!ts)
|
2014-10-30 14:09:45 +00:00
|
|
|
return;
|
|
|
|
|
2018-12-21 12:06:15 +00:00
|
|
|
if (trace_nr != ts->trace_nr) {
|
|
|
|
if (ts->trace_nr)
|
|
|
|
__thread_stack__flush(thread, ts);
|
|
|
|
ts->trace_nr = trace_nr;
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:16 +00:00
|
|
|
static void __thread_stack__free(struct thread *thread, struct thread_stack *ts)
|
|
|
|
{
|
|
|
|
__thread_stack__flush(thread, ts);
|
|
|
|
zfree(&ts->stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_stack__reset(struct thread *thread, struct thread_stack *ts)
|
|
|
|
{
|
|
|
|
unsigned int arr_sz = ts->arr_sz;
|
|
|
|
|
|
|
|
__thread_stack__free(thread, ts);
|
|
|
|
memset(ts, 0, sizeof(*ts));
|
|
|
|
ts->arr_sz = arr_sz;
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:09:42 +00:00
|
|
|
void thread_stack__free(struct thread *thread)
|
|
|
|
{
|
2018-12-21 12:06:15 +00:00
|
|
|
struct thread_stack *ts = thread->ts;
|
2018-12-21 12:06:16 +00:00
|
|
|
unsigned int pos;
|
2018-12-21 12:06:15 +00:00
|
|
|
|
|
|
|
if (ts) {
|
2018-12-21 12:06:16 +00:00
|
|
|
for (pos = 0; pos < ts->arr_sz; pos++)
|
|
|
|
__thread_stack__free(thread, ts + pos);
|
2014-10-30 14:09:42 +00:00
|
|
|
zfree(&thread->ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 09:10:42 +00:00
|
|
|
static inline u64 callchain_context(u64 ip, u64 kernel_start)
|
|
|
|
{
|
|
|
|
return ip < kernel_start ? PERF_CONTEXT_USER : PERF_CONTEXT_KERNEL;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
void thread_stack__sample(struct thread *thread, int cpu,
|
|
|
|
struct ip_callchain *chain,
|
2018-10-31 09:10:42 +00:00
|
|
|
size_t sz, u64 ip, u64 kernel_start)
|
2014-10-30 14:09:42 +00:00
|
|
|
{
|
2018-12-21 12:06:19 +00:00
|
|
|
struct thread_stack *ts = thread__stack(thread, cpu);
|
2018-10-31 09:10:42 +00:00
|
|
|
u64 context = callchain_context(ip, kernel_start);
|
|
|
|
u64 last_context;
|
|
|
|
size_t i, j;
|
2014-10-30 14:09:42 +00:00
|
|
|
|
2018-10-31 09:10:42 +00:00
|
|
|
if (sz < 2) {
|
|
|
|
chain->nr = 0;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-30 14:09:42 +00:00
|
|
|
|
2018-10-31 09:10:42 +00:00
|
|
|
chain->ips[0] = context;
|
|
|
|
chain->ips[1] = ip;
|
|
|
|
|
2018-12-21 12:06:15 +00:00
|
|
|
if (!ts) {
|
2018-10-31 09:10:42 +00:00
|
|
|
chain->nr = 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_context = context;
|
|
|
|
|
2018-12-21 12:06:15 +00:00
|
|
|
for (i = 2, j = 1; i < sz && j <= ts->cnt; i++, j++) {
|
|
|
|
ip = ts->stack[ts->cnt - j].ret_addr;
|
2018-10-31 09:10:42 +00:00
|
|
|
context = callchain_context(ip, kernel_start);
|
|
|
|
if (context != last_context) {
|
|
|
|
if (i >= sz - 1)
|
|
|
|
break;
|
|
|
|
chain->ips[i++] = context;
|
|
|
|
last_context = context;
|
|
|
|
}
|
|
|
|
chain->ips[i] = ip;
|
|
|
|
}
|
2014-10-30 14:09:42 +00:00
|
|
|
|
2018-10-31 09:10:42 +00:00
|
|
|
chain->nr = i;
|
2014-10-30 14:09:42 +00:00
|
|
|
}
|
2014-10-30 14:09:45 +00:00
|
|
|
|
|
|
|
struct call_return_processor *
|
2019-02-28 13:00:24 +00:00
|
|
|
call_return_processor__new(int (*process)(struct call_return *cr, u64 *parent_db_id, void *data),
|
2014-10-30 14:09:45 +00:00
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct call_return_processor *crp;
|
|
|
|
|
|
|
|
crp = zalloc(sizeof(struct call_return_processor));
|
|
|
|
if (!crp)
|
|
|
|
return NULL;
|
|
|
|
crp->cpr = call_path_root__new();
|
|
|
|
if (!crp->cpr)
|
|
|
|
goto out_free;
|
|
|
|
crp->process = process;
|
|
|
|
crp->data = data;
|
|
|
|
return crp;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
free(crp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void call_return_processor__free(struct call_return_processor *crp)
|
|
|
|
{
|
|
|
|
if (crp) {
|
|
|
|
call_path_root__free(crp->cpr);
|
|
|
|
free(crp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_stack__push_cp(struct thread_stack *ts, u64 ret_addr,
|
|
|
|
u64 timestamp, u64 ref, struct call_path *cp,
|
2018-09-20 13:00:46 +00:00
|
|
|
bool no_call, bool trace_end)
|
2014-10-30 14:09:45 +00:00
|
|
|
{
|
|
|
|
struct thread_stack_entry *tse;
|
|
|
|
int err;
|
|
|
|
|
2019-01-09 09:18:31 +00:00
|
|
|
if (!cp)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
if (ts->cnt == ts->sz) {
|
|
|
|
err = thread_stack__grow(ts);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tse = &ts->stack[ts->cnt++];
|
|
|
|
tse->ret_addr = ret_addr;
|
|
|
|
tse->timestamp = timestamp;
|
|
|
|
tse->ref = ref;
|
|
|
|
tse->branch_count = ts->branch_count;
|
2019-05-20 11:37:19 +00:00
|
|
|
tse->insn_count = ts->insn_count;
|
|
|
|
tse->cyc_count = ts->cyc_count;
|
2014-10-30 14:09:45 +00:00
|
|
|
tse->cp = cp;
|
|
|
|
tse->no_call = no_call;
|
2018-09-20 13:00:46 +00:00
|
|
|
tse->trace_end = trace_end;
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
tse->non_call = false;
|
2019-02-28 13:00:24 +00:00
|
|
|
tse->db_id = 0;
|
2014-10-30 14:09:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_stack__pop_cp(struct thread *thread, struct thread_stack *ts,
|
|
|
|
u64 ret_addr, u64 timestamp, u64 ref,
|
|
|
|
struct symbol *sym)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!ts->cnt)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (ts->cnt == 1) {
|
|
|
|
struct thread_stack_entry *tse = &ts->stack[0];
|
|
|
|
|
|
|
|
if (tse->cp->sym == sym)
|
|
|
|
return thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
timestamp, ref, false);
|
|
|
|
}
|
|
|
|
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
if (ts->stack[ts->cnt - 1].ret_addr == ret_addr &&
|
|
|
|
!ts->stack[ts->cnt - 1].non_call) {
|
2014-10-30 14:09:45 +00:00
|
|
|
return thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
timestamp, ref, false);
|
|
|
|
} else {
|
|
|
|
size_t i = ts->cnt - 1;
|
|
|
|
|
|
|
|
while (i--) {
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
if (ts->stack[i].ret_addr != ret_addr ||
|
|
|
|
ts->stack[i].non_call)
|
2014-10-30 14:09:45 +00:00
|
|
|
continue;
|
|
|
|
i += 1;
|
|
|
|
while (ts->cnt > i) {
|
|
|
|
err = thread_stack__call_return(thread, ts,
|
|
|
|
--ts->cnt,
|
|
|
|
timestamp, ref,
|
|
|
|
true);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
timestamp, ref, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:06:14 +00:00
|
|
|
static int thread_stack__bottom(struct thread_stack *ts,
|
2014-10-30 14:09:45 +00:00
|
|
|
struct perf_sample *sample,
|
|
|
|
struct addr_location *from_al,
|
|
|
|
struct addr_location *to_al, u64 ref)
|
|
|
|
{
|
|
|
|
struct call_path_root *cpr = ts->crp->cpr;
|
|
|
|
struct call_path *cp;
|
|
|
|
struct symbol *sym;
|
|
|
|
u64 ip;
|
|
|
|
|
|
|
|
if (sample->ip) {
|
|
|
|
ip = sample->ip;
|
|
|
|
sym = from_al->sym;
|
|
|
|
} else if (sample->addr) {
|
|
|
|
ip = sample->addr;
|
|
|
|
sym = to_al->sym;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = call_path__findnew(cpr, &cpr->call_path, sym, ip,
|
|
|
|
ts->kernel_start);
|
|
|
|
|
2018-12-21 12:06:14 +00:00
|
|
|
return thread_stack__push_cp(ts, ip, sample->time, ref, cp,
|
2018-09-20 13:00:46 +00:00
|
|
|
true, false);
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 06:44:28 +00:00
|
|
|
static int thread_stack__pop_ks(struct thread *thread, struct thread_stack *ts,
|
|
|
|
struct perf_sample *sample, u64 ref)
|
|
|
|
{
|
|
|
|
u64 tm = sample->time;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Return to userspace, so pop all kernel addresses */
|
|
|
|
while (thread_stack__in_kernel(ts)) {
|
|
|
|
err = thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
tm, ref, true);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
static int thread_stack__no_call_return(struct thread *thread,
|
|
|
|
struct thread_stack *ts,
|
|
|
|
struct perf_sample *sample,
|
|
|
|
struct addr_location *from_al,
|
|
|
|
struct addr_location *to_al, u64 ref)
|
|
|
|
{
|
|
|
|
struct call_path_root *cpr = ts->crp->cpr;
|
2019-01-09 09:18:32 +00:00
|
|
|
struct call_path *root = &cpr->call_path;
|
|
|
|
struct symbol *fsym = from_al->sym;
|
|
|
|
struct symbol *tsym = to_al->sym;
|
2014-10-30 14:09:45 +00:00
|
|
|
struct call_path *cp, *parent;
|
|
|
|
u64 ks = ts->kernel_start;
|
2019-01-09 09:18:32 +00:00
|
|
|
u64 addr = sample->addr;
|
|
|
|
u64 tm = sample->time;
|
|
|
|
u64 ip = sample->ip;
|
2014-10-30 14:09:45 +00:00
|
|
|
int err;
|
|
|
|
|
2019-01-09 09:18:32 +00:00
|
|
|
if (ip >= ks && addr < ks) {
|
2014-10-30 14:09:45 +00:00
|
|
|
/* Return to userspace, so pop all kernel addresses */
|
2019-06-19 06:44:29 +00:00
|
|
|
err = thread_stack__pop_ks(thread, ts, sample, ref);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2014-10-30 14:09:45 +00:00
|
|
|
|
|
|
|
/* If the stack is empty, push the userspace address */
|
|
|
|
if (!ts->cnt) {
|
2019-01-09 09:18:32 +00:00
|
|
|
cp = call_path__findnew(cpr, root, tsym, addr, ks);
|
|
|
|
return thread_stack__push_cp(ts, 0, tm, ref, cp, true,
|
|
|
|
false);
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
2019-01-09 09:18:32 +00:00
|
|
|
} else if (thread_stack__in_kernel(ts) && ip < ks) {
|
2014-10-30 14:09:45 +00:00
|
|
|
/* Return to userspace, so pop all kernel addresses */
|
2019-06-19 06:44:29 +00:00
|
|
|
err = thread_stack__pop_ks(thread, ts, sample, ref);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ts->cnt)
|
|
|
|
parent = ts->stack[ts->cnt - 1].cp;
|
|
|
|
else
|
2019-01-09 09:18:32 +00:00
|
|
|
parent = root;
|
2014-10-30 14:09:45 +00:00
|
|
|
|
perf thread-stack: Improve thread_stack__no_call_return()
Improve thread_stack__no_call_return() to better handle 'returns' that
do not match the stack i.e. 'no call'. See code comments for details.
The example below shows how retpolines are affected:
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> bar
After:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
Committer testing:
Chose "Reports", Then "Context-Sensitive Call Graph" and then go on
expanding:
Before:
simple-retpolin
PID:PID
_start
_start
__libc_start_main
main
__x86_indirect_thunk_rax
__x86_indirect_thunk_rax
bar
After:
Remove the "simple.retpoline.db" file, run again the 'perf script' line
to regenerate the .db file and run the exported-sql-viewer.py again to
get the same all the way to 'main', then, from there, including 'main':
main
__x86_indirect_thunk_rax
__x86_indirect_thunk_rax
foo
bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-6-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:34 +00:00
|
|
|
if (parent->sym == from_al->sym) {
|
|
|
|
/*
|
|
|
|
* At the bottom of the stack, assume the missing 'call' was
|
|
|
|
* before the trace started. So, pop the current symbol and push
|
|
|
|
* the 'to' symbol.
|
|
|
|
*/
|
|
|
|
if (ts->cnt == 1) {
|
|
|
|
err = thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
tm, ref, false);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ts->cnt) {
|
|
|
|
cp = call_path__findnew(cpr, root, tsym, addr, ks);
|
|
|
|
|
|
|
|
return thread_stack__push_cp(ts, addr, tm, ref, cp,
|
|
|
|
true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise assume the 'return' is being used as a jump (e.g.
|
|
|
|
* retpoline) and just push the 'to' symbol.
|
|
|
|
*/
|
|
|
|
cp = call_path__findnew(cpr, parent, tsym, addr, ks);
|
|
|
|
|
|
|
|
err = thread_stack__push_cp(ts, 0, tm, ref, cp, true, false);
|
|
|
|
if (!err)
|
|
|
|
ts->stack[ts->cnt - 1].non_call = true;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume 'parent' has not yet returned, so push 'to', and then push and
|
|
|
|
* pop 'from'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cp = call_path__findnew(cpr, parent, tsym, addr, ks);
|
2014-10-30 14:09:45 +00:00
|
|
|
|
2019-01-09 09:18:32 +00:00
|
|
|
err = thread_stack__push_cp(ts, addr, tm, ref, cp, true, false);
|
2014-10-30 14:09:45 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
perf thread-stack: Improve thread_stack__no_call_return()
Improve thread_stack__no_call_return() to better handle 'returns' that
do not match the stack i.e. 'no call'. See code comments for details.
The example below shows how retpolines are affected:
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> bar
After:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
Committer testing:
Chose "Reports", Then "Context-Sensitive Call Graph" and then go on
expanding:
Before:
simple-retpolin
PID:PID
_start
_start
__libc_start_main
main
__x86_indirect_thunk_rax
__x86_indirect_thunk_rax
bar
After:
Remove the "simple.retpoline.db" file, run again the 'perf script' line
to regenerate the .db file and run the exported-sql-viewer.py again to
get the same all the way to 'main', then, from there, including 'main':
main
__x86_indirect_thunk_rax
__x86_indirect_thunk_rax
foo
bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-6-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:34 +00:00
|
|
|
cp = call_path__findnew(cpr, cp, fsym, ip, ks);
|
|
|
|
|
|
|
|
err = thread_stack__push_cp(ts, ip, tm, ref, cp, true, false);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return thread_stack__call_return(thread, ts, --ts->cnt, tm, ref, false);
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_stack__trace_begin(struct thread *thread,
|
|
|
|
struct thread_stack *ts, u64 timestamp,
|
|
|
|
u64 ref)
|
|
|
|
{
|
|
|
|
struct thread_stack_entry *tse;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!ts->cnt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Pop trace end */
|
|
|
|
tse = &ts->stack[ts->cnt - 1];
|
2018-09-20 13:00:46 +00:00
|
|
|
if (tse->trace_end) {
|
2014-10-30 14:09:45 +00:00
|
|
|
err = thread_stack__call_return(thread, ts, --ts->cnt,
|
|
|
|
timestamp, ref, false);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_stack__trace_end(struct thread_stack *ts,
|
|
|
|
struct perf_sample *sample, u64 ref)
|
|
|
|
{
|
|
|
|
struct call_path_root *cpr = ts->crp->cpr;
|
|
|
|
struct call_path *cp;
|
|
|
|
u64 ret_addr;
|
|
|
|
|
|
|
|
/* No point having 'trace end' on the bottom of the stack */
|
|
|
|
if (!ts->cnt || (ts->cnt == 1 && ts->stack[0].ref == ref))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp, NULL, 0,
|
|
|
|
ts->kernel_start);
|
|
|
|
|
|
|
|
ret_addr = sample->ip + sample->insn_len;
|
|
|
|
|
|
|
|
return thread_stack__push_cp(ts, ret_addr, sample->time, ref, cp,
|
2018-09-20 13:00:46 +00:00
|
|
|
false, true);
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
static bool is_x86_retpoline(const char *name)
|
|
|
|
{
|
|
|
|
const char *p = strstr(name, "__x86_indirect_thunk_");
|
|
|
|
|
|
|
|
return p == name || !strcmp(name, "__indirect_thunk_start");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* x86 retpoline functions pollute the call graph. This function removes them.
|
|
|
|
* This does not handle function return thunks, nor is there any improvement
|
|
|
|
* for the handling of inline thunks or extern thunks.
|
|
|
|
*/
|
|
|
|
static int thread_stack__x86_retpoline(struct thread_stack *ts,
|
|
|
|
struct perf_sample *sample,
|
|
|
|
struct addr_location *to_al)
|
|
|
|
{
|
|
|
|
struct thread_stack_entry *tse = &ts->stack[ts->cnt - 1];
|
|
|
|
struct call_path_root *cpr = ts->crp->cpr;
|
|
|
|
struct symbol *sym = tse->cp->sym;
|
|
|
|
struct symbol *tsym = to_al->sym;
|
|
|
|
struct call_path *cp;
|
|
|
|
|
|
|
|
if (sym && is_x86_retpoline(sym->name)) {
|
|
|
|
/*
|
|
|
|
* This is a x86 retpoline fn. It pollutes the call graph by
|
|
|
|
* showing up everywhere there is an indirect branch, but does
|
|
|
|
* not itself mean anything. Here the top-of-stack is removed,
|
|
|
|
* by decrementing the stack count, and then further down, the
|
|
|
|
* resulting top-of-stack is replaced with the actual target.
|
|
|
|
* The result is that the retpoline functions will no longer
|
|
|
|
* appear in the call graph. Note this only affects the call
|
|
|
|
* graph, since all the original branches are left unchanged.
|
|
|
|
*/
|
|
|
|
ts->cnt -= 1;
|
|
|
|
sym = ts->stack[ts->cnt - 2].cp->sym;
|
|
|
|
if (sym && sym == tsym && to_al->addr != tsym->start) {
|
|
|
|
/*
|
|
|
|
* Target is back to the middle of the symbol we came
|
|
|
|
* from so assume it is an indirect jmp and forget it
|
|
|
|
* altogether.
|
|
|
|
*/
|
|
|
|
ts->cnt -= 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (sym && sym == tsym) {
|
|
|
|
/*
|
|
|
|
* Target is back to the symbol we came from so assume it is an
|
|
|
|
* indirect jmp and forget it altogether.
|
|
|
|
*/
|
|
|
|
ts->cnt -= 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = call_path__findnew(cpr, ts->stack[ts->cnt - 2].cp, tsym,
|
|
|
|
sample->addr, ts->kernel_start);
|
|
|
|
if (!cp)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Replace the top-of-stack with the actual target */
|
|
|
|
ts->stack[ts->cnt - 1].cp = cp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
int thread_stack__process(struct thread *thread, struct comm *comm,
|
|
|
|
struct perf_sample *sample,
|
|
|
|
struct addr_location *from_al,
|
|
|
|
struct addr_location *to_al, u64 ref,
|
|
|
|
struct call_return_processor *crp)
|
|
|
|
{
|
2018-12-21 12:06:19 +00:00
|
|
|
struct thread_stack *ts = thread__stack(thread, sample->cpu);
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
enum retpoline_state_t rstate;
|
2014-10-30 14:09:45 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2018-12-21 12:06:13 +00:00
|
|
|
if (ts && !ts->crp) {
|
|
|
|
/* Supersede thread_stack__event() */
|
2018-12-21 12:06:16 +00:00
|
|
|
thread_stack__reset(thread, ts);
|
2018-12-21 12:06:13 +00:00
|
|
|
ts = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ts) {
|
2018-12-21 12:06:19 +00:00
|
|
|
ts = thread_stack__new(thread, sample->cpu, crp);
|
2018-12-21 12:06:15 +00:00
|
|
|
if (!ts)
|
2014-10-30 14:09:45 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
ts->comm = comm;
|
|
|
|
}
|
|
|
|
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
rstate = ts->rstate;
|
|
|
|
if (rstate == X86_RETPOLINE_DETECTED)
|
|
|
|
ts->rstate = X86_RETPOLINE_POSSIBLE;
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
/* Flush stack on exec */
|
|
|
|
if (ts->comm != comm && thread->pid_ == thread->tid) {
|
2015-05-29 13:33:30 +00:00
|
|
|
err = __thread_stack__flush(thread, ts);
|
2014-10-30 14:09:45 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
ts->comm = comm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the stack is empty, put the current symbol on the stack */
|
|
|
|
if (!ts->cnt) {
|
2018-12-21 12:06:14 +00:00
|
|
|
err = thread_stack__bottom(ts, sample, from_al, to_al, ref);
|
2014-10-30 14:09:45 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ts->branch_count += 1;
|
2019-05-20 11:37:19 +00:00
|
|
|
ts->insn_count += sample->insn_cnt;
|
|
|
|
ts->cyc_count += sample->cyc_cnt;
|
2014-10-30 14:09:45 +00:00
|
|
|
ts->last_time = sample->time;
|
|
|
|
|
|
|
|
if (sample->flags & PERF_IP_FLAG_CALL) {
|
2018-09-20 13:00:46 +00:00
|
|
|
bool trace_end = sample->flags & PERF_IP_FLAG_TRACE_END;
|
2014-10-30 14:09:45 +00:00
|
|
|
struct call_path_root *cpr = ts->crp->cpr;
|
|
|
|
struct call_path *cp;
|
|
|
|
u64 ret_addr;
|
|
|
|
|
|
|
|
if (!sample->ip || !sample->addr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret_addr = sample->ip + sample->insn_len;
|
|
|
|
if (ret_addr == sample->addr)
|
|
|
|
return 0; /* Zero-length calls are excluded */
|
|
|
|
|
|
|
|
cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp,
|
|
|
|
to_al->sym, sample->addr,
|
|
|
|
ts->kernel_start);
|
|
|
|
err = thread_stack__push_cp(ts, ret_addr, sample->time, ref,
|
2018-09-20 13:00:46 +00:00
|
|
|
cp, false, trace_end);
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A call to the same symbol but not the start of the symbol,
|
|
|
|
* may be the start of a x86 retpoline.
|
|
|
|
*/
|
|
|
|
if (!err && rstate == X86_RETPOLINE_POSSIBLE && to_al->sym &&
|
|
|
|
from_al->sym == to_al->sym &&
|
|
|
|
to_al->addr != to_al->sym->start)
|
|
|
|
ts->rstate = X86_RETPOLINE_DETECTED;
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
} else if (sample->flags & PERF_IP_FLAG_RETURN) {
|
2019-06-19 06:44:28 +00:00
|
|
|
if (!sample->addr) {
|
|
|
|
u32 return_from_kernel = PERF_IP_FLAG_SYSCALLRET |
|
|
|
|
PERF_IP_FLAG_INTERRUPT;
|
|
|
|
|
|
|
|
if (!(sample->flags & return_from_kernel))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Pop kernel stack */
|
|
|
|
return thread_stack__pop_ks(thread, ts, sample, ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sample->ip)
|
2014-10-30 14:09:45 +00:00
|
|
|
return 0;
|
|
|
|
|
perf thread-stack: Hide x86 retpolines
x86 retpoline functions pollute the call graph by showing up everywhere
there is an indirect branch, but they do not really mean anything. Make
changes so that the default retpoline functions will no longer appear in
the call graph. Note this only affects the call graph, since all the
original branches are left unchanged.
This does not handle function return thunks, nor is there any
improvement for the handling of inline thunks or extern thunks.
Example:
$ cat simple-retpoline.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
int foo(void)
{
return bar() + 1;
}
__attribute__((indirect_branch("thunk"))) int main()
{
int (*volatile fn)(void) = foo;
fn();
return fn();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o simple-retpoline simple-retpoline.c
$ objdump -d simple-retpoline
<SNIP>
0000000000001040 <main>:
1040: 48 83 ec 18 sub $0x18,%rsp
1044: 48 8d 05 25 01 00 00 lea 0x125(%rip),%rax # 1170 <foo>
104b: 48 89 44 24 08 mov %rax,0x8(%rsp)
1050: 48 8b 44 24 08 mov 0x8(%rsp),%rax
1055: e8 1f 01 00 00 callq 1179 <__x86_indirect_thunk_rax>
105a: 48 8b 44 24 08 mov 0x8(%rsp),%rax
105f: 48 83 c4 18 add $0x18,%rsp
1063: e9 11 01 00 00 jmpq 1179 <__x86_indirect_thunk_rax>
<SNIP>
0000000000001160 <bar>:
1160: b8 ff ff ff ff mov $0xffffffff,%eax
1165: c3 retq
<SNIP>
0000000000001170 <foo>:
1170: e8 eb ff ff ff callq 1160 <bar>
1175: 83 c0 01 add $0x1,%eax
1178: c3 retq
0000000000001179 <__x86_indirect_thunk_rax>:
1179: e8 07 00 00 00 callq 1185 <__x86_indirect_thunk_rax+0xc>
117e: f3 90 pause
1180: 0f ae e8 lfence
1183: eb f9 jmp 117e <__x86_indirect_thunk_rax+0x5>
1185: 48 89 04 24 mov %rax,(%rsp)
1189: c3 retq
<SNIP>
$ perf record -o simple-retpoline.perf.data -e intel_pt/cyc/u ./simple-retpoline
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB simple-retpoline.perf.data ]
$ perf script -i simple-retpoline.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py simple-retpoline.db branches calls
2019-01-08 14:03:37.851655 Creating database...
2019-01-08 14:03:37.863256 Writing records...
2019-01-08 14:03:38.069750 Adding indexes
2019-01-08 14:03:38.078799 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py simple-retpoline.db
Before:
main
-> __x86_indirect_thunk_rax
-> __x86_indirect_thunk_rax
-> foo
-> bar
After:
main
-> foo
-> bar
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-7-adrian.hunter@intel.com
[ Remove (sym->name != NULL) test, this is not a pointer and breaks the build with clang version 7.0.1 (Fedora 7.0.1-2.fc30) ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:35 +00:00
|
|
|
/* x86 retpoline 'return' doesn't match the stack */
|
|
|
|
if (rstate == X86_RETPOLINE_DETECTED && ts->cnt > 2 &&
|
|
|
|
ts->stack[ts->cnt - 1].ret_addr != sample->addr)
|
|
|
|
return thread_stack__x86_retpoline(ts, sample, to_al);
|
|
|
|
|
2014-10-30 14:09:45 +00:00
|
|
|
err = thread_stack__pop_cp(thread, ts, sample->addr,
|
|
|
|
sample->time, ref, from_al->sym);
|
|
|
|
if (err) {
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = thread_stack__no_call_return(thread, ts, sample,
|
|
|
|
from_al, to_al, ref);
|
|
|
|
}
|
|
|
|
} else if (sample->flags & PERF_IP_FLAG_TRACE_BEGIN) {
|
|
|
|
err = thread_stack__trace_begin(thread, ts, sample->time, ref);
|
|
|
|
} else if (sample->flags & PERF_IP_FLAG_TRACE_END) {
|
|
|
|
err = thread_stack__trace_end(ts, sample, ref);
|
perf thread-stack: Represent jmps to the start of a different symbol
The compiler might optimize a call/ret combination by making it a jmp.
However the thread-stack does not presently cater for that, so that such
control flow is not visible in the call graph. Make it visible by
recording on the stack a branch to the start of a different symbol.
Note, that means when a ret pops the stack, all jmps must be popped off
first.
Example:
$ cat jmp-to-fn.c
__attribute__((noinline)) int bar(void)
{
return -1;
}
__attribute__((noinline)) int foo(void)
{
return bar() + 1;
}
int main()
{
return foo();
}
$ gcc -ggdb3 -Wall -Wextra -O2 -o jmp-to-fn jmp-to-fn.c
$ objdump -d jmp-to-fn
<SNIP>
0000000000001040 <main>:
1040: 31 c0 xor %eax,%eax
1042: e9 09 01 00 00 jmpq 1150 <foo>
<SNIP>
0000000000001140 <bar>:
1140: b8 ff ff ff ff mov $0xffffffff,%eax
1145: c3 retq
<SNIP>
0000000000001150 <foo>:
1150: 31 c0 xor %eax,%eax
1152: e8 e9 ff ff ff callq 1140 <bar>
1157: 83 c0 01 add $0x1,%eax
115a: c3 retq
<SNIP>
$ perf record -o jmp-to-fn.perf.data -e intel_pt/cyc/u ./jmp-to-fn
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,017 MB jmp-to-fn.perf.data ]
$ perf script -i jmp-to-fn.perf.data --itrace=be -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py jmp-to-fn.db branches calls
2019-01-08 13:24:58.783069 Creating database...
2019-01-08 13:24:58.794650 Writing records...
2019-01-08 13:24:59.008050 Adding indexes
2019-01-08 13:24:59.015802 Done
$ ~/libexec/perf-core/scripts/python/exported-sql-viewer.py jmp-to-fn.db
Before:
main
-> bar
After:
main
-> foo
-> bar
Committer testing:
Install the python2-pyside package, then select these menu options
on the GUI:
"Reports"
"Context sensitive callgraphs"
Then go on expanding the symbols, to get, full picture when doing this
on a fedora:29 with gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC):
jmp-to-fn
PID:TID
_start (ld-2.28.so)
__libc_start_main
main
foo
bar
To verify that indeed, this fixes the problem.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: http://lkml.kernel.org/r/20190109091835.5570-5-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-01-09 09:18:33 +00:00
|
|
|
} else if (sample->flags & PERF_IP_FLAG_BRANCH &&
|
|
|
|
from_al->sym != to_al->sym && to_al->sym &&
|
|
|
|
to_al->addr == to_al->sym->start) {
|
|
|
|
struct call_path_root *cpr = ts->crp->cpr;
|
|
|
|
struct call_path *cp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The compiler might optimize a call/ret combination by making
|
|
|
|
* it a jmp. Make that visible by recording on the stack a
|
|
|
|
* branch to the start of a different symbol. Note, that means
|
|
|
|
* when a ret pops the stack, all jmps must be popped off first.
|
|
|
|
*/
|
|
|
|
cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp,
|
|
|
|
to_al->sym, sample->addr,
|
|
|
|
ts->kernel_start);
|
|
|
|
err = thread_stack__push_cp(ts, 0, sample->time, ref, cp, false,
|
|
|
|
false);
|
|
|
|
if (!err)
|
|
|
|
ts->stack[ts->cnt - 1].non_call = true;
|
2014-10-30 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
perf script: Add callindent option
Based on patches from Andi Kleen.
When printing PT instruction traces with perf script it is rather useful
to see some indentation for the call tree. This patch adds a new
callindent field to perf script that prints spaces for the function call
stack depth.
We already have code to track the function call stack for PT, that we
can reuse with minor modifications.
The resulting output is not quite as nice as ftrace yet, but a lot
better than what was there before.
Note there are some corner cases when the thread stack gets code
confused and prints incorrect indentation. Even with that it is fairly
useful.
When displaying kernel code traces it is recommended to run as root, as
otherwise perf doesn't understand the kernel addresses properly, and may
not reset the call stack correctly on kernel boundaries.
Example output:
sudo perf-with-kcore record eg2 -a -e intel_pt// -- sleep 1
sudo perf-with-kcore script eg2 --ns -F callindent,time,comm,pid,sym,ip,addr,flags,cpu --itrace=cre | less
...
swapper 0 [000] 5830.389116586: call irq_exit ffffffff8104d620 smp_call_function_single_interrupt+0x30 => ffffffff8107e720 irq_exit
swapper 0 [000] 5830.389116586: call idle_cpu ffffffff8107e769 irq_exit+0x49 => ffffffff810a3970 idle_cpu
swapper 0 [000] 5830.389116586: return idle_cpu ffffffff810a39b7 idle_cpu+0x47 => ffffffff8107e76e irq_exit
swapper 0 [000] 5830.389116586: call tick_nohz_irq_exit ffffffff8107e7bd irq_exit+0x9d => ffffffff810f2fc0 tick_nohz_irq_exit
swapper 0 [000] 5830.389116919: call __tick_nohz_idle_enter ffffffff810f2fe0 tick_nohz_irq_exit+0x20 => ffffffff810f28d0 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call ktime_get ffffffff810f28f1 __tick_nohz_idle_enter+0x21 => ffffffff810e9ec0 ktime_get
swapper 0 [000] 5830.389116919: call read_tsc ffffffff810e9ef6 ktime_get+0x36 => ffffffff81035070 read_tsc
swapper 0 [000] 5830.389116919: return read_tsc ffffffff81035084 read_tsc+0x14 => ffffffff810e9efc ktime_get
swapper 0 [000] 5830.389116919: return ktime_get ffffffff810e9f46 ktime_get+0x86 => ffffffff810f28f6 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call sched_clock_idle_sleep_event ffffffff810f290b __tick_nohz_idle_enter+0x3b => ffffffff810a7380 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: call sched_clock_cpu ffffffff810a738b sched_clock_idle_sleep_event+0xb => ffffffff810a72e0 sched_clock_cpu
swapper 0 [000] 5830.389116919: call sched_clock ffffffff810a734d sched_clock_cpu+0x6d => ffffffff81035750 sched_clock
swapper 0 [000] 5830.389116919: call native_sched_clock ffffffff81035754 sched_clock+0x4 => ffffffff81035640 native_sched_clock
swapper 0 [000] 5830.389116919: return native_sched_clock ffffffff8103568c native_sched_clock+0x4c => ffffffff81035759 sched_clock
swapper 0 [000] 5830.389116919: return sched_clock ffffffff8103575c sched_clock+0xc => ffffffff810a7352 sched_clock_cpu
swapper 0 [000] 5830.389116919: return sched_clock_cpu ffffffff810a7356 sched_clock_cpu+0x76 => ffffffff810a7390 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: return sched_clock_idle_sleep_event ffffffff810a7391 sched_clock_idle_sleep_event+0x11 => ffffffff810f2910 __tick_nohz_idle_enter
...
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/1466689258-28493-4-git-send-email-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-06-23 13:40:58 +00:00
|
|
|
|
2018-12-21 12:06:19 +00:00
|
|
|
size_t thread_stack__depth(struct thread *thread, int cpu)
|
perf script: Add callindent option
Based on patches from Andi Kleen.
When printing PT instruction traces with perf script it is rather useful
to see some indentation for the call tree. This patch adds a new
callindent field to perf script that prints spaces for the function call
stack depth.
We already have code to track the function call stack for PT, that we
can reuse with minor modifications.
The resulting output is not quite as nice as ftrace yet, but a lot
better than what was there before.
Note there are some corner cases when the thread stack gets code
confused and prints incorrect indentation. Even with that it is fairly
useful.
When displaying kernel code traces it is recommended to run as root, as
otherwise perf doesn't understand the kernel addresses properly, and may
not reset the call stack correctly on kernel boundaries.
Example output:
sudo perf-with-kcore record eg2 -a -e intel_pt// -- sleep 1
sudo perf-with-kcore script eg2 --ns -F callindent,time,comm,pid,sym,ip,addr,flags,cpu --itrace=cre | less
...
swapper 0 [000] 5830.389116586: call irq_exit ffffffff8104d620 smp_call_function_single_interrupt+0x30 => ffffffff8107e720 irq_exit
swapper 0 [000] 5830.389116586: call idle_cpu ffffffff8107e769 irq_exit+0x49 => ffffffff810a3970 idle_cpu
swapper 0 [000] 5830.389116586: return idle_cpu ffffffff810a39b7 idle_cpu+0x47 => ffffffff8107e76e irq_exit
swapper 0 [000] 5830.389116586: call tick_nohz_irq_exit ffffffff8107e7bd irq_exit+0x9d => ffffffff810f2fc0 tick_nohz_irq_exit
swapper 0 [000] 5830.389116919: call __tick_nohz_idle_enter ffffffff810f2fe0 tick_nohz_irq_exit+0x20 => ffffffff810f28d0 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call ktime_get ffffffff810f28f1 __tick_nohz_idle_enter+0x21 => ffffffff810e9ec0 ktime_get
swapper 0 [000] 5830.389116919: call read_tsc ffffffff810e9ef6 ktime_get+0x36 => ffffffff81035070 read_tsc
swapper 0 [000] 5830.389116919: return read_tsc ffffffff81035084 read_tsc+0x14 => ffffffff810e9efc ktime_get
swapper 0 [000] 5830.389116919: return ktime_get ffffffff810e9f46 ktime_get+0x86 => ffffffff810f28f6 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call sched_clock_idle_sleep_event ffffffff810f290b __tick_nohz_idle_enter+0x3b => ffffffff810a7380 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: call sched_clock_cpu ffffffff810a738b sched_clock_idle_sleep_event+0xb => ffffffff810a72e0 sched_clock_cpu
swapper 0 [000] 5830.389116919: call sched_clock ffffffff810a734d sched_clock_cpu+0x6d => ffffffff81035750 sched_clock
swapper 0 [000] 5830.389116919: call native_sched_clock ffffffff81035754 sched_clock+0x4 => ffffffff81035640 native_sched_clock
swapper 0 [000] 5830.389116919: return native_sched_clock ffffffff8103568c native_sched_clock+0x4c => ffffffff81035759 sched_clock
swapper 0 [000] 5830.389116919: return sched_clock ffffffff8103575c sched_clock+0xc => ffffffff810a7352 sched_clock_cpu
swapper 0 [000] 5830.389116919: return sched_clock_cpu ffffffff810a7356 sched_clock_cpu+0x76 => ffffffff810a7390 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: return sched_clock_idle_sleep_event ffffffff810a7391 sched_clock_idle_sleep_event+0x11 => ffffffff810f2910 __tick_nohz_idle_enter
...
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/1466689258-28493-4-git-send-email-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-06-23 13:40:58 +00:00
|
|
|
{
|
2018-12-21 12:06:19 +00:00
|
|
|
struct thread_stack *ts = thread__stack(thread, cpu);
|
2018-12-21 12:06:15 +00:00
|
|
|
|
|
|
|
if (!ts)
|
perf script: Add callindent option
Based on patches from Andi Kleen.
When printing PT instruction traces with perf script it is rather useful
to see some indentation for the call tree. This patch adds a new
callindent field to perf script that prints spaces for the function call
stack depth.
We already have code to track the function call stack for PT, that we
can reuse with minor modifications.
The resulting output is not quite as nice as ftrace yet, but a lot
better than what was there before.
Note there are some corner cases when the thread stack gets code
confused and prints incorrect indentation. Even with that it is fairly
useful.
When displaying kernel code traces it is recommended to run as root, as
otherwise perf doesn't understand the kernel addresses properly, and may
not reset the call stack correctly on kernel boundaries.
Example output:
sudo perf-with-kcore record eg2 -a -e intel_pt// -- sleep 1
sudo perf-with-kcore script eg2 --ns -F callindent,time,comm,pid,sym,ip,addr,flags,cpu --itrace=cre | less
...
swapper 0 [000] 5830.389116586: call irq_exit ffffffff8104d620 smp_call_function_single_interrupt+0x30 => ffffffff8107e720 irq_exit
swapper 0 [000] 5830.389116586: call idle_cpu ffffffff8107e769 irq_exit+0x49 => ffffffff810a3970 idle_cpu
swapper 0 [000] 5830.389116586: return idle_cpu ffffffff810a39b7 idle_cpu+0x47 => ffffffff8107e76e irq_exit
swapper 0 [000] 5830.389116586: call tick_nohz_irq_exit ffffffff8107e7bd irq_exit+0x9d => ffffffff810f2fc0 tick_nohz_irq_exit
swapper 0 [000] 5830.389116919: call __tick_nohz_idle_enter ffffffff810f2fe0 tick_nohz_irq_exit+0x20 => ffffffff810f28d0 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call ktime_get ffffffff810f28f1 __tick_nohz_idle_enter+0x21 => ffffffff810e9ec0 ktime_get
swapper 0 [000] 5830.389116919: call read_tsc ffffffff810e9ef6 ktime_get+0x36 => ffffffff81035070 read_tsc
swapper 0 [000] 5830.389116919: return read_tsc ffffffff81035084 read_tsc+0x14 => ffffffff810e9efc ktime_get
swapper 0 [000] 5830.389116919: return ktime_get ffffffff810e9f46 ktime_get+0x86 => ffffffff810f28f6 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call sched_clock_idle_sleep_event ffffffff810f290b __tick_nohz_idle_enter+0x3b => ffffffff810a7380 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: call sched_clock_cpu ffffffff810a738b sched_clock_idle_sleep_event+0xb => ffffffff810a72e0 sched_clock_cpu
swapper 0 [000] 5830.389116919: call sched_clock ffffffff810a734d sched_clock_cpu+0x6d => ffffffff81035750 sched_clock
swapper 0 [000] 5830.389116919: call native_sched_clock ffffffff81035754 sched_clock+0x4 => ffffffff81035640 native_sched_clock
swapper 0 [000] 5830.389116919: return native_sched_clock ffffffff8103568c native_sched_clock+0x4c => ffffffff81035759 sched_clock
swapper 0 [000] 5830.389116919: return sched_clock ffffffff8103575c sched_clock+0xc => ffffffff810a7352 sched_clock_cpu
swapper 0 [000] 5830.389116919: return sched_clock_cpu ffffffff810a7356 sched_clock_cpu+0x76 => ffffffff810a7390 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: return sched_clock_idle_sleep_event ffffffff810a7391 sched_clock_idle_sleep_event+0x11 => ffffffff810f2910 __tick_nohz_idle_enter
...
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/1466689258-28493-4-git-send-email-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-06-23 13:40:58 +00:00
|
|
|
return 0;
|
2018-12-21 12:06:15 +00:00
|
|
|
return ts->cnt;
|
perf script: Add callindent option
Based on patches from Andi Kleen.
When printing PT instruction traces with perf script it is rather useful
to see some indentation for the call tree. This patch adds a new
callindent field to perf script that prints spaces for the function call
stack depth.
We already have code to track the function call stack for PT, that we
can reuse with minor modifications.
The resulting output is not quite as nice as ftrace yet, but a lot
better than what was there before.
Note there are some corner cases when the thread stack gets code
confused and prints incorrect indentation. Even with that it is fairly
useful.
When displaying kernel code traces it is recommended to run as root, as
otherwise perf doesn't understand the kernel addresses properly, and may
not reset the call stack correctly on kernel boundaries.
Example output:
sudo perf-with-kcore record eg2 -a -e intel_pt// -- sleep 1
sudo perf-with-kcore script eg2 --ns -F callindent,time,comm,pid,sym,ip,addr,flags,cpu --itrace=cre | less
...
swapper 0 [000] 5830.389116586: call irq_exit ffffffff8104d620 smp_call_function_single_interrupt+0x30 => ffffffff8107e720 irq_exit
swapper 0 [000] 5830.389116586: call idle_cpu ffffffff8107e769 irq_exit+0x49 => ffffffff810a3970 idle_cpu
swapper 0 [000] 5830.389116586: return idle_cpu ffffffff810a39b7 idle_cpu+0x47 => ffffffff8107e76e irq_exit
swapper 0 [000] 5830.389116586: call tick_nohz_irq_exit ffffffff8107e7bd irq_exit+0x9d => ffffffff810f2fc0 tick_nohz_irq_exit
swapper 0 [000] 5830.389116919: call __tick_nohz_idle_enter ffffffff810f2fe0 tick_nohz_irq_exit+0x20 => ffffffff810f28d0 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call ktime_get ffffffff810f28f1 __tick_nohz_idle_enter+0x21 => ffffffff810e9ec0 ktime_get
swapper 0 [000] 5830.389116919: call read_tsc ffffffff810e9ef6 ktime_get+0x36 => ffffffff81035070 read_tsc
swapper 0 [000] 5830.389116919: return read_tsc ffffffff81035084 read_tsc+0x14 => ffffffff810e9efc ktime_get
swapper 0 [000] 5830.389116919: return ktime_get ffffffff810e9f46 ktime_get+0x86 => ffffffff810f28f6 __tick_nohz_idle_enter
swapper 0 [000] 5830.389116919: call sched_clock_idle_sleep_event ffffffff810f290b __tick_nohz_idle_enter+0x3b => ffffffff810a7380 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: call sched_clock_cpu ffffffff810a738b sched_clock_idle_sleep_event+0xb => ffffffff810a72e0 sched_clock_cpu
swapper 0 [000] 5830.389116919: call sched_clock ffffffff810a734d sched_clock_cpu+0x6d => ffffffff81035750 sched_clock
swapper 0 [000] 5830.389116919: call native_sched_clock ffffffff81035754 sched_clock+0x4 => ffffffff81035640 native_sched_clock
swapper 0 [000] 5830.389116919: return native_sched_clock ffffffff8103568c native_sched_clock+0x4c => ffffffff81035759 sched_clock
swapper 0 [000] 5830.389116919: return sched_clock ffffffff8103575c sched_clock+0xc => ffffffff810a7352 sched_clock_cpu
swapper 0 [000] 5830.389116919: return sched_clock_cpu ffffffff810a7356 sched_clock_cpu+0x76 => ffffffff810a7390 sched_clock_idle_sleep_event
swapper 0 [000] 5830.389116919: return sched_clock_idle_sleep_event ffffffff810a7391 sched_clock_idle_sleep_event+0x11 => ffffffff810f2910 __tick_nohz_idle_enter
...
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/1466689258-28493-4-git-send-email-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-06-23 13:40:58 +00:00
|
|
|
}
|