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.129.124]) by sourceware.org (Postfix) with ESMTPS id 1DC00385842B for ; Mon, 25 Apr 2022 09:15:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1DC00385842B Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-461-f-Flvz6LM8uI1uoUrSzuJw-1; Mon, 25 Apr 2022 05:15:50 -0400 X-MC-Unique: f-Flvz6LM8uI1uoUrSzuJw-1 Received: by mail-wm1-f70.google.com with SMTP id t187-20020a1c46c4000000b0038ebc45dbfcso6518416wma.2 for ; Mon, 25 Apr 2022 02:15:50 -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=9B3Uks0s11jtnyMcZzZ9ge6WfGBQQ0egazI8ZMkagsQ=; b=Kf92Us7DiuN/TwfssPstzA4BVpfUCTMg6KXOmDmeZmNdqiPzrah8DN8wbPioj6zuUv lT4ACLbxSPu1zRcrT5THd88t7X+TR9IsixWARynXIN/N+YblJA5q9ldUX0cXkFoxJcP4 si2cLMpskHrBl9z+NnzJkOBH/HLRrBIdSRofMmKwdKEP+fnGPyEGzWDfH6LIoYdGDtq+ bxW1l4mG7FPFZ4cS9WHreoO3dP7RebsCfHKtlJmfWZD6u0tmrUEJRi36Rwt0o2sBYkaj zNQigiNoGQVzH74kj1D4ZDCFjiljPvD4oIrevmJ9lL12nUCV+YAcMHGQRKc2s5FomWmo K9rQ== X-Gm-Message-State: AOAM533Xvd1VM14quJxC5WU+3tVN14oG4i4TfHPTUGDkynw9hEglVOYH +sl236Y8n1MeG94AoHjyUMhXZGqWA4g/qKyuzI/y8IvDI0IW3mwf0mtSNmY/SVfz8wJS+6xRjlZ T7ILTnuSzqR2n2kZk0TcJokBjZcpWb+N3iaEeKiLFvi/0k0Pl37T/6696lvSAOISOdE0TFfV7jw == X-Received: by 2002:adf:e3c1:0:b0:20a:aba9:9b38 with SMTP id k1-20020adfe3c1000000b0020aaba99b38mr12901622wrm.673.1650878149007; Mon, 25 Apr 2022 02:15:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzW2Bhj3WcuJ38pG/kGL5vPUnXQiho96RSDJRr957UP7d0FKbtGvTlEY4B+NaQGlhQ7K4GG+A== X-Received: by 2002:adf:e3c1:0:b0:20a:aba9:9b38 with SMTP id k1-20020adfe3c1000000b0020aaba99b38mr12901602wrm.673.1650878148658; Mon, 25 Apr 2022 02:15:48 -0700 (PDT) Received: from localhost (host81-136-113-48.range81-136.btcentralplus.com. [81.136.113.48]) by smtp.gmail.com with ESMTPSA id l14-20020adffe8e000000b00207af9cdd90sm8079887wrr.39.2022.04.25.02.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Apr 2022 02:15:47 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv4 2/5] gdb: add extension language print_insn hook Date: Mon, 25 Apr 2022 10:15:38 +0100 Message-Id: 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: application/octet-stream; x-default=true X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Mon, 25 Apr 2022 09:15:53 -0000 From: Andrew Burgess This commit is setup for the next commit. In the next commit I will add a Python API to intercept the print_insn calls within GDB, each print_insn call is responsible for disassembling, and printing one instruction. After the next commit it will be possible for a user to write Python code that either wraps around the existing disassembler, or even, in extreme situations, entirely replaces the existing disassembler. This commit does not add any new Python API. What this commit does is put the extension language framework in place for a print_insn hook. There's a new callback added to 'struct extension_language_ops', which is then filled in with nullptr for Python and Guile. Finally, in the disassembler, the code is restructured so that the new extension language function ext_lang_print_insn is called before we delegate to gdbarch_print_insn. After this, the next commit can focus entirely on providing a Python implementation of the new print_insn callback. There should be no user visible change after this commit. --- gdb/disasm.c | 29 ++++++++++++++++++++++++++--- gdb/extension-priv.h | 15 +++++++++++++++ gdb/extension.c | 20 ++++++++++++++++++++ gdb/extension.h | 17 +++++++++++++++++ gdb/guile/guile.c | 6 +++++- gdb/python/python.c | 2 ++ 6 files changed, 85 insertions(+), 4 deletions(-) diff --git a/gdb/disasm.c b/gdb/disasm.c index 3f55e12665b..16e3c39b702 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -844,6 +844,29 @@ gdb_disassemble_info::~gdb_disassemble_info () disassemble_free_target (&m_di); } +/* Wrapper around calling gdbarch_print_insn. This function takes care of + first calling the extension language hooks for print_insn, and, if none + of the extension languages can print this instruction, calls + gdbarch_print_insn to do the work. + + GDBARCH is the architecture to disassemble in, VMA is the address of the + instruction being disassembled, and INFO is the libopcodes disassembler + related information. */ + +static int +gdb_print_insn_1 (struct gdbarch *gdbarch, CORE_ADDR vma, + struct disassemble_info *info) +{ + /* Call into the extension languages to do the disassembly. */ + gdb::optional length = ext_lang_print_insn (gdbarch, vma, info); + if (length.has_value ()) + return *length; + + /* No extension language wanted to do the disassembly, so do it + manually. */ + return gdbarch_print_insn (gdbarch, vma, info); +} + /* See disasm.h. */ bool gdb_disassembler::use_ext_lang_colorization_p = true; @@ -857,7 +880,7 @@ gdb_disassembler::print_insn (CORE_ADDR memaddr, m_err_memaddr.reset (); m_buffer.clear (); - int length = gdbarch_print_insn (arch (), memaddr, &m_di); + int length = gdb_print_insn_1 (arch (), memaddr, &m_di); /* If we have successfully disassembled an instruction, styling is on, we think that the extension language might be able to perform styling for @@ -892,7 +915,7 @@ gdb_disassembler::print_insn (CORE_ADDR memaddr, gdb_assert (!m_buffer.term_out ()); m_buffer.~string_file (); new (&m_buffer) string_file (true); - length = gdbarch_print_insn (arch (), memaddr, &m_di); + length = gdb_print_insn_1 (arch (), memaddr, &m_di); gdb_assert (length > 0); } } @@ -1047,7 +1070,7 @@ gdb_buffered_insn_length (struct gdbarch *gdbarch, gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr, &disassembler_options_holder); - int result = gdbarch_print_insn (gdbarch, addr, &di); + int result = gdb_print_insn_1 (gdbarch, addr, &di); disassemble_free_target (&di); return result; } diff --git a/gdb/extension-priv.h b/gdb/extension-priv.h index d9450b51231..7c74e721c57 100644 --- a/gdb/extension-priv.h +++ b/gdb/extension-priv.h @@ -263,6 +263,21 @@ struct extension_language_ops contents, or an empty optional. */ gdb::optional (*colorize_disasm) (const std::string &content, gdbarch *gdbarch); + + /* Print a single instruction from ADDRESS in architecture GDBARCH. INFO + is the standard libopcodes disassembler_info structure. Bytes for the + instruction being printed should be read using INFO->read_memory_func + as the actual instruction bytes might be in a buffer. + + Use INFO->fprintf_func to print the results of the disassembly, and + return the length of the instruction. + + If no instruction can be disassembled then return an empty value and + other extension languages will get a chance to perform the + disassembly. */ + gdb::optional (*print_insn) (struct gdbarch *gdbarch, + CORE_ADDR address, + struct disassemble_info *info); }; /* State necessary to restore a signal handler to its previous value. */ diff --git a/gdb/extension.c b/gdb/extension.c index 8f39b86e952..62f41c6445d 100644 --- a/gdb/extension.c +++ b/gdb/extension.c @@ -924,6 +924,26 @@ ext_lang_colorize_disasm (const std::string &content, gdbarch *gdbarch) return result; } +/* See extension.h. */ + +gdb::optional +ext_lang_print_insn (struct gdbarch *gdbarch, CORE_ADDR address, + struct disassemble_info *info) +{ + for (const struct extension_language_defn *extlang : extension_languages) + { + if (extlang->ops == nullptr + || extlang->ops->print_insn == nullptr) + continue; + gdb::optional length + (extlang->ops->print_insn (gdbarch, address, info)); + if (length.has_value ()) + return length; + } + + return {}; +} + /* Called via an observer before gdb prints its prompt. Iterate over the extension languages giving them a chance to change the prompt. The first one to change the prompt wins, diff --git a/gdb/extension.h b/gdb/extension.h index 7eb89530c44..f7518f91b35 100644 --- a/gdb/extension.h +++ b/gdb/extension.h @@ -327,6 +327,23 @@ extern gdb::optional ext_lang_colorize extern gdb::optional ext_lang_colorize_disasm (const std::string &content, gdbarch *gdbarch); +/* Try to disassemble a single instruction. ADDRESS is the address that + the instructions apparent address, though bytes for the instruction + should be read by calling INFO->read_memory_func as we might be + disassembling out of a buffer. GDBARCH is the architecture in which we + are performing the disassembly. + + The disassembled instruction should be printed by calling + INFO->fprintf_func, and the length (in octets) of the disassembled + instruction should be returned. + + If no instruction could be disassembled then an empty value is returned + and GDB will call gdbarch_print_insn to perform the disassembly + itself. */ + +extern gdb::optional ext_lang_print_insn + (struct gdbarch *gdbarch, CORE_ADDR address, struct disassemble_info *info); + #if GDB_SELF_TEST namespace selftests { extern void (*hook_set_active_ext_lang) (); diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c index c7be48fb739..14b191ded62 100644 --- a/gdb/guile/guile.c +++ b/gdb/guile/guile.c @@ -130,8 +130,12 @@ static const struct extension_language_ops guile_extension_ops = gdbscm_breakpoint_has_cond, gdbscm_breakpoint_cond_says_stop, - NULL, /* gdbscm_check_quit_flag, */ NULL, /* gdbscm_set_quit_flag, */ + NULL, /* gdbscm_check_quit_flag, */ + NULL, /* gdbscm_before_prompt, */ + NULL, /* gdbscm_get_matching_xmethod_workers */ + NULL, /* gdbscm_colorize */ + NULL, /* gdbscm_print_insn */ }; #endif diff --git a/gdb/python/python.c b/gdb/python/python.c index 11aaa7ae778..b5b8379e23c 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -166,6 +166,8 @@ static const struct extension_language_ops python_extension_ops = gdbpy_colorize, gdbpy_colorize_disasm, + + NULL, /* gdbpy_print_insn, */ }; #endif /* HAVE_PYTHON */ -- 2.25.4