This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH 1/2] record: add "record instruction-history" command
- From: markus dot t dot metzger at intel dot com
- To: jan dot kratochvil at redhat dot com
- Cc: gdb-patches at sourceware dot org, markus dot t dot metzger at gmail dot com, Markus Metzger <markus dot t dot metzger at intel dot com>
- Date: Fri, 22 Feb 2013 13:17:16 +0100
- Subject: [PATCH 1/2] record: add "record instruction-history" command
- References: <1361535437-22493-1-git-send-email-markus.t.metzger@intel.com>
From: Markus Metzger <markus.t.metzger@intel.com>
Add a command to provide a disassembly of the execution trace log.
The command iterates over the execution trace log similar to the
"list" command.
2013-02-22 Markus Metzger <markus.t.metzger@intel.com>
* target.h (target_ops): Add to_insn_history and
to_insn_history_range fields.
(target_insn_history): New.
(target_insn_history_range): New.
* target.c (target_insn_history): New.
(target_insn_history_range): New.
* record.c: Include cli/cli-utils.h, disasm.h, ctype.h.
(record_insn_history_size): New.
(get_insn_number): New.
(get_insn_history_modifiers): New.
(cmd_record_insn_history): New.
(_initialize_record): Add "set/show record instruction-history-size"
command. Add "record instruction-history" command.
---
gdb/record.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
gdb/target.c | 34 +++++++++++
gdb/target.h | 13 ++++
3 files changed, 226 insertions(+), 0 deletions(-)
diff --git a/gdb/record.c b/gdb/record.c
index abd8216..6ac0c8c 100644
--- a/gdb/record.c
+++ b/gdb/record.c
@@ -24,10 +24,17 @@
#include "observer.h"
#include "inferior.h"
#include "common/common-utils.h"
+#include "cli/cli-utils.h"
+#include "disasm.h"
+
+#include <ctype.h>
/* This is the debug switch for process record. */
unsigned int record_debug = 0;
+/* The number of instructions to print in "record instruction-history". */
+static unsigned int record_insn_history_size = 10;
+
struct cmd_list_element *record_cmdlist = NULL;
struct cmd_list_element *set_record_cmdlist = NULL;
struct cmd_list_element *show_record_cmdlist = NULL;
@@ -289,6 +296,152 @@ cmd_record_goto (char *arg, int from_tty)
}
}
+/* Read an instruction number from an argument string. */
+
+static ULONGEST
+get_insn_number (char **arg)
+{
+ ULONGEST number;
+ const char *begin, *end, *pos;
+
+ begin = *arg;
+ pos = skip_spaces_const (begin);
+
+ if (!isdigit (*pos))
+ error (_("Expected positive number, got: %s."), pos);
+
+ number = strtoulst (pos, &end, 10);
+
+ *arg += (end - begin);
+
+ return number;
+}
+
+/* Read instruction-history modifiers from an argument string. */
+
+static int
+get_insn_history_modifiers (char **arg)
+{
+ int modifiers;
+ char *args;
+
+ modifiers = 0;
+ args = *arg;
+
+ if (args == NULL)
+ return modifiers;
+
+ while (*args == '/')
+ {
+ ++args;
+
+ if (*args == '\0')
+ error (_("Missing modifier."));
+
+ for (; *args; ++args)
+ {
+ if (isspace (*args))
+ break;
+
+ if (*args == '/')
+ continue;
+
+ switch (*args)
+ {
+ case 'm':
+ modifiers |= DISASSEMBLY_SOURCE;
+ modifiers |= DISASSEMBLY_FILENAME;
+ break;
+ case 'r':
+ modifiers |= DISASSEMBLY_RAW_INSN;
+ break;
+ case 'f':
+ modifiers |= DISASSEMBLY_OMIT_FNAME;
+ break;
+ default:
+ error (_("Invalid modifier: %c."), *args);
+ }
+ }
+
+ args = skip_spaces (args);
+ }
+
+ /* Update the argument string. */
+ *arg = args;
+
+ return modifiers;
+}
+
+/* The "record instruction-history" command. */
+
+static void
+cmd_record_insn_history (char *arg, int from_tty)
+{
+ int flags;
+
+ require_record_target ();
+
+ flags = get_insn_history_modifiers (&arg);
+
+ if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
+ target_insn_history ((int) record_insn_history_size, flags);
+ else if (strcmp (arg, "-") == 0)
+ target_insn_history (- (int) record_insn_history_size, flags);
+ else
+ {
+ ULONGEST begin, end, context;
+
+ begin = get_insn_number (&arg);
+
+ if (*arg == ',')
+ {
+ arg = skip_spaces (++arg);
+
+ if (*arg == '+')
+ {
+ arg = skip_spaces (++arg);
+
+ context = get_insn_number (&arg);
+
+ end = begin + context;
+ }
+ else if (*arg == '-')
+ {
+ arg = skip_spaces (++arg);
+
+ context = get_insn_number (&arg);
+
+ /* Truncate the context, if necessary. */
+ context = min (context, begin);
+
+ end = begin;
+ begin -= context;
+ }
+ else
+ end = get_insn_number (&arg);
+ }
+ else
+ {
+ ULONGEST before;
+
+ /* If the execution log does not start at zero, we might not
+ disassemble the entire record_insn_history_size instructions. */
+
+ before = record_insn_history_size / 2;
+ if (begin < before)
+ before = begin;
+
+ begin -= before;
+ end = begin + record_insn_history_size;
+ }
+
+ if (*arg != 0)
+ error (_("Junk after argument: %s."), arg);
+
+ target_insn_history_range (begin, end, flags);
+ }
+}
+
/* Provide a prototype to silence -Wmissing-prototypes. */
extern initialize_file_ftype _initialize_record;
@@ -305,6 +458,13 @@ _initialize_record (void)
NULL, show_record_debug, &setdebuglist,
&showdebuglist);
+ add_setshow_uinteger_cmd ("instruction-history-size", no_class,
+ &record_insn_history_size, _("\
+Set number of instructions to print in \"record instruction-history\"."), _("\
+Show number of instructions to print in \"record instruction-history\"."),
+ NULL, NULL, NULL, &set_record_cmdlist,
+ &show_record_cmdlist);
+
c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
_("Start recording."),
&record_cmdlist, "record ", 0, &cmdlist);
@@ -346,4 +506,23 @@ Default filename is 'gdb_record.<process_id>'."),
Restore the program to its state at instruction number N.\n\
Argument is instruction number, as shown by 'info record'."),
&record_cmdlist);
+
+ add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
+Print disassembled instructions stored in the execution log.\n\
+With a /m modifier, source lines are included (if available).\n\
+With a /r modifier, raw instructions in hex are included.\n\
+With a /f modifier, function names are omitted.\n\
+With no argument, disassembles ten more instructions after or around the \
+previous disassembly.\n\
+\"record instruction-history -\" disassembles ten instructions before a \
+previous ten-line disassembly.\n\
+One argument specifies an instruction number as shown by 'info record', and \
+ten instructions are disassembled around that instruction.\n\
+Two arguments with comma between specify starting and ending instruction \
+numbers to disassemble.\n\
+If the second argument is preceded by '+' or '-', it specifies the distance \
+from the first argument.\n\
+The number of instructions to disassemble can be defined with \"set record \
+instruction-history-size\"."),
+ &record_cmdlist);
}
diff --git a/gdb/target.c b/gdb/target.c
index 868ff63..7643d6a 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -4360,6 +4360,40 @@ target_goto_record (ULONGEST insn)
tcomplain ();
}
+/* See target.h. */
+
+void
+target_insn_history (int size, int flags)
+{
+ struct target_ops *t;
+
+ for (t = current_target.beneath; t != NULL; t = t->beneath)
+ if (t->to_insn_history != NULL)
+ {
+ t->to_insn_history (size, flags);
+ return;
+ }
+
+ tcomplain ();
+}
+
+/* See target.h. */
+
+void
+target_insn_history_range (ULONGEST begin, ULONGEST end, int flags)
+{
+ struct target_ops *t;
+
+ for (t = current_target.beneath; t != NULL; t = t->beneath)
+ if (t->to_insn_history_range != NULL)
+ {
+ t->to_insn_history_range (begin, end, flags);
+ return;
+ }
+
+ tcomplain ();
+}
+
static void
debug_to_prepare_to_store (struct regcache *regcache)
{
diff --git a/gdb/target.h b/gdb/target.h
index d416193..d5551aa 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -897,6 +897,13 @@ struct target_ops
/* Go to a specific location in the recorded execution trace. */
void (*to_goto_record) (ULONGEST insn);
+ /* Disassemble SIZE instructions in the recorded execution trace from
+ the current position. */
+ void (*to_insn_history) (int size, int flags);
+
+ /* Disassemble a section of the recorded execution trace. */
+ void (*to_insn_history_range) (ULONGEST begin, ULONGEST end, int flags);
+
int to_magic;
/* Need sub-structure for target machine related rather than comm related?
*/
@@ -1983,4 +1990,10 @@ extern void target_goto_record_end (void);
/* See to_goto_record in struct target_ops. */
extern void target_goto_record (ULONGEST insn);
+/* See to_insn_history. */
+extern void target_insn_history (int size, int flags);
+
+/* See to_insn_history_range. */
+extern void target_insn_history_range (ULONGEST begin, ULONGEST end, int flags);
+
#endif /* !defined (TARGET_H) */
--
1.7.1