Patchwork [v6] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages

login
register
mail settings
Submitter Sergio Durigan Junior
Date April 16, 2018, 8:04 p.m.
Message ID <20180416200444.30090-1-sergiodj@redhat.com>
Download mbox | patch
Permalink /patch/26759/
State New
Headers show

Comments

Sergio Durigan Junior - April 16, 2018, 8:04 p.m.
Changes from v5:

- Save result from 'target_pid_to_str' using std::string instead of
  'const char *', avoiding a possible race condition.


This patch aims to turn 'set print inferior-events' always on, and do
some cleanup on the messages printed by GDB when various inferior
events happen (attach, detach, fork, kill, exit).

To make sure that the patch is correct, I've tested it with a handful
of combinations of 'set follow-fork-mode', 'set detach-on-fork' and
'set print inferior-events'.  In the end, I decided to make my
hand-made test into an official testcase.  More on that below.

Using the following program as an example:

  #include <unistd.h>
  int main ()
  {
    fork ();
    return 0;
  }

We see the following outputs from the patched GDB:

- With 'set print inferior-events on':

    (gdb) r
    Starting program: a.out
    [Detaching after fork from child process 27749]
    [Inferior 1 (process 27745) exited normally]
    (gdb)

- With 'set print inferior-events off':

    (gdb) r
    Starting program: a.out
    [Inferior 1 (process 27823) exited normally]
    (gdb)

  Comparing this against an unpatched GDB:

- With 'set print inferior-events off' and 'set follow-fork-mode
  child':

    (gdb) r
    Starting program: a.out
    [Inferior 2 (process 5993) exited normally]
    (gdb)

  Compare this against an unpatched GDB:

    (unpatched-gdb) r
    Starting program: a.out
    [New process 5702]
    [Inferior 2 (process 5702) exited normally]
    (unpatched-gdb)

  It is possible to notice that, in this scenario, the patched GDB
  will lose the '[New process %d]' message.

- With 'set print inferior-events on', 'set follow-fork-mode child'
  and 'set detach-on-fork on':

    (gdb) r
    Starting program: a.out
    [Attaching after process 27905 fork to child process 27909]
    [New inferior 2 (process 27909)]
    [Detaching after fork from parent process 27905]
    [Inferior 1 (process 27905) detached]
    [Inferior 2 (process 27909) exited normally]
    (gdb)

  Compare this output with an unpatched GDB, using the same settings:

    (unpatched-gdb) r
    Starting program: a.out
    [New inferior 28033]
    [Inferior 28029 detached]
    [New process 28033]
    [Inferior 2 (process 28033) exited normally]
    [Inferior 28033 exited]
    (unpatched-gdb)

As can be seen above, I've also made a few modifications to messages
that are printed when 'set print inferior-events' is on.  For example,
a few of the messages did not contain the '[' and ']' as
prefix/suffix, which led to a few inconsistencies like:

  Attaching after process 22995 fork to child process 22999.
  [New inferior 22999]
  Detaching after fork from child process 22999.
  [Inferior 22995 detached]
  [Inferior 2 (process 22999) exited normally]

So I took the opportunity and included the square brackets where
applicable.  I have also made the existing messages more uniform, by
always printing "Inferior %d (process %d)..." where applicable.  This
makes it easier to identify the inferior number and the PID number
from the messages.

As suggested by Pedro, the "[Inferior %d exited]" message from
'exit_inferior' has been removed, because it got duplicated when
'inferior-events' is on.  I'm also using the
'add_{thread,inferior}_silent' versions (instead of their verbose
counterparts) on some locations, also to avoid duplicated messages.
For example, a patched GDB with 'set print inferior-events on', 'set
detach-on-fork on' and 'set follow-fork-mode child', but using
'add_thread', would print:

  (gdb) run
  Starting program: a.out
  [Attaching after process 25088 fork to child process 25092.]
  [New inferior 25092]   <--- duplicated
  [Detaching after fork from child process 25092.]
  [Inferior 25088 detached]
  [New process 25092]    <--- duplicated
  [Inferior 2 (process 25092) exited normally]

But if we use 'add_thread_silent' (with the same configuration as
before):

  (gdb) run
  Starting program: a.out
  [Attaching after process 31606 fork to child process 31610]
  [New inferior 2 (process 31610)]
  [Detaching after fork from parent process 31606]
  [Inferior 1 (process 31606) detached]
  [Inferior 2 (process 31610) exited normally]

As for the tests, the configuration options being exercised are:

- follow-fork-mode: child/parent
- detach-on-fork: on/off
- print inferior-events: on/off

It was also necessary to perform adjustments on several testcases,
because the expected messages changed considerably.

Built and regtested on BuildBot, without regressions.

gdb/ChangeLog:
2018-04-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* infcmd.c (kill_command): Print message when inferior has
	been killed.
	* inferior.c (print_inferior_events): Remove 'static'.  Set as
	'1'.
	(add_inferior): Improve message printed when
	'print_inferior_events' is on.
	(exit_inferior): Remove message printed when
	'print_inferior_events' is on.
	(detach_inferior): Improve message printed when
	'print_inferior_events' is on.
	(initialize_inferiors): Use 'add_inferior_silent' to set
	'current_inferior_'.
	* inferior.h (print_inferior_events): Declare here as
	'extern'.
	* infrun.c (follow_fork_inferior): Print '[Attaching...]' or
	'[Detaching...]' messages when 'print_inferior_events' is on.
	Use 'add_thread_silent' instead of 'add_thread'.  Add '[' and ']'
	as prefix/suffix for messages.  Remove periods.  Fix erroneous
	'Detaching after fork from child...', replace it by '... from
	parent...'.
	(handle_vfork_child_exec_or_exit): Add '[' and ']' as
	prefix/suffix when printing 'Detaching...' messages.  Print
	them when 'print_inferior_events' is on.
	* remote.c (remote_detach_1): Print message when detaching
	from inferior and '!is_fork_parent'.

gdb/testsuite/ChangeLog:
2018-04-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* gdb.base/attach-non-pgrp-leader.exp: Adjust 'Detaching...'
	regexps to expect for '[Inferior ... detached]' as well.
	* gdb.base/attach.exp: Likewise.
	* gdb.base/catch-syscall.exp (check_for_program_end): Adjust
	"gdb_continue_to_end".
	(test_catch_syscall_with_wrong_args): Likewise.
	* gdb.base/foll-fork.exp: Adjust regexps to match '[' and
	']'.  Don't set 'verbose' on.
	* gdb.base/foll-vfork.exp: Likewise.
	* gdb.base/fork-print-inferior-events.c: New file.
	* gdb.base/fork-print-inferior-events.exp: New file.
	* gdb.base/hook-stop.exp: Adjust regexps to expect for new
	'[Inferior ... has been killed]' message.
	* gdb.base/kill-after-signal.exp: Likewise.
	* gdb.base/solib-overlap.exp: Adjust regexps to expect for new
	detach message.
	* gdb.threads/kill.exp: Adjust regexps to expect for new kill
	message.
	* gdb.threads/process-dies-while-detaching.c
	(parent_function): Use 'usleep' in order to avoid
	race-conditions.
	* gdb.threads/clone-attach-detach.exp: Adjust 'Detaching...'
	regexps to expect for '[Inferior ... detached]' as well.
	* gdb.threads/process-dies-while-detaching.exp: Likewise.
---
 gdb/infcmd.c                                       |  8 ++
 gdb/inferior.c                                     | 19 +++--
 gdb/inferior.h                                     |  4 +
 gdb/infrun.c                                       | 37 +++++-----
 gdb/remote.c                                       |  7 +-
 gdb/testsuite/gdb.base/attach-non-pgrp-leader.exp  |  5 +-
 gdb/testsuite/gdb.base/attach.exp                  |  3 +-
 gdb/testsuite/gdb.base/catch-syscall.exp           |  4 +-
 gdb/testsuite/gdb.base/foll-fork.exp               | 14 ++--
 gdb/testsuite/gdb.base/foll-vfork.exp              | 16 ++--
 .../gdb.base/fork-print-inferior-events.c          | 37 ++++++++++
 .../gdb.base/fork-print-inferior-events.exp        | 85 ++++++++++++++++++++++
 gdb/testsuite/gdb.base/hook-stop.exp               |  3 +-
 gdb/testsuite/gdb.base/kill-after-signal.exp       |  6 +-
 gdb/testsuite/gdb.base/solib-overlap.exp           |  2 +-
 gdb/testsuite/gdb.threads/clone-attach-detach.exp  |  4 +-
 gdb/testsuite/gdb.threads/kill.exp                 |  8 +-
 .../gdb.threads/process-dies-while-detaching.exp   |  4 +-
 18 files changed, 205 insertions(+), 61 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/fork-print-inferior-events.c
 create mode 100644 gdb/testsuite/gdb.base/fork-print-inferior-events.exp
Pedro Alves - April 17, 2018, 3:56 p.m.
On 04/16/2018 09:04 PM, Sergio Durigan Junior wrote:
> Changes from v5:
> 
> - Save result from 'target_pid_to_str' using std::string instead of
>   'const char *', avoiding a possible race condition.

Sorry Sergio, we're getting there, but this will need more work.

> Built and regtested on BuildBot, without regressions.
> 

> 	* gdb.threads/process-dies-while-detaching.c
> 	(parent_function): Use 'usleep' in order to avoid
> 	race-conditions.

This is stale.  There's no 'usleep' any more.

> @@ -121,9 +120,11 @@ struct inferior *
>  add_inferior (int pid)
>  {
>    struct inferior *inf = add_inferior_silent (pid);
> +  std::string infpid = target_pid_to_str (pid_to_ptid (pid));
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
> +    printf_unfiltered (_("[New inferior %d (%s)]\n"),
> +		       inf->num, infpid.c_str ());

Let's call target_pid_to_str directly here without the
deep copy.

You needed to call target_pid_to_str early in kill_command
because there first kill the target, and print the
string _afterwards_.  "target_kill" may pop the target from the
target stack, so we can't use target_pid_to_str after the
target is gone already.

But here, we're not messing with the target stack.

(Nor are we calling target_pid_to_str twice in a row in
the same expression.)

>  void
> @@ -261,12 +259,13 @@ void
>  detach_inferior (inferior *inf)
>  {
>    /* Save the pid, since exit_inferior_1 will reset it.  */
> -  int pid = inf->pid;
> +  std::string infpid = target_pid_to_str (pid_to_ptid (inf->pid));
>  
>    exit_inferior_1 (inf, 0);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
> +    printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
> +		       inf->num, infpid.c_str ());

Same here.

> diff --git a/gdb/remote.c b/gdb/remote.c
> index f54a38833b..be118c5d97 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -5137,7 +5137,12 @@ remote_detach_1 (int from_tty, inferior *inf)
>    /* If doing detach-on-fork, we don't mourn, because that will delete
>       breakpoints that should be available for the followed inferior.  */
>    if (!is_fork_parent)
> -    target_mourn_inferior (inferior_ptid);
> +    {
> +      target_mourn_inferior (inferior_ptid);
> +      if (print_inferior_events)
> +	printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
> +			   inf->num, pid);

This is still using "(process %d)", which is incorrect
for target remote.

Also, here, if debugging in "target remote" mode, and this is 
the last live process, that target_mourn_inferior call will
unpush the remote target from the target stack.  So this is another
case where you need to call target_pid_to_str _before_ calling
target_mourn_inferior.

As mentioned before, not all targets print "process $decimal" in
their target_pid_to_str implementation.  So your testsuite changes
will need to adapt too.

Please try this: hack away gdbserver/server.c's support for
the RSP multi-process extensions (look for "multiprocess+"), and
test with "target remote".  That hack will emulate older gdbservers and
other stubs that don't support telling gdb the process's pid.
Manually confirm that you get "Remote target" instead of
"process $pid" -- see remote.c's implementation of
target_pid_to_str.  Then run the testsuite against that gdbserver.

Thanks,
Pedro Alves
Sergio Durigan Junior - April 17, 2018, 8:07 p.m.
On Tuesday, April 17 2018, Pedro Alves wrote:

> On 04/16/2018 09:04 PM, Sergio Durigan Junior wrote:
>> Changes from v5:
>> 
>> - Save result from 'target_pid_to_str' using std::string instead of
>>   'const char *', avoiding a possible race condition.
>
> Sorry Sergio, we're getting there, but this will need more work.

I'm the one who apologizes.

>> Built and regtested on BuildBot, without regressions.
>> 
>
>> 	* gdb.threads/process-dies-while-detaching.c
>> 	(parent_function): Use 'usleep' in order to avoid
>> 	race-conditions.
>
> This is stale.  There's no 'usleep' any more.

Removed.

>> @@ -121,9 +120,11 @@ struct inferior *
>>  add_inferior (int pid)
>>  {
>>    struct inferior *inf = add_inferior_silent (pid);
>> +  std::string infpid = target_pid_to_str (pid_to_ptid (pid));
>>  
>>    if (print_inferior_events)
>> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
>> +    printf_unfiltered (_("[New inferior %d (%s)]\n"),
>> +		       inf->num, infpid.c_str ());
>
> Let's call target_pid_to_str directly here without the
> deep copy.
>
> You needed to call target_pid_to_str early in kill_command
> because there first kill the target, and print the
> string _afterwards_.  "target_kill" may pop the target from the
> target stack, so we can't use target_pid_to_str after the
> target is gone already.
>
> But here, we're not messing with the target stack.
>
> (Nor are we calling target_pid_to_str twice in a row in
> the same expression.)

I confess I knew this, but decided to do a deep copy anyway because I
was aiming for consistency.  But no problem at all, I'll simplify this
part.

>
>>  void
>> @@ -261,12 +259,13 @@ void
>>  detach_inferior (inferior *inf)
>>  {
>>    /* Save the pid, since exit_inferior_1 will reset it.  */
>> -  int pid = inf->pid;
>> +  std::string infpid = target_pid_to_str (pid_to_ptid (inf->pid));
>>  
>>    exit_inferior_1 (inf, 0);
>>  
>>    if (print_inferior_events)
>> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
>> +    printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
>> +		       inf->num, infpid.c_str ());
>
> Same here.

Fixed.

>> diff --git a/gdb/remote.c b/gdb/remote.c
>> index f54a38833b..be118c5d97 100644
>> --- a/gdb/remote.c
>> +++ b/gdb/remote.c
>> @@ -5137,7 +5137,12 @@ remote_detach_1 (int from_tty, inferior *inf)
>>    /* If doing detach-on-fork, we don't mourn, because that will delete
>>       breakpoints that should be available for the followed inferior.  */
>>    if (!is_fork_parent)
>> -    target_mourn_inferior (inferior_ptid);
>> +    {
>> +      target_mourn_inferior (inferior_ptid);
>> +      if (print_inferior_events)
>> +	printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
>> +			   inf->num, pid);
>
> This is still using "(process %d)", which is incorrect
> for target remote.

You're right, sorry about this.

> Also, here, if debugging in "target remote" mode, and this is 
> the last live process, that target_mourn_inferior call will
> unpush the remote target from the target stack.  So this is another
> case where you need to call target_pid_to_str _before_ calling
> target_mourn_inferior.

Good point.  This is fixed.

> As mentioned before, not all targets print "process $decimal" in
> their target_pid_to_str implementation.  So your testsuite changes
> will need to adapt too.
>
> Please try this: hack away gdbserver/server.c's support for
> the RSP multi-process extensions (look for "multiprocess+"), and
> test with "target remote".  That hack will emulate older gdbservers and
> other stubs that don't support telling gdb the process's pid.
> Manually confirm that you get "Remote target" instead of
> "process $pid" -- see remote.c's implementation of
> target_pid_to_str.  Then run the testsuite against that gdbserver.

[ After a quick chat to clarify things...  ]

I will make sure to adjust all regexps and take into account the many
outputs that can happen here.

Thanks,

Patch

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index d43e7f202d..6c9f885bad 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2595,8 +2595,16 @@  kill_command (const char *arg, int from_tty)
     error (_("The program is not being run."));
   if (!query (_("Kill the program being debugged? ")))
     error (_("Not confirmed."));
+
+  std::string pid_str = target_pid_to_str (inferior_ptid);
+  int infnum = current_inferior ()->num;
+
   target_kill ();
 
+  if (print_inferior_events)
+    printf_unfiltered (_("[Inferior %d (%s) has been killed]\n"),
+		       infnum, pid_str.c_str ());
+
   /* If we still have other inferiors to debug, then don't mess with
      with their threads.  */
   if (!have_inferiors ())
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 4ff5712d75..311ef8f8b0 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -45,9 +45,8 @@  DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
 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;
+/* See inferior.h.  */
+int print_inferior_events = 1;
 
 /* The Current Inferior.  This is a strong reference.  I.e., whenever
    an inferior is the current inferior, its refcount is
@@ -121,9 +120,11 @@  struct inferior *
 add_inferior (int pid)
 {
   struct inferior *inf = add_inferior_silent (pid);
+  std::string infpid = target_pid_to_str (pid_to_ptid (pid));
 
   if (print_inferior_events)
-    printf_unfiltered (_("[New inferior %d]\n"), pid);
+    printf_unfiltered (_("[New inferior %d (%s)]\n"),
+		       inf->num, infpid.c_str ());
 
   return inf;
 }
@@ -234,9 +235,6 @@  exit_inferior (int pid)
   struct inferior *inf = find_inferior_pid (pid);
 
   exit_inferior_1 (inf, 0);
-
-  if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d exited]\n"), pid);
 }
 
 void
@@ -261,12 +259,13 @@  void
 detach_inferior (inferior *inf)
 {
   /* Save the pid, since exit_inferior_1 will reset it.  */
-  int pid = inf->pid;
+  std::string infpid = target_pid_to_str (pid_to_ptid (inf->pid));
 
   exit_inferior_1 (inf, 0);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
+    printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
+		       inf->num, infpid.c_str ());
 }
 
 /* See inferior.h.  */
@@ -989,7 +988,7 @@  initialize_inferiors (void)
      can only allocate an inferior when all those modules have done
      that.  Do this after initialize_progspace, due to the
      current_program_space reference.  */
-  current_inferior_ = add_inferior (0);
+  current_inferior_ = add_inferior_silent (0);
   current_inferior_->incref ();
   current_inferior_->pspace = current_program_space;
   current_inferior_->aspace = current_program_space->aspace;
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 391a5fdaa5..bd26c8a86d 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -220,6 +220,10 @@  extern enum stop_stack_kind stop_stack_dummy;
 
 extern int stopped_by_random_signal;
 
+/* Print notices on inferior events (attach, detach, etc.), set with
+   `set print inferior-events'.  */
+extern int print_inferior_events;
+
 /* STEP_OVER_ALL means step over all subroutine calls.
    STEP_OVER_UNDEBUGGABLE means step over calls to undebuggable functions.
    STEP_OVER_NONE means don't step over any subroutine calls.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 9e5e0639ce..b513265698 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -461,14 +461,14 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	      remove_breakpoints_pid (ptid_get_pid (inferior_ptid));
 	    }
 
-	  if (info_verbose || debug_infrun)
+	  if (print_inferior_events)
 	    {
 	      /* Ensure that we have a process ptid.  */
 	      ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
 
 	      target_terminal::ours_for_output ();
 	      fprintf_filtered (gdb_stdlog,
-				_("Detaching after %s from child %s.\n"),
+				_("[Detaching after %s from child %s]\n"),
 				has_vforked ? "vfork" : "fork",
 				target_pid_to_str (process_ptid));
 	    }
@@ -489,7 +489,7 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	  scoped_restore_current_pspace_and_thread restore_pspace_thread;
 
 	  inferior_ptid = child_ptid;
-	  add_thread (inferior_ptid);
+	  add_thread_silent (inferior_ptid);
 	  set_current_inferior (child_inf);
 	  child_inf->symfile_flags = SYMFILE_NO_READ;
 
@@ -549,14 +549,17 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
       struct inferior *parent_inf, *child_inf;
       struct program_space *parent_pspace;
 
-      if (info_verbose || debug_infrun)
+      if (print_inferior_events)
 	{
+	  std::string parent_pid = target_pid_to_str (parent_ptid);
+	  std::string child_pid = target_pid_to_str (child_ptid);
+
 	  target_terminal::ours_for_output ();
 	  fprintf_filtered (gdb_stdlog,
-			    _("Attaching after %s %s to child %s.\n"),
-			    target_pid_to_str (parent_ptid),
+			    _("[Attaching after %s %s to child %s]\n"),
+			    parent_pid.c_str (),
 			    has_vforked ? "vfork" : "fork",
-			    target_pid_to_str (child_ptid));
+			    child_pid.c_str ());
 	}
 
       /* Add the new inferior first, so that the target_detach below
@@ -594,15 +597,15 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	}
       else if (detach_fork)
 	{
-	  if (info_verbose || debug_infrun)
+	  if (print_inferior_events)
 	    {
 	      /* Ensure that we have a process ptid.  */
-	      ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
+	      ptid_t process_ptid = pid_to_ptid (ptid_get_pid (parent_ptid));
 
 	      target_terminal::ours_for_output ();
 	      fprintf_filtered (gdb_stdlog,
-				_("Detaching after fork from "
-				  "child %s.\n"),
+				_("[Detaching after fork from "
+				  "parent %s]\n"),
 				target_pid_to_str (process_ptid));
 	    }
 
@@ -616,7 +619,7 @@  holding the child stopped.  Try \"set detach-on-fork\" or \
 	 informing the solib layer about this new process.  */
 
       inferior_ptid = child_ptid;
-      add_thread (inferior_ptid);
+      add_thread_silent (inferior_ptid);
       set_current_inferior (child_inf);
 
       /* If this is a vfork child, then the address-space is shared
@@ -956,22 +959,22 @@  handle_vfork_child_exec_or_exit (int exec)
 	  inf->aspace = NULL;
 	  inf->pspace = NULL;
 
-	  if (debug_infrun || info_verbose)
+	  if (print_inferior_events)
 	    {
 	      target_terminal::ours_for_output ();
 
 	      if (exec)
 		{
 		  fprintf_filtered (gdb_stdlog,
-				    _("Detaching vfork parent process "
-				      "%d after child exec.\n"),
+				    _("[Detaching vfork parent process "
+				      "%d after child exec]\n"),
 				    inf->vfork_parent->pid);
 		}
 	      else
 		{
 		  fprintf_filtered (gdb_stdlog,
-				    _("Detaching vfork parent process "
-				      "%d after child exit.\n"),
+				    _("[Detaching vfork parent process "
+				      "%d after child exit]\n"),
 				    inf->vfork_parent->pid);
 		}
 	    }
diff --git a/gdb/remote.c b/gdb/remote.c
index f54a38833b..be118c5d97 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5137,7 +5137,12 @@  remote_detach_1 (int from_tty, inferior *inf)
   /* If doing detach-on-fork, we don't mourn, because that will delete
      breakpoints that should be available for the followed inferior.  */
   if (!is_fork_parent)
-    target_mourn_inferior (inferior_ptid);
+    {
+      target_mourn_inferior (inferior_ptid);
+      if (print_inferior_events)
+	printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
+			   inf->num, pid);
+    }
   else
     {
       inferior_ptid = null_ptid;
diff --git a/gdb/testsuite/gdb.base/attach-non-pgrp-leader.exp b/gdb/testsuite/gdb.base/attach-non-pgrp-leader.exp
index dbe554eff3..89f0ecd01b 100644
--- a/gdb/testsuite/gdb.base/attach-non-pgrp-leader.exp
+++ b/gdb/testsuite/gdb.base/attach-non-pgrp-leader.exp
@@ -30,6 +30,7 @@  if { [build_executable ${testfile}.exp ${testfile} $srcfile {debug}] == -1 } {
 
 proc do_test {} {
     global binfile
+    global decimal
 
     set test_spawn_id [spawn_wait_for_attach $binfile]
     set parent_pid [spawn_id_get_pid $test_spawn_id]
@@ -52,7 +53,7 @@  proc do_test {} {
 	}
 
 	gdb_test "detach" \
-	    "Detaching from program: .*process $parent_pid"
+	    "Detaching from program: .*process $parent_pid\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]"
     }
 
     # Start over, and attach to the child this time.
@@ -67,7 +68,7 @@  proc do_test {} {
 	gdb_continue_to_breakpoint "marker"
 
 	gdb_test "detach" \
-	    "Detaching from program: .*process $child_pid"
+	    "Detaching from program: .*process $child_pid\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]"
     }
 
     kill_wait_spawned_process $test_spawn_id
diff --git a/gdb/testsuite/gdb.base/attach.exp b/gdb/testsuite/gdb.base/attach.exp
index efec49e385..173afbcd74 100644
--- a/gdb/testsuite/gdb.base/attach.exp
+++ b/gdb/testsuite/gdb.base/attach.exp
@@ -53,6 +53,7 @@  proc do_attach_tests {} {
     global testfile
     global subdir
     global timeout
+    global decimal
     
     # Figure out a regular expression that will match the sysroot,
     # noting that the default sysroot is "target:", and also noting
@@ -191,7 +192,7 @@  proc do_attach_tests {} {
     # Detach the process.
    
     gdb_test "detach" \
-	"Detaching from program: .*$escapedbinfile, process $testpid" \
+	"Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" \
 	"attach1 detach"
 
     # Wait a bit for gdb to finish detaching
diff --git a/gdb/testsuite/gdb.base/catch-syscall.exp b/gdb/testsuite/gdb.base/catch-syscall.exp
index 2a8bf27e5c..20fa041155 100644
--- a/gdb/testsuite/gdb.base/catch-syscall.exp
+++ b/gdb/testsuite/gdb.base/catch-syscall.exp
@@ -179,7 +179,7 @@  proc check_for_program_end {} {
     # Deleting the catchpoints
     delete_breakpoints
 
-    gdb_continue_to_end
+    gdb_continue_to_end "" continue 1
 }
 
 proc test_catch_syscall_without_args {} {
@@ -250,7 +250,7 @@  proc test_catch_syscall_with_wrong_args {} {
 	# If it doesn't, everything is right (since we don't have
 	# a syscall named "mlock" in it).  Otherwise, this is a failure.
 	set thistest "catch syscall with unused syscall ($syscall_name)"
-	gdb_continue_to_end $thistest
+	gdb_continue_to_end $thistest continue 1
     }
 }
 
diff --git a/gdb/testsuite/gdb.base/foll-fork.exp b/gdb/testsuite/gdb.base/foll-fork.exp
index a715b7fa9d..9e8fe99542 100644
--- a/gdb/testsuite/gdb.base/foll-fork.exp
+++ b/gdb/testsuite/gdb.base/foll-fork.exp
@@ -110,13 +110,13 @@  proc test_follow_fork { who detach cmd } {
 	# Set up the output we expect to see after we run.
 	set expected_re ""
 	if {$who == "child"} {
-	    set expected_re "Attaching after.* fork to.*"
+	    set expected_re "\\\[Attaching after.* fork to.*"
 	    if {$detach == "on"} {
-		append expected_re "Detaching after fork from .*"
+		append expected_re "\\\[Detaching after fork from .*"
 	    }
 	    append expected_re "set breakpoint here.*"
 	} elseif {$who == "parent" && $detach == "on"} {
-	    set expected_re "Detaching after fork from .*set breakpoint here.*"
+	    set expected_re "\\\[Detaching after fork from .*set breakpoint here.*"
 	} else {
 	    set expected_re ".*set breakpoint here.*"
 	}
@@ -217,7 +217,7 @@  proc catch_fork_child_follow {} {
 	"Temporary breakpoint.*, line $bp_after_fork.*" \
 	"set follow-fork child, tbreak"
 
-    set expected_re "Attaching after.* fork to.*Detaching after fork from"
+    set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from"
     append expected_re ".* at .*$bp_after_fork.*"
     gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
 
@@ -305,7 +305,7 @@  proc tcatch_fork_parent_follow {} {
 	"set follow-fork parent, tbreak"
 
     gdb_test "continue" \
-	"Detaching after fork from.* at .*$bp_after_fork.*" \
+	"\\\[Detaching after fork from.* at .*$bp_after_fork.*" \
 	"set follow-fork parent, hit tbreak"
 
     # The child has been detached; allow time for any output it might
@@ -398,10 +398,6 @@  By default, the debugger will follow the parent process..*" \
     if [runto_main] then { tcatch_fork_parent_follow }
 }
 
-# The "Detaching..." and "Attaching..." messages may be hidden by
-# default.
-gdb_test_no_output "set verbose"
-
 # This is a test of gdb's ability to follow the parent, child or both
 # parent and child of a Unix fork() system call.
 #
diff --git a/gdb/testsuite/gdb.base/foll-vfork.exp b/gdb/testsuite/gdb.base/foll-vfork.exp
index 6aa4edd9fe..ddda2d6143 100644
--- a/gdb/testsuite/gdb.base/foll-vfork.exp
+++ b/gdb/testsuite/gdb.base/foll-vfork.exp
@@ -55,10 +55,6 @@  proc setup_gdb {} {
 
     clean_restart $testfile
 
-    # The "Detaching..." and "Attaching..." messages may be hidden by
-    # default.
-    gdb_test_no_output "set verbose"
-
     if ![runto_main] {
 	return -code return
     }
@@ -103,7 +99,7 @@  proc vfork_parent_follow_through_step {} {
 
    set test "step"
    gdb_test_multiple "next" $test {
-       -re "Detaching after vfork from.*if \\(pid == 0\\).*$gdb_prompt " {
+       -re "\\\[Detaching after vfork from.*if \\(pid == 0\\).*$gdb_prompt " {
 	   pass $test
        }
    }
@@ -128,7 +124,7 @@  proc vfork_parent_follow_to_bp {} {
 
    set test "continue to bp"
    gdb_test_multiple "continue" $test {
-       -re ".*Detaching after vfork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
+       -re ".*\\\[Detaching after vfork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
 	   pass $test
        }
    }
@@ -153,7 +149,7 @@  proc vfork_child_follow_to_exit {} {
 	  # PR gdb/14766
 	  fail "$test"
       }
-      -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " {
+       -re "\\\[Attaching after.* vfork to.*\\\[Detaching vfork parent .* after child exit.*$gdb_prompt " {
 	  pass $test
       }
    }
@@ -177,7 +173,7 @@  proc vfork_and_exec_child_follow_to_main_bp {} {
 
    set test "continue to bp"
    gdb_test_multiple "continue" $test {
-      -re "Attaching after.* vfork to.*Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
+      -re "\\\[Attaching after.* vfork to.*\\\[Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
 	  pass $test
       }
    }
@@ -203,7 +199,7 @@  proc vfork_and_exec_child_follow_through_step {} {
    # before it execs.  Thus, "next" lands on the next line after
    # the vfork.
    gdb_test_multiple "next" $test {
-       -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
+       -re "\\\[Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
 	   pass "$test"
        }
    }
@@ -341,7 +337,7 @@  proc vfork_relations_in_info_inferiors { variant } {
 
    set test "step over vfork"
    gdb_test_multiple "next" $test {
-       -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
+       -re "\\\[Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
 	   pass "$test"
        }
    }
diff --git a/gdb/testsuite/gdb.base/fork-print-inferior-events.c b/gdb/testsuite/gdb.base/fork-print-inferior-events.c
new file mode 100644
index 0000000000..182a363dcc
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-print-inferior-events.c
@@ -0,0 +1,37 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2007-2018 Free Software Foundation, Inc.
+
+   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 <stdlib.h>
+#include <unistd.h>
+
+int
+main (int argc, char *argv[])
+{
+  pid_t child;
+
+  child = fork ();
+  switch (child)
+    {
+      case -1:
+	abort ();
+      case 0:
+      default:
+	break;
+    }
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/fork-print-inferior-events.exp b/gdb/testsuite/gdb.base/fork-print-inferior-events.exp
new file mode 100644
index 0000000000..caa01a594e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-print-inferior-events.exp
@@ -0,0 +1,85 @@ 
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2007-2018 Free Software Foundation, Inc.
+
+# 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/>.
+
+# Test that the event messages printed when using 'set print
+# inferior-events [on,off]', 'set follow-fork-mode [child,parent]' and
+# 'set detach-on-fork [on,off]' are the correct ones.
+
+# This test relies on "run", so it cannot run on target remote stubs.
+if { [use_gdb_stub] } {
+    untested "not supported on target remote stubs"
+    return
+}
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+    return -1
+}
+
+# This is the expected output for each of the test combinations
+# below.  The order here is important:
+#
+#    inferior-events: on;  follow-fork: child;  detach-on-fork: on
+#    inferior-events: on;  follow-fork: child;  detach-on-fork: off
+#    inferior-events: on;  follow-fork: parent; detach-on-fork: on
+#    inferior-events: on;  follow-fork: parent; detach-on-fork: off
+#    inferior-events: off; follow-fork: child;  detach-on-fork: on
+#    inferior-events: off; follow-fork: child;  detach-on-fork: off
+#    inferior-events: off; follow-fork: parent; detach-on-fork: on
+#    inferior-events: off; follow-fork: parent; detach-on-fork: off
+
+set reading_re "(Reading.*from remote target\\.\\.\\.\r\n)*"
+set exited_normally_re "${reading_re}\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]"
+# gdbserver produces a slightly different message when attaching after
+# a fork, so we have to tweak the regexp to accomodate that.
+set attach_child_re "${reading_re}\\\[Attaching after (process $decimal\|Thread ${decimal}\\.${decimal}) fork to child (process $decimal\|Thread ${decimal}\\.${decimal})\\\]\r\n"
+set detach_child_re "${reading_re}\\\[Detaching after fork from child process $decimal\\\]\r\n"
+set detach_parent_re "${reading_re}\\\[Detaching after fork from parent process $decimal\\\]\r\n"
+set new_inf_re "${reading_re}\\\[New inferior $decimal \\(process $decimal\\)\\\]\r\n"
+set inf_detached_re "${reading_re}\\\[Inferior $decimal \\(process $decimal\\) detached\\\]\r\n"
+
+set expected_output [list \
+			 "${attach_child_re}${new_inf_re}${detach_parent_re}${inf_detached_re}" \
+			 "${attach_child_re}${new_inf_re}" \
+			 "${detach_child_re}" \
+			 "${new_inf_re}" \
+			 "" \
+			 "" \
+			 "" \
+			 "" \
+			]
+
+set i 0
+
+foreach_with_prefix print_inferior_events { "on" "off" } {
+    foreach_with_prefix follow_fork_mode { "child" "parent" } {
+	foreach_with_prefix detach_on_fork { "on" "off" } {
+	    clean_restart $binfile
+	    gdb_test_no_output "set print inferior-events $print_inferior_events"
+	    gdb_test_no_output "set follow-fork-mode $follow_fork_mode"
+	    gdb_test_no_output "set detach-on-fork $detach_on_fork"
+
+	    set output [lindex $expected_output $i]
+	    # Always add the "Starting program..." string so that we
+	    # match exactly the lines we want.
+	    set output "Starting program: $binfile\\s*\r\n${output}${exited_normally_re}"
+	    set i [expr $i + 1]
+	    gdb_test "run" $output
+	}
+    }
+}
diff --git a/gdb/testsuite/gdb.base/hook-stop.exp b/gdb/testsuite/gdb.base/hook-stop.exp
index fbdfcfe3d6..9b62d57aeb 100644
--- a/gdb/testsuite/gdb.base/hook-stop.exp
+++ b/gdb/testsuite/gdb.base/hook-stop.exp
@@ -78,6 +78,7 @@  proc hook_stop_before_frame {} {
 proc hook_stop_kill {} {
     with_test_prefix "hook-stop kills inferior" {
 	global gdb_prompt
+	global decimal
 
 	setup "kill"
 
@@ -85,7 +86,7 @@  proc hook_stop_kill {} {
 
 	set test "run hook-stop"
 	gdb_test_multiple "continue" "$test" {
-	    -re "Continuing.\r\n${gdb_prompt} $" {
+	    -re "Continuing.\r\n\\\[Inferior $decimal \\((process ${decimal}\|Thread .*)\\) has been killed\\\]\r\n${gdb_prompt} $" {
 		pass $test
 	    }
 	}
diff --git a/gdb/testsuite/gdb.base/kill-after-signal.exp b/gdb/testsuite/gdb.base/kill-after-signal.exp
index 76f9d5af70..056d414a31 100644
--- a/gdb/testsuite/gdb.base/kill-after-signal.exp
+++ b/gdb/testsuite/gdb.base/kill-after-signal.exp
@@ -37,4 +37,8 @@  if ![runto_main] {
 
 gdb_test "continue" "Program received signal SIGUSR1, .*"
 gdb_test "stepi" "\r\nhandler .*"
-gdb_test "kill" "^y" "kill" "Kill the program being debugged\\? \\(y or n\\) $" "y"
+gdb_test_multiple "kill" "kill" {
+    -re "Kill the program being debugged\\? \\(y or n\\) $" {
+       gdb_test "y" "\\\[Inferior $decimal \\((process $decimal\|Thread .*)\\) has been killed\\\]" "kill"
+    }
+}
diff --git a/gdb/testsuite/gdb.base/solib-overlap.exp b/gdb/testsuite/gdb.base/solib-overlap.exp
index 88762be449..bc8a1a13a8 100644
--- a/gdb/testsuite/gdb.base/solib-overlap.exp
+++ b/gdb/testsuite/gdb.base/solib-overlap.exp
@@ -119,7 +119,7 @@  foreach prelink_lib1 {0x40000000 0x50000000} { with_test_prefix "$prelink_lib1"
 
     # Detach the process.
 
-    gdb_test "detach" "Detaching from program: .*$escapedbinfile, process $testpid"
+    gdb_test "detach" "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(process $testpid\\) detached\\\]"
 
     # Wait a bit for gdb to finish detaching
 
diff --git a/gdb/testsuite/gdb.threads/clone-attach-detach.exp b/gdb/testsuite/gdb.threads/clone-attach-detach.exp
index 1fbdc95ffc..d597f2faf6 100644
--- a/gdb/testsuite/gdb.threads/clone-attach-detach.exp
+++ b/gdb/testsuite/gdb.threads/clone-attach-detach.exp
@@ -56,7 +56,7 @@  for {set attempt 1} {$attempt <= $attempts} {incr attempt} {
 	    "1.*${thread_re}.*\r\n.*2.*${thread_re}.*" \
 	    "info threads shows two LWPs"
 
-	gdb_test "detach" "Detaching from .*, process $testpid"
+	gdb_test "detach" "Detaching from .*, process $testpid\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]"
     }
 }
 
@@ -91,7 +91,7 @@  for {set attempt 1} {$attempt <= $attempts} {incr attempt} {
 	    "1.*${thread_re}.*\\(running\\)\r\n.*2.*${thread_re}.*\\(running\\)" \
 	    "info threads shows two LWPs"
 
-	gdb_test "detach" "Detaching from .*, process $testpid"
+	gdb_test "detach" "Detaching from .*, process $testpid\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]"
     }
 }
 
diff --git a/gdb/testsuite/gdb.threads/kill.exp b/gdb/testsuite/gdb.threads/kill.exp
index 61384578e6..cdf26ea4f4 100644
--- a/gdb/testsuite/gdb.threads/kill.exp
+++ b/gdb/testsuite/gdb.threads/kill.exp
@@ -21,7 +21,7 @@  standard_testfile
 # program and spawn several threads before trying to kill the program.
 
 proc test {threaded} {
-    global testfile srcfile
+    global testfile srcfile decimal
 
     with_test_prefix [expr ($threaded)?"threaded":"non-threaded"] {
 
@@ -68,7 +68,11 @@  proc test {threaded} {
 	#
 	# the above would mean that the remote end crashed.
 
-	gdb_test "kill" "^y" "kill program" "Kill the program being debugged\\? \\(y or n\\) $" "y"
+	gdb_test_multiple "kill" "kill" {
+	    -re "Kill the program being debugged\\? \\(y or n\\) $" {
+		gdb_test "y" "\\\[Inferior $decimal \\((process $decimal\|Thread .*)\\) has been killed\\\]" "kill"
+	    }
+	}
     }
 }
 
diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
index e05acb1711..e5dd3bd596 100644
--- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
+++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
@@ -169,7 +169,7 @@  proc do_detach {multi_process cmd child_exit} {
 			 && [target_info gdb_protocol] == "remote"}]
 
     if {$multi_process} {
-	gdb_test "detach" "Detaching from .*, process $decimal" \
+	gdb_test "detach" "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" \
 	    "detach child"
 
 	gdb_test "inferior 1" "\[Switching to inferior $decimal\].*" \
@@ -193,7 +193,7 @@  proc do_detach {multi_process cmd child_exit} {
 	    set extra ""
 	}
 	if {$cmd == "detach"} {
-	    gdb_test "detach" "Detaching from .*, process $decimal$extra"
+	    gdb_test "detach" "Detaching from .*, process ${decimal}\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]$extra"
 	} elseif {$cmd == "continue"} {
 	    gdb_test "continue" $continue_re
 	} else {