From f53d6ca749e82f331c2ffb61f6904c766bb96b9a Mon Sep 17 00:00:00 2001
From: Avi Kivity <avi@cloudius-systems.com>
Date: Sun, 29 Sep 2013 09:46:42 +0300
Subject: [PATCH] trace: fix --trace-backtraces uninitialized data in backtrace

Also fix other review comments related to 1f161695d86d.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
---
 README               | 3 +++
 core/trace.cc        | 6 +++++-
 include/osv/trace.hh | 2 +-
 scripts/loader.py    | 2 +-
 4 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/README b/README
index 011c8036f..25121c9c3 100644
--- a/README
+++ b/README
@@ -147,6 +147,9 @@ reboot after setting the breakpoint:
  you can use multiple --trace= switches, or a single one with commas.  Shell-style wildcards
  allow enabling multiple tracepoints (as in the example). 
  
+ If you add the --trace-backtrace switch, every tracepoint hit will also record
+ a stack backtrace.
+
  To trace all function entries/returns in the program, build with conf-tracing=1 (clean build
  needed), and enable "function*" tracepoints, with --trace=.
  
diff --git a/core/trace.cc b/core/trace.cc
index 6bd3196ce..036b4f950 100644
--- a/core/trace.cc
+++ b/core/trace.cc
@@ -16,6 +16,8 @@
 #include "prio.hh"
 #include <osv/execinfo.hh>
 
+using namespace std;
+
 tracepoint<1, void*, void*> trace_function_entry("function entry", "fn %p caller %p");
 tracepoint<2, void*, void*> trace_function_exit("function exit", "fn %p caller %p");
 
@@ -223,7 +225,9 @@ void tracepoint_base::log_backtraces()
 void tracepoint_base::do_log_backtrace(trace_record* tr, u8*& buffer)
 {
     tr->backtrace = true;
-    backtrace_safe(reinterpret_cast<void**>(buffer), backtrace_len);
+    auto bt = reinterpret_cast<void**>(buffer);
+    auto done = backtrace_safe(bt, backtrace_len);
+    fill(bt + done, bt + backtrace_len, nullptr);
     buffer += backtrace_len * sizeof(void*);
 }
 
diff --git a/include/osv/trace.hh b/include/osv/trace.hh
index 166539eef..a0d5233b6 100644
--- a/include/osv/trace.hh
+++ b/include/osv/trace.hh
@@ -32,7 +32,7 @@ struct trace_record {
     sched::thread* thread;
     u64 time;
     unsigned cpu;
-    bool backtrace : 1;  // 10-elemenet backtrace precedes parameters
+    bool backtrace : 1;  // 10-element backtrace precedes parameters
     union {
         u8 buffer[0];
         long align[0];
diff --git a/scripts/loader.py b/scripts/loader.py
index f5ca1e0e9..eedc4259f 100644
--- a/scripts/loader.py
+++ b/scripts/loader.py
@@ -493,7 +493,7 @@ def dump_trace(out_func):
     trace_log = trace_log[pivot:] + trace_log[:pivot]
     last += max_trace - pivot
     indents = defaultdict(int)
-    backtrace_len = 10
+    backtrace_len = ulong(gdb.parse_and_eval('tracepoint_base::backtrace_len'))
     bt_format = '   [' + str.join(' ', ['0x%x'] * backtrace_len) + ']'
     def lookup_tp(name):
         tp_base = gdb.lookup_type('tracepoint_base')
-- 
GitLab