From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12373 invoked by alias); 5 Dec 2007 19:29:09 -0000 Received: (qmail 12361 invoked by uid 22791); 5 Dec 2007 19:29:08 -0000 X-Spam-Check-By: sourceware.org Received: from pauline.vellum.cz (HELO pauline.vellum.cz) (89.250.243.234) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 05 Dec 2007 19:28:56 +0000 Received: from host0.dyn.jankratochvil.net (localhost.localdomain [127.0.0.1]) by pauline.vellum.cz (8.12.11.20060308/8.12.11) with ESMTP id lB5JSp38032493 for ; Wed, 5 Dec 2007 20:28:51 +0100 Received: from host0.dyn.jankratochvil.net (localhost [127.0.0.1]) by host0.dyn.jankratochvil.net (8.14.2/8.14.1) with ESMTP id lB5JSpvB021185 for ; Wed, 5 Dec 2007 20:28:51 +0100 Received: (from jkratoch@localhost) by host0.dyn.jankratochvil.net (8.14.2/8.14.2/Submit) id lB5JSoYw021184 for gdb-patches@sourceware.org; Wed, 5 Dec 2007 20:28:50 +0100 Date: Wed, 05 Dec 2007 19:42:00 -0000 From: Jan Kratochvil To: gdb-patches@sourceware.org Subject: [patch+rfc] Warn the user on forked-off child processes Message-ID: <20071205192850.GA17442@host0.dyn.jankratochvil.net> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="mYCpIKhGyMATD0i+" Content-Disposition: inline User-Agent: Mutt/1.5.17 (2007-11-01) X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2007-12/txt/msg00088.txt.bz2 --mYCpIKhGyMATD0i+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 4994 Hi, some programs fork(2)-off a child process for some part of its run. It was seen on the LMbench `bin/bw_mem' program. User sets a breakpoint which is silently missed and nobody notices there were multiple processes being run. If you would set set debug lin-lwp 1 you would see a message Detaching after fork from child process 11741. but it is clear nobody runs with `set debug lin-lwp 1' set as default. I did put there at least a warning message giving advice there is a fork: [Detaching after fork from child process 20372. (Try `set detach-on-fork off'.)] Regards, Jan Current version: ---------------- /tmp/lmbench-3.0-a9$ upstream-gdb --args bin/x86_64-linux-gnu/bw_mem 1000 rd GNU gdb 6.7.50.20071205-cvs Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-unknown-linux-gnu"... (gdb) b init_loop Breakpoint 1 at 0x4028a1: file bw_mem.c, line 150. (gdb) r Starting program: /tmp/lmbench-3.0-a9/bin/x86_64-linux-gnu/bw_mem 1000 rd warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff1a1fc000 GETOPT ind=1 n=1 arg= av[1]='1000' 0.001000 56284.07 Program exited normally. (gdb) _ Patched GDB at least giving a hint: ----------------------------------- /tmp/lmbench-3.0-a9$ upstream-gdb --args bin/x86_64-linux-gnu/bw_mem 1000 rd GNU gdb 6.7.50.20071205-cvs Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-unknown-linux-gnu"... (gdb) b init_loop Breakpoint 1 at 0x4028a1: file bw_mem.c, line 150. (gdb) r Starting program: /tmp/lmbench-3.0-a9/bin/x86_64-linux-gnu/bw_mem 1000 rd warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff19ffd000 GETOPT ind=1 n=1 arg= av[1]='1000' [Detaching after fork from child process 20372. (Try `set detach-on-fork off'.)] 0.001000 56557.02 Program exited normally. (gdb) _ How to catch the breakpoint the right way in this case: ------------------------------------------------------- /tmp/lmbench-3.0-a9$ upstream-gdb --args bin/x86_64-linux-gnu/bw_mem 1000 rd GNU gdb 6.7.50.20071205-cvs Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-unknown-linux-gnu"... (gdb) set follow-fork-mode child (gdb) b init_loop Breakpoint 1 at 0x4028a1: file bw_mem.c, line 150. (gdb) r Starting program: /tmp/lmbench-3.0-a9/bin/x86_64-linux-gnu/bw_mem 1000 rd warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff57bfd000 GETOPT ind=1 n=1 arg= av[1]='1000' [Switching to process 19531] Breakpoint 1, init_loop (iterations=0, cookie=0x7fff57aa4600) at bw_mem.c:150 150 { (gdb) _ How to catch the breakpoint the suggested way in this case: ----------------------------------------------------------- /tmp/lmbench-3.0-a9$ upstream-gdb --args bin/x86_64-linux-gnu/bw_mem 1000 rd GNU gdb 6.7.50.20071205-cvs Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-unknown-linux-gnu"... (gdb) set detach-on-fork off (gdb) b init_loop Breakpoint 1 at 0x4028a1: file bw_mem.c, line 150. (gdb) r Starting program: /tmp/lmbench-3.0-a9/bin/x86_64-linux-gnu/bw_mem 1000 rd warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff329fc000 GETOPT ind=1 n=1 arg= av[1]='1000' Program received signal SIGINT, Interrupt. 0x00000031668cdda3 in __select_nocancel () from /lib64/libc.so.6 (gdb) info forks 1 process 19596 at 0x316689ad22, file fork.c, line 127 * 0 process 19593 (main process) at 0x31668cdda3, <__select_nocancel> (gdb) fork 1 Switching to process 19596 #0 0x000000316689ad22 in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:127 127 pid = ARCH_FORK (); (gdb) c Continuing. Program received signal SIGINT, Interrupt. 0x000000316689ad22 in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:127 127 pid = ARCH_FORK (); (gdb) c Continuing. Breakpoint 1, init_loop (iterations=0, cookie=0x7fff328613c0) at bw_mem.c:150 150 { (gdb) _ --mYCpIKhGyMATD0i+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline; filename="gdb-fork-detach-warning.patch" Content-length: 4107 2007-04-22 Jan Kratochvil * linux-nat.c (child_follow_fork): Print a user visible warning on a forked-off child process. --- ./gdb/linux-nat.c 9 Feb 2007 20:52:16 -0000 1.56 +++ ./gdb/linux-nat.c 22 Apr 2007 17:20:25 -0000 @@ -378,9 +378,17 @@ child_follow_fork (struct target_ops *op /* Detach new forked process? */ if (detach_fork) { + static int advice_printed = 0; + + target_terminal_ours (); + fprintf_filtered (gdb_stdlog, + _("[Detaching after fork from child process %d.%s]\n"), + child_pid, (advice_printed ? "" : + _(" (Try `set detach-on-fork off'.)"))); + advice_printed = 1; + if (debug_linux_nat) { - target_terminal_ours (); fprintf_filtered (gdb_stdlog, "Detaching after fork from child process %d.\n", child_pid); 2007-04-22 Jan Kratochvil * gdb.base/fork-detach.c, gdb.base/fork-detach.exp: New files. --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ./gdb/testsuite/gdb.base/fork-detach.c 22 Apr 2007 17:20:25 -0000 @@ -0,0 +1,57 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2007 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 2 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, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Please email any bugs, comments, and/or additions to this file to: + bug-gdb@prep.ai.mit.edu */ + +#include +#include +#include +#include +#include + +static void func (void) +{ +} + +int main (void) +{ + pid_t child; + + child = fork (); + switch (child) + { + case -1: + abort (); + case 0: + func (); + break; + default: + { +/* We do not test the switching to the other fork by GDB `fork 1'. */ +#if 0 + pid_t got; + + got = waitpid (child, NULL, 0); + assert (got == child); +#endif + break; + } + } + return 0; +} --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ./gdb/testsuite/gdb.base/fork-detach.exp 22 Apr 2007 17:20:25 -0000 @@ -0,0 +1,43 @@ +# Copyright 2007 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 2 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +if $tracelevel then { + strace $tracelevel +} + +set prms_id 0 +set bug_id 0 + +set testfile fork-detach +set srcfile ${testfile}.c +set binfile ${objdir}/${subdir}/${testfile} +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { + untested "Couldn't compile test program" + return -1 +} + +# Get things started. + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir +gdb_load ${binfile} + +gdb_run_cmd +# `Starting program: .*' prefix is available since gdb-6.7. +gdb_test "" \ + "\\\[Detaching after fork from child process.*Program exited normally\\..*" \ + "Info message caught" --mYCpIKhGyMATD0i+--