From d38086cce9ab3d1c0acd48dd89fe5721caa08fed Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 21 Apr 2023 09:45:30 -0400 Subject: [PATCH] gdb: add interp::on_inferior_disappeared method Same idea as previous patches, but for inferior_disappeared. For symmetry with on_inferior_appeared, I named this one on_inferior_disappeared, despite the observer being called inferior_exit. This is called when detaching an inferior, so I think that calling it "disappeared" is a bit less misleading (the observer should probably be renamed later). Change-Id: I372101586bc9454997953c1e540a2a6685f53ef6 --- gdb/inferior.c | 11 ++++++++++- gdb/interps.c | 8 ++++++++ gdb/interps.h | 6 ++++++ gdb/mi/mi-interp.c | 34 ++++++++++++---------------------- gdb/mi/mi-interp.h | 1 + 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/gdb/inferior.c b/gdb/inferior.c index 46d418a2be3..32fe9ccbd65 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -283,6 +283,15 @@ delete_inferior (struct inferior *inf) delete inf; } +/* Notify interpreters and observers that inferior INF disappeared. */ + +static void +notify_inferior_disappeared (inferior *inf) +{ + interps_notify_inferior_disappeared (inf); + gdb::observers::inferior_exit.notify (inf); +} + /* If SILENT then be quiet -- don't announce a inferior exit, or the exit of its threads. */ @@ -291,7 +300,7 @@ exit_inferior_1 (struct inferior *inf, int silent) { inf->clear_thread_list (silent); - gdb::observers::inferior_exit.notify (inf); + notify_inferior_disappeared (inf); inf->pid = 0; inf->fake_pid_p = false; diff --git a/gdb/interps.c b/gdb/interps.c index ed33df2efd4..46b0ecbd73f 100644 --- a/gdb/interps.c +++ b/gdb/interps.c @@ -478,6 +478,14 @@ interps_notify_inferior_appeared (inferior *inf) interps_notify (&interp::on_inferior_appeared, inf); } +/* See interps.h. */ + +void +interps_notify_inferior_disappeared (inferior *inf) +{ + interps_notify (&interp::on_inferior_disappeared, inf); +} + /* This just adds the "interpreter-exec" command. */ void _initialize_interpreter (); void diff --git a/gdb/interps.h b/gdb/interps.h index 09edaaeb003..b1aee5db829 100644 --- a/gdb/interps.h +++ b/gdb/interps.h @@ -128,6 +128,9 @@ public: /* Notify the interpreter that inferior INF was started or attached. */ virtual void on_inferior_appeared (inferior *inf) {} + /* Notify the interpreter that inferior INF exited or was detached. */ + virtual void on_inferior_disappeared (inferior *inf) {} + private: /* The memory for this is static, it comes from literal strings (e.g. "cli"). */ const char *m_name; @@ -251,6 +254,9 @@ extern void interps_notify_inferior_added (inferior *inf); /* Notify all interpreters that inferior INF was started or attached. */ extern void interps_notify_inferior_appeared (inferior *inf); +/* Notify all interpreters that inferior INF exited or was detached. */ +extern void interps_notify_inferior_disappeared (inferior *inf); + /* well-known interpreters */ #define INTERP_CONSOLE "console" #define INTERP_MI2 "mi2" diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c index 3fce9b69f98..ad934d337e5 100644 --- a/gdb/mi/mi-interp.c +++ b/gdb/mi/mi-interp.c @@ -62,7 +62,6 @@ static void mi_remove_notify_hooks (void); static void mi_record_changed (struct inferior*, int, const char *, const char *); -static void mi_inferior_exit (struct inferior *inf); static void mi_inferior_removed (struct inferior *inf); static void mi_on_resume (ptid_t ptid); static void mi_solib_loaded (struct so_list *solib); @@ -379,29 +378,21 @@ mi_interp::on_inferior_appeared (inferior *inf) gdb_flush (this->event_channel); } -static void -mi_inferior_exit (struct inferior *inf) +void +mi_interp::on_inferior_disappeared (inferior *inf) { - SWITCH_THRU_ALL_UIS () - { - struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); - - if (mi == NULL) - continue; - - target_terminal::scoped_restore_terminal_state term_state; - target_terminal::ours_for_output (); + target_terminal::scoped_restore_terminal_state term_state; + target_terminal::ours_for_output (); - if (inf->has_exit_code) - gdb_printf (mi->event_channel, - "thread-group-exited,id=\"i%d\",exit-code=\"%s\"", - inf->num, int_string (inf->exit_code, 8, 0, 0, 1)); - else - gdb_printf (mi->event_channel, - "thread-group-exited,id=\"i%d\"", inf->num); + if (inf->has_exit_code) + gdb_printf (this->event_channel, + "thread-group-exited,id=\"i%d\",exit-code=\"%s\"", + inf->num, int_string (inf->exit_code, 8, 0, 0, 1)); + else + gdb_printf (this->event_channel, + "thread-group-exited,id=\"i%d\"", inf->num); - gdb_flush (mi->event_channel); - } + gdb_flush (this->event_channel); } static void @@ -1130,7 +1121,6 @@ _initialize_mi_interp () interp_factory_register (INTERP_MI4, mi_interp_factory); interp_factory_register (INTERP_MI, mi_interp_factory); - gdb::observers::inferior_exit.attach (mi_inferior_exit, "mi-interp"); gdb::observers::inferior_removed.attach (mi_inferior_removed, "mi-interp"); gdb::observers::record_changed.attach (mi_record_changed, "mi-interp"); gdb::observers::target_resumed.attach (mi_on_resume, "mi-interp"); diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h index f57f99bffd3..66c55855230 100644 --- a/gdb/mi/mi-interp.h +++ b/gdb/mi/mi-interp.h @@ -54,6 +54,7 @@ public: void on_thread_exited (thread_info *t, int silent) override; void on_inferior_added (inferior *inf) override; void on_inferior_appeared (inferior *inf) override; + void on_inferior_disappeared (inferior *inf) override; /* MI's output channels */ mi_console_file *out; -- 2.30.2