Add command to erase all flash memory regions
authorLuis Machado <lgustavo@codesourcery.com>
Fri, 20 Jan 2017 14:13:03 +0000 (08:13 -0600)
committerLuis Machado <lgustavo@codesourcery.com>
Fri, 20 Jan 2017 14:13:03 +0000 (08:13 -0600)
Changes in v4:

- Replaced phex call with hex_string.

Changes in v3:

- Addressed comments by Pedro.
- Output of memory region size now in hex format.
- Misc formatting fixups.
- Addressed Simon's comments on formatting.
- Adjusted command text in the manual entry.
- Fixed up ChangeLog.
- Renamed flash_erase_all_command to flash_erase_command.

Changes in v2:

- Added NEWS entry.
- Fixed long lines.
- Address printing with paddress.

Years ago we contributed flash programming patches upstream.  The following
patch is a leftover one that complements that functionality by adding a new
command to erase all reported flash memory blocks.

The command is most useful when we're dealing with flash-enabled targets
(mostly bare-metal) and we need to reset the board for some reason.

The wiping out of flash memory regions should help the target come up with a
known clean state from which the user can load a new image and resume
debugging. It is convenient enough to do this from the debugger, and there is
also an MI command to expose this functionality to the IDE's.

gdb/doc/ChangeLog:

2017-01-20  Mike Wrighton  <mike_wrighton@codesourcery.com>
    Luis Machado  <lgustavo@codesourcery.com>

* gdb.texinfo (-target-flash-erase): New MI command description.
(flash-erase): New CLI command description.

gdb/ChangeLog:

2017-01-20  Mike Wrighton  <mike_wrighton@codesourcery.com>
    Luis Machado  <lgustavo@codesourcery.com>

* NEWS (New commands): Mention flash-erase.
(New MI commands): Mention target-flash-erase.
* mi/mi-cmds.c (mi_cmd_target_flash_erase): Add target-flash-erase MI
command.
* mi/mi-cmds.h (mi_cmd_target_flash_erase): New declaration.
* mi/mi-main.c (mi_cmd_target_flash_erase): New function.
* target.c (flash_erase_command): New function.
(initialize_targets): Add new flash-erase command.
* target.h (flash_erase_command): New declaration.

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/mi/mi-cmds.c
gdb/mi/mi-cmds.h
gdb/mi/mi-main.c
gdb/target.c
gdb/target.h

index fd42635f3e5f869a8b4b20c1ca338f4543b20b98..1aef3f5dacc16fac1de99f6811919ad04d6857f0 100644 (file)
@@ -1,3 +1,16 @@
+2017-01-20  Mike Wrighton  <mike_wrighton@codesourcery.com>
+           Luis Machado  <lgustavo@codesourcery.com>
+
+       * NEWS (New commands): Mention flash-erase.
+       (New MI commands): Mention target-flash-erase.
+       * mi/mi-cmds.c (mi_cmd_target_flash_erase): Add target-flash-erase MI
+       command.
+       * mi/mi-cmds.h (mi_cmd_target_flash_erase): New declaration.
+       * mi/mi-main.c (mi_cmd_target_flash_erase): New function.
+       * target.c (flash_erase_command): New function.
+       (initialize_targets): Add new flash-erase command.
+       * target.h (flash_erase_command): New declaration.
+
 2017-01-20  Joel Brobecker  <brobecker@adacore.com>
 
        * nat/linux-ptrace.c: Only include <sys/procfs.h> if
index b9768159cd2371471d2e91e814d6e4309f48db9b..21e8cd31b024fa8c267059187a054047bc06c685 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -57,6 +57,17 @@ FreeBSD/mips                 mips*-*-freebsd
 Synopsys ARC                   arc*-*-elf32
 FreeBSD/mips                   mips*-*-freebsd
 
+* New commands
+
+flash-erase
+  Erases all the flash memory regions reported by the target.
+
+* New MI commands
+
+-target-flash-erase
+  Erases all the flash memory regions reported by the target.  This is
+  equivalent to the CLI command flash-erase.
+
 *** Changes in GDB 7.12
 
 * GDB and GDBserver now build with a C++ compiler by default.
index 4c6e718ae181b092506fe603a9e3d17b48453e00..4d69e36eb6a62a95bb0d426feb154491d42ab2c1 100644 (file)
@@ -1,3 +1,9 @@
+2017-01-20  Mike Wrighton  <mike_wrighton@codesourcery.com>
+           Luis Machado  <lgustavo@codesourcery.com>
+
+       * gdb.texinfo (-target-flash-erase): New MI command description.
+       (flash-erase): New CLI command description.
+
 2017-01-04  John Baldwin  <jhb@FreeBSD.org>
 
        * gdb.texinfo (Contributors): Add SRI International and University
index 2b6b6547c5072530d66e63ac6902d09c7c300946..dcca91bd688c8993cc2f825cf15fc7d9c6b4c484 100644 (file)
@@ -19622,6 +19622,16 @@ load programs into flash memory.
 @code{load} does not repeat if you press @key{RET} again after using it.
 @end table
 
+@table @code
+
+@kindex flash-erase
+@item flash-erase
+@anchor{flash-erase}
+
+Erases all known flash memory regions on the target.
+
+@end table
+
 @node Byte Order
 @section Choosing Target Byte Order
 
@@ -31859,6 +31869,28 @@ No equivalent.
 @subsubheading Example
 N.A.
 
+@subheading The @code{-target-flash-erase} Command
+@findex -target-flash-erase
+
+@subsubheading Synopsis
+
+@smallexample
+ -target-flash-erase
+@end smallexample
+
+Erases all known flash memory regions on the target.
+
+The corresponding @value{GDBN} command is @samp{flash-erase}.
+
+The output is a list of flash regions that have been erased, with starting
+addresses and memory region sizes.
+
+@smallexample
+(gdb)
+-target-flash-erase
+^done,erased-regions=@{address="0x0",size="0x40000"@}
+(gdb)
+@end smallexample
 
 @subheading The @code{-target-select} Command
 @findex -target-select
index cdea008cb1906e8d81bf6b3022fb4e58b7439353..abb70bde83dc965a813c9de84129cfc222880e5c 100644 (file)
@@ -147,6 +147,7 @@ static struct mi_cmd mi_cmds[] =
   DEF_MI_CMD_MI ("target-file-delete", mi_cmd_target_file_delete),
   DEF_MI_CMD_MI ("target-file-get", mi_cmd_target_file_get),
   DEF_MI_CMD_MI ("target-file-put", mi_cmd_target_file_put),
+  DEF_MI_CMD_MI ("target-flash-erase", mi_cmd_target_flash_erase),
   DEF_MI_CMD_CLI ("target-select", "target", 1),
   DEF_MI_CMD_MI ("thread-info", mi_cmd_thread_info),
   DEF_MI_CMD_MI ("thread-list-ids", mi_cmd_thread_list_ids),
index 8bd947bc7524f3aa8f1b869fbf60ae52f9a905fd..d0906e6b6c8b2baafb4bcb33938d7a6d304adc1b 100644 (file)
@@ -93,6 +93,7 @@ extern mi_cmd_argv_ftype mi_cmd_target_detach;
 extern mi_cmd_argv_ftype mi_cmd_target_file_get;
 extern mi_cmd_argv_ftype mi_cmd_target_file_put;
 extern mi_cmd_argv_ftype mi_cmd_target_file_delete;
+extern mi_cmd_argv_ftype mi_cmd_target_flash_erase;
 extern mi_cmd_argv_ftype mi_cmd_thread_info;
 extern mi_cmd_argv_ftype mi_cmd_thread_list_ids;
 extern mi_cmd_argv_ftype mi_cmd_thread_select;
index 22803cb55694a835f59368d69bd0c8a389d778db..57c23ebf5d6b2d3b398aa40ebd9b3cb70c56125c 100644 (file)
@@ -552,6 +552,12 @@ mi_cmd_target_detach (char *command, char **argv, int argc)
   detach_command (NULL, 0);
 }
 
+void
+mi_cmd_target_flash_erase (char *command, char **argv, int argc)
+{
+  flash_erase_command (NULL, 0);
+}
+
 void
 mi_cmd_thread_select (char *command, char **argv, int argc)
 {
index be7367c4c940e0ee5889cd00193ecb9e9de01b6b..3c409f0f619141205dfdcbbf8e46a277585ed683 100644 (file)
@@ -3943,6 +3943,52 @@ do_monitor_command (char *cmd,
   target_rcmd (cmd, gdb_stdtarg);
 }
 
+/* Erases all the memory regions marked as flash.  CMD and FROM_TTY are
+   ignored.  */
+
+void
+flash_erase_command (char *cmd, int from_tty)
+{
+  /* Used to communicate termination of flash operations to the target.  */
+  bool found_flash_region = false;
+  struct mem_region *m;
+  struct gdbarch *gdbarch = target_gdbarch ();
+
+  VEC(mem_region_s) *mem_regions = target_memory_map ();
+
+  /* Iterate over all memory regions.  */
+  for (int i = 0; VEC_iterate (mem_region_s, mem_regions, i, m); i++)
+    {
+      /* Fetch the memory attribute.  */
+      struct mem_attrib *attrib = &m->attrib;
+
+      /* Is this a flash memory region?  */
+      if (attrib->mode == MEM_FLASH)
+        {
+          found_flash_region = true;
+          target_flash_erase (m->lo, m->hi - m->lo);
+
+         struct cleanup *cleanup_tuple
+             = make_cleanup_ui_out_tuple_begin_end (current_uiout,
+                                                    "erased-regions");
+
+          current_uiout->message (_("Erasing flash memory region at address "));
+          current_uiout->field_fmt ("address", "%s", paddress (gdbarch,
+                                                                m->lo));
+          current_uiout->message (", size = ");
+          current_uiout->field_fmt ("size", "%s", hex_string (m->hi - m->lo));
+          current_uiout->message ("\n");
+          do_cleanups (cleanup_tuple);
+        }
+    }
+
+  /* Did we do any flash operations?  If so, we need to finalize them.  */
+  if (found_flash_region)
+    target_flash_done ();
+  else
+    current_uiout->message (_("No flash memory regions found.\n"));
+}
+
 /* Print the name of each layers of our target stack.  */
 
 static void
@@ -4233,6 +4279,9 @@ Otherwise, any attempt to interrupt or stop will be ignored."),
                           set_target_permissions, NULL,
                           &setlist, &showlist);
 
+  add_com ("flash-erase", no_class, flash_erase_command,
+           _("Erase all flash memory regions."));
+
   add_setshow_boolean_cmd ("auto-connect-native-target", class_support,
                           &auto_connect_native_target, _("\
 Set whether GDB may automatically connect to the native target."), _("\
index f2b9181ffcd77068d6d7c6f40cd535b740859549..8df117ece2752189db70d271a4a001377d6813f6 100644 (file)
@@ -1451,6 +1451,9 @@ extern int target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr,
    is returned.  */
 VEC(mem_region_s) *target_memory_map (void);
 
+/* Erases all flash memory regions on the target.  */
+void flash_erase_command (char *cmd, int from_tty);
+
 /* Erase the specified flash region.  */
 void target_flash_erase (ULONGEST address, LONGEST length);
 
This page took 0.060157 seconds and 4 git commands to generate.