From: Markus Metzger <markus.t.metzger@intel.com>
To: jan.kratochvil@redhat.com
Cc: gdb-patches@sourceware.org, Eli Zaretskii <eliz@gnu.org>
Subject: [PATCH 15/15] record-btrace: extend unwinder
Date: Thu, 02 May 2013 12:04:00 -0000 [thread overview]
Message-ID: <1367496216-21217-16-git-send-email-markus.t.metzger@intel.com> (raw)
In-Reply-To: <1367496216-21217-1-git-send-email-markus.t.metzger@intel.com>
Extend the always failing unwinder to provide the PC based on the call structure
detected in the branch trace.
There are several open points:
An assertion in get_frame_id at frame.c:340 requires that a frame provides a
stack address. The record-btrace unwinder can't provide this since the trace
does not contain data. I incorrectly set stack_addr_p to 1 to avoid the
assertion.
When evaluating arguments for printing the stack back trace, there's an ugly
error displayed: "error reading variable: can't compute CFA for this frame".
The error is correct, we can't compute the CFA since we don't have the stack at
that time, but it is rather annoying at this place and makes the back trace
difficult to read.
Now that we set the PC to a different value and provide a fake unwinder, we have
the potential to affect almost every other command. How can this be tested
sufficiently? I added a few tests for the intended functionality, but nothing
so far to ensure that it does not break some other command when used in this
context.
CC: Eli Zaretskii <eliz@gnu.org>
2013-04-24 Markus Metzger <markus.t.metzger@intel.com>
* frame.h (enum frame_type) <BTRACE_FRAME>: New.
* record-btrace.c: Include hashtab.h.
(btrace_get_bfun_name): New.
(btrace_call_history): Call btrace_get_bfun_name.
(enum btrace_frame_flag): New.
(struct btrace_frame_cache): New.
(bfcache): New.
(bfcache_hash, bfcache_eq, bfcache_new): New.
(btrace_get_frame_function): New.
(record_btrace_frame_unwind_stop_reason): Allow unwinding.
(record_btrace_frame_this_id): Compute own id.
(record_btrace_frame_prev_register): Provide PC, throw_error
for all other registers.
(record_btrace_frame_sniffer): Detect btrace frames.
(record_btrace_frame_dealloc_cache): New.
(record_btrace_frame_unwind): Add new functions.
(_initialize_record_btrace): Allocate cache.
* btrace.c (btrace_clear): Call reinit_frame_cache.
* NEWS: Announce it.
testsuite/
* gdb.btrace/record_goto.exp: Add backtrace test.
* gdb.btrace/tailcall.exp: Add backtrace test.
---
gdb/NEWS | 2 +
gdb/btrace.c | 4 +
gdb/frame.h | 4 +-
gdb/record-btrace.c | 274 +++++++++++++++++++++++++++--
gdb/testsuite/gdb.btrace/record_goto.exp | 13 ++
gdb/testsuite/gdb.btrace/tailcall.exp | 17 ++
6 files changed, 294 insertions(+), 20 deletions(-)
diff --git a/gdb/NEWS b/gdb/NEWS
index ba17f7d..2a5287c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -4,6 +4,8 @@
*** Changes since GDB 7.6
* The btrace record target supports the 'record goto' command.
+ For locations inside the execution trace, the back trace is computed
+ based on the information stored in the execution trace.
* The command 'record function-call-history' supports a new modifier '/c' to
indent the function names based on their call stack depth.
diff --git a/gdb/btrace.c b/gdb/btrace.c
index bb2e051..a2f8785 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -734,6 +734,10 @@ btrace_clear (struct thread_info *tp)
DEBUG ("clear thread %d (%s)", tp->num, target_pid_to_str (tp->ptid));
+ /* Make sure btrace frames that may hold a pointer into the branch
+ trace data are destroyed. */
+ reinit_frame_cache ();
+
btinfo = &tp->btrace;
it = btinfo->begin;
diff --git a/gdb/frame.h b/gdb/frame.h
index 31b9cb7..db4cc52 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -216,7 +216,9 @@ enum frame_type
ARCH_FRAME,
/* Sentinel or registers frame. This frame obtains register values
direct from the inferior's registers. */
- SENTINEL_FRAME
+ SENTINEL_FRAME,
+ /* A branch tracing frame. */
+ BTRACE_FRAME
};
/* For every stopped thread, GDB tracks two frames: current and
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 388c2d7..8c95e76 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -34,6 +34,7 @@
#include "filenames.h"
#include "regcache.h"
#include "frame-unwind.h"
+#include "hashtab.h"
/* The target_ops of record-btrace. */
static struct target_ops record_btrace_ops;
@@ -500,6 +501,25 @@ btrace_call_history_src_line (struct ui_out *uiout,
ui_out_field_int (uiout, "max line", end);
}
+/* Get the name of a branch trace function. */
+
+static const char *
+btrace_get_bfun_name (const struct btrace_function *bfun)
+{
+ struct minimal_symbol *msym;
+ struct symbol *sym;
+
+ msym = bfun->msym;
+ sym = bfun->sym;
+
+ if (sym != NULL)
+ return SYMBOL_PRINT_NAME (sym);
+ else if (msym != NULL)
+ return SYMBOL_PRINT_NAME (msym);
+ else
+ return "<unknown>";
+}
+
/* Disassemble a section of the recorded function trace. */
static void
@@ -515,12 +535,6 @@ btrace_call_history (struct ui_out *uiout,
for (bfun = begin; bfun != end; bfun = bfun->flow.next)
{
- struct minimal_symbol *msym;
- struct symbol *sym;
-
- msym = bfun->msym;
- sym = bfun->sym;
-
/* Print the function index. */
ui_out_field_uint (uiout, "index", bfun->number);
ui_out_text (uiout, "\t");
@@ -533,12 +547,7 @@ btrace_call_history (struct ui_out *uiout,
ui_out_text (uiout, " ");
}
- if (sym != NULL)
- ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (sym));
- else if (msym != NULL)
- ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (msym));
- else
- ui_out_field_string (uiout, "function", "<unknown>");
+ ui_out_field_string (uiout, "function", btrace_get_bfun_name (bfun));
if ((flags & record_print_insn_range) != 0)
{
@@ -942,13 +951,114 @@ record_btrace_prepare_to_store (struct target_ops *ops,
}
}
+/* A collection of branch trace frame flags. */
+
+enum btrace_frame_flag
+ {
+ /* The PC when unwinding into this frame points to the first insn. */
+ pc_is_first = 1 << 0,
+
+ /* The PC when unwinding into this frame points to the last insn. */
+ pc_is_last = 1 << 1,
+
+ /* The PC when unwinding into this frame points to the instruction
+ following the last insn. */
+ pc_is_next = 1 << 2
+ };
+
+/* The branch trace frame cache. */
+
+struct btrace_frame_cache
+{
+ /* The thread. */
+ struct thread_info *tp;
+
+ /* The frame info. */
+ struct frame_info *frame;
+
+ /* The branch trace function segment. */
+ struct btrace_function *bfun;
+
+ /* A bit-vector of btrace frame flags. */
+ unsigned int flags;
+};
+
+/* A struct btrace_frame_cache hash table indexed by NEXT. */
+
+static htab_t bfcache;
+
+/* hash_f for htab_create_alloc of bfcache. */
+
+static hashval_t
+bfcache_hash (const void *arg)
+{
+ const struct btrace_frame_cache *cache = arg;
+
+ return htab_hash_pointer (cache->frame);
+}
+
+/* eq_f for htab_create_alloc of bfcache. */
+
+static int
+bfcache_eq (const void *arg1, const void *arg2)
+{
+ const struct btrace_frame_cache *cache1 = arg1;
+ const struct btrace_frame_cache *cache2 = arg2;
+
+ return cache1->frame == cache2->frame;
+}
+
+/* Create a new btrace frame cache. */
+
+static struct btrace_frame_cache *
+bfcache_new (struct frame_info *frame)
+{
+ struct btrace_frame_cache *cache;
+ void **slot;
+
+ cache = FRAME_OBSTACK_ZALLOC (struct btrace_frame_cache);
+ cache->frame = frame;
+
+ slot = htab_find_slot (bfcache, cache, INSERT);
+ gdb_assert (*slot == NULL);
+ *slot = cache;
+
+ return cache;
+}
+
+/* Extract the branch trace function from a branch trace frame. */
+
+static struct btrace_function *
+btrace_get_frame_function (struct frame_info *frame)
+{
+ struct btrace_frame_cache *cache, pattern;
+ struct btrace_function *bfun;
+ void **slot;
+
+ pattern.frame = frame;
+
+ slot = htab_find_slot (bfcache, &pattern, NO_INSERT);
+ if (slot == NULL)
+ return NULL;
+
+ cache = *slot;
+ return cache->bfun;
+}
+
/* Implement stop_reason method for record_btrace_frame_unwind. */
static enum unwind_stop_reason
record_btrace_frame_unwind_stop_reason (struct frame_info *this_frame,
void **this_cache)
{
- return UNWIND_UNAVAILABLE;
+ struct btrace_frame_cache *cache;
+
+ cache = *this_cache;
+
+ if (cache->bfun == NULL)
+ return UNWIND_UNAVAILABLE;
+
+ return UNWIND_NO_REASON;
}
/* Implement this_id method for record_btrace_frame_unwind. */
@@ -957,7 +1067,22 @@ static void
record_btrace_frame_this_id (struct frame_info *this_frame, void **this_cache,
struct frame_id *this_id)
{
- /* Leave there the outer_frame_id value. */
+ struct btrace_frame_cache *cache;
+
+ cache = *this_cache;
+
+ memset (this_id, 0, sizeof (*this_id));
+
+ this_id->code_addr = get_frame_pc (this_frame);
+ this_id->code_addr_p = 1;
+
+ /* GDB requires frame id's to provide a stack address. */
+ this_id->stack_addr_p = 1;
+
+ /* We encode the btrace function pointer into the frame id to get
+ frame ids unique. */
+ this_id->special_addr = (CORE_ADDR) cache->bfun;
+ this_id->special_addr_p = 1;
}
/* Implement prev_register method for record_btrace_frame_unwind. */
@@ -967,8 +1092,46 @@ record_btrace_frame_prev_register (struct frame_info *this_frame,
void **this_cache,
int regnum)
{
- throw_error (NOT_AVAILABLE_ERROR,
- _("Registers are not available in btrace record history"));
+ struct btrace_frame_cache *cache;
+ struct btrace_function *bfun;
+ struct btrace_insn *insn;
+ struct gdbarch *gdbarch;
+ CORE_ADDR pc;
+ int pcreg;
+
+ gdbarch = get_frame_arch (this_frame);
+ pcreg = gdbarch_pc_regnum (gdbarch);
+ if (pcreg < 0 || regnum != pcreg)
+ throw_error (NOT_AVAILABLE_ERROR,
+ _("Registers are not available in btrace record history"));
+
+ cache = *this_cache;
+ bfun = cache->bfun;
+ gdb_assert (bfun != NULL);
+
+ if (VEC_empty (btrace_insn_s, bfun->insn))
+ throw_error (NOT_AVAILABLE_ERROR,
+ _("Registers are not available in btrace record history"));
+
+ if ((cache->flags & pc_is_first) != 0)
+ {
+ insn = VEC_index (btrace_insn_s, bfun->insn, 0);
+ pc = insn->pc;
+ }
+ else
+ {
+ insn = VEC_last (btrace_insn_s, bfun->insn);
+ pc = insn->pc;
+
+ if ((cache->flags & pc_is_next) != 0)
+ pc += gdb_insn_length (gdbarch, pc);
+ }
+
+ DEBUG ("[frame] unwound PC for %s on level %d: %s",
+ btrace_get_bfun_name (bfun), bfun->level,
+ core_addr_to_string_nz (pc));
+
+ return frame_unwind_got_address (this_frame, regnum, pc);
}
/* Implement sniffer method for record_btrace_frame_unwind. */
@@ -981,6 +1144,10 @@ record_btrace_frame_sniffer (const struct frame_unwind *self,
struct thread_info *tp;
struct btrace_thread_info *btinfo;
struct btrace_insn_iterator *replay;
+ struct btrace_frame_cache *cache;
+ struct btrace_function *bfun;
+ struct frame_info *next;
+ unsigned int flags;
/* This doesn't seem right. Yet, I don't see how I could get from a frame
to its thread. */
@@ -988,7 +1155,72 @@ record_btrace_frame_sniffer (const struct frame_unwind *self,
if (tp == NULL)
return 0;
- return btrace_is_replaying (tp);
+ replay = tp->btrace.replay;
+ if (replay == NULL)
+ return 0;
+
+ /* Find the next frame's branch trace function. */
+ flags = 0;
+ next = get_next_frame (this_frame);
+ if (next == NULL)
+ {
+ /* The sentinel frame below corresponds to our replay position. */
+ bfun = replay->function;
+ flags |= pc_is_last;
+ }
+ else
+ {
+ /* This is an outer frame. It must be the predecessor of another
+ branch trace frame. Let's get this frame's branch trace function
+ so we can compute our own. */
+ bfun = btrace_get_frame_function (next);
+
+ if (bfun != NULL)
+ {
+ if ((bfun->flags & bfun_up_links_to_ret) != 0)
+ flags |= pc_is_first;
+ else if ((bfun->flags & bfun_up_links_to_tailcall) != 0)
+ flags |= pc_is_last;
+ else
+ flags |= pc_is_next;
+ }
+ }
+
+ /* If we did not find a branch trace function, this is not our frame. */
+ if (bfun == NULL)
+ return 0;
+
+ /* Go up to the calling function segment. */
+ bfun = bfun->up;
+
+ if (bfun != NULL)
+ DEBUG ("[frame] sniffed frame for %s on level %d",
+ btrace_get_bfun_name (bfun), bfun->level);
+ else
+ DEBUG ("[frame] sniffed top btrace frame");
+
+ /* This is our frame. Initialize the frame cache. */
+ cache = bfcache_new (this_frame);
+ cache->tp = tp;
+ cache->bfun = bfun;
+ cache->flags = flags;
+
+ *this_cache = cache;
+ return 1;
+}
+
+static void
+record_btrace_frame_dealloc_cache (struct frame_info *self, void *this_cache)
+{
+ struct btrace_frame_cache *cache;
+ void **slot;
+
+ cache = this_cache;
+
+ slot = htab_find_slot (bfcache, cache, NO_INSERT);
+ gdb_assert (slot != NULL);
+
+ htab_remove_elt (bfcache, cache);
}
/* btrace recording does not store previous memory content, neither the stack
@@ -999,12 +1231,13 @@ record_btrace_frame_sniffer (const struct frame_unwind *self,
static const struct frame_unwind record_btrace_frame_unwind =
{
- NORMAL_FRAME,
+ BTRACE_FRAME,
record_btrace_frame_unwind_stop_reason,
record_btrace_frame_this_id,
record_btrace_frame_prev_register,
NULL,
- record_btrace_frame_sniffer
+ record_btrace_frame_sniffer,
+ record_btrace_frame_dealloc_cache
};
/* The to_resume method of target record-btrace. */
@@ -1201,4 +1434,7 @@ _initialize_record_btrace (void)
init_record_btrace_ops ();
add_target (&record_btrace_ops);
+
+ bfcache = htab_create_alloc (50, bfcache_hash, bfcache_eq, NULL,
+ xcalloc, xfree);
}
diff --git a/gdb/testsuite/gdb.btrace/record_goto.exp b/gdb/testsuite/gdb.btrace/record_goto.exp
index 008d956..a045d9f 100644
--- a/gdb/testsuite/gdb.btrace/record_goto.exp
+++ b/gdb/testsuite/gdb.btrace/record_goto.exp
@@ -76,6 +76,19 @@ gdb_test "record instruction-history" "
gdb_test "record goto 25" "
.*fun3 \\(\\) at record_goto.c:35.*" "record_goto - goto 25"
+# check the back trace at that location
+gdb_test "backtrace" "
+#0.*fun3.*at record_goto.c:35.*\r
+#1.*fun4.*at record_goto.c:43.*\r
+#2.*main.*at record_goto.c:51.*\r
+Backtrace stopped: not enough registers or memory available to unwind further" "backtrace at 25"
+
+# walk the backtrace
+gdb_test "up" "
+.*fun4.*at record_goto.c:43.*" "up to fun4"
+gdb_test "up" "
+.*main.*at record_goto.c:51.*" "up to main"
+
# the function call history should start at the new location
gdb_test "record function-call-history /ci -" "
7\t fun3\tinst 18,20\r
diff --git a/gdb/testsuite/gdb.btrace/tailcall.exp b/gdb/testsuite/gdb.btrace/tailcall.exp
index 8e47a12..0b1bd61 100644
--- a/gdb/testsuite/gdb.btrace/tailcall.exp
+++ b/gdb/testsuite/gdb.btrace/tailcall.exp
@@ -47,3 +47,20 @@ gdb_test "record function-call-history /c 0" "
0\t foo\r
1\t bar\r
2\tmain" "tailcall - calls indented"
+
+# go into bar
+gdb_test "record goto 2" "
+.*bar \\(\\) at .*x86-tailcall.c:24.*" "go to bar"
+
+# check the backtrace
+gdb_test "backtrace" "
+#0.*bar.*at .*x86-tailcall.c:24.*\r
+#1.*foo.*at .*x86-tailcall.c:29.*\r
+#2.*main.*at .*x86-tailcall.c:37.*\r
+Backtrace stopped: not enough registers or memory available to unwind further" "backtrace in bar"
+
+# walk the backtrace
+gdb_test "up" "
+.*foo \\(\\) at .*x86-tailcall.c:29.*" "up to foo"
+gdb_test "up" "
+.*main \\(\\) at .*x86-tailcall.c:37.*" "up to main"
--
1.7.1
next prev parent reply other threads:[~2013-05-02 12:04 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-02 12:03 [PATCH 00/15] record-btrace: goto support Markus Metzger
2013-05-02 12:03 ` [PATCH 07/15] btrace: add replay position to btrace thread info Markus Metzger
2013-05-02 12:03 ` [PATCH 12/15] record-btrace: provide xfer_partial target method Markus Metzger
2013-05-02 12:03 ` [PATCH 03/15] record-btrace: fix insn range in function call history Markus Metzger
2013-05-02 12:03 ` [PATCH 01/15] gdbarch: add instruction predicate methods Markus Metzger
2013-05-13 15:23 ` Jan Kratochvil
2013-05-02 12:03 ` [PATCH 06/15] record-btrace: make ranges include begin and end Markus Metzger
2013-05-02 15:51 ` Eli Zaretskii
2013-05-02 12:03 ` [PATCH 09/15] record-btrace: supply register target methods Markus Metzger
2013-05-02 12:03 ` [PATCH 08/15] target: add ops parameter to to_prepare_to_store method Markus Metzger
2013-05-02 12:03 ` [PATCH 14/15] record-btrace: add record goto target methods Markus Metzger
2013-05-02 17:11 ` Eli Zaretskii
2013-05-02 12:03 ` [PATCH 10/15] frame, backtrace: allow targets to supply a frame unwinder Markus Metzger
2013-05-02 12:04 ` [PATCH 02/15] btrace: change branch trace data structure Markus Metzger
2013-05-13 15:25 ` Jan Kratochvil
2013-05-14 15:27 ` Metzger, Markus T
2013-05-14 16:11 ` Doug Evans
2013-05-02 12:04 ` Markus Metzger [this message]
2013-05-02 15:52 ` [PATCH 15/15] record-btrace: extend unwinder Eli Zaretskii
2013-05-02 12:04 ` [PATCH 04/15] btrace: increase buffer size Markus Metzger
2013-05-02 12:04 ` [PATCH 13/15] record-btrace: add to_wait and to_resume target methods Markus Metzger
2013-05-02 12:04 ` [PATCH 05/15] record-btrace: optionally indent function call history Markus Metzger
2013-05-02 17:10 ` Eli Zaretskii
2013-05-02 12:04 ` [PATCH 11/15] record-btrace, frame: supply target-specific unwinder Markus Metzger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1367496216-21217-16-git-send-email-markus.t.metzger@intel.com \
--to=markus.t.metzger@intel.com \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
--cc=jan.kratochvil@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox