From patchwork Tue Sep 10 19:08:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 34478 Received: (qmail 25925 invoked by alias); 10 Sep 2019 19:09:10 -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 25507 invoked by uid 89); 10 Sep 2019 19:09:07 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy= X-HELO: gateway22.websitewelcome.com Received: from gateway22.websitewelcome.com (HELO gateway22.websitewelcome.com) (192.185.47.129) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 10 Sep 2019 19:09:04 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway22.websitewelcome.com (Postfix) with ESMTP id A3D71BE2E for ; Tue, 10 Sep 2019 14:09:02 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 7lVeiochI2qH77lVeiinww; Tue, 10 Sep 2019 14:09:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=uCxU40hEpCV8E1JtfNqmZhXJWH8aUg0h3mgN/QXtPuo=; b=GY1ag3dzSHo8AxNWr7S6d0yuO0 aWtXcWR9zA2aWfCdQ4EyW1EQVZ9css76uhmO3vR36Dlq9Zi71PsryJULqFeOAKeb6HeKkKu1+2ehd MQASlFPAIhomm1HoIDPmfaoe6; Received: from 71-218-73-27.hlrn.qwest.net ([71.218.73.27]:51786 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1i7lVe-000EYF-CO; Tue, 10 Sep 2019 14:09:02 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 08/20] Simplify TUI disassembly Date: Tue, 10 Sep 2019 13:08:45 -0600 Message-Id: <20190910190857.6562-9-tom@tromey.com> In-Reply-To: <20190910190857.6562-1-tom@tromey.com> References: <20190910190857.6562-1-tom@tromey.com> This simplifies TUI disassembly somewhat, by removing manual memory management. gdb/ChangeLog 2019-09-10 Tom Tromey * tui/tui-disasm.c (struct tui_asm_line) : Now std::string. (tui_disassemble): Add "pos" parameter. (tui_disasm_window::set_contents): Simplify. --- gdb/ChangeLog | 7 +++++ gdb/tui/tui-disasm.c | 71 ++++++++++++++++---------------------------- 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c index 2a331327e77..53ea061afb6 100644 --- a/gdb/tui/tui-disasm.c +++ b/gdb/tui/tui-disasm.c @@ -44,34 +44,32 @@ struct tui_asm_line { CORE_ADDR addr; - char *addr_string; - char *insn; + std::string addr_string; + std::string insn; }; /* Function to set the disassembly window's content. Disassemble count lines starting at pc. Return address of the count'th instruction after pc. */ static CORE_ADDR -tui_disassemble (struct gdbarch *gdbarch, struct tui_asm_line *asm_lines, - CORE_ADDR pc, int count) +tui_disassemble (struct gdbarch *gdbarch, + std::vector &asm_lines, + CORE_ADDR pc, int pos, int count) { string_file gdb_dis_out; /* Now construct each line. */ - for (; count > 0; count--, asm_lines++) + for (int i = 0; i < count; ++i) { - xfree (asm_lines->addr_string); - xfree (asm_lines->insn); - print_address (gdbarch, pc, &gdb_dis_out); - asm_lines->addr = pc; - asm_lines->addr_string = xstrdup (gdb_dis_out.c_str ()); + asm_lines[pos + i].addr = pc; + asm_lines[pos + i].addr_string = std::move (gdb_dis_out.string ()); gdb_dis_out.clear (); pc = pc + gdb_print_insn (gdbarch, pc, &gdb_dis_out, NULL); - asm_lines->insn = xstrdup (gdb_dis_out.c_str ()); + asm_lines[pos + i].insn = std::move (gdb_dis_out.string ()); /* Reset the buffer to empty. */ gdb_dis_out.clear (); @@ -87,20 +85,17 @@ tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from) { CORE_ADDR new_low; int max_lines; - int i; - struct tui_asm_line *asm_lines; max_lines = (from > 0) ? from : - from; if (max_lines <= 1) - return pc; + return pc; - asm_lines = XALLOCAVEC (struct tui_asm_line, max_lines); - memset (asm_lines, 0, sizeof (struct tui_asm_line) * max_lines); + std::vector asm_lines (max_lines); new_low = pc; if (from > 0) { - tui_disassemble (gdbarch, asm_lines, pc, max_lines); + tui_disassemble (gdbarch, asm_lines, pc, 0, max_lines); new_low = asm_lines[max_lines - 1].addr; } else @@ -122,7 +117,7 @@ tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from) else new_low += 1 * max_lines; - tui_disassemble (gdbarch, asm_lines, new_low, max_lines); + tui_disassemble (gdbarch, asm_lines, new_low, 0, max_lines); last_addr = asm_lines[pos].addr; } while (last_addr > pc && msymbol.minsym); @@ -139,8 +134,8 @@ tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from) if (pos >= max_lines) pos = 0; - next_addr = tui_disassemble (gdbarch, &asm_lines[pos], - last_addr, 1); + next_addr = tui_disassemble (gdbarch, asm_lines, + last_addr, pos, 1); /* If there are some problems while disassembling exit. */ if (next_addr <= last_addr) @@ -152,11 +147,6 @@ tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from) pos = 0; new_low = asm_lines[pos].addr; } - for (i = 0; i < max_lines; i++) - { - xfree (asm_lines[i].addr_string); - xfree (asm_lines[i].insn); - } return new_low; } @@ -172,10 +162,8 @@ tui_disasm_window::set_contents (struct gdbarch *arch, CORE_ADDR cur_pc; struct tui_locator_window *locator = tui_locator_win_info_ptr (); int tab_len = tui_tab_width; - struct tui_asm_line *asm_lines; int insn_pos; int addr_size, insn_size; - char *line; gdb_assert (line_or_addr.loa == LOA_ADDRESS); CORE_ADDR pc = line_or_addr.u.addr; @@ -192,22 +180,21 @@ tui_disasm_window::set_contents (struct gdbarch *arch, line_width = width - TUI_EXECINFO_SIZE - 2; /* Get temporary table that will hold all strings (addr & insn). */ - asm_lines = XALLOCAVEC (struct tui_asm_line, max_lines); - memset (asm_lines, 0, sizeof (struct tui_asm_line) * max_lines); + std::vector asm_lines (max_lines); - tui_disassemble (gdbarch, asm_lines, pc, max_lines); + tui_disassemble (gdbarch, asm_lines, pc, 0, max_lines); /* Determine maximum address- and instruction lengths. */ addr_size = 0; insn_size = 0; for (i = 0; i < max_lines; i++) { - size_t len = strlen (asm_lines[i].addr_string); + size_t len = asm_lines[i].addr_string.size (); if (len > addr_size) addr_size = len; - len = strlen (asm_lines[i].insn); + len = asm_lines[i].insn.size (); if (len > insn_size) insn_size = len; } @@ -215,23 +202,20 @@ tui_disasm_window::set_contents (struct gdbarch *arch, /* Align instructions to the same column. */ insn_pos = (1 + (addr_size / tab_len)) * tab_len; - /* Allocate memory to create each line. */ - line = (char*) alloca (insn_pos + insn_size + 1); - /* Now construct each line. */ content.resize (max_lines); for (i = 0; i < max_lines; i++) { - int cur_len; - tui_source_element *src = &content[i]; - strcpy (line, asm_lines[i].addr_string); - cur_len = strlen (line); - memset (line + cur_len, ' ', insn_pos - cur_len); - strcpy (line + insn_pos, asm_lines[i].insn); + + std::string line + = (asm_lines[i].addr_string + + n_spaces (insn_pos + - asm_lines[i].addr_string.size ()) + + asm_lines[i].insn); /* Now copy the line taking the offset into account. */ - if (strlen (line) > offset) + if (line.size() > offset) src->line.reset (xstrndup (&line[offset], line_width)); else src->line.reset (xstrdup ("")); @@ -239,9 +223,6 @@ tui_disasm_window::set_contents (struct gdbarch *arch, src->line_or_addr.loa = LOA_ADDRESS; src->line_or_addr.u.addr = asm_lines[i].addr; src->is_exec_point = asm_lines[i].addr == cur_pc; - - xfree (asm_lines[i].addr_string); - xfree (asm_lines[i].insn); } return TUI_SUCCESS; }