perf: Dwarf cfi based user callchains
Subsystems (3)
Various things have changed in this set.
- copy_from_user_nmi() renamed into copy_from_user_gup()
- regs dumping uses a version now and the user can select its own subset flavour of the regs proposed by the arch. But look: there is an open question in the 3rd patch to handle the compat stuff.
- new exclude_user_callchain attribute, instead of dropping fp based user callchains if we dump regs and stack.
- etc...
Important things to be done:
- Still no dwarf cfi from vdso. But I've read somewhere that vdso has dwarf cfi info. I need to handle that somewhow. Until then, it is only usable if you profile only userspace (use the :u flag in your event, as in the example below).
- Split generic/arch code in tools/perf/util/unwind.c
- Use .eh_frame_hdr for binary searching
- Make it much faster, it's too slow right now. Perhaps that needs some cfi info caching.
- handle compat regs, look at the open question in 3rd patch.
How to test:
perf record -g dwarf -e cycles:u ./hackbench 10 perf report
You can increase the stack dump by playing with the size, it is about 4000 by default, if you want 10000 bytes:
perf record -g dwarf,10000 -e cycles:u ./hackbench 10
You can pull from:
git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
perf/unwind-v2
Thanks.
---
Frederic Weisbecker (11):
uaccess: New copy_from_user_gup() API
perf: Unified API to record selective sets of arch registers
perf: Add ability to dump user regs
perf: Add ability to dump part of the user stack
perf: New attribute to filter out user callchains
perf: Support for dwarf mode callchain on perf record
perf: Build with dwarf cfi
perf: Support for error passed over pointers
perf: Add libunwind dependency for dwarf cfi unwinding
perf: Support user regs and stack in sample parsing
perf: Support for dwarf cfi unwinding on post processing
arch/Kconfig | 7 + arch/x86/Kconfig | 1 + arch/x86/include/asm/perf_regs.h | 10 + arch/x86/include/asm/perf_regs_32.h | 85 +++ arch/x86/include/asm/uaccess.h | 5 + arch/x86/kernel/cpu/perf_event.c | 8 +- arch/x86/kernel/cpu/perf_event_intel_ds.c | 2 +- include/asm-generic/uaccess.h | 4 + include/linux/perf_event.h | 40 +- kernel/perf_event.c | 194 +++++- tools/perf/Makefile | 30 +- tools/perf/arch/x86/include/perf_regs.h | 11 + tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 2 +- tools/perf/builtin-record.c | 78 ++- tools/perf/builtin-report.c | 9 +- tools/perf/builtin-sched.c | 2 +- tools/perf/builtin-timechart.c | 2 +- tools/perf/builtin-trace.c | 2 +- tools/perf/feature-tests.mak | 14 + tools/perf/util/callchain.c | 35 +- tools/perf/util/callchain.h | 19 +- tools/perf/util/event.c | 28 +- tools/perf/util/event.h | 15 +- tools/perf/util/header.c | 33 +- tools/perf/util/header.h | 3 +- tools/perf/util/include/linux/err.h | 24 + tools/perf/util/perf_regs.h | 11 + tools/perf/util/session.c | 4 +- tools/perf/util/session.h | 6 + tools/perf/util/unwind.c | 1112 +++++++++++++++++++++++++++++ 31 files changed, 1739 insertions(+), 59 deletions(-)
RFC PATCH 1/11 uaccess: New copy_from_user_gup() API
This 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.
Since perf is the first user for now, let the overriden x86 implementation in the perf source file.
--- arch/x86/include/asm/uaccess.h | 5 +++++ arch/x86/kernel/cpu/perf_event.c | 8 ++++---- arch/x86/kernel/cpu/perf_event_intel_ds.c | 2 +- include/asm-generic/uaccess.h | 4 ++++ 4 files changed, 14 insertions(+), 5 deletions(-)
arch/x86/include/asm/uaccess.h +5 -0
@@ -229,6 +229,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 /**
arch/x86/kernel/cpu/perf_event.c +4 -4
@@ -45,8 +45,8 @@ do { \/* * best effort, GUP based copy_from_user() that assumes IRQ or NMI context */ -static unsigned long -copy_from_user_nmi (void *to,const void __user *from,unsigned long n) +unsigned long +__copy_from_user_gup (void *to,const void __user *from,unsigned long n) {unsigned long offset, addr = (unsigned long )from;int type =in_nmi () ?KM_NMI :KM_IRQ0 ;
@@ -1688,7 +1688,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 ;
@@ -1731,7 +1731,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 +1 -1
@@ -426,7 +426,7 @@ 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 ;
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.6.2.3
RFC PATCH 2/11 perf: Unified API to record selective sets of arch registers
In reply to Frederic WeisbeckerThis brings a new API to help the selective dump of registers on event sampling, and its implementation in x86-32.
- 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.
--- arch/Kconfig | 7 +++ arch/x86/Kconfig | 1 + arch/x86/include/asm/perf_regs.h | 10 ++++ arch/x86/include/asm/perf_regs_32.h | 85 +++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/perf_regs.h create mode 100644 arch/x86/include/asm/perf_regs_32.h
arch/Kconfig +7 -0
@@ -158,6 +158,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
@@ -58,6 +58,7 @@ config X86 select HAVE_MIXED_BREAKPOINTS_REGS select PERF_EVENTS select HAVE_PERF_EVENTS_NMI + select HAVE_PERF_REGS_DEFS if X86_32 select ANON_INODES select HAVE_ARCH_KMEMCHECK select HAVE_USER_RETURN_NOTIFIER
arch/x86/include/asm/perf_regs.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef _ASM_X86_PERF_REGS_H +#define _ASM_X86_PERF_REGS_H + +#ifdef CONFIG_X86_32 +#include "perf_regs_32.h" +#endif + +/* TODO: x86-64 and compat */ + +#endif /* _ASM_X86_PERF_REGS_H */
arch/x86/include/asm/perf_regs_32.h new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
1.6.2.3
RFC PATCH 3/11 perf: Add ability to dump user regs
In reply to Frederic WeisbeckerAdd 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...
Hmm?
--- include/linux/perf_event.h | 28 +++++++++++++++++++ kernel/perf_event.c | 63 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletions(-)
include/linux/perf_event.h +28 -0
@@ -226,6 +226,16 @@ struct perf_event_attr {__u32 bp_type;__u64 bp_addr;__u64 bp_len; + +/* Future breakpoint fields extension */ +__u64 __reserved_2; +__u64 __reserved_3; + +/* +* Arch specific mask that defines a set of user regs to dump on +* samples. See asm/perf_regs.h for details. +*/ +__u64 user_regs; };/*
@@ -475,6 +485,7 @@ struct perf_guest_info_callbacks {#include <asm/hw_breakpoint.h> #endif +#include <linux/list.h> #include <linux/mutex.h> #include <linux/rculist.h>
@@ -891,6 +902,22 @@ 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 (struct pt_regs *regs,int idx) +{ +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);extern void perf_pmu_unregister (struct pmu *pmu);
@@ -950,6 +977,7 @@ struct perf_sample_data {u64 period;struct perf_callchain_entry *callchain;struct perf_raw_record *raw; +struct pt_regs *uregs; }; static inline
kernel/perf_event.c +62 -1
99 lines not on this page · raw message
Signature · 1 line
1.6.2.3
RFC PATCH 4/11 perf: Add ability to dump part of the user stack
In reply to Frederic WeisbeckerBeeing 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 | 9 +++- kernel/perf_event.c | 123 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 108 insertions(+), 24 deletions(-)
include/linux/perf_event.h +7 -2
@@ -236,6 +236,10 @@ struct perf_event_attr {* samples. See asm/perf_regs.h for details. */ __u64 user_regs; +__u32 ustack_dump_size; + +/* Future extension */ +__u32 __reserved_4; };/*
@@ -1107,8 +1111,9 @@ extern int perf_output_begin(struct perf_output_handle *handle, struct perf_event *event, unsigned int size, int nmi, int sample);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/perf_event.c +101 -22
141 lines not on this page · raw message
Signature · 1 line
1.6.2.3
RFC PATCH 5/11 perf: New attribute to filter out user callchains
In reply to Frederic WeisbeckerAdd 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 | 3 ++- kernel/perf_event.c | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-)
include/linux/perf_event.h +2 -1
@@ -215,8 +215,9 @@ struct perf_event_attr {*/ precise_ip : 2, /* skid constraint */ mmap_data : 1, /* non-exec mmap data */ + exclude_user_callchain : 1, /* only record kernel callchains */ - __reserved_1 : 46; + __reserved_1 : 45;union {__u32 wakeup_events;/* wakeup every n events */
kernel/perf_event.c +5 -3
@@ -2016,7 +2016,8 @@ put_callchain_entry(int rctx)put_recursion_context (__get_cpu_var (callchain_recursion), rctx); } -static struct perf_callchain_entry *perf_callchain (struct pt_regs *regs) +static struct perf_callchain_entry * +perf_callchain (struct pt_regs *regs,int user) {int rctx;struct perf_callchain_entry *entry;
@@ -2040,7 +2041,7 @@ static 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); }
@@ -3738,8 +3739,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;
Signature · 1 line
1.6.2.3
RFC PATCH 6/11 perf: Support for dwarf mode callchain on perf record
In reply to Frederic Weisbecker"perf record -g" is the command to record frame pointer based callchains. This patch extends the "-g" option to support the dwarf cfi mode.
The new behaviour is:
- "perf record -g" will record frame pointer based callchains as it did before.
- "perf record -g fp" is the same as "-g" alone
- "perf record -g dwarf" will record frame pointer based kernel callchains but will ignore the user part. Instead it will dump user regs and 4000 bytes of stack by default to each samples.
- "perf record -g dwarf,x" does the same but overrides the default 4000 bytes of user stack dumped to x bytes instead. The higher is the size, the deeper will be the callchain but the higher will be the overhead of the profiling and the size of the output file.
--- tools/perf/Makefile | 7 +++ tools/perf/arch/x86/include/perf_regs.h | 11 ++++ tools/perf/builtin-record.c | 78 +++++++++++++++++++++++++++++-- tools/perf/util/perf_regs.h | 11 ++++ 4 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 tools/perf/arch/x86/include/perf_regs.h create mode 100644 tools/perf/util/perf_regs.h
tools/perf/Makefile +7 -0
@@ -179,10 +179,12 @@ 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 # Additional ARCH settings for x86 ifeq ($(ARCH),i386) ARCH := x86 + NO_PERF_REGS_DEFS := 0 endif ifeq ($(ARCH),x86_64) ARCH := x86
@@ -417,6 +419,7 @@ LIB_H += util/probe-finder.h LIB_H += util/probe-event.h LIB_H += util/pstack.h LIB_H += util/cpumap.h +LIB_H += util/perf_regs.h LIB_OBJS += $(OUTPUT)util/abspath.o LIB_OBJS += $(OUTPUT)util/alias.o
@@ -662,6 +665,10 @@ 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 +11 -0
@@ -0,0 +1,11 @@ +#include "../../../../../arch/x86/include/asm/perf_regs_32.h" + + +#define BIT (x) (1 << (x)) + +#define PERF_UNWIND_REGS_MASK \ + (BIT(PERF_X86_32_REG_EAX) | BIT(PERF_X86_32_REG_EBX) | \ + BIT(PERF_X86_32_REG_ECX) | BIT(PERF_X86_32_REG_EDX) | \ + BIT(PERF_X86_32_REG_ESI) | BIT(PERF_X86_32_REG_EDI) | \ + BIT(PERF_X86_32_REG_EBP) | BIT(PERF_X86_32_REG_ESP) | \ + BIT(PERF_X86_32_REG_EIP))
tools/perf/builtin-record.c +74 -4
122 lines not on this page · raw message
tools/perf/util/perf_regs.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef __PERF_REGS_H +#define __PERF_REGS_H + +#ifndef NO_PERF_REGS_DEFS +#include <perf_regs.h> +#else + +#define PERF_UNWIND_REGS_MASK 0 +#endif /* NO_PERF_REGS_DEFS */ + +#endif
Signature · 1 line
1.6.2.3
RFC PATCH 7/11 perf: Build with dwarf cfi
In reply to Frederic WeisbeckerBuild perf with the dwarf call frame informations so that we can unwind callchains in perf itself.
--- tools/perf/Makefile | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
tools/perf/Makefile +1 -1
@@ -226,7 +226,7 @@ ifndef PERF_DEBUG CFLAGS_OPTIMIZE = -O6 endif -CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) +CFLAGS = -ggdb3 -funwind-tables -Wall -Wextra -std=gnu99 -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 ALL_LDFLAGS = $(LDFLAGS)
Signature · 1 line
1.6.2.3
RFC PATCH 8/11 perf: Support for error passed over pointers
In reply to Frederic WeisbeckerExport the linux/err.h that carries the ERR_PTR/PTR_ERR/IS_ERR macros so that they also become usable by perf.
--- tools/perf/Makefile | 1 + tools/perf/util/include/linux/err.h | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 0 deletions(-) create mode 100644 tools/perf/util/include/linux/err.h
tools/perf/Makefile +1 -0
@@ -377,6 +377,7 @@ LIB_H += util/include/linux/prefetch.h LIB_H += util/include/linux/rbtree.h LIB_H += util/include/linux/string.h LIB_H += util/include/linux/types.h +LIB_H += util/include/linux/err.h LIB_H += util/include/asm/asm-offsets.h LIB_H += util/include/asm/bug.h LIB_H += util/include/asm/byteorder.h
tools/perf/util/include/linux/err.h new file +24 -0
@@ -0,0 +1,24 @@ +#ifndef PERF_ERR_H +#define PERF_ERR_H + + +#define MAX_ERRNO 4095 + +#define IS_ERR_VALUE (x) ((x) >= (unsigned long)-MAX_ERRNO) + +static inline void *ERR_PTR (long err) +{ +return (void *) err; +} + +static inline long PTR_ERR (const void *ptr) +{ +return (long ) ptr; +} + +static inline long IS_ERR (const void *ptr) +{ +return IS_ERR_VALUE ((unsigned long )ptr); +} + +#endif /* PERF_ERR_H */
Signature · 1 line
1.6.2.3
RFC PATCH 9/11 perf: Add libunwind dependency for dwarf cfi unwinding
In reply to Frederic WeisbeckerThis is not mandatory, but required to get dwarf cfi unwinding support.
--- tools/perf/Makefile | 19 +++++++++++++++++++ tools/perf/feature-tests.mak | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 0 deletions(-)
tools/perf/Makefile +19 -0
@@ -518,6 +518,20 @@ ifneq ($(call try-cc,$(SOURCE_DWARF),$(FLAGS_DWARF)),y) endif # Dwarf support endif # NO_DWARF + +# Only x86-32 is supported for now +ifneq ($(ARCH),x86) + NO_LIBUNWIND := 1 +endif + +ifndef NO_LIBUNWIND +FLAGS_UNWIND=$(ALL_CFLAGS) -lunwind-x86 -lunwind-ptrace $(ALL_LDFLAGS) $(EXTLIBS) +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 ifeq ($(uname_S),Darwin)
@@ -564,6 +578,11 @@ else endif # PERF_HAVE_DWARF_REGS endif # NO_DWARF +ifndef NO_LIBUNWIND + BASIC_CFLAGS += -DLIBUNWIND_SUPPORT + EXTLIBS += -lunwind-ptrace -lunwind-x86 +endif + ifdef NO_NEWT BASIC_CFLAGS += -DNO_NEWT_SUPPORT else
tools/perf/feature-tests.mak +14 -0
@@ -90,6 +90,20 @@ int main(void) endef endif +ifndef NO_LIBUNWIND +define SOURCE_LIBUNWIND +#include <libunwind.h> +#include <stdlib.h> + +int main(void) +{ + unw_addr_space_t addr_space; + addr_space = unw_create_addr_space(NULL, 0); + return 0; +} +endef +endif + define SOURCE_BFD #include <bfd.h>
Signature · 1 line
1.6.2.3
RFC PATCH 10/11 perf: Support user regs and stack in sample parsing
In reply to Frederic WeisbeckerThis will be needed for stack unwinding.
--- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/builtin-timechart.c | 2 +- tools/perf/builtin-trace.c | 2 +- tools/perf/util/event.c | 28 ++++++++++++++++++++++++++-- tools/perf/util/event.h | 15 ++++++++++++++- tools/perf/util/header.c | 33 +++++++++++++++++++++++++++++++-- tools/perf/util/header.h | 3 ++- tools/perf/util/session.c | 4 ++-- tools/perf/util/session.h | 6 ++++++ 11 files changed, 86 insertions(+), 13 deletions(-)
tools/perf/builtin-kmem.c +1 -1
@@ -314,7 +314,7 @@ static int process_sample_event(event_t *event, struct perf_session *session) data.cpu =-1 ; data.period =1 ; -event__parse_sample (event, session->sample_type, &data); +event__parse_sample (event, session, &data);dump_printf ("(IP, %d): %d/%d: %#Lx period: %Ld\n" , event->header.misc, data.pid, data.tid, data.ip, data.period);
tools/perf/builtin-lock.c +1 -1
@@ -840,7 +840,7 @@ static int process_sample_event(event_t *self, struct perf_session *s)struct thread *thread;bzero (&data,sizeof (data)); -event__parse_sample (self, s->sample_type, &data); +event__parse_sample (self, s, &data); thread =perf_session__findnew (s, data.tid);if (thread ==NULL ) {
tools/perf/builtin-sched.c +1 -1
@@ -1619,7 +1619,7 @@ static int process_sample_event(event_t *event, struct perf_session *session) data.cpu =-1 ; data.period =-1 ; -event__parse_sample (event, session->sample_type, &data); +event__parse_sample (event, session, &data);dump_printf ("(IP, %d): %d/%d: %#Lx period: %Ld\n" , event->header.misc, data.pid, data.tid, data.ip, data.period);
tools/perf/builtin-timechart.c +1 -1
@@ -477,7 +477,7 @@ static int process_sample_event(event_t *event, struct perf_session *session)memset (&data,0 ,sizeof (data)); -event__parse_sample (event, session->sample_type, &data); +event__parse_sample (event, session, &data);if (session->sample_type &PERF_SAMPLE_TIME ) {if (!first_time || first_time > data.time)
tools/perf/builtin-trace.c +1 -1
@@ -74,7 +74,7 @@ static int process_sample_event(event_t *event, struct perf_session *session) data.cpu =-1 ; data.period =1 ; -event__parse_sample (event, session->sample_type, &data); +event__parse_sample (event, session, &data);dump_printf ("(IP, %d): %d/%d: %#Lx period: %Ld\n" , event->header.misc, data.pid, data.tid, data.ip, data.period);
tools/perf/util/event.c +26 -2
@@ -676,7 +676,7 @@ int event__preprocess_sample(const event_t *self, struct perf_session *session,u8 cpumode = self->header.misc &PERF_RECORD_MISC_CPUMODE_MASK ;struct thread *thread; -event__parse_sample (self, session->sample_type, data); +event__parse_sample (self, session, data); dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld cpu:%d\n", self->header.misc, data->pid, data->tid, data->ip,
@@ -766,9 +766,11 @@ out_filtered:return 0 ; } -int event__parse_sample (const event_t *event,u64 type,struct sample_data *data) +int event__parse_sample (const event_t *event,struct perf_session *session, +struct sample_data *data) {const u64 *array = event->sample.array; +u64 type = session->sample_type;if (type &PERF_SAMPLE_IP ) { data->ip = event->ip.ip;
@@ -830,6 +832,28 @@ int event__parse_sample(const event_t *event, u64 type, struct sample_data *data data->raw_size = *p; p++; data->raw_data = p; + array +=1 + (data->raw_size *sizeof (u64)); + } + +if (session->sample_uregs_nr) { + data->uregs.version = *array++; + +if (data->uregs.version) { + data->uregs.regs = (u64 *)array; + array += session->sample_uregs_nr; + } + } + +if (session->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/event.h +14 -1
@@ -61,6 +61,16 @@ struct sample_event {u64 array[]; }; +struct user_regs { +u64 version; +u64 *regs; +}; + +struct user_stack_dump { +u64 size; +char *data; +}; +struct sample_data {u64 ip;u32 pid, tid;
@@ -73,6 +83,8 @@ struct sample_data {u32 raw_size;void *raw_data;struct ip_callchain *callchain; +struct user_regs uregs; +struct user_stack_dump stack; };#define BUILD_ID_SIZE 20
@@ -160,7 +172,8 @@ struct addr_location;int event__preprocess_sample (const event_t *self,struct perf_session *session,struct addr_location *al,struct sample_data *data,symbol_filter_t filter); -int event__parse_sample (const event_t *event,u64 type,struct sample_data *data); +int event__parse_sample (const event_t *event,struct perf_session *session, +struct sample_data *data);extern const char *event__name[];
tools/perf/util/header.c +31 -2
@@ -922,9 +922,26 @@ out_errno:return -errno; } -u64 perf_header__sample_type (struct perf_header *header) +static int regs_weight (u64 user_regs) +{ +int i, bits =0 ; + +for (i =0 ; i < (int )sizeof (u64) *8 && user_regs; i++) { +if (user_regs &1 ) + bits++; + + user_regs >>=1 ; + } + +return bits; +} + +void perf_header__sample_type (struct perf_header *header, +struct perf_session *session) {u64 type =0 ; +u64 uregs =0 ; +int uregs_nr =0 , ustack = false;int i;for (i =0 ; i < header->attrs; i++) {
@@ -934,9 +951,21 @@ u64 perf_header__sample_type(struct perf_header *header) type = attr->attr.sample_type; else if (type != attr->attr.sample_type) die("non matching sample_type"); + +if (i ==0 ) { + uregs = attr->attr.user_regs; + uregs_nr =regs_weight (uregs); + ustack = !!attr->attr.ustack_dump_size; + }else if (uregs != attr->attr.user_regs || + ustack != !!attr->attr.ustack_dump_size) { +die ("non matching sample_type" ); + } } -return type; + session->sample_type = type; + session->sample_uregs = uregs; + session->sample_uregs_nr = uregs_nr; + session->sample_ustack = ustack; } struct perf_event_attr *
tools/perf/util/header.h +2 -1
@@ -80,7 +80,8 @@ void perf_header_attr__delete(struct perf_header_attr *self);int perf_header_attr__add_id (struct perf_header_attr *self,u64 id); -u64 perf_header__sample_type (struct perf_header *header); +void perf_header__sample_type (struct perf_header *header, +struct perf_session *session);struct perf_event_attr *perf_header__find_attr (u64 id,struct perf_header *header);void perf_header__set_feat (struct perf_header *self,int feat);
tools/perf/util/session.c +2 -2
@@ -67,7 +67,7 @@ out_close:void perf_session__update_sample_type (struct perf_session *self) { - self->sample_type =perf_header__sample_type (&self->header); +perf_header__sample_type (&self->header, self); }int perf_session__create_kernel_maps (struct perf_session *self)
@@ -576,7 +576,7 @@ static int perf_session__process_sample(event_t *event, struct perf_session *s,return ops->sample (event, s);bzero (&data,sizeof (struct sample_data )); -event__parse_sample (event, s->sample_type, &data); +event__parse_sample (event, s, &data);queue_sample_event (event, &data, s);
tools/perf/util/session.h +6 -0
@@ -39,6 +39,12 @@ struct perf_session {*/ struct hists hists;u64 sample_type; +u64 sample_uregs; +/* We should probably use a quick hweight64() in sample_uregs to get +* this instead of storing it. +*/ +int sample_uregs_nr; +bool sample_ustack;int fd;bool fd_pipe;bool repipe;
Signature · 1 line
1.6.2.3
RFC PATCH 11/11 perf: Support for dwarf cfi unwinding on post processing
In reply to Frederic WeisbeckerThis 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. We use the stack and regs dumped with samples to retrieve the context state necessary for this unwinding.
No specific options are needed for perf report to handle this, just run it as usual.
Only x86-32 is supported for now.
--- tools/perf/Makefile | 1 + tools/perf/builtin-report.c | 9 +- tools/perf/util/callchain.c | 35 ++- tools/perf/util/callchain.h | 19 +- tools/perf/util/unwind.c | 1112 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1169 insertions(+), 7 deletions(-) create mode 100644 tools/perf/util/unwind.c
tools/perf/Makefile +1 -0
@@ -468,6 +468,7 @@ LIB_OBJS += $(OUTPUT)util/hist.o LIB_OBJS += $(OUTPUT)util/probe-event.o LIB_OBJS += $(OUTPUT)util/util.o LIB_OBJS += $(OUTPUT)util/cpumap.o +LIB_OBJS += $(OUTPUT)util/unwind.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
tools/perf/builtin-report.c +8 -1
@@ -30,6 +30,8 @@#include "util/sort.h" #include "util/hist.h" +#include <linux/err.h> +static char const *input_name ="perf.data" ;static bool force, use_tui, use_stdio;
@@ -87,6 +89,7 @@ static int perf_session__add_hist_entry(struct perf_session *self,struct hist_entry *he;struct hists *hists;struct perf_event_attr *attr; +struct dwarf_callchain *dc;if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) { syms = perf_session__resolve_callchain(self, al->thread,
@@ -95,6 +98,10 @@ static int perf_session__add_hist_entry(struct perf_session *self,return -ENOMEM ; } + dc =callchain_unwind (self, al->thread, data); +if (IS_ERR (dc)) +return PTR_ERR (dc); + attr =perf_header__find_attr (data->id, &self->header);if (attr) hists =perf_session__hists_findnew (self, data->id, attr->type, attr->config);
@@ -108,7 +115,7 @@ static int perf_session__add_hist_entry(struct perf_session *self, err =0 ;if (symbol_conf.use_callchain) { err =callchain_append (he->callchain, data->callchain, syms, - data->period); + dc, data->period);if (err) goto out_free_syms; }
tools/perf/util/callchain.c +30 -5
@@ -365,8 +365,10 @@ append_chain(struct callchain_node *root, struct resolved_chain *chain, }static void filter_context (struct ip_callchain *old,struct resolved_chain *new, -struct map_symbol *syms) +struct map_symbol *syms, +struct dwarf_callchain *dwarf_chain) { +struct dwarf_callchain_entry *entry, *tmp;int i, j =0 ;for (i =0 ; i < (int )old->nr; i++) {
@@ -379,23 +381,46 @@ static void filter_context(struct ip_callchain *old, struct resolved_chain *new, } new->nr = j; + +if (!dwarf_chain) +return ; + +list_for_each_entry_safe (entry, tmp, &dwarf_chain->chain_head, list) { + new->ips[j].ip = entry->ip; + new->ips[j].ms = entry->ms; + j++; + +list_del (&entry->list); +free (entry); + } + + new->nr += dwarf_chain->nb; + +free (dwarf_chain); }int callchain_append (struct callchain_root *root,struct ip_callchain *chain, -struct map_symbol *syms,u64 period) +struct map_symbol *syms, +struct dwarf_callchain *dwarf_chain,u64 period) {struct resolved_chain *filtered; +int entries; + + entries = chain->nr; + +if (dwarf_chain) + entries += dwarf_chain->nb; -if (!chain->nr) +if (!entries)return 0 ; filtered =zalloc (sizeof (*filtered) + - chain->nr *sizeof (struct resolved_ip )); + entries *sizeof (struct resolved_ip ));if (!filtered)return -ENOMEM ; -filter_context (chain, filtered, syms); +filter_context (chain, filtered, syms, dwarf_chain);if (!filtered->nr) goto end;
tools/perf/util/callchain.h +18 -1
@@ -31,6 +31,17 @@ struct callchain_root {struct callchain_node node; }; +struct dwarf_callchain_entry { +struct list_head list; +u64 ip; +struct map_symbol ms; +}; + +struct dwarf_callchain { +int nb; +struct list_head chain_head; +}; +struct callchain_param ; typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
@@ -68,8 +79,14 @@ static inline u64 cumul_hits(struct callchain_node *node)int register_callchain_param (struct callchain_param *param);int callchain_append (struct callchain_root *root,struct ip_callchain *chain, -struct map_symbol *syms,u64 period); +struct map_symbol *syms, +struct dwarf_callchain *dwarf_chain,u64 period); +int callchain_merge (struct callchain_root *dst,struct callchain_root *src);bool ip_callchain__valid (struct ip_callchain *chain,const event_t *event); + +struct dwarf_callchain *callchain_unwind (struct perf_session *session, +struct thread *thread, +struct sample_data *data);#endif /* __PERF_CALLCHAIN_H */
tools/perf/util/unwind.c new file +1112 -0
1,112 lines not on this page · raw message
Signature · 1 line
1.6.2.3