public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Always print "Detaching after fork from child..."
@ 2018-01-24 19:47 Sergio Durigan Junior
  2018-01-24 20:43 ` Jan Kratochvil
                   ` (6 more replies)
  0 siblings, 7 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-01-24 19:47 UTC (permalink / raw)
  To: GDB Patches; +Cc: Sergio Durigan Junior

I'd like to propose another patch that we carry on Fedora GDB.

It's a simple patch, whose purpose is to always print the "Detaching
after fork from child %s." message after a fork/vfork happens.
Currently the user can see this message on upstream GDB when she
enables "debug infrun" or sets verbosity to "on".

The first version of this patch was posted by Jan Kratochvil here:

  https://www.sourceware.org/ml/gdb-patches/2007-12/msg00087.html

But after a reply from Daniel Jacobowitz nothing else happened, until
6c95b8df7fe, which actually introduced the messages as part of a
bigger patch by Pedro.

I guess the point here is that informing the user that a fork happened
is a good thing, and shouldn't be something done only when "debug
inferior" or "verbose" are on.  I remember a few occasions when, while
debugging something using Fedora's GDB, I saw this message and was
able to better track the problem.  Last, but not least, this patch is
being carried with Fedora GDB because of an actual complaint:

  https://bugzilla.redhat.com/show_bug.cgi?id=235197

Below you can see what changes on the output.

With a simple test program:

  #include <unistd.h>

  int
  main (int argc, char *argv[])
  {
    fork ();

    return 0;
  }

Here's the current output, without the patch:

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

And here's the output with the patch:

  (gdb) r
  Starting program: /tmp/a.out
  Detaching after fork from child process 24905.
  [Inferior 1 (process 24901) exited normally]

I've had to adapt gdb.base/catch-syscall.exp's usage of
"gdb_continue_to_end", but other than that there are no regressions
introduced by this change.

gdb/ChangeLog:
2018-01-24  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>

	* infrun.c (): Always print 'fork detach' message.

gdb/testsuite/ChangeLog:
2018-01-24  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.base/catch-syscall.exp (check_for_program_end): Adjust
	"gdb_continue_to_end".
	(test_catch_syscall_with_wrong_args): Likewise.
	* gdb.base/fork-detach-info.c: New file.
	* gdb.base/fork-detach-info.exp: New file.
---
 gdb/infrun.c                                | 17 ++++++-------
 gdb/testsuite/gdb.base/catch-syscall.exp    |  4 ++--
 gdb/testsuite/gdb.base/fork-detach-info.c   | 37 +++++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/fork-detach-info.exp | 27 +++++++++++++++++++++
 4 files changed, 73 insertions(+), 12 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/fork-detach-info.c
 create mode 100644 gdb/testsuite/gdb.base/fork-detach-info.exp

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 45fe36a717..cca6e22bb4 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -461,17 +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)
-	    {
-	      /* Ensure that we have a process ptid.  */
-	      ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
+	  /* 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"),
-				has_vforked ? "vfork" : "fork",
-				target_pid_to_str (process_ptid));
-	    }
+	  target_terminal::ours_for_output ();
+	  fprintf_filtered (gdb_stdlog,
+			    _("Detaching after %s from child %s.\n"),
+			    has_vforked ? "vfork" : "fork",
+			    target_pid_to_str (process_ptid));
 	}
       else
 	{
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/fork-detach-info.c b/gdb/testsuite/gdb.base/fork-detach-info.c
new file mode 100644
index 0000000000..182a363dcc
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-detach-info.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-detach-info.exp b/gdb/testsuite/gdb.base/fork-detach-info.exp
new file mode 100644
index 0000000000..b58c4ba6bf
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-detach-info.exp
@@ -0,0 +1,27 @@
+# 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/>.
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+    return -1
+}
+
+set test "print detach message after fork"
+gdb_test "r" \
+    "Detaching after fork from child process $decimal\.\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    $test
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Always print "Detaching after fork from child..."
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
@ 2018-01-24 20:43 ` Jan Kratochvil
  2018-01-24 20:56   ` Sergio Durigan Junior
  2018-01-31 16:57 ` [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages Sergio Durigan Junior
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 33+ messages in thread
From: Jan Kratochvil @ 2018-01-24 20:43 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: GDB Patches

On Wed, 24 Jan 2018 20:47:14 +0100, Sergio Durigan Junior wrote:
>   https://bugzilla.redhat.com/show_bug.cgi?id=235197

As a justification for this patch:

------------------------------------------------------------------------------
cat >fork2.c <<EOH
#include <unistd.h>
#include <stdio.h>
static void printit(void) {
  puts("printed");
}
int main(void) {
  if (!fork()) printit();
  return 0;
}
EOH
gcc -o fork2 fork2.c -Wall -g
gdb -q ./fork2
(gdb) b printit
Breakpoint 1 at 0x40052b: file fork2.c, line 4.
(gdb) r
Starting program: /quad/home/jkratoch/t/fork2 
printed
[Inferior 1 (process 15812) exited normally]
(gdb) q
------------------------------------------------------------------------------

As the GDB user does not expect the program could do any forks s/he is
confused the breakpoint did not get hit and assumes GDB is just broken.

But then I cannot say this patch is too great, it produces many uninteresting 
	Detaching after fork from child process 24905.
messages rather just annoying in most cases.  So nowadays I feel the message
is more an excuse how to show it is user's fault s/he did not read it.
But I think nobody reads them as there are too many such messages.

I believe the right fix would be to make "set detach-on-fork off" the default.
But that is sure a new can of worms I do not want to speculate about.


Jan

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Always print "Detaching after fork from child..."
  2018-01-24 20:43 ` Jan Kratochvil
@ 2018-01-24 20:56   ` Sergio Durigan Junior
  2018-01-25 15:59     ` Pedro Alves
  0 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-01-24 20:56 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: GDB Patches

On Wednesday, January 24 2018, Jan Kratochvil wrote:

> On Wed, 24 Jan 2018 20:47:14 +0100, Sergio Durigan Junior wrote:
>>   https://bugzilla.redhat.com/show_bug.cgi?id=235197
>
> As a justification for this patch:
>
> ------------------------------------------------------------------------------
> cat >fork2.c <<EOH
> #include <unistd.h>
> #include <stdio.h>
> static void printit(void) {
>   puts("printed");
> }
> int main(void) {
>   if (!fork()) printit();
>   return 0;
> }
> EOH
> gcc -o fork2 fork2.c -Wall -g
> gdb -q ./fork2
> (gdb) b printit
> Breakpoint 1 at 0x40052b: file fork2.c, line 4.
> (gdb) r
> Starting program: /quad/home/jkratoch/t/fork2 
> printed
> [Inferior 1 (process 15812) exited normally]
> (gdb) q
> ------------------------------------------------------------------------------
>
> As the GDB user does not expect the program could do any forks s/he is
> confused the breakpoint did not get hit and assumes GDB is just broken.

Thanks for the extra justification and the useful example.

> But then I cannot say this patch is too great, it produces many uninteresting 
> 	Detaching after fork from child process 24905.
> messages rather just annoying in most cases.  So nowadays I feel the message
> is more an excuse how to show it is user's fault s/he did not read it.
> But I think nobody reads them as there are too many such messages.

I understand where you're coming from, but I still think this is a good
patch because I read the messages, and as I said, they even helped me in
one occasion.

> I believe the right fix would be to make "set detach-on-fork off" the default.
> But that is sure a new can of worms I do not want to speculate about.

Yeah, I can see the rationale for this, and I think it's an idea worth
discussing.  Not sure what others think, but I also don't want deviate
much from what the current patch is proposing.

Thanks,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Always print "Detaching after fork from child..."
  2018-01-24 20:56   ` Sergio Durigan Junior
@ 2018-01-25 15:59     ` Pedro Alves
  2018-01-25 20:21       ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-01-25 15:59 UTC (permalink / raw)
  To: Sergio Durigan Junior, Jan Kratochvil; +Cc: GDB Patches

On 01/24/2018 08:56 PM, Sergio Durigan Junior wrote:
> On Wednesday, January 24 2018, Jan Kratochvil wrote:
> 
>> On Wed, 24 Jan 2018 20:47:14 +0100, Sergio Durigan Junior wrote:
>>>   https://bugzilla.redhat.com/show_bug.cgi?id=235197
>>
>> As a justification for this patch:
>>
>> ------------------------------------------------------------------------------
>> cat >fork2.c <<EOH
>> #include <unistd.h>
>> #include <stdio.h>
>> static void printit(void) {
>>   puts("printed");
>> }
>> int main(void) {
>>   if (!fork()) printit();
>>   return 0;
>> }
>> EOH
>> gcc -o fork2 fork2.c -Wall -g
>> gdb -q ./fork2
>> (gdb) b printit
>> Breakpoint 1 at 0x40052b: file fork2.c, line 4.
>> (gdb) r
>> Starting program: /quad/home/jkratoch/t/fork2 
>> printed
>> [Inferior 1 (process 15812) exited normally]
>> (gdb) q
>> ------------------------------------------------------------------------------
>>
>> As the GDB user does not expect the program could do any forks s/he is
>> confused the breakpoint did not get hit and assumes GDB is just broken.
> 
> Thanks for the extra justification and the useful example.
> 
>> But then I cannot say this patch is too great, it produces many uninteresting 
>> 	Detaching after fork from child process 24905.
>> messages rather just annoying in most cases.  So nowadays I feel the message
>> is more an excuse how to show it is user's fault s/he did not read it.
>> But I think nobody reads them as there are too many such messages.
> 
> I understand where you're coming from, but I still think this is a good
> patch because I read the messages, and as I said, they even helped me in
> one occasion.

I'm of two minds here.  On the one hand, "help, GDB doesn't stop at
my breakpoint!" is a FAQ on IRC.  OTOH, I also worry a bit about
printing too many messages.

Maybe the middle ground is having a way to toggle this output on/off
other than the over-broad "set verbose".  An idea would be to make it
conditional on the existing "set print inferior-events" instead, and
flip "set print inferior-events" on by default, like
"set print thread-events" is on by default.  One trouble with the
"set print inferior-events" setting is that currently it prints
redundant info, like:

 ~~
 [Inferior 1 (process 2629) exited normally]
 [Inferior 2629 exited]
 ~~

But we can fix that, I think.  See a quick straw-man patch below.
(Really just a a straw-man; there's more redundancy if you do "follow-fork child",
for example.)

> 
>> I believe the right fix would be to make "set detach-on-fork off" the default.
>> But that is sure a new can of worms I do not want to speculate about.
> 
> Yeah, I can see the rationale for this, and I think it's an idea worth
> discussing.  Not sure what others think, but I also don't want deviate
> much from what the current patch is proposing.

Yeah, I'm wondered about doing that too, though I don't think we're ready
for that yet.  For example, in all-stop mode, execution stops whenever one of
the children exit.  The user then has to switch back to the parent, and
resume it.  (Something Tromey was looking at a while back.)  Once things
like that are sorted out, then we can reconsider.  It may be a useful
exercise to add "set detach-on-fork off" in one's ~/.gdb_init.  Until that
works neatly, we can't consider flipping the default, IMO.

Thanks,
Pedro Alves

From c222c0b2f1bce4b23245681222461df9e75e497e Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Thu, 25 Jan 2018 15:42:37 +0000
Subject: [PATCH] inferior-events

---
 gdb/inferior.c | 7 ++-----
 gdb/infrun.c   | 9 ++++++---
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 38b7369275b..70b54531a95 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -47,7 +47,7 @@ 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;
+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
@@ -232,9 +232,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
@@ -986,7 +983,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/infrun.c b/gdb/infrun.c
index 45fe36a7175..9d968be121d 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -408,6 +408,9 @@ show_follow_fork_mode_string (struct ui_file *file, int from_tty,
 }
 \f
 
+/* Putme in header.  */
+extern int print_inferior_events;
+
 /* Handle changes to the inferior list based on the type of fork,
    which process is being followed, and whether the other process
    should be detached.  On entry inferior_ptid must be the ptid of
@@ -461,7 +464,7 @@ 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));
@@ -549,7 +552,7 @@ 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)
 	{
 	  target_terminal::ours_for_output ();
 	  fprintf_filtered (gdb_stdlog,
@@ -594,7 +597,7 @@ 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));
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Always print "Detaching after fork from child..."
  2018-01-25 15:59     ` Pedro Alves
@ 2018-01-25 20:21       ` Sergio Durigan Junior
  2018-01-25 22:39         ` Pedro Alves
  0 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-01-25 20:21 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Jan Kratochvil, GDB Patches

On Thursday, January 25 2018, Pedro Alves wrote:

> On 01/24/2018 08:56 PM, Sergio Durigan Junior wrote:
>> On Wednesday, January 24 2018, Jan Kratochvil wrote:
>> 
>>> On Wed, 24 Jan 2018 20:47:14 +0100, Sergio Durigan Junior wrote:
>>>>   https://bugzilla.redhat.com/show_bug.cgi?id=235197
>>>
>>> As a justification for this patch:
>>>
>>> ------------------------------------------------------------------------------
>>> cat >fork2.c <<EOH
>>> #include <unistd.h>
>>> #include <stdio.h>
>>> static void printit(void) {
>>>   puts("printed");
>>> }
>>> int main(void) {
>>>   if (!fork()) printit();
>>>   return 0;
>>> }
>>> EOH
>>> gcc -o fork2 fork2.c -Wall -g
>>> gdb -q ./fork2
>>> (gdb) b printit
>>> Breakpoint 1 at 0x40052b: file fork2.c, line 4.
>>> (gdb) r
>>> Starting program: /quad/home/jkratoch/t/fork2 
>>> printed
>>> [Inferior 1 (process 15812) exited normally]
>>> (gdb) q
>>> ------------------------------------------------------------------------------
>>>
>>> As the GDB user does not expect the program could do any forks s/he is
>>> confused the breakpoint did not get hit and assumes GDB is just broken.
>> 
>> Thanks for the extra justification and the useful example.
>> 
>>> But then I cannot say this patch is too great, it produces many uninteresting 
>>> 	Detaching after fork from child process 24905.
>>> messages rather just annoying in most cases.  So nowadays I feel the message
>>> is more an excuse how to show it is user's fault s/he did not read it.
>>> But I think nobody reads them as there are too many such messages.
>> 
>> I understand where you're coming from, but I still think this is a good
>> patch because I read the messages, and as I said, they even helped me in
>> one occasion.
>
> I'm of two minds here.  On the one hand, "help, GDB doesn't stop at
> my breakpoint!" is a FAQ on IRC.  OTOH, I also worry a bit about
> printing too many messages.
>
> Maybe the middle ground is having a way to toggle this output on/off
> other than the over-broad "set verbose".  An idea would be to make it
> conditional on the existing "set print inferior-events" instead, and
> flip "set print inferior-events" on by default, like
> "set print thread-events" is on by default.  One trouble with the
> "set print inferior-events" setting is that currently it prints
> redundant info, like:
>
>  ~~
>  [Inferior 1 (process 2629) exited normally]
>  [Inferior 2629 exited]
>  ~~
>
> But we can fix that, I think.  See a quick straw-man patch below.
> (Really just a a straw-man; there's more redundancy if you do "follow-fork child",
> for example.)

Thanks for the reply and the patch.  It is my understanding that you
would like it to be extended in order to avoid redundancy in other
cases.  I will take a look at it.

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Always print "Detaching after fork from child..."
  2018-01-25 20:21       ` Sergio Durigan Junior
@ 2018-01-25 22:39         ` Pedro Alves
  0 siblings, 0 replies; 33+ messages in thread
From: Pedro Alves @ 2018-01-25 22:39 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: Jan Kratochvil, GDB Patches

On 01/25/2018 08:20 PM, Sergio Durigan Junior wrote:
> On Thursday, January 25 2018, Pedro Alves wrote:

>> But we can fix that, I think.  See a quick straw-man patch below.
>> (Really just a a straw-man; there's more redundancy if you do "follow-fork child",
>> for example.)
> 
> Thanks for the reply and the patch.  It is my understanding that you
> would like it to be extended in order to avoid redundancy in other
> cases.  I will take a look at it.
Yes, if this is the direction to go then we'll need to make sure
that the output of both "set print inferior-events off/on" looks
reasonable in the multiple use cases that spawn / attach /
fork / detach / kill etc. inferiors, with both native and
remote debugging.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
  2018-01-24 20:43 ` Jan Kratochvil
@ 2018-01-31 16:57 ` Sergio Durigan Junior
  2018-02-01 17:17   ` Pedro Alves
  2018-03-09 21:56 ` [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages Sergio Durigan Junior
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-01-31 16:57 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves, Sergio Durigan Junior

This is a followup of Pedro's suggestion to turn 'set print
inferior-events' always on, and do some cleanup on the messages
printed by GDB when attaching/detaching a inferior, or when it exits.

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.

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 22995.
  [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.

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.

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

Built and regtested on BuildBot, without regressions.

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

	* inferior.c (print_inferior_events): Remove 'static'.  Set as
	'1'.
	(exit_inferior): Remove 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 '[Detaching...]'
	message when 'print_inferior_events' is on.  Use
	'add_thread_silent' instead of 'add_thread'.  Add '[' and ']'
	as prefix/suffix for 'Detaching...' message.
	(handle_vfork_child_exec_or_exit): Add '[' and ']' as
	prefix/suffix when printing 'Detaching...' message.  Print
	them when 'print_inferior_events' is on.

gdb/testsuite/ChangeLog:
2018-01-30  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.base/attach-non-pgrp-leader.exp: Adjust 'Detaching...'
	regexps to expect for '[Inferior %d 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-detach-info.c: New file.
	* gdb.base/fork-detach-info.exp: New file.
	* gdb.threads/clone-attach-detach.exp: Adjust 'Detaching...'
	regexps to expect for '[Inferior %d detached]' as well.
	* gdb.threads/process-dies-while-detaching.exp: Likewise.
---
 gdb/inferior.c                                     | 10 +---
 gdb/inferior.h                                     |  4 ++
 gdb/infrun.c                                       | 28 ++++-----
 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/testsuite/gdb.base/fork-detach-info.c          | 37 ++++++++++++
 gdb/testsuite/gdb.base/fork-detach-info.exp        | 68 ++++++++++++++++++++++
 gdb/testsuite/gdb.threads/clone-attach-detach.exp  |  4 +-
 .../gdb.threads/process-dies-while-detaching.exp   |  6 +-
 12 files changed, 149 insertions(+), 50 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/fork-detach-info.c
 create mode 100644 gdb/testsuite/gdb.base/fork-detach-info.exp

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 38b7369275..5bff8b3c07 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
@@ -232,9 +231,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
@@ -986,7 +982,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 22008a005b..6550d50d53 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 45fe36a717..4ac5bde303 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,11 +549,11 @@ 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)
 	{
 	  target_terminal::ours_for_output ();
 	  fprintf_filtered (gdb_stdlog,
-			    _("Attaching after %s %s to child %s.\n"),
+			    _("[Attaching after %s %s to child %s.]\n"),
 			    target_pid_to_str (parent_ptid),
 			    has_vforked ? "vfork" : "fork",
 			    target_pid_to_str (child_ptid));
@@ -594,15 +594,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));
 
 	      target_terminal::ours_for_output ();
 	      fprintf_filtered (gdb_stdlog,
-				_("Detaching after fork from "
-				  "child %s.\n"),
+				_("[Detaching after fork from "
+				  "child %s.]\n"),
 				target_pid_to_str (process_ptid));
 	    }
 
@@ -616,7 +616,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 +956,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/testsuite/gdb.base/attach-non-pgrp-leader.exp b/gdb/testsuite/gdb.base/attach-non-pgrp-leader.exp
index dbe554eff3..6da2751b45 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 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 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 d315c2792e..42cf6b8328 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
@@ -194,7 +195,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 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-detach-info.c b/gdb/testsuite/gdb.base/fork-detach-info.c
new file mode 100644
index 0000000000..182a363dcc
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-detach-info.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-detach-info.exp b/gdb/testsuite/gdb.base/fork-detach-info.exp
new file mode 100644
index 0000000000..aa9a85c0d5
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-detach-info.exp
@@ -0,0 +1,68 @@
+# 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/>.
+
+if { [use_gdb_stub] || [target_info gdb_protocol] == "extended-remote" } {
+    untested "not supported on gdbserver"
+    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:
+#
+#    follow-fork: child; detach-on-fork: on; inferior-events: on
+#    follow-fork: child; detach-on-fork: on; inferior-events: off
+#    follow-fork: child; detach-on-fork: off; inferior-events: on
+#    follow-fork: child; detach-on-fork: off; inferior-events: off
+#    follow-fork: parent; detach-on-fork: on; inferior-events: on
+#    follow-fork: parent; detach-on-fork: on; inferior-events: off
+#    follow-fork: parent; detach-on-fork: off; inferior-events: on
+#    follow-fork: parent; detach-on-fork: off; inferior-events: off
+set expected_output [list \
+    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal detached\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
+    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]"]
+
+set i 0
+
+foreach_with_prefix follow_fork_mode { "child" "parent" } {
+    foreach_with_prefix detach_on_fork { "on" "off" } {
+	foreach_with_prefix print_inferior_events { "on" "off" } {
+	    clean_restart $binfile
+	    gdb_test_no_output "set follow-fork-mode $follow_fork_mode"
+	    gdb_test_no_output "set detach-on-fork $detach_on_fork"
+	    gdb_test_no_output "set print inferior-events $print_inferior_events"
+
+	    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"
+	    set i [expr $i + 1]
+	    gdb_test "run" $output
+	}
+    }
+}
diff --git a/gdb/testsuite/gdb.threads/clone-attach-detach.exp b/gdb/testsuite/gdb.threads/clone-attach-detach.exp
index 1fbdc95ffc..07b10f534b 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 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 detached\\\]"
     }
 }
 
diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
index e05acb1711..515c25c341 100644
--- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
+++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
@@ -82,7 +82,7 @@ proc detach_and_expect_exit {inf_output_re test} {
     global gdb_prompt
 
     return_if_fail [gdb_test_multiple "detach" $test {
-	-re "Detaching from .*, process $decimal" {
+	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal detached\\\]" {
 	}
     }]
 
@@ -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 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 detached\\\]$extra"
 	} elseif {$cmd == "continue"} {
 	    gdb_test "continue" $continue_re
 	} else {
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages
  2018-01-31 16:57 ` [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages Sergio Durigan Junior
@ 2018-02-01 17:17   ` Pedro Alves
  2018-03-06  1:44     ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-02-01 17:17 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

[-- Attachment #1: Type: text/plain, Size: 8855 bytes --]

On 01/31/2018 04:57 PM, Sergio Durigan Junior wrote:
> This is a followup of Pedro's suggestion to turn 'set print
> inferior-events' always on, and do some cleanup on the messages
> printed by GDB when attaching/detaching a inferior, or when it exits.
> 
> 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.
> 
> 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 22995.
>   [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.
> 
> 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.

Can you give examples of the latter?  (what led to 'add_{thread,inferior}_silent')

> 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
> 
> Built and regtested on BuildBot, without regressions.

Can you update the log to include examples of what the new
output looks like?  Both "set inferior-events on/off".
That makes it much easier to review/discuss/evaluate.

Can you say what happens to the output with
"set print inferior-events off" compared to current master?
Does output change in that case compared to today?  Not changing
output is not the goal, but it'd be good to be clear.

I'm experimenting a bit, and I'm seeing some inconsistencies
that I wonder whether we should consider revising.

For example, with "set print inferior-events on/off", we see,
for detach:

 (gdb) detach
 Detaching from program: /home/pedro/gdb/build/gdb/testsuite/outputs/gdb.base/foll-fork/foll-fork, process 2685
 [Inferior 2685 detached]
 (gdb) 

But for kill, we see:

 (gdb) k
 Kill the program being debugged? (y or n) y
 (gdb) 

Should detach vs kill be consistent?  I'd think so off hand.

Another thing is related to what I was proposing in the other
thread.  With the patch, we say:

 (gdb) c
 Continuing.
 [New inferior 3239]
 [Inferior 5 (process 3236) exited normally]

The "New inferior 3239" one is showing the target process ID,
which is confusing.  I think we should tweak to mention the
inferior ID, like:

 (gdb) c
 Continuing.
 [New inferior 5 (process 3236)]
 [Inferior 5 (process 3236) exited normally]


Looking at follow-fork child, detach-on-fork on, we see:

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

That could use some normalization to make messages consistent
with one another.

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

(see more about this case below)

>  
>  	      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));

I also noticed that this ends up with a period at the end, while
other [] notices don't, like (playing with "set detach-on-fork on"):

 [Detaching after fork from child process 3417.]
 [Inferior 5 (process 3416) exited normally]

> diff --git a/gdb/testsuite/gdb.base/fork-detach-info.exp b/gdb/testsuite/gdb.base/fork-detach-info.exp
> new file mode 100644
> index 0000000000..aa9a85c0d5
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/fork-detach-info.exp

Maybe this could use a different name, since this isn't
just about _detach_ info?

fork-print-inferior-events.exp, maybe.

> @@ -0,0 +1,68 @@
> +# 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/>.
> +

Please add an intro comment mentioning what the testcase is about.

> +if { [use_gdb_stub] || [target_info gdb_protocol] == "extended-remote" } {
> +    untested "not supported on gdbserver"
> +    return
> +}

I see that this relies on "run", so can't work with use_gdb_stub.
But why is this not supported with extended-remote gdbserver?

> +
> +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:
> +#
> +#    follow-fork: child; detach-on-fork: on; inferior-events: on
> +#    follow-fork: child; detach-on-fork: on; inferior-events: off
> +#    follow-fork: child; detach-on-fork: off; inferior-events: on
> +#    follow-fork: child; detach-on-fork: off; inferior-events: off
> +#    follow-fork: parent; detach-on-fork: on; inferior-events: on
> +#    follow-fork: parent; detach-on-fork: on; inferior-events: off
> +#    follow-fork: parent; detach-on-fork: off; inferior-events: on
> +#    follow-fork: parent; detach-on-fork: off; inferior-events: off
> +set expected_output [list \
> +    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal detached\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]"]
> +

I had a lot of trouble making sense of this big blurb of expected output.
I didn't know exactly what to suggest, so I played with it locally, and
I think putting the individual messages in variables helps a lot.
And then, I noticed that all the cases end up with 

  \\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]

so we can factor it out.

And then, if we iterate over print_inferior_events first, then
it's super clear that the "set print-inferior-events off"
case is always "no output".  See attached patch on top of yours.

The infrun.c hunk is there in my patch because while playing with
this, I noticed that gdb says that it's detaching the child
(and uses the child's pid), when in fact it is detaching the
parent!

I.e., currently, "set follow-fork child", "set detach-on-fork on":

 [Attaching after process 12069 fork to child process 12069.]
 [New inferior 2 12073]
 [Detaching after fork from child process 12073.]   <<< nope, it's detaching 12069 / the parent
 [Inferior 12069 detached]                          <<< see?
 [Inferior 2 (process 12073) exited normally]

after that fix:

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

Thanks,
Pedro Alves

[-- Attachment #2: 0001-inferior-events.patch --]
[-- Type: text/x-patch, Size: 5345 bytes --]

From 7394aafa8f9126521c12e3a822666f75906059a3 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Thu, 1 Feb 2018 17:11:43 +0000
Subject: [PATCH] inferior-events

---
 gdb/infrun.c                                |  4 +--
 gdb/testsuite/gdb.base/fork-detach-info.exp | 51 +++++++++++++++++------------
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 4ac5bde3035..85feedf6d4e 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -597,12 +597,12 @@ holding the child stopped.  Try \"set detach-on-fork\" or \
 	  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"),
+				  "parent %s.]\n"),
 				target_pid_to_str (process_ptid));
 	    }
 
diff --git a/gdb/testsuite/gdb.base/fork-detach-info.exp b/gdb/testsuite/gdb.base/fork-detach-info.exp
index aa9a85c0d5d..bc4aa5edcdd 100644
--- a/gdb/testsuite/gdb.base/fork-detach-info.exp
+++ b/gdb/testsuite/gdb.base/fork-detach-info.exp
@@ -29,38 +29,47 @@ if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
 # This is the expected output for each of the test combinations
 # below.  The order here is important:
 #
-#    follow-fork: child; detach-on-fork: on; inferior-events: on
-#    follow-fork: child; detach-on-fork: on; inferior-events: off
-#    follow-fork: child; detach-on-fork: off; inferior-events: on
-#    follow-fork: child; detach-on-fork: off; inferior-events: off
-#    follow-fork: parent; detach-on-fork: on; inferior-events: on
-#    follow-fork: parent; detach-on-fork: on; inferior-events: off
-#    follow-fork: parent; detach-on-fork: off; inferior-events: on
-#    follow-fork: parent; detach-on-fork: off; inferior-events: off
+#    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 exited_normally_re "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]"
+set attach_child_re "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n"
+set detach_child_re "\\\[Detaching after fork from child process $decimal\\.\\\]\r\n"
+set detach_parent_re "\\\[Detaching after fork from parent process $decimal\\.\\\]\r\n"
+set new_inf_re "\\\[New inferior $decimal\\\]\r\n"
+set inf_detached_re "\\\[Inferior $decimal detached\\\]\r\n"
+
 set expected_output [list \
-    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal detached\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
-    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]"]
+			 "${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 follow_fork_mode { "child" "parent" } {
-    foreach_with_prefix detach_on_fork { "on" "off" } {
-	foreach_with_prefix print_inferior_events { "on" "off" } {
+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"
-	    gdb_test_no_output "set print inferior-events $print_inferior_events"
 
 	    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"
+	    set output "Starting program: $binfile\\s*\r\n$output${exited_normally_re}"
 	    set i [expr $i + 1]
 	    gdb_test "run" $output
 	}
-- 
2.14.3


^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages
  2018-02-01 17:17   ` Pedro Alves
@ 2018-03-06  1:44     ` Sergio Durigan Junior
  0 siblings, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-03-06  1:44 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Thursday, February 01 2018, Pedro Alves wrote:

> On 01/31/2018 04:57 PM, Sergio Durigan Junior wrote:
>> This is a followup of Pedro's suggestion to turn 'set print
>> inferior-events' always on, and do some cleanup on the messages
>> printed by GDB when attaching/detaching a inferior, or when it exits.
>> 
>> 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.
>> 
>> 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 22995.
>>   [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.
>> 
>> 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.
>
> Can you give examples of the latter?  (what led to 'add_{thread,inferior}_silent')

[ Resurrecting the thread... ]

Yes.

Using add_thread, set print inferior-events on, set follow-fork-mode
child, and set detach-on-fork on, I see:

  (gdb) run
  Starting program: a.out
  [Attaching after process 25088 fork to child process 25088.]
  [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 using add_thread_silent (with the same configuration as before):

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

So we get rid of one duplicated line, produced by add_thread.

As for the add_inferior_silent case (on
inferiors.c:initialize_inferiors), it's useful to get rid of the:

  [New inferior 0]

message that happens when initializing GDB.

>> 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
>> 
>> Built and regtested on BuildBot, without regressions.
>
> Can you update the log to include examples of what the new
> output looks like?  Both "set inferior-events on/off".
> That makes it much easier to review/discuss/evaluate.

Yes, that's a good idea.  Sorry for not doing that earlier.

> Can you say what happens to the output with
> "set print inferior-events off" compared to current master?
> Does output change in that case compared to today?  Not changing
> output is not the goal, but it'd be good to be clear.

Based on a test using an inferior that just forks:

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

And with set follow-fork-mode child, current master does:

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

And my patch does:

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

So yeah, we end up losing the notification of "[New process %d]" there,
which comes from thread.c:add_thread_with_info.

Pedantically, the "[Inferior 2 ... exited normally]" message should also
be considered an inferior event, and maybe should not be printed.

> I'm experimenting a bit, and I'm seeing some inconsistencies
> that I wonder whether we should consider revising.
>
> For example, with "set print inferior-events on/off", we see,
> for detach:
>
>  (gdb) detach
>  Detaching from program: /home/pedro/gdb/build/gdb/testsuite/outputs/gdb.base/foll-fork/foll-fork, process 2685
>  [Inferior 2685 detached]
>  (gdb) 
>
> But for kill, we see:
>
>  (gdb) k
>  Kill the program being debugged? (y or n) y
>  (gdb) 
>
> Should detach vs kill be consistent?  I'd think so off hand.

Hm, alright.  I can modify kill_command and print the message.  WDYT:

  (gdb) kill 
  Kill the program being debugged? (y or n) y
  [Inferior 14161 has been killed]

?

> Another thing is related to what I was proposing in the other
> thread.  With the patch, we say:
>
>  (gdb) c
>  Continuing.
>  [New inferior 3239]
>  [Inferior 5 (process 3236) exited normally]
>
> The "New inferior 3239" one is showing the target process ID,
> which is confusing.  I think we should tweak to mention the
> inferior ID, like:
>
>  (gdb) c
>  Continuing.
>  [New inferior 5 (process 3236)]
>  [Inferior 5 (process 3236) exited normally]

This looks a bit redundant to me, but I think it's the best way (at
least I couldn't think of a better one).

Consider it done.

> Looking at follow-fork child, detach-on-fork on, we see:
>
>  [Attaching after process 12069 fork to child process 12069.]
>  [New inferior 12073]
>  [Detaching after fork from child process 12073.]
>  [Inferior 12069 detached]
>  [Inferior 2 (process 12073) exited normally]
>
> That could use some normalization to make messages consistent
> with one another.
>
>  [Attaching after process 12069 fork to child process 12069.]
>  [New inferior 2 (process 12073)]
>  [Detaching after fork from child process 12073.]
>  [Inferior 1 (process 12069) detached]
>  [Inferior 2 (process 12073) exited normally]
>
> (see more about this case below)
>
>>  
>>  	      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));
>
> I also noticed that this ends up with a period at the end, while
> other [] notices don't, like (playing with "set detach-on-fork on"):
>
>  [Detaching after fork from child process 3417.]
>  [Inferior 5 (process 3416) exited normally]

Good point, I'll remove the periods.

>
>> diff --git a/gdb/testsuite/gdb.base/fork-detach-info.exp b/gdb/testsuite/gdb.base/fork-detach-info.exp
>> new file mode 100644
>> index 0000000000..aa9a85c0d5
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.base/fork-detach-info.exp
>
> Maybe this could use a different name, since this isn't
> just about _detach_ info?
>
> fork-print-inferior-events.exp, maybe.

Renamed.

>> @@ -0,0 +1,68 @@
>> +# 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/>.
>> +
>
> Please add an intro comment mentioning what the testcase is about.

Done.

>> +if { [use_gdb_stub] || [target_info gdb_protocol] == "extended-remote" } {
>> +    untested "not supported on gdbserver"
>> +    return
>> +}
>
> I see that this relies on "run", so can't work with use_gdb_stub.
> But why is this not supported with extended-remote gdbserver?

Just because, for extended-remote, the messages are a bit different:

  [Attaching after Thread 7738.7738 fork to child Thread 7738.7738]
  [New inferior 2 (process 7739)]
  [Detaching after fork from parent process 7738]
  [Inferior 7738 detached]
  [Inferior 2 (process 7739) exited normally]

This case is easy, it would just require an adjustment to the first line
(instead of "process $decimal" we'd expect "Thread
$decimal\.$decimal").  There are more annoying cases, like:

  Reading /lib64/ld-linux-x86-64.so.2 from remote target...
  Reading /lib64/ld-2.26.so.debug from remote target...
  Reading /lib64/.debug/ld-2.26.so.debug from remote target...
  Reading /lib64/libm.so.6 from remote target...
  Reading /lib64/libc.so.6 from remote target...
  Reading /lib64/libm-2.26.so.debug from remote target...
  Reading /lib64/.debug/libm-2.26.so.debug from remote target...
  Reading /lib64/libc-2.26.so.debug from remote target...
  Reading /lib64/.debug/libc-2.26.so.debug from remote target...
  [Attaching after Thread 7762.7762 fork to child Thread 7762.7762]
  [New inferior 2 (process 7763)]
  Reading /lib64/libm.so.6 from remote target...
  Reading /lib64/libc.so.6 from remote target...
  Reading /lib64/ld-linux-x86-64.so.2 from remote target...
  Reading /lib64/libm-2.26.so.debug from remote target...
  Reading /lib64/.debug/libm-2.26.so.debug from remote target...
  Reading /lib64/libc-2.26.so.debug from remote target...
  Reading /lib64/.debug/libc-2.26.so.debug from remote target...
  Reading /lib64/ld-2.26.so.debug from remote target...
  Reading /lib64/.debug/ld-2.26.so.debug from remote target...
  Reading /lib64/ld-linux-x86-64.so.2 from remote target...
  [Inferior 2 (process 7763) exited normally]

In this case, we'd have all the "Reading ..." messages to ignore, which
makes it hard to match the consecutive lines we expect.  Therefore, I
thought it made sense to make this test local.

Maybe a better solution for this would be to provide a similar test
under gdb.server.  Or just tweak the regexps.

>> +
>> +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:
>> +#
>> +#    follow-fork: child; detach-on-fork: on; inferior-events: on
>> +#    follow-fork: child; detach-on-fork: on; inferior-events: off
>> +#    follow-fork: child; detach-on-fork: off; inferior-events: on
>> +#    follow-fork: child; detach-on-fork: off; inferior-events: off
>> +#    follow-fork: parent; detach-on-fork: on; inferior-events: on
>> +#    follow-fork: parent; detach-on-fork: on; inferior-events: off
>> +#    follow-fork: parent; detach-on-fork: off; inferior-events: on
>> +#    follow-fork: parent; detach-on-fork: off; inferior-events: off
>> +set expected_output [list \
>> + "\\\[Attaching after process $decimal fork to child process
>> $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Detaching
>> after fork from child process $decimal\\.\\\]\r\n\\\[Inferior
>> $decimal detached\\\]\r\n\\\[Inferior $decimal \\(process
>> $decimal\\) exited normally\\\]" \
>> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
>> +    "\\\[Attaching after process $decimal fork to child process $decimal\\.\\\]\r\n\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
>> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
>> +    "\\\[Detaching after fork from child process $decimal\\.\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
>> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
>> +    "\\\[New inferior $decimal\\\]\r\n\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]" \
>> +    "\\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]"]
>> +
>
> I had a lot of trouble making sense of this big blurb of expected output.
> I didn't know exactly what to suggest, so I played with it locally, and
> I think putting the individual messages in variables helps a lot.
> And then, I noticed that all the cases end up with 
>
>   \\\[Inferior $decimal \\(process $decimal\\) exited normally\\\]
>
> so we can factor it out.
>
> And then, if we iterate over print_inferior_events first, then
> it's super clear that the "set print-inferior-events off"
> case is always "no output".  See attached patch on top of yours.

Thanks for the patch, I applied it on top of what I have here.

> The infrun.c hunk is there in my patch because while playing with
> this, I noticed that gdb says that it's detaching the child
> (and uses the child's pid), when in fact it is detaching the
> parent!
>
> I.e., currently, "set follow-fork child", "set detach-on-fork on":
>
>  [Attaching after process 12069 fork to child process 12069.]
>  [New inferior 2 12073]
>  [Detaching after fork from child process 12073.]   <<< nope, it's detaching 12069 / the parent
>  [Inferior 12069 detached]                          <<< see?
>  [Inferior 2 (process 12073) exited normally]
>
> after that fix:
>
>  [Attaching after process 18500 fork to child process 18500.]
>  [New inferior 18504]
>  [Detaching after fork from parent process 18500.]
>  [Inferior 18500 detached]
>  [Inferior 2 (process 18504) exited normally]

Ah, good catch.  ISTR having a strange feeling when examining these
outputs, but couldn't tell why.

I'll send a new version soon.

Thanks,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
  2018-01-24 20:43 ` Jan Kratochvil
  2018-01-31 16:57 ` [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages Sergio Durigan Junior
@ 2018-03-09 21:56 ` Sergio Durigan Junior
  2018-03-20 19:24   ` Sergio Durigan Junior
  2018-03-26 10:58   ` Pedro Alves
  2018-04-05 18:47 ` [PATCH v4] " Sergio Durigan Junior
                   ` (3 subsequent siblings)
  6 siblings, 2 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-03-09 21:56 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves, Sergio Durigan Junior

Changes from v2 (addressing Pedro's comments):

- Greatly improved the commit message by adding several examples of
  output with the patch.

- Made "[Inferior %d (process %d)...]" messages uniform.

- Added message to kill event.

- Removed periods from messages.

- Renamed testcase.

- Applied Pedro's patch to improve testcase readability and fix a typo
  on infrun.c.

- Made the new test work on native-extended-gdbserver.

- Adjusted several more testcases due to changes in the messages.



This is a followup of Pedro's suggestion 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 27905]
    [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 22995.
  [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 25088.]
  [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 26807 fork to child process 26807.]
  [New inferior 26811]
  [Detaching after fork from child process 26811.]
  [Inferior 26807 detached]
  [Inferior 2 (process 26811) 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-03-09  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-03-09  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/process-dies-while-detaching.exp: Adjust regexps
	to expect for new detach message.
	* 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                                       |  6 ++
 gdb/inferior.c                                     | 16 ++---
 gdb/inferior.h                                     |  4 ++
 gdb/infrun.c                                       | 30 ++++----
 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        | 84 ++++++++++++++++++++++
 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.c     |  2 +
 .../gdb.threads/process-dies-while-detaching.exp   |  6 +-
 19 files changed, 198 insertions(+), 59 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

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index c10a49892e..fbec92a958 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2598,8 +2598,14 @@ 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."));
+  pid_t pid = ptid_get_pid (inferior_ptid);
+  int infnum = current_inferior ()->num;
   target_kill ();
 
+  if (print_inferior_events)
+    printf_unfiltered (_("[Inferior %d (process %d) has been killed]\n"),
+		       infnum, pid);
+
   /* 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 c88a23c241..fbc7c468a6 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
@@ -123,7 +122,8 @@ add_inferior (int pid)
   struct inferior *inf = add_inferior_silent (pid);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[New inferior %d]\n"), pid);
+    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
+		       inf->num, pid);
 
   return inf;
 }
@@ -234,9 +234,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
@@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
   exit_inferior_1 (inf, 0);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
+    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
+		       inf->num, pid);
 }
 
 /* See inferior.h.  */
@@ -988,7 +986,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 5aeafef08b..305b2dc38b 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,11 +549,11 @@ 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)
 	{
 	  target_terminal::ours_for_output ();
 	  fprintf_filtered (gdb_stdlog,
-			    _("Attaching after %s %s to child %s.\n"),
+			    _("[Attaching after %s %s to child %s]\n"),
 			    target_pid_to_str (parent_ptid),
 			    has_vforked ? "vfork" : "fork",
 			    target_pid_to_str (child_ptid));
@@ -594,15 +594,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 +616,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 +956,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 7f409fdb1d..ce9fc5e970 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5153,7 +5153,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..437d1c7d95
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-print-inferior-events.exp
@@ -0,0 +1,84 @@
+# 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.
+
+if { [use_gdb_stub] } {
+    untested "not supported on gdbserver"
+    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..f1b930c380 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\\) 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..082927ddb3 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\\) 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..c7dca1407f 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\\) has been killed\\\]" "kill"
+	    }
+	}
     }
 }
 
diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
index b0fd84b483..1871f6cf81 100644
--- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
+++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
@@ -80,6 +80,8 @@ parent_function (pid_t child)
   alarm (300);
 
   ret = waitpid (child, &status, 0);
+  /* Give a chance to GDB print its messages.  */
+  usleep (100);
 
   if (ret == -1)
     {
diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
index e05acb1711..a273e25bd8 100644
--- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
+++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
@@ -82,7 +82,7 @@ proc detach_and_expect_exit {inf_output_re test} {
     global gdb_prompt
 
     return_if_fail [gdb_test_multiple "detach" $test {
-	-re "Detaching from .*, process $decimal" {
+	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
 	}
     }]
 
@@ -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 {
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-03-09 21:56 ` [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages Sergio Durigan Junior
@ 2018-03-20 19:24   ` Sergio Durigan Junior
  2018-03-26 10:58   ` Pedro Alves
  1 sibling, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-03-20 19:24 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves

On Friday, March 09 2018, I wrote:

> Changes from v2 (addressing Pedro's comments):
>
> - Greatly improved the commit message by adding several examples of
>   output with the patch.
>
> - Made "[Inferior %d (process %d)...]" messages uniform.
>
> - Added message to kill event.
>
> - Removed periods from messages.
>
> - Renamed testcase.
>
> - Applied Pedro's patch to improve testcase readability and fix a typo
>   on infrun.c.
>
> - Made the new test work on native-extended-gdbserver.
>
> - Adjusted several more testcases due to changes in the messages.

Ping.

>
>
> This is a followup of Pedro's suggestion 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 27905]
>     [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 22995.
>   [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 25088.]
>   [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 26807 fork to child process 26807.]
>   [New inferior 26811]
>   [Detaching after fork from child process 26811.]
>   [Inferior 26807 detached]
>   [Inferior 2 (process 26811) 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-03-09  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-03-09  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/process-dies-while-detaching.exp: Adjust regexps
> 	to expect for new detach message.
> 	* 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                                       |  6 ++
>  gdb/inferior.c                                     | 16 ++---
>  gdb/inferior.h                                     |  4 ++
>  gdb/infrun.c                                       | 30 ++++----
>  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        | 84 ++++++++++++++++++++++
>  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.c     |  2 +
>  .../gdb.threads/process-dies-while-detaching.exp   |  6 +-
>  19 files changed, 198 insertions(+), 59 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
>
> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
> index c10a49892e..fbec92a958 100644
> --- a/gdb/infcmd.c
> +++ b/gdb/infcmd.c
> @@ -2598,8 +2598,14 @@ 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."));
> +  pid_t pid = ptid_get_pid (inferior_ptid);
> +  int infnum = current_inferior ()->num;
>    target_kill ();
>  
> +  if (print_inferior_events)
> +    printf_unfiltered (_("[Inferior %d (process %d) has been killed]\n"),
> +		       infnum, pid);
> +
>    /* 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 c88a23c241..fbc7c468a6 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
> @@ -123,7 +122,8 @@ add_inferior (int pid)
>    struct inferior *inf = add_inferior_silent (pid);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
> +    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
> +		       inf->num, pid);
>  
>    return inf;
>  }
> @@ -234,9 +234,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
> @@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
>    exit_inferior_1 (inf, 0);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
> +    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
> +		       inf->num, pid);
>  }
>  
>  /* See inferior.h.  */
> @@ -988,7 +986,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 5aeafef08b..305b2dc38b 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,11 +549,11 @@ 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)
>  	{
>  	  target_terminal::ours_for_output ();
>  	  fprintf_filtered (gdb_stdlog,
> -			    _("Attaching after %s %s to child %s.\n"),
> +			    _("[Attaching after %s %s to child %s]\n"),
>  			    target_pid_to_str (parent_ptid),
>  			    has_vforked ? "vfork" : "fork",
>  			    target_pid_to_str (child_ptid));
> @@ -594,15 +594,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 +616,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 +956,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 7f409fdb1d..ce9fc5e970 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -5153,7 +5153,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..437d1c7d95
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/fork-print-inferior-events.exp
> @@ -0,0 +1,84 @@
> +# 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.
> +
> +if { [use_gdb_stub] } {
> +    untested "not supported on gdbserver"
> +    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..f1b930c380 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\\) 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..082927ddb3 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\\) 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..c7dca1407f 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\\) has been killed\\\]" "kill"
> +	    }
> +	}
>      }
>  }
>  
> diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
> index b0fd84b483..1871f6cf81 100644
> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
> @@ -80,6 +80,8 @@ parent_function (pid_t child)
>    alarm (300);
>  
>    ret = waitpid (child, &status, 0);
> +  /* Give a chance to GDB print its messages.  */
> +  usleep (100);
>  
>    if (ret == -1)
>      {
> diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> index e05acb1711..a273e25bd8 100644
> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> @@ -82,7 +82,7 @@ proc detach_and_expect_exit {inf_output_re test} {
>      global gdb_prompt
>  
>      return_if_fail [gdb_test_multiple "detach" $test {
> -	-re "Detaching from .*, process $decimal" {
> +	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
>  	}
>      }]
>  
> @@ -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 {
> -- 
> 2.14.3

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-03-09 21:56 ` [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages Sergio Durigan Junior
  2018-03-20 19:24   ` Sergio Durigan Junior
@ 2018-03-26 10:58   ` Pedro Alves
  2018-03-26 11:43     ` Pedro Alves
  2018-04-02 21:51     ` Sergio Durigan Junior
  1 sibling, 2 replies; 33+ messages in thread
From: Pedro Alves @ 2018-03-26 10:58 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

On 03/09/2018 09:55 PM, Sergio Durigan Junior wrote:

> But if we use 'add_thread_silent' (with the same configuration as
> before):
> 
>   (gdb) run
>   Starting program: a.out
>   [Attaching after process 26807 fork to child process 26807.]
>   [New inferior 26811]
>   [Detaching after fork from child process 26811.]
>   [Inferior 26807 detached]
>   [Inferior 2 (process 26811) exited normally]

I still think the "inferior PID" messages are misleading,
because PID is not the inferior number.  I think it would be
better if they read something like:

    [Attaching after process 26807 fork to child process 26807.]
-   [New inferior 26811]
+   [New inferior 2 (process 26811)]
    [Detaching after fork from child process 26811.]
-   [Inferior 26807 detached]
+   [Inferior 1 (process 26807) detached]
    [Inferior 2 (process 26811) exited normally]

I.e.:

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

Please consider fixing that at the same time.

> @@ -2598,8 +2598,14 @@ 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."));
> +  pid_t pid = ptid_get_pid (inferior_ptid);
> +  int infnum = current_inferior ()->num;
>    target_kill ();
>  
> +  if (print_inferior_events)
> +    printf_unfiltered (_("[Inferior %d (process %d) has been killed]\n"),
> +		       infnum, pid);

The "process %d" part should be using target_pid_to_str instead.
Not all targets have the concept of a "process" (e.g., when remote
debugging a bare metal system), or have access to the actual pid
number (older gdbservers).  In such case, the above prints
the fake internal magic process id (magic_null_ptid).  E.g.:

 $ ./gdb -q --batch -ex "set remote multiprocess-feature-packet off" -ex "tar rem :9999" -ex "info inferiors" -ex "kill"
   Num  Description       Executable        
 * 1    Remote target     gdb/binutils-gdb/build/gdb/gdbserver/gdbserver
        ^^^^^^^^^^^^^
 Kill the program being debugged? (y or n)
 [Inferior 1 (process 42000) has been killed]
              ^^^^^^^^^^^^^

You'll need to capture the target_pid_to_str output
before killing, otherwise after target_kill the target
might no longer be pushed on the target stack.

This pattern appears in several places in the patch.  

The fork-related paths should be fine to print inf->pid directly,
since fork implies support for multiple processes.

> 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

Changes like this appear several times in the patch -- can you
expand on why they're needed?


> +
> +if { [use_gdb_stub] } {
> +    untested "not supported on gdbserver"

There should be a comment above this mentioning what
wouldn't work with gdbserver, since it's not obvious to
me.  (I think we may have gone through that in a previous
iteration, but it'd be better if the reason was written
down here).

Note: it's not "on gdbserver", it's on "target remote" stubs.
gdbserver+extended-remote works.  And there are stubs others
than gdbserver.

> diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
> index b0fd84b483..1871f6cf81 100644
> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
> @@ -80,6 +80,8 @@ parent_function (pid_t child)
>    alarm (300);
>  
>    ret = waitpid (child, &status, 0);
> +  /* Give a chance to GDB print its messages.  */
> +  usleep (100);
>  

This is probably racy and I'd a prefer a better fix that
avoids it.  Why did you need it?  What/how does the failure
look like without this?  Why does it happen?

>    if (ret == -1)
>      {
Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-03-26 10:58   ` Pedro Alves
@ 2018-03-26 11:43     ` Pedro Alves
  2018-04-03  0:15       ` Sergio Durigan Junior
  2018-04-02 21:51     ` Sergio Durigan Junior
  1 sibling, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-03-26 11:43 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

On 03/26/2018 11:58 AM, Pedro Alves wrote:
> On 03/09/2018 09:55 PM, Sergio Durigan Junior wrote:
> 
>> But if we use 'add_thread_silent' (with the same configuration as
>> before):
>>
>>   (gdb) run
>>   Starting program: a.out
>>   [Attaching after process 26807 fork to child process 26807.]

BTW, those two PIDs being the same looks suspicious.  Is that
another instance of a parent vs child mixup?  Or is that the case
that is fixed by the patch?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-03-26 10:58   ` Pedro Alves
  2018-03-26 11:43     ` Pedro Alves
@ 2018-04-02 21:51     ` Sergio Durigan Junior
  1 sibling, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-02 21:51 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Monday, March 26 2018, Pedro Alves wrote:

> On 03/09/2018 09:55 PM, Sergio Durigan Junior wrote:
>
>> But if we use 'add_thread_silent' (with the same configuration as
>> before):
>> 
>>   (gdb) run
>>   Starting program: a.out
>>   [Attaching after process 26807 fork to child process 26807.]
>>   [New inferior 26811]
>>   [Detaching after fork from child process 26811.]
>>   [Inferior 26807 detached]
>>   [Inferior 2 (process 26811) exited normally]
>
> I still think the "inferior PID" messages are misleading,
> because PID is not the inferior number.  I think it would be
> better if they read something like:
>
>     [Attaching after process 26807 fork to child process 26807.]
> -   [New inferior 26811]
> +   [New inferior 2 (process 26811)]
>     [Detaching after fork from child process 26811.]
> -   [Inferior 26807 detached]
> +   [Inferior 1 (process 26807) detached]
>     [Inferior 2 (process 26811) exited normally]
>
> I.e.:
>
>     [Attaching after process 26807 fork to child process 26807.]
>     [New inferior 2 (process 26811)]
>     [Detaching after fork from child process 26811.]
>     [Inferior 1 (process 26807) detached]
>     [Inferior 2 (process 26811) exited normally]
>
> Please consider fixing that at the same time.

Sorry, I believe I forgot to update this part of the commit message.
The current patch already fixes this problem, so we actually see:

  [Attaching after process 19317 fork to child process 19317]                                    
  [New inferior 2 (process 19321)]               
  [Detaching after fork from parent process 19317]                                               
  [Inferior 1 (process 19317) detached]          
  [Inferior 2 (process 19321) exited normally]   

I'll make sure to update the commit log.

>> @@ -2598,8 +2598,14 @@ 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."));
>> +  pid_t pid = ptid_get_pid (inferior_ptid);
>> +  int infnum = current_inferior ()->num;
>>    target_kill ();
>>  
>> +  if (print_inferior_events)
>> +    printf_unfiltered (_("[Inferior %d (process %d) has been killed]\n"),
>> +		       infnum, pid);
>
> The "process %d" part should be using target_pid_to_str instead.
> Not all targets have the concept of a "process" (e.g., when remote
> debugging a bare metal system), or have access to the actual pid
> number (older gdbservers).  In such case, the above prints
> the fake internal magic process id (magic_null_ptid).  E.g.:
>
>  $ ./gdb -q --batch -ex "set remote multiprocess-feature-packet off" -ex "tar rem :9999" -ex "info inferiors" -ex "kill"
>    Num  Description       Executable        
>  * 1    Remote target     gdb/binutils-gdb/build/gdb/gdbserver/gdbserver
>         ^^^^^^^^^^^^^
>  Kill the program being debugged? (y or n)
>  [Inferior 1 (process 42000) has been killed]
>               ^^^^^^^^^^^^^
>
> You'll need to capture the target_pid_to_str output
3> before killing, otherwise after target_kill the target
> might no longer be pushed on the target stack.
>
> This pattern appears in several places in the patch.  
>
> The fork-related paths should be fine to print inf->pid directly,
> since fork implies support for multiple processes.

Thanks, I'll fix this.

>
>> 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
>
> Changes like this appear several times in the patch -- can you
> expand on why they're needed?

They actually appear twice, both on catch-syscall.exp.  They're needed
because now we print the following line during the test:

  (gdb) continue
  Continuing.
  [Detaching after vfork from child process 22282] <--- THIS LINE
  [Inferior 1 (process 22281) exited normally]

And therefore we need to inform the gdb_continue_to_end procedure to
expect extra cruft after the "continue" command is issued.

>> +
>> +if { [use_gdb_stub] } {
>> +    untested "not supported on gdbserver"
>
> There should be a comment above this mentioning what
> wouldn't work with gdbserver, since it's not obvious to
> me.  (I think we may have gone through that in a previous
> iteration, but it'd be better if the reason was written
> down here).
>
> Note: it's not "on gdbserver", it's on "target remote" stubs.
> gdbserver+extended-remote works.  And there are stubs others
> than gdbserver.

Alright, here's what I added:

  # 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
  }

>> diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
>> index b0fd84b483..1871f6cf81 100644
>> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
>> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.c
>> @@ -80,6 +80,8 @@ parent_function (pid_t child)
>>    alarm (300);
>>  
>>    ret = waitpid (child, &status, 0);
>> +  /* Give a chance to GDB print its messages.  */
>> +  usleep (100);
>>  
>
> This is probably racy and I'd a prefer a better fix that
> avoids it.  Why did you need it?  What/how does the failure
> look like without this?  Why does it happen?

You're right, this is racy, now that I see it again I understand it
won't necessarily work in all cases.

This was needed because of the following situation:

  (gdb) detach
  Detaching from program: /dir/gdb/testsuite/outputs/gdb.threads/process-dies-while-detaching/process-dies-while-detaching-1-detach, process 25193
  signaled, sig=9  <--- THIS LINE
  [Inferior 1 (process 25193) detached]
  (gdb) FAIL: gdb.threads/process-dies-while-detaching.exp: multi-process: detach: killed outside: detach parent

As you can see, the line marked as "THIS LINE" above is sometimes
printed in the middle of GDB's output, which confuses dejagnu.  It's
important to say that this is also racy: the failure doesn't happen
every time the test is run.

I guess a "better" solution would be to expect extra output between the
"Detaching..." and "[Inferior...]" messages.  This would mean more
complicated regexps, though.

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-03-26 11:43     ` Pedro Alves
@ 2018-04-03  0:15       ` Sergio Durigan Junior
  0 siblings, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-03  0:15 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Monday, March 26 2018, Pedro Alves wrote:

> On 03/26/2018 11:58 AM, Pedro Alves wrote:
>> On 03/09/2018 09:55 PM, Sergio Durigan Junior wrote:
>> 
>>> But if we use 'add_thread_silent' (with the same configuration as
>>> before):
>>>
>>>   (gdb) run
>>>   Starting program: a.out
>>>   [Attaching after process 26807 fork to child process 26807.]
>
> BTW, those two PIDs being the same looks suspicious.  Is that
> another instance of a parent vs child mixup?  Or is that the case
> that is fixed by the patch?

Ah, good catch.  After some time investigating, I found that the problem
occurs because the code is now calling target_pid_to_str twice in the
same statement, but target_pid_to_str actually returns a static string,
which means that this string is overwritten when the second call
happens:

	  fprintf_filtered (gdb_stdlog,
			    _("[Attaching after %s %s to child %s]\n"),
			    target_pid_to_str (parent_ptid),  <-- HERE
			    has_vforked ? "vfork" : "fork",
			    target_pid_to_str (child_ptid));  <-- AND HERE

I fixed this by saving a copy of both strings before calling
fprintf_filtered.

I'll send an updated patch soon.

Thanks,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v4] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
                   ` (2 preceding siblings ...)
  2018-03-09 21:56 ` [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages Sergio Durigan Junior
@ 2018-04-05 18:47 ` Sergio Durigan Junior
  2018-04-05 21:32   ` Sergio Durigan Junior
  2018-04-06 15:39   ` Pedro Alves
  2018-04-11 18:46 ` [PATCH v5] " Sergio Durigan Junior
                   ` (2 subsequent siblings)
  6 siblings, 2 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-05 18:47 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves, Sergio Durigan Junior

Changes from v3:

- Revisited commit log and fixed wrong copy&paste from GDB output.

- Use target_pid_to_str where applicable.  Use
  gdb::unique_xmalloc_ptr<char> to save results from
  target_pid_to_str in some cases.

- Add comment on testsuite about not being able to run on target
  remote stubs.

- Try another approach on gdb.threads/process-dies-while-detaching.exp
  in order to avoid racy conditions with GDB output.


This is a followup of Pedro's suggestion 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-05  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-05  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/process-dies-while-detaching.exp: Adjust regexps
	to expect for new detach message.
	* 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                                     | 16 ++--
 gdb/inferior.h                                     |  4 +
 gdb/infrun.c                                       | 39 +++++-----
 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   | 20 +++--
 18 files changed, 218 insertions(+), 63 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

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 9c236b8e70..580436dbd9 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2598,8 +2598,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."));
+
+  const char *pid_str = target_pid_to_str (inferior_ptid);
+  int infnum = current_inferior ()->num;
+
   target_kill ();
 
+  if (print_inferior_events)
+    printf_unfiltered (_("[Inferior %d (process %s) has been killed]\n"),
+		       infnum, pid_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..eeb1ac8b7c 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
@@ -123,7 +122,8 @@ add_inferior (int pid)
   struct inferior *inf = add_inferior_silent (pid);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[New inferior %d]\n"), pid);
+    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
+		       inf->num, pid);
 
   return inf;
 }
@@ -234,9 +234,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
@@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
   exit_inferior_1 (inf, 0);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
+    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
+		       inf->num, pid);
 }
 
 /* See inferior.h.  */
@@ -989,7 +987,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 6648698df6..4d664d4ac4 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,19 @@ 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)
 	{
+	  gdb::unique_xmalloc_ptr<char>
+	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
+	  gdb::unique_xmalloc_ptr<char>
+	    child_pid (xstrdup (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.get (),
 			    has_vforked ? "vfork" : "fork",
-			    target_pid_to_str (child_ptid));
+			    child_pid.get ());
 	}
 
       /* Add the new inferior first, so that the target_detach below
@@ -594,15 +599,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 +621,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 +961,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 68c43f8312..4cb4badd8a 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..f1b930c380 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\\) 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..082927ddb3 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\\) 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..c7dca1407f 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\\) 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..616b6cf7a4 100644
--- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
+++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
@@ -81,8 +81,15 @@ proc detach_and_expect_exit {inf_output_re test} {
     global inferior_spawn_id
     global gdb_prompt
 
+    set saw_inf_exit 0
     return_if_fail [gdb_test_multiple "detach" $test {
-	-re "Detaching from .*, process $decimal" {
+	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
+	}
+	# inf_output_re can also appear in the middle, so we catch
+	# this case here in order to avoid racy results.
+	-re "Detaching from .*, process $decimal\r\n${inf_output_re}\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
+	    verbose -log "saw inferior exit"
+	    set saw_inf_exit 1
 	}
     }]
 
@@ -92,10 +99,13 @@ proc detach_and_expect_exit {inf_output_re test} {
     # "target remote" mode, the eof caused by gdbserver exiting is
     # left for the caller to handle.
     global daee_spawn_id_list
-    set daee_spawn_id_list "$inferior_spawn_id $gdb_spawn_id"
+    if { !$saw_inf_exit } {
+	set daee_spawn_id_list "$inferior_spawn_id $gdb_spawn_id"
+    } else {
+	set daee_spawn_id_list "$gdb_spawn_id"
+    }
 
     set saw_prompt 0
-    set saw_inf_exit 0
     while { !$saw_prompt || ! $saw_inf_exit } {
 	# We don't know what order the interesting things will arrive in.
 	# Using a pattern of the form 'x|y|z' instead of -re x ... -re y
@@ -169,7 +179,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 +203,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 {
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v4] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-05 18:47 ` [PATCH v4] " Sergio Durigan Junior
@ 2018-04-05 21:32   ` Sergio Durigan Junior
  2018-04-06 15:39   ` Pedro Alves
  1 sibling, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-05 21:32 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves

On Thursday, April 05 2018, I wrote:

> Changes from v3:
>
> - Revisited commit log and fixed wrong copy&paste from GDB output.
>
> - Use target_pid_to_str where applicable.  Use
>   gdb::unique_xmalloc_ptr<char> to save results from
>   target_pid_to_str in some cases.
>
> - Add comment on testsuite about not being able to run on target
>   remote stubs.
>
> - Try another approach on gdb.threads/process-dies-while-detaching.exp
>   in order to avoid racy conditions with GDB output.

Sorry, this patch has a few regressions I forgot to fix.  I thought it
was regression-free, but I'm now seeing the BuildBot messages.  I'll
send an updated patch soon.

>
> This is a followup of Pedro's suggestion 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-05  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-05  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/process-dies-while-detaching.exp: Adjust regexps
> 	to expect for new detach message.
> 	* 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                                     | 16 ++--
>  gdb/inferior.h                                     |  4 +
>  gdb/infrun.c                                       | 39 +++++-----
>  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   | 20 +++--
>  18 files changed, 218 insertions(+), 63 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
>
> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
> index 9c236b8e70..580436dbd9 100644
> --- a/gdb/infcmd.c
> +++ b/gdb/infcmd.c
> @@ -2598,8 +2598,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."));
> +
> +  const char *pid_str = target_pid_to_str (inferior_ptid);
> +  int infnum = current_inferior ()->num;
> +
>    target_kill ();
>  
> +  if (print_inferior_events)
> +    printf_unfiltered (_("[Inferior %d (process %s) has been killed]\n"),
> +		       infnum, pid_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..eeb1ac8b7c 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
> @@ -123,7 +122,8 @@ add_inferior (int pid)
>    struct inferior *inf = add_inferior_silent (pid);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
> +    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
> +		       inf->num, pid);
>  
>    return inf;
>  }
> @@ -234,9 +234,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
> @@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
>    exit_inferior_1 (inf, 0);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
> +    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
> +		       inf->num, pid);
>  }
>  
>  /* See inferior.h.  */
> @@ -989,7 +987,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 6648698df6..4d664d4ac4 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,19 @@ 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)
>  	{
> +	  gdb::unique_xmalloc_ptr<char>
> +	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
> +	  gdb::unique_xmalloc_ptr<char>
> +	    child_pid (xstrdup (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.get (),
>  			    has_vforked ? "vfork" : "fork",
> -			    target_pid_to_str (child_ptid));
> +			    child_pid.get ());
>  	}
>  
>        /* Add the new inferior first, so that the target_detach below
> @@ -594,15 +599,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 +621,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 +961,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 68c43f8312..4cb4badd8a 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..f1b930c380 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\\) 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..082927ddb3 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\\) 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..c7dca1407f 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\\) 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..616b6cf7a4 100644
> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> @@ -81,8 +81,15 @@ proc detach_and_expect_exit {inf_output_re test} {
>      global inferior_spawn_id
>      global gdb_prompt
>  
> +    set saw_inf_exit 0
>      return_if_fail [gdb_test_multiple "detach" $test {
> -	-re "Detaching from .*, process $decimal" {
> +	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
> +	}
> +	# inf_output_re can also appear in the middle, so we catch
> +	# this case here in order to avoid racy results.
> +	-re "Detaching from .*, process $decimal\r\n${inf_output_re}\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
> +	    verbose -log "saw inferior exit"
> +	    set saw_inf_exit 1
>  	}
>      }]
>  
> @@ -92,10 +99,13 @@ proc detach_and_expect_exit {inf_output_re test} {
>      # "target remote" mode, the eof caused by gdbserver exiting is
>      # left for the caller to handle.
>      global daee_spawn_id_list
> -    set daee_spawn_id_list "$inferior_spawn_id $gdb_spawn_id"
> +    if { !$saw_inf_exit } {
> +	set daee_spawn_id_list "$inferior_spawn_id $gdb_spawn_id"
> +    } else {
> +	set daee_spawn_id_list "$gdb_spawn_id"
> +    }
>  
>      set saw_prompt 0
> -    set saw_inf_exit 0
>      while { !$saw_prompt || ! $saw_inf_exit } {
>  	# We don't know what order the interesting things will arrive in.
>  	# Using a pattern of the form 'x|y|z' instead of -re x ... -re y
> @@ -169,7 +179,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 +203,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 {
> -- 
> 2.14.3

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v4] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-05 18:47 ` [PATCH v4] " Sergio Durigan Junior
  2018-04-05 21:32   ` Sergio Durigan Junior
@ 2018-04-06 15:39   ` Pedro Alves
  2018-04-06 15:56     ` Sergio Durigan Junior
  1 sibling, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-04-06 15:39 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

On 04/05/2018 07:46 PM, Sergio Durigan Junior wrote:
> Changes from v3:
> 
> - Revisited commit log and fixed wrong copy&paste from GDB output.
> 
> - Use target_pid_to_str where applicable.  Use
>   gdb::unique_xmalloc_ptr<char> to save results from
>   target_pid_to_str in some cases.

Arguably target_pid_to_str implementations should be
using get_print_cell instead of a single static buffer, avoiding
the problem.  But that's a larger change, so local copy is fine
with me.

> @@ -2598,8 +2598,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."));
> +
> +  const char *pid_str = target_pid_to_str (inferior_ptid);
> +  int infnum = current_inferior ()->num;
> +
>    target_kill ();
>  
> +  if (print_inferior_events)
> +    printf_unfiltered (_("[Inferior %d (process %s) has been killed]\n"),
> +		       infnum, pid_str);

This still seem risky -- Target backends use target_pid_to_str inside
target_kill, e.g., when logging is enabled.  E.g., 

linux_nat_kill -> -> stop_callback -> target_pid_to_str

ISTM a deep copy like:

  std::string pid_str = target_pid_to_str (inferior_ptid);

would be safer/better.

>  /* The Current Inferior.  This is a strong reference.  I.e., whenever
>     an inferior is the current inferior, its refcount is
> @@ -123,7 +122,8 @@ add_inferior (int pid)
>    struct inferior *inf = add_inferior_silent (pid);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
> +    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
> +		       inf->num, pid);

As discussed in a previous revision, using hardcoded "(process %d)"
doesn't work properly, because PID can be a fake PID number, or the
target may have no concept of processes, e.g., when debugging remote
targets.  ISTRM this should use target_pid_to_str as well.

> @@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
>    exit_inferior_1 (inf, 0);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
> +    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
> +		       inf->num, pid);

Ditto.

> -      if (info_verbose || debug_infrun)
> +      if (print_inferior_events)
>  	{
> +	  gdb::unique_xmalloc_ptr<char>
> +	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
> +	  gdb::unique_xmalloc_ptr<char>
> +	    child_pid (xstrdup (target_pid_to_str (child_ptid)));

std::string would be simpler than gdb::unique_xmalloc_ptr here:

	  std::string parent_pid = target_pid_to_str (parent_ptid));
	  std::string child_pid = target_pid_to_str (child_ptid));

gdb::unique_xmalloc_ptr is handy when you have no way around
malloc/free, but here you're in charge of the dup yourself.


> diff --git a/gdb/remote.c b/gdb/remote.c
> index 68c43f8312..4cb4badd8a 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);

Hardcoded "(process %d)" here too.

Fixing this issue in the several spots may affect your
testsuite changes -- please be sure to rerun tests with 
"target remote" afterwards.

> +    }
>    else
>      {

>  }
>  
> diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> index e05acb1711..616b6cf7a4 100644
> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
> @@ -81,8 +81,15 @@ proc detach_and_expect_exit {inf_output_re test} {
>      global inferior_spawn_id
>      global gdb_prompt
>  
> +    set saw_inf_exit 0
>      return_if_fail [gdb_test_multiple "detach" $test {
> -	-re "Detaching from .*, process $decimal" {
> +	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
> +	}
> +	# inf_output_re can also appear in the middle, so we catch
> +	# this case here in order to avoid racy results.
> +	-re "Detaching from .*, process $decimal\r\n${inf_output_re}\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
> +	    verbose -log "saw inferior exit"
> +	    set saw_inf_exit 1
>  	}

I'm not sure I understand the need for this.  If you left this
gdb_test_multiple exactly as it was before your patch, wouldn't it all
work the same?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v4] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-06 15:39   ` Pedro Alves
@ 2018-04-06 15:56     ` Sergio Durigan Junior
  2018-04-06 16:41       ` Pedro Alves
  0 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-06 15:56 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Friday, April 06 2018, Pedro Alves wrote:

> On 04/05/2018 07:46 PM, Sergio Durigan Junior wrote:
>> Changes from v3:
>> 
>> - Revisited commit log and fixed wrong copy&paste from GDB output.
>> 
>> - Use target_pid_to_str where applicable.  Use
>>   gdb::unique_xmalloc_ptr<char> to save results from
>>   target_pid_to_str in some cases.
>
> Arguably target_pid_to_str implementations should be
> using get_print_cell instead of a single static buffer, avoiding
> the problem.  But that's a larger change, so local copy is fine
> with me.

I can send a patch for this later.

>> @@ -2598,8 +2598,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."));
>> +
>> +  const char *pid_str = target_pid_to_str (inferior_ptid);
>> +  int infnum = current_inferior ()->num;
>> +
>>    target_kill ();
>>  
>> +  if (print_inferior_events)
>> +    printf_unfiltered (_("[Inferior %d (process %s) has been killed]\n"),
>> +		       infnum, pid_str);
>
> This still seem risky -- Target backends use target_pid_to_str inside
> target_kill, e.g., when logging is enabled.  E.g., 
>
> linux_nat_kill -> -> stop_callback -> target_pid_to_str
>
> ISTM a deep copy like:
>
>   std::string pid_str = target_pid_to_str (inferior_ptid);
>
> would be safer/better.

Fair enough, I'll change it.

>>  /* The Current Inferior.  This is a strong reference.  I.e., whenever
>>     an inferior is the current inferior, its refcount is
>> @@ -123,7 +122,8 @@ add_inferior (int pid)
>>    struct inferior *inf = add_inferior_silent (pid);
>>  
>>    if (print_inferior_events)
>> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
>> +    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
>> +		       inf->num, pid);
>
> As discussed in a previous revision, using hardcoded "(process %d)"
> doesn't work properly, because PID can be a fake PID number, or the
> target may have no concept of processes, e.g., when debugging remote
> targets.  ISTRM this should use target_pid_to_str as well.

That's true, I apologize for overlooking this one.  And I also noticed
the problem with including the "process" string before, that's why I
sent the message "cancelling" this patch.

>
>> @@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
>>    exit_inferior_1 (inf, 0);
>>  
>>    if (print_inferior_events)
>> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
>> +    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
>> +		       inf->num, pid);
>
> Ditto.

Got it.

>> -      if (info_verbose || debug_infrun)
>> +      if (print_inferior_events)
>>  	{
>> +	  gdb::unique_xmalloc_ptr<char>
>> +	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
>> +	  gdb::unique_xmalloc_ptr<char>
>> +	    child_pid (xstrdup (target_pid_to_str (child_ptid)));
>
> std::string would be simpler than gdb::unique_xmalloc_ptr here:
>
> 	  std::string parent_pid = target_pid_to_str (parent_ptid));
> 	  std::string child_pid = target_pid_to_str (child_ptid));
>
> gdb::unique_xmalloc_ptr is handy when you have no way around
> malloc/free, but here you're in charge of the dup yourself.

That's true.  I guess I wasn't thinking in C++ terms.

>> diff --git a/gdb/remote.c b/gdb/remote.c
>> index 68c43f8312..4cb4badd8a 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);
>
> Hardcoded "(process %d)" here too.
>
> Fixing this issue in the several spots may affect your
> testsuite changes -- please be sure to rerun tests with 
> "target remote" afterwards.

I'm doing it, thanks.

>
>> +    }
>>    else
>>      {
>
>>  }
>>  
>> diff --git a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
>> index e05acb1711..616b6cf7a4 100644
>> --- a/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
>> +++ b/gdb/testsuite/gdb.threads/process-dies-while-detaching.exp
>> @@ -81,8 +81,15 @@ proc detach_and_expect_exit {inf_output_re test} {
>>      global inferior_spawn_id
>>      global gdb_prompt
>>  
>> +    set saw_inf_exit 0
>>      return_if_fail [gdb_test_multiple "detach" $test {
>> -	-re "Detaching from .*, process $decimal" {
>> +	-re "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
>> +	}
>> +	# inf_output_re can also appear in the middle, so we catch
>> +	# this case here in order to avoid racy results.
>> +	-re "Detaching from .*, process $decimal\r\n${inf_output_re}\r\n\\\[Inferior $decimal \\(process $decimal\\) detached\\\]" {
>> +	    verbose -log "saw inferior exit"
>> +	    set saw_inf_exit 1
>>  	}
>
> I'm not sure I understand the need for this.  If you left this
> gdb_test_multiple exactly as it was before your patch, wouldn't it all
> work the same?

As I said in the other message, the problem here is that
${inf_output_re} can happen between the two messages.  For example:

  Detaching from program: .../gdb/testsuite/outputs/gdb.threads/process-dies-while-detaching/process-dies-while-detaching-1-detach, process 7440
  exited, status=0
  [Inferior 1 (process 7440) detached]

In this case, leave gdb_test_multiple as it was before doesn't catch
this case, which leads to a racy failure.  However, I noticed that my
patch also doesn't fix the failure (I thought it did, but then I saw it
happening again on the BuildBot).  That's another reason why I
"cancelled" this version of the patch.

I'm trying to come up with another solution for this race, but so far I
haven't had much success.  Of course, if you have any ideas feel free to
suggest them.  Otherwise, I'll see what I can do during the weekend (I'm
on PTO today).

Thanks,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v4] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-06 15:56     ` Sergio Durigan Junior
@ 2018-04-06 16:41       ` Pedro Alves
  2018-04-10 16:22         ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-04-06 16:41 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: GDB Patches, Jan Kratochvil

On 04/06/2018 04:56 PM, Sergio Durigan Junior wrote:

>> I'm not sure I understand the need for this.  If you left this
>> gdb_test_multiple exactly as it was before your patch, wouldn't it all
>> work the same?
> 
> As I said in the other message, the problem here is that
> ${inf_output_re} can happen between the two messages.  For example:
> 
>   Detaching from program: .../gdb/testsuite/outputs/gdb.threads/process-dies-while-detaching/process-dies-while-detaching-1-detach, process 7440
>   exited, status=0
>   [Inferior 1 (process 7440) detached]
> 
> In this case, leave gdb_test_multiple as it was before doesn't catch
> this case, which leads to a racy failure.  

I'm not sure I get it -- why does it need to catch it?

The original gdb_test_multiple matches the first line:

"Detaching from program: .../gdb/testsuite/outputs/gdb.threads/process-dies-while-detaching/process-dies-while-detaching-1-detach, process 7440"

and stops here, there's no $gdb_prompt anchor.  And then, the loop below
should consume both the inferior output and the gdb prompt.

My question is then, why do you need to expect the
 "[Inferior 1 (process 7440) detached]"
part in this test at all?

> However, I noticed that my
> patch also doesn't fix the failure (I thought it did, but then I saw it
> happening again on the BuildBot).  That's another reason why I
> "cancelled" this version of the patch.

Also please try these racy issues with "make check-read1".

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v4] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-06 16:41       ` Pedro Alves
@ 2018-04-10 16:22         ` Sergio Durigan Junior
  0 siblings, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-10 16:22 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Friday, April 06 2018, Pedro Alves wrote:

> On 04/06/2018 04:56 PM, Sergio Durigan Junior wrote:
>
>>> I'm not sure I understand the need for this.  If you left this
>>> gdb_test_multiple exactly as it was before your patch, wouldn't it all
>>> work the same?
>> 
>> As I said in the other message, the problem here is that
>> ${inf_output_re} can happen between the two messages.  For example:
>> 
>>   Detaching from program: .../gdb/testsuite/outputs/gdb.threads/process-dies-while-detaching/process-dies-while-detaching-1-detach, process 7440
>>   exited, status=0
>>   [Inferior 1 (process 7440) detached]
>> 
>> In this case, leave gdb_test_multiple as it was before doesn't catch
>> this case, which leads to a racy failure.  
>
> I'm not sure I get it -- why does it need to catch it?
>
> The original gdb_test_multiple matches the first line:
>
> "Detaching from program: .../gdb/testsuite/outputs/gdb.threads/process-dies-while-detaching/process-dies-while-detaching-1-detach, process 7440"
>
> and stops here, there's no $gdb_prompt anchor.  And then, the loop below
> should consume both the inferior output and the gdb prompt.
>
> My question is then, why do you need to expect the
>  "[Inferior 1 (process 7440) detached]"
> part in this test at all?

Thanks for the message.

I guess I was confusing things, then.  I updated all regexps to expect
the "[Inferior ... detached]" message after the "Detaching ...", because
that's what GDB will print.  But in this specific case, as you noticed,
it's not necessary.  I reverted the testcase back to its original form,
and am running a loop to make sure there's no racy test, but I think
it should be enough to just it as is.

>> However, I noticed that my
>> patch also doesn't fix the failure (I thought it did, but then I saw it
>> happening again on the BuildBot).  That's another reason why I
>> "cancelled" this version of the patch.
>
> Also please try these racy issues with "make check-read1".

I did this, and found no racy issues.

I'll run another full regression test on BuildBot, and resubmit the
patch.

Thanks,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v5] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
                   ` (3 preceding siblings ...)
  2018-04-05 18:47 ` [PATCH v4] " Sergio Durigan Junior
@ 2018-04-11 18:46 ` Sergio Durigan Junior
  2018-04-11 19:05   ` Pedro Alves
  2018-04-16 20:04 ` [PATCH v6] " Sergio Durigan Junior
  2018-04-19 19:54 ` [PATCH v7] " Sergio Durigan Junior
  6 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-11 18:46 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves, Sergio Durigan Junior

Changes from v4:

- Fix race on gdb.threads/process-dies-while-detaching-1-detach.exp.

- Fix some wrong regexps when expecting for the inferior to be killed.


This is a followup of Pedro's suggestion 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-11  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-11  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                                     | 16 ++--
 gdb/inferior.h                                     |  4 +
 gdb/infrun.c                                       | 39 +++++-----
 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(+), 60 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

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index d43e7f202d..1f724fbf23 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."));
+
+  const char *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);
+
   /* 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..eeb1ac8b7c 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
@@ -123,7 +122,8 @@ add_inferior (int pid)
   struct inferior *inf = add_inferior_silent (pid);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[New inferior %d]\n"), pid);
+    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
+		       inf->num, pid);
 
   return inf;
 }
@@ -234,9 +234,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
@@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
   exit_inferior_1 (inf, 0);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
+    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
+		       inf->num, pid);
 }
 
 /* See inferior.h.  */
@@ -989,7 +987,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 c1db689c1c..1ce35171b5 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,19 @@ 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)
 	{
+	  gdb::unique_xmalloc_ptr<char>
+	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
+	  gdb::unique_xmalloc_ptr<char>
+	    child_pid (xstrdup (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.get (),
 			    has_vforked ? "vfork" : "fork",
-			    target_pid_to_str (child_ptid));
+			    child_pid.get ());
 	}
 
       /* Add the new inferior first, so that the target_detach below
@@ -594,15 +599,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 +621,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 +961,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 {
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-11 18:46 ` [PATCH v5] " Sergio Durigan Junior
@ 2018-04-11 19:05   ` Pedro Alves
  2018-04-11 19:08     ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-04-11 19:05 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

On 04/11/2018 07:46 PM, Sergio Durigan Junior wrote:
> Changes from v4:
> 
> - Fix race on gdb.threads/process-dies-while-detaching-1-detach.exp.

I don't think that testcase exists.  :-)  Anyway, just remember
to remove the "Changes from" section before pushing that then
it's moot.

> 
> - Fix some wrong regexps when expecting for the inferior to be killed.

Hmm, you didn't address the comments to the gdb code itself.


> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
> index d43e7f202d..1f724fbf23 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."));
> +
> +  const char *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);
> +

My previous comment still applies here:

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 This still seem risky -- Target backends use target_pid_to_str inside
 target_kill, e.g., when logging is enabled.  E.g., 

 linux_nat_kill -> -> stop_callback -> target_pid_to_str

 ISTM a deep copy like:

   std::string pid_str = target_pid_to_str (inferior_ptid);

 would be safer/better.
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> @@ -123,7 +122,8 @@ add_inferior (int pid)
>    struct inferior *inf = add_inferior_silent (pid);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
> +    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
> +		       inf->num, pid);

Likewise.

> @@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
>    exit_inferior_1 (inf, 0);
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
> +    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
> +		       inf->num, pid);
>  }
>  

Likewise?

> -      if (info_verbose || debug_infrun)
> +      if (print_inferior_events)
>  	{
> +	  gdb::unique_xmalloc_ptr<char>
> +	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
> +	  gdb::unique_xmalloc_ptr<char>
> +	    child_pid (xstrdup (target_pid_to_str (child_ptid)));
> +

Likewise?

/me stops looking further.

Looks like you missed handling the comments for the gdb code.  

Try again? :-)

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-11 19:05   ` Pedro Alves
@ 2018-04-11 19:08     ` Sergio Durigan Junior
  0 siblings, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-11 19:08 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Wednesday, April 11 2018, Pedro Alves wrote:

> On 04/11/2018 07:46 PM, Sergio Durigan Junior wrote:
>> Changes from v4:
>> 
>> - Fix race on gdb.threads/process-dies-while-detaching-1-detach.exp.
>
> I don't think that testcase exists.  :-)  Anyway, just remember
> to remove the "Changes from" section before pushing that then
> it's moot.

That's true, wrong copy&paste.

>> 
>> - Fix some wrong regexps when expecting for the inferior to be killed.
>
> Hmm, you didn't address the comments to the gdb code itself.

Sigh.  Sorry about that.  I'll do that now.

Thanks,

>
>> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
>> index d43e7f202d..1f724fbf23 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."));
>> +
>> +  const char *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);
>> +
>
> My previous comment still applies here:
>
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  This still seem risky -- Target backends use target_pid_to_str inside
>  target_kill, e.g., when logging is enabled.  E.g., 
>
>  linux_nat_kill -> -> stop_callback -> target_pid_to_str
>
>  ISTM a deep copy like:
>
>    std::string pid_str = target_pid_to_str (inferior_ptid);
>
>  would be safer/better.
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>> @@ -123,7 +122,8 @@ add_inferior (int pid)
>>    struct inferior *inf = add_inferior_silent (pid);
>>  
>>    if (print_inferior_events)
>> -    printf_unfiltered (_("[New inferior %d]\n"), pid);
>> +    printf_unfiltered (_("[New inferior %d (process %d)]\n"),
>> +		       inf->num, pid);
>
> Likewise.
>
>> @@ -266,7 +263,8 @@ detach_inferior (inferior *inf)
>>    exit_inferior_1 (inf, 0);
>>  
>>    if (print_inferior_events)
>> -    printf_unfiltered (_("[Inferior %d detached]\n"), pid);
>> +    printf_unfiltered (_("[Inferior %d (process %d) detached]\n"),
>> +		       inf->num, pid);
>>  }
>>  
>
> Likewise?
>
>> -      if (info_verbose || debug_infrun)
>> +      if (print_inferior_events)
>>  	{
>> +	  gdb::unique_xmalloc_ptr<char>
>> +	    parent_pid (xstrdup (target_pid_to_str (parent_ptid)));
>> +	  gdb::unique_xmalloc_ptr<char>
>> +	    child_pid (xstrdup (target_pid_to_str (child_ptid)));
>> +
>
> Likewise?
>
> /me stops looking further.
>
> Looks like you missed handling the comments for the gdb code.  
>
> Try again? :-)
>
> Thanks,
> Pedro Alves

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v6] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
                   ` (4 preceding siblings ...)
  2018-04-11 18:46 ` [PATCH v5] " Sergio Durigan Junior
@ 2018-04-16 20:04 ` Sergio Durigan Junior
  2018-04-17 15:57   ` Pedro Alves
  2018-04-19 19:54 ` [PATCH v7] " Sergio Durigan Junior
  6 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-16 20:04 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves, Sergio Durigan Junior

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

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 {
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v6] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-16 20:04 ` [PATCH v6] " Sergio Durigan Junior
@ 2018-04-17 15:57   ` Pedro Alves
  2018-04-17 20:07     ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-04-17 15:57 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

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

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v6] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-17 15:57   ` Pedro Alves
@ 2018-04-17 20:07     ` Sergio Durigan Junior
  0 siblings, 0 replies; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-17 20:07 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

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,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
                   ` (5 preceding siblings ...)
  2018-04-16 20:04 ` [PATCH v6] " Sergio Durigan Junior
@ 2018-04-19 19:54 ` Sergio Durigan Junior
  2018-04-24 13:33   ` Pedro Alves
  6 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-19 19:54 UTC (permalink / raw)
  To: GDB Patches; +Cc: Jan Kratochvil, Pedro Alves, Sergio Durigan Junior

Changes from v6:

- Update ChangeLog.

- Optimized spots where a deep copy was made (but not needed) when
  calling target_pid_to_str.

- Fix a few more places where we were printing "process %d" instead of
  calling target_pid_to_str.

- Readjusted a bunch of tests in order to be more flexible when
  matching the "process $decimal" or "Thread $decimal.$decimal"
  strings.


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-19  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-19  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/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                                     | 18 ++---
 gdb/inferior.h                                     |  4 +
 gdb/infrun.c                                       | 42 ++++++-----
 gdb/remote.c                                       |  9 ++-
 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, 210 insertions(+), 62 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

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..d7122fcfad 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
@@ -123,7 +122,9 @@ add_inferior (int pid)
   struct inferior *inf = add_inferior_silent (pid);
 
   if (print_inferior_events)
-    printf_unfiltered (_("[New inferior %d]\n"), pid);
+    printf_unfiltered (_("[New inferior %d (%s)]\n"),
+		       inf->num,
+		       target_pid_to_str (pid_to_ptid (pid)));
 
   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
@@ -266,7 +264,9 @@ detach_inferior (inferior *inf)
   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,
+		       target_pid_to_str (pid_to_ptid (pid)));
 }
 
 /* See inferior.h.  */
@@ -989,7 +989,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..223e836ede 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,23 +959,24 @@ handle_vfork_child_exec_or_exit (int exec)
 	  inf->aspace = NULL;
 	  inf->pspace = NULL;
 
-	  if (debug_infrun || info_verbose)
+	  if (print_inferior_events)
 	    {
+	      const char *pidstr
+		= target_pid_to_str (pid_to_ptid (inf->vfork_parent->pid));
+
 	      target_terminal::ours_for_output ();
 
 	      if (exec)
 		{
 		  fprintf_filtered (gdb_stdlog,
-				    _("Detaching vfork parent process "
-				      "%d after child exec.\n"),
-				    inf->vfork_parent->pid);
+				    _("[Detaching vfork parent %s "
+				      "after child exec]\n"), pidstr);
 		}
 	      else
 		{
 		  fprintf_filtered (gdb_stdlog,
-				    _("Detaching vfork parent process "
-				      "%d after child exit.\n"),
-				    inf->vfork_parent->pid);
+				    _("[Detaching vfork parent %s "
+				      "after child exit]\n"), pidstr);
 		}
 	    }
 
diff --git a/gdb/remote.c b/gdb/remote.c
index f54a38833b..105c0df054 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5137,7 +5137,14 @@ 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);
+    {
+      std::string infpid = target_pid_to_str (inferior_ptid);
+
+      target_mourn_inferior (inferior_ptid);
+      if (print_inferior_events)
+	printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
+			   inf->num, infpid.c_str ());
+    }
   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..60345f3894 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 \\(.*\\) 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 \\(.*\\) 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..8199a80c6b 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 \\(.*\\) 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..53ea99c83f
--- /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 \\(.*\\) 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 .* fork to child .*\\\]\r\n"
+set detach_child_re "${reading_re}\\\[Detaching after fork from child .*\\\]\r\n"
+set detach_parent_re "${reading_re}\\\[Detaching after fork from parent .*\\\]\r\n"
+set new_inf_re "${reading_re}\\\[New inferior $decimal \\(.*\\)\\\]\r\n"
+set inf_detached_re "${reading_re}\\\[Inferior $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..5717f94fc2 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 \\(.*\\) 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..67e6f62750 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 \\(.*\\) has been killed\\\]" "kill"
+    }
+}
diff --git a/gdb/testsuite/gdb.base/solib-overlap.exp b/gdb/testsuite/gdb.base/solib-overlap.exp
index 88762be449..de15f477e1 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 \\(.*\\) 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..27132e35e4 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 \\(.*\\) 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 \\(.*\\) detached\\\]"
     }
 }
 
diff --git a/gdb/testsuite/gdb.threads/kill.exp b/gdb/testsuite/gdb.threads/kill.exp
index 61384578e6..aea4a986c6 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 \\(.*\\) 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..7cedb4bd54 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 \\(.*\\) 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 \\(.*\\) detached\\\]$extra"
 	} elseif {$cmd == "continue"} {
 	    gdb_test "continue" $continue_re
 	} else {
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-19 19:54 ` [PATCH v7] " Sergio Durigan Junior
@ 2018-04-24 13:33   ` Pedro Alves
  2018-04-24 19:49     ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-04-24 13:33 UTC (permalink / raw)
  To: Sergio Durigan Junior, GDB Patches; +Cc: Jan Kratochvil

On 04/19/2018 08:53 PM, Sergio Durigan Junior wrote:
> Changes from v6:

> - Readjusted a bunch of tests in order to be more flexible when
>   matching the "process $decimal" or "Thread $decimal.$decimal"
>   strings.

I think you meant "process $decimal" or "Remote target", because
"Thread $decimal.$decimal" won't be used with pid-only ptids.

This version is OK.  Thanks!

Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
  2018-04-24 13:33   ` Pedro Alves
@ 2018-04-24 19:49     ` Sergio Durigan Junior
  2018-04-25 17:41       ` [PATCH] Fix new inferior events output (Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages) Pedro Alves
  0 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-24 19:49 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Tuesday, April 24 2018, Pedro Alves wrote:

> On 04/19/2018 08:53 PM, Sergio Durigan Junior wrote:
>> Changes from v6:
>
>> - Readjusted a bunch of tests in order to be more flexible when
>>   matching the "process $decimal" or "Thread $decimal.$decimal"
>>   strings.
>
> I think you meant "process $decimal" or "Remote target", because
> "Thread $decimal.$decimal" won't be used with pid-only ptids.

I actually had to adjust a few regexps (in previous versions of the
patch) to accept "Thread $decimal.$decimal".  But that's a different
problem than the pid-only ptids, and I don't include the "Changes from
v..." in the commit log.

> This version is OK.  Thanks!

Thanks a lot for all the revisions and the time spent.

Pushed.

f67c0c9171508672167b6868c67211571421a1c6

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH] Fix new inferior events output (Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages)
  2018-04-24 19:49     ` Sergio Durigan Junior
@ 2018-04-25 17:41       ` Pedro Alves
  2018-04-25 17:53         ` Sergio Durigan Junior
  0 siblings, 1 reply; 33+ messages in thread
From: Pedro Alves @ 2018-04-25 17:41 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: GDB Patches, Jan Kratochvil

Hi,

-- On 04/24/2018 08:49 PM, Sergio Durigan Junior wrote:
>> On 04/19/2018 08:53 PM, Sergio Durigan Junior wrote:
>>> Changes from v6:
>>
>>> - Readjusted a bunch of tests in order to be more flexible when
>>>   matching the "process $decimal" or "Thread $decimal.$decimal"
>>>   strings.
>>
>> I think you meant "process $decimal" or "Remote target", because
>> "Thread $decimal.$decimal" won't be used with pid-only ptids.
> 
> I actually had to adjust a few regexps (in previous versions of the
> patch) to accept "Thread $decimal.$decimal".  But that's a different
> problem than the pid-only ptids, and I don't include the "Changes from
> v..." in the commit log.
Hmm, now that I try current master I think I see what you are
saying.  But that shows an actual problem with the patch, we really
shouldn't be seeing those "Thread $decimal.$decimal".  See patch below.

From 4b17a2be78349e6912f68b924a5e3761035ee831 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Wed, 25 Apr 2018 17:28:25 +0100
Subject: [PATCH] Fix new inferior events output

Since f67c0c917150 ("Enable 'set print inferior-events' and improve
detach/fork/kill/exit messages"), when detaching a remote process, we
get, for detach against a remote target:

 (gdb) detach
 Detaching from program: ...., process 5388
 Ending remote debugging.
 [Inferior 1 (Thread 5388.5388) detached]
              ^^^^^^^^^^^^^^^^

That is incorrect, for it is printing a thread id as string while we
should be printing the process id instead.  I.e., either one of:

 [Inferior 1 (process 5388) detached]
 [Inferior 1 (Remote target) detached]

depending on remote stub support for the multi-process extensions.


Similarly, after killing a process, we're printing of thread ids while
we should be printing process ids.  E.g., on native GNU/Linux:

 (gdb) k
 Kill the program being debugged? (y or n) y
 [Inferior 1 (Thread 0x7ffff7faa8c0 (LWP 30721)) has been killed]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

while it should have been:

 Kill the program being debugged? (y or n) y
 [Inferior 1 (process 30721) has been killed]
              ^^^^^^^^^^^^^

There's a wording inconsistency between detach and kill:

 [Inferior 1 (process 30721) has been killed]
 [Inferior 1 (process 30721) detached]

Given we were already saying "detached" instead of "has been
detached", and we used to say just "exited", and given that the "has
been" doesn't really add any information, this commit changes the the
message to just "killed":

 [Inferior 1 (process 30721) killed]

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* infcmd.c (kill_command): Print the pid as string, not the whole
	thread's ptid.  Add comment.  s/has been killed/killed/ in output
	message.
	* remote.c (remote_detach_1): Print the pid as string, not the
	whole thread's ptid.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* gdb.base/hook-stop.exp: Expect "killed" instead of "has been
	killed".
	* gdb.base/kill-after-signal.exp: Likewise.
	* gdb.threads/kill.exp: Likewise.
---
 gdb/infcmd.c                                 | 7 +++++--
 gdb/remote.c                                 | 4 +++-
 gdb/testsuite/gdb.base/hook-stop.exp         | 2 +-
 gdb/testsuite/gdb.base/kill-after-signal.exp | 2 +-
 gdb/testsuite/gdb.threads/kill.exp           | 2 +-
 5 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 6c9f885badd..21772b6cff0 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2596,13 +2596,16 @@ kill_command (const char *arg, int from_tty)
   if (!query (_("Kill the program being debugged? ")))
     error (_("Not confirmed."));
 
-  std::string pid_str = target_pid_to_str (inferior_ptid);
+  int pid = current_inferior ()->pid;
+  /* Save the pid as a string before killing the inferior, since that
+     may unpush the current target, and we need the string after.  */
+  std::string pid_str = target_pid_to_str (pid_to_ptid (pid));
   int infnum = current_inferior ()->num;
 
   target_kill ();
 
   if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d (%s) has been killed]\n"),
+    printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
 		       infnum, pid_str.c_str ());
 
   /* If we still have other inferiors to debug, then don't mess with
diff --git a/gdb/remote.c b/gdb/remote.c
index 61d1dcb5738..193037b6e7a 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5138,7 +5138,9 @@ remote_detach_1 (int from_tty, inferior *inf)
      breakpoints that should be available for the followed inferior.  */
   if (!is_fork_parent)
     {
-      std::string infpid = target_pid_to_str (inferior_ptid);
+      /* Save the pid as a string before mourning, since that will
+	 unpush the remote target, and we need the string after.  */
+      std::string infpid = target_pid_to_str (pid_to_ptid (pid));
 
       target_mourn_inferior (inferior_ptid);
       if (print_inferior_events)
diff --git a/gdb/testsuite/gdb.base/hook-stop.exp b/gdb/testsuite/gdb.base/hook-stop.exp
index 5717f94fc2a..b3f4084af1e 100644
--- a/gdb/testsuite/gdb.base/hook-stop.exp
+++ b/gdb/testsuite/gdb.base/hook-stop.exp
@@ -86,7 +86,7 @@ proc hook_stop_kill {} {
 
 	set test "run hook-stop"
 	gdb_test_multiple "continue" "$test" {
-	    -re "Continuing.\r\n\\\[Inferior $decimal \\(.*\\) has been killed\\\]\r\n${gdb_prompt} $" {
+	    -re "Continuing.\r\n\\\[Inferior $decimal \\(.*\\) 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 67e6f627504..8830d492383 100644
--- a/gdb/testsuite/gdb.base/kill-after-signal.exp
+++ b/gdb/testsuite/gdb.base/kill-after-signal.exp
@@ -39,6 +39,6 @@ gdb_test "continue" "Program received signal SIGUSR1, .*"
 gdb_test "stepi" "\r\nhandler .*"
 gdb_test_multiple "kill" "kill" {
     -re "Kill the program being debugged\\? \\(y or n\\) $" {
-       gdb_test "y" "\\\[Inferior $decimal \\(.*\\) has been killed\\\]" "kill"
+       gdb_test "y" "\\\[Inferior $decimal \\(.*\\) killed\\\]" "kill"
     }
 }
diff --git a/gdb/testsuite/gdb.threads/kill.exp b/gdb/testsuite/gdb.threads/kill.exp
index aea4a986c64..82f221eef67 100644
--- a/gdb/testsuite/gdb.threads/kill.exp
+++ b/gdb/testsuite/gdb.threads/kill.exp
@@ -70,7 +70,7 @@ proc test {threaded} {
 
 	gdb_test_multiple "kill" "kill" {
 	    -re "Kill the program being debugged\\? \\(y or n\\) $" {
-		gdb_test "y" "\\\[Inferior $decimal \\(.*\\) has been killed\\\]" "kill"
+		gdb_test "y" "\\\[Inferior $decimal \\(.*\\) killed\\\]" "kill"
 	    }
 	}
     }
-- 
2.14.3

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Fix new inferior events output (Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages)
  2018-04-25 17:41       ` [PATCH] Fix new inferior events output (Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages) Pedro Alves
@ 2018-04-25 17:53         ` Sergio Durigan Junior
  2018-04-25 18:07           ` Pedro Alves
  0 siblings, 1 reply; 33+ messages in thread
From: Sergio Durigan Junior @ 2018-04-25 17:53 UTC (permalink / raw)
  To: Pedro Alves; +Cc: GDB Patches, Jan Kratochvil

On Wednesday, April 25 2018, Pedro Alves wrote:

> Hi,
>
> -- On 04/24/2018 08:49 PM, Sergio Durigan Junior wrote:
>>> On 04/19/2018 08:53 PM, Sergio Durigan Junior wrote:
>>>> Changes from v6:
>>>
>>>> - Readjusted a bunch of tests in order to be more flexible when
>>>>   matching the "process $decimal" or "Thread $decimal.$decimal"
>>>>   strings.
>>>
>>> I think you meant "process $decimal" or "Remote target", because
>>> "Thread $decimal.$decimal" won't be used with pid-only ptids.
>> 
>> I actually had to adjust a few regexps (in previous versions of the
>> patch) to accept "Thread $decimal.$decimal".  But that's a different
>> problem than the pid-only ptids, and I don't include the "Changes from
>> v..." in the commit log.
> Hmm, now that I try current master I think I see what you are
> saying.  But that shows an actual problem with the patch, we really
> shouldn't be seeing those "Thread $decimal.$decimal".  See patch below.

Thanks for catching this problem.  I don't think I have anything to add,
other than "sorry for not catching this earlier".  For whatever it is
worth, looks good to me.

> From 4b17a2be78349e6912f68b924a5e3761035ee831 Mon Sep 17 00:00:00 2001
> From: Pedro Alves <palves@redhat.com>
> Date: Wed, 25 Apr 2018 17:28:25 +0100
> Subject: [PATCH] Fix new inferior events output
>
> Since f67c0c917150 ("Enable 'set print inferior-events' and improve
> detach/fork/kill/exit messages"), when detaching a remote process, we
> get, for detach against a remote target:
>
>  (gdb) detach
>  Detaching from program: ...., process 5388
>  Ending remote debugging.
>  [Inferior 1 (Thread 5388.5388) detached]
>               ^^^^^^^^^^^^^^^^
>
> That is incorrect, for it is printing a thread id as string while we
> should be printing the process id instead.  I.e., either one of:
>
>  [Inferior 1 (process 5388) detached]
>  [Inferior 1 (Remote target) detached]
>
> depending on remote stub support for the multi-process extensions.
>
>
> Similarly, after killing a process, we're printing of thread ids while
> we should be printing process ids.  E.g., on native GNU/Linux:
>
>  (gdb) k
>  Kill the program being debugged? (y or n) y
>  [Inferior 1 (Thread 0x7ffff7faa8c0 (LWP 30721)) has been killed]
>               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> while it should have been:
>
>  Kill the program being debugged? (y or n) y
>  [Inferior 1 (process 30721) has been killed]
>               ^^^^^^^^^^^^^
>
> There's a wording inconsistency between detach and kill:
>
>  [Inferior 1 (process 30721) has been killed]
>  [Inferior 1 (process 30721) detached]
>
> Given we were already saying "detached" instead of "has been
> detached", and we used to say just "exited", and given that the "has
> been" doesn't really add any information, this commit changes the the
> message to just "killed":
>
>  [Inferior 1 (process 30721) killed]
>
> gdb/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <palves@redhat.com>
>
> 	* infcmd.c (kill_command): Print the pid as string, not the whole
> 	thread's ptid.  Add comment.  s/has been killed/killed/ in output
> 	message.
> 	* remote.c (remote_detach_1): Print the pid as string, not the
> 	whole thread's ptid.
>
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <palves@redhat.com>
>
> 	* gdb.base/hook-stop.exp: Expect "killed" instead of "has been
> 	killed".
> 	* gdb.base/kill-after-signal.exp: Likewise.
> 	* gdb.threads/kill.exp: Likewise.
> ---
>  gdb/infcmd.c                                 | 7 +++++--
>  gdb/remote.c                                 | 4 +++-
>  gdb/testsuite/gdb.base/hook-stop.exp         | 2 +-
>  gdb/testsuite/gdb.base/kill-after-signal.exp | 2 +-
>  gdb/testsuite/gdb.threads/kill.exp           | 2 +-
>  5 files changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
> index 6c9f885badd..21772b6cff0 100644
> --- a/gdb/infcmd.c
> +++ b/gdb/infcmd.c
> @@ -2596,13 +2596,16 @@ kill_command (const char *arg, int from_tty)
>    if (!query (_("Kill the program being debugged? ")))
>      error (_("Not confirmed."));
>  
> -  std::string pid_str = target_pid_to_str (inferior_ptid);
> +  int pid = current_inferior ()->pid;
> +  /* Save the pid as a string before killing the inferior, since that
> +     may unpush the current target, and we need the string after.  */
> +  std::string pid_str = target_pid_to_str (pid_to_ptid (pid));
>    int infnum = current_inferior ()->num;
>  
>    target_kill ();
>  
>    if (print_inferior_events)
> -    printf_unfiltered (_("[Inferior %d (%s) has been killed]\n"),
> +    printf_unfiltered (_("[Inferior %d (%s) killed]\n"),
>  		       infnum, pid_str.c_str ());
>  
>    /* If we still have other inferiors to debug, then don't mess with
> diff --git a/gdb/remote.c b/gdb/remote.c
> index 61d1dcb5738..193037b6e7a 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -5138,7 +5138,9 @@ remote_detach_1 (int from_tty, inferior *inf)
>       breakpoints that should be available for the followed inferior.  */
>    if (!is_fork_parent)
>      {
> -      std::string infpid = target_pid_to_str (inferior_ptid);
> +      /* Save the pid as a string before mourning, since that will
> +	 unpush the remote target, and we need the string after.  */
> +      std::string infpid = target_pid_to_str (pid_to_ptid (pid));
>  
>        target_mourn_inferior (inferior_ptid);
>        if (print_inferior_events)
> diff --git a/gdb/testsuite/gdb.base/hook-stop.exp b/gdb/testsuite/gdb.base/hook-stop.exp
> index 5717f94fc2a..b3f4084af1e 100644
> --- a/gdb/testsuite/gdb.base/hook-stop.exp
> +++ b/gdb/testsuite/gdb.base/hook-stop.exp
> @@ -86,7 +86,7 @@ proc hook_stop_kill {} {
>  
>  	set test "run hook-stop"
>  	gdb_test_multiple "continue" "$test" {
> -	    -re "Continuing.\r\n\\\[Inferior $decimal \\(.*\\) has been killed\\\]\r\n${gdb_prompt} $" {
> +	    -re "Continuing.\r\n\\\[Inferior $decimal \\(.*\\) 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 67e6f627504..8830d492383 100644
> --- a/gdb/testsuite/gdb.base/kill-after-signal.exp
> +++ b/gdb/testsuite/gdb.base/kill-after-signal.exp
> @@ -39,6 +39,6 @@ gdb_test "continue" "Program received signal SIGUSR1, .*"
>  gdb_test "stepi" "\r\nhandler .*"
>  gdb_test_multiple "kill" "kill" {
>      -re "Kill the program being debugged\\? \\(y or n\\) $" {
> -       gdb_test "y" "\\\[Inferior $decimal \\(.*\\) has been killed\\\]" "kill"
> +       gdb_test "y" "\\\[Inferior $decimal \\(.*\\) killed\\\]" "kill"
>      }
>  }
> diff --git a/gdb/testsuite/gdb.threads/kill.exp b/gdb/testsuite/gdb.threads/kill.exp
> index aea4a986c64..82f221eef67 100644
> --- a/gdb/testsuite/gdb.threads/kill.exp
> +++ b/gdb/testsuite/gdb.threads/kill.exp
> @@ -70,7 +70,7 @@ proc test {threaded} {
>  
>  	gdb_test_multiple "kill" "kill" {
>  	    -re "Kill the program being debugged\\? \\(y or n\\) $" {
> -		gdb_test "y" "\\\[Inferior $decimal \\(.*\\) has been killed\\\]" "kill"
> +		gdb_test "y" "\\\[Inferior $decimal \\(.*\\) killed\\\]" "kill"
>  	    }
>  	}
>      }
> -- 
> 2.14.3

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] Fix new inferior events output (Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages)
  2018-04-25 17:53         ` Sergio Durigan Junior
@ 2018-04-25 18:07           ` Pedro Alves
  0 siblings, 0 replies; 33+ messages in thread
From: Pedro Alves @ 2018-04-25 18:07 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: GDB Patches, Jan Kratochvil

On 04/25/2018 06:53 PM, Sergio Durigan Junior wrote:

> Thanks for catching this problem.  I don't think I have anything to add,
> other than "sorry for not catching this earlier".  For whatever it is
> worth, looks good to me.

No worries.  Now pushed.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 33+ messages in thread

end of thread, other threads:[~2018-04-25 18:07 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-24 19:47 [PATCH] Always print "Detaching after fork from child..." Sergio Durigan Junior
2018-01-24 20:43 ` Jan Kratochvil
2018-01-24 20:56   ` Sergio Durigan Junior
2018-01-25 15:59     ` Pedro Alves
2018-01-25 20:21       ` Sergio Durigan Junior
2018-01-25 22:39         ` Pedro Alves
2018-01-31 16:57 ` [PATCH v2] Enable 'set print inferior-events' and cleanup attach/detach messages Sergio Durigan Junior
2018-02-01 17:17   ` Pedro Alves
2018-03-06  1:44     ` Sergio Durigan Junior
2018-03-09 21:56 ` [PATCH v3] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages Sergio Durigan Junior
2018-03-20 19:24   ` Sergio Durigan Junior
2018-03-26 10:58   ` Pedro Alves
2018-03-26 11:43     ` Pedro Alves
2018-04-03  0:15       ` Sergio Durigan Junior
2018-04-02 21:51     ` Sergio Durigan Junior
2018-04-05 18:47 ` [PATCH v4] " Sergio Durigan Junior
2018-04-05 21:32   ` Sergio Durigan Junior
2018-04-06 15:39   ` Pedro Alves
2018-04-06 15:56     ` Sergio Durigan Junior
2018-04-06 16:41       ` Pedro Alves
2018-04-10 16:22         ` Sergio Durigan Junior
2018-04-11 18:46 ` [PATCH v5] " Sergio Durigan Junior
2018-04-11 19:05   ` Pedro Alves
2018-04-11 19:08     ` Sergio Durigan Junior
2018-04-16 20:04 ` [PATCH v6] " Sergio Durigan Junior
2018-04-17 15:57   ` Pedro Alves
2018-04-17 20:07     ` Sergio Durigan Junior
2018-04-19 19:54 ` [PATCH v7] " Sergio Durigan Junior
2018-04-24 13:33   ` Pedro Alves
2018-04-24 19:49     ` Sergio Durigan Junior
2018-04-25 17:41       ` [PATCH] Fix new inferior events output (Re: [PATCH v7] Enable 'set print inferior-events' and improve detach/fork/kill/exit messages) Pedro Alves
2018-04-25 17:53         ` Sergio Durigan Junior
2018-04-25 18:07           ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).