This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFA 01/08] multi-process support: struct inferior


This patch adds a `struct inferior' object, and the code needed to
maintain an inferior list.  The patch adds an inferior.c file (looks
like it was just waiting for this) -- you'll notice that the code
is very similar in principle to gdbthread.h/thread.c.  That's not
a coincidence, this code was heavilly borrowed from there.

The object is still mostly empty in this patch.  The following
patches will start moving a couple of globals into it.

-- 
Pedro Alves
gdb/
2008-09-12  Pedro Alves  <pedro@codesourcery.com>

	* inferior.h: Forward declare struct ui_out.
	Forward declare struct private_inferior.
	(struct inferior): New.
	(init_inferior_list, add_inferior, add_inferior_silent)
	(delete_inferior, delete_inferior_silent, detach_inferior)
	(gdb_inferior_id_to_pid, pid_to_gdb_inferior_id, in_inferior_list)
	(valid_inferior_id, find_inferior_pid): New functions.
	(inferior_callback_func): New typedef.
	(iterate_over_inferiors, print_inferior, have_inferiors)
	(current_inferior): New functions.
	* inferior.c: New file.
	
	* Makefile.in (SFILES): Add inferior.c.
	(COMMON_OBS): Add inferior.o.

gdb/doc/
2008-09-12  Pedro Alves  <pedro@codesourcery.com>

	* gdb.texinfo (Inferiors): New node.
	(Listing Inferios): New section.
	Document new "info inferiors" and "set/show inferior-events" commands.

---
 gdb/Makefile.in     |    6 
 gdb/doc/gdb.texinfo |   30 ++++
 gdb/inferior.c      |  332 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/inferior.h      |   85 +++++++++++++
 4 files changed, 451 insertions(+), 2 deletions(-)

Index: src/gdb/Makefile.in
===================================================================
--- src.orig/gdb/Makefile.in	2008-09-12 12:27:25.000000000 +0100
+++ src/gdb/Makefile.in	2008-09-12 12:27:27.000000000 +0100
@@ -655,7 +655,8 @@ SFILES = ada-exp.y ada-lang.c ada-typepr
 	user-regs.c \
 	valarith.c valops.c valprint.c value.c varobj.c vec.c \
 	wrapper.c \
-	xml-tdesc.c xml-support.c
+	xml-tdesc.c xml-support.c \
+	inferior.c
 
 LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
 
@@ -803,7 +804,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $
 	tramp-frame.o \
 	solib.o solib-null.o \
 	prologue-value.o memory-map.o xml-support.o \
-	target-descriptions.o target-memory.o xml-tdesc.o xml-builtin.o
+	target-descriptions.o target-memory.o xml-tdesc.o xml-builtin.o \
+	inferior.o
 
 TSOBS = inflow.o
 
Index: src/gdb/inferior.h
===================================================================
--- src.orig/gdb/inferior.h	2008-09-12 12:27:25.000000000 +0100
+++ src/gdb/inferior.h	2008-09-12 12:43:11.000000000 +0100
@@ -29,6 +29,7 @@ struct ui_file;
 struct type;
 struct gdbarch;
 struct regcache;
+struct ui_out;
 
 /* For bpstat.  */
 #include "breakpoint.h"
@@ -397,4 +398,88 @@ extern int suppress_resume_observer;
 #if !defined(START_INFERIOR_TRAPS_EXPECTED)
 #define START_INFERIOR_TRAPS_EXPECTED	2
 #endif
+
+struct private_inferior;
+
+struct inferior
+{
+  struct inferior *next;
+
+  int pid;			/* Actual inferior id.  Usually, a
+				   process id.  */
+  int num;			/* Convenient handle (GDB inferior
+				   id) */
+
+  /* Private data used by the target vector implementation.  */
+  struct private_inferior *private;
+};
+
+/* Create an empty inferior list, or empty the existing one.  */
+extern void init_inferior_list (void);
+
+/* Add an inferior to the inferior list, print a message that a new
+   inferior is found, and return the pointer to the new inferior.
+   Caller may use this pointer to initialize the private inferior
+   data.  */
+extern struct inferior *add_inferior (int pid);
+
+/* Same as add_inferior, but don't print new inferior notifications to
+   the CLI.  */
+extern struct inferior *add_inferior_silent (int pid);
+
+/* Delete an existing inferior list entry, due to inferior exit.  */
+extern void delete_inferior (int pid);
+
+/* Same as delete_inferior, but don't print new inferior notifications
+   to the CLI.  */
+extern void delete_inferior_silent (int pid);
+
+/* Delete an existing inferior list entry, due to inferior detaching.  */
+extern void detach_inferior (int pid);
+
+/* Translate the integer inferior id (GDB's homegrown id, not the system's)
+   into a "pid" (which may be overloaded with extra inferior information).  */
+extern int gdb_inferior_id_to_pid (int);
+
+/* Translate a target 'pid' into the integer inferior id (GDB's
+   homegrown id, not the system's).  */
+extern int pid_to_gdb_inferior_id (int pid);
+
+/* Boolean test for an already-known pid.  */
+extern int in_inferior_list (int pid);
+
+/* Boolean test for an already-known inferior id (GDB's homegrown id,
+   not the system's).  */
+extern int valid_inferior_id (int gpid);
+
+/* Search function to lookup a inferior by target 'pid'.  */
+extern struct inferior *find_inferior_pid (int pid);
+
+/* Inferior iterator function.
+
+   Calls a callback function once for each inferior, so long as the
+   callback function returns false.  If the callback function returns
+   true, the iteration will end and the current inferior will be
+   returned.  This can be useful for implementing a search for a
+   inferior with arbitrary attributes, or for applying some operation
+   to every inferior.
+
+   It is safe to delete the iterated inferior from the callback.  */
+extern struct inferior *iterate_over_inferiors (int (*) (struct inferior *,
+							 void *),
+						void *);
+
+/* Prints the list of inferiors and their details on UIOUT.
+
+   If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior
+   that should be printed.  Otherwise, all inferiors are printed.  */
+extern void print_inferior (struct ui_out *uiout, int requested_inferior);
+
+/* Returns true if the inferior list is not empty.  */
+extern int have_inferiors (void);
+
+/* Return a pointer to the current inferior.  It is an error to call
+   this if there is no current inferior.  */
+extern struct inferior *current_inferior (void);
+
 #endif /* !defined (INFERIOR_H) */
Index: src/gdb/inferior.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ src/gdb/inferior.c	2008-09-12 12:39:51.000000000 +0100
@@ -0,0 +1,332 @@
+/* Multi-process control for GDB, the GNU debugger.
+
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "inferior.h"
+#include "target.h"
+#include "command.h"
+#include "gdbcmd.h"
+#include "gdbthread.h"
+#include "ui-out.h"
+
+void _initialize_inferiors (void);
+
+static struct inferior *inferior_list = NULL;
+static int highest_inferior_num;
+
+/* Print notices on inferior events (attach, detach, etc.), set with
+   `set print inferior-events'.  */
+static int print_inferior_events = 0;
+
+struct inferior*
+current_inferior (void)
+{
+  struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
+  gdb_assert (inf);
+  return inf;
+}
+
+static void
+free_inferior (struct inferior *inf)
+{
+  xfree (inf->private);
+  xfree (inf);
+}
+
+void
+init_inferior_list (void)
+{
+  struct inferior *inf, *infnext;
+
+  highest_inferior_num = 0;
+  if (!inferior_list)
+    return;
+
+  for (inf = inferior_list; inf; inf = infnext)
+    {
+      infnext = inf->next;
+      free_inferior (inf);
+    }
+
+  inferior_list = NULL;
+}
+
+struct inferior *
+add_inferior_silent (int pid)
+{
+  struct inferior *inf;
+
+  inf = xmalloc (sizeof (*inf));
+  memset (inf, 0, sizeof (*inf));
+  inf->pid = pid;
+
+  inf->num = ++highest_inferior_num;
+  inf->next = inferior_list;
+  inferior_list = inf;
+
+  return inf;
+}
+
+struct inferior *
+add_inferior (int pid)
+{
+  struct inferior *inf = add_inferior_silent (pid);
+
+  if (print_inferior_events)
+    printf_unfiltered (_("[New inferior %d]\n"), pid);
+
+  return inf;
+}
+
+struct delete_thread_of_inferior_arg
+{
+  int pid;
+  int silent;
+};
+
+static int
+delete_thread_of_inferior (struct thread_info *tp, void *data)
+{
+  struct delete_thread_of_inferior_arg *arg = data;
+
+  if (ptid_get_pid (tp->ptid) == arg->pid)
+    {
+      if (arg->silent)
+	delete_thread_silent (tp->ptid);
+      else
+	delete_thread (tp->ptid);
+    }
+
+  return 0;
+}
+
+/* If SILENT then be quiet -- don't announce a inferior death, or the
+   exit of its threads.  */
+static void
+delete_inferior_1 (int pid, int silent)
+{
+  struct inferior *inf, *infprev;
+  struct delete_thread_of_inferior_arg arg = { pid, silent };
+
+  infprev = NULL;
+
+  for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
+    if (inf->pid == pid)
+      break;
+
+  if (!inf)
+    return;
+
+  if (infprev)
+    infprev->next = inf->next;
+  else
+    inferior_list = inf->next;
+
+  free_inferior (inf);
+
+  arg.pid = pid;
+  arg.silent = silent;
+
+  iterate_over_threads (delete_thread_of_inferior, &arg);
+}
+
+void
+delete_inferior (int pid)
+{
+  delete_inferior_1 (pid, 0);
+
+  if (print_inferior_events)
+    printf_unfiltered (_("[Inferior %d exited]\n"), pid);
+}
+
+void
+delete_inferior_silent (int pid)
+{
+  delete_inferior_1 (pid, 1);
+}
+
+void
+detach_inferior (int pid)
+{
+  delete_inferior_1 (pid, 1);
+
+  if (print_inferior_events)
+    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
+}
+
+static struct inferior *
+find_inferior_id (int num)
+{
+  struct inferior *inf;
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    if (inf->num == num)
+      return inf;
+
+  return NULL;
+}
+
+struct inferior *
+find_inferior_pid (int pid)
+{
+  struct inferior *inf;
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    if (inf->pid == pid)
+      return inf;
+
+  return NULL;
+}
+
+struct inferior *
+iterate_over_inferiors (int (*callback) (struct inferior *, void *),
+			void *data)
+{
+  struct inferior *inf, *infnext;
+
+  for (inf = inferior_list; inf; inf = infnext)
+    {
+      infnext = inf->next;
+      if ((*callback) (inf, data))
+	return inf;
+    }
+
+  return NULL;
+}
+
+int
+valid_gdb_inferior_id (int num)
+{
+  struct inferior *inf;
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    if (inf->num == num)
+      return 1;
+
+  return 0;
+}
+
+int
+pid_to_gdb_inferior_id (int pid)
+{
+  struct inferior *inf;
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    if (inf->pid == pid)
+      return inf->num;
+
+  return 0;
+}
+
+int
+gdb_inferior_id_to_pid (int num)
+{
+  struct inferior *inferior = find_inferior_id (num);
+  if (inferior)
+    return inferior->pid;
+  else
+    return -1;
+}
+
+int
+in_inferior_list (int pid)
+{
+  struct inferior *inf;
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    if (inf->pid == pid)
+      return 1;
+
+  return 0;
+}
+
+int
+have_inferiors (void)
+{
+  return inferior_list != NULL;
+}
+
+/* Prints the list of inferiors and their details on UIOUT.  This is a
+   version of 'info_inferior_command' suitable for use from MI.
+
+   If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
+   should be printed.  Otherwise, all inferiors are printed.  */
+void
+print_inferior (struct ui_out *uiout, int requested_inferior)
+{
+  struct inferior *inf;
+  struct cleanup *old_chain;
+
+  old_chain = make_cleanup_ui_out_list_begin_end (uiout, "inferiors");
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    {
+      struct cleanup *chain2;
+
+      if (requested_inferior != -1 && inf->num != requested_inferior)
+	continue;
+
+      chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+
+      if (inf->pid == ptid_get_pid (inferior_ptid))
+	ui_out_text (uiout, "* ");
+      else
+	ui_out_text (uiout, "  ");
+
+      ui_out_field_int (uiout, "id", inf->num);
+      ui_out_text (uiout, " ");
+      ui_out_field_int (uiout, "target-id", inf->pid);
+
+      ui_out_text (uiout, "\n");
+      do_cleanups (chain2);
+    }
+
+  do_cleanups (old_chain);
+}
+
+/* Print information about currently known inferiors.  */
+
+static void
+info_inferiors_command (char *arg, int from_tty)
+{
+  print_inferior (uiout, -1);
+}
+
+/* Print notices when new inferiors are created and die.  */
+static void
+show_print_inferior_events (struct ui_file *file, int from_tty,
+			   struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
+}
+
+void
+_initialize_inferiors (void)
+{
+  add_info ("inferiors", info_inferiors_command,
+	    _("IDs of currently known inferiors."));
+
+  add_setshow_boolean_cmd ("inferior-events", no_class,
+         &print_inferior_events, _("\
+Set printing of inferior events (e.g., inferior start and exit)."), _("\
+Show printing of inferior events (e.g., inferior start and exit)."), NULL,
+         NULL,
+         show_print_inferior_events,
+         &setprintlist, &showprintlist);
+}
Index: src/gdb/doc/gdb.texinfo
===================================================================
--- src.orig/gdb/doc/gdb.texinfo	2008-09-12 12:27:25.000000000 +0100
+++ src/gdb/doc/gdb.texinfo	2008-09-12 12:27:27.000000000 +0100
@@ -2750,6 +2750,36 @@ You can use the @code{catch} command to 
 a @code{fork}, @code{vfork}, or @code{exec} call is made.  @xref{Set
 Catchpoints, ,Setting Catchpoints}.
 
+@node Inferiors
+@section Listing Inferiors
+
+@value{GDBN} keeps track of the inferiors under control, be them
+either running processes, core files, or remote targets without a
+notion of processes, but which nonetheless have execution, and allows
+the user to query and be notified about them uniformally, using the
+commands below.
+
+@table @code
+@kindex info inferiors
+@item info inferiors
+Print a list of all inferiors under the control of @value{GDBN}.
+
+@kindex set print inferior-events
+@cindex print messages on inferior start and exit
+@item set print inferior-events
+@itemx set print inferior-events on
+@itemx set print inferior-events off
+The @code{set print inferior-events} command allows you to enable or
+disable printing of messages when @value{GDBN} notices that new
+inferiors have started or that inferiors have exited or have been
+detached.  By default, these messages will not be printed.
+
+@kindex show print inferior-events
+@item show print inferior-events
+Show whether messages will be printed when @value{GDBN} detects that
+inferiors have started, exited or have been detached.
+@end table
+
 @node Checkpoint/Restart
 @section Setting a @emph{Bookmark} to Return to Later
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]