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 ED035385AE78 for ; Thu, 23 Jun 2022 16:05:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ED035385AE78 Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-34-Zp0Syj5BMaCPelYHa7C0Hw-1; Thu, 23 Jun 2022 12:05:38 -0400 X-MC-Unique: Zp0Syj5BMaCPelYHa7C0Hw-1 Received: by mail-wr1-f71.google.com with SMTP id z11-20020adfc00b000000b0021a3ab8ec82so4584715wre.23 for ; Thu, 23 Jun 2022 09:05:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Gmr/f5sT8+/Zb8NeSXzTgRu6Do4bDUK8meyXVZPOoro=; b=UCmGYdZgzU99KuPOwl7juaVSGk8lKgSc8ZGn8hZeTYvpmqBK1C4gBy1RZcGX0K5T+w aWGfiSsknCnmGKbcBQ6GiB5f7WWjeRBg6fOqwpHAgFpY8ucdvTIBzcUxS40KlZy9Kfum ZPD3oPg98FWJVaUhfaUqoacR+2V3muZ2249QtH+dilsItbJruqqH2yWMHMn3TIy/Fg8r +25DKPQ8f3Utgjjhv3SAEsJPyPQGiC+Vcp8tgLVKvIanNc+TRTR4FOzKEsLkHwQ4S0S6 A5Ay4ybNQlsQzWerba8c0senw8mc0bQ15QxUhAXBA4pie2sC/DisA4NNVnbXOUwz1HDi UQjQ== X-Gm-Message-State: AJIora/Wes99Ta0s4q7zFnR3XQAEqpTl0OzlOy9TpprhxcNX0FdIhwBX 1PB/4pH/vUH5TPAKYQ7NxwJ1/XXWpxFxFIC5DVLFFiGLp/CpP6iMLYWR56VpVpCKheSGQzXNMLY 8pVWIGwjOVMsxszZAennj4T6cVFik/Or4itdWLs2ltSH3w7OcGn5IsaROrQHT1+Iq9LcZ8/jAbw == X-Received: by 2002:a05:600c:1f05:b0:39c:51c6:7c85 with SMTP id bd5-20020a05600c1f0500b0039c51c67c85mr5044355wmb.33.1656000337223; Thu, 23 Jun 2022 09:05:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t/Dv+DztEVZwkfiUpBUMhfCHfdeXkFedscSS03GnKS20TVzovfLRzj4seWOntVZvTxhGeJvQ== X-Received: by 2002:a05:600c:1f05:b0:39c:51c6:7c85 with SMTP id bd5-20020a05600c1f0500b0039c51c67c85mr5044295wmb.33.1656000336644; Thu, 23 Jun 2022 09:05:36 -0700 (PDT) Received: from localhost ([195.213.152.79]) by smtp.gmail.com with ESMTPSA id j15-20020a5d604f000000b0021b944c9ce4sm9988809wrt.15.2022.06.23.09.05.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 09:05:36 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 9/9] gdb/mi: new options for -data-disassemble command Date: Thu, 23 Jun 2022 17:05:16 +0100 Message-Id: <7e451914de0fc57677e73bf48d66a70b23276f7f.1655999715.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 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=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jun 2022 16:05:44 -0000 Now that the disassembler has two different strategies for laying out the opcode bytes of an instruction (see /r vs /b for the disassemble command), I wanted to add support for this to the MI disassemble command. Currently the -data-disassemble command takes a single 'mode' value, which currently has 6 different values (0 -> 5), 3 of these modes relate to opcode display. So, clearly I should just add an additional 3 modes to handle the new opcode format, right? No, I didn't think that was a great idea either. So, I wonder, could I adjust the -data-disassemble command in a backward compatible way, that would allow GDB to move away from using the mode value altogether? I think we can. In this commit, I propose adding two new options to -data-disassemble, these are: --opcodes none|bytes|display --source Additionally, I will make the mode optional, and default to mode 0 if no mode value is given. Mode 0 is the simplest, no source code, no opcodes disassembly mode. The two new options are only valid for mode 0, if they are used with any other mode then an error is thrown. The --opcodes option can add opcodes to the result, with 'bytes' being equivalent to 'disassemble /b' and 'display' being 'disassemble /r'. The --source option will enable the /s style source code display, this is equivalent to modes 4 and 5. There is no way, using the new command options to get the now deprecated /m style source code display, that is mode 1 and 3. My hope is that new users of the MI will not use the mode at all, and instead will just use the new options to achieve the output they need. Existing MI users can continue to use the mode, and will not need to be updated to use the new options. --- gdb/doc/gdb.texinfo | 83 ++++++++++++++++++------- gdb/mi/mi-cmd-disas.c | 75 +++++++++++++++++++--- gdb/testsuite/gdb.mi/mi-disassemble.exp | 66 +++++++++++++------- 3 files changed, 174 insertions(+), 50 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index ca9d29bd364..456d362a109 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -34387,7 +34387,9 @@ ( -s @var{start-addr} -e @var{end-addr} | -a @var{addr} | -f @var{filename} -l @var{linenum} [ -n @var{lines} ] ) - -- @var{mode} + [ --opcodes @var{opcodes-mode} ] + [ --source ] + [ -- @var{mode} ] @end smallexample @noindent @@ -34416,31 +34418,56 @@ 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{opcodes-mode} +is one of @samp{none}, @samp{bytes}, or @samp{display}. This setting +can only be used with @var{mode} 0. Passing @samp{none} here will +prevent opcodes being included in the result; passing @samp{bytes} here +will include opcodes in the result, the opcodes will be formatted as for +@kbd{disassemble /b}; and passing @samp{display} here will include the +opcodes in the result, the opcodes will be formatted as for +@kbd{disassemble /r}. @item @var{mode} -is one of: +the use of @var{mode} is deprecated in favour of using the +@code{--opcodes} and @code{--source} options. When no @var{mode} is +given, @var{mode} 0 will be assumed. However, the @var{mode} is still +available for backward compatibility. The @var{mode} should be one of: @itemize @bullet -@item 0 disassembly only -@item 1 mixed source and disassembly (deprecated) -@item 2 disassembly with raw opcodes -@item 3 mixed source and disassembly with raw opcodes (deprecated) -@item 4 mixed source and disassembly -@item 5 mixed source and disassembly with raw opcodes +@item 0 disassembly only: +This is the default mode if no mode is specified. +@item 1 mixed source and disassembly (deprecated): +It is not possible to recreate this mode using @code{--opcodes} and +@code{--source} options. +@item 2 disassembly with raw opcodes: +This mode is equivalent to using @var{mode} 0 and passing +@code{--opcodes bytes} to the command. +@item 3 mixed source and disassembly with raw opcodes (deprecated): +It is not possible to recreate this mode using @code{--opcodes} and +@code{--source} options. +@item 4 mixed source and disassembly: +This mode is equivalent to using @var{mode} 0 and passing +@code{--source} to the command. +@item 5 mixed source and disassembly with raw opcodes: +This mode is equivalent to using @var{mode} 0 and passing +@code{--opcodes bytes} and @code{--source} to the command. @end itemize - Modes 1 and 3 are deprecated. The output is ``source centric'' which hasn't proved useful in practice. @xref{Machine Code}, for a discussion of the difference between @code{/m} and @code{/s} output of the @code{disassemble} command. @end table +The @code{--source} can only be used with @var{mode} 0. Passing this +option will include the source code in the disassembly result as if +@var{mode} 4 or 5 had been used. + @subsubheading Result The result of the @code{-data-disassemble} command will be a list named -@samp{asm_insns}, the contents of this list depend on the @var{mode} -used with the @code{-data-disassemble} command. +@samp{asm_insns}, the contents of this list depend on the options used +with the @code{-data-disassemble} command. -For modes 0 and 2 the @samp{asm_insns} list contains tuples with the -following fields: +For modes 0 and 2, and when the @code{--source} option is not used the +@samp{asm_insns} list contains tuples with the following fields: @table @code @item address @@ -34456,15 +34483,27 @@ The text disassembly for this @samp{address}. @item opcodes -This field is only present for modes 2, 3 and 5. This contains the -raw opcode bytes for the @samp{inst} field. The bytes are formatted -as single bytes, in hex, in ascending address order, with a single -space between each byte. - -@end table - -For modes 1, 3, 4 and 5 the @samp{asm_insns} list contains tuples named -@samp{src_and_asm_line}, each of which has the following fields: +This field is only present for modes 2, 3 and 5, or when the +@code{--opcodes} option @samp{bytes} or @samp{display} is used. This +contains the raw opcode bytes for the @samp{inst} field. + +When the @samp{--opcodes} option is not passed to +@code{-data-disassemble}, or the @samp{bytes} value is passed to +@samp{--opcodes}, then the bytes are formatted as a series of single +bytes, in hex, in ascending address order, with a single space between +each byte. This format is equivalent to the @samp{/b} option being used +with the @kbd{disassemble} command. + +When @samp{--opcodes} is passed the value @samp{display} then the bytes +are formatted in the natural instruction display order. This means +multiple bytes can be grouped together, and the bytes might be +byte-swapped. This format is equivalent to the @samp{/r} option being +used with the @kbd{disassemble} command. +@end table + +For modes 1, 3, 4 and 5, or when the @code{--source} option is used, the +@samp{asm_insns} list contains tuples named @samp{src_and_asm_line}, +each of which has the following fields: @table @code @item line diff --git a/gdb/mi/mi-cmd-disas.c b/gdb/mi/mi-cmd-disas.c index 4f6e5613b9d..db5a78827fc 100644 --- a/gdb/mi/mi-cmd-disas.c +++ b/gdb/mi/mi-cmd-disas.c @@ -68,6 +68,8 @@ mi_cmd_disassemble (const char *command, char **argv, int argc) bool start_seen = false; bool end_seen = false; bool addr_seen = false; + bool opcodes_seen = false; + bool source_seen = false; /* ... and their corresponding value. */ char *file_string = NULL; @@ -77,12 +79,23 @@ mi_cmd_disassemble (const char *command, char **argv, int argc) CORE_ADDR high = 0; CORE_ADDR addr = 0; + /* Flags to handle the --opcodes option. */ + enum opcodes_mode + { + OPCODES_DEFAULT, OPCODES_NONE, OPCODES_DISPLAY, OPCODES_BYTES + }; + enum opcodes_mode opcodes_mode = OPCODES_DEFAULT; + + /* Handle the -source option. */ + bool show_source = false; + /* Options processing stuff. */ int oind = 0; char *oarg; enum opt { - FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT, ADDR_OPT + FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT, ADDR_OPT, OPCODES_OPT, + SHOW_SRC_OPT }; static const struct mi_opt opts[] = { @@ -92,6 +105,8 @@ mi_cmd_disassemble (const char *command, char **argv, int argc) {"s", START_OPT, 1}, {"e", END_OPT, 1}, {"a", ADDR_OPT, 1}, + {"-opcodes", OPCODES_OPT, 1}, + {"-source", SHOW_SRC_OPT, 0}, { 0, 0, 0 } }; @@ -129,6 +144,21 @@ mi_cmd_disassemble (const char *command, char **argv, int argc) addr = parse_and_eval_address (oarg); addr_seen = true; break; + case OPCODES_OPT: + opcodes_seen = true; + if (strcmp (oarg, "none") == 0) + opcodes_mode = OPCODES_NONE; + else if (strcmp (oarg, "display") == 0) + opcodes_mode = OPCODES_DISPLAY; + else if (strcmp (oarg, "bytes") == 0) + opcodes_mode = OPCODES_BYTES; + else + error (_("-data-disassemble: unknown value for -opcodes argument")); + break; + case SHOW_SRC_OPT: + source_seen = true; + show_source = true; + break; } } argv += oind; @@ -146,13 +176,23 @@ mi_cmd_disassemble (const char *command, char **argv, int argc) || (!line_seen && !file_seen && !num_seen && !start_seen && !end_seen && addr_seen)) - || argc != 1) - error (_("-data-disassemble: Usage: ( [-f filename -l linenum " - "[-n howmany]] | [-s startaddr -e endaddr] | [-a addr] ) [--] mode.")); + || argc > 1) + error (_("-data-disassemble: Usage: " + "( -f filename -l linenum [-n howmany] |" + " -s startaddr -e endaddr | -a addr ) " + "[ --opcodes mode ] [ --source ] [ [--] mode ].")); + + if (argc == 1) + { + mode = atoi (argv[0]); + if (mode < 0 || mode > 5) + error (_("-data-disassemble: Mode argument must be in the range 0-5.")); + } + else + mode = 0; - mode = atoi (argv[0]); - if (mode < 0 || mode > 5) - error (_("-data-disassemble: Mode argument must be in the range 0-5.")); + if (mode != 0 && (source_seen || opcodes_seen)) + error (_("-data-disassemble: --opcodes and --source can only be used with mode 0")); /* Convert the mode into a set of disassembly flags. */ @@ -180,6 +220,27 @@ mi_cmd_disassemble (const char *command, char **argv, int argc) gdb_assert_not_reached ("bad disassembly mode"); } + /* Now handle the (optional) --opcodes argument. This partially + overrides the mode value. */ + if (opcodes_mode != OPCODES_DEFAULT) + { + /* Remove any existing flags related to opcodes display. */ + disasm_flags &= ~(DISASSEMBLY_RAW_BYTES | DISASSEMBLY_RAW_INSN); + + /* Add back any required flags. */ + if (opcodes_mode == OPCODES_DISPLAY) + disasm_flags |= DISASSEMBLY_RAW_INSN; + else if (opcodes_mode == OPCODES_BYTES) + disasm_flags |= DISASSEMBLY_RAW_BYTES; + } + + /* Handle the optional --source argument. */ + if (show_source) + { + disasm_flags &= ~DISASSEMBLY_SOURCE_DEPRECATED; + disasm_flags |= DISASSEMBLY_SOURCE; + } + /* We must get the function beginning and end where line_num is contained. */ diff --git a/gdb/testsuite/gdb.mi/mi-disassemble.exp b/gdb/testsuite/gdb.mi/mi-disassemble.exp index ef3337d4062..4d7c2f2e6e7 100644 --- a/gdb/testsuite/gdb.mi/mi-disassemble.exp +++ b/gdb/testsuite/gdb.mi/mi-disassemble.exp @@ -47,21 +47,27 @@ proc test_disassembly_only {} { mi_gdb_test "print/x \$pc" "" "" - mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 0" \ - "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \ - "data-disassemble from pc to pc+12 assembly only" - mi_gdb_test "112-data-disassemble -a \$pc -- 0" \ - "112\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \ - "data-disassemble function around pc assembly only" + foreach { test_name option_string } [list "mode 0" "-- 0" \ + "default mode" "" ] { + with_test_prefix $test_name { + mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" ${option_string}" \ + "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \ + "data-disassemble from pc to pc+12 assembly only" - mi_gdb_test "113-data-disassemble -a callee4 -- 0" \ - "113\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\}.*\]" \ - "data-disassemble function callee4 assembly only" + mi_gdb_test "112-data-disassemble -a \$pc ${option_string}" \ + "112\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \ + "data-disassemble function around pc assembly only" - mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -- 0" \ - "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \ - "data-disassemble file & line, assembly only" + mi_gdb_test "113-data-disassemble -a callee4 ${option_string}" \ + "113\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\}.*\]" \ + "data-disassemble function callee4 assembly only" + + mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body ${option_string}" \ + "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \ + "data-disassemble file & line, assembly only" + } + } } proc test_disassembly_with_opcodes {} { @@ -78,13 +84,20 @@ proc test_disassembly_with_opcodes {} { # -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" + foreach { test_name option_string} [list "mode 2" "-- 2" \ + "mode 0 and --opcodes bytes" "--opcodes bytes -- 0" \ + "default mode and --opcodes bytes" "--opcodes bytes"] { + with_test_prefix $test_name { + mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" ${option_string}" \ + "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" + + mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body ${option_string}" \ + "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" + } + } } proc test_disassembly_lines_limit {} { @@ -230,13 +243,24 @@ proc test_disassembly_bogus_args {} { "data-disassemble bogus address, -a" mi_gdb_test "456-data-disassemble -s \$pc -f basics.c -- 0" \ - "456\\^error,msg=\"-data-disassemble: Usage: \\( .-f filename -l linenum .-n howmany.. \\| .-s startaddr -e endaddr. \\| .-a addr. \\) .--. mode.\"" \ - "data-disassemble mix different args" + "456\\^error,msg=\"-data-disassemble: Usage: \\( -f filename -l linenum .-n howmany. \\| -s startaddr -e endaddr \\| -a addr \\) . --opcodes mode . . --source . . .--. mode .\\.\"" \ + "data-disassemble mix different args" mi_gdb_test "789-data-disassemble -f basics.c -l $line_main_body -- 9" \ "789\\^error,msg=\"-data-disassemble: Mode argument must be in the range 0-5.\"" \ - "data-disassemble wrong mode arg" + "data-disassemble wrong mode arg" + + foreach mode { 1 2 3 4 5 } { + foreach opcode_arg { none bytes display } { + mi_gdb_test "801-data-disassemble -s \$pc -e \"\$pc + 12\" --opcodes ${opcode_arg} -- ${mode}" \ + "801\\^error,msg=\"-data-disassemble: --opcodes and --source can only be used with mode 0\"" \ + "data-disassemble use --opcode ${opcode_arg} with mode ${mode}" + } + mi_gdb_test "802-data-disassemble -s \$pc -e \"\$pc + 12\" --source -- ${mode}" \ + "802\\^error,msg=\"-data-disassemble: --opcodes and --source can only be used with mode 0\"" \ + "data-disassemble use --source with mode ${mode}" + } } # Check the format of the opcode bytes. -- 2.25.4