Patchwork [08/20] Simplify TUI disassembly

login
register
mail settings
Submitter Tom Tromey
Date Sept. 10, 2019, 7:08 p.m.
Message ID <20190910190857.6562-9-tom@tromey.com>
Download mbox | patch
Permalink /patch/34478/
State New
Headers show

Comments

Tom Tromey - Sept. 10, 2019, 7:08 p.m.
This simplifies TUI disassembly somewhat, by removing manual memory
management.

gdb/ChangeLog
2019-09-10  Tom Tromey  <tom@tromey.com>

	* tui/tui-disasm.c (struct tui_asm_line) <addr_string, insn>: 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(-)

Patch

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<tui_asm_line> &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<tui_asm_line> 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<tui_asm_line> 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;
 }