patches/gdb/6.3/730-debian_gdb-fix-tracefork-check.patch
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Mon Jul 30 21:09:12 2007 +0000 (2007-07-30)
changeset 306 1984d7bcea28
permissions -rw-r--r--
Small typo fix.
     1 Status: submitted for comments
     2 
     3 2004-11-12  Daniel Jacobowitz  <dan@debian.org>
     4 
     5 	* linux-nat.c (my_waitpid): New function.
     6 	(linux_test_for_tracefork): Make more robust and verbose.  Take
     7 	an ORIGINAL_PID argument and test for PTRACE_SETOPTIONS first.
     8 	(linux_supports_tracefork, linux_supports_tracevforkdone): Take a PID
     9 	argument.  Update calls to linux_test_for_tracefork.
    10 	(linux_enable_event_reporting, child_follow_fork)
    11 	(child_insert_fork_catchpoint, child_insert_vfork_catchpoint)
    12 	(child_insert_exec_catchpoint): Update calls to
    13 	linux_supports_tracefork and linux_supports_tracevforkdone.
    14 
    15 Index: gdb-6.3/gdb/linux-nat.c
    16 ===================================================================
    17 --- gdb-6.3.orig/gdb/linux-nat.c	2004-10-08 16:29:47.000000000 -0400
    18 +++ gdb-6.3/gdb/linux-nat.c	2004-11-13 16:41:51.368720845 -0500
    19 @@ -150,18 +150,47 @@ linux_tracefork_child (void)
    20    exit (0);
    21  }
    22  
    23 -/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.  We
    24 +/* Wrapper function for waitpid which handles EINTR.  */
    25 +
    26 +static int
    27 +my_waitpid (int pid, int *status, int flags)
    28 +{
    29 +  int ret;
    30 +  do
    31 +    {
    32 +      ret = waitpid (pid, status, flags);
    33 +    }
    34 +  while (ret == -1 && errno == EINTR);
    35 +
    36 +  return ret;
    37 +}
    38 +
    39 +/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
    40 +
    41 +   First, we try to enable fork tracing on ORIGINAL_PID.  If this fails,
    42 +   we know that the feature is not available.  This may change the tracing
    43 +   options for ORIGINAL_PID, but we'll be setting them shortly anyway.
    44 +
    45 +   However, if it succeeds, we don't know for sure that the feature is
    46 +   available; old versions of PTRACE_SETOPTIONS ignored unknown options.  We
    47     create a child process, attach to it, use PTRACE_SETOPTIONS to enable
    48 -   fork tracing, and let it fork.  If the process exits, we assume that
    49 -   we can't use TRACEFORK; if we get the fork notification, and we can
    50 -   extract the new child's PID, then we assume that we can.  */
    51 +   fork tracing, and let it fork.  If the process exits, we assume that we
    52 +   can't use TRACEFORK; if we get the fork notification, and we can extract
    53 +   the new child's PID, then we assume that we can.  */
    54  
    55  static void
    56 -linux_test_for_tracefork (void)
    57 +linux_test_for_tracefork (int original_pid)
    58  {
    59    int child_pid, ret, status;
    60    long second_pid;
    61  
    62 +  linux_supports_tracefork_flag = 0;
    63 +  linux_supports_tracevforkdone_flag = 0;
    64 +
    65 +  ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
    66 +  if (ret != 0)
    67 +    return;
    68 +
    69    child_pid = fork ();
    70    if (child_pid == -1)
    71      perror_with_name ("linux_test_for_tracefork: fork");
    72 @@ -169,7 +198,7 @@ linux_test_for_tracefork (void)
    73    if (child_pid == 0)
    74      linux_tracefork_child ();
    75  
    76 -  ret = waitpid (child_pid, &status, 0);
    77 +  ret = my_waitpid (child_pid, &status, 0);
    78    if (ret == -1)
    79      perror_with_name ("linux_test_for_tracefork: waitpid");
    80    else if (ret != child_pid)
    81 @@ -177,13 +206,23 @@ linux_test_for_tracefork (void)
    82    if (! WIFSTOPPED (status))
    83      error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
    84  
    85 -  linux_supports_tracefork_flag = 0;
    86 -
    87    ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
    88    if (ret != 0)
    89      {
    90 -      ptrace (PTRACE_KILL, child_pid, 0, 0);
    91 -      waitpid (child_pid, &status, 0);
    92 +      ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
    93 +      if (ret != 0)
    94 +	{
    95 +	  warning ("linux_test_for_tracefork: failed to kill child");
    96 +	  return;
    97 +	}
    98 +
    99 +      ret = my_waitpid (child_pid, &status, 0);
   100 +      if (ret != child_pid)
   101 +	warning ("linux_test_for_tracefork: failed to wait for killed child");
   102 +      else if (!WIFSIGNALED (status))
   103 +	warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
   104 +		 "killed child", status);
   105 +
   106        return;
   107      }
   108  
   109 @@ -192,8 +231,12 @@ linux_test_for_tracefork (void)
   110  		PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
   111    linux_supports_tracevforkdone_flag = (ret == 0);
   112  
   113 -  ptrace (PTRACE_CONT, child_pid, 0, 0);
   114 -  ret = waitpid (child_pid, &status, 0);
   115 +  ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
   116 +  if (ret != 0)
   117 +    warning ("linux_test_for_tracefork: failed to resume child");
   118 +
   119 +  ret = my_waitpid (child_pid, &status, 0);
   120 +
   121    if (ret == child_pid && WIFSTOPPED (status)
   122        && status >> 16 == PTRACE_EVENT_FORK)
   123      {
   124 @@ -204,34 +247,38 @@ linux_test_for_tracefork (void)
   125  	  int second_status;
   126  
   127  	  linux_supports_tracefork_flag = 1;
   128 -	  waitpid (second_pid, &second_status, 0);
   129 -	  ptrace (PTRACE_DETACH, second_pid, 0, 0);
   130 +	  my_waitpid (second_pid, &second_status, 0);
   131 +	  ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
   132 +	  if (ret != 0)
   133 +	    warning ("linux_test_for_tracefork: failed to kill second child");
   134  	}
   135      }
   136 +  else
   137 +    warning ("linux_test_for_tracefork: unexpected result from waitpid "
   138 +	     "(%d, status 0x%x)", ret, status);
   139  
   140 -  if (WIFSTOPPED (status))
   141 -    {
   142 -      ptrace (PTRACE_DETACH, child_pid, 0, 0);
   143 -      waitpid (child_pid, &status, 0);
   144 -    }
   145 +  ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
   146 +  if (ret != 0)
   147 +    warning ("linux_test_for_tracefork: failed to kill child");
   148 +  my_waitpid (child_pid, &status, 0);
   149  }
   150  
   151  /* Return non-zero iff we have tracefork functionality available.
   152     This function also sets linux_supports_tracefork_flag.  */
   153  
   154  static int
   155 -linux_supports_tracefork (void)
   156 +linux_supports_tracefork (int pid)
   157  {
   158    if (linux_supports_tracefork_flag == -1)
   159 -    linux_test_for_tracefork ();
   160 +    linux_test_for_tracefork (pid);
   161    return linux_supports_tracefork_flag;
   162  }
   163  
   164  static int
   165 -linux_supports_tracevforkdone (void)
   166 +linux_supports_tracevforkdone (int pid)
   167  {
   168    if (linux_supports_tracefork_flag == -1)
   169 -    linux_test_for_tracefork ();
   170 +    linux_test_for_tracefork (pid);
   171    return linux_supports_tracevforkdone_flag;
   172  }
   173  
   174 @@ -242,12 +289,12 @@ linux_enable_event_reporting (ptid_t pti
   175    int pid = ptid_get_pid (ptid);
   176    int options;
   177  
   178 -  if (! linux_supports_tracefork ())
   179 +  if (! linux_supports_tracefork (pid))
   180      return;
   181  
   182    options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
   183      | PTRACE_O_TRACECLONE;
   184 -  if (linux_supports_tracevforkdone ())
   185 +  if (linux_supports_tracevforkdone (pid))
   186      options |= PTRACE_O_TRACEVFORKDONE;
   187  
   188    /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
   189 @@ -308,7 +355,8 @@ child_follow_fork (int follow_child)
   190  
   191        if (has_vforked)
   192  	{
   193 -	  if (linux_supports_tracevforkdone ())
   194 +	  gdb_assert (linux_supports_tracefork_flag >= 0);
   195 +	  if (linux_supports_tracevforkdone (0))
   196  	    {
   197  	      int status;
   198  
   199 @@ -476,7 +524,7 @@ linux_handle_extended_wait (int pid, int
   200  int
   201  child_insert_fork_catchpoint (int pid)
   202  {
   203 -  if (! linux_supports_tracefork ())
   204 +  if (! linux_supports_tracefork (pid))
   205      error ("Your system does not support fork catchpoints.");
   206  
   207    return 0;
   208 @@ -485,7 +533,7 @@ child_insert_fork_catchpoint (int pid)
   209  int
   210  child_insert_vfork_catchpoint (int pid)
   211  {
   212 -  if (!linux_supports_tracefork ())
   213 +  if (!linux_supports_tracefork (pid))
   214      error ("Your system does not support vfork catchpoints.");
   215  
   216    return 0;
   217 @@ -494,7 +542,7 @@ child_insert_vfork_catchpoint (int pid)
   218  int
   219  child_insert_exec_catchpoint (int pid)
   220  {
   221 -  if (!linux_supports_tracefork ())
   222 +  if (!linux_supports_tracefork (pid))
   223      error ("Your system does not support exec catchpoints.");
   224  
   225    return 0;