perf: Add backtrace post dwarf unwind
Subsystems (3)
cover perf: Add backtrace post dwarf unwind
hi,
sending RFC version of the post unwinding user stack backtrace using dwarf unwind - via libunwind. The original work was done by Frederic. I mostly took his patches and make them compile in current kernel code plus I added some stuff here and there.
The main idea is to store user registers and portion of user stack when the sample data during the record phase. Then during the report, when the data is presented, perform the actual dwarf dwarf unwind.
attached patches:
01/15 perf, tool: Fix the array pointer to follow event data properly 02/15 uaccess: Add new copy_from_user_gup API
03/15 perf: Unified API to record selective sets of arch registers 04/15 perf: Add ability to dump user regs
05/15 perf: Add ability to dump part of the user stack 06/15 perf: Add attribute to filter out user callchains 07/15 perf, tool: Factor DSO symtab types to generic binary types 08/15 perf, tool: Add interface to read DSO image data 09/15 perf, tool: Add '.note' check into search for NOTE section 10/15 perf, tool: Back [vdso] DSO with real data
11/15 perf, tool: Add interface to arch registers sets 12/15 perf, tool: Add libunwind dependency for dwarf cfi unwinding 13/15 perf, tool: Support user regs and stack in sample parsing 14/15 perf, tool: Support for dwarf cfi unwinding on post processing 15/15 perf, tool: Support for dwarf mode callchain on perf record
The unwind processing could considerably prolong the computing time of the report command, but I believe this could be improved.
- caching DSO data accesses (as suggested in patch 8/15)
- maybe separate thread with unwind processing on background, so the user does no need to wait for all the data to be processed.
I tested on Fedora. There was not much gain on i386, because the binaries are compiled with frame pointers. Thought the dwarf backtrace is more accurade and unwraps calls in more details (functions that do not set the frame pointers).
I could see some improvement on x86_64, where I got full backtrace where current code could got just the first address out of the instruction pointer.
Example on x86_64:
[dwarf]
perf record -g -e syscalls:sys_enter_write date
100.00% date libc-2.14.90.so [.] __GI___libc_write
|
--- __GI___libc_write
_IO_file_write@@GLIBC_2.2.5
new_do_write
_IO_do_write@@GLIBC_2.2.5
_IO_file_overflow@@GLIBC_2.2.5
0x4022cd
0x401ee6
__libc_start_main
0x4020b9[frame pointer]
perf record -g fp -e syscalls:sys_enter_write date
100.00% date libc-2.14.90.so [.] __GI___libc_write
|
--- __GI___libc_writeAlso I tested on coreutils binaries mainly, but I could see getting wider backtraces with dwarf unwind for more complex application like firefox.
The unwind should go throught [vdso] object. I haven't studied the [vsyscall] yet, so not sure there.
Attached patches should work on both x86 and x86_64. I did some initial testing so far.
The unwind backtrace can be interrupted by following reasons:
- bug in unwind information of processed shared library
- bug in unwind processing code (most likely ;) )
- insufficient dump stack size
- wrong register value - x86_64 does not store whole set of registers when in exception, but so far it looks like RIP and RSP should be enough
I'd like to have some automated tests on this, but so far nothing smart is comming to me.. ;)
thanks for comments,
jirka
--- arch/Kconfig | 7 + arch/x86/Kconfig | 1 + arch/x86/include/asm/perf_regs.h | 15 + arch/x86/include/asm/perf_regs_32.h | 86 +++ arch/x86/include/asm/perf_regs_64.h | 101 ++++ arch/x86/include/asm/uaccess.h | 8 +- arch/x86/kernel/cpu/perf_event.c | 4 +- arch/x86/kernel/cpu/perf_event_intel_ds.c | 3 +- arch/x86/kernel/cpu/perf_event_intel_lbr.c | 2 +- arch/x86/lib/usercopy.c | 4 +- arch/x86/oprofile/backtrace.c | 4 +- include/asm-generic/uaccess.h | 4 + include/linux/perf_event.h | 36 ++- kernel/events/callchain.c | 4 +- kernel/events/core.c | 127 +++++- kernel/events/internal.h | 59 ++- kernel/events/ring_buffer.c | 4 +- tools/perf/Makefile | 40 ++- tools/perf/arch/x86/Makefile | 3 + tools/perf/arch/x86/include/perf_regs.h | 101 ++++ tools/perf/arch/x86/util/unwind.c | 111 ++++ tools/perf/builtin-record.c | 89 +++- tools/perf/builtin-report.c | 24 +- tools/perf/builtin-script.c | 56 ++- tools/perf/builtin-test.c | 3 +- tools/perf/builtin-top.c | 7 +- tools/perf/config/feature-tests.mak | 25 + tools/perf/perf.h | 9 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/event.h | 15 +- tools/perf/util/evlist.c | 16 + tools/perf/util/evlist.h | 2 + tools/perf/util/evsel.c | 36 ++- tools/perf/util/include/linux/compiler.h | 1 + tools/perf/util/map.c | 16 +- tools/perf/util/map.h | 7 +- tools/perf/util/perf_regs.h | 10 + tools/perf/util/python.c | 3 +- .../perf/util/scripting-engines/trace-event-perl.c | 3 +- .../util/scripting-engines/trace-event-python.c | 3 +- tools/perf/util/session.c | 100 +++- tools/perf/util/session.h | 10 +- tools/perf/util/symbol.c | 317 +++++++++--- tools/perf/util/symbol.h | 40 +- tools/perf/util/trace-event-scripting.c | 3 +- tools/perf/util/trace-event.h | 5 +- tools/perf/util/unwind.c | 563 ++++++++++++++++++++ tools/perf/util/unwind.h | 34 ++ tools/perf/util/vdso.c | 92 ++++ tools/perf/util/vdso.h | 7 + 50 files changed, 2023 insertions(+), 199 deletions(-)
PATCH 1/15 perf, tool: Fix the array pointer to follow event data properly
Currently we dont update the 'array' pointer properly after processing the RAW data. This way perf might report wrong data for branch stack if it is used along with tracepoint sample.
I'm not sure tracepoint could be connected with branch stack, but I think the array pointer should have correct value after each sample processing.
--- tools/perf/util/evsel.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
tools/perf/util/evsel.c +1 -0
@@ -580,6 +580,7 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,return -EFAULT ; data->raw_data = (void *) pdata; + array = (u64 *)(((char *)array) + data->raw_size +sizeof (u32)); }if (type &PERF_SAMPLE_BRANCH_STACK ) {
Signature · 1 line
1.7.1
PATCH 2/15 uaccess: Add new copy_from_user_gup API
In reply to Jiri OlsaThis brings a get_user_page_fast() based copy_from_user() that can do a best effort copy from any context.
In order to support user stack dump safely in perf samples from generic code, rename x86 copy_from_user_nmi to copy_from_user_gup and make it generally available. If the arch doesn't provide an implementation it will map to copy_from_user_inatomic.
--- arch/x86/include/asm/uaccess.h | 8 +++++--- arch/x86/kernel/cpu/perf_event.c | 4 ++-- arch/x86/kernel/cpu/perf_event_intel_ds.c | 3 ++- arch/x86/kernel/cpu/perf_event_intel_lbr.c | 2 +- arch/x86/lib/usercopy.c | 4 ++-- arch/x86/oprofile/backtrace.c | 4 ++-- include/asm-generic/uaccess.h | 4 ++++ 7 files changed, 18 insertions(+), 11 deletions(-)
arch/x86/include/asm/uaccess.h +5 -3
@@ -228,6 +228,11 @@ extern void __put_user_2(void);extern void __put_user_4 (void );extern void __put_user_8 (void ); +extern unsigned long +__copy_from_user_gup (void *to,const void __user *from,unsigned long n); + +#define copy_from_user_gup (to, from, n) __copy_from_user_gup(to, from, n) +#ifdef CONFIG_X86_WP_WORKS_OK /**
@@ -555,9 +560,6 @@ struct __large_struct { unsigned long buf[100]; };#endif /* CONFIG_X86_WP_WORKS_OK */ -extern unsigned long -copy_from_user_nmi (void *to,const void __user *from,unsigned long n); -/* * movsl can be slow when source and dest are not both 8-byte aligned */
arch/x86/kernel/cpu/perf_event.c +2 -2
@@ -1781,7 +1781,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) frame.next_frame =0 ; frame.return_address =0 ; - bytes =copy_from_user_nmi (&frame, fp,sizeof (frame)); + bytes =copy_from_user_gup (&frame, fp,sizeof (frame));if (bytes !=sizeof (frame))break ;
@@ -1827,7 +1827,7 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) frame.next_frame =NULL ; frame.return_address =0 ; - bytes =copy_from_user_nmi (&frame, fp,sizeof (frame)); + bytes =copy_from_user_gup (&frame, fp,sizeof (frame));if (bytes !=sizeof (frame))break ;
arch/x86/kernel/cpu/perf_event_intel_ds.c +2 -1
@@ -520,7 +520,8 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)if (!kernel_ip (ip)) {int bytes, size =MAX_INSN_SIZE ; - bytes =copy_from_user_nmi (buf, (void __user *)to, size); + bytes =copy_from_user_gup (buf, (void __user *)to, + size);if (bytes != size)return 0 ;
arch/x86/kernel/cpu/perf_event_intel_lbr.c +1 -1
@@ -437,7 +437,7 @@ static int branch_type(unsigned long from, unsigned long to)return X86_BR_NONE ;/* may fail if text not present */ - bytes =copy_from_user_nmi (buf, (void __user *)from, size); + bytes =copy_from_user_gup (buf, (void __user *)from, size);if (bytes != size)return X86_BR_NONE ;
arch/x86/lib/usercopy.c +2 -2
@@ -11,7 +11,7 @@* best effort, GUP based copy_from_user() that is NMI-safe */ unsigned long -copy_from_user_nmi (void *to,const void __user *from,unsigned long n) +__copy_from_user_gup (void *to,const void __user *from,unsigned long n) {unsigned long offset, addr = (unsigned long )from;unsigned long size, len =0 ;
@@ -40,4 +40,4 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)return len; } -EXPORT_SYMBOL_GPL (copy_from_user_nmi); +EXPORT_SYMBOL_GPL (__copy_from_user_gup);
arch/x86/oprofile/backtrace.c +2 -2
@@ -46,7 +46,7 @@ dump_user_backtrace_32(struct stack_frame_ia32 *head)struct stack_frame_ia32 *fp;unsigned long bytes; - bytes =copy_from_user_nmi (bufhead, head,sizeof (bufhead)); + bytes =copy_from_user_gup (bufhead, head,sizeof (bufhead));if (bytes !=sizeof (bufhead))return NULL ;
@@ -92,7 +92,7 @@ static struct stack_frame *dump_user_backtrace(struct stack_frame *head)struct stack_frame bufhead[2 ];unsigned long bytes; - bytes =copy_from_user_nmi (bufhead, head,sizeof (bufhead)); + bytes =copy_from_user_gup (bufhead, head,sizeof (bufhead));if (bytes !=sizeof (bufhead))return NULL ;
include/asm-generic/uaccess.h +4 -0
@@ -240,6 +240,10 @@ extern int __get_user_bad(void) __attribute__((noreturn));#define __copy_to_user_inatomic __copy_to_user#endif +#ifndef copy_from_user_gup +#define copy_from_user_gup __copy_from_user_inatomic +#endif +static inline long copy_from_user (void *to,const void __user * from,unsigned long n) {
Signature · 1 line
1.7.1
PATCH 3/15 perf: Unified API to record selective sets of arch registers
In reply to Jiri OlsaThis brings a new API to help the selective dump of registers on event sampling, and its implementation in x86.
- The informations about the desired registers will be passed to a single u64 mask. It's up to the architecture to map the registers into the mask bits.
- The architecture must provide a non-zero and unique id to identify the origin of a register set because interpreting a register dump requires to know from which architecture it comes. The achitecture is considered different between the 32 and 64 bits version. x86-32 has the id 1, x86-64 has the id 2.
--- arch/Kconfig | 7 +++ arch/x86/Kconfig | 1 + arch/x86/include/asm/perf_regs.h | 15 +++++ arch/x86/include/asm/perf_regs_32.h | 86 +++++++++++++++++++++++++++++ arch/x86/include/asm/perf_regs_64.h | 101 +++++++++++++++++++++++++++++++++++ 5 files changed, 210 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/perf_regs.h create mode 100644 arch/x86/include/asm/perf_regs_32.h create mode 100644 arch/x86/include/asm/perf_regs_64.h
arch/Kconfig +7 -0
@@ -204,6 +204,13 @@ config HAVE_PERF_EVENTS_NMI subsystem. Also has support for calculating CPU cycle events to determine how many clock cycles in a given period. +config HAVE_PERF_REGS_DEFS + bool + help + Support selective register dumps for perf events. This includes + bit-mapping of each registers and a unique architecture version, + also different between 32 and 64 bits. + config HAVE_ARCH_JUMP_LABEL bool
arch/x86/Kconfig +1 -0
@@ -59,6 +59,7 @@ config X86 select HAVE_MIXED_BREAKPOINTS_REGS select PERF_EVENTS select HAVE_PERF_EVENTS_NMI + select HAVE_PERF_REGS_DEFS select ANON_INODES select HAVE_ALIGNED_STRUCT_PAGE if SLUB && !M386 select HAVE_CMPXCHG_LOCAL if !M386
arch/x86/include/asm/perf_regs.h new file +15 -0
@@ -0,0 +1,15 @@ +#ifndef _ASM_X86_PERF_REGS_H +#define _ASM_X86_PERF_REGS_H + +enum { +PERF_X86_32_REG_VERSION =1UL , +PERF_X86_64_REG_VERSION =2UL , +}; + +#ifdef CONFIG_X86_32 +#include "perf_regs_32.h" +#else +#include "perf_regs_64.h" +#endif + +#endif /* _ASM_X86_PERF_REGS_H */
arch/x86/include/asm/perf_regs_32.h new file +86 -0
86 lines not on this page · raw message
arch/x86/include/asm/perf_regs_64.h new file +101 -0
101 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 5/15 perf: Add ability to dump part of the user stack
In reply to Jiri OlsaBeeing able to dump parts of the user stack, starting from the stack pointer, will be useful to make a post mortem dwarf CFI based stack unwinding.
This is done through the new ustack_dump_size perf attribute. If it is non zero, the user stack will dumped in samples following the requested size in bytes.
The longer is the dump, the deeper will be the resulting retrieved callchain.
--- include/linux/perf_event.h | 6 +++- kernel/events/core.c | 63 +++++++++++++++++++++++++++++++++++++++++++ kernel/events/internal.h | 56 ++++++++++++++++++++++++-------------- kernel/events/ring_buffer.c | 4 +- 4 files changed, 104 insertions(+), 25 deletions(-)
include/linux/perf_event.h +4 -2
@@ -278,6 +278,7 @@ struct perf_event_attr {* samples. See asm/perf_regs.h for details. */ __u64 user_regs; +__u32 ustack_dump_size; };/*
@@ -1309,8 +1310,9 @@ static inline bool has_branch_stack(struct perf_event *event)extern int perf_output_begin (struct perf_output_handle *handle,struct perf_event *event,unsigned int size);extern void perf_output_end (struct perf_output_handle *handle); -extern void perf_output_copy (struct perf_output_handle *handle, -const void *buf,unsigned int len); +extern unsigned int +perf_output_copy (struct perf_output_handle *handle, +const void *buf,unsigned int len);extern int perf_swevent_get_recursion_context (void );extern void perf_swevent_put_recursion_context (int rctx);extern void perf_event_enable (struct perf_event *event);
kernel/events/core.c +63 -0
75 lines not on this page · raw message
kernel/events/internal.h +35 -21
65 lines not on this page · raw message
kernel/events/ring_buffer.c +2 -2
@@ -182,10 +182,10 @@ out:return -ENOSPC ; } -void perf_output_copy (struct perf_output_handle *handle, +unsigned int perf_output_copy (struct perf_output_handle *handle,const void *buf,unsigned int len) { -__output_copy (handle, buf, len); +return __output_copy (handle, buf, len); }void perf_output_end (struct perf_output_handle *handle)
Signature · 1 line
1.7.1
PATCH 7/15 perf, tool: Factor DSO symtab types to generic binary types
In reply to Jiri OlsaAdding interface to access DSOs so it could be used from another place.
New DSO binary type is added - making current SYMTAB__* types more general:
DSO_BINARY_TYPE__* = SYMTAB__*
Folowing function is added to return path based on the specified binary type:
dso__binary_type_file
--- tools/perf/builtin-top.c | 2 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/symbol.c | 181 +++++++++++++++++++++++++++----------------- tools/perf/util/symbol.h | 32 ++++---- 4 files changed, 129 insertions(+), 88 deletions(-)
tools/perf/builtin-top.c +1 -1
@@ -123,7 +123,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)/* * We can't annotate with just /proc/kallsyms */ -if (map->dso->symtab_type ==SYMTAB__KALLSYMS ) { +if (map->dso->symtab_type ==DSO_BINARY_TYPE__KALLSYMS ) {pr_err ("Can't annotate %s: No vmlinux file was found in the " "path\n" , sym->name);sleep (1 );
tools/perf/util/annotate.c +1 -1
@@ -296,7 +296,7 @@ fallback: free_filename = false; } -if (dso->symtab_type ==SYMTAB__KALLSYMS ) { +if (dso->symtab_type ==DSO_BINARY_TYPE__KALLSYMS ) {char bf[BUILD_ID_SIZE *2 +16 ] =" with build id " ;char *build_id_msg =NULL ;
tools/perf/util/symbol.c +110 -71
248 lines not on this page · raw message
tools/perf/util/symbol.h +17 -15
@@ -149,6 +149,20 @@ struct addr_location {s32 cpu; }; +enum dso_binary_type { +DSO_BINARY_TYPE__KALLSYMS =0 , +DSO_BINARY_TYPE__GUEST_KALLSYMS , +DSO_BINARY_TYPE__JAVA_JIT , +DSO_BINARY_TYPE__BUILD_ID_CACHE , +DSO_BINARY_TYPE__FEDORA_DEBUGINFO , +DSO_BINARY_TYPE__UBUNTU_DEBUGINFO , +DSO_BINARY_TYPE__BUILDID_DEBUGINFO , +DSO_BINARY_TYPE__SYSTEM_PATH_DSO , +DSO_BINARY_TYPE__GUEST_KMODULE , +DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE , +DSO_BINARY_TYPE__NOT_FOUND , +}; +enum dso_kernel_type {DSO_TYPE_USER =0 ,DSO_TYPE_KERNEL ,
@@ -160,13 +174,13 @@ struct dso {struct rb_root symbols[MAP__NR_TYPES ];struct rb_root symbol_names[MAP__NR_TYPES ];enum dso_kernel_type kernel; +enum dso_binary_type symtab_type; u8 adjust_symbols:1; u8 has_build_id:1; u8 hit:1; u8 annotate_warned:1; u8 sname_alloc:1; u8 lname_alloc:1; -unsigned char symtab_type;u8 sorted_by_name;u8 loaded;u8 build_id[BUILD_ID_SIZE ];
@@ -218,20 +232,6 @@ size_t dso__fprintf_symbols_by_name(struct dso *dso, enum map_type type, FILE *fp);size_t dso__fprintf (struct dso *dso,enum map_type type,FILE *fp); -enum symtab_type { -SYMTAB__KALLSYMS =0 , -SYMTAB__GUEST_KALLSYMS , -SYMTAB__JAVA_JIT , -SYMTAB__BUILD_ID_CACHE , -SYMTAB__FEDORA_DEBUGINFO , -SYMTAB__UBUNTU_DEBUGINFO , -SYMTAB__BUILDID_DEBUGINFO , -SYMTAB__SYSTEM_PATH_DSO , -SYMTAB__GUEST_KMODULE , -SYMTAB__SYSTEM_PATH_KMODULE , -SYMTAB__NOT_FOUND , -}; -char dso__symtab_origin (const struct dso *dso);void dso__set_long_name (struct dso *dso,char *name);void dso__set_build_id (struct dso *dso,void *build_id);
@@ -267,4 +267,6 @@ bool symbol_type__is_a(char symbol_type, enum map_type map_type);size_t machine__fprintf_vmlinux_path (struct machine *machine,FILE *fp); +int dso__binary_type_file (struct dso *dso,enum dso_binary_type type, +char *root_dir,char *file,size_t size);#endif /* __PERF_SYMBOL */
Signature · 1 line
1.7.1
PATCH 8/15 perf, tool: Add interface to read DSO image data
In reply to Jiri OlsaAdding following interface for DSO object to allow reading of DSO image data:
dso__data_fd
- opens DSO and returns file descriptor
Binary types are used to locate/open DSO in following order:
DSO_BINARY_TYPE__BUILD_ID_CACHE DSO_BINARY_TYPE__SYSTEM_PATH_DSO
In other word we first try to open DSO build-id path, and if that fails we try to open DSO system path.
dso__data_read_offset
- reads DSO data from specified offset
dso__data_read_addr
- reads DSO data from specified addres/map.
TODO:
Implement read cache. Currently following no-op hooks are added:
dso_cache_add/dso_cache_read to store/retrieve dso read data.
--- tools/perf/util/symbol.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/symbol.h | 8 +++ 2 files changed, 115 insertions(+), 0 deletions(-)
tools/perf/util/symbol.c +107 -0
@@ -65,6 +65,14 @@ static enum dso_binary_type binary_type_symtab[] = {#define DSO_BINARY_TYPE__SYMTAB_CNT sizeof(binary_type_symtab) +static enum dso_binary_type binary_type_data[] = { +DSO_BINARY_TYPE__BUILD_ID_CACHE , +DSO_BINARY_TYPE__SYSTEM_PATH_DSO , +DSO_BINARY_TYPE__NOT_FOUND , +}; + +#define DSO_BINARY_TYPE__DATA_CNT sizeof(binary_type_data) +int dso__name_len (const struct dso *dso) {if (verbose)
@@ -334,6 +342,7 @@ struct dso *dso__new(const char *name)for (i =0 ; i <MAP__NR_TYPES ; ++i) dso->symbols[i] = dso->symbol_names[i] =RB_ROOT ; dso->symtab_type =DSO_BINARY_TYPE__NOT_FOUND ; + dso->data_type =DSO_BINARY_TYPE__NOT_FOUND ; dso->loaded =0 ; dso->sorted_by_name =0 ; dso->has_build_id =0 ;
@@ -2822,3 +2831,101 @@ int machine__load_vmlinux_path(struct machine *machine, enum map_type type,return ret; } + +static int open_dso (struct dso *dso,struct machine *machine) +{ +char *root_dir = (char *)"" ; +char *name; +int fd; + + name =malloc (PATH_MAX ); +if (!name) +return -ENOMEM ; + +if (machine) + root_dir = machine->root_dir; + +if (dso__binary_type_file (dso, dso->data_type, + root_dir, name,PATH_MAX )) +return -EINVAL ; + + fd =open (name,O_RDONLY ); +free (name); +return fd; +} + +int dso__data_fd (struct dso *dso,struct machine *machine) +{ +int i =0 ; + +if (dso->data_type !=DSO_BINARY_TYPE__NOT_FOUND ) +return open_dso (dso, machine); + +do { +int fd; + + dso->data_type = binary_type_data[i++]; + + fd =open_dso (dso, machine); +if (fd >=0 ) +return fd; + + }while (dso->data_type !=DSO_BINARY_TYPE__NOT_FOUND ); + +return -EINVAL ; +} + +static ssize_t dso_cache_read (struct dso *dso__used ,u64 offset__used , +u8 *data__used ,ssize_t size__used ) +{ +return -EINVAL ; +} + +static int dso_cache_add (struct dso *dso__used ,u64 offset__used , +u8 *data__used ,ssize_t size__used ) +{ +return 0 ; +} + +static ssize_t read_dso_data (struct dso *dso,struct machine *machine, +u64 offset,u8 *data,ssize_t size) +{ +ssize_t rsize =-1 ; +int fd; + + fd =dso__data_fd (dso, machine); +if (fd <0 ) +return -1 ; + +do { +if (-1 ==lseek (fd, offset,SEEK_SET )) +break ; + + rsize =read (fd, data, size); +if (-1 == rsize) +break ; + +if (dso_cache_add (dso, offset, data, size)) +pr_err ("Failed to add data int dso cache." ); + + }while (0 ); + +close (fd); +return rsize; +} + +ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine, +u64 offset,u8 *data,ssize_t size) +{ +if (dso_cache_read (dso, offset, data, size)) +return read_dso_data (dso, machine, offset, data, size); +return 0 ; +} + +ssize_t dso__data_read_addr (struct dso *dso,struct map *map, +struct machine *machine,u64 addr, +u8 *data,ssize_t size) +{ +u64 offset = map->map_ip (map, addr); +return dso__data_read_offset (dso, machine, offset, data, size); +}
tools/perf/util/symbol.h +8 -0
@@ -175,6 +175,7 @@ struct dso {struct rb_root symbol_names[MAP__NR_TYPES ];enum dso_kernel_type kernel;enum dso_binary_type symtab_type; +enum dso_binary_type data_type; u8 adjust_symbols:1; u8 has_build_id:1; u8 hit:1;
@@ -269,4 +270,11 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);int dso__binary_type_file (struct dso *dso,enum dso_binary_type type,char *root_dir,char *file,size_t size); + +int dso__data_fd (struct dso *dso,struct machine *machine); +ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine, +u64 offset,u8 *data,ssize_t size); +ssize_t dso__data_read_addr (struct dso *dso,struct map *map, +struct machine *machine,u64 addr, +u8 *data,ssize_t size);#endif /* __PERF_SYMBOL */
Signature · 1 line
1.7.1
PATCH 9/15 perf, tool: Add '.note' check into search for NOTE section
In reply to Jiri OlsaAdding '.note' section name to be check when looking for notes section. The '.note' name is used by kernel VDSO.
--- tools/perf/util/symbol.c | 29 +++++++++++++++++++++++------ 1 files changed, 23 insertions(+), 6 deletions(-)
tools/perf/util/symbol.c +23 -6
@@ -1473,14 +1473,31 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size) goto out; } - sec =elf_section_by_name (elf, &ehdr, &shdr, -".note.gnu.build-id" ,NULL ); -if (sec ==NULL ) { +/* +* Check following sections for notes: +* '.note.gnu.build-id' +* '.notes' +* '.note' (VDSO specific) +*/ +do { + sec =elf_section_by_name (elf, &ehdr, &shdr, +".note.gnu.build-id" ,NULL ); +if (sec) +break ; + sec =elf_section_by_name (elf, &ehdr, &shdr,".notes" ,NULL ); -if (sec ==NULL ) - goto out; - } +if (sec) +break ; + + sec =elf_section_by_name (elf, &ehdr, &shdr, +".note" ,NULL ); +if (sec) +break ; + +return err; + + }while (0 ); data =elf_getdata (sec,NULL );if (data ==NULL )
Signature · 1 line
1.7.1
PATCH 10/15 perf, tool: Back [vdso] DSO with real data
In reply to Jiri OlsaStoring data for VDSO shared object, because we need it for the unwind process.
The idea is that VDSO shared object is same for all process on a running system, so it makes no difference if we store it inside the tracer - perf.
The record command:
When [vdso] map memory is hit, we retrieve [vdso] DSO image and store it into temporary file. During the build-id processing the [vdso] DSO image is stored as in build-id db, and build-id refference is made inside perf.data. The temporary file is removed when record is finished.
The report command:
We read build-id from perf.data and store [vdso] DSO object. This object is refferenced and attached to map when the MMAP events are processed. Thus during the SAMPLE event processing we have correct mmap/dso attached.
Adding following functions for vdso object:
vdso__get_filename
- finds and store VDSO image into temp file,
the temp file path is returned
vdso__exit
- removes temporary VDSO image if there's any
--- tools/perf/Makefile | 2 + tools/perf/builtin-record.c | 3 + tools/perf/util/map.c | 16 +++++++- tools/perf/util/vdso.c | 92 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/vdso.h | 7 +++ 5 files changed, 119 insertions(+), 1 deletions(-) create mode 100644 tools/perf/util/vdso.c create mode 100644 tools/perf/util/vdso.h
tools/perf/Makefile +2 -0
@@ -317,6 +317,7 @@ LIB_H += util/cpumap.h LIB_H += util/top.h LIB_H += $(ARCH_INCLUDE) LIB_H += util/cgroup.h +LIB_H += util/vdso.h LIB_OBJS += $(OUTPUT)util/abspath.o LIB_OBJS += $(OUTPUT)util/alias.o
@@ -378,6 +379,7 @@ LIB_OBJS += $(OUTPUT)util/util.o LIB_OBJS += $(OUTPUT)util/xyarray.o LIB_OBJS += $(OUTPUT)util/cpumap.o LIB_OBJS += $(OUTPUT)util/cgroup.o +LIB_OBJS += $(OUTPUT)util/vdso.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
tools/perf/builtin-record.c +3 -0
@@ -26,6 +26,7 @@#include "util/symbol.h" #include "util/cpumap.h" #include "util/thread_map.h" +#include "util/vdso.h" #include <unistd.h> #include <sched.h>
@@ -336,6 +337,7 @@ static void perf_record__exit(int status __used, void *arg)perf_session__delete (rec->session);perf_evlist__delete (rec->evlist);symbol__exit (); +vdso__exit (); } }
@@ -924,5 +926,6 @@ out_free_fd:perf_evlist__delete_maps (evsel_list); out_symbol_exit:symbol__exit (); +vdso__exit ();return err; }
tools/perf/util/map.c +15 -1
@@ -7,6 +7,7 @@#include <stdio.h> #include <unistd.h> #include "map.h" +#include "vdso.h" const char *map_type__name[MAP__NR_TYPES ] = { [MAP__FUNCTION ] ="Functions" ,
@@ -18,10 +19,14 @@ static inline int is_anon_memory(const char *filename)return strcmp (filename,"//anon" ) ==0 ; } +static inline int is_vdso_memory (const char *filename) +{ +return !strcmp (filename,"[vdso]" ); +} +static inline int is_no_dso_memory (const char *filename) {return !strcmp (filename,"[stack]" ) || - !strcmp (filename,"[vdso]" ) || !strcmp (filename,"[heap]" ); }
@@ -63,6 +68,15 @@ struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,if (dso ==NULL ) goto out_delete; +if (is_vdso_memory (filename) && !dso->has_build_id) { +char *vdso =vdso__get_filename (); +if (vdso) { +dso__set_long_name (dso, vdso); + pgoff =0 ; + }else + no_dso =1 ; + } +map__init (self, type, start, start + len, pgoff, dso);if (anon || no_dso) {
tools/perf/util/vdso.c new file +92 -0
92 lines not on this page · raw message
tools/perf/util/vdso.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef __VDSO__ +#define __VDSO__ + +char *vdso__get_filename (void ); +void vdso__exit (void ); + +#endif /* __VDSO__ */
Signature · 1 line
1.7.1
PATCH 12/15 perf, tool: Add libunwind dependency for dwarf cfi unwinding
In reply to Jiri OlsaAdding libunwind to be linked with perf if available. It's required for the to get dwarf cfi unwinding support.
Also building perf with the dwarf call frame informations by default, so that we can unwind callchains in perf itself.
Adding LIBUNWIND_DIR Makefile variable allowing user to specify the directory with libunwind to be linked. This is used for debug purposes.
--- tools/perf/Makefile | 27 ++++++++++++++++++++++++++- tools/perf/config/feature-tests.mak | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletions(-)
tools/perf/Makefile +26 -1
@@ -69,6 +69,7 @@ BISON= $(CROSS_COMPILE)bison ifeq ($(ARCH),i386) ARCH := x86 NO_PERF_REGS_DEFS := 0 + LIBUNWIND_LIBS = -lunwind -lunwind-x86 endif ifeq ($(ARCH),x86_64) ARCH := x86
@@ -82,6 +83,7 @@ ifeq ($(ARCH),x86_64) ARCH_INCLUDE = ../../arch/x86/lib/memcpy_64.S ../../arch/x86/lib/memset_64.S endif NO_PERF_REGS_DEFS := 0 + LIBUNWIND_LIBS = -lunwind -lunwind-x86_64 endif # Treat warnings as errors unless directed not to
@@ -121,7 +123,7 @@ ifndef PERF_DEBUG CFLAGS_OPTIMIZE = -O6 endif -CFLAGS = -fno-omit-frame-pointer -ggdb3 -Wall -Wextra -std=gnu99 $(CFLAGS_WERROR) $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) +CFLAGS = -fno-omit-frame-pointer -ggdb3 -funwind-tables -Wall -Wextra -std=gnu99 $(CFLAGS_WERROR) $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) EXTLIBS = -lpthread -lrt -lelf -lm ALL_CFLAGS = $(CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE ALL_LDFLAGS = $(LDFLAGS)
@@ -454,6 +456,21 @@ ifneq ($(call try-cc,$(SOURCE_DWARF),$(FLAGS_DWARF)),y) endif # Dwarf support endif # NO_DWARF +ifndef NO_LIBUNWIND +# for linding with debug library, run like: +# make DEBUG=1 LIBUNWIND_DIR=/opt/libunwind/ +ifdef LIBUNWIND_DIR + LIBUNWIND_CFLAGS := -I$(LIBUNWIND_DIR)/include + LIBUNWIND_LDFLAGS := -L$(LIBUNWIND_DIR)/lib +endif + +FLAGS_UNWIND=$(LIBUNWIND_CFLAGS) $(ALL_CFLAGS) $(LIBUNWIND_LDFLAGS) $(ALL_LDFLAGS) $(EXTLIBS) $(LIBUNWIND_LIBS) +ifneq ($(call try-cc,$(SOURCE_LIBUNWIND),$(FLAGS_UNWIND)),y) + msg := $(warning No libunwind found. Please install libunwind >= 0.99); + NO_LIBUNWIND := 1 +endif # Libunwind support +endif # NO_LIBUNWIND + -include arch/$(ARCH)/Makefile ifneq ($(OUTPUT),)
@@ -485,6 +502,14 @@ else endif # PERF_HAVE_DWARF_REGS endif # NO_DWARF +ifdef NO_LIBUNWIND + BASIC_CFLAGS += -DNO_LIBUNWIND_SUPPORT +else + EXTLIBS += $(LIBUNWIND_LIBS) + BASIC_CFLAGS := $(LIBUNWIND_CFLAGS) $(BASIC_CFLAGS) + BASIC_LDFLAGS := $(LIBUNWIND_LDFLAGS) $(BASIC_LDFLAGS) +endif + ifdef NO_NEWT BASIC_CFLAGS += -DNO_NEWT_SUPPORT else
tools/perf/config/feature-tests.mak +25 -0
@@ -141,3 +141,28 @@ int main(void) return 0; } endef + +ifndef NO_LIBUNWIND +define SOURCE_LIBUNWIND +#include <libunwind.h> +#include <stdlib.h> + +extern int UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as, + unw_word_t ip, + unw_dyn_info_t *di, + unw_proc_info_t *pi, + int need_unwind_info, void *arg); + + +#define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table) + +int main(void) +{ + unw_addr_space_t addr_space; + addr_space = unw_create_addr_space(NULL, 0); + unw_init_remote(NULL, addr_space, NULL); + dwarf_search_unwind_table(addr_space, 0, NULL, NULL, 0, NULL); + return 0; +} +endef +endif
Signature · 1 line
1.7.1
PATCH 11/15 perf, tool: Add interface to arch registers sets
In reply to Jiri OlsaAdding header files to access unified API for arch registers.
In addition adding a way to obtain register name based on the API ID value.
Also adding PERF_REGS_MASK macro with mask definition of all current arch registers (will be used in unwind patches).
--- tools/perf/Makefile | 9 +++- tools/perf/arch/x86/include/perf_regs.h | 101 +++++++++++++++++++++++++++++++ tools/perf/util/perf_regs.h | 10 +++ 3 files changed, 119 insertions(+), 1 deletions(-) create mode 100644 tools/perf/arch/x86/include/perf_regs.h create mode 100644 tools/perf/util/perf_regs.h
tools/perf/Makefile +8 -1
@@ -58,6 +58,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \ -e s/s390x/s390/ -e s/parisc64/parisc/ \ -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \ -e s/sh[234].*/sh/ ) +NO_PERF_REGS_DEFS := 1 CC = $(CROSS_COMPILE)gcc AR = $(CROSS_COMPILE)ar
@@ -66,7 +67,8 @@ BISON= $(CROSS_COMPILE)bison # Additional ARCH settings for x86 ifeq ($(ARCH),i386) - ARCH := x86 + ARCH := x86 + NO_PERF_REGS_DEFS := 0 endif ifeq ($(ARCH),x86_64) ARCH := x86
@@ -79,6 +81,7 @@ ifeq ($(ARCH),x86_64) ARCH_CFLAGS := -DARCH_X86_64 ARCH_INCLUDE = ../../arch/x86/lib/memcpy_64.S ../../arch/x86/lib/memset_64.S endif + NO_PERF_REGS_DEFS := 0 endif # Treat warnings as errors unless directed not to
@@ -318,6 +321,7 @@ LIB_H += util/top.h LIB_H += $(ARCH_INCLUDE) LIB_H += util/cgroup.h LIB_H += util/vdso.h +LIB_H += util/perf_regs.h LIB_OBJS += $(OUTPUT)util/abspath.o LIB_OBJS += $(OUTPUT)util/alias.o
@@ -655,6 +659,9 @@ else endif endif +ifeq ($(NO_PERF_REGS_DEFS),1) + BASIC_CFLAGS += -DNO_PERF_REGS_DEFS +endif ifdef NO_STRLCPY BASIC_CFLAGS += -DNO_STRLCPY
tools/perf/arch/x86/include/perf_regs.h new file +101 -0
101 lines not on this page · raw message
tools/perf/util/perf_regs.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef __PERF_REGS_H +#define __PERF_REGS_H + +#ifndef NO_PERF_REGS_DEFS +#include <perf_regs.h> +#else +#define PERF_REGS_MASK 0 +#endif /* NO_PERF_REGS_DEFS */ + +#endif /* __PERF_REGS_H */
Signature · 1 line
1.7.1
PATCH 13/15 perf, tool: Support user regs and stack in sample parsing
In reply to Jiri OlsaAdding following info to be parsed out of the event sample:
- user register set
- user stack dump
Both are global and specific to all events within the session. This info will be used in the unwind patches comming in shortly.
--- tools/perf/builtin-test.c | 3 ++- tools/perf/util/event.h | 15 ++++++++++++++- tools/perf/util/evlist.c | 16 ++++++++++++++++ tools/perf/util/evlist.h | 2 ++ tools/perf/util/evsel.c | 25 +++++++++++++++++++++++++ tools/perf/util/python.c | 3 ++- tools/perf/util/session.c | 3 +++ tools/perf/util/session.h | 7 ++++++- 8 files changed, 70 insertions(+), 4 deletions(-)
tools/perf/builtin-test.c +2 -1
@@ -564,7 +564,7 @@ static int test__basic_mmap(void) } err =perf_event__parse_sample (event, attr.sample_type, sample_size, - false, &sample, false); + false,0 , false, &sample, false);if (err) {pr_err ("Can't parse sample, err = %d\n" , err); goto out_munmap;
@@ -1307,6 +1307,7 @@ static int test__PERF_RECORD(void) err =perf_event__parse_sample (event, sample_type, sample_size, true, +0 , false, &sample, false);if (err <0 ) {if (verbose)
tools/perf/util/event.h +14 -1
@@ -69,6 +69,16 @@ struct sample_event {u64 array[]; }; +struct user_regs { +u64 version; +u64 *regs; +}; + +struct user_stack_dump { +u64 size; +char *data; +}; +struct perf_sample {u64 ip;u32 pid, tid;
@@ -82,6 +92,8 @@ struct perf_sample {void *raw_data;struct ip_callchain *callchain;struct branch_stack *branch_stack; +struct user_regs uregs; +struct user_stack_dump stack; };#define BUILD_ID_SIZE 20
@@ -199,7 +211,8 @@ const char *perf_event__name(unsigned int id);int perf_event__parse_sample (const union perf_event *event,u64 type,int sample_size,bool sample_id_all, -struct perf_sample *sample,bool swapped); +u64 sample_uregs,bool sample_ustack, +struct perf_sample *data,bool swapped);int perf_event__synthesize_sample (union perf_event *event,u64 type,const struct perf_sample *sample,bool swapped);
tools/perf/util/evlist.c +16 -0
@@ -672,6 +672,22 @@ bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist)return true; } +u64 perf_evlist_sample_uregs (const struct perf_evlist *evlist) +{ +struct perf_evsel *first; + + first = list_entry(evlist->entries.next, struct perf_evsel, node); +return first->attr.user_regs; +} + +bool perf_evlist_sample_ustack (const struct perf_evlist *evlist) +{ +struct perf_evsel *first; + + first = list_entry(evlist->entries.next, struct perf_evsel, node); +return (first->attr.ustack_dump_size !=0 ); +} +u64 perf_evlist__sample_type (const struct perf_evlist *evlist) {struct perf_evsel *first;
tools/perf/util/evlist.h +2 -0
@@ -117,6 +117,8 @@ u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist);bool perf_evlist__valid_sample_type (const struct perf_evlist *evlist);bool perf_evlist__valid_sample_id_all (const struct perf_evlist *evlist); +u64 perf_evlist_sample_uregs (const struct perf_evlist *evlist); +bool perf_evlist_sample_ustack (const struct perf_evlist *evlist); void perf_evlist__splice_list_tail(struct perf_evlist *evlist, struct list_head *list,
tools/perf/util/evsel.c +25 -0
@@ -8,6 +8,7 @@*/ #include <byteswap.h> +#include <linux/bitops.h> #include "asm/bug.h" #include "evsel.h" #include "evlist.h"
@@ -453,8 +454,10 @@ static bool sample_overlap(const union perf_event *event,int perf_event__parse_sample (const union perf_event *event,u64 type,int sample_size,bool sample_id_all, +u64 sample_uregs,bool sample_ustack,struct perf_sample *data,bool swapped) { +int sample_uregs_nr =hweight_long (sample_uregs);const u64 *array;/*
@@ -593,6 +596,28 @@ int perf_event__parse_sample(const union perf_event *event, u64 type, sz /=sizeof (u64); array += sz; } + +if (sample_uregs_nr) { + data->uregs.version = *array++; + +if (data->uregs.version) { + data->uregs.regs = (u64 *)array; + array += sample_uregs_nr; + } + } + +if (sample_ustack) { +u64 size = *array++; + +if (!size) { + data->stack.size =0 ; + }else { + data->stack.data = (char *)array; + array += size /sizeof (*array); + data->stack.size = *array; + } + } +return 0 ; }
tools/perf/util/python.c +2 -1
@@ -807,7 +807,8 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, first = list_entry(evlist->entries.next, struct perf_evsel, node); err =perf_event__parse_sample (event, first->attr.sample_type,perf_evsel__sample_size (first), - sample_id_all, &pevent->sample, false); + sample_id_all,0 , false, + &pevent->sample, false);if (err)return PyErr_Format (PyExc_OSError,"perf: can't parse sample, err=%d" , err);
tools/perf/util/session.c +3 -0
@@ -86,6 +86,9 @@ void perf_session__update_sample_type(struct perf_session *self) self->sample_id_all =perf_evlist__sample_id_all (self->evlist); self->id_hdr_size =perf_evlist__id_hdr_size (self->evlist); self->host_machine.id_hdr_size = self->id_hdr_size; + + self->sample_uregs =perf_evlist_sample_uregs (self->evlist); + self->sample_ustack =perf_evlist_sample_ustack (self->evlist); }int perf_session__create_kernel_maps (struct perf_session *self)
tools/perf/util/session.h +6 -1
@@ -42,6 +42,8 @@ struct perf_session {struct hists hists;u64 sample_type;int sample_size; +u64 sample_uregs; +bool sample_ustack;int fd;bool fd_pipe;bool repipe;
@@ -134,7 +136,10 @@ static inline int perf_session__parse_sample(struct perf_session *session, {return perf_event__parse_sample (event, session->sample_type, session->sample_size, - session->sample_id_all, sample, + session->sample_id_all, + session->sample_uregs, + session->sample_ustack, + sample, session->header.needs_swap); }
Signature · 1 line
1.7.1
PATCH 14/15 perf, tool: Support for dwarf cfi unwinding on post processing
In reply to Jiri OlsaThis brings the support for dwarf cfi unwinding on perf post processing. Call frame informations are retrieved and then passed to libunwind that requests memory and register content from the applications.
Adding unwind object to handle the user stack backtrace based on the user register values and user stack dump.
The unwind object access the libunwind via remote interface and provides to it all the necessary data to unwind the stack.
The unwind interface provides following function:
unwind__get_entries
And callback (specified in above function) to retrieve the backtrace entries:
typedef int (*unwind_entry_cb_t)(struct unwind_entry *entry, void *arg);
--- tools/perf/Makefile | 2 + tools/perf/arch/x86/Makefile | 3 + tools/perf/arch/x86/util/unwind.c | 111 ++++ tools/perf/builtin-report.c | 24 +- tools/perf/builtin-script.c | 56 ++- tools/perf/builtin-top.c | 5 +- tools/perf/util/include/linux/compiler.h | 1 + tools/perf/util/map.h | 7 +- .../perf/util/scripting-engines/trace-event-perl.c | 3 +- .../util/scripting-engines/trace-event-python.c | 3 +- tools/perf/util/session.c | 97 +++- tools/perf/util/session.h | 3 +- tools/perf/util/trace-event-scripting.c | 3 +- tools/perf/util/trace-event.h | 5 +- tools/perf/util/unwind.c | 563 ++++++++++++++++++++ tools/perf/util/unwind.h | 34 ++ 16 files changed, 867 insertions(+), 53 deletions(-) create mode 100644 tools/perf/arch/x86/util/unwind.c create mode 100644 tools/perf/util/unwind.c create mode 100644 tools/perf/util/unwind.h
tools/perf/Makefile +2 -0
@@ -324,6 +324,7 @@ LIB_H += $(ARCH_INCLUDE) LIB_H += util/cgroup.h LIB_H += util/vdso.h LIB_H += util/perf_regs.h +LIB_H += util/unwind.h LIB_OBJS += $(OUTPUT)util/abspath.o LIB_OBJS += $(OUTPUT)util/alias.o
@@ -508,6 +509,7 @@ else EXTLIBS += $(LIBUNWIND_LIBS) BASIC_CFLAGS := $(LIBUNWIND_CFLAGS) $(BASIC_CFLAGS) BASIC_LDFLAGS := $(LIBUNWIND_LDFLAGS) $(BASIC_LDFLAGS) + LIB_OBJS += $(OUTPUT)util/unwind.o endif ifdef NO_NEWT
tools/perf/arch/x86/Makefile +3 -0
@@ -2,4 +2,7 @@ ifndef NO_DWARF PERF_HAVE_DWARF_REGS := 1 LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o endif +ifndef NO_LIBUNWIND +LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind.o +endif LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o
tools/perf/arch/x86/util/unwind.c new file +111 -0
111 lines not on this page · raw message
tools/perf/builtin-report.c +13 -11
@@ -69,8 +69,8 @@ static int perf_report__add_branch_hist_entry(struct perf_tool *tool,if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) { - err =machine__resolve_callchain (machine, evsel, al->thread, - sample->callchain, &parent); + err =machine__resolve_callchain (rep->session, machine, evsel, + al->thread, sample, &parent);if (err)return err; }
@@ -130,7 +130,8 @@ out:return err; } -static int perf_evsel__add_hist_entry (struct perf_evsel *evsel, +static int perf_evsel__add_hist_entry (struct perf_session *session, +struct perf_evsel *evsel,struct addr_location *al,struct perf_sample *sample,struct machine *machine)
@@ -140,8 +141,8 @@ static int perf_evsel__add_hist_entry(struct perf_evsel *evsel,struct hist_entry *he;if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) { - err =machine__resolve_callchain (machine, evsel, al->thread, - sample->callchain, &parent); + err =machine__resolve_callchain (session, machine, evsel, + al->thread, sample, &parent);if (err)return err; }
@@ -213,7 +214,8 @@ static int process_sample_event(struct perf_tool *tool,if (al.map !=NULL ) al.map->dso->hit =1 ; -if (perf_evsel__add_hist_entry (evsel, &al, sample, machine)) { +if (perf_evsel__add_hist_entry (rep->session, evsel, &al, + sample, machine)) {pr_debug ("problem incrementing symbol period, skipping event\n" );return -1 ; }
@@ -386,17 +388,17 @@ static int __cmd_report(struct perf_report *rep) "If some relocation was applied (e.g. kexec) symbols may be misresolved."); } -if (dump_trace) { -perf_session__fprintf_nr_events (session, stdout); - goto out_delete; - } -if (verbose >3 )perf_session__fprintf (session, stdout);if (verbose >2 )perf_session__fprintf_dsos (session, stdout); +if (dump_trace) { +perf_session__fprintf_nr_events (session, stdout); + goto out_delete; + } + nr_samples =0 ;list_for_each_entry (pos, &session->evlist->entries, node) {struct hists *hists = &pos->hists;
tools/perf/builtin-script.c +36 -20
124 lines not on this page · raw message
tools/perf/builtin-top.c +3 -2
@@ -740,8 +740,9 @@ static void perf_event__process_sample(struct perf_tool *tool,if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) { - err =machine__resolve_callchain (machine, evsel, al.thread, - sample->callchain, &parent); + err =machine__resolve_callchain (top->session, + machine, evsel, al.thread, + sample, &parent);if (err)return ; }
tools/perf/util/include/linux/compiler.h +1 -0
@@ -10,5 +10,6 @@#endif #define __used __attribute__((__unused__)) +#define __packed __attribute__((__packed__))#endif
tools/perf/util/map.h +5 -2
@@ -155,9 +155,12 @@ int machine__init(struct machine *self, const char *root_dir, pid_t pid);void machine__exit (struct machine *self);void machine__delete (struct machine *self); -int machine__resolve_callchain (struct machine *machine, +struct perf_session ; +struct perf_sample ; +int machine__resolve_callchain (struct perf_session *session, +struct machine *machine,struct perf_evsel *evsel,struct thread *thread, -struct ip_callchain *chain, +struct perf_sample *sample,struct symbol **parent);int maps__set_kallsyms_ref_reloc_sym (struct map **maps,const char *symbol_name,u64 addr);
tools/perf/util/scripting-engines/trace-event-perl.c +2 -1
@@ -364,7 +364,8 @@ static void perl_process_event_generic(union perf_event *pevent __unused,LEAVE ; } -static void perl_process_event(union perf_event *pevent, +static void perl_process_event(struct perf_session *session __used, + union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine,
tools/perf/util/scripting-engines/trace-event-python.c +2 -1
@@ -205,7 +205,8 @@ static inline struct event *find_cache_event(int type)return event; } -static void python_process_event(union perf_event *pevent __unused, +static void python_process_event(struct perf_session *session, + union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel __unused, struct machine *machine __unused,
tools/perf/util/session.c +84 -13
158 lines not on this page · raw message
tools/perf/util/session.h +2 -1
@@ -154,7 +154,8 @@ static inline int perf_session__synthesize_sample(struct perf_session *session,struct perf_evsel *perf_session__find_first_evtype (struct perf_session *session,unsigned int type); -void perf_event__print_ip (union perf_event *event,struct perf_sample *sample, +void perf_event__print_ip (struct perf_session *session, +union perf_event *event,struct perf_sample *sample,struct machine *machine,struct perf_evsel *evsel,int print_sym,int print_dso,int print_symoffset);
tools/perf/util/trace-event-scripting.c +2 -1
@@ -35,7 +35,8 @@ static int stop_script_unsupported(void)return 0 ; } -static void process_event_unsupported(union perf_event *event __unused, +static void process_event_unsupported(struct perf_session *session __used, + union perf_event *event __unused, struct perf_sample *sample __unused, struct perf_evsel *evsel __unused, struct machine *machine __unused,
tools/perf/util/trace-event.h +4 -1
@@ -289,11 +289,14 @@ enum trace_flag_type { TRACE_FLAG_SOFTIRQ = 0x10, }; +struct perf_session ; +struct scripting_ops {const char *name;int (*start_script) (const char *script,int argc,const char **argv);int (*stop_script) (void ); - void (*process_event) (union perf_event *event, + void (*process_event) (struct perf_session *session, + union perf_event *event, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine,
tools/perf/util/unwind.c new file +563 -0
563 lines not on this page · raw message
tools/perf/util/unwind.h new file +34 -0
@@ -0,0 +1,34 @@ +#ifndef __UNWIND_H +#define __UNWIND_H + +#include "types.h" +#include "event.h" +#include "symbol.h" + +struct unwind_entry { +struct map *map; +struct symbol *sym; +u64 ip; +}; + +typedef int (*unwind_entry_cb_t )(struct unwind_entry *entry,void *arg); + +#ifndef NO_LIBUNWIND_SUPPORT +int unwind__get_entries (unwind_entry_cb_t cb,void *arg, +struct machine *machine, +struct thread *thread, +u64 sample_uregs, +struct perf_sample *data); +int unwind__arch_reg_id (int regnum); +#else +static inline int +unwind__get_entries (unwind_entry_cb_t cb__used ,void *arg__used , +struct machine *machine__used , +struct thread *thread__used , +u64 sample_uregs__used , +struct perf_sample *data__used ) +{ +return 0 ; +} +#endif /* NO_LIBUNWIND_SUPPORT */ +#endif /* __UNWIND_H */
Signature · 1 line
1.7.1
PATCH 15/15 perf, tool: Support for dwarf mode callchain on perf record
In reply to Jiri OlsaThis patch enables perf to use the dwarf unwind code.
It extends the perf record '-g' option with following arguments:
'fp' - provides framepointer based user
stack backtrace
'dwarf[,size]' - provides dwarf (libunwind) based user stack
backtrace. The size specifies the size of the user stack dump. If ommited it is 8192 by default.
If libunwind is found during the perf build, then the 'dwarf' argument becomes the default option. If not, 'fp' stays as default and 'dwarf' is not available. The '-g' usage help text is updated accordingly.
Examples: (perf compiled with libunwind)
perf record -g -- ls
perf record -g dwarf ls
- provide dwarf unwind with 8192 as stack dump size
perf record -g dwarf,4096 ls
- provides dwarf unwind with 4096 as stack dump size
perf record -g fp ls
- provides frame pointer unwind
--- tools/perf/builtin-record.c | 86 ++++++++++++++++++++++++++++++++++++++++++- tools/perf/perf.h | 9 ++++- tools/perf/util/evsel.c | 10 ++++- 3 files changed, 101 insertions(+), 4 deletions(-)
tools/perf/builtin-record.c +84 -2
@@ -27,11 +27,25 @@#include "util/cpumap.h" #include "util/thread_map.h" #include "util/vdso.h" +#include "util/perf_regs.h" #include <unistd.h> #include <sched.h> #include <sys/mman.h> +#define CALLCHAIN_HELP "do call-graph (stack chain/backtrace) recording " + +#ifdef NO_LIBUNWIND_SUPPORT +static const char callchain_default_opt[] ="fp" ; +static unsigned long default_stack_dump_size; +static char callchain_help[] =CALLCHAIN_HELP "[fp]" ; +#else +static const char callchain_default_opt[] ="dwarf" ; +static unsigned long default_stack_dump_size =8192 ; +static char callchain_help[] =CALLCHAIN_HELP "[dwarf]" ; +#endif + +enum write_mode_t {WRITE_FORCE ,WRITE_APPEND
@@ -727,6 +741,73 @@ error:return ret; } +static int +parse_callchain_opt (const struct option *opt__used ,const char *arg, +int unset) +{ +struct perf_record *rec = (struct perf_record *)opt->value; +char *tok, *name, *saveptr =NULL ; +char buf[20 ]; +int ret =-1 ; + +/* --no-call-graph */ +if (unset) +return 0 ; + +/* We specified default option if none is provided. */ +BUG_ON (!arg); + +/* We need buffer that we know we can write to. */ +snprintf (buf,20 ,"%s" , arg); + + tok =strtok_r ((char *)buf,"," , &saveptr); + name = tok ? : (char *)buf; + +do { +/* Framepointer style */ +if (!strncmp (name,"fp" ,sizeof ("fp" ))) { +if (!strtok_r (NULL ,"," , &saveptr)) { + rec->opts.call_graph =CALLCHAIN_FP ; + ret =0 ; + }else +pr_err ("callchain: No more arguments " +"needed for -g fp\n" ); +break ; + } + +/* Dwarf style */ +if (strncmp (name,"dwarf" ,sizeof ("dwarf" ))) { +pr_err ("callchain: Unknown -g option " +"value: %s\n" , name); +break ; + } + + ret =0 ; + rec->opts.call_graph =CALLCHAIN_DWARF ; + rec->opts.stack_dump_size = default_stack_dump_size; + + tok =strtok_r (NULL ,"," , &saveptr); +if (tok) { +char *endptr; + + rec->opts.stack_dump_size =strtoul (tok, &endptr,0 ); +if (*endptr) { +pr_err ("callchain: Incorrect stack " +"dump size: %s\n" , tok); + ret =-1 ; + } + } + + }while (0 ); + +if (!ret) +pr_debug ("callchain: type %d, size %lu\n" , + rec->opts.call_graph, + rec->opts.stack_dump_size); + +return ret; +} +static const char *const record_usage[] = {"perf record [<options>] [<command>]" ,"perf record [<options>] -- <command> [<options>]" ,
@@ -795,8 +876,9 @@ const struct option record_options[] = { "number of mmap data pages"),OPT_BOOLEAN (0 ,"group" , &record.opts.group,"put the counters into a counter group" ), -OPT_BOOLEAN ('g' ,"call-graph" , &record.opts.call_graph, -"do call-graph (stack chain/backtrace) recording" ), +OPT_CALLBACK_DEFAULT ('g' ,"call-graph" , &record,"mode,dump_size" , + callchain_help, &parse_callchain_opt, + callchain_default_opt),OPT_INCR ('v' ,"verbose" , &verbose,"be more verbose (show counter open errors, etc)" ), OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
tools/perf/perf.h +8 -1
@@ -207,11 +207,17 @@ extern const char perf_version_string[];void pthread__unblock_sigwinch (void ); +enum perf_call_graph_mode { +CALLCHAIN_NONE , +CALLCHAIN_FP , +CALLCHAIN_DWARF +}; +struct perf_record_opts {const char *target_pid;const char *target_tid;uid_t uid; -bool call_graph; +int call_graph;bool group;bool inherit_stat;bool no_delay;
@@ -232,6 +238,7 @@ struct perf_record_opts {u64 default_interval;u64 user_interval;const char *cpu_list; + unsigned long stack_dump_size; };#endif
tools/perf/util/evsel.c +9 -1
@@ -15,6 +15,7 @@#include "util.h" #include "cpumap.h" #include "thread_map.h" +#include "perf_regs.h" #define FD (e, x, y) (*(int *)xyarray__entry(e->fd, x, y))#define GROUP_FD (group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
@@ -104,9 +105,16 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, attr->mmap_data = track; } -if (opts->call_graph) +if (opts->call_graph) { attr->sample_type |=PERF_SAMPLE_CALLCHAIN ; +if (opts->call_graph ==CALLCHAIN_DWARF ) { + attr->user_regs =PERF_REGS_MASK ; + attr->ustack_dump_size = opts->stack_dump_size; + attr->exclude_user_callchain =1 ; + } + } +if (opts->system_wide) attr->sample_type |=PERF_SAMPLE_CPU ;
Signature · 1 line
1.7.1
PATCH 6/15 perf: Add attribute to filter out user callchains
In reply to Jiri OlsaAdd the new exclude_user_callchain attribute to filter out frame pointer based user callchains. This is something we want to select when we use the dwarf cfi callchain mode, because frame pointer based user callchains are useless in this mode.
--- include/linux/perf_event.h | 4 +++- kernel/events/callchain.c | 4 ++-- kernel/events/core.c | 3 ++- kernel/events/internal.h | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-)
include/linux/perf_event.h +3 -1
@@ -255,7 +255,9 @@ struct perf_event_attr { exclude_host : 1, /* don't count in host */ exclude_guest : 1, /* don't count in guest */ - __reserved_1 : 43; + exclude_user_callchain : 1, /* only record kernel callchains */ + + __reserved_1 : 42;union {__u32 wakeup_events;/* wakeup every n events */
kernel/events/callchain.c +2 -2
@@ -153,7 +153,7 @@ put_callchain_entry(int rctx)put_recursion_context (__get_cpu_var (callchain_recursion), rctx); } -struct perf_callchain_entry *perf_callchain (struct pt_regs *regs) +struct perf_callchain_entry *perf_callchain (struct pt_regs *regs,int user) {int rctx;struct perf_callchain_entry *entry;
@@ -177,7 +177,7 @@ struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) regs =NULL ; } -if (regs) { +if (user && regs) {perf_callchain_store (entry,PERF_CONTEXT_USER );perf_callchain_user (entry, regs); }
kernel/events/core.c +2 -1
@@ -4117,8 +4117,9 @@ void perf_prepare_sample(struct perf_event_header *header,if (sample_type &PERF_SAMPLE_CALLCHAIN ) {int size =1 ; +int user = !event->attr.exclude_user_callchain; - data->callchain =perf_callchain (regs); + data->callchain =perf_callchain (regs, user);if (data->callchain) size += data->callchain->nr;
kernel/events/internal.h +2 -1
@@ -115,7 +115,8 @@ DEFINE_PERF_OUTPUT_COPY(__output_copy, memcpy_common)DEFINE_PERF_OUTPUT_COPY (__output_copy_user_gup, copy_from_user_gup) /* Callchain handling */ -extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs); +extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs, +int user);extern int get_callchain_buffers (void );extern void put_callchain_buffers (void );
Signature · 1 line
1.7.1
PATCH 4/15 perf: Add ability to dump user regs
In reply to Jiri OlsaAdd new attr->user_regs bitmap that lets a user choose a set of user registers to dump to the sample. The layout of this bitmap is described in asm/perf_regs.h for archs that support CONFIG_HAVE_PERF_REGS_DEFS, otherwise the perf syscall will fail if attr->user_regs is non zero.
The register value here are those of the user space context as it was before the user entered the kernel for whatever reason (syscall, irq, exception, or a PMI happening in userspace).
This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.
FIXME: the issue of compat regs has yet to be solved. I think we need to split the regs bitmap in:
attr->user_regs32 attr->user_regs64
So that userspace doesn't need to care about beeing a compat task or not, running on a 32 bits kernel or not, it can provide both bitmaps and let the kernel handle that, ignore user_regs64 if it is a 32 bits kernel, handle it otherwise and also user_regs32 for compat tasks, etc...
--- include/linux/perf_event.h | 26 ++++++++++++++++++ kernel/events/core.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 0 deletions(-)
include/linux/perf_event.h +26 -0
@@ -272,6 +272,12 @@ struct perf_event_attr {__u64 config2;/* extension of config1 */ };__u64 branch_sample_type;/* enum branch_sample_type */ + +/* +* Arch specific mask that defines a set of user regs to dump on +* samples. See asm/perf_regs.h for details. +*/ +__u64 user_regs; };/*
@@ -1079,6 +1085,25 @@ struct perf_output_handle {#ifdef CONFIG_PERF_EVENTS +#ifdef CONFIG_HAVE_PERF_REGS_DEFS +#include <asm/perf_regs.h> +#else +static inline int perf_reg_value (struct pt_regs *regs,int idx) +{ +return 0 ; +} + +static inline int perf_reg_version (void ) +{ +return -EINVAL ; +} + +static inline int perf_reg_validate (u64 mask) +{ +return -ENOSYS ; +} +#endif /*CONFIG_HAVE_PERF_REGS_DUMP */ +extern int perf_pmu_register (struct pmu *pmu,char *name,int type);extern void perf_pmu_unregister (struct pmu *pmu);
@@ -1130,6 +1155,7 @@ struct perf_sample_data {struct perf_callchain_entry *callchain;struct perf_raw_record *raw;struct perf_branch_stack *br_stack; +struct pt_regs *uregs; };static inline void perf_sample_data_init (struct perf_sample_data *data,u64 addr)
kernel/events/core.c +61 -0
85 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [PATCH 04/15] perf: Add ability to dump user regs
Hi, Jiri -
Quoted 5 lines · Jiri Olsa
[...]
The register value here are those of the user space context as it was before the user entered the kernel for whatever reason (syscall, irq, exception, or a PMI happening in userspace). [...]
As I understand the situation, there is a complication here that you haven't accounted for. Upon a normal syscall entry to the kernel, not all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. It turns out that some of these saved registers are sometimes necessary to accomplish a user-space unwind.
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound - and not via frame pointers, but the full dwarf unwind/cfi machinery. This RFC code does not appear aware of the difference between the explicitly saved and the incidentally-spilled registers, and thus may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
- FChE
Re: [PATCH 04/15] perf: Add ability to dump user regs
Quoted 9 lines · Frank Ch. Eigler
Hi, Jiri -
Quoted 5 lines · Jiri Olsa
[...]
The register value here are those of the user space context as it was before the user entered the kernel for whatever reason (syscall, irq, exception, or a PMI happening in userspace). [...]
Quoted 6 lines
As I understand the situation, there is a complication here that you haven't accounted for. Upon a normal syscall entry to the kernel, not all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. It turns out that some of these saved registers are sometimes necessary to accomplish a user-space unwind.
Are you reffering to x86_64 where only portion of registers is stored by SAVE_ARGS macro? Seems like 32 bits stores the whole pt_regs.
Generally you could need all the registers to start the unwind, but I was assuming that for most cases the stack pointer and instruction pointer should be enough.. but I might be wrong here.
Quoted 8 lines
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound - and not via frame pointers, but the full dwarf unwind/cfi machinery. This RFC code does not appear aware of the difference between the explicitly saved and the incidentally-spilled registers, and thus may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
AFAIK not going to happen any time soon ;)
thanks,
jirka
Re: [PATCH 04/15] perf: Add ability to dump user regs
Hi, Jiri -
Quoted 9 lines
[...]
Quoted 4 lines
[...] Upon a normal syscall entry to the kernel, not
all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. [...]Are you reffering to
x86_64where only portion of registers is stored bySAVE_ARGSmacro? Seems like 32 bits stores the wholept_regs.
I believe that's the right area. I'm not sure even the 32-bit variant is complete enough, for example exempting MMX/SSE registers. These may also contain spilled registers before long.
Quoted 3 lines
Generally you could need all the registers to start the unwind, but I was assuming that for most cases the stack pointer and instruction pointer should be enough.. but I might be wrong here.
Yeah; the question is how much is missed besides those "most cases".
Quoted 6 lines
Quoted 4 lines
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound [... Without that, it ...] may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
AFAIK not going to happen any time soon ;)
Understood. Then the code needs to ensure that it does not purport to pass register values that it does not know. (Back when we were at this stage in systemtap, we got some reasonable backtraces even without kernel unwinding, ie. tolerating missing registers.)
- FChE
Re: [PATCH 04/15] perf: Add ability to dump user regs
Quoted 8 lines · Frank Ch. Eigler
Hi, Jiri -
Quoted 6 lines
[...]
Quoted 4 lines
[...] Upon a normal syscall entry to the kernel, not
all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. [...]
Quoted 7 lines
Quoted 3 lines
Are you reffering to
x86_64where only portion of registers is stored bySAVE_ARGSmacro? Seems like 32 bits stores the wholept_regs.I believe that's the right area. I'm not sure even the 32-bit variant is complete enough, for example exempting MMX/SSE registers. These may also contain spilled registers before long.
right, I covered only general registers... need to check about mmc/sse
jirka
Quoted 23 lines
Quoted 5 lines
Generally you could need all the registers to start the unwind, but I was assuming that for most cases the stack pointer and instruction pointer should be enough.. but I might be wrong here.
Yeah; the question is how much is missed besides those "most cases".
Quoted 6 lines
Quoted 4 lines
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound [... Without that, it ...] may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
AFAIK not going to happen any time soon ;)
Understood. Then the code needs to ensure that it does not purport to pass register values that it does not know. (Back when we were at this stage in systemtap, we got some reasonable backtraces even without kernel unwinding, ie. tolerating missing registers.)
- FChE
Re: [PATCH 04/15] perf: Add ability to dump user regs
Quoted 13 lines · Jiri Olsa
Quoted 12 lines · Frank Ch. Eigler
Hi, Jiri -
Quoted 9 lines
[...]
Quoted 4 lines
[...] Upon a normal syscall entry to the kernel, not
all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. [...]Are you reffering to
x86_64where only portion of registers is stored bySAVE_ARGSmacro? Seems like 32 bits stores the wholept_regs.
Quoted 5 lines
Quoted 3 lines
I believe that's the right area. I'm not sure even the 32-bit variant is complete enough, for example exempting MMX/SSE registers. These may also contain spilled registers before long.
right, I covered only general registers... need to check about mmc/sse
I think you can forget that. At least for now.
I believe in most cases you'll only need rsp for the unwinding. Then on some tricky frame setups you might need more general registers values but I guess this shouldn't be too frequent.
But you won't need MMX/SSE.
Re: [PATCH 04/15] perf: Add ability to dump user regs
In reply to Frank Ch. EiglerQuoted 29 lines · Frank Ch. Eigler
Hi, Jiri -
Quoted 9 lines
[...]
Quoted 4 lines
[...] Upon a normal syscall entry to the kernel, not
all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. [...]Are you reffering to
x86_64where only portion of registers is stored bySAVE_ARGSmacro? Seems like 32 bits stores the wholept_regs.I believe that's the right area. I'm not sure even the 32-bit variant is complete enough, for example exempting MMX/SSE registers. These may also contain spilled registers before long.
Quoted 3 lines
Generally you could need all the registers to start the unwind, but I was assuming that for most cases the stack pointer and instruction pointer should be enough.. but I might be wrong here.
Yeah; the question is how much is missed besides those "most cases".
Quoted 4 lines
Quoted 4 lines
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound [... Without that, it ...] may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
Quoted 6 lines
Quoted 1 line
AFAIK not going to happen any time soon ;)
Understood. Then the code needs to ensure that it does not purport to pass register values that it does not know. (Back when we were at this stage in systemtap, we got some reasonable backtraces even without kernel unwinding, ie. tolerating missing registers.)
Right.
I think in normal syscall case we save rdi, rsi, rdx, rax and rip. If we take the syscall slow path we save rbx, rbp, r12-15.
Unfortunately we don't save rsp, which must be the most important for cfi unwinding.
We probably need to check what is saved in irqs (set_irq_regs()) and exceptions as well.
Re: [PATCH 04/15] perf: Add ability to dump user regs
Quoted 37 lines · Frederic Weisbecker
Quoted 29 lines · Frank Ch. Eigler
Hi, Jiri -
Quoted 9 lines
[...]
Quoted 4 lines
[...] Upon a normal syscall entry to the kernel, not
all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. [...]Are you reffering to
x86_64where only portion of registers is stored bySAVE_ARGSmacro? Seems like 32 bits stores the wholept_regs.I believe that's the right area. I'm not sure even the 32-bit variant is complete enough, for example exempting MMX/SSE registers. These may also contain spilled registers before long.
Quoted 3 lines
Generally you could need all the registers to start the unwind, but I was assuming that for most cases the stack pointer and instruction pointer should be enough.. but I might be wrong here.
Yeah; the question is how much is missed besides those "most cases".
Quoted 4 lines
Quoted 4 lines
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound [... Without that, it ...] may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
Quoted 6 lines
Quoted 1 line
AFAIK not going to happen any time soon ;)
Understood. Then the code needs to ensure that it does not purport to pass register values that it does not know. (Back when we were at this stage in systemtap, we got some reasonable backtraces even without kernel unwinding, ie. tolerating missing registers.)
Quoted 7 lines
Right.
I think in normal syscall case we save rdi, rsi, rdx, rax and rip. If we take the syscall slow path we save rbx, rbp, r12-15.
Unfortunately we don't save rsp, which must be the most important for cfi unwinding.
hm, I think we always have stack pointer
should be saved by cpu itself together with other control regs like: ip cs eflags sp ss
For syscalls, we also have the ones stored by SAVE_ARGS The rest of the registers (SAVE_REST) are available only for the sake of the syscall_trace_enter during the slow path, but it's poped out before executing the actuall syscall.
jirka
Quoted 3 lines
We probably need to check what is saved in irqs (
set_irq_regs()) and exceptions as well.
Re: [PATCH 04/15] perf: Add ability to dump user regs
Quoted 48 lines · Jiri Olsa
Quoted 37 lines · Frederic Weisbecker
Quoted 29 lines · Frank Ch. Eigler
Hi, Jiri -
Quoted 9 lines
[...]
Quoted 4 lines
[...] Upon a normal syscall entry to the kernel, not
all user registers are saved explicitly for such easy retrieval. The others may be spilled to the stack by gcc during the various sys_* functions or elsewhere. [...]Are you reffering to
x86_64where only portion of registers is stored bySAVE_ARGSmacro? Seems like 32 bits stores the wholept_regs.I believe that's the right area. I'm not sure even the 32-bit variant is complete enough, for example exempting MMX/SSE registers. These may also contain spilled registers before long.
Quoted 3 lines
Generally you could need all the registers to start the unwind, but I was assuming that for most cases the stack pointer and instruction pointer should be enough.. but I might be wrong here.
Yeah; the question is how much is missed besides those "most cases".
Quoted 4 lines
Quoted 4 lines
To recover these registers at run time, we found that the kernel stack itself has to be partially unwound [... Without that, it ...] may accidentally pass garbage data to perf userspace. Correcting this could require a kernel-space libunwind.
Quoted 6 lines
Quoted 1 line
AFAIK not going to happen any time soon ;)
Understood. Then the code needs to ensure that it does not purport to pass register values that it does not know. (Back when we were at this stage in systemtap, we got some reasonable backtraces even without kernel unwinding, ie. tolerating missing registers.)
Quoted 7 lines
Right.
I think in normal syscall case we save rdi, rsi, rdx, rax and rip. If we take the syscall slow path we save rbx, rbp, r12-15.
Unfortunately we don't save rsp, which must be the most important for cfi unwinding.
hm, I think we always have stack pointer
Quoted 8 lines
should be saved by cpu itself together with other control regs like: ip cs eflags sp ss
For syscalls, we also have the ones stored by
SAVE_ARGSThe rest of the registers (SAVE_REST) are available only for the sake of thesyscall_trace_enterduring the slow path, but it's poped out before executing the actuall syscall.
Ah good point!
Quoted 7 lines · Jiri Olsa
hi,
sending RFC version of the post unwinding user stack backtrace using dwarf unwind - via libunwind. The original work was done by Frederic. I mostly took his patches and make them compile in current kernel code plus I added some stuff here and there.
Quoted 5 lines
The main idea is to store user registers and portion of user stack when the sample data during the record phase. Then during the report, when the data is presented, perform the actual dwarf dwarf unwind.
Although I understand why you need this for user level dwarf unwinding, I think you also need to look at the more general problem of capturing the machine state registers at the interrupted IP as well. There are interesting measurements one can make with those, such as sampling of function arguments.
I think the mechanism should allow the user to select which registers (you have that) but also where they are captured. You have
the user level state, but you also want the interrupted state or the precise state, i.e., extracting the register at retirement of an instruction that caused the sampling PMU event (PEBS on Intel). Personally, I am interested in the last two. I had a prototype patch for those. It is based on the same approach in terms of register naming. You need to be able to name individual registers. That's obviously arch specific and you have that. Now there needs to be a way to indicate where the registers must to be captured. Note that you may want to combine user + interrupt states. So I think we may need multiple register bitmasks.
I am aware of the security issues with smapling machine state registers at the kernel level but they can be restricted, just like system-wide sessions.
Quoted 132 lines
attached patches:
01/15 perf, tool: Fix the array pointer to follow event data properly 02/15 uaccess: Add newcopy_from_user_gupAPI
03/15 perf: Unified API to record selective sets of arch registers 04/15 perf: Add ability to dump user regs
05/15 perf: Add ability to dump part of the user stack 06/15 perf: Add attribute to filter out user callchains 07/15 perf, tool: Factor DSO symtab types to generic binary types 08/15 perf, tool: Add interface to read DSO image data 09/15 perf, tool: Add '.note' check into search for NOTE section 10/15 perf, tool: Back [vdso] DSO with real data
11/15 perf, tool: Add interface to arch registers sets 12/15 perf, tool: Add libunwind dependency for dwarf cfi unwinding 13/15 perf, tool: Support user regs and stack in sample parsing 14/15 perf, tool: Support for dwarf cfi unwinding on post processing 15/15 perf, tool: Support for dwarf mode callchain on perf recordThe unwind processing could considerably prolong the computing time of the report command, but I believe this could be improved.
- caching DSO data accesses (as suggested in patch 8/15)
- maybe separate thread with unwind processing on background, so the user does no need to wait for all the data to be processed.I tested on Fedora. There was not much gain on i386, because the binaries are compiled with frame pointers. Thought the dwarf backtrace is more accurade and unwraps calls in more details (functions that do not set the frame pointers).
I could see some improvement on
x86_64, where I got full backtrace where current code could got just the first address out of the instruction pointer.Example on
x86_64:
[dwarf]
perf record -g -e syscalls:sys_enter_write date100.00% date libc-2.14.90.so [.]
__GI___libc_write|
---__GI___libc_write_IO_file_write@@GLIBC_2.2.5new_do_write_IO_do_write@@GLIBC_2.2.5 _IO_file_overflow@@GLIBC_2.2.50x4022cd
0x401ee6
__libc_start_main
0x4020b9[frame pointer]
perf record -g fp -e syscalls:sys_enter_write date100.00% date libc-2.14.90.so [.]
__GI___libc_write|
---__GI___libc_writeAlso I tested on coreutils binaries mainly, but I could see getting wider backtraces with dwarf unwind for more complex application like firefox.
The unwind should go throught [vdso] object. I haven't studied the [vsyscall] yet, so not sure there.
Attached patches should work on both x86 and
x86_64. I did some initial testing so far.The unwind backtrace can be interrupted by following reasons:
- bug in unwind information of processed shared library
- bug in unwind processing code (most likely ;) )
- insufficient dump stack size
- wrong register value -x86_64does not store whole set of registers when in exception, but so far it looks like RIP and RSP should be enoughI'd like to have some automated tests on this, but so far nothing smart is comming to me.. ;)
thanks for comments,
jirka
---
arch/Kconfig | 7 +arch/x86/Kconfig| 1 +arch/x86/include/asm/perf_regs.h| 15 +arch/x86/include/asm/perf_regs_32.h| 86 +++arch/x86/include/asm/perf_regs_64.h| 101 ++++arch/x86/include/asm/uaccess.h| 8 +-arch/x86/kernel/cpu/perf_event.c| 4 +-arch/x86/kernel/cpu/perf_event_intel_ds.c| 3 +-arch/x86/kernel/cpu/perf_event_intel_lbr.c| 2 +-arch/x86/lib/usercopy.c| 4 +-arch/x86/oprofile/backtrace.c| 4 +-include/asm-generic/uaccess.h| 4 +include/linux/perf_event.h| 36 ++-kernel/events/callchain.c| 4 +-kernel/events/core.c| 127 +++++-kernel/events/internal.h| 59 ++-kernel/events/ring_buffer.c| 4 +-tools/perf/Makefile| 40 ++-tools/perf/arch/x86/Makefile| 3 +tools/perf/arch/x86/include/perf_regs.h| 101 ++++tools/perf/arch/x86/util/unwind.c| 111 ++++tools/perf/builtin-record.c | 89 +++- tools/perf/builtin-report.c | 24 +- tools/perf/builtin-script.c | 56 ++-tools/perf/builtin-test.c | 3 +-
tools/perf/builtin-top.c| 7 +-tools/perf/config/feature-tests.mak| 25 +tools/perf/perf.h| 9 +-tools/perf/util/annotate.c| 2 +-tools/perf/util/event.h| 15 +-tools/perf/util/evlist.c| 16 +tools/perf/util/evlist.h| 2 +tools/perf/util/evsel.c| 36 ++-tools/perf/util/include/linux/compiler.h| 1 +tools/perf/util/map.c| 16 +-tools/perf/util/map.h| 7 +-tools/perf/util/perf_regs.h| 10 +tools/perf/util/python.c| 3 +-.../perf/util/scripting-engines/trace-event-perl.c| 3 +-.../util/scripting-engines/trace-event-python.c| 3 +-tools/perf/util/session.c | 100 +++- tools/perf/util/session.h | 10 +- tools/perf/util/symbol.c | 317 +++++++++--- tools/perf/util/symbol.h | 40 +-tools/perf/util/trace-event-scripting.c | 3 +-
tools/perf/util/trace-event.h| 5 +-tools/perf/util/unwind.c| 563 ++++++++++++++++++++tools/perf/util/unwind.h| 34 ++tools/perf/util/vdso.c| 92 ++++tools/perf/util/vdso.h| 7 +50 files changed, 2023 insertions(+), 199 deletions(-)
Quoted 5 lines · Stephane Eranian
I think the mechanism should allow the user to select which registers (you have that) but also where they are captured. You have the user level state, but you also want the interrupted state or the precise state, i.e., extracting the register at retirement of an instruction
Quoted 8 lines
that caused the sampling PMU event (PEBS on Intel). Personally, I am interested in the last two. I had a prototype patch for those. It is based on the same approach in terms of register naming. You need to be able to name individual registers. That's obviously arch specific and you have that. Now there needs to be a way to indicate where the registers must to be captured. Note that you may want to combine user + interrupt states. So I think we may need multiple register bitmasks.
This all comes very close to something I suggested to Masami once where you could specify data to grab on [ku]probe hits.
That is, when creating a [ku]probe based tracepoint you can specify several variable/argument names and the userspace perf-probe bits will use the dwarf info to figure out how to obtain the value of said var/arg at the probe point (if possible, if not bail).
The kernel would simply get something along the lines of %rax->foo->bar, or (%esp+x)->blah along with a size of the data structure to be found there and copy whatever it finds out to the raw field.
So what you're saying is you basically want this for any possible sample, not just [ku]probes.
Quoted 23 lines · Peter Zijlstra
Quoted 5 lines · Stephane Eranian
I think the mechanism should allow the user to select which registers (you have that) but also where they are captured. You have the user level state, but you also want the interrupted state or the precise state, i.e., extracting the register at retirement of an instruction
Quoted 8 lines
that caused the sampling PMU event (PEBS on Intel). Personally, I am interested in the last two. I had a prototype patch for those. It is based on the same approach in terms of register naming. You need to be able to name individual registers. That's obviously arch specific and you have that. Now there needs to be a way to indicate where the registers must to be captured. Note that you may want to combine user + interrupt states. So I think we may need multiple register bitmasks.
This all comes very close to something I suggested to Masami once where you could specify data to grab on [ku]probe hits.
That is, when creating a [ku]probe based tracepoint you can specify several variable/argument names and the userspace perf-probe bits will use the dwarf info to figure out how to obtain the value of said var/arg at the probe point (if possible, if not bail).
Quoted 8 lines
The kernel would simply get something along the lines of %rax->foo->bar, or (%esp+x)->blah along with a size of the data structure to be found there and copy whatever it finds out to the raw field.
So what you're saying is you basically want this for any possible sample, not just [ku]probes.
What I'd like to have is something similar to:
attr->sample_type |= PERF_SAMPLE_REGS
attr->sample_regs = EAX | EBX | EDI | ESI |.....
attr->sample_reg_mode = { INTR, PRECISE, USER }Then in each sample for the event you dump the u64 values of the requested registers. The order is that of the enum enum regs {}. That enum is arch specific.
When you are in precise mode on Intel, you extract the regs from PEBS. You already know the registers supported by PEBS so you can reject any request for unsupported regs.
When you are in intr they you get the regs from pt_regs. The user mode case is taken care of by the this patch series already.
I am not sure the sample_reg_mode needs to be a bitmask, i.e., do we need the reg state for INTR+PRECISE or USER+INTR? But if so, then we would need attr->sample_regs[3] as not all registers may be available in each mode.
Quoted 13 lines · Stephane Eranian
What I'd like to have is something similar to:
attr->sample_type |= PERF_SAMPLE_REGS attr->sample_regs = EAX | EBX | EDI | ESI |..... attr->sample_reg_mode = { INTR, PRECISE, USER }Then in each sample for the event you dump the u64 values of the requested registers. The order is that of the enum enum regs {}. That enum is arch specific.
When you are in precise mode on Intel, you extract the regs from PEBS. You already know the registers supported by PEBS so you can reject any request for unsupported regs.
Quoted 8 lines
When you are in intr they you get the regs from
pt_regs. The user mode case is taken care of by the this patch series already.I am not sure the
sample_reg_modeneeds to be a bitmask, i.e., do we need the reg state for INTR+PRECISE or USER+INTR? But if so, then we would need attr->sample_regs[3] as not all registers may be available in each mode.
I'm really having trouble seeing how useful this is. You mentioned sampling function arguments, but most samples would be in the middle of functions where the regs are completely unrelated to arguments. Also isn't the 'normal' C ABI passing args on stack rather than registers?
Quoted 23 lines · Peter Zijlstra
Quoted 13 lines · Stephane Eranian
What I'd like to have is something similar to:
attr->sample_type|=PERF_SAMPLE_REGSattr->sample_regs= EAX | EBX | EDI | ESI |.....attr->sample_reg_mode= { INTR, PRECISE, USER }Then in each sample for the event you dump the u64 values of the requested registers. The order is that of the enum enum regs {}. That enum is arch specific.
When you are in precise mode on Intel, you extract the regs from PEBS. You already know the registers supported by PEBS so you can reject any request for unsupported regs.
Quoted 8 lines
When you are in intr they you get the regs from
pt_regs. The user mode case is taken care of by the this patch series already.I am not sure the
sample_reg_modeneeds to be a bitmask, i.e., do we need the reg state for INTR+PRECISE or USER+INTR? But if so, then we would need attr->sample_regs[3] as not all registers may be available in each mode.
Quoted 5 lines
I'm really having trouble seeing how useful this is. You mentioned sampling function arguments, but most samples would be in the middle of functions where the regs are completely unrelated to arguments. Also isn't the 'normal' C ABI passing args on stack rather than registers?
If you look at the SNB events, you'll see that br_inst_retired:near_call supports PEBS. The sample is taken at retirement of the call, i.e., the first instruction of the function, exactly where you want it to be.
Unless I am mistaken, the x86_64 calling convention passes the first 6 integer arguments in registers.
Quoted 2 lines · Stephane Eranian
Unless I am mistaken, the
x86_64calling convention passes the first 6 integer arguments in registers.
This is documented here:
http://www.x86-64.org/documentation/abi.pdf
3.2.3 Parameter Passing
-Robert
Signature · 2 lines
Advanced Micro Devices, Inc. Operating System Research Center
Quoted 15 lines · Stephane Eranian
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Stephane Eranian
What I'd like to have is something similar to:
attr->sample_type |= PERF_SAMPLE_REGS attr->sample_regs = EAX | EBX | EDI | ESI |..... attr->sample_reg_mode = { INTR, PRECISE, USER }Then in each sample for the event you dump the u64 values of the requested registers. The order is that of the enum enum regs {}. That enum is arch specific.
When you are in precise mode on Intel, you extract the regs from PEBS. You already know the registers supported by PEBS so you can reject any request for unsupported regs.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
When you are in intr they you get the regs from
pt_regs. The user mode case is taken care of by the this patch series already.I am not sure the
sample_reg_modeneeds to be a bitmask, i.e., do we need the reg state for INTR+PRECISE or USER+INTR? But if so, then we would need attr->sample_regs[3] as not all registers may be available in each mode.
Would you have any good reason why we use INTR? Without PEBS, it may mislead users because the register value can be changed before interrupted. Personally, I would not like to use such values for debugging use :)
So, I think this regs options may be good for the option of PEBS events.
Quoted 11 lines
Quoted 5 lines
I'm really having trouble seeing how useful this is. You mentioned sampling function arguments, but most samples would be in the middle of functions where the regs are completely unrelated to arguments. Also isn't the 'normal' C ABI passing args on stack rather than registers?
If you look at the SNB events, you'll see that br_inst_retired:near_call supports PEBS. The sample is taken at retirement of the call, i.e., the first instruction of the function, exactly where you want it to be.
Unless I am mistaken, the
x86_64calling convention passes the first 6 integer arguments in registers.
Right, almost all function arguments are passed by register on x86-64. Hmm, this might be useful because it can trace function register-arguments in user-space applications... even though it causes interruption on every sampling calls...
Thanks,
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Re: [PATCH 03/15] perf: Unified API to record selective sets of arch registers
In reply to Jiri OlsaOn Wed, Mar 28, 2012 at 02:35:46PM +0200, Jiri Olsa wrote:
...
Quoted 21 lines
arch/x86/include/asm/perf_regs_64.h new file +21
-0+static inline u64 perf_reg_value (struct pt_regs *regs,int idx) +{ +switch (idx) { +case PERF_X86_64_REG_RAX : +return regs->ax; +case PERF_X86_64_REG_RBX : +return regs->bx; +case PERF_X86_64_REG_RCX : +return regs->cx; +case PERF_X86_64_REG_RDX : +return regs->dx; +case PERF_X86_64_REG_RSI : +return regs->si; +case PERF_X86_64_REG_RDI : +return regs->di; +case PERF_X86_64_REG_R8 : +return regs->r8; +case PERF_X86_64_REG_R9 : +return regs->r8; +case PERF_X86_64_REG_R10 : +return regs->r8;
arch/x86/include/asm/perf_regs_64.h line 81
Quoted 10 lines
arch/x86/include/asm/perf_regs_64.h new file +10
-0+case PERF_X86_64_REG_R11 : +return regs->r8; +case PERF_X86_64_REG_R12 : +return regs->r8; +case PERF_X86_64_REG_R13 : +return regs->r8; +case PERF_X86_64_REG_R14 : +return regs->r8; +case PERF_X86_64_REG_R15 : +return regs->r8;
I guess there are too much r8's :-)
Cyrill
Re: [PATCH 03/15] perf: Unified API to record selective sets of arch registers
Quoted 36 lines · Cyrill Gorcunov
On Wed, Mar 28, 2012 at 02:35:46PM +0200, Jiri Olsa wrote:
...Quoted 21 lines
arch/x86/include/asm/perf_regs_64.h new file +21
-0+static inline u64 perf_reg_value (struct pt_regs *regs,int idx) +{ +switch (idx) { +case PERF_X86_64_REG_RAX : +return regs->ax; +case PERF_X86_64_REG_RBX : +return regs->bx; +case PERF_X86_64_REG_RCX : +return regs->cx; +case PERF_X86_64_REG_RDX : +return regs->dx; +case PERF_X86_64_REG_RSI : +return regs->si; +case PERF_X86_64_REG_RDI : +return regs->di; +case PERF_X86_64_REG_R8 : +return regs->r8; +case PERF_X86_64_REG_R9 : +return regs->r8; +case PERF_X86_64_REG_R10 : +return regs->r8;arch/x86/include/asm/perf_regs_64.h line 81
Quoted 10 lines
arch/x86/include/asm/perf_regs_64.h new file +10
-0+case PERF_X86_64_REG_R11 : +return regs->r8; +case PERF_X86_64_REG_R12 : +return regs->r8; +case PERF_X86_64_REG_R13 : +return regs->r8; +case PERF_X86_64_REG_R14 : +return regs->r8; +case PERF_X86_64_REG_R15 : +return regs->r8;I guess there are too much r8's :-)
Quoted 1 line
Cyrill
yay :)) right, one should be enough..
thanks,
jirka
Quoted 4 lines · Masami Hiramatsu
Right, almost all function arguments are passed by register on x86-64. Hmm, this might be useful because it can trace function register-arguments in user-space applications... even though it causes interruption on every sampling calls...
You only have to do this for the first call frame and only if the breakpoint is at the first instruction of the function.
Otherwise the register content can of course already be destroyed.
Given this, is it really worth it? For instance, for any backtrace from the breakpoint in kernel there is nothing at all you gain from recording the register content.
Re: [PATCH 04/15] perf: Add ability to dump user regs
In reply to Jiri OlsaQuoted 131 lines · Jiri Olsa
Add new
attr->user_regsbitmap that lets a user choose a set of user registers to dump to the sample. The layout of this bitmap is described inasm/perf_regs.hfor archs that supportCONFIG_HAVE_PERF_REGS_DEFS, otherwise the perf syscall will fail ifattr->user_regsis non zero.The register value here are those of the user space context as it was before the user entered the kernel for whatever reason (syscall, irq, exception, or a PMI happening in userspace).
This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.
FIXME: the issue of compat regs has yet to be solved. I think we need to split the regs bitmap in:
attr->user_regs32 attr->user_regs64So that userspace doesn't need to care about beeing a compat task or not, running on a 32 bits kernel or not, it can provide both bitmaps and let the kernel handle that, ignore
user_regs64if it is a 32 bits kernel, handle it otherwise and alsouser_regs32for compat tasks, etc...Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/perf_event.h | 26 ++++++++++++++++++ kernel/events/core.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 0 deletions(-)include/linux/perf_event.h +26
-0@@ -272,6 +272,12 @@ struct perf_event_attr {__u64 config2;/* extension of config1 */ };__u64 branch_sample_type;/* enum branch_sample_type */ + +/* +* Arch specific mask that defines a set of user regs to dump on +* samples. See asm/perf_regs.h for details. +*/ +__u64 user_regs; };/* @@ -1079,6 +1085,25 @@ struct perf_output_handle {#ifdef CONFIG_PERF_EVENTS +#ifdef CONFIG_HAVE_PERF_REGS_DEFS +#include <asm/perf_regs.h> +#else +static inline int perf_reg_value (struct pt_regs *regs,int idx) +{ +return 0 ; +} + +static inline int perf_reg_version (void ) +{ +return -EINVAL ; +} + +static inline int perf_reg_validate (u64 mask) +{ +return -ENOSYS ; +} +#endif /*CONFIG_HAVE_PERF_REGS_DUMP */ +extern int perf_pmu_register (struct pmu *pmu,char *name,int type);extern void perf_pmu_unregister (struct pmu *pmu);@@ -1130,6 +1155,7 @@ struct perf_sample_data {struct perf_callchain_entry *callchain;struct perf_raw_record *raw;struct perf_branch_stack *br_stack; +struct pt_regs *uregs; };static inline void perf_sample_data_init (struct perf_sample_data *data,u64 addr)kernel/events/core.c +33
-0@@ -3751,6 +3751,37 @@ int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks); +static void +perf_output_sample_regs (struct perf_output_handle *handle, +struct pt_regs *regs,u64 mask) +{ +int i =0 ; + +do { +u64 val; + +if (mask &1 ) { + val =perf_reg_value (regs, i); +perf_output_put (handle, val); + } + + mask >>=1 ; + i++; + }while (mask); +} + +static struct pt_regs *perf_sample_uregs (struct pt_regs *regs) +{ +if (!user_mode (regs)) { +if (current->mm) + regs =task_pt_regs (current); +else + regs =NULL ; + } + +return regs; +} +static void __perf_event_header__init_id (struct perf_event_header *header,struct perf_sample_data *data,struct perf_event *event)@@ -4011,6 +4042,22 @@ void perf_output_sample(struct perf_output_handle *handle,perf_output_put (handle, nr); } } + +if (event->attr.user_regs) {
kernel/events/core.c void perf_output_sample(struct perf_output_handle *handle, · line 4056
Quoted 10 lines
kernel/events/core.c +10
-0+u64 id; + +/* If there is no regs to dump, notice it through a 0 version */ +if (!data->uregs) { + id =0 ; +perf_output_put (handle, id); + }else { + + id =perf_reg_version (); +perf_output_put (handle, id);
I have the feeling we don't need this arch version thing. Depending on the current task, whether it is a 32, compat or 64, let the arch return the right reg value requested.
Then let the perf tools look at the elf headers of the dsos to find out the architecture (32 - 64) of a task. On top of that we can know what we are dealing with.
Quoted 25 lines · Masami Hiramatsu
Quoted 15 lines · Stephane Eranian
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Stephane Eranian
What I'd like to have is something similar to:
attr->sample_type|=PERF_SAMPLE_REGSattr->sample_regs= EAX | EBX | EDI | ESI |.....attr->sample_reg_mode= { INTR, PRECISE, USER }Then in each sample for the event you dump the u64 values of the requested registers. The order is that of the enum enum regs {}. That enum is arch specific.
When you are in precise mode on Intel, you extract the regs from PEBS. You already know the registers supported by PEBS so you can reject any request for unsupported regs.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
When you are in intr they you get the regs from
pt_regs. The user mode case is taken care of by the this patch series already.I am not sure the
sample_reg_modeneeds to be a bitmask, i.e., do we need the reg state for INTR+PRECISE or USER+INTR? But if so, then we would need attr->sample_regs[3] as not all registers may be available in each mode.
Quoted 4 lines
Would you have any good reason why we use INTR? Without PEBS, it may mislead users because the register value can be changed before interrupted. Personally, I would not like to use such values for debugging use :)
By INTR I meant PMU interrupt. That gives you the machine state at the interrupted IP. That is interesting in the case of stalls (sampling on cycles for instance). You may be able to backtrack to the cause of the stalls. Take the following simple example:
0x1000: ld r1=[r32] <--- long latency miss
....
0x100a: add r2=r1,1 <--- use pointSuppose you get a long latency miss of the load @ 0x1000, you stall at the use point (not the load), here the add instruction @ 0x100a. If you sample on cycles, you'll get an IP=0x100a. No skid on stalls because you have nothing else to execute.
Let's assume r32 is not modified between 0x1000 and 0x100a, then you can figure out the miss address just by looking at the machine state at 0x100a. You need some static analysis of the code but this can be done.
In fact, I know that for sure because some tools did that years ago:
http://www.supercomp.org/sc2003/paperpdfs/pap182.pdf
Section 2.2.3 for instance.
Quoted 3 lines
So, I think this regs options may be good for the option of PEBS events.
Yes, that one is more obvious and requires no static analysis of the code in most cases.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
I'm really having trouble seeing how useful this is. You mentioned sampling function arguments, but most samples would be in the middle of functions where the regs are completely unrelated to arguments. Also isn't the 'normal' C ABI passing args on stack rather than registers?
Quoted 8 lines
Quoted 6 lines
If you look at the SNB events, you'll see that br_inst_retired:near_call supports PEBS. The sample is taken at retirement of the call, i.e., the first instruction of the function, exactly where you want it to be.
Unless I am mistaken, the
x86_64calling convention passes the first 6 integer arguments in registers.Right, almost all function arguments are passed by register on x86-64.
Including some of the FP registers but that's not captured by PEBS, unfortunately.
Quoted 4 lines
Hmm, this might be useful because it can trace function register-arguments in user-space applications... even though it causes interruption on every sampling calls...
It's not on every call to a function. This is statistical sampling not tracing.
Also I am not necessarily interested in using the machine state to unwind the call stack. In the example above it's to support a local static code analysis