From patchwork Wed Feb 1 23:50:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 19088 Received: (qmail 30911 invoked by alias); 1 Feb 2017 23:50:42 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 30898 invoked by uid 89); 1 Feb 2017 23:50:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=sk:opcode_, H*i:sk:3d886f4, UD:opcode_stream.printf, UD:printf X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 01 Feb 2017 23:50:39 +0000 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5EAB180F90; Wed, 1 Feb 2017 23:50:39 +0000 (UTC) Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A0CA8899DF; Wed, 1 Feb 2017 23:50:38 +0000 (UTC) Subject: Re: [PATCH v4 1/2] Add back gdb_pretty_print_insn To: Simon Marchi References: <1485909045-30285-1-git-send-email-palves@redhat.com> <1485909045-30285-2-git-send-email-palves@redhat.com> <31c02e39-d45f-8b65-2ff5-c21582d0f43d@redhat.com> <3d886f4f-ba53-4ef6-3767-a7fe8a17ba85@redhat.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: <0c624f14-cd12-289d-a0a5-6cd56b79a376@redhat.com> Date: Wed, 1 Feb 2017 23:50:36 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <3d886f4f-ba53-4ef6-3767-a7fe8a17ba85@redhat.com> On 02/01/2017 08:31 PM, Pedro Alves wrote: > reuse both. Which suggests creating a new struct to hold > whatever should survive across calls. And then we can > go the step further, and make that a class, with > pretty_print_insn a method of _that_ class instead of > of gdb_disassembler. Like: > > I can try that, but I think I'd rather do it after > string_file is in already. Here's what it looks like, on top of the v4 series. From d50ad8e4bbfee8d4dff37c1bcf254e4afbbe0512 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Wed, 1 Feb 2017 23:14:00 +0000 Subject: [PATCH] gdb_pretty_print_disassembler --- gdb/disasm.c | 25 ++++++++++++++----------- gdb/disasm.h | 31 +++++++++++++++++++++++++++---- gdb/record-btrace.c | 4 +++- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/gdb/disasm.c b/gdb/disasm.c index 92bca99..25b9105 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -182,9 +182,9 @@ compare_lines (const void *mle1p, const void *mle2p) /* See disasm.h. */ int -gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout, - const struct disasm_insn *insn, - int flags) +gdb_pretty_print_disassembler::pretty_print_insn (struct ui_out *uiout, + const struct disasm_insn *insn, + int flags) { /* parts of the symbolic representation of the address */ int unmapped; @@ -195,6 +195,7 @@ gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout, char *filename = NULL; char *name = NULL; CORE_ADDR pc; + struct gdbarch *gdbarch = arch (); ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); pc = insn->addr; @@ -248,7 +249,7 @@ gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout, if (name != NULL) xfree (name); - string_file stb; + m_insn_stb.clear (); if (flags & DISASSEMBLY_RAW_INSN) { @@ -259,25 +260,25 @@ gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout, /* Build the opcodes using a temporary stream so we can write them out in a single go for the MI. */ - string_file opcode_stream; + m_opcode_stb.clear (); - size = gdb_print_insn (gdbarch, pc, &stb, NULL); + size = m_di.print_insn (pc); end_pc = pc + size; for (;pc < end_pc; ++pc) { read_code (pc, &data, 1); - opcode_stream.printf ("%s%02x", spacer, (unsigned) data); + m_opcode_stb.printf ("%s%02x", spacer, (unsigned) data); spacer = " "; } - uiout->field_stream ("opcodes", opcode_stream); + uiout->field_stream ("opcodes", m_opcode_stb); uiout->text ("\t"); } else - size = gdb_print_insn (gdbarch, pc, &stb, NULL); + size = m_di.print_insn (pc); - uiout->field_stream ("inst", stb); + uiout->field_stream ("inst", m_insn_stb); do_cleanups (ui_out_chain); uiout->text ("\n"); @@ -295,11 +296,13 @@ dump_insns (struct gdbarch *gdbarch, memset (&insn, 0, sizeof (insn)); insn.addr = low; + gdb_pretty_print_disassembler disasm (gdbarch); + while (insn.addr < high && (how_many < 0 || num_displayed < how_many)) { int size; - size = gdb_pretty_print_insn (gdbarch, uiout, &insn, flags); + size = disasm.pretty_print_insn (uiout, &insn, flags); if (size <= 0) break; diff --git a/gdb/disasm.h b/gdb/disasm.h index dffe9cd..a03eba6 100644 --- a/gdb/disasm.h +++ b/gdb/disasm.h @@ -97,11 +97,34 @@ extern void gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout, extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr, struct ui_file *stream, int *branch_delay_insns); -/* Prints the instruction INSN into UIOUT and returns the length of - the printed instruction in bytes. */ +/* Class used to pretty-print an instruction. */ -extern int gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout, - const struct disasm_insn *insn, int flags); +struct gdb_pretty_print_disassembler +{ +public: + gdb_pretty_print_disassembler (struct gdbarch *gdbarch) + : m_di (gdbarch, &m_insn_stb) + {} + + /* Prints the instruction INSN into UIOUT and returns the length of + the printed instruction in bytes. */ + int pretty_print_insn (struct ui_out *uiout, const struct disasm_insn *insn, + int flags); + + /* Returns the architecture used for disassembling. */ + struct gdbarch *arch () { return m_di.arch (); } + +private: + /* The disassembler used for instruction printing. */ + gdb_disassembler m_di; + + /* The buffer used to build the instruction string. The + disassembler is initialized with this stream. */ + string_file m_insn_stb; + + /* The buffer used to build the raw opcodes string. */ + string_file m_opcode_stb; +}; /* Return the length in bytes of the instruction at address MEMADDR in debugged memory. */ diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c index 80b8aff..6f8790f 100644 --- a/gdb/record-btrace.c +++ b/gdb/record-btrace.c @@ -715,6 +715,8 @@ btrace_insn_history (struct ui_out *uiout, instructions corresponding to that line. */ ui_item_chain = NULL; + gdb_pretty_print_disassembler disasm (gdbarch); + for (it = *begin; btrace_insn_cmp (&it, end) != 0; btrace_insn_next (&it, 1)) { const struct btrace_insn *insn; @@ -768,7 +770,7 @@ btrace_insn_history (struct ui_out *uiout, if ((insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) != 0) dinsn.is_speculative = 1; - gdb_pretty_print_insn (gdbarch, uiout, &dinsn, flags); + disasm.pretty_print_insn (uiout, &dinsn, flags); } }