Skip to contentSkip to conversation

perf: Add backtrace post dwarf unwind

RFC Superseded Jiri Olsa 3 participants 28 messages Apr 17, 2012 – May 3, 2012

Subsystems (3)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

v2 cover perf: Add backtrace post dwarf unwind

hi,
sending another RFC version. There are some fixies and some yet unresolved issues outlined below.

thanks for comments,
jirka

v2 changes:
02/16 - fixed register enums
12/16 - fixed the perf_evlist__* names
14/16 - 'fp' stays default even if compiled with dwarf unwind 15/16 - added cache to the DSO data interface, it makes the
dwarf unwind real fast now

v2 not solved yet:
1) generic user regs capturing
discussed in here:
http://marc.info/?l=linux-kernel&m=133304076114629&w=2

Looks like we could have a generic way of getting registers on samples and support more than only user level registers. But looks like it wasn't completely decided what register levels are worth to store..

It looks to me that each level could add its own registers mask and it'd be used/saved if the mask is non zero. The same way the user level regs are dealt with now ;).

Or we could add something that Stephane came up with:
attr->sample_type |= PERF_SAMPLE_REGS attr->sample_regs = EAX | EBX | EDI | ESI |..... attr->sample_reg_mode = { INTR, PRECISE, USER }

But I guess we need to decide what levels make sense to store first. Also if there's like 2 or 3 only, it might be better to use separate masks as pointed out above.

2) compat task handling
we dont handle compat tasks unwinding currently, we probably want to make it part of the 1) solution or add something like:
__u64 user_regs_compat;

Handling the compat task would also need some other changes in the unwind code and I'm not completely sure how libunwind deals with that, need to check.

How much do we want this? ;)

3) registers version names
this one is now probably connected to 1) and 2) ;)
I kept the register version, since I think it might be usefull for dealing with compat tasks - to recognize what type of registers were stored

---
Adding 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/16 uaccess: Add new copy_from_user_gup API
02/16 perf: Unified API to record selective sets of arch registers 03/16 perf: Add ability to dump user regs
04/16 perf: Add ability to dump part of the user stack 05/16 perf: Add attribute to filter out user callchains 06/16 perf, tool: Factor DSO symtab types to generic binary types 07/16 perf, tool: Add interface to read DSO image data 08/16 perf, tool: Add '.note' check into search for NOTE section 09/16 perf, tool: Back [vdso] DSO with real data
10/16 perf, tool: Add interface to arch registers sets 11/16 perf, tool: Add libunwind dependency for dwarf cfi unwinding 12/16 perf, tool: Support user regs and stack in sample parsing 13/16 perf, tool: Support for dwarf cfi unwinding on post processing 14/16 perf, tool: Support for dwarf mode callchain on perf record 15/16 perf, tool: Add dso data caching
16/16 perf, tool: Add dso data caching tests

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_write

Also 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

thanks for comments,
jirka

---
 arch/x86/include/asm/perf_regs.h                   |   16 +
 arch/x86/include/asm/perf_regs_32.h                |   84 +++
 arch/x86/include/asm/perf_regs_64.h                |  101 ++++
 arch/x86/include/asm/uaccess.h                     |    7 +-
 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/perf_regs.h                    |   23 +
 include/asm-generic/uaccess.h                      |    4 +
 include/linux/perf_event.h                         |   18 +-
 kernel/events/callchain.c                          |    4 +-
 kernel/events/core.c                               |  127 +++++-
 kernel/events/internal.h                           |   59 ++-
 kernel/events/ring_buffer.c                        |    4 +-
 tools/perf/Makefile                                |   41 ++-
 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                        |   86 +++-
 tools/perf/builtin-report.c                        |   24 +-
 tools/perf/builtin-script.c                        |   56 ++-
 tools/perf/builtin-test.c                          |    7 +-
 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/dso-test.c                         |  154 ++++++
 tools/perf/util/event.h                            |   15 +-
 tools/perf/util/evlist.c                           |   16 +
 tools/perf/util/evlist.h                           |    2 +
 tools/perf/util/evsel.c                            |   35 ++-
 tools/perf/util/include/linux/compiler.h           |    1 +
 tools/perf/util/map.c                              |   23 +-
 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                          |  104 ++++-
 tools/perf/util/session.h                          |   10 +-
 tools/perf/util/symbol.c                           |  435 +++++++++++++---
 tools/perf/util/symbol.h                           |   52 ++-
 tools/perf/util/trace-event-scripting.c            |    3 +-
 tools/perf/util/trace-event.h                      |    5 +-
 tools/perf/util/unwind.c                           |  565 ++++++++++++++++++++
 tools/perf/util/unwind.h                           |   34 ++
 tools/perf/util/vdso.c                             |   90 +++
 tools/perf/util/vdso.h                             |    8 +
 50 files changed, 2315 insertions(+), 199 deletions(-)
author
#1 lore raw

PATCH 1/16 uaccess: Add 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.

---
 arch/x86/include/asm/uaccess.h             |    7 +++++--
 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(+), 10 deletions(-)
arch/x86/include/asm/uaccess.h +5 -2
index e054459..b8724e9 100644
@@ -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,8 +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);
 extern __must_check long
 strncpy_from_user(char *dst, const char __user *src, long count);
 
arch/x86/kernel/cpu/perf_event.c +2 -2
index bb8e034..294ad68 100644
@@ -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
index 7f64df1..77053af 100644
@@ -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
index 520b426..8f0b9ce 100644
@@ -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
index d6ae30b..e797d6e 100644
@@ -13,7 +13,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;
@@ -42,7 +42,7 @@ 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);
 
 static inline unsigned long count_bytes(unsigned long mask)
 {
arch/x86/oprofile/backtrace.c +2 -2
index d6aa6e8..42511b0 100644
@@ -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
index 9788568..759339b 100644
@@ -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.7.6
author
#2 lore raw

PATCH 2/16 perf: Unified API to record selective sets of arch registers

In reply to Jiri Olsa

This 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/x86/include/asm/perf_regs.h    |   16 ++++++
 arch/x86/include/asm/perf_regs_32.h |   84 +++++++++++++++++++++++++++++
 arch/x86/include/asm/perf_regs_64.h |  101 +++++++++++++++++++++++++++++++++++
 include/asm-generic/perf_regs.h     |   23 ++++++++
 4 files changed, 224 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
 create mode 100644 include/asm-generic/perf_regs.h
arch/x86/include/asm/perf_regs.h new file +16 -0
new file mode 100644
index 0000000..80b7fbe
@@ -0,0 +1,16 @@
+#ifndef _ASM_X86_PERF_REGS_H
+#define _ASM_X86_PERF_REGS_H
+
+enum {
+	PERF_REGS_VERSION_NONE   = 0UL,
+	PERF_REGS_VERSION_X86_32 = 1UL,
+	PERF_REGS_VERSION_X86_64 = 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 +84 -0

84 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

include/asm-generic/perf_regs.h new file +23 -0
new file mode 100644
index 0000000..f616096
@@ -0,0 +1,23 @@
+#ifndef __ASM_GENERIC_PERF_REGS_H
+#define __ASM_GENERIC_PERF_REGS_H
+
+enum {
+	PERF_REGS_VERSION_NONE = 0UL,
+};
+
+static inline int perf_reg_value(struct pt_regs *regs, int idx)
+{
+	return 0;
+}
+
+static inline int perf_reg_version(void)
+{
+	return PERF_REGS_VERSION_NONE;
+}
+
+static inline int perf_reg_validate(u64 mask)
+{
+	return mask ? -ENOSYS : 0;
+}
+
+#endif /* __ASM_GENERIC_PERF_REGS_H */
Signature · 1 line
1.7.7.6
#3 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 32 lines · Jiri Olsa

This 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/x86/include/asm/perf_regs.h    |   16 ++++++ arch/x86/include/asm/perf_regs_32.h |   84 +++++++++++++++++++++++++++++ arch/x86/include/asm/perf_regs_64.h |  101 +++++++++++++++++++++++++++++++++++ include/asm-generic/perf_regs.h     |   23 ++++++++

 4 files changed, 224 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 create mode 100644 include/asm-generic/perf_regs.h

arch/x86/include/asm/perf_regs.h new file +0 -0
new file mode 100644
index 0000000..80b7fbe

arch/x86/include/asm/perf_regs.h line 9

Quoted 9 lines
arch/x86/include/asm/perf_regs.h new file +9 -0
@@ -0,0 +1,16 @@
+#ifndef _ASM_X86_PERF_REGS_H
+#define _ASM_X86_PERF_REGS_H
+
+enum {
+       PERF_REGS_VERSION_NONE   = 0UL,
+       PERF_REGS_VERSION_X86_32 = 1UL,
+       PERF_REGS_VERSION_X86_64 = 2UL,
+};
+

I don't really like the term VERSION here. It's not a versioning problem you're trying to solve. It's an ABI problem, unless I am mistaken. You should rename to PERF_REGS_ABI_X86_32 and PERF_REGS_ABI_X86_64.

I assume the NONE is here to cover the case where you don't have a user machine state, i.e., hit a kernel thread. Is that right?

arch/x86/include/asm/perf_regs.h line 15

Quoted 6 lines
arch/x86/include/asm/perf_regs.h new file +6 -0
+#ifdef CONFIG_X86_32
+#include "perf_regs_32.h"
+#else
+#include "perf_regs_64.h"
+#endif
+

How are you going to deal with 32-bit binaries sampled on a 64-bit system?

Quoted 230 lines
arch/x86/include/asm/perf_regs.h new file +1 -0
+#endif /* _ASM_X86_PERF_REGS_H */
arch/x86/include/asm/perf_regs_32.h new file +84 -0
new file mode 100644
index 0000000..3c5aa80
@@ -0,0 +1,84 @@
+#ifndef _ASM_X86_PERF_REGS_32_H
+#define _ASM_X86_PERF_REGS_32_H
+
+enum perf_event_x86_32_regs {
+       PERF_X86_32_REG_EAX,
+       PERF_X86_32_REG_EBX,
+       PERF_X86_32_REG_ECX,
+       PERF_X86_32_REG_EDX,
+       PERF_X86_32_REG_ESI,
+       PERF_X86_32_REG_EDI,
+       PERF_X86_32_REG_EBP,
+       PERF_X86_32_REG_ESP,
+       PERF_X86_32_REG_EIP,
+       PERF_X86_32_REG_FLAGS,
+       PERF_X86_32_REG_CS,
+       PERF_X86_32_REG_DS,
+       PERF_X86_32_REG_ES,
+       PERF_X86_32_REG_FS,
+       PERF_X86_32_REG_GS,
+
+       /* Non ABI */
+       PERF_X86_32_REG_MAX,
+       PERF_REG_IP = PERF_X86_32_REG_EIP,
+       PERF_REG_SP = PERF_X86_32_REG_ESP,
+};
+
+#ifdef __KERNEL__
+
+#define PERF_X86_32_REG_RESERVED (~((1ULL << PERF_X86_32_REG_MAX) - 1ULL))
+
+static inline u64 perf_reg_version(void)
+{
+       return PERF_REGS_VERSION_X86_32;
+}
+
+static inline int perf_reg_validate(u64 mask)
+{
+       if (mask & PERF_X86_32_REG_RESERVED)
+               return -EINVAL;
+
+       return 0;
+}
+
+static inline u64 perf_reg_value(struct pt_regs *regs, int idx)
+{
+       switch (idx) {
+       case PERF_X86_32_REG_EAX:
+               return regs->ax;
+       case PERF_X86_32_REG_EBX:
+               return regs->bx;
+       case PERF_X86_32_REG_ECX:
+               return regs->cx;
+       case PERF_X86_32_REG_EDX:
+               return regs->dx;
+       case PERF_X86_32_REG_ESI:
+               return regs->si;
+       case PERF_X86_32_REG_EDI:
+               return regs->di;
+       case PERF_X86_32_REG_EBP:
+               return regs->bp;
+       case PERF_X86_32_REG_ESP:
+               return regs->sp;
+       case PERF_X86_32_REG_EIP:
+               return regs->ip;
+       case PERF_X86_32_REG_FLAGS:
+               return regs->flags;
+       case PERF_X86_32_REG_CS:
+               return regs->cs;
+       case PERF_X86_32_REG_DS:
+               return regs->ds;
+       case PERF_X86_32_REG_ES:
+               return regs->es;
+       case PERF_X86_32_REG_FS:
+               return regs->fs;
+       case PERF_X86_32_REG_GS:
+               return regs->gs;
+       }
+
+       return 0;
+}
+
+#endif /* __KERNEL__ */
+
+#endif /* _ASM_X86_PERF_REGS_32_H */
arch/x86/include/asm/perf_regs_64.h new file +101 -0
new file mode 100644
index 0000000..d775213
@@ -0,0 +1,101 @@
+#ifndef _ASM_X86_PERF_REGS_64_H
+#define _ASM_X86_PERF_REGS_64_H
+
+#define PERF_X86_64_REG_VERSION                1ULL
+
+enum perf_event_x86_64_regs {
+       PERF_X86_64_REG_RAX,
+       PERF_X86_64_REG_RBX,
+       PERF_X86_64_REG_RCX,
+       PERF_X86_64_REG_RDX,
+       PERF_X86_64_REG_RSI,
+       PERF_X86_64_REG_RDI,
+       PERF_X86_64_REG_R8,
+       PERF_X86_64_REG_R9,
+       PERF_X86_64_REG_R10,
+       PERF_X86_64_REG_R11,
+       PERF_X86_64_REG_R12,
+       PERF_X86_64_REG_R13,
+       PERF_X86_64_REG_R14,
+       PERF_X86_64_REG_R15,
+       PERF_X86_64_REG_RBP,
+       PERF_X86_64_REG_RSP,
+       PERF_X86_64_REG_RIP,
+       PERF_X86_64_REG_FLAGS,
+       PERF_X86_64_REG_CS,
+       PERF_X86_64_REG_SS,
+
+       /* Non ABI */
+       PERF_X86_64_REG_MAX,
+       PERF_REG_IP = PERF_X86_64_REG_RIP,
+       PERF_REG_SP = PERF_X86_64_REG_RSP,
+};
+
+#ifdef __KERNEL__
+
+#define PERF_X86_64_REG_RESERVED (~((1ULL << PERF_X86_64_REG_MAX) - 1ULL))
+
+static inline u64 perf_reg_version(void)
+{
+       return PERF_REGS_VERSION_X86_64;
+}
+
+static inline int perf_reg_validate(u64 mask)
+{
+       if (mask & PERF_X86_64_REG_RESERVED)
+               return -EINVAL;
+
+       return 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->r9;
+       case PERF_X86_64_REG_R10:
+               return regs->r10;
+       case PERF_X86_64_REG_R11:
+               return regs->r11;
+       case PERF_X86_64_REG_R12:
+               return regs->r12;
+       case PERF_X86_64_REG_R13:
+               return regs->r13;
+       case PERF_X86_64_REG_R14:
+               return regs->r14;
+       case PERF_X86_64_REG_R15:
+               return regs->r15;
+       case PERF_X86_64_REG_RBP:
+               return regs->bp;
+       case PERF_X86_64_REG_RSP:
+               return regs->sp;
+       case PERF_X86_64_REG_RIP:
+               return regs->ip;
+       case PERF_X86_64_REG_FLAGS:
+               return regs->flags;
+       case PERF_X86_64_REG_CS:
+               return regs->cs;
+       case PERF_X86_64_REG_SS:
+               return regs->ss;
+       }
+
+       return 0;
+}
+
+#endif /* __KERNEL__ */
+
+#endif /* _ASM_X86_PERF_REGS_64_H */
include/asm-generic/perf_regs.h new file +23 -1
new file mode 100644
index 0000000..f616096
@@ -0,0 +1,23 @@
+#ifndef __ASM_GENERIC_PERF_REGS_H
+#define __ASM_GENERIC_PERF_REGS_H
+
+enum {
+       PERF_REGS_VERSION_NONE = 0UL,
+};
+
+static inline int perf_reg_value(struct pt_regs *regs, int idx)
+{
+       return 0;
+}
+
+static inline int perf_reg_version(void)
+{
+       return PERF_REGS_VERSION_NONE;
+}
+
+static inline int perf_reg_validate(u64 mask)
+{
+       return mask ? -ENOSYS : 0;
+}
+
+#endif /* __ASM_GENERIC_PERF_REGS_H */
--

1.7.7.6

author
#4 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 47 lines · Stephane Eranian
Quoted 32 lines · Jiri Olsa

This 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/x86/include/asm/perf_regs.h    |   16 ++++++ arch/x86/include/asm/perf_regs_32.h |   84 +++++++++++++++++++++++++++++ arch/x86/include/asm/perf_regs_64.h |  101 +++++++++++++++++++++++++++++++++++ include/asm-generic/perf_regs.h     |   23 ++++++++

 4 files changed, 224 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 create mode 100644 include/asm-generic/perf_regs.h

arch/x86/include/asm/perf_regs.h new file +0 -0
new file mode 100644
index 0000000..80b7fbe

arch/x86/include/asm/perf_regs.h line 9

Quoted 9 lines
arch/x86/include/asm/perf_regs.h new file +9 -0
@@ -0,0 +1,16 @@
+#ifndef _ASM_X86_PERF_REGS_H
+#define _ASM_X86_PERF_REGS_H
+
+enum {
+       PERF_REGS_VERSION_NONE   = 0UL,
+       PERF_REGS_VERSION_X86_32 = 1UL,
+       PERF_REGS_VERSION_X86_64 = 2UL,
+};
+

I don't really like the term VERSION here. It's not a versioning problem you're trying to solve. It's an ABI problem, unless I am mistaken. You should rename to PERF_REGS_ABI_X86_32 and PERF_REGS_ABI_X86_64.

Quoted 2 lines

I assume the NONE is here to cover the case where you don't have a user machine state, i.e., hit a kernel thread. Is that right?

right

Quoted 9 lines

arch/x86/include/asm/perf_regs.h line 15

Quoted 8 lines
arch/x86/include/asm/perf_regs.h new file +6 -0
+#ifdef CONFIG_X86_32
+#include "perf_regs_32.h"
+#else
+#include "perf_regs_64.h"
+#endif
+

How are you going to deal with 32-bit binaries sampled on a 64-bit system?

I dont have the solution right now... but seems like compat tasks need more thinking even before go ahead with this patchset.. since it's going affect the perf_event_attr and could bite us in future.

I'll see what I can do about that and send out new patchset

jirka

author
#5 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 2 lines · Jiri Olsa
Quoted 1 line · Stephane Eranian

On Tue, Apr 17, 2012 at 1:17 PM, Jiri Olsa <[email protected]> wrote:

SNIP

Quoted 5 lines
Quoted 1 line

How are you going to deal with 32-bit binaries sampled on a 64-bit system?

I dont have the solution right now... but seems like compat tasks need more thinking even before go ahead with this patchset.. since it's going affect the perf_event_attr and could bite us in future.

hi,
got more info on the compat task unwind

- for 32 bit task running under 64 bit env. the 64 bits user registers values are stored on kernel stack when entering the kernel via exception or interrupt, like for native 64 bit task

So I think we can keep the current interface as far as compat tasks are concerned, since we will get 64 bits registers all the time anyway.

The place that will take care of compat task unwind is the post processing unwind.

For each processed sample we:
- get the sample and translate IP into MAP and DSO
- read DSO ELF class and figure out wether we deal with 64 or 32 bit task
- run libunwind interface with proper task class info, which gets us to next bullet:

- 64 bit libunwind does not support unwind of 32 bit tasks ;) so unless that change, I can see just one hacky way of doing this via 32 bit libunwind being loaded in separate 32 bit process and doing remote unwind for us..

I'll try to follow on this to see if there'd be some better libunwind interface solution.. but thats quite longterm ;)

As for the sample registers interface.

Currently we have:

u64 user_sample_regs
- if != 0 we provide the user registers with mask specified by its value

- it will stay for compat tasks as well
- we could use PERF_SAMPLE_USER_REGS sample type instead of the != 0 check to be more consistent, but that would eat up one sample bit unnecessary

In some previous email you suggested some generic interface like

    attr->sample_type |= PERF_SAMPLE_REGS
    attr->sample_regs = EAX | EBX | EDI | ESI |.....
    attr->sample_reg_mode = { INTR, PRECISE, USER }

I think we can have something like:

    attr->sample_type |= PERF_SAMPLE_REGS
    attr->sample_reg_mode = { INTR, PRECISE, USER }

but in case we want eg both USER and INTR modes together then we still need to have:

u64 user_sample_regs
u64 intr_sample_regs
...

for the register modes mask definition. Some mode combinations might be useless, but I think this could work.. we could always customize our needs with new mode ;)

I'll start to work on this unless I hear some screaming ;)

thoughts? ;)

thanks and sorry for long email,
jirka

#6 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Sorry for the delay, had higher priority tasks to do. [+asharma]

Quoted 11 lines · Jiri Olsa
Quoted 2 lines · Jiri Olsa
Quoted 1 line · Stephane Eranian

On Tue, Apr 17, 2012 at 1:17 PM, Jiri Olsa <[email protected]> wrote:

SNIP

Quoted 5 lines
Quoted 1 line

How are you going to deal with 32-bit binaries sampled on a 64-bit system?

I dont have the solution right now... but seems like compat tasks need more thinking even before go ahead with this patchset.. since it's going affect the perf_event_attr and could bite us in future.

Quoted 8 lines

hi,
got more info on the compat task unwind

- for 32 bit task running under 64 bit env. the 64 bits user registers values are stored on kernel stack when entering the kernel via exception or interrupt, like for native 64 bit task

You mean the 32-bit registers are stored on the kernel stack, right? Or you mean 64-bit and the upper 32 are guaranteed 0.

Quoted 14 lines

 So I think we can keep the current interface as far as compat tasks are concerned, since we will get 64 bits registers all the time anyway.

 The place that will take care of compat task unwind is the post processing unwind.

 For each processed sample we:
    - get the sample and translate IP into MAP and DSO
    - read DSO ELF class and figure out wether we deal with 64 or 32 bit task
    - run libunwind interface with proper task class info, which gets us to next bullet:

Quoted 4 lines

- 64 bit libunwind does not support unwind of 32 bit tasks ;) so unless that change, I can see just one hacky way of doing this via 32 bit libunwind being loaded in separate 32 bit process and doing remote unwind for us..

okay was not aware of that restriction on libunwind. I copied Arun on this response, so maybe he can comment on that.

Quoted 7 lines

 I'll try to follow on this to see if there'd be some better libunwind interface solution.. but thats quite longterm ;)

As for the sample registers interface.

Quoted 7 lines

Currently we have:

 u64 user_sample_regs
 - if != 0 we provide the user registers with mask specified by its value

 - it will stay for compat tasks as well

What if I say EAX|EBX|R15? but the sample was captured
on a 32-bit tasks. Are you going to just store 0 for R15? Unless you also store a bitmask of what was actually saved, then you have to fill in non-existent registers with zeroes, otherwise the tool cannot parse the sample.

Quoted 3 lines

 - we could use PERF_SAMPLE_USER_REGS sample type instead of the != 0 check to be more consistent, but that would eat up one sample bit unnecessary

But then that would be aligned with how branch_stack has been implemented for instance (PERF_SAMPLE_BRANCH_STACK).

Quoted 12 lines

In some previous email you suggested some generic interface like

   attr->sample_type |= PERF_SAMPLE_REGS attr->sample_regs = EAX | EBX | EDI | ESI |..... attr->sample_reg_mode = { INTR, PRECISE, USER }

I think we can have something like:

   attr->sample_type |= PERF_SAMPLE_REGS attr->sample_reg_mode = { INTR, PRECISE, USER }

Quoted 7 lines

but in case we want eg both USER and INTR modes together then we still need to have:

 u64 user_sample_regs
 u64 intr_sample_regs
 ...

Yes. but if we allow any combinations, then you'd need u64 user_sample_regs
u64 intr_sample_regs
u64 precise_sample_regs

Note that in the case of Intel PEBS used for precise mode, there are only a subset of the INTR registers available.

Quoted 4 lines

for the register modes mask definition. Some mode combinations might be useless, but I think this could work.. we could always customize our needs with new mode ;)

The INTR vs. PRECISE is useful to get an idea of the skid. The USER vs. INTR is useful to determine how we entered the kernel in case the IP @ INTR is in the kernel.

Quoted 2 lines

I'll start to work on this unless I hear some screaming ;)

In any case, the important issue is how does the kernel satisfy the request for registers when those may not be available in the interrupt task AND it is impossible to know this in advance.

Note that in the case of precise on Intel, we know in advance which registers will be available. So you can fail early, when the event is created.

The alternative is to include the bitmask of which registers was actually saved at the beginning of the section after the ABI type flag.

Quoted 5 lines

thoughts? ;)

thanks and sorry for long email,
jirka

author
#7 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 1 line · Stephane Eranian

Sorry for the delay, had higher priority tasks to do.

hi,
np at all :)
I just sent v3, but I answered some of your comments below

thanks,
jirka

Quoted 17 lines

[+asharma]

Quoted 14 lines · Jiri Olsa
Quoted 2 lines · Jiri Olsa
Quoted 1 line · Stephane Eranian

On Tue, Apr 17, 2012 at 1:17 PM, Jiri Olsa <[email protected]> wrote:

SNIP

Quoted 5 lines
Quoted 1 line

How are you going to deal with 32-bit binaries sampled on a 64-bit system?

I dont have the solution right now... but seems like compat tasks need more thinking even before go ahead with this patchset.. since it's going affect the perf_event_attr and could bite us in future.

hi,
got more info on the compat task unwind

Quoted 7 lines
Quoted 5 lines

- for 32 bit task running under 64 bit env. the 64 bits user registers values are stored on kernel stack when entering the kernel via exception or interrupt, like for native 64 bit task

You mean the 32-bit registers are stored on the kernel stack, right? Or you mean 64-bit and the upper 32 are guaranteed 0.

I meant 64 bit registers are stored on stack the same way as for native process. There are different code paths for exception, but same registers' saved stack layout.

So if there's an event within the compat task, you still get 64 bit registers saved on stack as if the event happened in native process.

The upper 32 are probably 0, but I'm not sure that's garanteed.

Quoted 37 lines
Quoted 16 lines

 So I think we can keep the current interface as far as compat tasks are concerned, since we will get 64 bits registers all the time anyway.

 The place that will take care of compat task unwind is the post processing unwind.

 For each processed sample we:
    - get the sample and translate IP into MAP and DSO
    - read DSO ELF class and figure out wether we deal with 64 or 32 bit task
    - run libunwind interface with proper task class info, which gets us to next bullet:

Quoted 4 lines

- 64 bit libunwind does not support unwind of 32 bit tasks ;) so unless that change, I can see just one hacky way of doing this via 32 bit libunwind being loaded in separate 32 bit process and doing remote unwind for us..

okay was not aware of that restriction on libunwind. I copied Arun on this response, so maybe he can comment on that.

Quoted 13 lines

 I'll try to follow on this to see if there'd be some better libunwind interface solution.. but thats quite longterm ;)

As for the sample registers interface.

Currently we have:

 u64 user_sample_regs
 - if != 0 we provide the user registers with mask specified by its value

Quoted 7 lines
Quoted 1 line

 - it will stay for compat tasks as well

What if I say EAX|EBX|R15? but the sample was captured
on a 32-bit tasks. Are you going to just store 0 for R15? Unless you also store a bitmask of what was actually saved, then you have to fill in non-existent registers with zeroes, otherwise the tool cannot parse the sample.

I just sent v3, with changed design to be more generic, please check

anyway, currently there's no way to mix 32 and 64 bit registers in sample.

As I mentioned above, once running compat task, 64 bit registers are stored anyway. Given that all 32 bit registers have 64 equiv. you can ask to store RAX|RBX|R15.

You need to know wether to examine 32 or 64 bit register afterwards.

Quoted 36 lines
Quoted 5 lines

 - we could use PERF_SAMPLE_USER_REGS sample type instead of the != 0 check to be more consistent, but that would eat up one sample bit unnecessary

But then that would be aligned with how branch_stack has been implemented for instance (PERF_SAMPLE_BRANCH_STACK).

Quoted 12 lines

In some previous email you suggested some generic interface like

   attr->sample_type |= PERF_SAMPLE_REGS attr->sample_regs = EAX | EBX | EDI | ESI |..... attr->sample_reg_mode = { INTR, PRECISE, USER }

I think we can have something like:

   attr->sample_type |= PERF_SAMPLE_REGS attr->sample_reg_mode = { INTR, PRECISE, USER }

Quoted 7 lines

but in case we want eg both USER and INTR modes together then we still need to have:

 u64 user_sample_regs
 u64 intr_sample_regs
 ...

Yes. but if we allow any combinations, then you'd need u64 user_sample_regs
u64 intr_sample_regs
u64 precise_sample_regs

Note that in the case of Intel PEBS used for precise mode, there are only a subset of the INTR registers available.

Quoted 10 lines
Quoted 4 lines

for the register modes mask definition. Some mode combinations might be useless, but I think this could work.. we could always customize our needs with new mode ;)

The INTR vs. PRECISE is useful to get an idea of the skid. The USER vs. INTR is useful to determine how we entered the kernel in case the IP @ INTR is in the kernel.

Quoted 2 lines

I'll start to work on this unless I hear some screaming ;)

my thinking with v3 was to have new sample type PERF_SAMPLE_REGS

Once set there's perf_event_attr:sample_regs value carying the king of registers we want to store.

Currently there's just following user regs bit:

enum perf_sample_regs {
PERF_SAMPLE_REGS_USER = 1U << 0, /* user registers */
PERF_SAMPLE_REGS_MAX = 1U << 1, /* non-ABI */
};

If PERF_SAMPLE_REGS_USER is set then perf_event_attr::sample_regs_user gives the mask of user registers to store.

we could add more bits like:

       PERF_SAMPLE_REGS_KERNEL
       PERF_SAMPLE_REGS_PRECISE
       ...

to determine the kind of registers we want to dump and retrieve registers accordingly. And if the bit needs additional info we add new perf_event_attr value same like in sample_regs_user case.

Quoted 20 lines

In any case, the important issue is how does the kernel satisfy the request for registers when those may not be available in the interrupt task AND it is impossible to know this in advance.

Note that in the case of precise on Intel, we know in advance which registers will be available. So you can fail early, when the event is created.

The alternative is to include the bitmask of which registers was actually saved at the beginning of the section after the ABI type flag.

Quoted 5 lines

thoughts? ;)

thanks and sorry for long email,
jirka

#8 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 90 lines · Jiri Olsa
Quoted 1 line · Stephane Eranian

Sorry for the delay, had higher priority tasks to do.

hi,
np at all :)
I just sent v3, but I answered some of your comments below

thanks,
jirka

Quoted 17 lines

[+asharma]

Quoted 14 lines · Jiri Olsa
Quoted 2 lines · Jiri Olsa
Quoted 1 line · Stephane Eranian

On Tue, Apr 17, 2012 at 1:17 PM, Jiri Olsa <[email protected]> wrote:

SNIP

Quoted 5 lines
Quoted 1 line

How are you going to deal with 32-bit binaries sampled on a 64-bit system?

I dont have the solution right now... but seems like compat tasks need more thinking even before go ahead with this patchset.. since it's going affect the perf_event_attr and could bite us in future.

hi,
got more info on the compat task unwind

Quoted 7 lines
Quoted 5 lines

- for 32 bit task running under 64 bit env. the 64 bits user áregisters values are stored on kernel stack when entering áthe kernel via exception or interrupt, like for native á64 bit task

You mean the 32-bit registers are stored on the kernel stack, right? Or you mean 64-bit and the upper 32 are guaranteed 0.

I meant 64 bit registers are stored on stack the same way as for native process. There are different code paths for exception, but same registers' saved stack layout.

So if there's an event within the compat task, you still get 64 bit registers saved on stack as if the event happened in native process.

The upper 32 are probably 0, but I'm not sure that's garanteed.

Quoted 37 lines
Quoted 16 lines

áSo I think we can keep the current interface as far as ácompat tasks are concerned, since we will get 64 bits áregisters all the time anyway.

áThe place that will take care of compat task unwind áis the post processing unwind.

áFor each processed sample we:
á á - get the sample and translate IP into MAP and DSO á á - read DSO ELF class and figure out wether we deal with á á á 64 or 32 bit task
á á - run libunwind interface with proper task class info, á á á which gets us to next bullet:

Quoted 4 lines

- 64 bit libunwind does not support unwind of 32 bit tasks ;) áso unless that change, I can see just one hacky way of doing áthis via 32 bit libunwind being loaded in separate 32 bit áprocess and doing remote unwind for us..

okay was not aware of that restriction on libunwind. I copied Arun on this response, so maybe he can comment on that.

Quoted 13 lines

áI'll try to follow on this to see if there'd be some better álibunwind interface solution.. but thats quite longterm ;)

As for the sample registers interface.

Currently we have:

áu64 user_sample_regs
á- if != 0 we provide the user registers with mask specified á áby its value

Quoted 7 lines
Quoted 1 line

á- it will stay for compat tasks as well

What if I say EAX|EBX|R15? but the sample was captured
on a 32-bit tasks. Are you going to just store 0 for R15? Unless you also store a bitmask of what was actually saved, then you have to fill in non-existent registers with zeroes, otherwise the tool cannot parse the sample.

Quoted 8 lines

I just sent v3, with changed design to be more generic, please check

anyway, currently there's no way to mix 32 and 64 bit registers in sample.

As I mentioned above, once running compat task, 64 bit registers are stored anyway. Given that all 32 bit registers have 64 equiv. you can ask to store RAX|RBX|R15.

Well, R8-R15 do not exist in 32-bit mode. So I wonder what is saved on the stack for those, probably nothing. And in that case, how do you handle the case where the user asked for R15 but it is not available and you know that only on PMU interrupt.

Quoted 95 lines

You need to know wether to examine 32 or 64 bit register afterwards.

Quoted 36 lines
Quoted 5 lines

á- we could use PERF_SAMPLE_USER_REGS sample type instead of the != 0 á ácheck to be more consistent, but that would eat up one sample bit á áunnecessary

But then that would be aligned with how branch_stack has been implemented for instance (PERF_SAMPLE_BRANCH_STACK).

Quoted 12 lines

In some previous email you suggested some generic interface like

á áattr->sample_type |= PERF_SAMPLE_REGS
á áattr->sample_regs = EAX | EBX | EDI | ESI |..... á áattr->sample_reg_mode = { INTR, PRECISE, USER }

I think we can have something like:

á áattr->sample_type |= PERF_SAMPLE_REGS
á áattr->sample_reg_mode = { INTR, PRECISE, USER }

Quoted 7 lines

but in case we want eg both USER and INTR modes together then we still need to have:

áu64 user_sample_regs
áu64 intr_sample_regs
á...

Yes. but if we allow any combinations, then you'd need u64 user_sample_regs
u64 intr_sample_regs
u64 precise_sample_regs

Note that in the case of Intel PEBS used for precise mode, there are only a subset of the INTR registers available.

Quoted 10 lines
Quoted 4 lines

for the register modes mask definition. Some mode combinations might be useless, but I think this could work.. we could always customize our needs with new mode ;)

The INTR vs. PRECISE is useful to get an idea of the skid. The USER vs. INTR is useful to determine how we entered the kernel in case the IP @ INTR is in the kernel.

Quoted 2 lines

I'll start to work on this unless I hear some screaming ;)

my thinking with v3 was to have new sample type PERF_SAMPLE_REGS

Once set there's perf_event_attr:sample_regs value carying the king of registers we want to store.

Currently there's just following user regs bit:

enum perf_sample_regs {
      PERF_SAMPLE_REGS_USER   = 1U << 0, /* user registers */ PERF_SAMPLE_REGS_MAX    = 1U << 1, /* non-ABI */ };

If PERF_SAMPLE_REGS_USER is set then perf_event_attr::sample_regs_user gives the mask of user registers to store.

we could add more bits like:
      PERF_SAMPLE_REGS_KERNEL
      PERF_SAMPLE_REGS_PRECISE
      ...

to determine the kind of registers we want to dump and retrieve registers accordingly. And if the bit needs additional info we add new perf_event_attr value same like in sample_regs_user case.

Quoted 20 lines

In any case, the important issue is how does the kernel satisfy the request for registers when those  may not be available in the interrupt task AND it is impossible to know this in advance.

Note that in the case of precise on Intel, we know in advance which registers will be available. So you can fail early, when the event is created.

The alternative is to include the bitmask of which registers was actually saved at the beginning of the section after the ABI type flag.

Quoted 5 lines

thoughts? ;)

thanks and sorry for long email,
jirka

author
#9 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

SNIP

Quoted 5 lines
Quoted 5 lines

I just sent v3, with changed design to be more generic, please check

anyway, currently there's no way to mix 32 and 64 bit registers in sample.

Quoted 8 lines
Quoted 4 lines

As I mentioned above, once running compat task, 64 bit registers are stored anyway. Given that all 32 bit registers have 64 equiv. you can ask to store RAX|RBX|R15.

Well, R8-R15 do not exist in 32-bit mode. So I wonder what is saved on the stack for those, probably nothing. And in that case, how do you handle the case where the user asked for R15 but it is not available and you know that only on PMU interrupt.

right, R8-R15 do not exist in 32 bit mode, meaning that the 32 bit task do not use them... but when you enter 64 bit kernel from 32 bit compat task, still 64bits registers are saved.. as for native 64 process, there's no difference.. so even for 32 bit task you get 64 bits registers described below.

The availbility of registers is tricky for user space register dump.

We dump whatever we got in pt_regs structure from perf_sample_regs_user. The problem is, that exception do not store all of the registers.. the rest of the registers is whatever is in stack occupying the pt_regs space.

When kernel is entered (by exception or irq), following is saved by CPU (both 32 and 64 bits):
ss, sp, flags, cs ip

followed by SAVE_ARGS (for exception):

        movq_cfi rdi, 8*8
        movq_cfi rsi, 7*8
        movq_cfi rdx, 6*8

        .if \save_rcx
        movq_cfi rcx, 5*8
        .endif

        movq_cfi rax, 4*8

        .if \save_r891011
        movq_cfi r8,  3*8
        movq_cfi r9,  2*8
        movq_cfi r10, 1*8
        movq_cfi r11, 0*8

SAVE_REST - saves the whole pt_regs structure just for the sake of the syscall_trace_enter function, once finished it's poped out..

As for unwind we should get the most important ones. Of course the unwind could be based on any register, and with bogus value the unwind just stops.

I think we are in better shape for irqs but I'd need to check.

jirka

#10 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 12 lines · Jiri Olsa

SNIP

Quoted 9 lines
Quoted 9 lines

I just sent v3, with changed design to be more generic, please check

anyway, currently there's no way to mix 32 and 64 bit registers in sample.

As I mentioned above, once running compat task, 64 bit registers are stored anyway. Given that all 32 bit registers have 64 equiv. you can ask to store RAX|RBX|R15.

Quoted 8 lines
Quoted 4 lines

Well, R8-R15 do not exist in 32-bit mode. So I wonder what is saved on the stack for those, probably nothing. And in that case, how do you handle the case where the user asked for R15 but it is not available and you know that only on PMU interrupt.

right, R8-R15 do not exist in 32 bit mode, meaning that the 32 bit task do not use them... but when you enter 64 bit kernel from 32 bit compat task, still 64bits registers are saved.. as for native 64 process,

I am confused by your term '64-bit registers' here. I assume you mean registers are saved as 64-bit integers. But that does not means that the full set of 64-bit registers (incl. R8-R15) is saved. Unless you're telling
me that whatever values are in those 64-bit ABI only registers are thus saved on the stack.

Quoted 42 lines

there's no difference.. so even for 32 bit task you get 64 bits registers described below.

The availbility of registers is tricky for user space register dump.

We dump whatever we got in pt_regs structure from perf_sample_regs_user. The problem is, that exception do not store all of the registers.. the rest of the registers is whatever is in stack occupying the pt_regs space.

When kernel is entered (by exception or irq), following is saved by CPU (both 32 and 64 bits):
 ss, sp, flags, cs ip

followed by SAVE_ARGS  (for exception):
       movq_cfi rdi, 8*8
       movq_cfi rsi, 7*8
       movq_cfi rdx, 6*8

       .if \save_rcx
       movq_cfi rcx, 5*8
       .endif

       movq_cfi rax, 4*8

       .if \save_r891011
       movq_cfi r8,  3*8
       movq_cfi r9,  2*8
       movq_cfi r10, 1*8
       movq_cfi r11, 0*8

SAVE_REST - saves the whole pt_regs structure just for the sake of the syscall_trace_enter function, once finished it's poped out..

As for unwind we should get the most important ones. Of course the unwind could be based on any register, and with bogus value the unwind just stops.

I think we are in better shape for irqs but I'd need to check.

jirka

author
#11 lore raw

Re: [PATCH 02/16] perf: Unified API to record selective sets of arch registers

Quoted 18 lines · Stephane Eranian
Quoted 17 lines · Jiri Olsa

SNIP

Quoted 13 lines
Quoted 9 lines

I just sent v3, with changed design to be more generic, please check

anyway, currently there's no way to mix 32 and 64 bit registers in sample.

As I mentioned above, once running compat task, 64 bit registers are stored anyway. Given that all 32 bit registers have 64 equiv. you can ask to store RAX|RBX|R15.

Well, R8-R15 do not exist in 32-bit mode. So I wonder what is saved on the stack for those, probably nothing. And in that case, how do you handle the case where the user asked for R15 but it is not available and you know that only on PMU interrupt.

Quoted 7 lines
Quoted 3 lines

right, R8-R15 do not exist in 32 bit mode, meaning that the 32 bit task do not use them... but when you enter 64 bit kernel from 32 bit compat task, still 64bits registers are saved.. as for native 64 process,

I am confused by your term '64-bit registers' here. I assume you mean registers are saved as 64-bit integers. But that does not means that the full set of 64-bit registers (incl. R8-R15) is saved. Unless

same set as for 64 bit tasks.. it's not allways full as I described in previous email

Quoted 3 lines

you're telling
me that whatever values are in those 64-bit ABI only registers are thus saved on the stack.

yep, thats what I see in arch/x86/ia32/ia32entry.S

jirka

author
#12 lore raw

PATCH 3/16 perf: Add ability to dump user regs

In reply to Jiri Olsa

Add 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 register dump.

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.

TODO handle compat tasks

---
 include/linux/perf_event.h |    8 +++++
 kernel/events/core.c       |   63 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 0 deletions(-)
include/linux/perf_event.h +8 -0
index ddbb6a9..c63b807 100644
@@ -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;
 };
 
 /*
@@ -608,6 +614,7 @@ struct perf_guest_info_callbacks {
 #include <linux/atomic.h>
 #include <linux/sysfs.h>
 #include <asm/local.h>
+#include <asm/perf_regs.h>
 
 #define PERF_MAX_STACK_DEPTH		255
 
@@ -1130,6 +1137,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 +63 -0

87 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#13 lore raw

Re: [PATCH 03/16] perf: Add ability to dump user regs

Quoted 30 lines · Jiri Olsa

Add 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 register dump.

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.

TODO handle compat tasks

---
 include/linux/perf_event.h |    8 +++++
 kernel/events/core.c       |   63 ++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 71 insertions(+), 0 deletions(-)
include/linux/perf_event.h +0 -0
index ddbb6a9..c63b807 100644
@@ -272,6 +272,12 @@ struct perf_event_attr {

               __u64           config2; /* extension of config1 */ };

Quoted 9 lines

       __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;
 };

Don't like the name of the field too much. You be more explicit. Something like user_sample_regs.

Quoted 46 lines

 /*

include/linux/perf_event.h +2 -0
@@ -608,6 +614,7 @@ struct perf_guest_info_callbacks {
  #include <linux/atomic.h>
  #include <linux/sysfs.h>
  #include <asm/local.h>
+#include <asm/perf_regs.h>
 
  #define PERF_MAX_STACK_DEPTH           255
 
@@ -1130,6 +1137,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 +21 -0
index a6a9ec4..9f29fc3 100644
@@ -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)
+{

kernel/events/core.c int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) · line 3784

Quoted 10 lines
kernel/events/core.c +10 -0
+       if (!user_mode(regs)) {
+               if (current->mm)
+                       regs = task_pt_regs(current);
+               else
+                       regs = NULL;
+       }
+
+       return regs;
+}
+

You are assuming the user app is running the same ABI than that of the kernel. That's not correct on X86 and possibly other architectures.

Quoted 43 lines
kernel/events/core.c +25 -0
  static void __perf_event_header__init_id(struct perf_event_header *header,
                                         struct perf_sample_data *data,
                                         struct perf_event *event)
@@ -4011,6 +4042,21 @@ void perf_output_sample(struct perf_output_handle *handle,
                        perf_output_put(handle, nr);
                }
        }
+
+       if (event->attr.user_regs) {
+               u64 id;
+
+               /*
+                * If there are no regs to dump, notice it through a
+                * PERF_REGS_VERSION_NONE version.
+                */
+               id = data->uregs ? perf_reg_version() : PERF_REGS_VERSION_NONE;
+               perf_output_put(handle, id);
+
+               if (id)
+                       perf_output_sample_regs(handle, data->uregs,
+                                               event->attr.user_regs);
+       }
  }
 
  void perf_prepare_sample(struct perf_event_header *header,
@@ -4062,6 +4108,16 @@ void perf_prepare_sample(struct perf_event_header *header,
                }
                header->size += size;
        }
+
+       if (event->attr.user_regs) {
+               int size = sizeof(u64); /* the version size */
+
+               data->uregs = perf_sample_uregs(regs);
+               if (data->uregs)
+                       size += hweight64(event->attr.user_regs) * sizeof(u64);
+
+               header->size += size;
+       }
  }
 
  static void perf_event_output(struct perf_event *event,

kernel/events/core.c static int perf_copy_attr(struct perf_event_attr __user *uattr, · line 6177

Quoted 10 lines
kernel/events/core.c +7 -0
@@ -6112,6 +6168,13 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
                        attr->branch_sample_type = mask;
                }
        }
+
+       /*
+        * Don't let throught invalid register mask (i.e. the architecture
+        * does not support register dump at all).
+        */
+       ret = perf_reg_validate(attr->user_regs);
+

Again, how do you deal with 32-bit apps vs. 64-bit kernel?

Quoted 6 lines

 out:
       return ret;

--
1.7.7.6

author
#14 lore raw

PATCH 4/16 perf: Add ability to dump part of the user stack

In reply to Jiri Olsa

Beeing 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        |   61 +++++++++++++++++++++++++++++++++++++++++++
 kernel/events/internal.h    |   56 ++++++++++++++++++++++++---------------
 kernel/events/ring_buffer.c |    4 +-
 4 files changed, 102 insertions(+), 25 deletions(-)
include/linux/perf_event.h +4 -2
index c63b807..e40bac5 100644
@@ -278,6 +278,7 @@ struct perf_event_attr {
 	 * samples. See asm/perf_regs.h for details.
 	 */
 	__u64			user_regs;
+	__u32			ustack_dump_size;
 };
 
 /*
@@ -1291,8 +1292,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 +61 -0

73 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
index 6ddaba4..b4c2ad3 100644
@@ -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.7.6
author
#15 lore raw

PATCH 5/16 perf: Add attribute to filter out user callchains

In reply to Jiri Olsa

Add 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
index e40bac5..b121c05 100644
@@ -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
index 6581a04..884e997 100644
@@ -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
index 5bae705..35c28f5 100644
@@ -4114,8 +4114,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
index 1ae5270..b3ade19 100644
@@ -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.7.6
author
#16 lore raw

PATCH 6/16 perf, tool: Factor DSO symtab types to generic binary types

In reply to Jiri Olsa

Adding 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
index 8ef59f8..10184f6 100644
@@ -125,7 +125,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
index 1e7fd52..7ceef29 100644
@@ -307,7 +307,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
index ac49ef2..13c3618 100644
@@ -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.7.6
author
#17 lore raw

PATCH 7/16 perf, tool: Add interface to read DSO image data

In reply to Jiri Olsa

Adding 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.

---
 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
index 09b440c..1e42034 100644
@@ -64,6 +64,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 (!dso)
@@ -335,6 +343,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;
@@ -2823,3 +2832,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
index 13c3618..b62321f 100644
@@ -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.7.6
author
#18 lore raw

PATCH 8/16 perf, tool: Add '.note' check into search for NOTE section

In reply to Jiri Olsa

Adding '.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
index 1e42034..9c0fa32 100644
@@ -1474,14 +1474,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.7.6
author
#19 lore raw

PATCH 9/16 perf, tool: Back [vdso] DSO with real data

In reply to Jiri Olsa

Storing 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 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 API for vdso object:
vdso__file
- vdso temp file path

vdso__get_file
- 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/util/map.c     |   23 ++++++++++-
 tools/perf/util/session.c |    2 +
 tools/perf/util/vdso.c    |   90 +++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/vdso.h    |    8 ++++
 5 files changed, 123 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/vdso.c
 create mode 100644 tools/perf/util/vdso.h
tools/perf/Makefile +2 -0
index e98e14c..d82af48 100644
@@ -300,6 +300,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
@@ -361,6 +362,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/util/map.c +21 -2
index 35ae568..1649ea0 100644
@@ -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]");
 }
 
@@ -50,9 +55,10 @@ struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
 	if (self != NULL) {
 		char newfilename[PATH_MAX];
 		struct dso *dso;
-		int anon, no_dso;
+		int anon, no_dso, vdso;
 
 		anon = is_anon_memory(filename);
+		vdso = is_vdso_memory(filename);
 		no_dso = is_no_dso_memory(filename);
 
 		if (anon) {
@@ -60,10 +66,23 @@ struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
 			filename = newfilename;
 		}
 
+		if (vdso) {
+			filename = (char *) vdso__file;
+			pgoff = 0;
+		}
+
 		dso = __dsos__findnew(dsos__list, filename);
 		if (dso == NULL)
 			goto out_delete;
 
+		if (vdso && !dso->has_build_id) {
+			char *file_vdso = vdso__get_file();
+			if (file_vdso)
+				dso__set_long_name(dso, file_vdso);
+			else
+				no_dso = 1;
+		}
+
 		map__init(self, type, start, start + len, pgoff, dso);
 
 		if (anon || no_dso) {
tools/perf/util/session.c +2 -0
index 1efd3be..ae30ca5 100644
@@ -14,6 +14,7 @@
 #include "sort.h"
 #include "util.h"
 #include "cpumap.h"
+#include "vdso.h"
 
 static int perf_session__open(struct perf_session *self, bool force)
 {
@@ -209,6 +210,7 @@ void perf_session__delete(struct perf_session *self)
 	machine__exit(&self->host_machine);
 	close(self->fd);
 	free(self);
+	vdso__exit();
 }
 
 void machine__remove_thread(struct machine *self, struct thread *th)
tools/perf/util/vdso.c new file +90 -0

90 lines not on this page · raw message

tools/perf/util/vdso.h new file +8 -0
new file mode 100644
index 0000000..908b041
@@ -0,0 +1,8 @@
+#ifndef __VDSO__
+#define __VDSO__
+
+extern const char vdso__file[];
+char *vdso__get_file(void);
+void  vdso__exit(void);
+
+#endif /* __VDSO__ */
Signature · 1 line
1.7.7.6
author
#20 lore raw

PATCH 10/16 perf, tool: Add interface to arch registers sets

In reply to Jiri Olsa

Adding 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
index d82af48..4ce268c 100644
@@ -50,13 +50,15 @@ 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
 
 # Additional ARCH settings for x86
 ifeq ($(ARCH),i386)
-        ARCH := x86
+	ARCH := x86
+	NO_PERF_REGS_DEFS := 0
 endif
 ifeq ($(ARCH),x86_64)
 	ARCH := x86
@@ -69,6 +71,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
@@ -301,6 +304,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
@@ -638,6 +642,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
new file mode 100644
index 0000000..5e2a945
@@ -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.7.6
author
#21 lore raw

PATCH 11/16 perf, tool: Add libunwind dependency for dwarf cfi unwinding

In reply to Jiri Olsa

Adding 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
index 4ce268c..01d67ec 100644
@@ -59,6 +59,7 @@ AR = $(CROSS_COMPILE)ar
 ifeq ($(ARCH),i386)
 	ARCH := x86
 	NO_PERF_REGS_DEFS := 0
+	LIBUNWIND_LIBS = -lunwind -lunwind-x86
 endif
 ifeq ($(ARCH),x86_64)
 	ARCH := x86
@@ -72,6 +73,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
@@ -86,7 +88,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)
@@ -437,6 +439,21 @@ ifneq ($(call try-cc,$(SOURCE_DWARF),$(FLAGS_DWARF)),y)
 endif # Dwarf support
 endif # NO_DWARF
 
+ifndef NO_LIBUNWIND
+# for linking 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),)
@@ -468,6 +485,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
index d9084e0..51cd201 100644
@@ -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.7.6
author
#22 lore raw

PATCH 12/16 perf, tool: Support user regs and stack in sample parsing

In reply to Jiri Olsa

Adding 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 |    2 ++
 tools/perf/util/session.h |    7 ++++++-
 8 files changed, 69 insertions(+), 4 deletions(-)
tools/perf/builtin-test.c +2 -1
index 1c5b980..a434aaa 100644
@@ -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
index 1b19728..31d3e8c 100644
@@ -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
index 1986d80..11cc50e 100644
@@ -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
index 21f1c9e..a90cf24 100644
@@ -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
index 8c13dbc..7ee47c1 100644
@@ -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;
 
 	/*
@@ -594,6 +597,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
index e03b58a..257efcd 100644
@@ -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 +2 -0
index ae30ca5..a2e1c1b 100644
@@ -87,6 +87,8 @@ 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
index 7a5434c..182c0e5 100644
@@ -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.7.6
author
#23 lore raw

PATCH 13/16 perf, tool: Support for dwarf cfi unwinding on post processing

In reply to Jiri Olsa

This 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                          |  100 +++-
 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                           |  565 ++++++++++++++++++++
 tools/perf/util/unwind.h                           |   34 ++
 16 files changed, 872 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
index 01d67ec..3614b1a 100644
@@ -307,6 +307,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
@@ -491,6 +492,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
index 744e629..815841c 100644
@@ -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
index cec2b8c..3753820 100644
@@ -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;
 		}
@@ -389,17 +391,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
index 10184f6..1a4d9b5 100644
@@ -774,8 +774,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
index 547628e..2dc8671 100644
@@ -10,5 +10,6 @@
 #endif
 
 #define __used		__attribute__((__unused__))
+#define __packed	__attribute__((__packed__))
 
 #endif
tools/perf/util/map.h +5 -2
index 81371ba..a27a0a1 100644
@@ -156,9 +156,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
index e30749e..ae07098 100644
@@ -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
index c2623c6..c593ba9 100644
@@ -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 +87 -13

161 lines not on this page · raw message

tools/perf/util/session.h +2 -1
index 182c0e5..090563a 100644
@@ -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
index 18ae6c1..496c8d2 100644
@@ -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
index 58ae14c..15b3133 100644
@@ -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 +565 -0

565 lines not on this page · raw message

tools/perf/util/unwind.h new file +34 -0
new file mode 100644
index 0000000..919bd6a
@@ -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.7.6
author
#24 lore raw

PATCH 14/16 perf, tool: Support for dwarf mode callchain on perf record

In reply to Jiri Olsa

This 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 available for record command. The 'fp' stays as default option in any case.

Examples: (perf compiled with libunwind)

perf record -g dwarf ls
- provides 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 -- ls
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
index 10b1f1f..ba01545 100644
@@ -31,6 +31,15 @@
 #include <sched.h>
 #include <sys/mman.h>
 
+#define CALLCHAIN_HELP "do call-graph (stack chain/backtrace) recording: "
+
+#ifdef NO_LIBUNWIND_SUPPORT
+static char callchain_help[] = CALLCHAIN_HELP "[fp]";
+#else
+static unsigned long default_stack_dump_size = 8192;
+static char callchain_help[] = CALLCHAIN_HELP "[fp] dwarf";
+#endif
+
 enum write_mode_t {
 	WRITE_FORCE,
 	WRITE_APPEND
@@ -725,6 +734,78 @@ 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;
+
+#ifndef NO_LIBUNWIND_SUPPORT
+		/* Dwarf style */
+		} else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
+			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;
+				unsigned long size;
+
+				size = strtoul(tok, &endptr, 0);
+				if (*endptr) {
+					pr_err("callchain: Incorrect stack "
+					       "dump size: %s\n", tok);
+					ret = -1;
+				}
+
+				rec->opts.stack_dump_size = size;
+			}
+
+			pr_debug("callchain: stack dump size %lu\n",
+				 rec->opts.stack_dump_size);
+#endif
+		} else {
+			pr_err("callchain: Unknown -g option "
+			       "value: %s\n", name);
+			break;
+		}
+
+	} while (0);
+
+	if (!ret)
+		pr_debug("callchain: type %d\n", rec->opts.call_graph);
+
+	return ret;
+}
+
 static const char * const record_usage[] = {
 	"perf record [<options>] [<command>]",
 	"perf record [<options>] -- <command> [<options>]",
@@ -793,8 +874,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,
+			     "fp"),
 	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
index 89e3355..1b72438 100644
@@ -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
index 7ee47c1..f2c4ca6 100644
@@ -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.7.6
author
#25 lore raw

PATCH 15/16 perf, tool: Add dso data caching

In reply to Jiri Olsa

Adding dso data caching so we dont need to open/read/close, each time we want dso data.

The DSO data caching affects following functions:
dso__data_read_offset
dso__data_read_addr

Each DSO read tries to find the data (based on offset) inside the cache. If it's not present it fills the cache from file, and returns the data. If it is present, data are returned with no file read.

Each data read is cached by reading cache page sized/alligned amount of DSO data. The cache page size is hardcoded to 4096. The cache is using RB tree with file offset as a sort key.

---
 tools/perf/util/symbol.c |  154 ++++++++++++++++++++++++++++++++++++++++------
 tools/perf/util/symbol.h |   11 +++
 2 files changed, 147 insertions(+), 18 deletions(-)
tools/perf/util/symbol.c +136 -18
index 9c0fa32..b7b0c2a 100644
@@ -29,6 +29,7 @@
 #define NT_GNU_BUILD_ID 3
 #endif
 
+static void data_cache__free(struct rb_root *root);
 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
 static int elf_read_build_id(Elf *elf, void *bf, size_t size);
 static void dsos__add(struct list_head *head, struct dso *dso);
@@ -342,6 +343,7 @@ struct dso *dso__new(const char *name)
 		dso__set_short_name(dso, dso->name);
 		for (i = 0; i < MAP__NR_TYPES; ++i)
 			dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
+		dso->data_cache = RB_ROOT;
 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->loaded = 0;
@@ -376,6 +378,7 @@ void dso__delete(struct dso *dso)
 		free((char *)dso->short_name);
 	if (dso->lname_alloc)
 		free(dso->long_name);
+	data_cache__free(&dso->data_cache);
 	free(dso);
 }
 
@@ -2893,22 +2896,87 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 	return -EINVAL;
 }
 
-static ssize_t dso_cache_read(struct dso *dso __used, u64 offset __used,
-			      u8 *data __used, ssize_t size __used)
+static void
+data_cache__free(struct rb_root *root)
 {
-	return -EINVAL;
+	struct rb_node *next = rb_first(root);
+
+	while (next) {
+		struct dso__data_cache *cache;
+
+		cache = rb_entry(next, struct dso__data_cache, rb_node);
+		next = rb_next(&cache->rb_node);
+		rb_erase(&cache->rb_node, root);
+		free(cache);
+	}
 }
 
-static int dso_cache_add(struct dso *dso __used, u64 offset __used,
-			 u8 *data __used, ssize_t size __used)
+static struct dso__data_cache*
+data_cache__find(struct rb_root *root, u64 offset)
 {
-	return 0;
+	struct rb_node **p = &root->rb_node;
+	struct rb_node *parent = NULL;
+	struct dso__data_cache *cache;
+
+	while (*p != NULL) {
+		u64 end;
+
+		parent = *p;
+		cache = rb_entry(parent, struct dso__data_cache, rb_node);
+		end = cache->offset + DSO__DATA_CACHE_SIZE;
+
+		if (offset < cache->offset)
+			p = &(*p)->rb_left;
+		else if (offset >= end)
+			p = &(*p)->rb_right;
+		else
+			return cache;
+	}
+	return NULL;
+}
+
+static void
+data_cache__insert(struct rb_root *root, struct dso__data_cache *new)
+{
+	struct rb_node **p = &root->rb_node;
+	struct rb_node *parent = NULL;
+	struct dso__data_cache *cache;
+	u64 offset = new->offset;
+
+	while (*p != NULL) {
+		u64 end;
+
+		parent = *p;
+		cache = rb_entry(parent, struct dso__data_cache, rb_node);
+		end = cache->offset + DSO__DATA_CACHE_SIZE;
+
+		if (offset < cache->offset)
+			p = &(*p)->rb_left;
+		else if (offset >= end)
+			p = &(*p)->rb_right;
+	}
+
+	rb_link_node(&new->rb_node, parent, p);
+	rb_insert_color(&new->rb_node, root);
+}
+
+static ssize_t
+data_cache__data(struct dso__data_cache *cache, u64 offset,
+		 u8 *data, u64 size)
+{
+	u64 cache_offset = offset - cache->offset;
+	u64 cache_size   = min(cache->size - cache_offset, size);
+
+	memcpy(data, cache->data + cache_offset, cache_size);
+	return cache_size;
 }
 
-static ssize_t read_dso_data(struct dso *dso, struct machine *machine,
-		     u64 offset, u8 *data, ssize_t size)
+static ssize_t
+data_cache__read(struct dso *dso, struct machine *machine,
+		 u64 offset, u8 *data, ssize_t size)
 {
-	ssize_t rsize = -1;
+	struct dso__data_cache *cache;
+	ssize_t ret;
 	int fd;
 
 	fd = dso__data_fd(dso, machine);
@@ -2916,28 +2984,78 @@ static ssize_t read_dso_data(struct dso *dso, struct machine *machine,
 		return -1;
 
 	do {
-		if (-1 == lseek(fd, offset, SEEK_SET))
+		u64 cache_offset;
+
+		ret = -ENOMEM;
+
+		cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
+		if (!cache)
 			break;
 
-		rsize = read(fd, data, size);
-		if (-1 == rsize)
+		cache_offset = offset & DSO__DATA_CACHE_MASK;
+		ret = -EINVAL;
+
+		if (-1 == lseek(fd, cache_offset, SEEK_SET))
 			break;
 
-		if (dso_cache_add(dso, offset, data, size))
-			pr_err("Failed to add data int dso cache.");
+		ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
+		if (ret <= 0)
+			break;
+
+		cache->offset = cache_offset;
+		cache->size   = ret;
+		data_cache__insert(&dso->data_cache, cache);
+
+		ret = data_cache__data(cache, offset, data, size);
 
 	} while (0);
 
+	if (ret <= 0)
+		free(cache);
+
 	close(fd);
-	return rsize;
+	return ret;
+}
+
+static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
+			      u64 offset, u8 *data, ssize_t size)
+{
+	struct dso__data_cache *cache;
+
+	cache = data_cache__find(&dso->data_cache, offset);
+	if (cache)
+		return data_cache__data(cache, offset, data, size);
+	else
+		return data_cache__read(dso, machine, offset, data, size);
 }
 
 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 r = 0;
+	u8 *p = data;
+
+	do {
+		ssize_t ret;
+
+		ret = dso_cache_read(dso, machine, offset, p, size);
+		if (ret < 0)
+			return ret;
+
+		/* Reached EOF, return what we have. */
+		if (!ret)
+			break;
+
+		BUG_ON(ret > size);
+
+		r      += ret;
+		p      += ret;
+		offset += ret;
+		size   -= ret;
+
+	} while (size);
+
+	return r;
 }
 
 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
tools/perf/util/symbol.h +11 -0
index b62321f..e5744c0 100644
@@ -169,10 +169,21 @@ enum dso_kernel_type {
 	DSO_TYPE_GUEST_KERNEL
 };
 
+#define DSO__DATA_CACHE_SIZE 4096
+#define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
+
+struct dso__data_cache {
+	struct rb_node	rb_node;
+	u64 offset;
+	u64 size;
+	char data[0];
+};
+
 struct dso {
 	struct list_head node;
 	struct rb_root	 symbols[MAP__NR_TYPES];
 	struct rb_root	 symbol_names[MAP__NR_TYPES];
+	struct rb_root	 data_cache;
 	enum dso_kernel_type	kernel;
 	enum dso_binary_type	symtab_type;
 	enum dso_binary_type	data_type;
Signature · 1 line
1.7.7.6
author
#26 lore raw

PATCH 16/16 perf, tool: Add dso data caching tests

In reply to Jiri Olsa

Adding automated test for DSO data reading. Testing raw/cached reads from different file/cache locations.

---
 tools/perf/Makefile        |    1 +
 tools/perf/builtin-test.c  |    4 +
 tools/perf/util/dso-test.c |  154 ++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/symbol.h   |    1 +
 4 files changed, 160 insertions(+), 0 deletions(-)
 create mode 100644 tools/perf/util/dso-test.c
tools/perf/Makefile +1 -0
index 3614b1a..dc56c8e 100644
@@ -342,6 +342,7 @@ LIB_OBJS += $(OUTPUT)util/usage.o
 LIB_OBJS += $(OUTPUT)util/wrapper.o
 LIB_OBJS += $(OUTPUT)util/sigchain.o
 LIB_OBJS += $(OUTPUT)util/symbol.o
+LIB_OBJS += $(OUTPUT)util/dso-test.o
 LIB_OBJS += $(OUTPUT)util/color.o
 LIB_OBJS += $(OUTPUT)util/pager.o
 LIB_OBJS += $(OUTPUT)util/header.o
tools/perf/builtin-test.c +4 -0
index a434aaa..83ff5ba 100644
@@ -1662,6 +1662,10 @@ static struct test {
 		.func = test__perf_pmu,
 	},
 	{
+		.desc = "Test dso data interface",
+		.func = dso__test_data,
+	},
+	{
 		.func = NULL,
 	},
 };
tools/perf/util/dso-test.c new file +154 -0

154 lines not on this page · raw message

tools/perf/util/symbol.h +1 -0
index e5744c0..897b35e 100644
@@ -288,4 +288,5 @@ ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
 			    struct machine *machine, u64 addr,
 			    u8 *data, ssize_t size);
+int dso__test_data(void);
 #endif /* __PERF_SYMBOL */
Signature · 1 line
1.7.7.6
#27 lore raw
In reply to Jiri Olsa
Quoted 19 lines · Jiri Olsa

hi,
sending another RFC version. There are some fixies and some yet unresolved issues outlined below.

thanks for comments,
jirka

v2 changes:
02/16 - fixed register enums
12/16 - fixed the perf_evlist__* names
14/16 - 'fp' stays default even if compiled with dwarf unwind 15/16 - added cache to the DSO data interface, it makes the
dwarf unwind real fast now

v2 not solved yet:
1) generic user regs capturing
discussed in here:
http://marc.info/?l=linux-kernel&m=133304076114629&w=2

Quoted 4 lines

Looks like we could have a generic way of getting registers on samples and support more than only user level registers. But looks like it wasn't completely decided what register levels are worth to store..

Right but I think this is outside the scope of this patchset. Especially without a proper usecase in tools, we can only do mistakes by implementing early support for other kind of regs dump in samples.

Quoted 9 lines

It looks to me that each level could add its own registers mask and it'd be used/saved if the mask is non zero. The same way the user level regs are dealt with now ;).

Or we could add something that Stephane came up with:
attr->sample_type |= PERF_SAMPLE_REGS attr->sample_regs = EAX | EBX | EDI | ESI |..... attr->sample_reg_mode = { INTR, PRECISE, USER }

If we do this we won't be able to record user regs and, say, precise regs at the same time. These should be different sample types that can be set at the same time: PERF_SAMPLE_UREGS | PERF_SAMPLE_PRECISE_REGS | ...

And then have attr->sample_uregs, attr->sample_pregs, ...

This is scary because I fear we'll need to increase the size of attr and the ABI is then going to become incompatible. We'll probably need a way to extend properly.

Anyway, no need to implement support for regs samples (other than user) in this patchset. But we indeed need to make plans such that the ABI is ready to host that later.

Quoted 9 lines

But I guess we need to decide what levels make sense to store first. Also if there's like 2 or 3 only, it might be better to use separate masks as pointed out above.

2) compat task handling
we dont handle compat tasks unwinding currently, we probably want to make it part of the 1) solution or add something like:
__u64 user_regs_compat;

Yep. One question I have for those who know well architectures support in general is: do we have some archs that support more than just one kind of compat/emulated mode?

If so I think we can't rely on that native/compat dichotomy but rather we need to treat all these modes as different architectures.

In this case, attr->sample_regs would only make sense with some arch ID.

Anybody, more clues on this?

Quoted 4 lines

Handling the compat task would also need some other changes in the unwind code and I'm not completely sure how libunwind deals with that, need to check.

Indeed.

Quoted 2 lines

How much do we want this? ;)

I think we do. But there is no emergency. We can do it incrementally once we have native support. But then again I think we need to ensure we got the things right to make the ABI able to host that in the future.

Quoted 6 lines

3) registers version names
this one is now probably connected to 1) and 2) ;)
I kept the register version, since I think it might be usefull for dealing with compat tasks - to recognize what type of registers were stored

Right. Hm, I need to look at this deeper.

Thanks!