Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 10/13] Use gdb_bfd_sections in restore_command
Date: Fri, 28 Aug 2020 10:23:46 -0600	[thread overview]
Message-ID: <20200828162349.987-11-tom@tromey.com> (raw)
In-Reply-To: <20200828162349.987-1-tom@tromey.com>

This changes restore_command to avoid bfd_map_over_sections, in favor
of iteration.  A helper data structure can also be removed by this
patch.

gdb/ChangeLog
2020-08-28  Tom Tromey  <tom@tromey.com>

	* cli/cli-dump.c (struct callback_data): Remove.
	(restore_one_section): Rename from restore_section_callback.
	Change parameters.
	(restore_binary_file): Change parameters.
	(restore_command): Use foreach.
---
 gdb/ChangeLog      |  8 ++++
 gdb/cli/cli-dump.c | 94 ++++++++++++++++++++++------------------------
 2 files changed, 53 insertions(+), 49 deletions(-)

diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index 567ef2eeded..849964ece6b 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -371,22 +371,14 @@ add_dump_command (const char *name,
     c->doc = concat ("Append ", c->doc + 6, (char *)NULL);
 }
 
-/* Opaque data for restore_section_callback.  */
-struct callback_data {
-  CORE_ADDR load_offset;
-  CORE_ADDR load_start;
-  CORE_ADDR load_end;
-};
-
-/* Function: restore_section_callback.
-
-   Callback function for bfd_map_over_sections.
-   Selectively loads the sections into memory.  */
+/* Selectively loads the sections into memory.  */
 
 static void
-restore_section_callback (bfd *ibfd, asection *isec, void *args)
+restore_one_section (bfd *ibfd, asection *isec,
+		     CORE_ADDR load_offset,
+		     CORE_ADDR load_start,
+		     CORE_ADDR load_end)
 {
-  struct callback_data *data = (struct callback_data *) args;
   bfd_vma sec_start  = bfd_section_vma (isec);
   bfd_size_type size = bfd_section_size (isec);
   bfd_vma sec_end    = sec_start + size;
@@ -399,8 +391,8 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args)
     return;
 
   /* Does the section overlap with the desired restore range? */
-  if (sec_end <= data->load_start 
-      || (data->load_end > 0 && sec_start >= data->load_end))
+  if (sec_end <= load_start 
+      || (load_end > 0 && sec_start >= load_end))
     {
       /* No, no useable data in this section.  */
       printf_filtered (_("skipping section %s...\n"), 
@@ -411,12 +403,12 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args)
   /* Compare section address range with user-requested
      address range (if any).  Compute where the actual
      transfer should start and end.  */
-  if (sec_start < data->load_start)
-    sec_offset = data->load_start - sec_start;
+  if (sec_start < load_start)
+    sec_offset = load_start - sec_start;
   /* Size of a partial transfer.  */
   sec_load_count -= sec_offset;
-  if (data->load_end > 0 && sec_end > data->load_end)
-    sec_load_count -= sec_end - data->load_end;
+  if (load_end > 0 && sec_end > load_end)
+    sec_load_count -= sec_end - load_end;
 
   /* Get the data.  */
   gdb::byte_vector buf (size);
@@ -429,26 +421,28 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args)
 		   (unsigned long) sec_start, 
 		   (unsigned long) sec_end);
 
-  if (data->load_offset != 0 || data->load_start != 0 || data->load_end != 0)
+  if (load_offset != 0 || load_start != 0 || load_end != 0)
     printf_filtered (" into memory (%s to %s)\n",
 		     paddress (target_gdbarch (),
 			       (unsigned long) sec_start
-			       + sec_offset + data->load_offset), 
+			       + sec_offset + load_offset), 
 		     paddress (target_gdbarch (),
 			       (unsigned long) sec_start + sec_offset
-				+ data->load_offset + sec_load_count));
+				+ load_offset + sec_load_count));
   else
     puts_filtered ("\n");
 
   /* Write the data.  */
-  ret = target_write_memory (sec_start + sec_offset + data->load_offset, 
+  ret = target_write_memory (sec_start + sec_offset + load_offset, 
 			     &buf[sec_offset], sec_load_count);
   if (ret != 0)
     warning (_("restore: memory write failed (%s)."), safe_strerror (ret));
 }
 
 static void
-restore_binary_file (const char *filename, struct callback_data *data)
+restore_binary_file (const char *filename, CORE_ADDR load_offset,
+		     CORE_ADDR load_start, CORE_ADDR load_end)
+
 {
   gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB);
   long len;
@@ -466,25 +460,25 @@ restore_binary_file (const char *filename, struct callback_data *data)
   else
     perror_with_name (filename);
 
-  if (len <= data->load_start)
+  if (len <= load_start)
     error (_("Start address is greater than length of binary file %s."), 
 	   filename);
 
   /* Chop off "len" if it exceeds the requested load_end addr.  */
-  if (data->load_end != 0 && data->load_end < len)
-    len = data->load_end;
+  if (load_end != 0 && load_end < len)
+    len = load_end;
   /* Chop off "len" if the requested load_start addr skips some bytes.  */
-  if (data->load_start > 0)
-    len -= data->load_start;
+  if (load_start > 0)
+    len -= load_start;
 
   printf_filtered 
     ("Restoring binary file %s into memory (0x%lx to 0x%lx)\n", 
      filename, 
-     (unsigned long) (data->load_start + data->load_offset),
-     (unsigned long) (data->load_start + data->load_offset + len));
+     (unsigned long) (load_start + load_offset),
+     (unsigned long) (load_start + load_offset + len));
 
   /* Now set the file pos to the requested load start pos.  */
-  if (fseek (file.get (), data->load_start, SEEK_SET) != 0)
+  if (fseek (file.get (), load_start, SEEK_SET) != 0)
     perror_with_name (filename);
 
   /* Now allocate a buffer and read the file contents.  */
@@ -493,8 +487,7 @@ restore_binary_file (const char *filename, struct callback_data *data)
     perror_with_name (filename);
 
   /* Now write the buffer into target memory.  */
-  len = target_write_memory (data->load_start + data->load_offset,
-			     buf.data (), len);
+  len = target_write_memory (load_start + load_offset, buf.data (), len);
   if (len != 0)
     warning (_("restore: memory write failed (%s)."), safe_strerror (len));
 }
@@ -502,15 +495,14 @@ restore_binary_file (const char *filename, struct callback_data *data)
 static void
 restore_command (const char *args, int from_tty)
 {
-  struct callback_data data;
   int binary_flag = 0;
 
   if (!target_has_execution)
     noprocess ();
 
-  data.load_offset = 0;
-  data.load_start  = 0;
-  data.load_end    = 0;
+  CORE_ADDR load_offset = 0;
+  CORE_ADDR load_start  = 0;
+  CORE_ADDR load_end    = 0;
 
   /* Parse the input arguments.  First is filename (required).  */
   gdb::unique_xmalloc_ptr<char> filename = scan_filename (&args, NULL);
@@ -527,19 +519,20 @@ restore_command (const char *args, int from_tty)
 	}
       /* Parse offset (optional).  */
       if (args != NULL && *args != '\0')
-	data.load_offset = binary_flag ?
-	  parse_and_eval_address (scan_expression (&args, NULL).get ()) :
-	  parse_and_eval_long (scan_expression (&args, NULL).get ());
+	load_offset
+	  = (binary_flag
+	     ? parse_and_eval_address (scan_expression (&args, NULL).get ())
+	     : parse_and_eval_long (scan_expression (&args, NULL).get ()));
       if (args != NULL && *args != '\0')
 	{
 	  /* Parse start address (optional).  */
-	  data.load_start = 
+	  load_start = 
 	    parse_and_eval_long (scan_expression (&args, NULL).get ());
 	  if (args != NULL && *args != '\0')
 	    {
 	      /* Parse end address (optional).  */
-	      data.load_end = parse_and_eval_long (args);
-	      if (data.load_end <= data.load_start)
+	      load_end = parse_and_eval_long (args);
+	      if (load_end <= load_start)
 		error (_("Start must be less than end."));
 	    }
 	}
@@ -547,13 +540,14 @@ restore_command (const char *args, int from_tty)
 
   if (info_verbose)
     printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n",
-		     filename.get (), (unsigned long) data.load_offset,
-		     (unsigned long) data.load_start, 
-		     (unsigned long) data.load_end);
+		     filename.get (), (unsigned long) load_offset,
+		     (unsigned long) load_start, 
+		     (unsigned long) load_end);
 
   if (binary_flag)
     {
-      restore_binary_file (filename.get (), &data);
+      restore_binary_file (filename.get (), load_offset, load_start,
+			   load_end);
     }
   else
     {
@@ -561,7 +555,9 @@ restore_command (const char *args, int from_tty)
       gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename.get (), NULL));
 
       /* Process the sections.  */
-      bfd_map_over_sections (ibfd.get (), restore_section_callback, &data);
+      for (asection *sect : gdb_bfd_sections (ibfd))
+	restore_one_section (ibfd.get (), sect, load_offset, load_start,
+			     load_end);
     }
 }
 
-- 
2.17.2



  parent reply	other threads:[~2020-08-28 16:23 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-28 16:23 [PATCH 00/13] Use gdb_bfd_sections in more places Tom Tromey
2020-08-28 16:23 ` [PATCH 01/13] Add a new overload of gdb_bfd_sections Tom Tromey
2020-08-28 16:34   ` Simon Marchi
2020-08-28 19:25     ` Tom Tromey
2020-08-28 16:23 ` [PATCH 02/13] Use gdb_bfd_sections in core_target_open Tom Tromey
2020-08-28 16:23 ` [PATCH 03/13] Use gdb_bfd_sections in gdb_bfd_close_or_warn Tom Tromey
2020-08-28 16:23 ` [PATCH 04/13] Use gdb_bfd_sections in get_stap_base_address Tom Tromey
2020-08-28 17:06   ` Simon Marchi
2020-08-28 16:23 ` [PATCH 05/13] Use gdb_bfd_sections in build_objfile_section_table Tom Tromey
2020-08-28 16:23 ` [PATCH 06/13] Use gdb_bfd_sections in symfile.c Tom Tromey
2020-08-28 17:00   ` Simon Marchi
2020-08-28 17:06     ` Simon Marchi
2020-08-28 16:23 ` [PATCH 07/13] Use gdb_bfd_sections in dwarf2/read.c Tom Tromey
2020-08-28 16:23 ` [PATCH 08/13] Use gdb_bfd_sections in ELF osabi tag sniffing Tom Tromey
2020-08-28 16:23 ` [PATCH 09/13] Use gdb_bfd_sections in gcore_memory_sections Tom Tromey
2020-08-28 16:23 ` Tom Tromey [this message]
2020-08-28 17:12   ` [PATCH 10/13] Use gdb_bfd_sections in restore_command Simon Marchi
2020-08-29 15:47     ` Tom Tromey
2020-08-29 15:49       ` Simon Marchi
2020-08-28 16:23 ` [PATCH 11/13] Use gdb_bfd_sections in elf_symfile_read Tom Tromey
2020-08-28 16:23 ` [PATCH 12/13] Use gdb_bfd_sections in build_section_table Tom Tromey
2020-08-28 16:23 ` [PATCH 13/13] Use gdb_bfd_sections in generic_load Tom Tromey
2020-08-28 17:14 ` [PATCH 00/13] Use gdb_bfd_sections in more places Simon Marchi
2020-08-31 18:19 ` John Baldwin
2020-09-19 18:07 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200828162349.987-11-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox