OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [linux-thread.c] - Diff between revs 105 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 105 Rev 1765
/* Low level interface for debugging GNU/Linux threads for GDB,
/* Low level interface for debugging GNU/Linux threads for GDB,
   the GNU debugger.
   the GNU debugger.
   Copyright 1998, 1999 Free Software Foundation, Inc.
   Copyright 1998, 1999 Free Software Foundation, Inc.
 
 
This file is part of GDB.
This file is part of GDB.
 
 
This program is free software; you can redistribute it and/or modify
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
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 
/* This module implements the debugging interface of the linuxthreads package
/* This module implements the debugging interface of the linuxthreads package
   of the glibc. This package implements a simple clone()-based implementation
   of the glibc. This package implements a simple clone()-based implementation
   of Posix threads for Linux. To use this module, be sure that you have at
   of Posix threads for Linux. To use this module, be sure that you have at
   least the version of the linuxthreads package that holds the support of
   least the version of the linuxthreads package that holds the support of
   GDB (currently 0.8 included in the glibc-2.0.7).
   GDB (currently 0.8 included in the glibc-2.0.7).
 
 
   Right now, the linuxthreads package does not care of priority scheduling,
   Right now, the linuxthreads package does not care of priority scheduling,
   so, neither this module does; In particular, the threads are resumed
   so, neither this module does; In particular, the threads are resumed
   in any order, which could lead to different scheduling than the one
   in any order, which could lead to different scheduling than the one
   happening when GDB does not control the execution.
   happening when GDB does not control the execution.
 
 
   The latest point is that ptrace(PT_ATTACH, ...) is intrusive in Linux:
   The latest point is that ptrace(PT_ATTACH, ...) is intrusive in Linux:
   When a process is attached, then the attaching process becomes the current
   When a process is attached, then the attaching process becomes the current
   parent of the attached process, and the old parent has lost this child.
   parent of the attached process, and the old parent has lost this child.
   If the old parent does a wait[...](), then this child is no longer
   If the old parent does a wait[...](), then this child is no longer
   considered by the kernel as a child of the old parent, thus leading to
   considered by the kernel as a child of the old parent, thus leading to
   results of the call different when the child is attached and when it's not.
   results of the call different when the child is attached and when it's not.
 
 
   A fix has been submitted to the Linux community to solve this problem,
   A fix has been submitted to the Linux community to solve this problem,
   which consequences are not visible to the application itself, but on the
   which consequences are not visible to the application itself, but on the
   process which may wait() for the completion of the application (mostly,
   process which may wait() for the completion of the application (mostly,
   it may consider that the application no longer exists (errno == ECHILD),
   it may consider that the application no longer exists (errno == ECHILD),
   although it does, and thus being unable to get the exit status and resource
   although it does, and thus being unable to get the exit status and resource
   usage of the child. If by chance, it is able to wait() for the application
   usage of the child. If by chance, it is able to wait() for the application
   after it has died (by receiving first a SIGCHILD, and then doing a wait(),
   after it has died (by receiving first a SIGCHILD, and then doing a wait(),
   then the exit status and resource usage may be wrong, because the
   then the exit status and resource usage may be wrong, because the
   linuxthreads package heavily relies on wait() synchronization to keep
   linuxthreads package heavily relies on wait() synchronization to keep
   them correct.  */
   them correct.  */
 
 
#include "defs.h"
#include "defs.h"
#include <sys/types.h> /* for pid_t */
#include <sys/types.h> /* for pid_t */
#include <sys/ptrace.h> /* for PT_* flags */
#include <sys/ptrace.h> /* for PT_* flags */
#include "gdb_wait.h" /* for WUNTRACED and __WCLONE flags */
#include "gdb_wait.h" /* for WUNTRACED and __WCLONE flags */
#include <signal.h> /* for struct sigaction and NSIG */
#include <signal.h> /* for struct sigaction and NSIG */
#include <sys/utsname.h>
#include <sys/utsname.h>
 
 
#include "target.h"
#include "target.h"
#include "inferior.h"
#include "inferior.h"
#include "gdbcore.h"
#include "gdbcore.h"
#include "gdbthread.h"
#include "gdbthread.h"
#include "gdbcmd.h"
#include "gdbcmd.h"
#include "breakpoint.h"
#include "breakpoint.h"
 
 
#ifndef PT_ATTACH
#ifndef PT_ATTACH
#define PT_ATTACH       PTRACE_ATTACH
#define PT_ATTACH       PTRACE_ATTACH
#endif
#endif
#ifndef PT_KILL
#ifndef PT_KILL
#define PT_KILL         PTRACE_KILL
#define PT_KILL         PTRACE_KILL
#endif
#endif
#ifndef PT_READ_U
#ifndef PT_READ_U
#define PT_READ_U       PTRACE_PEEKUSR
#define PT_READ_U       PTRACE_PEEKUSR
#endif
#endif
 
 
#ifdef NSIG
#ifdef NSIG
#define LINUXTHREAD_NSIG NSIG
#define LINUXTHREAD_NSIG NSIG
#else
#else
#ifdef _NSIG
#ifdef _NSIG
#define LINUXTHREAD_NSIG _NSIG
#define LINUXTHREAD_NSIG _NSIG
#endif
#endif
#endif
#endif
 
 
extern int child_suppress_run;          /* make inftarg.c non-runnable */
extern int child_suppress_run;          /* make inftarg.c non-runnable */
struct target_ops linuxthreads_ops;     /* Forward declaration */
struct target_ops linuxthreads_ops;     /* Forward declaration */
extern struct target_ops child_ops;     /* target vector for inftarg.c */
extern struct target_ops child_ops;     /* target vector for inftarg.c */
 
 
static CORE_ADDR linuxthreads_handles;  /* array of linuxthreads handles */
static CORE_ADDR linuxthreads_handles;  /* array of linuxthreads handles */
static CORE_ADDR linuxthreads_manager;  /* pid of linuxthreads manager thread */
static CORE_ADDR linuxthreads_manager;  /* pid of linuxthreads manager thread */
static CORE_ADDR linuxthreads_initial;  /* pid of linuxthreads initial thread */
static CORE_ADDR linuxthreads_initial;  /* pid of linuxthreads initial thread */
static CORE_ADDR linuxthreads_debug;    /* linuxthreads internal debug flag */
static CORE_ADDR linuxthreads_debug;    /* linuxthreads internal debug flag */
static CORE_ADDR linuxthreads_num;      /* number of valid handle entries */
static CORE_ADDR linuxthreads_num;      /* number of valid handle entries */
 
 
static int linuxthreads_max;            /* Maximum number of linuxthreads.
static int linuxthreads_max;            /* Maximum number of linuxthreads.
                                           Zero if this executable doesn't use
                                           Zero if this executable doesn't use
                                           threads, or wasn't linked with a
                                           threads, or wasn't linked with a
                                           debugger-friendly version of the
                                           debugger-friendly version of the
                                           linuxthreads library.  */
                                           linuxthreads library.  */
 
 
static int linuxthreads_sizeof_handle;  /* size of a linuxthreads handle */
static int linuxthreads_sizeof_handle;  /* size of a linuxthreads handle */
static int linuxthreads_offset_descr;   /* h_descr offset of the linuxthreads
static int linuxthreads_offset_descr;   /* h_descr offset of the linuxthreads
                                           handle */
                                           handle */
static int linuxthreads_offset_pid;     /* p_pid offset of the linuxthreads
static int linuxthreads_offset_pid;     /* p_pid offset of the linuxthreads
                                           descr */
                                           descr */
 
 
static int linuxthreads_manager_pid;    /* manager pid */
static int linuxthreads_manager_pid;    /* manager pid */
static int linuxthreads_initial_pid;    /* initial pid */
static int linuxthreads_initial_pid;    /* initial pid */
 
 
/* These variables form a bag of threads with interesting status.  If
/* These variables form a bag of threads with interesting status.  If
   wait_thread (PID) finds that PID stopped for some interesting
   wait_thread (PID) finds that PID stopped for some interesting
   reason (i.e. anything other than stopped with SIGSTOP), then it
   reason (i.e. anything other than stopped with SIGSTOP), then it
   records its status in this queue.  linuxthreads_wait and
   records its status in this queue.  linuxthreads_wait and
   linuxthreads_find_trap extract processes from here.  */
   linuxthreads_find_trap extract processes from here.  */
static int *linuxthreads_wait_pid;      /* wait array of pid */
static int *linuxthreads_wait_pid;      /* wait array of pid */
static int *linuxthreads_wait_status;   /* wait array of status */
static int *linuxthreads_wait_status;   /* wait array of status */
static int linuxthreads_wait_last;      /* index of last valid elt in
static int linuxthreads_wait_last;      /* index of last valid elt in
                                           linuxthreads_wait_{pid,status} */
                                           linuxthreads_wait_{pid,status} */
 
 
static sigset_t linuxthreads_block_mask;  /* sigset without SIGCHLD */
static sigset_t linuxthreads_block_mask;  /* sigset without SIGCHLD */
 
 
static int linuxthreads_step_pid;       /* current stepped pid */
static int linuxthreads_step_pid;       /* current stepped pid */
static int linuxthreads_step_signo;     /* current stepped target signal */
static int linuxthreads_step_signo;     /* current stepped target signal */
static int linuxthreads_exit_status;    /* exit status of initial thread */
static int linuxthreads_exit_status;    /* exit status of initial thread */
 
 
static int linuxthreads_inferior_pid;   /* temporary internal inferior pid */
static int linuxthreads_inferior_pid;   /* temporary internal inferior pid */
static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */
static int linuxthreads_breakpoint_pid; /* last pid that hit a breakpoint */
static int linuxthreads_attach_pending; /* attach command without wait */
static int linuxthreads_attach_pending; /* attach command without wait */
 
 
static int linuxthreads_breakpoints_inserted;   /* any breakpoints inserted */
static int linuxthreads_breakpoints_inserted;   /* any breakpoints inserted */
 
 
/* LinuxThreads uses certain signals for communication between
/* LinuxThreads uses certain signals for communication between
   processes; we need to tell GDB to pass them through silently to the
   processes; we need to tell GDB to pass them through silently to the
   inferior.  The LinuxThreads library has global variables we can
   inferior.  The LinuxThreads library has global variables we can
   read containing the relevant signal numbers, but since the signal
   read containing the relevant signal numbers, but since the signal
   numbers are chosen at run-time, those variables aren't initialized
   numbers are chosen at run-time, those variables aren't initialized
   until the shared library's constructors have had a chance to run.  */
   until the shared library's constructors have had a chance to run.  */
 
 
struct linuxthreads_signal {
struct linuxthreads_signal {
 
 
  /* The name of the LinuxThreads library variable that contains
  /* The name of the LinuxThreads library variable that contains
     the signal number.  */
     the signal number.  */
  char *var;
  char *var;
 
 
  /* True if this variable must exist for us to debug properly.  */
  /* True if this variable must exist for us to debug properly.  */
  int required;
  int required;
 
 
  /* The variable's address in the inferior, or zero if the
  /* The variable's address in the inferior, or zero if the
     LinuxThreads library hasn't been loaded into this inferior yet.  */
     LinuxThreads library hasn't been loaded into this inferior yet.  */
  CORE_ADDR addr;
  CORE_ADDR addr;
 
 
  /* The signal number, or zero if we don't know yet (either because
  /* The signal number, or zero if we don't know yet (either because
     we haven't found the variable, or it hasn't been initialized).
     we haven't found the variable, or it hasn't been initialized).
     This is an actual target signal number that you could pass to
     This is an actual target signal number that you could pass to
     `kill', not a GDB signal number.  */
     `kill', not a GDB signal number.  */
  int signal;
  int signal;
 
 
  /* GDB's original settings for `stop' and `print' for this signal.
  /* GDB's original settings for `stop' and `print' for this signal.
     We restore them when the user selects a different executable.
     We restore them when the user selects a different executable.
     Invariant: if sig->signal != 0, then sig->{stop,print} contain
     Invariant: if sig->signal != 0, then sig->{stop,print} contain
     the original settings.  */
     the original settings.  */
  int stop, print;
  int stop, print;
};
};
 
 
struct linuxthreads_signal linuxthreads_sig_restart = {
struct linuxthreads_signal linuxthreads_sig_restart = {
  "__pthread_sig_restart", 1, 0, 0, 0, 0
  "__pthread_sig_restart", 1, 0, 0, 0, 0
};
};
struct linuxthreads_signal linuxthreads_sig_cancel = {
struct linuxthreads_signal linuxthreads_sig_cancel = {
  "__pthread_sig_cancel", 1, 0, 0, 0, 0
  "__pthread_sig_cancel", 1, 0, 0, 0, 0
};
};
struct linuxthreads_signal linuxthreads_sig_debug = {
struct linuxthreads_signal linuxthreads_sig_debug = {
  "__pthread_sig_debug", 0, 0, 0, 0, 0
  "__pthread_sig_debug", 0, 0, 0, 0, 0
};
};
 
 
/* Set by thread_db module when it takes over the thread_stratum.
/* Set by thread_db module when it takes over the thread_stratum.
   In that case we must:
   In that case we must:
   a) refrain from turning on the debug signal, and
   a) refrain from turning on the debug signal, and
   b) refrain from calling add_thread.  */
   b) refrain from calling add_thread.  */
 
 
int using_thread_db = 0;
int using_thread_db = 0;
 
 
/* A table of breakpoint locations, one per PID.  */
/* A table of breakpoint locations, one per PID.  */
static struct linuxthreads_breakpoint {
static struct linuxthreads_breakpoint {
  CORE_ADDR     pc;     /* PC of breakpoint */
  CORE_ADDR     pc;     /* PC of breakpoint */
  int           pid;    /* pid of breakpoint */
  int           pid;    /* pid of breakpoint */
  int           step;   /* whether the pc has been reached after sstep */
  int           step;   /* whether the pc has been reached after sstep */
} *linuxthreads_breakpoint_zombie;              /* Zombie breakpoints array */
} *linuxthreads_breakpoint_zombie;              /* Zombie breakpoints array */
static int linuxthreads_breakpoint_last;        /* Last zombie breakpoint */
static int linuxthreads_breakpoint_last;        /* Last zombie breakpoint */
 
 
/* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
/* linuxthreads_{insert,remove}_breakpoint pass the breakpoint address
   to {insert,remove}_breakpoint via this variable, since
   to {insert,remove}_breakpoint via this variable, since
   iterate_active_threads doesn't provide any way to pass values
   iterate_active_threads doesn't provide any way to pass values
   through to the worker function.  */
   through to the worker function.  */
static CORE_ADDR linuxthreads_breakpoint_addr;
static CORE_ADDR linuxthreads_breakpoint_addr;
 
 
#define REMOVE_BREAKPOINT_ZOMBIE(_i) \
#define REMOVE_BREAKPOINT_ZOMBIE(_i) \
{ \
{ \
  if ((_i) < linuxthreads_breakpoint_last) \
  if ((_i) < linuxthreads_breakpoint_last) \
    linuxthreads_breakpoint_zombie[(_i)] = \
    linuxthreads_breakpoint_zombie[(_i)] = \
      linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
      linuxthreads_breakpoint_zombie[linuxthreads_breakpoint_last]; \
  linuxthreads_breakpoint_last--; \
  linuxthreads_breakpoint_last--; \
}
}
 
 
 
 


#ifndef PTRACE_XFER_TYPE
#ifndef PTRACE_XFER_TYPE
#define PTRACE_XFER_TYPE int
#define PTRACE_XFER_TYPE int
#endif
#endif
/* Check to see if the given thread is alive.  */
/* Check to see if the given thread is alive.  */
static int
static int
linuxthreads_thread_alive (pid)
linuxthreads_thread_alive (pid)
     int pid;
     int pid;
{
{
  errno = 0;
  errno = 0;
  return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
  return ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE)0, 0) >= 0 || errno == 0;
}
}
 
 
/* On detach(), find a SIGTRAP status.  If stop is non-zero, find a
/* On detach(), find a SIGTRAP status.  If stop is non-zero, find a
   SIGSTOP one, too.
   SIGSTOP one, too.
 
 
   Make sure PID is ready to run, and free of interference from our
   Make sure PID is ready to run, and free of interference from our
   efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals).  If
   efforts to debug it (e.g., pending SIGSTOP or SIGTRAP signals).  If
   STOP is zero, just look for a SIGTRAP.  If STOP is non-zero, look
   STOP is zero, just look for a SIGTRAP.  If STOP is non-zero, look
   for a SIGSTOP, too.  Return non-zero if PID is alive and ready to
   for a SIGSTOP, too.  Return non-zero if PID is alive and ready to
   run; return zero if PID is dead.
   run; return zero if PID is dead.
 
 
   PID may or may not be stopped at the moment, and we may or may not
   PID may or may not be stopped at the moment, and we may or may not
   have waited for it already.  We check the linuxthreads_wait bag in
   have waited for it already.  We check the linuxthreads_wait bag in
   case we've already got a status for it.  We may possibly wait for
   case we've already got a status for it.  We may possibly wait for
   it ourselves.
   it ourselves.
 
 
   PID may have signals waiting to be delivered.  If they're caused by
   PID may have signals waiting to be delivered.  If they're caused by
   our efforts to debug it, accept them with wait, but don't pass them
   our efforts to debug it, accept them with wait, but don't pass them
   through to PID.  Do pass all other signals through.  */
   through to PID.  Do pass all other signals through.  */
static int
static int
linuxthreads_find_trap (pid, stop)
linuxthreads_find_trap (pid, stop)
    int pid;
    int pid;
    int stop;
    int stop;
{
{
  int i;
  int i;
  int rpid;
  int rpid;
  int status;
  int status;
  int found_stop = 0;
  int found_stop = 0;
  int found_trap = 0;
  int found_trap = 0;
 
 
  /* PID may have any number of signals pending.  The kernel will
  /* PID may have any number of signals pending.  The kernel will
     report each of them to us via wait, and then it's up to us to
     report each of them to us via wait, and then it's up to us to
     pass them along to the process via ptrace, if we so choose.
     pass them along to the process via ptrace, if we so choose.
 
 
     We need to paw through the whole set until we've found a SIGTRAP
     We need to paw through the whole set until we've found a SIGTRAP
     (or a SIGSTOP, if `stop' is set).  We don't pass the SIGTRAP (or
     (or a SIGSTOP, if `stop' is set).  We don't pass the SIGTRAP (or
     SIGSTOP) through, but we do re-send all the others, so PID will
     SIGSTOP) through, but we do re-send all the others, so PID will
     receive them when we resume it.  */
     receive them when we resume it.  */
  int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
  int *wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
  int last = 0;
  int last = 0;
 
 
  /* Look at the pending status */
  /* Look at the pending status */
  for (i = linuxthreads_wait_last; i >= 0; i--)
  for (i = linuxthreads_wait_last; i >= 0; i--)
    if (linuxthreads_wait_pid[i] == pid)
    if (linuxthreads_wait_pid[i] == pid)
      {
      {
        status = linuxthreads_wait_status[i];
        status = linuxthreads_wait_status[i];
 
 
        /* Delete the i'th member of the table.  Since the table is
        /* Delete the i'th member of the table.  Since the table is
           unordered, we can do this simply by copying the table's
           unordered, we can do this simply by copying the table's
           last element to the i'th position, and shrinking the table
           last element to the i'th position, and shrinking the table
           by one element.  */
           by one element.  */
        if (i < linuxthreads_wait_last)
        if (i < linuxthreads_wait_last)
          {
          {
            linuxthreads_wait_status[i] =
            linuxthreads_wait_status[i] =
              linuxthreads_wait_status[linuxthreads_wait_last];
              linuxthreads_wait_status[linuxthreads_wait_last];
            linuxthreads_wait_pid[i] =
            linuxthreads_wait_pid[i] =
              linuxthreads_wait_pid[linuxthreads_wait_last];
              linuxthreads_wait_pid[linuxthreads_wait_last];
          }
          }
        linuxthreads_wait_last--;
        linuxthreads_wait_last--;
 
 
        if (!WIFSTOPPED(status)) /* Thread has died */
        if (!WIFSTOPPED(status)) /* Thread has died */
          return 0;
          return 0;
 
 
        if (WSTOPSIG(status) == SIGTRAP)
        if (WSTOPSIG(status) == SIGTRAP)
          {
          {
            if (stop)
            if (stop)
              found_trap = 1;
              found_trap = 1;
            else
            else
              return 1;
              return 1;
          }
          }
        else if (WSTOPSIG(status) == SIGSTOP)
        else if (WSTOPSIG(status) == SIGSTOP)
          {
          {
            if (stop)
            if (stop)
              found_stop = 1;
              found_stop = 1;
          }
          }
        else
        else
          {
          {
            wstatus[0] = status;
            wstatus[0] = status;
            last = 1;
            last = 1;
          }
          }
 
 
        break;
        break;
      }
      }
 
 
  if (stop)
  if (stop)
    {
    {
      /* Make sure that we'll find what we're looking for.  */
      /* Make sure that we'll find what we're looking for.  */
      if (!found_trap)
      if (!found_trap)
        {
        {
          kill (pid, SIGTRAP);
          kill (pid, SIGTRAP);
        }
        }
      if (!found_stop)
      if (!found_stop)
        {
        {
          kill (pid, SIGSTOP);
          kill (pid, SIGSTOP);
        }
        }
    }
    }
 
 
  /* Catch all status until SIGTRAP and optionally SIGSTOP show up.  */
  /* Catch all status until SIGTRAP and optionally SIGSTOP show up.  */
  for (;;)
  for (;;)
    {
    {
      /* resume the child every time... */
      /* resume the child every time... */
      child_resume (pid, 1, TARGET_SIGNAL_0);
      child_resume (pid, 1, TARGET_SIGNAL_0);
 
 
      /* loop as long as errno == EINTR:
      /* loop as long as errno == EINTR:
         waitpid syscall may be aborted due to GDB receiving a signal.
         waitpid syscall may be aborted due to GDB receiving a signal.
         FIXME: EINTR handling should no longer be necessary here, since
         FIXME: EINTR handling should no longer be necessary here, since
         we now block SIGCHLD except in an explicit sigsuspend call.  */
         we now block SIGCHLD except in an explicit sigsuspend call.  */
 
 
      for (;;)
      for (;;)
        {
        {
          rpid = waitpid (pid, &status, __WCLONE);
          rpid = waitpid (pid, &status, __WCLONE);
          if (rpid > 0)
          if (rpid > 0)
            {
            {
              break;
              break;
            }
            }
          if (errno == EINTR)
          if (errno == EINTR)
            {
            {
              continue;
              continue;
            }
            }
 
 
          /* There are a few reasons the wait call above may have
          /* There are a few reasons the wait call above may have
             failed.  If the thread manager dies, its children get
             failed.  If the thread manager dies, its children get
             reparented, and this interferes with GDB waiting for
             reparented, and this interferes with GDB waiting for
             them, in some cases.  Another possibility is that the
             them, in some cases.  Another possibility is that the
             initial thread was not cloned, so calling wait with
             initial thread was not cloned, so calling wait with
             __WCLONE won't find it.  I think neither of these should
             __WCLONE won't find it.  I think neither of these should
             occur in modern Linux kernels --- they don't seem to in
             occur in modern Linux kernels --- they don't seem to in
             2.0.36.  */
             2.0.36.  */
          rpid = waitpid (pid, &status, 0);
          rpid = waitpid (pid, &status, 0);
          if (rpid > 0)
          if (rpid > 0)
            {
            {
              break;
              break;
            }
            }
          if (errno != EINTR)
          if (errno != EINTR)
            perror_with_name ("find_trap/waitpid");
            perror_with_name ("find_trap/waitpid");
        }
        }
 
 
      if (!WIFSTOPPED(status)) /* Thread has died */
      if (!WIFSTOPPED(status)) /* Thread has died */
        return 0;
        return 0;
 
 
      if (WSTOPSIG(status) == SIGTRAP)
      if (WSTOPSIG(status) == SIGTRAP)
        if (!stop || found_stop)
        if (!stop || found_stop)
          break;
          break;
        else
        else
          found_trap = 1;
          found_trap = 1;
      else if (WSTOPSIG(status) != SIGSTOP)
      else if (WSTOPSIG(status) != SIGSTOP)
        wstatus[last++] = status;
        wstatus[last++] = status;
      else if (stop)
      else if (stop)
        {
        {
          if (found_trap)
          if (found_trap)
            break;
            break;
          else
          else
            found_stop = 1;
            found_stop = 1;
        }
        }
    }
    }
 
 
  /* Resend any other signals we noticed to the thread, to be received
  /* Resend any other signals we noticed to the thread, to be received
     when we continue it.  */
     when we continue it.  */
  while (--last >= 0)
  while (--last >= 0)
    {
    {
      kill (pid, WSTOPSIG(wstatus[last]));
      kill (pid, WSTOPSIG(wstatus[last]));
    }
    }
 
 
  return 1;
  return 1;
}
}
 
 
/* Cleanup stub for save_inferior_pid.  */
/* Cleanup stub for save_inferior_pid.  */
static void
static void
restore_inferior_pid (void *arg)
restore_inferior_pid (void *arg)
{
{
  int *saved_pid_ptr = arg;
  int *saved_pid_ptr = arg;
  inferior_pid = *saved_pid_ptr;
  inferior_pid = *saved_pid_ptr;
  free (arg);
  free (arg);
}
}
 
 
/* Register a cleanup to restore the value of inferior_pid.  */
/* Register a cleanup to restore the value of inferior_pid.  */
static struct cleanup *
static struct cleanup *
save_inferior_pid (void)
save_inferior_pid (void)
{
{
  int *saved_pid_ptr;
  int *saved_pid_ptr;
 
 
  saved_pid_ptr = xmalloc (sizeof (int));
  saved_pid_ptr = xmalloc (sizeof (int));
  *saved_pid_ptr = inferior_pid;
  *saved_pid_ptr = inferior_pid;
  return make_cleanup (restore_inferior_pid, saved_pid_ptr);
  return make_cleanup (restore_inferior_pid, saved_pid_ptr);
}
}
 
 
static void
static void
sigchld_handler (signo)
sigchld_handler (signo)
    int signo;
    int signo;
{
{
  /* This handler is used to get an EINTR while doing waitpid()
  /* This handler is used to get an EINTR while doing waitpid()
     when an event is received */
     when an event is received */
}
}
 
 
/* Have we already collected a wait status for PID in the
/* Have we already collected a wait status for PID in the
   linuxthreads_wait bag?  */
   linuxthreads_wait bag?  */
static int
static int
linuxthreads_pending_status (pid)
linuxthreads_pending_status (pid)
    int pid;
    int pid;
{
{
  int i;
  int i;
  for (i = linuxthreads_wait_last; i >= 0; i--)
  for (i = linuxthreads_wait_last; i >= 0; i--)
    if (linuxthreads_wait_pid[i] == pid)
    if (linuxthreads_wait_pid[i] == pid)
      return 1;
      return 1;
  return 0;
  return 0;
}
}
 
 


/* Internal linuxthreads signal management */
/* Internal linuxthreads signal management */
 
 
/* Check in OBJFILE for the variable that holds the number for signal SIG.
/* Check in OBJFILE for the variable that holds the number for signal SIG.
   We assume that we've already found other LinuxThreads-ish variables
   We assume that we've already found other LinuxThreads-ish variables
   in OBJFILE, so we complain if it's required, but not there.
   in OBJFILE, so we complain if it's required, but not there.
   Return true iff things are okay.  */
   Return true iff things are okay.  */
static int
static int
find_signal_var (sig, objfile)
find_signal_var (sig, objfile)
     struct linuxthreads_signal *sig;
     struct linuxthreads_signal *sig;
     struct objfile *objfile;
     struct objfile *objfile;
{
{
  struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
  struct minimal_symbol *ms = lookup_minimal_symbol (sig->var, NULL, objfile);
 
 
  if (! ms)
  if (! ms)
    {
    {
      if (sig->required)
      if (sig->required)
        {
        {
          fprintf_unfiltered (gdb_stderr,
          fprintf_unfiltered (gdb_stderr,
                              "Unable to find linuxthreads symbol \"%s\"\n",
                              "Unable to find linuxthreads symbol \"%s\"\n",
                              sig->var);
                              sig->var);
          return 0;
          return 0;
        }
        }
      else
      else
        {
        {
          sig->addr = 0;
          sig->addr = 0;
          return 1;
          return 1;
        }
        }
    }
    }
 
 
  sig->addr = SYMBOL_VALUE_ADDRESS (ms);
  sig->addr = SYMBOL_VALUE_ADDRESS (ms);
 
 
  return 1;
  return 1;
}
}
 
 
static int
static int
find_all_signal_vars (objfile)
find_all_signal_vars (objfile)
     struct objfile *objfile;
     struct objfile *objfile;
{
{
  return (   find_signal_var (&linuxthreads_sig_restart, objfile)
  return (   find_signal_var (&linuxthreads_sig_restart, objfile)
          && find_signal_var (&linuxthreads_sig_cancel,  objfile)
          && find_signal_var (&linuxthreads_sig_cancel,  objfile)
          && find_signal_var (&linuxthreads_sig_debug,   objfile));
          && find_signal_var (&linuxthreads_sig_debug,   objfile));
}
}
 
 
/* A struct complaint isn't appropriate here.  */
/* A struct complaint isn't appropriate here.  */
static int complained_cannot_determine_thread_signal_number = 0;
static int complained_cannot_determine_thread_signal_number = 0;
 
 
/* Check to see if the variable holding the signal number for SIG has
/* Check to see if the variable holding the signal number for SIG has
   been initialized yet.  If it has, tell GDB to pass that signal
   been initialized yet.  If it has, tell GDB to pass that signal
   through to the inferior silently.  */
   through to the inferior silently.  */
static void
static void
check_signal_number (sig)
check_signal_number (sig)
     struct linuxthreads_signal *sig;
     struct linuxthreads_signal *sig;
{
{
  int num;
  int num;
 
 
  if (sig->signal)
  if (sig->signal)
    /* We already know this signal number.  */
    /* We already know this signal number.  */
    return;
    return;
 
 
  if (! sig->addr)
  if (! sig->addr)
    /* We don't know the variable's address yet.  */
    /* We don't know the variable's address yet.  */
    return;
    return;
 
 
  if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
  if (target_read_memory (sig->addr, (char *)&num, sizeof (num))
      != 0)
      != 0)
    {
    {
      /* If this happens once, it'll probably happen for all the
      /* If this happens once, it'll probably happen for all the
         signals, so only complain once.  */
         signals, so only complain once.  */
      if (! complained_cannot_determine_thread_signal_number)
      if (! complained_cannot_determine_thread_signal_number)
        warning ("Cannot determine thread signal number; "
        warning ("Cannot determine thread signal number; "
                 "GDB may report spurious signals.");
                 "GDB may report spurious signals.");
      complained_cannot_determine_thread_signal_number = 1;
      complained_cannot_determine_thread_signal_number = 1;
      return;
      return;
    }
    }
 
 
  if (num == 0)
  if (num == 0)
    /* It hasn't been initialized yet.  */
    /* It hasn't been initialized yet.  */
    return;
    return;
 
 
  /* We know sig->signal was zero, and is becoming non-zero, so it's
  /* We know sig->signal was zero, and is becoming non-zero, so it's
     okay to sample GDB's original settings.  */
     okay to sample GDB's original settings.  */
  sig->signal = num;
  sig->signal = num;
  sig->stop  = signal_stop_update  (target_signal_from_host (num), 0);
  sig->stop  = signal_stop_update  (target_signal_from_host (num), 0);
  sig->print = signal_print_update (target_signal_from_host (num), 0);
  sig->print = signal_print_update (target_signal_from_host (num), 0);
}
}
 
 
void
void
check_all_signal_numbers ()
check_all_signal_numbers ()
{
{
  /* If this isn't a LinuxThreads program, quit early.  */
  /* If this isn't a LinuxThreads program, quit early.  */
  if (! linuxthreads_max)
  if (! linuxthreads_max)
    return;
    return;
 
 
  check_signal_number (&linuxthreads_sig_restart);
  check_signal_number (&linuxthreads_sig_restart);
  check_signal_number (&linuxthreads_sig_cancel);
  check_signal_number (&linuxthreads_sig_cancel);
  check_signal_number (&linuxthreads_sig_debug);
  check_signal_number (&linuxthreads_sig_debug);
 
 
  /* handle linuxthread exit */
  /* handle linuxthread exit */
  if (linuxthreads_sig_debug.signal
  if (linuxthreads_sig_debug.signal
      || linuxthreads_sig_restart.signal)
      || linuxthreads_sig_restart.signal)
    {
    {
      struct sigaction sact;
      struct sigaction sact;
 
 
      sact.sa_handler = sigchld_handler;
      sact.sa_handler = sigchld_handler;
      sigemptyset(&sact.sa_mask);
      sigemptyset(&sact.sa_mask);
      sact.sa_flags = 0;
      sact.sa_flags = 0;
 
 
      if (linuxthreads_sig_debug.signal > 0)
      if (linuxthreads_sig_debug.signal > 0)
        sigaction(linuxthreads_sig_cancel.signal, &sact, NULL);
        sigaction(linuxthreads_sig_cancel.signal, &sact, NULL);
      else
      else
        sigaction(linuxthreads_sig_restart.signal, &sact, NULL);
        sigaction(linuxthreads_sig_restart.signal, &sact, NULL);
    }
    }
}
}
 
 
 
 
/* Restore GDB's original settings for SIG.
/* Restore GDB's original settings for SIG.
   This should only be called when we're no longer sure if we're
   This should only be called when we're no longer sure if we're
   talking to an executable that uses LinuxThreads, so we clear the
   talking to an executable that uses LinuxThreads, so we clear the
   signal number and variable address too.  */
   signal number and variable address too.  */
static void
static void
restore_signal (sig)
restore_signal (sig)
     struct linuxthreads_signal *sig;
     struct linuxthreads_signal *sig;
{
{
  if (! sig->signal)
  if (! sig->signal)
    return;
    return;
 
 
  /* We know sig->signal was non-zero, and is becoming zero, so it's
  /* We know sig->signal was non-zero, and is becoming zero, so it's
     okay to restore GDB's original settings.  */
     okay to restore GDB's original settings.  */
  signal_stop_update  (target_signal_from_host (sig->signal), sig->stop);
  signal_stop_update  (target_signal_from_host (sig->signal), sig->stop);
  signal_print_update (target_signal_from_host (sig->signal), sig->print);
  signal_print_update (target_signal_from_host (sig->signal), sig->print);
 
 
  sig->signal = 0;
  sig->signal = 0;
  sig->addr = 0;
  sig->addr = 0;
}
}
 
 
 
 
/* Restore GDB's original settings for all LinuxThreads signals.
/* Restore GDB's original settings for all LinuxThreads signals.
   This should only be called when we're no longer sure if we're
   This should only be called when we're no longer sure if we're
   talking to an executable that uses LinuxThreads, so we clear the
   talking to an executable that uses LinuxThreads, so we clear the
   signal number and variable address too.  */
   signal number and variable address too.  */
static void
static void
restore_all_signals ()
restore_all_signals ()
{
{
  restore_signal (&linuxthreads_sig_restart);
  restore_signal (&linuxthreads_sig_restart);
  restore_signal (&linuxthreads_sig_cancel);
  restore_signal (&linuxthreads_sig_cancel);
  restore_signal (&linuxthreads_sig_debug);
  restore_signal (&linuxthreads_sig_debug);
 
 
  /* If it happens again, we should complain again.  */
  /* If it happens again, we should complain again.  */
  complained_cannot_determine_thread_signal_number = 0;
  complained_cannot_determine_thread_signal_number = 0;
}
}
 
 
 
 


 
 
/* Apply FUNC to the pid of each active thread.  This consults the
/* Apply FUNC to the pid of each active thread.  This consults the
   inferior's handle table to find active threads.
   inferior's handle table to find active threads.
 
 
   If ALL is non-zero, process all threads.
   If ALL is non-zero, process all threads.
   If ALL is zero, skip threads with pending status.  */
   If ALL is zero, skip threads with pending status.  */
static void
static void
iterate_active_threads (func, all)
iterate_active_threads (func, all)
    void (*func)(int);
    void (*func)(int);
    int all;
    int all;
{
{
  CORE_ADDR descr;
  CORE_ADDR descr;
  int pid;
  int pid;
  int i;
  int i;
  int num;
  int num;
 
 
  read_memory (linuxthreads_num, (char *)&num, sizeof (int));
  read_memory (linuxthreads_num, (char *)&num, sizeof (int));
 
 
  for (i = 0; i < linuxthreads_max && num > 0; i++)
  for (i = 0; i < linuxthreads_max && num > 0; i++)
    {
    {
      read_memory (linuxthreads_handles +
      read_memory (linuxthreads_handles +
                   linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
                   linuxthreads_sizeof_handle * i + linuxthreads_offset_descr,
                   (char *)&descr, sizeof (void *));
                   (char *)&descr, sizeof (void *));
      if (descr)
      if (descr)
        {
        {
          num--;
          num--;
          read_memory (descr + linuxthreads_offset_pid,
          read_memory (descr + linuxthreads_offset_pid,
                       (char *)&pid, sizeof (pid_t));
                       (char *)&pid, sizeof (pid_t));
          if (pid > 0 && pid != linuxthreads_manager_pid
          if (pid > 0 && pid != linuxthreads_manager_pid
              && (all || (!linuxthreads_pending_status (pid))))
              && (all || (!linuxthreads_pending_status (pid))))
            (*func)(pid);
            (*func)(pid);
        }
        }
    }
    }
}
}
 
 
/* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
/* Insert a thread breakpoint at linuxthreads_breakpoint_addr.
   This is the worker function for linuxthreads_insert_breakpoint,
   This is the worker function for linuxthreads_insert_breakpoint,
   which passes it to iterate_active_threads.  */
   which passes it to iterate_active_threads.  */
static void
static void
insert_breakpoint (pid)
insert_breakpoint (pid)
    int pid;
    int pid;
{
{
  int j;
  int j;
 
 
  /* Remove (if any) the positive zombie breakpoint.  */
  /* Remove (if any) the positive zombie breakpoint.  */
  for (j = linuxthreads_breakpoint_last; j >= 0; j--)
  for (j = linuxthreads_breakpoint_last; j >= 0; j--)
    if (linuxthreads_breakpoint_zombie[j].pid == pid)
    if (linuxthreads_breakpoint_zombie[j].pid == pid)
      {
      {
        if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK
        if ((linuxthreads_breakpoint_zombie[j].pc - DECR_PC_AFTER_BREAK
             == linuxthreads_breakpoint_addr)
             == linuxthreads_breakpoint_addr)
            && !linuxthreads_breakpoint_zombie[j].step)
            && !linuxthreads_breakpoint_zombie[j].step)
          REMOVE_BREAKPOINT_ZOMBIE(j);
          REMOVE_BREAKPOINT_ZOMBIE(j);
        break;
        break;
      }
      }
}
}
 
 
/* Note that we're about to remove a thread breakpoint at
/* Note that we're about to remove a thread breakpoint at
   linuxthreads_breakpoint_addr.
   linuxthreads_breakpoint_addr.
 
 
   This is the worker function for linuxthreads_remove_breakpoint,
   This is the worker function for linuxthreads_remove_breakpoint,
   which passes it to iterate_active_threads.  The actual work of
   which passes it to iterate_active_threads.  The actual work of
   overwriting the breakpoint instruction is done by
   overwriting the breakpoint instruction is done by
   child_ops.to_remove_breakpoint; here, we simply create a zombie
   child_ops.to_remove_breakpoint; here, we simply create a zombie
   breakpoint if the thread's PC is pointing at the breakpoint being
   breakpoint if the thread's PC is pointing at the breakpoint being
   removed.  */
   removed.  */
static void
static void
remove_breakpoint (pid)
remove_breakpoint (pid)
    int pid;
    int pid;
{
{
  int j;
  int j;
 
 
  /* Insert a positive zombie breakpoint (if needed).  */
  /* Insert a positive zombie breakpoint (if needed).  */
  for (j = 0; j <= linuxthreads_breakpoint_last; j++)
  for (j = 0; j <= linuxthreads_breakpoint_last; j++)
    if (linuxthreads_breakpoint_zombie[j].pid == pid)
    if (linuxthreads_breakpoint_zombie[j].pid == pid)
      break;
      break;
 
 
  if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
  if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
    {
    {
      CORE_ADDR pc = read_pc_pid (pid);
      CORE_ADDR pc = read_pc_pid (pid);
      if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
      if (linuxthreads_breakpoint_addr == pc - DECR_PC_AFTER_BREAK
          && j > linuxthreads_breakpoint_last)
          && j > linuxthreads_breakpoint_last)
        {
        {
          linuxthreads_breakpoint_zombie[j].pid = pid;
          linuxthreads_breakpoint_zombie[j].pid = pid;
          linuxthreads_breakpoint_zombie[j].pc = pc;
          linuxthreads_breakpoint_zombie[j].pc = pc;
          linuxthreads_breakpoint_zombie[j].step = 0;
          linuxthreads_breakpoint_zombie[j].step = 0;
          linuxthreads_breakpoint_last++;
          linuxthreads_breakpoint_last++;
        }
        }
    }
    }
}
}
 
 
/* Kill a thread */
/* Kill a thread */
static void
static void
kill_thread (pid)
kill_thread (pid)
    int pid;
    int pid;
{
{
  if (in_thread_list (pid))
  if (in_thread_list (pid))
    {
    {
      ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
      ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
    }
    }
  else
  else
    {
    {
      kill (pid, SIGKILL);
      kill (pid, SIGKILL);
    }
    }
}
}
 
 
/* Resume a thread */
/* Resume a thread */
static void
static void
resume_thread (pid)
resume_thread (pid)
    int pid;
    int pid;
{
{
  if (pid != inferior_pid
  if (pid != inferior_pid
      && in_thread_list (pid)
      && in_thread_list (pid)
      && linuxthreads_thread_alive (pid))
      && linuxthreads_thread_alive (pid))
    {
    {
      if (pid == linuxthreads_step_pid)
      if (pid == linuxthreads_step_pid)
        {
        {
          child_resume (pid, 1, linuxthreads_step_signo);
          child_resume (pid, 1, linuxthreads_step_signo);
        }
        }
      else
      else
        {
        {
          child_resume (pid, 0, TARGET_SIGNAL_0);
          child_resume (pid, 0, TARGET_SIGNAL_0);
        }
        }
    }
    }
}
}
 
 
/* Detach a thread */
/* Detach a thread */
static void
static void
detach_thread (pid)
detach_thread (pid)
    int pid;
    int pid;
{
{
  if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
  if (in_thread_list (pid) && linuxthreads_thread_alive (pid))
    {
    {
      /* Remove pending SIGTRAP and SIGSTOP */
      /* Remove pending SIGTRAP and SIGSTOP */
      linuxthreads_find_trap (pid, 1);
      linuxthreads_find_trap (pid, 1);
 
 
      inferior_pid = pid;
      inferior_pid = pid;
      detach (TARGET_SIGNAL_0);
      detach (TARGET_SIGNAL_0);
      inferior_pid = linuxthreads_manager_pid;
      inferior_pid = linuxthreads_manager_pid;
    }
    }
}
}
 
 
/* Attach a thread */
/* Attach a thread */
void
void
attach_thread (pid)
attach_thread (pid)
     int pid;
     int pid;
{
{
  if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) != 0)
  if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) != 0)
    perror_with_name ("attach_thread");
    perror_with_name ("attach_thread");
}
}
 
 
/* Stop a thread */
/* Stop a thread */
static void
static void
stop_thread (pid)
stop_thread (pid)
    int pid;
    int pid;
{
{
  if (pid != inferior_pid)
  if (pid != inferior_pid)
    {
    {
      if (in_thread_list (pid))
      if (in_thread_list (pid))
        {
        {
          kill (pid, SIGSTOP);
          kill (pid, SIGSTOP);
        }
        }
      else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
      else if (ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0) == 0)
        {
        {
          if (!linuxthreads_attach_pending)
          if (!linuxthreads_attach_pending)
            printf_filtered ("[New %s]\n", target_pid_to_str (pid));
            printf_filtered ("[New %s]\n", target_pid_to_str (pid));
          add_thread (pid);
          add_thread (pid);
          if (linuxthreads_sig_debug.signal)
          if (linuxthreads_sig_debug.signal)
            {
            {
              /* After a new thread in glibc 2.1 signals gdb its existence,
              /* After a new thread in glibc 2.1 signals gdb its existence,
                 it suspends itself and wait for linuxthreads_sig_restart,
                 it suspends itself and wait for linuxthreads_sig_restart,
                 now we can wake it up. */
                 now we can wake it up. */
              kill (pid, linuxthreads_sig_restart.signal);
              kill (pid, linuxthreads_sig_restart.signal);
            }
            }
        }
        }
      else
      else
        perror_with_name ("ptrace in stop_thread");
        perror_with_name ("ptrace in stop_thread");
    }
    }
}
}
 
 
/* Wait for a thread */
/* Wait for a thread */
static void
static void
wait_thread (pid)
wait_thread (pid)
    int pid;
    int pid;
{
{
  int status;
  int status;
  int rpid;
  int rpid;
 
 
  if (pid != inferior_pid && in_thread_list (pid))
  if (pid != inferior_pid && in_thread_list (pid))
    {
    {
      /* loop as long as errno == EINTR:
      /* loop as long as errno == EINTR:
         waitpid syscall may be aborted if GDB receives a signal.
         waitpid syscall may be aborted if GDB receives a signal.
         FIXME: EINTR handling should no longer be necessary here, since
         FIXME: EINTR handling should no longer be necessary here, since
         we now block SIGCHLD except during an explicit sigsuspend call. */
         we now block SIGCHLD except during an explicit sigsuspend call. */
      for (;;)
      for (;;)
        {
        {
          /* Get first pid status.  */
          /* Get first pid status.  */
          rpid = waitpid(pid, &status, __WCLONE);
          rpid = waitpid(pid, &status, __WCLONE);
          if (rpid > 0)
          if (rpid > 0)
            {
            {
              break;
              break;
            }
            }
          if (errno == EINTR)
          if (errno == EINTR)
            {
            {
              continue;
              continue;
            }
            }
 
 
          /* There are two reasons this might have failed:
          /* There are two reasons this might have failed:
 
 
             1) PID is the initial thread, which wasn't cloned, so
             1) PID is the initial thread, which wasn't cloned, so
             passing the __WCLONE flag to waitpid prevented us from
             passing the __WCLONE flag to waitpid prevented us from
             finding it.
             finding it.
 
 
             2) The manager thread is the parent of all but the
             2) The manager thread is the parent of all but the
             initial thread; if it dies, the children will all be
             initial thread; if it dies, the children will all be
             reparented to init, which will wait for them.  This means
             reparented to init, which will wait for them.  This means
             our call to waitpid won't find them.
             our call to waitpid won't find them.
 
 
             Actually, based on a casual look at the 2.0.36 kernel
             Actually, based on a casual look at the 2.0.36 kernel
             code, I don't think either of these cases happen.  But I
             code, I don't think either of these cases happen.  But I
             don't have things set up for remotely debugging the
             don't have things set up for remotely debugging the
             kernel, so I'm not sure.  And perhaps older kernels
             kernel, so I'm not sure.  And perhaps older kernels
             didn't work.  */
             didn't work.  */
          rpid = waitpid(pid, &status, 0);
          rpid = waitpid(pid, &status, 0);
          if (rpid > 0)
          if (rpid > 0)
            {
            {
              break;
              break;
            }
            }
          if (errno != EINTR && linuxthreads_thread_alive (pid))
          if (errno != EINTR && linuxthreads_thread_alive (pid))
            perror_with_name ("wait_thread/waitpid");
            perror_with_name ("wait_thread/waitpid");
 
 
          /* the thread is dead.  */
          /* the thread is dead.  */
          return;
          return;
        }
        }
      if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
      if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
        {
        {
          linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
          linuxthreads_wait_pid[++linuxthreads_wait_last] = pid;
          linuxthreads_wait_status[linuxthreads_wait_last] = status;
          linuxthreads_wait_status[linuxthreads_wait_last] = status;
        }
        }
    }
    }
}
}
 
 
/* Walk through the linuxthreads handles in order to detect all
/* Walk through the linuxthreads handles in order to detect all
   threads and stop them */
   threads and stop them */
static void
static void
update_stop_threads (test_pid)
update_stop_threads (test_pid)
    int test_pid;
    int test_pid;
{
{
  struct cleanup *old_chain = NULL;
  struct cleanup *old_chain = NULL;
 
 
  check_all_signal_numbers ();
  check_all_signal_numbers ();
 
 
  if (linuxthreads_manager_pid == 0)
  if (linuxthreads_manager_pid == 0)
    {
    {
      if (linuxthreads_manager)
      if (linuxthreads_manager)
        {
        {
          if (test_pid > 0 && test_pid != inferior_pid)
          if (test_pid > 0 && test_pid != inferior_pid)
            {
            {
              old_chain = save_inferior_pid ();
              old_chain = save_inferior_pid ();
              inferior_pid = test_pid;
              inferior_pid = test_pid;
            }
            }
          read_memory (linuxthreads_manager,
          read_memory (linuxthreads_manager,
                       (char *)&linuxthreads_manager_pid, sizeof (pid_t));
                       (char *)&linuxthreads_manager_pid, sizeof (pid_t));
        }
        }
      if (linuxthreads_initial)
      if (linuxthreads_initial)
        {
        {
          if (test_pid > 0 && test_pid != inferior_pid)
          if (test_pid > 0 && test_pid != inferior_pid)
            {
            {
              old_chain = save_inferior_pid ();
              old_chain = save_inferior_pid ();
              inferior_pid = test_pid;
              inferior_pid = test_pid;
            }
            }
          read_memory(linuxthreads_initial,
          read_memory(linuxthreads_initial,
                      (char *)&linuxthreads_initial_pid, sizeof (pid_t));
                      (char *)&linuxthreads_initial_pid, sizeof (pid_t));
        }
        }
    }
    }
 
 
  if (linuxthreads_manager_pid != 0)
  if (linuxthreads_manager_pid != 0)
    {
    {
      if (old_chain == NULL && test_pid > 0 &&
      if (old_chain == NULL && test_pid > 0 &&
          test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
          test_pid != inferior_pid && linuxthreads_thread_alive (test_pid))
        {
        {
          old_chain = save_inferior_pid ();
          old_chain = save_inferior_pid ();
          inferior_pid = test_pid;
          inferior_pid = test_pid;
        }
        }
 
 
      if (linuxthreads_thread_alive (inferior_pid))
      if (linuxthreads_thread_alive (inferior_pid))
        {
        {
          if (test_pid > 0)
          if (test_pid > 0)
            {
            {
              if (test_pid != linuxthreads_manager_pid
              if (test_pid != linuxthreads_manager_pid
                  && !linuxthreads_pending_status (linuxthreads_manager_pid))
                  && !linuxthreads_pending_status (linuxthreads_manager_pid))
                {
                {
                  stop_thread (linuxthreads_manager_pid);
                  stop_thread (linuxthreads_manager_pid);
                  wait_thread (linuxthreads_manager_pid);
                  wait_thread (linuxthreads_manager_pid);
                }
                }
              if (!in_thread_list (test_pid))
              if (!in_thread_list (test_pid))
                {
                {
                  if (!linuxthreads_attach_pending)
                  if (!linuxthreads_attach_pending)
                    printf_filtered ("[New %s]\n",
                    printf_filtered ("[New %s]\n",
                                     target_pid_to_str (test_pid));
                                     target_pid_to_str (test_pid));
                  add_thread (test_pid);
                  add_thread (test_pid);
                  if (linuxthreads_sig_debug.signal
                  if (linuxthreads_sig_debug.signal
                      && inferior_pid == test_pid)
                      && inferior_pid == test_pid)
                    {
                    {
                      /* After a new thread in glibc 2.1 signals gdb its
                      /* After a new thread in glibc 2.1 signals gdb its
                         existence, it suspends itself and wait for
                         existence, it suspends itself and wait for
                         linuxthreads_sig_restart, now we can wake it up. */
                         linuxthreads_sig_restart, now we can wake it up. */
                      kill (test_pid, linuxthreads_sig_restart.signal);
                      kill (test_pid, linuxthreads_sig_restart.signal);
                    }
                    }
                }
                }
            }
            }
          iterate_active_threads (stop_thread, 0);
          iterate_active_threads (stop_thread, 0);
          iterate_active_threads (wait_thread, 0);
          iterate_active_threads (wait_thread, 0);
        }
        }
    }
    }
 
 
  if (old_chain != NULL)
  if (old_chain != NULL)
    do_cleanups (old_chain);
    do_cleanups (old_chain);
}
}
 
 
/* This routine is called whenever a new symbol table is read in, or
/* This routine is called whenever a new symbol table is read in, or
   when all symbol tables are removed.  linux-thread event handling
   when all symbol tables are removed.  linux-thread event handling
   can only be initialized when we find the right variables in
   can only be initialized when we find the right variables in
   libpthread.so.  Since it's a shared library, those variables don't
   libpthread.so.  Since it's a shared library, those variables don't
   show up until the library gets mapped and the symbol table is read
   show up until the library gets mapped and the symbol table is read
   in.  */
   in.  */
 
 
/* This new_objfile event is now managed by a chained function pointer.
/* This new_objfile event is now managed by a chained function pointer.
 * It is the callee's responsability to call the next client on the chain.
 * It is the callee's responsability to call the next client on the chain.
 */
 */
 
 
/* Saved pointer to previous owner of the new_objfile event. */
/* Saved pointer to previous owner of the new_objfile event. */
static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
static void (*target_new_objfile_chain) PARAMS ((struct objfile *));
 
 
void
void
linuxthreads_new_objfile (objfile)
linuxthreads_new_objfile (objfile)
    struct objfile *objfile;
    struct objfile *objfile;
{
{
  struct minimal_symbol *ms;
  struct minimal_symbol *ms;
 
 
  /* Call predecessor on chain, if any.
  /* Call predecessor on chain, if any.
     Calling the new module first allows it to dominate,
     Calling the new module first allows it to dominate,
     if it finds its compatible libraries.  */
     if it finds its compatible libraries.  */
 
 
  if (target_new_objfile_chain)
  if (target_new_objfile_chain)
    target_new_objfile_chain (objfile);
    target_new_objfile_chain (objfile);
 
 
  if (!objfile)
  if (!objfile)
    {
    {
      /* We're starting an entirely new executable, so we can no
      /* We're starting an entirely new executable, so we can no
         longer be sure that it uses LinuxThreads.  Restore the signal
         longer be sure that it uses LinuxThreads.  Restore the signal
         flags to their original states.  */
         flags to their original states.  */
      restore_all_signals ();
      restore_all_signals ();
 
 
      /* Indicate that we don't know anything's address any more.  */
      /* Indicate that we don't know anything's address any more.  */
      linuxthreads_max = 0;
      linuxthreads_max = 0;
 
 
      goto quit;
      goto quit;
    }
    }
 
 
  /* If we've already found our variables in another objfile, don't
  /* If we've already found our variables in another objfile, don't
     bother looking for them again.  */
     bother looking for them again.  */
  if (linuxthreads_max)
  if (linuxthreads_max)
    goto quit;
    goto quit;
 
 
  if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
  if (! lookup_minimal_symbol ("__pthread_initial_thread", NULL, objfile))
    /* This object file isn't the pthreads library.  */
    /* This object file isn't the pthreads library.  */
    goto quit;
    goto quit;
 
 
  if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
  if ((ms = lookup_minimal_symbol ("__pthread_threads_debug",
                                   NULL, objfile)) == NULL)
                                   NULL, objfile)) == NULL)
    {
    {
      /* The debugging-aware libpthreads is not present in this objfile */
      /* The debugging-aware libpthreads is not present in this objfile */
      warning ("\
      warning ("\
This program seems to use POSIX threads, but the thread library used\n\
This program seems to use POSIX threads, but the thread library used\n\
does not support debugging.  This may make using GDB difficult.  Don't\n\
does not support debugging.  This may make using GDB difficult.  Don't\n\
set breakpoints or single-step through code that might be executed by\n\
set breakpoints or single-step through code that might be executed by\n\
any thread other than the main thread.");
any thread other than the main thread.");
      goto quit;
      goto quit;
    }
    }
  linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
  linuxthreads_debug = SYMBOL_VALUE_ADDRESS (ms);
 
 
  /* Read internal structures configuration */
  /* Read internal structures configuration */
  if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle",
  if ((ms = lookup_minimal_symbol ("__pthread_sizeof_handle",
                                   NULL, objfile)) == NULL
                                   NULL, objfile)) == NULL
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
                             (char *)&linuxthreads_sizeof_handle,
                             (char *)&linuxthreads_sizeof_handle,
                             sizeof (linuxthreads_sizeof_handle)) != 0)
                             sizeof (linuxthreads_sizeof_handle)) != 0)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_sizeof_handle");
                          "__pthread_sizeof_handle");
      goto quit;
      goto quit;
    }
    }
 
 
  if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr",
  if ((ms = lookup_minimal_symbol ("__pthread_offsetof_descr",
                                   NULL, objfile)) == NULL
                                   NULL, objfile)) == NULL
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
                             (char *)&linuxthreads_offset_descr,
                             (char *)&linuxthreads_offset_descr,
                             sizeof (linuxthreads_offset_descr)) != 0)
                             sizeof (linuxthreads_offset_descr)) != 0)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_offsetof_descr");
                          "__pthread_offsetof_descr");
      goto quit;
      goto quit;
    }
    }
 
 
  if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid",
  if ((ms = lookup_minimal_symbol ("__pthread_offsetof_pid",
                                   NULL, objfile)) == NULL
                                   NULL, objfile)) == NULL
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
                             (char *)&linuxthreads_offset_pid,
                             (char *)&linuxthreads_offset_pid,
                             sizeof (linuxthreads_offset_pid)) != 0)
                             sizeof (linuxthreads_offset_pid)) != 0)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_offsetof_pid");
                          "__pthread_offsetof_pid");
      goto quit;
      goto quit;
    }
    }
 
 
  if (! find_all_signal_vars (objfile))
  if (! find_all_signal_vars (objfile))
    goto quit;
    goto quit;
 
 
  /* Read adresses of internal structures to access */
  /* Read adresses of internal structures to access */
  if ((ms = lookup_minimal_symbol ("__pthread_handles",
  if ((ms = lookup_minimal_symbol ("__pthread_handles",
                                   NULL, objfile)) == NULL)
                                   NULL, objfile)) == NULL)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_handles");
                          "__pthread_handles");
      goto quit;
      goto quit;
    }
    }
  linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
  linuxthreads_handles = SYMBOL_VALUE_ADDRESS (ms);
 
 
  if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
  if ((ms = lookup_minimal_symbol ("__pthread_handles_num",
                                   NULL, objfile)) == NULL)
                                   NULL, objfile)) == NULL)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_handles_num");
                          "__pthread_handles_num");
      goto quit;
      goto quit;
    }
    }
  linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
  linuxthreads_num = SYMBOL_VALUE_ADDRESS (ms);
 
 
  if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
  if ((ms = lookup_minimal_symbol ("__pthread_manager_thread",
                                   NULL, objfile)) == NULL)
                                   NULL, objfile)) == NULL)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_manager_thread");
                          "__pthread_manager_thread");
      goto quit;
      goto quit;
    }
    }
  linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
  linuxthreads_manager = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
 
 
  if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
  if ((ms = lookup_minimal_symbol ("__pthread_initial_thread",
                                   NULL, objfile)) == NULL)
                                   NULL, objfile)) == NULL)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_initial_thread");
                          "__pthread_initial_thread");
      goto quit;
      goto quit;
    }
    }
  linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
  linuxthreads_initial = SYMBOL_VALUE_ADDRESS (ms) + linuxthreads_offset_pid;
 
 
  /* Search for this last, so it won't be set to a non-zero value unless
  /* Search for this last, so it won't be set to a non-zero value unless
     we successfully found all the symbols above.  */
     we successfully found all the symbols above.  */
  if ((ms = lookup_minimal_symbol ("__pthread_threads_max",
  if ((ms = lookup_minimal_symbol ("__pthread_threads_max",
                                   NULL, objfile)) == NULL
                                   NULL, objfile)) == NULL
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
      || target_read_memory (SYMBOL_VALUE_ADDRESS (ms),
                             (char *)&linuxthreads_max,
                             (char *)&linuxthreads_max,
                             sizeof (linuxthreads_max)) != 0)
                             sizeof (linuxthreads_max)) != 0)
    {
    {
      fprintf_unfiltered (gdb_stderr,
      fprintf_unfiltered (gdb_stderr,
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "Unable to find linuxthreads symbol \"%s\"\n",
                          "__pthread_threads_max");
                          "__pthread_threads_max");
      goto quit;
      goto quit;
    }
    }
 
 
  /* Allocate gdb internal structures */
  /* Allocate gdb internal structures */
  linuxthreads_wait_pid =
  linuxthreads_wait_pid =
    (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
    (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
  linuxthreads_wait_status =
  linuxthreads_wait_status =
    (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
    (int *) xmalloc (sizeof (int) * (linuxthreads_max + 1));
  linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *)
  linuxthreads_breakpoint_zombie = (struct linuxthreads_breakpoint *)
    xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1));
    xmalloc (sizeof (struct linuxthreads_breakpoint) * (linuxthreads_max + 1));
 
 
  if (inferior_pid &&
  if (inferior_pid &&
      !linuxthreads_attach_pending &&
      !linuxthreads_attach_pending &&
      !using_thread_db)         /* suppressed by thread_db module */
      !using_thread_db)         /* suppressed by thread_db module */
    {
    {
      int on = 1;
      int on = 1;
 
 
      target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
      target_write_memory (linuxthreads_debug, (char *)&on, sizeof (on));
      linuxthreads_attach_pending = 1;
      linuxthreads_attach_pending = 1;
      update_stop_threads (inferior_pid);
      update_stop_threads (inferior_pid);
      linuxthreads_attach_pending = 0;
      linuxthreads_attach_pending = 0;
    }
    }
 
 
  check_all_signal_numbers ();
  check_all_signal_numbers ();
 
 
quit:
quit:
}
}
 
 
/* If we have switched threads from a one that stopped at breakpoint,
/* If we have switched threads from a one that stopped at breakpoint,
   return 1 otherwise 0.  */
   return 1 otherwise 0.  */
 
 
int
int
linuxthreads_prepare_to_proceed (step)
linuxthreads_prepare_to_proceed (step)
    int step;
    int step;
{
{
  if (!linuxthreads_max
  if (!linuxthreads_max
      || !linuxthreads_manager_pid
      || !linuxthreads_manager_pid
      || !linuxthreads_breakpoint_pid
      || !linuxthreads_breakpoint_pid
      || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid)))
      || !breakpoint_here_p (read_pc_pid (linuxthreads_breakpoint_pid)))
    return 0;
    return 0;
 
 
  if (step)
  if (step)
    {
    {
      /* Mark the current inferior as single stepping process.  */
      /* Mark the current inferior as single stepping process.  */
      linuxthreads_step_pid = inferior_pid;
      linuxthreads_step_pid = inferior_pid;
    }
    }
 
 
  linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
  linuxthreads_inferior_pid = linuxthreads_breakpoint_pid;
  return linuxthreads_breakpoint_pid;
  return linuxthreads_breakpoint_pid;
}
}
 
 
/* Convert a pid to printable form. */
/* Convert a pid to printable form. */
 
 
char *
char *
linuxthreads_pid_to_str (pid)
linuxthreads_pid_to_str (pid)
    int pid;
    int pid;
{
{
  static char buf[100];
  static char buf[100];
 
 
  sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
  sprintf (buf, "%s %d%s", linuxthreads_max ? "Thread" : "Pid", pid,
           (pid == linuxthreads_manager_pid) ? " (manager thread)"
           (pid == linuxthreads_manager_pid) ? " (manager thread)"
           : (pid == linuxthreads_initial_pid) ? " (initial thread)"
           : (pid == linuxthreads_initial_pid) ? " (initial thread)"
           : "");
           : "");
 
 
  return buf;
  return buf;
}
}
 
 
/* Attach to process PID, then initialize for debugging it
/* Attach to process PID, then initialize for debugging it
   and wait for the trace-trap that results from attaching.  */
   and wait for the trace-trap that results from attaching.  */
 
 
static void
static void
linuxthreads_attach (args, from_tty)
linuxthreads_attach (args, from_tty)
    char *args;
    char *args;
    int from_tty;
    int from_tty;
{
{
  if (!args)
  if (!args)
    error_no_arg ("process-id to attach");
    error_no_arg ("process-id to attach");
 
 
  push_target (&linuxthreads_ops);
  push_target (&linuxthreads_ops);
  linuxthreads_breakpoints_inserted = 1;
  linuxthreads_breakpoints_inserted = 1;
  linuxthreads_breakpoint_last = -1;
  linuxthreads_breakpoint_last = -1;
  linuxthreads_wait_last = -1;
  linuxthreads_wait_last = -1;
  WSETSTOP (linuxthreads_exit_status, 0);
  WSETSTOP (linuxthreads_exit_status, 0);
 
 
  child_ops.to_attach (args, from_tty);
  child_ops.to_attach (args, from_tty);
 
 
  if (linuxthreads_max)
  if (linuxthreads_max)
    linuxthreads_attach_pending = 1;
    linuxthreads_attach_pending = 1;
}
}
 
 
/* Take a program previously attached to and detaches it.
/* Take a program previously attached to and detaches it.
   The program resumes execution and will no longer stop
   The program resumes execution and will no longer stop
   on signals, etc.  We'd better not have left any breakpoints
   on signals, etc.  We'd better not have left any breakpoints
   in the program or it'll die when it hits one.  For this
   in the program or it'll die when it hits one.  For this
   to work, it may be necessary for the process to have been
   to work, it may be necessary for the process to have been
   previously attached.  It *might* work if the program was
   previously attached.  It *might* work if the program was
   started via the normal ptrace (PTRACE_TRACEME).  */
   started via the normal ptrace (PTRACE_TRACEME).  */
 
 
static void
static void
linuxthreads_detach (args, from_tty)
linuxthreads_detach (args, from_tty)
    char *args;
    char *args;
    int from_tty;
    int from_tty;
{
{
  if (linuxthreads_max)
  if (linuxthreads_max)
    {
    {
      int i;
      int i;
      int pid;
      int pid;
      int off = 0;
      int off = 0;
      target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
      target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
 
 
      /* Walk through linuxthreads array in order to detach known threads.  */
      /* Walk through linuxthreads array in order to detach known threads.  */
      if (linuxthreads_manager_pid != 0)
      if (linuxthreads_manager_pid != 0)
        {
        {
          /* Get rid of all positive zombie breakpoints.  */
          /* Get rid of all positive zombie breakpoints.  */
          for (i = 0; i <= linuxthreads_breakpoint_last; i++)
          for (i = 0; i <= linuxthreads_breakpoint_last; i++)
            {
            {
              if (linuxthreads_breakpoint_zombie[i].step)
              if (linuxthreads_breakpoint_zombie[i].step)
                continue;
                continue;
 
 
              pid = linuxthreads_breakpoint_zombie[i].pid;
              pid = linuxthreads_breakpoint_zombie[i].pid;
              if (!linuxthreads_thread_alive (pid))
              if (!linuxthreads_thread_alive (pid))
                continue;
                continue;
 
 
              if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
              if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (pid))
                continue;
                continue;
 
 
              /* Continue in STEP mode until the thread pc has moved or
              /* Continue in STEP mode until the thread pc has moved or
                 until SIGTRAP is found on the same PC.  */
                 until SIGTRAP is found on the same PC.  */
              if (linuxthreads_find_trap (pid, 0)
              if (linuxthreads_find_trap (pid, 0)
                  && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid))
                  && linuxthreads_breakpoint_zombie[i].pc == read_pc_pid (pid))
                write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
                write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
                              - DECR_PC_AFTER_BREAK, pid);
                              - DECR_PC_AFTER_BREAK, pid);
            }
            }
 
 
          /* Detach thread after thread.  */
          /* Detach thread after thread.  */
          inferior_pid = linuxthreads_manager_pid;
          inferior_pid = linuxthreads_manager_pid;
          iterate_active_threads (detach_thread, 1);
          iterate_active_threads (detach_thread, 1);
 
 
          /* Remove pending SIGTRAP and SIGSTOP */
          /* Remove pending SIGTRAP and SIGSTOP */
          linuxthreads_find_trap (inferior_pid, 1);
          linuxthreads_find_trap (inferior_pid, 1);
 
 
          linuxthreads_wait_last = -1;
          linuxthreads_wait_last = -1;
          WSETSTOP (linuxthreads_exit_status, 0);
          WSETSTOP (linuxthreads_exit_status, 0);
        }
        }
 
 
      linuxthreads_inferior_pid = 0;
      linuxthreads_inferior_pid = 0;
      linuxthreads_breakpoint_pid = 0;
      linuxthreads_breakpoint_pid = 0;
      linuxthreads_step_pid = 0;
      linuxthreads_step_pid = 0;
      linuxthreads_step_signo = TARGET_SIGNAL_0;
      linuxthreads_step_signo = TARGET_SIGNAL_0;
      linuxthreads_manager_pid = 0;
      linuxthreads_manager_pid = 0;
      linuxthreads_initial_pid = 0;
      linuxthreads_initial_pid = 0;
      linuxthreads_attach_pending = 0;
      linuxthreads_attach_pending = 0;
      init_thread_list ();           /* Destroy thread info */
      init_thread_list ();           /* Destroy thread info */
    }
    }
 
 
  child_ops.to_detach (args, from_tty);
  child_ops.to_detach (args, from_tty);
 
 
  unpush_target (&linuxthreads_ops);
  unpush_target (&linuxthreads_ops);
}
}
 
 
/* Resume execution of process PID.  If STEP is nozero, then
/* Resume execution of process PID.  If STEP is nozero, then
   just single step it.  If SIGNAL is nonzero, restart it with that
   just single step it.  If SIGNAL is nonzero, restart it with that
   signal activated.  */
   signal activated.  */
 
 
static void
static void
linuxthreads_resume (pid, step, signo)
linuxthreads_resume (pid, step, signo)
    int pid;
    int pid;
    int step;
    int step;
    enum target_signal signo;
    enum target_signal signo;
{
{
  if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
  if (!linuxthreads_max || stop_soon_quietly || linuxthreads_manager_pid == 0)
    {
    {
      child_ops.to_resume (pid, step, signo);
      child_ops.to_resume (pid, step, signo);
    }
    }
  else
  else
    {
    {
      int rpid;
      int rpid;
      if (linuxthreads_inferior_pid)
      if (linuxthreads_inferior_pid)
        {
        {
          /* Prepare resume of the last thread that hit a breakpoint */
          /* Prepare resume of the last thread that hit a breakpoint */
          linuxthreads_breakpoints_inserted = 0;
          linuxthreads_breakpoints_inserted = 0;
          rpid = linuxthreads_inferior_pid;
          rpid = linuxthreads_inferior_pid;
          linuxthreads_step_signo = signo;
          linuxthreads_step_signo = signo;
        }
        }
      else
      else
        {
        {
          struct cleanup *old_chain = NULL;
          struct cleanup *old_chain = NULL;
          int i;
          int i;
 
 
          if (pid < 0)
          if (pid < 0)
            {
            {
              linuxthreads_step_pid = step ? inferior_pid : 0;
              linuxthreads_step_pid = step ? inferior_pid : 0;
              linuxthreads_step_signo = signo;
              linuxthreads_step_signo = signo;
              rpid = inferior_pid;
              rpid = inferior_pid;
            }
            }
          else
          else
            rpid = pid;
            rpid = pid;
 
 
          if (pid < 0 || !step)
          if (pid < 0 || !step)
            {
            {
              linuxthreads_breakpoints_inserted = 1;
              linuxthreads_breakpoints_inserted = 1;
 
 
              /* Walk through linuxthreads array in order to resume threads */
              /* Walk through linuxthreads array in order to resume threads */
              if (pid >= 0 && inferior_pid != pid)
              if (pid >= 0 && inferior_pid != pid)
                {
                {
                  old_chain = save_inferior_pid ();
                  old_chain = save_inferior_pid ();
                  inferior_pid = pid;
                  inferior_pid = pid;
                }
                }
 
 
              iterate_active_threads (resume_thread, 0);
              iterate_active_threads (resume_thread, 0);
              if (linuxthreads_manager_pid != inferior_pid
              if (linuxthreads_manager_pid != inferior_pid
                  && !linuxthreads_pending_status (linuxthreads_manager_pid))
                  && !linuxthreads_pending_status (linuxthreads_manager_pid))
                resume_thread (linuxthreads_manager_pid);
                resume_thread (linuxthreads_manager_pid);
            }
            }
          else
          else
            linuxthreads_breakpoints_inserted = 0;
            linuxthreads_breakpoints_inserted = 0;
 
 
          /* Deal with zombie breakpoint */
          /* Deal with zombie breakpoint */
          for (i = 0; i <= linuxthreads_breakpoint_last; i++)
          for (i = 0; i <= linuxthreads_breakpoint_last; i++)
            if (linuxthreads_breakpoint_zombie[i].pid == rpid)
            if (linuxthreads_breakpoint_zombie[i].pid == rpid)
              {
              {
                if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
                if (linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
                  {
                  {
                    /* The current pc is out of zombie breakpoint.  */
                    /* The current pc is out of zombie breakpoint.  */
                    REMOVE_BREAKPOINT_ZOMBIE(i);
                    REMOVE_BREAKPOINT_ZOMBIE(i);
                  }
                  }
                break;
                break;
              }
              }
 
 
          if (old_chain != NULL)
          if (old_chain != NULL)
            do_cleanups (old_chain);
            do_cleanups (old_chain);
        }
        }
 
 
      /* Resume initial thread. */
      /* Resume initial thread. */
      /* [unles it has a wait event pending] */
      /* [unles it has a wait event pending] */
      if (!linuxthreads_pending_status (rpid))
      if (!linuxthreads_pending_status (rpid))
        {
        {
          child_ops.to_resume (rpid, step, signo);
          child_ops.to_resume (rpid, step, signo);
        }
        }
    }
    }
}
}
 
 
/* Abstract out the child_wait functionality.  */
/* Abstract out the child_wait functionality.  */
int
int
linux_child_wait (pid, rpid, status)
linux_child_wait (pid, rpid, status)
     int pid;
     int pid;
     int *rpid;
     int *rpid;
     int *status;
     int *status;
{
{
  int save_errno;
  int save_errno;
 
 
  /* Note: inftarg has these inside the loop. */
  /* Note: inftarg has these inside the loop. */
  set_sigint_trap ();   /* Causes SIGINT to be passed on to the
  set_sigint_trap ();   /* Causes SIGINT to be passed on to the
                           attached process. */
                           attached process. */
  set_sigio_trap  ();
  set_sigio_trap  ();
 
 
  errno = save_errno = 0;
  errno = save_errno = 0;
  for (;;)
  for (;;)
    {
    {
      errno = 0;
      errno = 0;
      *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
      *rpid = waitpid (pid, status, __WCLONE | WNOHANG);
      save_errno = errno;
      save_errno = errno;
 
 
      if (*rpid > 0)
      if (*rpid > 0)
        {
        {
          /* Got an event -- break out */
          /* Got an event -- break out */
          break;
          break;
        }
        }
      if (errno == EINTR)       /* interrupted by signal, try again */
      if (errno == EINTR)       /* interrupted by signal, try again */
        {
        {
          continue;
          continue;
        }
        }
 
 
      errno = 0;
      errno = 0;
      *rpid = waitpid (pid, status, WNOHANG);
      *rpid = waitpid (pid, status, WNOHANG);
      if (*rpid > 0)
      if (*rpid > 0)
        {
        {
          /* Got an event -- break out */
          /* Got an event -- break out */
          break;
          break;
        }
        }
      if (errno == EINTR)
      if (errno == EINTR)
        {
        {
          continue;
          continue;
        }
        }
      if (errno != 0 && save_errno != 0)
      if (errno != 0 && save_errno != 0)
        {
        {
          break;
          break;
        }
        }
      sigsuspend(&linuxthreads_block_mask);
      sigsuspend(&linuxthreads_block_mask);
    }
    }
  clear_sigio_trap  ();
  clear_sigio_trap  ();
  clear_sigint_trap ();
  clear_sigint_trap ();
 
 
  return errno ? errno : save_errno;
  return errno ? errno : save_errno;
}
}
 
 
 
 
/* Wait for any threads to stop.  We may have to convert PID from a thread id
/* Wait for any threads to stop.  We may have to convert PID from a thread id
   to a LWP id, and vice versa on the way out.  */
   to a LWP id, and vice versa on the way out.  */
 
 
static int
static int
linuxthreads_wait (pid, ourstatus)
linuxthreads_wait (pid, ourstatus)
     int pid;
     int pid;
     struct target_waitstatus *ourstatus;
     struct target_waitstatus *ourstatus;
{
{
  int status;
  int status;
  int rpid;
  int rpid;
  int i;
  int i;
  int last;
  int last;
  int *wstatus;
  int *wstatus;
 
 
  if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
  if (linuxthreads_max && !linuxthreads_breakpoints_inserted)
    wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
    wstatus = alloca (LINUXTHREAD_NSIG * sizeof (int));
 
 
  /* See if the inferior has chosen values for its signals yet.  By
  /* See if the inferior has chosen values for its signals yet.  By
     checking for them here, we can be sure we've updated GDB's signal
     checking for them here, we can be sure we've updated GDB's signal
     handling table before the inferior ever gets one of them.  (Well,
     handling table before the inferior ever gets one of them.  (Well,
     before we notice, anyway.)  */
     before we notice, anyway.)  */
  check_all_signal_numbers ();
  check_all_signal_numbers ();
 
 
  for (;;)
  for (;;)
    {
    {
      if (!linuxthreads_max)
      if (!linuxthreads_max)
          rpid = 0;
          rpid = 0;
      else if (!linuxthreads_breakpoints_inserted)
      else if (!linuxthreads_breakpoints_inserted)
        {
        {
          if (linuxthreads_inferior_pid)
          if (linuxthreads_inferior_pid)
            pid = linuxthreads_inferior_pid;
            pid = linuxthreads_inferior_pid;
          else if (pid < 0)
          else if (pid < 0)
            pid = inferior_pid;
            pid = inferior_pid;
          last = rpid = 0;
          last = rpid = 0;
        }
        }
      else if (pid < 0 && linuxthreads_wait_last >= 0)
      else if (pid < 0 && linuxthreads_wait_last >= 0)
        {
        {
          status = linuxthreads_wait_status[linuxthreads_wait_last];
          status = linuxthreads_wait_status[linuxthreads_wait_last];
          rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
          rpid = linuxthreads_wait_pid[linuxthreads_wait_last--];
        }
        }
      else if (pid > 0 && linuxthreads_pending_status (pid))
      else if (pid > 0 && linuxthreads_pending_status (pid))
        {
        {
          for (i = linuxthreads_wait_last; i >= 0; i--)
          for (i = linuxthreads_wait_last; i >= 0; i--)
            if (linuxthreads_wait_pid[i] == pid)
            if (linuxthreads_wait_pid[i] == pid)
                break;
                break;
          if (i < 0)
          if (i < 0)
            rpid = 0;
            rpid = 0;
          else
          else
            {
            {
              status = linuxthreads_wait_status[i];
              status = linuxthreads_wait_status[i];
              rpid = pid;
              rpid = pid;
              if (i < linuxthreads_wait_last)
              if (i < linuxthreads_wait_last)
                {
                {
                  linuxthreads_wait_status[i] =
                  linuxthreads_wait_status[i] =
                    linuxthreads_wait_status[linuxthreads_wait_last];
                    linuxthreads_wait_status[linuxthreads_wait_last];
                  linuxthreads_wait_pid[i] =
                  linuxthreads_wait_pid[i] =
                    linuxthreads_wait_pid[linuxthreads_wait_last];
                    linuxthreads_wait_pid[linuxthreads_wait_last];
                }
                }
              linuxthreads_wait_last--;
              linuxthreads_wait_last--;
            }
            }
        }
        }
      else
      else
          rpid = 0;
          rpid = 0;
 
 
      if (rpid == 0)
      if (rpid == 0)
        {
        {
          int save_errno;
          int save_errno;
 
 
          save_errno = linux_child_wait (pid, &rpid, &status);
          save_errno = linux_child_wait (pid, &rpid, &status);
 
 
          if (rpid == -1)
          if (rpid == -1)
            {
            {
              if (WIFEXITED(linuxthreads_exit_status))
              if (WIFEXITED(linuxthreads_exit_status))
                {
                {
                  store_waitstatus (ourstatus, linuxthreads_exit_status);
                  store_waitstatus (ourstatus, linuxthreads_exit_status);
                  return inferior_pid;
                  return inferior_pid;
                }
                }
              else
              else
                {
                {
                  fprintf_unfiltered
                  fprintf_unfiltered
                      (gdb_stderr, "Child process unexpectedly missing: %s.\n",
                      (gdb_stderr, "Child process unexpectedly missing: %s.\n",
                       safe_strerror (save_errno));
                       safe_strerror (save_errno));
                  /* Claim it exited with unknown signal.  */
                  /* Claim it exited with unknown signal.  */
                  ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
                  ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
                  ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
                  ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
                  return -1;
                  return -1;
                }
                }
            }
            }
 
 
          /* We have now gotten a new event from waitpid above. */
          /* We have now gotten a new event from waitpid above. */
 
 
          /* Signals arrive in any order.  So get all signals until
          /* Signals arrive in any order.  So get all signals until
             SIGTRAP and resend previous ones to be held after.  */
             SIGTRAP and resend previous ones to be held after.  */
          if (linuxthreads_max
          if (linuxthreads_max
              && !linuxthreads_breakpoints_inserted
              && !linuxthreads_breakpoints_inserted
              && WIFSTOPPED(status))
              && WIFSTOPPED(status))
            if (WSTOPSIG(status) == SIGTRAP)
            if (WSTOPSIG(status) == SIGTRAP)
              {
              {
                while (--last >= 0)
                while (--last >= 0)
                  {
                  {
                    kill (rpid, WSTOPSIG(wstatus[last]));
                    kill (rpid, WSTOPSIG(wstatus[last]));
                  }
                  }
 
 
                /* insert negative zombie breakpoint */
                /* insert negative zombie breakpoint */
                for (i = 0; i <= linuxthreads_breakpoint_last; i++)
                for (i = 0; i <= linuxthreads_breakpoint_last; i++)
                  if (linuxthreads_breakpoint_zombie[i].pid == rpid)
                  if (linuxthreads_breakpoint_zombie[i].pid == rpid)
                      break;
                      break;
                if (i > linuxthreads_breakpoint_last)
                if (i > linuxthreads_breakpoint_last)
                  {
                  {
                    linuxthreads_breakpoint_zombie[i].pid = rpid;
                    linuxthreads_breakpoint_zombie[i].pid = rpid;
                    linuxthreads_breakpoint_last++;
                    linuxthreads_breakpoint_last++;
                  }
                  }
                linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
                linuxthreads_breakpoint_zombie[i].pc = read_pc_pid (rpid);
                linuxthreads_breakpoint_zombie[i].step = 1;
                linuxthreads_breakpoint_zombie[i].step = 1;
              }
              }
            else
            else
              {
              {
                if (WSTOPSIG(status) != SIGSTOP)
                if (WSTOPSIG(status) != SIGSTOP)
                  {
                  {
                    for (i = 0; i < last; i++)
                    for (i = 0; i < last; i++)
                      if (wstatus[i] == status)
                      if (wstatus[i] == status)
                        break;
                        break;
                    if (i >= last)
                    if (i >= last)
                      {
                      {
                        wstatus[last++] = status;
                        wstatus[last++] = status;
                      }
                      }
                  }
                  }
                child_resume (rpid, 1, TARGET_SIGNAL_0);
                child_resume (rpid, 1, TARGET_SIGNAL_0);
                continue;
                continue;
              }
              }
          if (linuxthreads_inferior_pid)
          if (linuxthreads_inferior_pid)
            linuxthreads_inferior_pid = 0;
            linuxthreads_inferior_pid = 0;
        }
        }
 
 
      if (linuxthreads_max && !stop_soon_quietly)
      if (linuxthreads_max && !stop_soon_quietly)
        {
        {
          if (linuxthreads_max
          if (linuxthreads_max
              && WIFSTOPPED(status)
              && WIFSTOPPED(status)
              && WSTOPSIG(status) == SIGSTOP)
              && WSTOPSIG(status) == SIGSTOP)
            {
            {
              /* Skip SIGSTOP signals.  */
              /* Skip SIGSTOP signals.  */
              if (!linuxthreads_pending_status (rpid))
              if (!linuxthreads_pending_status (rpid))
                {
                {
                  if (linuxthreads_step_pid == rpid)
                  if (linuxthreads_step_pid == rpid)
                    {
                    {
                      child_resume (rpid, 1, linuxthreads_step_signo);
                      child_resume (rpid, 1, linuxthreads_step_signo);
                    }
                    }
                  else
                  else
                    {
                    {
                      child_resume (rpid, 0, TARGET_SIGNAL_0);
                      child_resume (rpid, 0, TARGET_SIGNAL_0);
                    }
                    }
                }
                }
              continue;
              continue;
            }
            }
 
 
          /* Do no report exit status of cloned threads.  */
          /* Do no report exit status of cloned threads.  */
          if (WIFEXITED(status))
          if (WIFEXITED(status))
            {
            {
              if (rpid == linuxthreads_initial_pid)
              if (rpid == linuxthreads_initial_pid)
                linuxthreads_exit_status = status;
                linuxthreads_exit_status = status;
 
 
              /* Remove any zombie breakpoint.  */
              /* Remove any zombie breakpoint.  */
              for (i = 0; i <= linuxthreads_breakpoint_last; i++)
              for (i = 0; i <= linuxthreads_breakpoint_last; i++)
                if (linuxthreads_breakpoint_zombie[i].pid == rpid)
                if (linuxthreads_breakpoint_zombie[i].pid == rpid)
                  {
                  {
                    REMOVE_BREAKPOINT_ZOMBIE(i);
                    REMOVE_BREAKPOINT_ZOMBIE(i);
                    break;
                    break;
                  }
                  }
              if (pid > 0)
              if (pid > 0)
                pid = -1;
                pid = -1;
              continue;
              continue;
            }
            }
 
 
          /* Deal with zombie breakpoint */
          /* Deal with zombie breakpoint */
          for (i = 0; i <= linuxthreads_breakpoint_last; i++)
          for (i = 0; i <= linuxthreads_breakpoint_last; i++)
            if (linuxthreads_breakpoint_zombie[i].pid == rpid)
            if (linuxthreads_breakpoint_zombie[i].pid == rpid)
              break;
              break;
 
 
          if (i <= linuxthreads_breakpoint_last)
          if (i <= linuxthreads_breakpoint_last)
            {
            {
              /* There is a potential zombie breakpoint */
              /* There is a potential zombie breakpoint */
              if (WIFEXITED(status)
              if (WIFEXITED(status)
                  || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
                  || linuxthreads_breakpoint_zombie[i].pc != read_pc_pid (rpid))
                {
                {
                  /* The current pc is out of zombie breakpoint.  */
                  /* The current pc is out of zombie breakpoint.  */
                  REMOVE_BREAKPOINT_ZOMBIE(i);
                  REMOVE_BREAKPOINT_ZOMBIE(i);
                }
                }
              else if (!linuxthreads_breakpoint_zombie[i].step
              else if (!linuxthreads_breakpoint_zombie[i].step
                       && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
                       && WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
                {
                {
                  /* This is a real one ==> decrement PC and restart.  */
                  /* This is a real one ==> decrement PC and restart.  */
                  write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
                  write_pc_pid (linuxthreads_breakpoint_zombie[i].pc
                                - DECR_PC_AFTER_BREAK, rpid);
                                - DECR_PC_AFTER_BREAK, rpid);
                  if (linuxthreads_step_pid == rpid)
                  if (linuxthreads_step_pid == rpid)
                    {
                    {
                      child_resume (rpid, 1, linuxthreads_step_signo);
                      child_resume (rpid, 1, linuxthreads_step_signo);
                    }
                    }
                  else
                  else
                    {
                    {
                      child_resume (rpid, 0, TARGET_SIGNAL_0);
                      child_resume (rpid, 0, TARGET_SIGNAL_0);
                    }
                    }
                  continue;
                  continue;
                }
                }
            }
            }
 
 
          /* Walk through linuxthreads array in order to stop them */
          /* Walk through linuxthreads array in order to stop them */
          if (linuxthreads_breakpoints_inserted)
          if (linuxthreads_breakpoints_inserted)
            update_stop_threads (rpid);
            update_stop_threads (rpid);
 
 
        }
        }
      else if (rpid != inferior_pid)
      else if (rpid != inferior_pid)
        continue;
        continue;
 
 
      store_waitstatus (ourstatus, status);
      store_waitstatus (ourstatus, status);
 
 
      if (linuxthreads_attach_pending && !stop_soon_quietly)
      if (linuxthreads_attach_pending && !stop_soon_quietly)
        {
        {
          int on = 1;
          int on = 1;
          if (!using_thread_db)
          if (!using_thread_db)
            {
            {
              target_write_memory (linuxthreads_debug,
              target_write_memory (linuxthreads_debug,
                                   (char *) &on, sizeof (on));
                                   (char *) &on, sizeof (on));
              update_stop_threads (rpid);
              update_stop_threads (rpid);
            }
            }
          linuxthreads_attach_pending = 0;
          linuxthreads_attach_pending = 0;
        }
        }
 
 
      if (linuxthreads_breakpoints_inserted
      if (linuxthreads_breakpoints_inserted
          && WIFSTOPPED(status)
          && WIFSTOPPED(status)
          && WSTOPSIG(status) == SIGTRAP)
          && WSTOPSIG(status) == SIGTRAP)
        linuxthreads_breakpoint_pid = rpid;
        linuxthreads_breakpoint_pid = rpid;
      else if (linuxthreads_breakpoint_pid)
      else if (linuxthreads_breakpoint_pid)
        linuxthreads_breakpoint_pid = 0;
        linuxthreads_breakpoint_pid = 0;
 
 
      return rpid;
      return rpid;
    }
    }
}
}
 
 
/* Fork an inferior process, and start debugging it with ptrace.  */
/* Fork an inferior process, and start debugging it with ptrace.  */
 
 
static void
static void
linuxthreads_create_inferior (exec_file, allargs, env)
linuxthreads_create_inferior (exec_file, allargs, env)
    char *exec_file;
    char *exec_file;
    char *allargs;
    char *allargs;
    char **env;
    char **env;
{
{
  if (!exec_file && !exec_bfd)
  if (!exec_file && !exec_bfd)
    {
    {
      error ("No executable file specified.\n\
      error ("No executable file specified.\n\
Use the \"file\" or \"exec-file\" command.");
Use the \"file\" or \"exec-file\" command.");
      return;
      return;
    }
    }
 
 
  push_target (&linuxthreads_ops);
  push_target (&linuxthreads_ops);
  linuxthreads_breakpoints_inserted = 1;
  linuxthreads_breakpoints_inserted = 1;
  linuxthreads_breakpoint_last = -1;
  linuxthreads_breakpoint_last = -1;
  linuxthreads_wait_last = -1;
  linuxthreads_wait_last = -1;
  WSETSTOP (linuxthreads_exit_status, 0);
  WSETSTOP (linuxthreads_exit_status, 0);
 
 
  if (linuxthreads_max)
  if (linuxthreads_max)
    linuxthreads_attach_pending = 1;
    linuxthreads_attach_pending = 1;
 
 
  child_ops.to_create_inferior (exec_file, allargs, env);
  child_ops.to_create_inferior (exec_file, allargs, env);
}
}
 
 
void
void
linuxthreads_discard_global_state ()
linuxthreads_discard_global_state ()
{
{
  linuxthreads_inferior_pid = 0;
  linuxthreads_inferior_pid = 0;
  linuxthreads_breakpoint_pid = 0;
  linuxthreads_breakpoint_pid = 0;
  linuxthreads_step_pid = 0;
  linuxthreads_step_pid = 0;
  linuxthreads_step_signo = TARGET_SIGNAL_0;
  linuxthreads_step_signo = TARGET_SIGNAL_0;
  linuxthreads_manager_pid = 0;
  linuxthreads_manager_pid = 0;
  linuxthreads_initial_pid = 0;
  linuxthreads_initial_pid = 0;
  linuxthreads_attach_pending = 0;
  linuxthreads_attach_pending = 0;
  linuxthreads_max = 0;
  linuxthreads_max = 0;
}
}
 
 
/* Clean up after the inferior dies.  */
/* Clean up after the inferior dies.  */
 
 
static void
static void
linuxthreads_mourn_inferior ()
linuxthreads_mourn_inferior ()
{
{
  if (linuxthreads_max)
  if (linuxthreads_max)
    {
    {
      int off = 0;
      int off = 0;
      target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
      target_write_memory (linuxthreads_debug, (char *)&off, sizeof (off));
 
 
      linuxthreads_discard_global_state ();
      linuxthreads_discard_global_state ();
      init_thread_list();           /* Destroy thread info */
      init_thread_list();           /* Destroy thread info */
    }
    }
 
 
  child_ops.to_mourn_inferior ();
  child_ops.to_mourn_inferior ();
 
 
  unpush_target (&linuxthreads_ops);
  unpush_target (&linuxthreads_ops);
}
}
 
 
/* Kill the inferior process */
/* Kill the inferior process */
 
 
static void
static void
linuxthreads_kill ()
linuxthreads_kill ()
{
{
  int rpid;
  int rpid;
  int status;
  int status;
 
 
  if (inferior_pid == 0)
  if (inferior_pid == 0)
    return;
    return;
 
 
  if (linuxthreads_max && linuxthreads_manager_pid != 0)
  if (linuxthreads_max && linuxthreads_manager_pid != 0)
    {
    {
      /* Remove all threads status.  */
      /* Remove all threads status.  */
      inferior_pid = linuxthreads_manager_pid;
      inferior_pid = linuxthreads_manager_pid;
      iterate_active_threads (kill_thread, 1);
      iterate_active_threads (kill_thread, 1);
    }
    }
 
 
  kill_thread (inferior_pid);
  kill_thread (inferior_pid);
 
 
#if 0
#if 0
  /* doing_quit_force solves a real problem, but I think a properly
  /* doing_quit_force solves a real problem, but I think a properly
     placed call to catch_errors would do the trick much more cleanly.  */
     placed call to catch_errors would do the trick much more cleanly.  */
  if (doing_quit_force >= 0)
  if (doing_quit_force >= 0)
    {
    {
      if (linuxthreads_max && linuxthreads_manager_pid != 0)
      if (linuxthreads_max && linuxthreads_manager_pid != 0)
        {
        {
          /* Wait for thread to complete */
          /* Wait for thread to complete */
          while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
          while ((rpid = waitpid (-1, &status, __WCLONE)) > 0)
            if (!WIFEXITED(status))
            if (!WIFEXITED(status))
              kill_thread (rpid);
              kill_thread (rpid);
 
 
          while ((rpid = waitpid (-1, &status, 0)) > 0)
          while ((rpid = waitpid (-1, &status, 0)) > 0)
            if (!WIFEXITED(status))
            if (!WIFEXITED(status))
              kill_thread (rpid);
              kill_thread (rpid);
        }
        }
      else
      else
        while ((rpid = waitpid (inferior_pid, &status, 0)) > 0)
        while ((rpid = waitpid (inferior_pid, &status, 0)) > 0)
          if (!WIFEXITED(status))
          if (!WIFEXITED(status))
            ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
            ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
    }
    }
#endif
#endif
 
 
  /* Wait for all threads. */
  /* Wait for all threads. */
  do
  do
    {
    {
      rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
      rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
    }
    }
  while (rpid > 0 || errno == EINTR);
  while (rpid > 0 || errno == EINTR);
  /* FIXME: should no longer need to handle EINTR here. */
  /* FIXME: should no longer need to handle EINTR here. */
 
 
  do
  do
    {
    {
      rpid = waitpid (-1, &status, WNOHANG);
      rpid = waitpid (-1, &status, WNOHANG);
    }
    }
  while (rpid > 0 || errno == EINTR);
  while (rpid > 0 || errno == EINTR);
  /* FIXME: should no longer need to handle EINTR here. */
  /* FIXME: should no longer need to handle EINTR here. */
 
 
  linuxthreads_mourn_inferior ();
  linuxthreads_mourn_inferior ();
}
}
 
 
/* Insert a breakpoint */
/* Insert a breakpoint */
 
 
static int
static int
linuxthreads_insert_breakpoint (addr, contents_cache)
linuxthreads_insert_breakpoint (addr, contents_cache)
    CORE_ADDR addr;
    CORE_ADDR addr;
    char *contents_cache;
    char *contents_cache;
{
{
  if (linuxthreads_max && linuxthreads_manager_pid != 0)
  if (linuxthreads_max && linuxthreads_manager_pid != 0)
    {
    {
      linuxthreads_breakpoint_addr = addr;
      linuxthreads_breakpoint_addr = addr;
      iterate_active_threads (insert_breakpoint, 1);
      iterate_active_threads (insert_breakpoint, 1);
      insert_breakpoint (linuxthreads_manager_pid);
      insert_breakpoint (linuxthreads_manager_pid);
    }
    }
 
 
  return child_ops.to_insert_breakpoint (addr, contents_cache);
  return child_ops.to_insert_breakpoint (addr, contents_cache);
}
}
 
 
/* Remove a breakpoint */
/* Remove a breakpoint */
 
 
static int
static int
linuxthreads_remove_breakpoint (addr, contents_cache)
linuxthreads_remove_breakpoint (addr, contents_cache)
    CORE_ADDR addr;
    CORE_ADDR addr;
    char *contents_cache;
    char *contents_cache;
{
{
  if (linuxthreads_max && linuxthreads_manager_pid != 0)
  if (linuxthreads_max && linuxthreads_manager_pid != 0)
    {
    {
      linuxthreads_breakpoint_addr = addr;
      linuxthreads_breakpoint_addr = addr;
      iterate_active_threads (remove_breakpoint, 1);
      iterate_active_threads (remove_breakpoint, 1);
      remove_breakpoint (linuxthreads_manager_pid);
      remove_breakpoint (linuxthreads_manager_pid);
    }
    }
 
 
  return child_ops.to_remove_breakpoint (addr, contents_cache);
  return child_ops.to_remove_breakpoint (addr, contents_cache);
}
}
 
 
/* Mark our target-struct as eligible for stray "run" and "attach" commands.  */
/* Mark our target-struct as eligible for stray "run" and "attach" commands.  */
 
 
static int
static int
linuxthreads_can_run ()
linuxthreads_can_run ()
{
{
  return child_suppress_run;
  return child_suppress_run;
}
}
 
 


static void
static void
init_linuxthreads_ops ()
init_linuxthreads_ops ()
{
{
  linuxthreads_ops.to_shortname = "linuxthreads";
  linuxthreads_ops.to_shortname = "linuxthreads";
  linuxthreads_ops.to_longname  = "LINUX threads and pthread.";
  linuxthreads_ops.to_longname  = "LINUX threads and pthread.";
  linuxthreads_ops.to_doc       = "LINUX threads and pthread support.";
  linuxthreads_ops.to_doc       = "LINUX threads and pthread support.";
  linuxthreads_ops.to_attach    = linuxthreads_attach;
  linuxthreads_ops.to_attach    = linuxthreads_attach;
  linuxthreads_ops.to_detach    = linuxthreads_detach;
  linuxthreads_ops.to_detach    = linuxthreads_detach;
  linuxthreads_ops.to_resume    = linuxthreads_resume;
  linuxthreads_ops.to_resume    = linuxthreads_resume;
  linuxthreads_ops.to_wait      = linuxthreads_wait;
  linuxthreads_ops.to_wait      = linuxthreads_wait;
  linuxthreads_ops.to_kill      = linuxthreads_kill;
  linuxthreads_ops.to_kill      = linuxthreads_kill;
  linuxthreads_ops.to_can_run   = linuxthreads_can_run;
  linuxthreads_ops.to_can_run   = linuxthreads_can_run;
  linuxthreads_ops.to_stratum   = thread_stratum;
  linuxthreads_ops.to_stratum   = thread_stratum;
  linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
  linuxthreads_ops.to_insert_breakpoint = linuxthreads_insert_breakpoint;
  linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
  linuxthreads_ops.to_remove_breakpoint = linuxthreads_remove_breakpoint;
  linuxthreads_ops.to_create_inferior   = linuxthreads_create_inferior;
  linuxthreads_ops.to_create_inferior   = linuxthreads_create_inferior;
  linuxthreads_ops.to_mourn_inferior    = linuxthreads_mourn_inferior;
  linuxthreads_ops.to_mourn_inferior    = linuxthreads_mourn_inferior;
  linuxthreads_ops.to_thread_alive      = linuxthreads_thread_alive;
  linuxthreads_ops.to_thread_alive      = linuxthreads_thread_alive;
  linuxthreads_ops.to_pid_to_str        = linuxthreads_pid_to_str;
  linuxthreads_ops.to_pid_to_str        = linuxthreads_pid_to_str;
  linuxthreads_ops.to_magic             = OPS_MAGIC;
  linuxthreads_ops.to_magic             = OPS_MAGIC;
}
}
 
 
void
void
_initialize_linuxthreads ()
_initialize_linuxthreads ()
{
{
  struct sigaction sact;
  struct sigaction sact;
  sigset_t linuxthreads_wait_mask;        /* sigset with SIGCHLD */
  sigset_t linuxthreads_wait_mask;        /* sigset with SIGCHLD */
 
 
  init_linuxthreads_ops ();
  init_linuxthreads_ops ();
  add_target (&linuxthreads_ops);
  add_target (&linuxthreads_ops);
  child_suppress_run = 1;
  child_suppress_run = 1;
 
 
  /* Hook onto the "new_objfile" event.
  /* Hook onto the "new_objfile" event.
   * If someone else is already hooked onto the event,
   * If someone else is already hooked onto the event,
   * then make sure he will be called after we are.
   * then make sure he will be called after we are.
   */
   */
  target_new_objfile_chain = target_new_objfile_hook;
  target_new_objfile_chain = target_new_objfile_hook;
  target_new_objfile_hook  = linuxthreads_new_objfile;
  target_new_objfile_hook  = linuxthreads_new_objfile;
 
 
  /* Attach SIGCHLD handler */
  /* Attach SIGCHLD handler */
  sact.sa_handler = sigchld_handler;
  sact.sa_handler = sigchld_handler;
  sigemptyset (&sact.sa_mask);
  sigemptyset (&sact.sa_mask);
  sact.sa_flags = 0;
  sact.sa_flags = 0;
  sigaction (SIGCHLD, &sact, NULL);
  sigaction (SIGCHLD, &sact, NULL);
 
 
  /* initialize SIGCHLD mask */
  /* initialize SIGCHLD mask */
  sigemptyset (&linuxthreads_wait_mask);
  sigemptyset (&linuxthreads_wait_mask);
  sigaddset (&linuxthreads_wait_mask, SIGCHLD);
  sigaddset (&linuxthreads_wait_mask, SIGCHLD);
 
 
  /* Use SIG_BLOCK to block receipt of SIGCHLD.
  /* Use SIG_BLOCK to block receipt of SIGCHLD.
     The block_mask will allow us to wait for this signal explicitly.  */
     The block_mask will allow us to wait for this signal explicitly.  */
  sigprocmask(SIG_BLOCK,
  sigprocmask(SIG_BLOCK,
              &linuxthreads_wait_mask,
              &linuxthreads_wait_mask,
              &linuxthreads_block_mask);
              &linuxthreads_block_mask);
  /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
  /* Make sure that linuxthreads_block_mask is not blocking SIGCHLD */
  sigdelset (&linuxthreads_block_mask, SIGCHLD);
  sigdelset (&linuxthreads_block_mask, SIGCHLD);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.