[PATCH 6/9] perf/jit: add unwinding support

2016-10-13 Thread Stephane Eranian
From: Stefano Sanfilippo 

This record is intended to provide unwinding information in the
eh_frame format. This is required to unwind JITed code which
does not maintain the frame pointer register during function calls.

The eh_frame unwinding information can be emitted by V8 / Chromium
when the --perf_prof_unwinding_info is passed.

A record of type jr_code_unwinding_info comes before the jr_code_load
it referred to and contains both the .eh_frame and .eh_frame_hdr.

The fields in the header have the following meaning:

  * unwinding_size: size of the eh_frame and eh_frame_hdr, necessary
for distinguishing the content from the padding.

  * eh_frame_hdr_size: as the name says.

  * mapped_size: size of the payload that was in memory at runtime.
typically unwinding_size if the .eh_frame_hdr and .eh_frame were
mapped, or 0 if they weren't. It should always be the former case,
since the .eh_frame is guaranteed to be mapped in memory. However,
certain JITs might want to inject an .eh_frame_hdr with an empty LUT
to trigger fp-based unwinding fallback in libunwind. The only part
of the .eh_frame_hdr that libunwind reads from remote memory is the
LUT, and since there is none, mapping the unwinding info in memory
is not necessary, and 0 in this field signifies that it wasn't.
This practical hack allows to save bytes in code memory for those
JIT compilers that might or might not maintain a valid frame pointer.

The payload that follows is assumed to contain first the .eh_frame and
then the .eh_header_hdr, with no padding between the two.

Signed-off-by: Stefano Sanfilippo 
Signed-off-by: Ross McIlroy 
Reviewed-by: Stephane Eranian 
---
 tools/perf/util/jitdump.c | 57 ---
 tools/perf/util/jitdump.h | 12 ++
 2 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
index 5db2feb90060..fdddeca26545 100644
--- a/tools/perf/util/jitdump.c
+++ b/tools/perf/util/jitdump.c
@@ -37,6 +37,10 @@ struct jit_buf_desc {
bool needs_bswap; /* handles cross-endianess */
bool use_arch_timestamp;
void *debug_data;
+   void *unwinding_data;
+   uint64_t unwinding_size;
+   uint64_t unwinding_mapped_size;
+   uint64_t eh_frame_hdr_size;
size_t   nr_debug_entries;
uint32_t code_load_count;
u64  bytes_written;
@@ -295,6 +299,13 @@ jit_get_next_entry(struct jit_buf_desc *jd)
}
}
break;
+   case JIT_CODE_UNWINDING_INFO:
+   if (jd->needs_bswap) {
+   jr->unwinding.unwinding_size = 
bswap_64(jr->unwinding.unwinding_size);
+   jr->unwinding.eh_frame_hdr_size = 
bswap_64(jr->unwinding.eh_frame_hdr_size);
+   jr->unwinding.mapped_size = 
bswap_64(jr->unwinding.mapped_size);
+   }
+   break;
case JIT_CODE_CLOSE:
break;
case JIT_CODE_LOAD:
@@ -370,7 +381,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
u16 idr_size;
const char *sym;
uint32_t count;
-   int ret, csize;
+   int ret, csize, usize;
pid_t pid, tid;
struct {
u32 pid, tid;
@@ -380,6 +391,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
pid   = jr->load.pid;
tid   = jr->load.tid;
csize = jr->load.code_size;
+   usize = jd->unwinding_mapped_size;
addr  = jr->load.code_addr;
sym   = (void *)((unsigned long)jr + sizeof(jr->load));
code  = (unsigned long)jr + jr->load.p.total_size - csize;
@@ -408,6 +420,14 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
jd->nr_debug_entries = 0;
}
 
+   if (jd->unwinding_data && jd->eh_frame_hdr_size) {
+   free(jd->unwinding_data);
+   jd->unwinding_data = NULL;
+   jd->eh_frame_hdr_size = 0;
+   jd->unwinding_mapped_size = 0;
+   jd->unwinding_size = 0;
+   }
+
if (ret) {
free(event);
return -1;
@@ -422,7 +442,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
 
event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
event->mmap2.start = addr;
-   event->mmap2.len   = csize;
+   event->mmap2.len   = usize ? ALIGN_8(csize) + usize : csize;
event->mmap2.pid   = pid;
event->mmap2.tid   = tid;
event->mmap2.ino   = st.st_ino;
@@ -473,6 +493,7 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, 
union jr_entry *jr)
char *filename;
size_t 

[PATCH 6/9] perf/jit: add unwinding support

2016-10-13 Thread Stephane Eranian
From: Stefano Sanfilippo 

This record is intended to provide unwinding information in the
eh_frame format. This is required to unwind JITed code which
does not maintain the frame pointer register during function calls.

The eh_frame unwinding information can be emitted by V8 / Chromium
when the --perf_prof_unwinding_info is passed.

A record of type jr_code_unwinding_info comes before the jr_code_load
it referred to and contains both the .eh_frame and .eh_frame_hdr.

The fields in the header have the following meaning:

  * unwinding_size: size of the eh_frame and eh_frame_hdr, necessary
for distinguishing the content from the padding.

  * eh_frame_hdr_size: as the name says.

  * mapped_size: size of the payload that was in memory at runtime.
typically unwinding_size if the .eh_frame_hdr and .eh_frame were
mapped, or 0 if they weren't. It should always be the former case,
since the .eh_frame is guaranteed to be mapped in memory. However,
certain JITs might want to inject an .eh_frame_hdr with an empty LUT
to trigger fp-based unwinding fallback in libunwind. The only part
of the .eh_frame_hdr that libunwind reads from remote memory is the
LUT, and since there is none, mapping the unwinding info in memory
is not necessary, and 0 in this field signifies that it wasn't.
This practical hack allows to save bytes in code memory for those
JIT compilers that might or might not maintain a valid frame pointer.

The payload that follows is assumed to contain first the .eh_frame and
then the .eh_header_hdr, with no padding between the two.

Signed-off-by: Stefano Sanfilippo 
Signed-off-by: Ross McIlroy 
Reviewed-by: Stephane Eranian 
---
 tools/perf/util/jitdump.c | 57 ---
 tools/perf/util/jitdump.h | 12 ++
 2 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
index 5db2feb90060..fdddeca26545 100644
--- a/tools/perf/util/jitdump.c
+++ b/tools/perf/util/jitdump.c
@@ -37,6 +37,10 @@ struct jit_buf_desc {
bool needs_bswap; /* handles cross-endianess */
bool use_arch_timestamp;
void *debug_data;
+   void *unwinding_data;
+   uint64_t unwinding_size;
+   uint64_t unwinding_mapped_size;
+   uint64_t eh_frame_hdr_size;
size_t   nr_debug_entries;
uint32_t code_load_count;
u64  bytes_written;
@@ -295,6 +299,13 @@ jit_get_next_entry(struct jit_buf_desc *jd)
}
}
break;
+   case JIT_CODE_UNWINDING_INFO:
+   if (jd->needs_bswap) {
+   jr->unwinding.unwinding_size = 
bswap_64(jr->unwinding.unwinding_size);
+   jr->unwinding.eh_frame_hdr_size = 
bswap_64(jr->unwinding.eh_frame_hdr_size);
+   jr->unwinding.mapped_size = 
bswap_64(jr->unwinding.mapped_size);
+   }
+   break;
case JIT_CODE_CLOSE:
break;
case JIT_CODE_LOAD:
@@ -370,7 +381,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
u16 idr_size;
const char *sym;
uint32_t count;
-   int ret, csize;
+   int ret, csize, usize;
pid_t pid, tid;
struct {
u32 pid, tid;
@@ -380,6 +391,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
pid   = jr->load.pid;
tid   = jr->load.tid;
csize = jr->load.code_size;
+   usize = jd->unwinding_mapped_size;
addr  = jr->load.code_addr;
sym   = (void *)((unsigned long)jr + sizeof(jr->load));
code  = (unsigned long)jr + jr->load.p.total_size - csize;
@@ -408,6 +420,14 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
jd->nr_debug_entries = 0;
}
 
+   if (jd->unwinding_data && jd->eh_frame_hdr_size) {
+   free(jd->unwinding_data);
+   jd->unwinding_data = NULL;
+   jd->eh_frame_hdr_size = 0;
+   jd->unwinding_mapped_size = 0;
+   jd->unwinding_size = 0;
+   }
+
if (ret) {
free(event);
return -1;
@@ -422,7 +442,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, 
union jr_entry *jr)
 
event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
event->mmap2.start = addr;
-   event->mmap2.len   = csize;
+   event->mmap2.len   = usize ? ALIGN_8(csize) + usize : csize;
event->mmap2.pid   = pid;
event->mmap2.tid   = tid;
event->mmap2.ino   = st.st_ino;
@@ -473,6 +493,7 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, 
union jr_entry *jr)
char *filename;
size_t size;
struct stat st;
+   int usize;
u16 idr_size;
int ret;