Patchwork [04/40] target_ops/C++: core target

login
register
mail settings
Submitter Pedro Alves
Date April 14, 2018, 7:09 p.m.
Message ID <20180414190953.24481-5-palves@redhat.com>
Download mbox | patch
Permalink /patch/26732/
State New
Headers show

Comments

Pedro Alves - April 14, 2018, 7:09 p.m.
Straighforward conversion.

The core_target global is renamed to avoid conflict with the
"core_target" class.
---
 gdb/corefile.c |  12 ++--
 gdb/corelow.c  | 202 ++++++++++++++++++++++++++++-----------------------------
 gdb/gdbcore.h  |   4 +-
 3 files changed, 105 insertions(+), 113 deletions(-)

Patch

diff --git a/gdb/corefile.c b/gdb/corefile.c
index 5a735264ef..c849a10646 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -53,9 +53,6 @@  static int exec_file_hook_count = 0;		/* Size of array.  */
 
 bfd *core_bfd = NULL;
 
-/* corelow.c target.  It is never NULL after GDB initialization.  */
-
-struct target_ops *core_target;
 
 
 /* Backward compatability with old way of specifying core files.  */
@@ -65,12 +62,12 @@  core_file_command (const char *filename, int from_tty)
 {
   dont_repeat ();		/* Either way, seems bogus.  */
 
-  gdb_assert (core_target != NULL);
+  gdb_assert (the_core_target != NULL);
 
   if (!filename)
-    (core_target->to_detach) (core_target, current_inferior (), from_tty);
+    the_core_target->detach (current_inferior (), from_tty);
   else
-    (core_target->to_open) (filename, from_tty);
+    the_core_target->open (filename, from_tty);
 }
 
 
@@ -237,8 +234,7 @@  read_memory_object (enum target_object object, CORE_ADDR memaddr,
       enum target_xfer_status status;
       ULONGEST xfered_len;
 
-      status = target_xfer_partial (current_target.beneath,
-				    object, NULL,
+      status = target_xfer_partial (target_stack, object, NULL,
 				    myaddr + xfered, NULL,
 				    memaddr + xfered, len - xfered,
 				    &xfered_len);
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 190f0162d9..f7202f75e3 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -50,6 +50,55 @@ 
 #define O_LARGEFILE 0
 #endif
 
+/* The core file target.  */
+
+class core_target final : public target_ops
+{
+public:
+  core_target ()
+  { to_stratum = process_stratum; }
+
+  const char *shortname () override
+  { return "core"; }
+
+  const char *longname () override
+  { return _("Local core dump file"); }
+
+  const char *doc () override
+  {
+    return _("\
+Use a core file as a target.  Specify the filename of the core file.");
+  }
+
+  void open (const char *, int) override;
+  void close () override;
+  void detach (inferior *, int) override;
+  void fetch_registers (struct regcache *, int) override;
+
+  enum target_xfer_status xfer_partial (enum target_object object,
+					const char *annex,
+					gdb_byte *readbuf,
+					const gdb_byte *writebuf,
+					ULONGEST offset, ULONGEST len,
+					ULONGEST *xfered_len) override;
+  void files_info () override;
+
+  int thread_alive (ptid_t ptid) override;
+  const struct target_desc *read_description () override;
+
+  const char *pid_to_str (ptid_t) override;
+
+  const char *thread_name (struct thread_info *) override;
+
+  int has_memory () override;
+  int has_stack () override;
+  int has_registers () override;
+  bool info_proc (const char *, enum info_proc_what) override;
+};
+
+/* See gdbcore.h.  */
+struct target_ops *the_core_target;
+
 /* List of all available core_fns.  On gdb startup, each core file
    register reader calls deprecated_add_core_fns() to register
    information on each core format it is prepared to read.  */
@@ -74,21 +123,15 @@  static struct gdbarch *core_gdbarch = NULL;
    unix child targets.  */
 static struct target_section_table *core_data;
 
-static void core_files_info (struct target_ops *);
-
 static struct core_fns *sniff_core_bfd (bfd *);
 
 static int gdb_check_format (bfd *);
 
-static void core_close (struct target_ops *self);
-
 static void core_close_cleanup (void *ignore);
 
 static void add_to_thread_list (bfd *, asection *, void *);
 
-static void init_core_ops (void);
-
-static struct target_ops core_ops;
+static core_target core_ops;
 
 /* An arbitrary identifier for the core inferior.  */
 #define CORELOW_PID 1
@@ -185,7 +228,7 @@  gdb_check_format (bfd *abfd)
    stack spaces as empty.  */
 
 static void
-core_close (struct target_ops *self)
+core_close ()
 {
   if (core_bfd)
     {
@@ -216,7 +259,13 @@  core_close (struct target_ops *self)
 static void
 core_close_cleanup (void *ignore)
 {
-  core_close (NULL);
+  core_close ();
+}
+
+void
+core_target::close ()
+{
+  core_close ();
 }
 
 /* Look for sections whose names start with `.reg/' so that we can
@@ -266,8 +315,8 @@  add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
 
 /* This routine opens and sets up the core file bfd.  */
 
-static void
-core_open (const char *arg, int from_tty)
+void
+core_target::open (const char *arg, int from_tty)
 {
   const char *p;
   int siggy;
@@ -463,10 +512,10 @@  core_open (const char *arg, int from_tty)
     }
 }
 
-static void
-core_detach (struct target_ops *ops, inferior *inf, int from_tty)
+void
+core_target::detach (inferior *inf, int from_tty)
 {
-  unpush_target (ops);
+  unpush_target (this);
   reinit_frame_cache ();
   if (from_tty)
     printf_filtered (_("No core file now.\n"));
@@ -585,9 +634,8 @@  get_core_registers_cb (const char *sect_name, int size,
 
 /* We just get all the registers, so we don't use regno.  */
 
-static void
-get_core_registers (struct target_ops *ops,
-		    struct regcache *regcache, int regno)
+void
+core_target::fetch_registers (struct regcache *regcache, int regno)
 {
   int i;
   struct gdbarch *gdbarch;
@@ -619,8 +667,8 @@  get_core_registers (struct target_ops *ops,
       regcache_raw_supply (regcache, i, NULL);
 }
 
-static void
-core_files_info (struct target_ops *t)
+void
+core_target::files_info ()
 {
   print_section_info (core_data, core_bfd);
 }
@@ -655,11 +703,10 @@  add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
   list->pos += 4;
 }
 
-static enum target_xfer_status
-core_xfer_partial (struct target_ops *ops, enum target_object object,
-		   const char *annex, gdb_byte *readbuf,
-		   const gdb_byte *writebuf, ULONGEST offset,
-		   ULONGEST len, ULONGEST *xfered_len)
+enum target_xfer_status
+core_target::xfer_partial (enum target_object object, const char *annex,
+			   gdb_byte *readbuf, const gdb_byte *writebuf,
+			   ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
 {
   switch (object)
     {
@@ -861,35 +908,13 @@  core_xfer_partial (struct target_ops *ops, enum target_object object,
       return TARGET_XFER_E_IO;
 
     default:
-      return ops->beneath->to_xfer_partial (ops->beneath, object,
-					    annex, readbuf,
-					    writebuf, offset, len,
-					    xfered_len);
+      return this->beneath->xfer_partial (object, annex, readbuf,
+					  writebuf, offset, len,
+					  xfered_len);
     }
 }
 
 
-/* If mourn is being called in all the right places, this could be say
-   `gdb internal error' (since generic_mourn calls
-   breakpoint_init_inferior).  */
-
-static int
-ignore (struct target_ops *ops, struct gdbarch *gdbarch,
-	struct bp_target_info *bp_tgt)
-{
-  return 0;
-}
-
-/* Implement the to_remove_breakpoint method.  */
-
-static int
-core_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
-			struct bp_target_info *bp_tgt,
-			enum remove_bp_reason reason)
-{
-  return 0;
-}
-
 
 /* Okay, let's be honest: threads gleaned from a core file aren't
    exactly lively, are they?  On the other hand, if we don't claim
@@ -897,8 +922,8 @@  core_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
    to appear in an "info thread" command, which is quite a useful
    behaviour.
  */
-static int
-core_thread_alive (struct target_ops *ops, ptid_t ptid)
+int
+core_target::thread_alive (ptid_t ptid)
 {
   return 1;
 }
@@ -908,24 +933,23 @@  core_thread_alive (struct target_ops *ops, ptid_t ptid)
    wrapper could be avoided if targets got a chance to specialize
    core_ops.  */
 
-static const struct target_desc *
-core_read_description (struct target_ops *target)
+const struct target_desc *
+core_target::read_description ()
 {
   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
     {
       const struct target_desc *result;
 
-      result = gdbarch_core_read_description (core_gdbarch, 
-					      target, core_bfd);
+      result = gdbarch_core_read_description (core_gdbarch, this, core_bfd);
       if (result != NULL)
 	return result;
     }
 
-  return target->beneath->to_read_description (target->beneath);
+  return this->beneath->read_description ();
 }
 
-static const char *
-core_pid_to_str (struct target_ops *ops, ptid_t ptid)
+const char *
+core_target::pid_to_str (ptid_t ptid)
 {
   static char buf[64];
   struct inferior *inf;
@@ -956,8 +980,8 @@  core_pid_to_str (struct target_ops *ops, ptid_t ptid)
   return buf;
 }
 
-static const char *
-core_thread_name (struct target_ops *self, struct thread_info *thr)
+const char *
+core_target::thread_name (struct thread_info *thr)
 {
   if (core_gdbarch
       && gdbarch_core_thread_name_p (core_gdbarch))
@@ -965,29 +989,28 @@  core_thread_name (struct target_ops *self, struct thread_info *thr)
   return NULL;
 }
 
-static int
-core_has_memory (struct target_ops *ops)
+int
+core_target::has_memory ()
 {
   return (core_bfd != NULL);
 }
 
-static int
-core_has_stack (struct target_ops *ops)
+int
+core_target::has_stack ()
 {
   return (core_bfd != NULL);
 }
 
-static int
-core_has_registers (struct target_ops *ops)
+int
+core_target::has_registers ()
 {
   return (core_bfd != NULL);
 }
 
 /* Implement the to_info_proc method.  */
 
-static void
-core_info_proc (struct target_ops *ops, const char *args,
-		enum info_proc_what request)
+bool
+core_target::info_proc (const char *args, enum info_proc_what request)
 {
   struct gdbarch *gdbarch = get_current_arch ();
 
@@ -995,47 +1018,18 @@  core_info_proc (struct target_ops *ops, const char *args,
      method on gdbarch, not 'info_proc'.  */
   if (gdbarch_core_info_proc_p (gdbarch))
     gdbarch_core_info_proc (gdbarch, args, request);
-}
-
-/* Fill in core_ops with its defined operations and properties.  */
 
-static void
-init_core_ops (void)
-{
-  core_ops.to_shortname = "core";
-  core_ops.to_longname = "Local core dump file";
-  core_ops.to_doc =
-    "Use a core file as a target.  Specify the filename of the core file.";
-  core_ops.to_open = core_open;
-  core_ops.to_close = core_close;
-  core_ops.to_detach = core_detach;
-  core_ops.to_fetch_registers = get_core_registers;
-  core_ops.to_xfer_partial = core_xfer_partial;
-  core_ops.to_files_info = core_files_info;
-  core_ops.to_insert_breakpoint = ignore;
-  core_ops.to_remove_breakpoint = core_remove_breakpoint;
-  core_ops.to_thread_alive = core_thread_alive;
-  core_ops.to_read_description = core_read_description;
-  core_ops.to_pid_to_str = core_pid_to_str;
-  core_ops.to_thread_name = core_thread_name;
-  core_ops.to_stratum = process_stratum;
-  core_ops.to_has_memory = core_has_memory;
-  core_ops.to_has_stack = core_has_stack;
-  core_ops.to_has_registers = core_has_registers;
-  core_ops.to_info_proc = core_info_proc;
-  core_ops.to_magic = OPS_MAGIC;
-
-  if (core_target)
-    internal_error (__FILE__, __LINE__, 
-		    _("init_core_ops: core target already exists (\"%s\")."),
-		    core_target->to_longname);
-  core_target = &core_ops;
+  return true;
 }
 
 void
 _initialize_corelow (void)
 {
-  init_core_ops ();
+  if (the_core_target != NULL)
+    internal_error (__FILE__, __LINE__,
+		    _("core target already exists (\"%s\")."),
+		    the_core_target->longname ());
+  the_core_target = &core_ops;
 
   add_target_with_completer (&core_ops, filename_completer);
 }
diff --git a/gdb/gdbcore.h b/gdb/gdbcore.h
index 5264eef4ee..04a235dd90 100644
--- a/gdb/gdbcore.h
+++ b/gdb/gdbcore.h
@@ -135,7 +135,9 @@  extern void specify_exec_file_hook (void (*hook) (const char *filename));
 
 extern bfd *core_bfd;
 
-extern struct target_ops *core_target;
+/* corelow.c target.  It is never NULL after GDB initialization.  */
+
+extern struct target_ops *the_core_target;
 
 /* Whether to open exec and core files read-only or read-write.  */