From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7E03B3858D1E for ; Thu, 22 Dec 2022 15:51:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E03B3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671724304; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=h/VkhEjoAYI4vmy4C4Tnw2rq4T3Qwtn3ZkqHP1NZARI=; b=QYBfcqvFDkht4lRkUPoNYjtGLtXENg7oas+z47ZCdhpY40DMksqag9UtrfhcnVDFtLk1/9 poG/W5ZoyG9tkGbEsoHAKJcqHPyMAHdRDYrsWT97/cJn6yMsum/ph99lne3lHn6wbwz6TE GsEwHglPwbsvEUiHd65tzvGCZRgDVsg= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-498-qTq18n6mMoezL_O_VtN6jg-1; Thu, 22 Dec 2022 10:51:40 -0500 X-MC-Unique: qTq18n6mMoezL_O_VtN6jg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A1784281DE74; Thu, 22 Dec 2022 15:51:40 +0000 (UTC) Received: from fedora.redhat.com (ovpn-193-77.brq.redhat.com [10.40.193.77]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D16474014D1A; Thu, 22 Dec 2022 15:51:39 +0000 (UTC) From: Bruno Larsen To: gdb-patches@sourceware.org Cc: Bruno Larsen , Eli Zaretskii Subject: [PATCH v3] gdb: add 'maintenance print record-instruction' command Date: Thu, 22 Dec 2022 16:43:39 +0100 Message-Id: <20221222154338.2223678-1-blarsen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: While chasing some reverse debugging bugs, I found myself wondering what was recorded by GDB to undo and redo a certain instruction. This commit implements a simple way of printing that information. If there isn't enough history to print the desired instruction (such as when the user hasn't started recording yet or when they request 2 instructions back but only 1 was recorded), GDB warns the user like so: (gdb) maint print record-instruction Not enough recorded history If there is enough, GDB prints the instruction like so: (gdb) maint print record-instruction 4 bytes of memory at address 0x00007fffffffd5dc changed from: 01 00 00 00 Register eflags changed: [ IF ] Register rip changed: (void (*)()) 0x401115 Approved-by: Eli Zaretskii --- Changes for v3: * Added minimal test for the new command * Changed register printing to use value_print Changes for v2: * Style changes --- gdb/NEWS | 6 ++ gdb/doc/gdb.texinfo | 8 ++ gdb/record-full.c | 95 +++++++++++++++++++ .../gdb.reverse/maint-print-instruction.c | 25 +++++ .../gdb.reverse/maint-print-instruction.exp | 66 +++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 gdb/testsuite/gdb.reverse/maint-print-instruction.c create mode 100644 gdb/testsuite/gdb.reverse/maint-print-instruction.exp diff --git a/gdb/NEWS b/gdb/NEWS index e61f06081de..2578412d017 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -107,6 +107,12 @@ * New commands +maintenance print record-instruction [ N ] + Print the recorded information for a given instruction. If N is not given + prints how GDB would undo the last instruction executed. If N is negative, + prints how GDB would undo the N-th previous instruction, and if N is + positive, it prints how GDB will redo the N-th following instruction. + maintenance set ignore-prologue-end-flag on|off maintenance show ignore-prologue-end-flag This setting, which is off by default, controls whether GDB ignores the diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index bef63eb4e8e..eea3037ea5b 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -40531,6 +40531,14 @@ that symbol is described. The type chain produced by this command is a recursive definition of the data type as stored in @value{GDBN}'s data structures, including its flags and contained types. +@kindex maint print record-instruction +@item maint print record-instruction +@itemx maint print record-instruction @var{N} +print how GDB recorded a given instruction. If @var{n} is not positive +number, it prints the values stored by the inferior before the @var{n}-th previous +instruction was exectued. If @var{n} is positive, print the values after the @var{n}-th +following instruction is executed. If @var{n} is not given, 0 is assumed. + @kindex maint selftest @cindex self tests @item maint selftest @r{[}-verbose@r{]} @r{[}@var{filter}@r{]} diff --git a/gdb/record-full.c b/gdb/record-full.c index 48b92281fe6..6c47813706b 100644 --- a/gdb/record-full.c +++ b/gdb/record-full.c @@ -39,6 +39,7 @@ #include "gdbsupport/gdb_unlinker.h" #include "gdbsupport/byte-vector.h" #include "async-event.h" +#include "valprint.h" #include @@ -2764,6 +2765,90 @@ set_record_full_insn_max_num (const char *args, int from_tty, } } +/* Implement the 'maintenance print record-instruction' command. */ + +static void +maintenance_print_record_instruction (const char *args, int from_tty) +{ + struct record_full_entry *to_print = record_full_list; + + if (args != nullptr) + { + int offset = value_as_long (parse_and_eval (args)); + if (offset > 0) + { + /* Move forward OFFSET instructions. We know we found the + end of an instruction when to_print->type is record_full_end. */ + while (to_print->next != nullptr && offset > 0) + { + to_print = to_print->next; + if (to_print->type == record_full_end) + offset--; + } + if (offset != 0) + error (_("Not enough recorded history")); + } + else + { + while (to_print->prev != nullptr && offset < 0) + { + to_print = to_print->prev; + if (to_print->type == record_full_end) + offset++; + } + if (offset != 0) + error (_("Not enough recorded history")); + } + } + gdb_assert (to_print != nullptr); + + /* Go back to the start of the instruction. */ + while (to_print->prev != nullptr && to_print->prev->type != record_full_end) + to_print = to_print->prev; + + /* if we're in the first record, there are no actual instructions + recorded. Warn the user and leave. */ + if (to_print == &record_full_first) + error (_("Not enough recorded history")); + + while (to_print->type != record_full_end) + { + switch (to_print->type) + { + case record_full_reg: + { + type *regtype = gdbarch_register_type (target_gdbarch (), + to_print->u.reg.num); + value *val + = value_from_contents (regtype, + record_full_get_loc (to_print)); + gdb_printf ("Register %s changed: ", + gdbarch_register_name (target_gdbarch (), + to_print->u.reg.num)); + struct value_print_options opts; + get_user_print_options (&opts); + opts.raw = true; + value_print (val, gdb_stdout, &opts); + gdb_printf ("\n"); + break; + } + case record_full_mem: + { + gdb_byte *b = record_full_get_loc (to_print); + gdb_printf ("%d bytes of memory at address %s changed from:", + to_print->u.mem.len, + print_core_address (target_gdbarch (), + to_print->u.mem.addr)); + for (int i = 0; i < to_print->u.mem.len; i++) + gdb_printf (" %02x", b[i]); + gdb_printf ("\n"); + break; + } + } + to_print = to_print->next; + } +} + void _initialize_record_full (); void _initialize_record_full () @@ -2868,4 +2953,14 @@ When ON, query if PREC cannot record memory change of next instruction."), c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.show, no_class, 1,&show_record_cmdlist); deprecate_cmd (c, "show record full memory-query"); + + add_cmd ("record-instruction", class_maintenance, + maintenance_print_record_instruction, + _("\ +Print a recorded instruction.\n\ +If no argument is provided, print the last instruction recorded.\n\ +If a negative argument is given, prints how the nth previous \ +instruction will be undone.\n\ +If a positive argument is given, prints \ +how the nth following instruction will be redone."), &maintenanceprintlist); } diff --git a/gdb/testsuite/gdb.reverse/maint-print-instruction.c b/gdb/testsuite/gdb.reverse/maint-print-instruction.c new file mode 100644 index 00000000000..f06ed530620 --- /dev/null +++ b/gdb/testsuite/gdb.reverse/maint-print-instruction.c @@ -0,0 +1,25 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int +main () +{ + int x = 0; + x ++; + x --; + return x; +} diff --git a/gdb/testsuite/gdb.reverse/maint-print-instruction.exp b/gdb/testsuite/gdb.reverse/maint-print-instruction.exp new file mode 100644 index 00000000000..b78b696add6 --- /dev/null +++ b/gdb/testsuite/gdb.reverse/maint-print-instruction.exp @@ -0,0 +1,66 @@ +# Copyright 2008-2022 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. it tests the functionality of +# the maintenance print record-instruction command, but does not check the +# syntax, only if the command finds or fails to find recorded history. + +if ![supports_reverse] { + return +} + +standard_testfile + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { + return -1 +} + +proc test_print { has_history level test_name } { + gdb_test_multiple "maint print record-instruction $level" $test_name { + -re -wrap ".*Not enough recorded history.*" { + gdb_assert !$has_history $test_name + } + + -re -wrap ".*changed.*" { + gdb_assert $has_history $test_name + } + } +} + +if { ![runto_main] } { + return 0 +} + +test_print false "" "print before starting to record" + +if [supports_process_record] { + # Activate process record/replay + gdb_test_no_output "record" "turn on process record" +} + +test_print false "" "print before any instruction" + +gdb_test "stepi 3" ".*" "collecting history" +test_print true "" "print current after executing a bit" +test_print true "-1" "print previous after executing a bit" +test_print false "1" "print following after executing a bit" + +gdb_test "reverse-stepi" ".*" "moving back" +test_print true "" "print current after reversing" +test_print true "-1" "print previous after reversing" +test_print true "1" "print following after reversing" + +test_print false "-10" "trying to print too far back" +test_print false "10" "trying to print too far forward" -- 2.38.1