public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 2/9] gdb/testsuite: new test for -data-disassemble opcodes format
Date: Thu, 23 Jun 2022 17:05:09 +0100	[thread overview]
Message-ID: <e349c21e43ccc7716454b69b9853ebeb7ffd844e.1655999715.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1655999715.git.aburgess@redhat.com>

Add another test for the output of MI command -data-disassemble.  The
new check validates the format of the 'opcodes' field, specifically,
this test checks that the field contains a series of bytes, separated
by a single space.

We also check that the bytes are in the correct order, that is, the
first byte is from the lowest address, and subsequent bytes are from
increasing addresses.

The motivation for this test (besides more tests being generally good)
is that I plan to make changes to how opcode bytes are displayed in
the disassembler output, and I want to ensure that I don't break any
existing MI behaviour.

There should be no user visible changes to GDB after this commit.
---
 gdb/testsuite/gdb.mi/mi-disassemble.exp | 83 +++++++++++++++++++++++++
 gdb/testsuite/lib/mi-support.exp        | 27 ++++++++
 2 files changed, 110 insertions(+)

diff --git a/gdb/testsuite/gdb.mi/mi-disassemble.exp b/gdb/testsuite/gdb.mi/mi-disassemble.exp
index 2d3e8e216b1..b7c52472c84 100644
--- a/gdb/testsuite/gdb.mi/mi-disassemble.exp
+++ b/gdb/testsuite/gdb.mi/mi-disassemble.exp
@@ -239,6 +239,88 @@ proc test_disassembly_bogus_args {} {
 
 }
 
+# Check the format of the opcode bytes.
+proc test_disassembly_opcode_format {} {
+    # First, we need to find a multi-byte instruction that we can
+    # then disassemble using the MI command.
+    set longest_insn_bytes ""
+    set longest_insn_addr ""
+    gdb_test_multiple "disassemble /r main" "" {
+	-re "^disassemble /r main\r\n" {
+	    exp_continue
+	}
+
+	-re "^&\"disassemble /r main.n\"\r\n" {
+	    exp_continue
+	}
+
+	-re "^~\"Dump of assembler code for function \[^\r\n\]+\r\n" {
+	    exp_continue
+	}
+
+	-re "^~\".. ($::hex) <\[^>\]+>:\\\\t(\[^\\\\\]+)\\\\t\[^\r\n\]+\r\n" {
+	    set addr $expect_out(1,string)
+	    set bytes $expect_out(2,string)
+	    if { [string length $bytes] > [string length $longest_insn_bytes] } {
+		set longest_insn_addr $addr
+		set longest_insn_bytes $bytes
+	    }
+	    exp_continue
+	}
+
+	-re "^~\"End of assembler dump\[^\r\n\]+\r\n" {
+	    exp_continue
+	}
+
+	-re "^\\^done\r\n$::mi_gdb_prompt$" {
+	    gdb_assert { ![string equal $longest_insn_bytes ""] } \
+		"found the bytes string for a longest instruction"
+	    gdb_assert { ![string equal $longest_insn_addr ""] } \
+		"found the address for a longest instruction"
+	}
+    }
+
+    verbose -log "Longest instruction at ${longest_insn_addr} with bytes '${longest_insn_bytes}'"
+
+    # Check that the instruction bytes that we found above consists of
+    # a series of individual bytes separated by a whitespace.  Also,
+    # we check that the bytes reported match what can be found in the
+    # inferior memory.
+    set split_bytes [split $longest_insn_bytes " "]
+    set is_bad false
+    set addr $longest_insn_addr
+    set idx 0
+    foreach b $split_bytes {
+	if { [string length $b] != 2 } {
+	    set is_bad true
+	}
+
+	# Load the actual byte value from memory, and check it matches
+	# the opcode byte reported in the disassembler output.
+	set addr 0x[format %x [expr $longest_insn_addr + $idx]]
+	set actual [format %02x [mi_get_valueof "/x" "*((unsigned char *) $addr)" "XX"]]
+	gdb_assert [string equal $actual "$b"] \
+	    "byte at $addr matches"
+
+	incr idx
+    }
+    gdb_assert { !$is_bad } "check length of each byte"
+    set check_bytes [join $split_bytes " "]
+    gdb_assert { [string equal $check_bytes $longest_insn_bytes] } \
+	"bytes are separated by a single space"
+
+    # Figure out an end address at which to stop the disassembly.
+    set byte_count [llength $split_bytes]
+    set end_addr 0x[format %x [expr $longest_insn_addr + $byte_count]]
+    set start_addr $longest_insn_addr
+
+    verbose -log "Instruction is ${byte_count} bytes, end address ${end_addr}"
+
+    mi_gdb_test "321-data-disassemble -s $start_addr -e $end_addr -- 2" \
+	"321\\^done,asm_insns=\\\[\{address=\"$start_addr\",func-name=\"main\",offset=\"$::decimal\",opcodes=\"$longest_insn_bytes\",inst=\".*\"\}\]" \
+	"data-disassemble checking the opcodes bytes format"
+}
+
 mi_clean_restart $binfile
 mi_runto_main
 test_disassembly_only
@@ -248,6 +330,7 @@ test_disassembly_mixed_with_opcodes
 test_disassembly_bogus_args
 test_disassembly_lines_limit
 test_disassembly_mixed_lines_limit
+test_disassembly_opcode_format
 
 mi_gdb_exit
 return 0
diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp
index e578a7e6f9b..2af12461ec5 100644
--- a/gdb/testsuite/lib/mi-support.exp
+++ b/gdb/testsuite/lib/mi-support.exp
@@ -2783,3 +2783,30 @@ proc mi_is_target_remote {} {
 
     return [gdb_is_target_remote_prompt "$mi_gdb_prompt"]
 }
+
+# Retrieve the value of EXP in the inferior, represented in format
+# specified in FMT (using "printFMT").  DEFAULT is used as fallback if
+# print fails.  TEST is the test message to use.  It can be omitted,
+# in which case a test message is built from EXP.
+#
+# This is an MI version of gdb_valueof.
+
+proc mi_get_valueof { fmt exp default {test ""} } {
+    global mi_gdb_prompt
+
+    if {$test == "" } {
+	set test "get valueof \"${exp}\""
+    }
+
+    set val ${default}
+    gdb_test_multiple "print${fmt} ${exp}" "$test" {
+	-re "~\"\\$\[0-9\]* = (\[^\r\n\]*)\\\\n\"\r\n\\^done\r\n$mi_gdb_prompt$" {
+	    set val $expect_out(1,string)
+	    pass "$test"
+	}
+	timeout {
+	    fail "$test (timeout)"
+	}
+    }
+    return ${val}
+}
-- 
2.25.4


  parent reply	other threads:[~2022-06-23 16:05 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-23 16:05 [PATCH 0/9] Disassembler opcode display and text alignment Andrew Burgess
2022-06-23 16:05 ` [PATCH 1/9] gdb/doc: improve description of --data-disassemble opcodes output Andrew Burgess
2022-06-23 16:09   ` Eli Zaretskii
2022-06-29 12:54     ` Andrew Burgess
2022-06-23 16:05 ` Andrew Burgess [this message]
2022-06-23 16:05 ` [PATCH 3/9] gdb/disasm: read opcodes bytes with a single read_code call Andrew Burgess
2022-06-23 16:05 ` [PATCH 4/9] gdb: disassembler opcode display formatting Andrew Burgess
2022-06-23 16:14   ` Eli Zaretskii
2022-06-23 16:05 ` [PATCH 5/9] gdb: make gdb_disassembly_flag unsigned Andrew Burgess
2022-06-23 16:05 ` [PATCH 6/9] gdb/doc: fix column widths in MI compatibility table Andrew Burgess
2022-06-23 16:22   ` Eli Zaretskii
2022-06-30  9:39     ` Andrew Burgess
2022-06-23 16:05 ` [PATCH 7/9] gdb/doc: update syntax of -data-disassemble command arguments Andrew Burgess
2022-06-23 16:21   ` Eli Zaretskii
2022-06-30 10:18     ` Andrew Burgess
2022-06-30 10:46       ` Eli Zaretskii
2022-06-23 16:05 ` [PATCH 8/9] gdb/mi: some int to bool conversion Andrew Burgess
2022-06-23 16:05 ` [PATCH 9/9] gdb/mi: new options for -data-disassemble command Andrew Burgess
2022-06-23 16:34   ` Eli Zaretskii
2022-06-30 11:22     ` Andrew Burgess
2022-06-30 13:36       ` Eli Zaretskii
2022-07-25 18:28 ` [PATCH 0/9] Disassembler opcode display and text alignment Andrew Burgess
2022-09-05 14:11   ` Andrew Burgess
2022-09-21 16:41 ` Tom Tromey
2022-10-02 13:15   ` Andrew Burgess

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=e349c21e43ccc7716454b69b9853ebeb7ffd844e.1655999715.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).