[RFC] Notification for attach/detach.

Vladimir Prus
2008-11-12T20:35:59Z

This patch introduces new =thread-group-created and =thread-group-exited notifications,
to help the frontend in multiprocess case.

I'll commit in a few days if there are no objections.

- Volodya

From c41177a84715643cdb6315256e2c5635275336ea Mon Sep 17 00:00:00 2001

gdb/
* Makefile.in: Update dependencies.
* inferior.c: Call the process observers.
* mi/mi-interp.c (mi_new_inferior, mi_inferior_exit): New.
(mi_interpreter_init): Register the above.

gdb/doc/
* observer.texi (new_inferior, inferior_exit): New observers.
---
gdb/doc/observer.texi | 10 ++++++++++
gdb/inferior.c | 5 +++++
gdb/mi/mi-interp.c | 24 ++++++++++++++++++++++++
gdb/mi/mi-main.c | 12 ++++++------
4 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/gdb/doc/observer.texi b/gdb/doc/observer.texi
index f76bf9f..cc0eb06 100644
--- a/gdb/doc/observer.texi
+++ b/gdb/doc/observer.texi
@@ -187,3 +187,13 @@ a pointer to the new architecture.
The thread's ptid has changed. The @var{old_ptid} parameter specifies
the old value, and @var{new_ptid} specifies the new value.
@end deftypefun
+
+@deftypefun void new_inferior (int @var{pid})
+@value{GDBN} has attached to a new inferior identified by @var{pid}.
+@end deftypefun
+
+@deftypefun void inferior_exit (int @var{pid})
+The inferior identified by @var{pid} has either exited, or was detached
+from.
+@end deftypefun
+
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 9fec4cc..24f8fa6 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -24,6 +24,7 @@
#include "gdbcmd.h"
#include "gdbthread.h"
#include "ui-out.h"
+#include "observer.h"

void _initialize_inferiors (void);

@@ -91,6 +92,8 @@ add_inferior (int pid)
{
struct inferior *inf = add_inferior_silent (pid);

+ observer_notify_new_inferior (pid);
+
if (print_inferior_events)
printf_unfiltered (_("[New inferior %d]\n"), pid);

@@ -147,6 +150,8 @@ delete_inferior_1 (int pid, int silent)
arg.silent = silent;

iterate_over_threads (delete_thread_of_inferior, &arg);
+
+ observer_notify_inferior_exit (pid);
}

void
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index df0d3df..3121604 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -69,6 +69,8 @@ static void mi_on_normal_stop (struct bpstats *bs);

static void mi_new_thread (struct thread_info *t);
static void mi_thread_exit (struct thread_info *t);
+static void mi_new_inferior (int pid);
+static void mi_inferior_exit (int pid);
static void mi_on_resume (ptid_t ptid);

static void *
@@ -94,6 +96,8 @@ mi_interpreter_init (int top_level)
{
observer_attach_new_thread (mi_new_thread);
observer_attach_thread_exit (mi_thread_exit);
+ observer_attach_new_inferior (mi_new_inferior);
+ observer_attach_inferior_exit (mi_inferior_exit);
observer_attach_normal_stop (mi_on_normal_stop);
observer_attach_target_resumed (mi_on_resume);
}
@@ -303,6 +307,26 @@ mi_thread_exit (struct thread_info *t)
}

static void
+mi_new_inferior (int pid)
+{
+ struct mi_interp *mi = top_level_interpreter_data ();
+ target_terminal_ours ();
+ fprintf_unfiltered (mi->event_channel, "thread-group-created,id=\"%d\"",
+ pid);
+ gdb_flush (mi->event_channel);
+}
+
+static void
+mi_inferior_exit (int pid)
+{
+ struct mi_interp *mi = top_level_interpreter_data ();
+ target_terminal_ours ();
+ fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"%d\"",
+ pid);
+ gdb_flush (mi->event_channel);
+}
+
+static void
mi_on_normal_stop (struct bpstats *bs)
{
/* Since this can be called when CLI command is executing,
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 544fec6..43ec0b4 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -46,7 +46,7 @@
#include "mi-main.h"
#include "language.h"
#include "valprint.h"
-#include "processes.h"
+#include "inferior.h"

#include <ctype.h>
#include <sys/time.h>
@@ -250,13 +250,13 @@ mi_cmd_thread_info (char *command, char **argv, int argc)
}

static int
-print_one_process (struct process_info *process, void *arg)
+print_one_inferior (struct inferior *inferior, void *arg)
{
struct cleanup *back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

- ui_out_field_fmt (uiout, "id", "p%d", process->pid);
+ ui_out_field_fmt (uiout, "id", "%d", inferior->pid);
ui_out_field_string (uiout, "type", "process");
- ui_out_field_int (uiout, "pid", process->pid);
+ ui_out_field_int (uiout, "pid", inferior->pid);

do_cleanups (back_to);
return 0;
@@ -284,14 +284,14 @@ mi_cmd_list_thread_groups (char *command, char **argv, int argc)
if (id)
{
int pid = atoi (id);
- if (!in_process_list (pid))
+ if (!in_inferior_list (pid))
error ("Invalid thread group id '%s'", id);
print_thread_info (uiout, -1, pid);
}
else
{
make_cleanup_ui_out_list_begin_end (uiout, "groups");
- iterate_over_processes (print_one_process, NULL);
+ iterate_over_inferiors (print_one_inferior, NULL);
}

do_cleanups (back_to);
--
1.5.3.5