Always print "Detaching after fork from child..."

Message ID 93ffe8e4-ad56-01b1-c0b5-f9d9c5fb71ec@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves Jan. 25, 2018, 3:58 p.m. UTC
  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(-)
  

Comments

Sergio Durigan Junior Jan. 25, 2018, 8:20 p.m. UTC | #1
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.
  
Pedro Alves Jan. 25, 2018, 10:39 p.m. UTC | #2
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
  

Patch

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,
 }
 
 
+/* 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));