Re: [PATCH] MI disassemble opcode support

Andrew Burgess
2010-12-16T10:46:21Z

On 15/12/2010 18:16, Eli Zaretskii wrote:
>> Date: Wed, 15 Dec 2010 13:32:05 +0000
>> From: "Andrew Burgess"<aburgess@broadcom.com>
>>
>> 2010-12-10 Andrew Burgess<aburgess@broadcom.com>
>>
>> * gdb.texinfo: Update to reflect changes in mi/mi-cmd-disas.c
>
> The ChangeLog entry should show the name of the node where this change
> was made (as if the node were a function).

done.

>> + MODE: 0 -- disassembly.
>> + 1 -- disassembly and source.
>> + 2 -- disassembly and opcodes.
>> + 3 -- disassembly and source and opcodes.
>
> Please edit the last line to be valid English:

done.

Thanks for taking the time to review this change.

Andrew

gdb/

2010-12-10 Andrew Burgess <aburgess@broadcom.com>

* disasm.c (dump_insns): Build the opcodes into a stream so
that we can dump them as a field for MI.

gdb/doc/

2010-12-10 Andrew Burgess <aburgess@broadcom.com>

* gdb.texinfo (GDB/MI Data Manipulation): Update to reflect
changes in mi/mi-cmd-disas.c

gdb/mi/

2010-12-10 Andrew Burgess <aburgess@broadcom.com>

* mi-cmd-disas.c (mi_cmd_disassemble): Allow mode to control
dumping of instruction opcodes.

gdb/testsuite/

2010-12-10 Andrew Burgess <aburgess@broadcom.com>

* gdb.mi/mi-disassemble.exp, gdb.mi/mi2-disassemble.exp: Update
expected output to reflect changes in gdb/mi/mi-cmd-disas.c and
add new tests to check opcode dumping works.

diff --git a/gdb/disasm.c b/gdb/disasm.c
index c51f0bf..9fa34a0 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -143,6 +143,12 @@ dump_insns (struct gdbarch *gdbarch, struct ui_out *uiout,
CORE_ADDR old_pc = pc;
bfd_byte data;
int status;
+ const char *spacer = "";
+
+ /* Temporary stream for building up the opcodes */
+ struct ui_stream *opcode_stream = ui_out_stream_new (uiout);
+ struct cleanup *cleanups =
+ make_cleanup_ui_out_stream_delete (opcode_stream);

pc += gdbarch_print_insn (gdbarch, pc, di);
for (;old_pc < pc; old_pc++)
@@ -150,9 +156,14 @@ dump_insns (struct gdbarch *gdbarch, struct ui_out *uiout,
status = (*di->read_memory_func) (old_pc, &data, 1, di);
if (status != 0)
(*di->memory_error_func) (status, old_pc, di);
- ui_out_message (uiout, 0, " %02x", (unsigned)data);
+ fprintf_filtered(opcode_stream->stream, "%s%02x",
+ spacer, (unsigned)data);
+ spacer = " ";
}
+ ui_out_field_stream (uiout, "opcodes", opcode_stream);
ui_out_text (uiout, "\t");
+
+ do_cleanups (cleanups);
}
else
pc += gdbarch_print_insn (gdbarch, pc, di);
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index ddc711b..57c89a8 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -27567,8 +27567,9 @@ displayed; if @var{lines} is higher than the number of lines between
@var{start-addr} and @var{end-addr}, only the lines up to @var{end-addr}
are displayed.
@item @var{mode}
-is either 0 (meaning only disassembly) or 1 (meaning mixed source and
-disassembly).
+is either 0 (meaning only disassembly), 1 (meaning mixed source and
+disassembly), 2 (meaning disassembly with raw opcodes), or 3 (meaning
+mixed source and disassembly with raw opcodes).
@end table

@subsubheading Result
diff --git a/gdb/mi/mi-cmd-disas.c b/gdb/mi/mi-cmd-disas.c
index b543acc..a93b5de 100644
--- a/gdb/mi/mi-cmd-disas.c
+++ b/gdb/mi/mi-cmd-disas.c
@@ -46,15 +46,18 @@

always required:

- MODE: 0 or 1 for disassembly only, or mixed source and disassembly,
- respectively. */
+ MODE: 0 -- disassembly.
+ 1 -- disassembly and source.
+ 2 -- disassembly and opcodes.
+ 3 -- disassembly, source and opcodes.
+*/
void
mi_cmd_disassemble (char *command, char **argv, int argc)
{
struct gdbarch *gdbarch = get_current_arch ();
CORE_ADDR start;

- int mixed_source_and_assembly;
+ int mode, disasm_flags;
struct symtab *s;

/* Which options have we processed ... */
@@ -135,10 +138,17 @@ mi_cmd_disassemble (char *command, char **argv, int argc)
error
("mi_cmd_disassemble: Usage: [-f filename -l linenum [-n howmany]] [-s startaddr -e endaddr] [--] mixed_mode.");

- mixed_source_and_assembly = atoi (argv[0]);
- if ((mixed_source_and_assembly != 0) && (mixed_source_and_assembly != 1))
- error (_("mi_cmd_disassemble: Mixed_mode argument must be 0 or 1."));
+ mode = atoi (argv[0]);
+ if (mode < 0 || mode > 3)
+ error (_("mi_cmd_disassemble: Mode argument must be 0, 1, 2, or 3."));

+ /* Convert the mode into a set of disassembly flags */
+
+ disasm_flags = 0;
+ if (mode & 0x1)
+ disasm_flags |= DISASSEMBLY_SOURCE;
+ if (mode & 0x2)
+ disasm_flags |= DISASSEMBLY_RAW_INSN;

/* We must get the function beginning and end where line_num is
contained. */
@@ -156,6 +166,6 @@ mi_cmd_disassemble (char *command, char **argv, int argc)

gdb_disassembly (gdbarch, uiout,
file_string,
- mixed_source_and_assembly? DISASSEMBLY_SOURCE : 0,
+ disasm_flags,
how_many, low, high);
}
diff --git a/gdb/testsuite/gdb.mi/mi-disassemble.exp b/gdb/testsuite/gdb.mi/mi-disassemble.exp
index f5aa784..4c1f1e4 100644
--- a/gdb/testsuite/gdb.mi/mi-disassemble.exp
+++ b/gdb/testsuite/gdb.mi/mi-disassemble.exp
@@ -60,6 +60,29 @@ proc test_disassembly_only {} {
"data-disassemble file & line, assembly only"
}

+proc test_disassembly_with_opcodes {} {
+ global mi_gdb_prompt
+ global hex
+ global decimal
+
+ set line_main_head [gdb_get_line_number "main ("]
+ set line_main_body [expr $line_main_head + 2]
+
+ # Test disassembly with opcodes for the current function.
+ # Tests:
+ # -data-disassemble -s $pc -e "$pc+8" -- 2
+ # -data-disassembly -f basics.c -l $line_main_body -- 2
+
+ mi_gdb_test "print/x \$pc" "" ""
+ mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 2" \
+ "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\]" \
+ "data-disassemble from pc to pc+12 assembly with opcodes"
+
+ mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -- 2" \
+ "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",opcodes=\".*\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]" \
+ "data-disassemble file & line, assembly with opcodes"
+}
+
proc test_disassembly_lines_limit {} {
global mi_gdb_prompt
global hex
@@ -116,6 +139,33 @@ proc test_disassembly_mixed {} {
"data-disassemble range assembly mixed"
}

+proc test_disassembly_mixed_with_opcodes {} {
+ global mi_gdb_prompt
+ global hex
+ global decimal
+
+ set line_callee2_head [gdb_get_line_number "callee2 ("]
+ set line_callee2_open_brace [expr $line_callee2_head + 1]
+
+ # Test disassembly mixed with opcodes for the current function.
+ # Tests:
+ # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 3
+ # -data-disassembly -s $pc -e "$pc+8" -- 3
+
+ mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 3" \
+ "002\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_callee2_open_brace\",file=\".*basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"callee2\",offset=\"0\",opcodes=\".*\",inst=\".*\"\}.*\\\]\}.*,src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",line_asm_insn=\\\[.*\{address=\"$hex\",func-name=\"callee2\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]\}\\\]" \
+ "data-disassemble file, line assembly mixed with opcodes"
+
+ #
+ # In mixed mode, the lowest level of granularity is the source line.
+ # So we are going to get the disassembly for the source line at
+ # which we are now, even if we have specified that the range is only 2 insns.
+ #
+ mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 3" \
+ "003\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]\}\\\]" \
+ "data-disassemble range assembly mixed with opcodes"
+}
+
proc test_disassembly_mixed_lines_limit {} {
global mi_gdb_prompt
global hex
@@ -172,14 +222,16 @@ proc test_disassembly_bogus_args {} {
"data-disassemble mix different args"

mi_gdb_test "789-data-disassemble -f basics.c -l $line_main_body -- 9" \
- "789\\^error,msg=\"mi_cmd_disassemble: Mixed_mode argument must be 0 or 1.\"" \
+ "789\\^error,msg=\"mi_cmd_disassemble: Mode argument must be 0, 1, 2, or 3.\"" \
"data-disassemble wrong mode arg"

}

mi_run_to_main
test_disassembly_only
+test_disassembly_with_opcodes
test_disassembly_mixed
+test_disassembly_mixed_with_opcodes
test_disassembly_bogus_args
test_disassembly_lines_limit
test_disassembly_mixed_lines_limit
diff --git a/gdb/testsuite/gdb.mi/mi2-disassemble.exp b/gdb/testsuite/gdb.mi/mi2-disassemble.exp
index 8ccbc9f..1f2ee0c 100644
--- a/gdb/testsuite/gdb.mi/mi2-disassemble.exp
+++ b/gdb/testsuite/gdb.mi/mi2-disassemble.exp
@@ -60,6 +60,29 @@ proc test_disassembly_only {} {
"data-disassemble file & line, assembly only"
}

+proc test_disassembly_with_opcodes {} {
+ global mi_gdb_prompt
+ global hex
+ global decimal
+
+ set line_main_head [gdb_get_line_number "main ("]
+ set line_main_body [expr $line_main_head + 2]
+
+ # Test disassembly with opcodes for the current function.
+ # Tests:
+ # -data-disassemble -s $pc -e "$pc+8" -- 2
+ # -data-disassembly -f basics.c -l $line_main_body -- 2
+
+ mi_gdb_test "print/x \$pc" "" ""
+ mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 2" \
+ "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\]" \
+ "data-disassemble from pc to pc+12 assembly with opcodes"
+
+ mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -- 2" \
+ "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",opcodes=\".*\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]" \
+ "data-disassemble file & line, assembly with opcodes"
+}
+
proc test_disassembly_lines_limit {} {
global mi_gdb_prompt
global hex
@@ -116,6 +139,33 @@ proc test_disassembly_mixed {} {
"data-disassemble range assembly mixed"
}

+proc test_disassembly_mixed_with_opcodes {} {
+ global mi_gdb_prompt
+ global hex
+ global decimal
+
+ set line_callee2_head [gdb_get_line_number "callee2 ("]
+ set line_callee2_open_brace [expr $line_callee2_head + 1]
+
+ # Test disassembly mixed with opcodes for the current function.
+ # Tests:
+ # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 3
+ # -data-disassembly -s $pc -e "$pc+8" -- 3
+
+ mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 3" \
+ "002\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_callee2_open_brace\",file=\".*basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"callee2\",offset=\"0\",opcodes=\".*\",inst=\".*\"\}.*\\\]\}.*,src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",line_asm_insn=\\\[.*\{address=\"$hex\",func-name=\"callee2\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]\}\\\]" \
+ "data-disassemble file, line assembly mixed with opcodes"
+
+ #
+ # In mixed mode, the lowest level of granularity is the source line.
+ # So we are going to get the disassembly for the source line at
+ # which we are now, even if we have specified that the range is only 2 insns.
+ #
+ mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 3" \
+ "003\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]\}\\\]" \
+ "data-disassemble range assembly mixed with opcodes"
+}
+
proc test_disassembly_mixed_lines_limit {} {
global mi_gdb_prompt
global hex
@@ -172,14 +222,16 @@ proc test_disassembly_bogus_args {} {