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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [uw-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 UnixWare user-mode threads for
/* Low level interface for debugging UnixWare user-mode threads for
   GDB, the GNU debugger.
   GDB, the GNU debugger.
 
 
   Copyright 1999, 2000 Free Software Foundation, Inc.
   Copyright 1999, 2000 Free Software Foundation, Inc.
   Written by Nick Duffek <nsd@cygnus.com>.
   Written by Nick Duffek <nsd@cygnus.com>.
 
 
   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,
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */
   Boston, MA 02111-1307, USA.  */
 
 
 
 
/* Like many systems, UnixWare implements two classes of threads:
/* Like many systems, UnixWare implements two classes of threads:
   kernel-mode threads, which are scheduled by the kernel; and
   kernel-mode threads, which are scheduled by the kernel; and
   user-mode threads, which are scheduled by a library.  UnixWare
   user-mode threads, which are scheduled by a library.  UnixWare
   calls these two classes lightweight processes (LWPs) and threads,
   calls these two classes lightweight processes (LWPs) and threads,
   respectively.
   respectively.
 
 
   This module deals with user-mode threads.  It calls procfs_ops
   This module deals with user-mode threads.  It calls procfs_ops
   functions to deal with LWPs and processes and core_ops functions to
   functions to deal with LWPs and processes and core_ops functions to
   deal with core files.
   deal with core files.
 
 
   As of this writing, the user-mode thread debugging interface is not
   As of this writing, the user-mode thread debugging interface is not
   documented beyond the comments in <thread.h>.  The following
   documented beyond the comments in <thread.h>.  The following
   description has been gleaned from experience and from information
   description has been gleaned from experience and from information
   provided by SCO.
   provided by SCO.
 
 
   libthread.so, against which all UnixWare user-mode thread programs
   libthread.so, against which all UnixWare user-mode thread programs
   link, provides a global thread_debug structure named _thr_debug.
   link, provides a global thread_debug structure named _thr_debug.
   It has three fields:
   It has three fields:
 
 
     (1) thr_map is a pointer to a pointer to an element of a
     (1) thr_map is a pointer to a pointer to an element of a
         thread_map ring.  A thread_map contains a single thread's id
         thread_map ring.  A thread_map contains a single thread's id
         number, state, LWP pointer, recent register state, and other
         number, state, LWP pointer, recent register state, and other
         useful information.
         useful information.
 
 
     (2) thr_brk is a pointer to a stub function that libthread.so
     (2) thr_brk is a pointer to a stub function that libthread.so
         calls when it changes a thread's state, e.g. by creating it,
         calls when it changes a thread's state, e.g. by creating it,
         switching it to an LWP, or causing it to exit.
         switching it to an LWP, or causing it to exit.
 
 
     (3) thr_debug_on controls whether libthread.so calls thr_brk().
     (3) thr_debug_on controls whether libthread.so calls thr_brk().
 
 
   Debuggers are able to track thread activity by setting a private
   Debuggers are able to track thread activity by setting a private
   breakpoint on thr_brk() and setting thr_debug_on to 1.
   breakpoint on thr_brk() and setting thr_debug_on to 1.
 
 
   thr_brk() receives two arguments:
   thr_brk() receives two arguments:
 
 
     (1) a pointer to a thread_map describing the thread being
     (1) a pointer to a thread_map describing the thread being
         changed; and
         changed; and
 
 
     (2) an enum thread_change specifying one of the following
     (2) an enum thread_change specifying one of the following
         changes:
         changes:
 
 
         invalid                 unknown
         invalid                 unknown
         thread_create           thread has just been created
         thread_create           thread has just been created
         thread_exit             thread has just exited
         thread_exit             thread has just exited
         switch_begin            thread will be switched to an LWP
         switch_begin            thread will be switched to an LWP
         switch_complete         thread has been switched to an LWP
         switch_complete         thread has been switched to an LWP
         cancel_complete         thread wasn't switched to an LWP
         cancel_complete         thread wasn't switched to an LWP
         thread_suspend          thread has been thr_suspend()ed
         thread_suspend          thread has been thr_suspend()ed
         thread_suspend_pending  thread will be thr_suspend()ed
         thread_suspend_pending  thread will be thr_suspend()ed
         thread_continue         thread has been thr_continue()d
         thread_continue         thread has been thr_continue()d
 
 
   The thread_map argument to thr_brk() is NULL under the following
   The thread_map argument to thr_brk() is NULL under the following
   circumstances:
   circumstances:
 
 
     - The main thread is being acted upon.  The main thread always
     - The main thread is being acted upon.  The main thread always
       has id 1, so its thread_map is easy to find by scanning through
       has id 1, so its thread_map is easy to find by scanning through
       _thr_debug.thr_map.
       _thr_debug.thr_map.
 
 
     - A "switch_complete" change is occurring, which means that the
     - A "switch_complete" change is occurring, which means that the
       thread specified in the most recent "switch_begin" change has
       thread specified in the most recent "switch_begin" change has
       moved to an LWP.
       moved to an LWP.
 
 
     - A "cancel_complete" change is occurring, which means that the
     - A "cancel_complete" change is occurring, which means that the
       thread specified in the most recent "switch_begin" change has
       thread specified in the most recent "switch_begin" change has
       not moved to an LWP after all.
       not moved to an LWP after all.
 
 
     - A spurious "switch_begin" change is occurring after a
     - A spurious "switch_begin" change is occurring after a
       "thread_exit" change.
       "thread_exit" change.
 
 
   Between switch_begin and switch_complete or cancel_complete, the
   Between switch_begin and switch_complete or cancel_complete, the
   affected thread's LWP pointer is not reliable.  It is possible that
   affected thread's LWP pointer is not reliable.  It is possible that
   other parts of the thread's thread_map are also unreliable during
   other parts of the thread's thread_map are also unreliable during
   that time. */
   that time. */
 
 
 
 
#include "defs.h"
#include "defs.h"
#include "gdbthread.h"
#include "gdbthread.h"
#include "target.h"
#include "target.h"
#include "inferior.h"
#include "inferior.h"
#include <fcntl.h>
#include <fcntl.h>
 
 
/* <thread.h> includes <sys/priocntl.h>, which requires boolean_t from
/* <thread.h> includes <sys/priocntl.h>, which requires boolean_t from
   <sys/types.h>, which doesn't typedef boolean_t with gcc. */
   <sys/types.h>, which doesn't typedef boolean_t with gcc. */
 
 
#define boolean_t int
#define boolean_t int
#include <thread.h>
#include <thread.h>
#undef boolean_t
#undef boolean_t
 
 
#include <synch.h>              /* for UnixWare 2.x */
#include <synch.h>              /* for UnixWare 2.x */
 
 
 
 
/* Whether to emit debugging output. */
/* Whether to emit debugging output. */
 
 
#define DEBUG 0
#define DEBUG 0
 
 
/* Default debugging output file, overridden by envvar UWTHR_DEBUG. */
/* Default debugging output file, overridden by envvar UWTHR_DEBUG. */
 
 
#define DEBUG_FILE "/dev/tty"
#define DEBUG_FILE "/dev/tty"
 
 
/* #if DEBUG, write string S to the debugging output channel. */
/* #if DEBUG, write string S to the debugging output channel. */
 
 
#if !DEBUG
#if !DEBUG
# define DBG(fmt_and_args)
# define DBG(fmt_and_args)
# define DBG2(fmt_and_args)
# define DBG2(fmt_and_args)
#else
#else
# define DBG(fmt_and_args) dbg fmt_and_args
# define DBG(fmt_and_args) dbg fmt_and_args
# define DBG2(fmt_and_args)
# define DBG2(fmt_and_args)
#endif
#endif
 
 
/* Back end to CALL_BASE() and TRY_BASE(): evaluate CALL, then convert
/* Back end to CALL_BASE() and TRY_BASE(): evaluate CALL, then convert
   inferior_pid to a composite thread/process id. */
   inferior_pid to a composite thread/process id. */
 
 
#define CALL_BASE_1(call)               \
#define CALL_BASE_1(call)               \
do {                                    \
do {                                    \
  DBG2(("CALL_BASE(" #call ")"));       \
  DBG2(("CALL_BASE(" #call ")"));       \
  call;                                 \
  call;                                 \
  do_cleanups (infpid_cleanup);         \
  do_cleanups (infpid_cleanup);         \
} while (0)
} while (0)
 
 
/* If inferior_pid can be converted to a composite lwp/process id, do so,
/* If inferior_pid can be converted to a composite lwp/process id, do so,
   evaluate base_ops function CALL, and then convert inferior_pid back to a
   evaluate base_ops function CALL, and then convert inferior_pid back to a
   composite thread/process id.
   composite thread/process id.
 
 
   Otherwise, issue an error message and return nonlocally. */
   Otherwise, issue an error message and return nonlocally. */
 
 
#define CALL_BASE(call)                 \
#define CALL_BASE(call)                 \
do {                                    \
do {                                    \
  if (!lwp_infpid ())                   \
  if (!lwp_infpid ())                   \
    error ("uw-thread: no lwp");        \
    error ("uw-thread: no lwp");        \
  CALL_BASE_1 (call);                   \
  CALL_BASE_1 (call);                   \
} while (0)
} while (0)
 
 
/* Like CALL_BASE(), but instead of returning nonlocally on error, set
/* Like CALL_BASE(), but instead of returning nonlocally on error, set
   *CALLED to whether the inferior_pid conversion was successful. */
   *CALLED to whether the inferior_pid conversion was successful. */
 
 
#define TRY_BASE(call, called)          \
#define TRY_BASE(call, called)          \
do {                                    \
do {                                    \
  if ((*(called) = lwp_infpid ()))      \
  if ((*(called) = lwp_infpid ()))      \
    CALL_BASE_1 (call);                 \
    CALL_BASE_1 (call);                 \
} while (0)
} while (0)
 
 
/* Information passed by thread_iter() to its callback parameter. */
/* Information passed by thread_iter() to its callback parameter. */
 
 
typedef struct {
typedef struct {
  struct thread_map map;
  struct thread_map map;
  __lwp_desc_t lwp;
  __lwp_desc_t lwp;
  CORE_ADDR mapp;
  CORE_ADDR mapp;
} iter_t;
} iter_t;
 
 
/* Private thread data for the thread_info struct. */
/* Private thread data for the thread_info struct. */
 
 
struct private_thread_info {
struct private_thread_info {
  int stable;           /* 0 if libthread.so is modifying thread map */
  int stable;           /* 0 if libthread.so is modifying thread map */
  int thrid;            /* thread id assigned by libthread.so */
  int thrid;            /* thread id assigned by libthread.so */
  int lwpid;            /* thread's lwp if .stable, 0 means no lwp */
  int lwpid;            /* thread's lwp if .stable, 0 means no lwp */
  CORE_ADDR mapp;       /* address of thread's map structure */
  CORE_ADDR mapp;       /* address of thread's map structure */
};
};
 
 
 
 
/* procfs.c's target-specific operations. */
/* procfs.c's target-specific operations. */
extern struct target_ops procfs_ops;
extern struct target_ops procfs_ops;
 
 
/* Flag to prevent procfs.c from starting inferior processes. */
/* Flag to prevent procfs.c from starting inferior processes. */
extern int procfs_suppress_run;
extern int procfs_suppress_run;
 
 
/* This module's target-specific operations. */
/* This module's target-specific operations. */
static struct target_ops uw_thread_ops;
static struct target_ops uw_thread_ops;
 
 
/* Copy of the target over which uw_thread_ops is pushed.  This is
/* Copy of the target over which uw_thread_ops is pushed.  This is
   more convenient than a pointer to procfs_ops or core_ops, because
   more convenient than a pointer to procfs_ops or core_ops, because
   they lack current_target's default callbacks. */
   they lack current_target's default callbacks. */
static struct target_ops base_ops;
static struct target_ops base_ops;
 
 
/* Saved pointer to previous owner of target_new_objfile_hook. */
/* Saved pointer to previous owner of target_new_objfile_hook. */
static void (*target_new_objfile_chain)(struct objfile *);
static void (*target_new_objfile_chain)(struct objfile *);
 
 
/* Whether we are debugging a user-space thread program.  This isn't
/* Whether we are debugging a user-space thread program.  This isn't
   set until after libthread.so is loaded by the program being
   set until after libthread.so is loaded by the program being
   debugged.
   debugged.
 
 
   Except for module one-time intialization and where otherwise
   Except for module one-time intialization and where otherwise
   documented, no functions in this module get called when
   documented, no functions in this module get called when
   !uw_thread_active. */
   !uw_thread_active. */
static int uw_thread_active;
static int uw_thread_active;
 
 
/* For efficiency, cache the addresses of libthread.so's _thr_debug
/* For efficiency, cache the addresses of libthread.so's _thr_debug
   structure, its thr_brk stub function, and the main thread's map. */
   structure, its thr_brk stub function, and the main thread's map. */
static CORE_ADDR thr_debug_addr;
static CORE_ADDR thr_debug_addr;
static CORE_ADDR thr_brk_addr;
static CORE_ADDR thr_brk_addr;
static CORE_ADDR thr_map_main;
static CORE_ADDR thr_map_main;
 
 
/* Remember the thread most recently marked as switching.  Necessary because
/* Remember the thread most recently marked as switching.  Necessary because
   libthread.so passes null map when calling stub with tc_*_complete. */
   libthread.so passes null map when calling stub with tc_*_complete. */
static struct thread_info *switchto_thread;
static struct thread_info *switchto_thread;
 
 
/* Cleanup chain for safely restoring inferior_pid after CALL_BASE. */
/* Cleanup chain for safely restoring inferior_pid after CALL_BASE. */
static struct cleanup *infpid_cleanup;
static struct cleanup *infpid_cleanup;
 
 
 
 
#if DEBUG
#if DEBUG
/* Helper function for DBG() macro: if printf-style FMT is non-null, format it
/* Helper function for DBG() macro: if printf-style FMT is non-null, format it
   with args and display the result on the debugging output channel. */
   with args and display the result on the debugging output channel. */
 
 
static void
static void
dbg (char *fmt, ...)
dbg (char *fmt, ...)
{
{
  static int fd = -1, len;
  static int fd = -1, len;
  va_list args;
  va_list args;
  char buf[1024];
  char buf[1024];
  char *path;
  char *path;
 
 
  if (!fmt)
  if (!fmt)
    return;
    return;
 
 
  if (fd < 0)
  if (fd < 0)
    {
    {
      path = getenv ("UWTHR_DEBUG");
      path = getenv ("UWTHR_DEBUG");
      if (!path)
      if (!path)
        path = DEBUG_FILE;
        path = DEBUG_FILE;
      if ((fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0)
      if ((fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0)
        error ("can't open %s\n", path);
        error ("can't open %s\n", path);
    }
    }
 
 
  va_start (args, fmt);
  va_start (args, fmt);
  vsprintf (buf, fmt, args);
  vsprintf (buf, fmt, args);
  va_end (args);
  va_end (args);
 
 
  len = strlen (buf);
  len = strlen (buf);
  buf[len] = '\n';
  buf[len] = '\n';
  (void)write (fd, buf, len + 1);
  (void)write (fd, buf, len + 1);
}
}
 
 
#if 0
#if 0
/* Return a string representing composite PID's components. */
/* Return a string representing composite PID's components. */
 
 
static char *
static char *
dbgpid (int pid)
dbgpid (int pid)
{
{
  static char *buf, buf1[80], buf2[80];
  static char *buf, buf1[80], buf2[80];
  if (!buf || buf == buf2)
  if (!buf || buf == buf2)
    buf = buf1;
    buf = buf1;
  else
  else
    buf = buf2;
    buf = buf2;
 
 
  if (pid <= 0)
  if (pid <= 0)
    sprintf (buf, "%d", pid);
    sprintf (buf, "%d", pid);
  else
  else
    sprintf (buf, "%s %d/%d", ISTID (pid) ? "thr" : "lwp",
    sprintf (buf, "%s %d/%d", ISTID (pid) ? "thr" : "lwp",
             TIDGET (pid), PIDGET (pid));
             TIDGET (pid), PIDGET (pid));
 
 
  return buf;
  return buf;
}
}
 
 
/* Return a string representing thread state CHANGE. */
/* Return a string representing thread state CHANGE. */
 
 
static char *
static char *
dbgchange (enum thread_change change)
dbgchange (enum thread_change change)
{
{
  switch (change) {
  switch (change) {
  case tc_invalid:                      return "invalid";
  case tc_invalid:                      return "invalid";
  case tc_thread_create:                return "thread_create";
  case tc_thread_create:                return "thread_create";
  case tc_thread_exit:                  return "thread_exit";
  case tc_thread_exit:                  return "thread_exit";
  case tc_switch_begin:                 return "switch_begin";
  case tc_switch_begin:                 return "switch_begin";
  case tc_switch_complete:              return "switch_complete";
  case tc_switch_complete:              return "switch_complete";
  case tc_cancel_complete:              return "cancel_complete";
  case tc_cancel_complete:              return "cancel_complete";
  case tc_thread_suspend:               return "thread_suspend";
  case tc_thread_suspend:               return "thread_suspend";
  case tc_thread_suspend_pending:       return "thread_suspend_pending";
  case tc_thread_suspend_pending:       return "thread_suspend_pending";
  case tc_thread_continue:              return "thread_continue";
  case tc_thread_continue:              return "thread_continue";
  default:                              return "unknown";
  default:                              return "unknown";
  }
  }
}
}
 
 
/* Return a string representing thread STATE. */
/* Return a string representing thread STATE. */
 
 
static char *
static char *
dbgstate (int state)
dbgstate (int state)
{
{
  switch (state) {
  switch (state) {
  case TS_ONPROC:       return "running";
  case TS_ONPROC:       return "running";
  case TS_SLEEP:        return "sleeping";
  case TS_SLEEP:        return "sleeping";
  case TS_RUNNABLE:     return "runnable";
  case TS_RUNNABLE:     return "runnable";
  case TS_ZOMBIE:       return "zombie";
  case TS_ZOMBIE:       return "zombie";
  case TS_SUSPENDED:    return "suspended";
  case TS_SUSPENDED:    return "suspended";
#ifdef TS_FORK
#ifdef TS_FORK
  case TS_FORK:         return "forking";
  case TS_FORK:         return "forking";
#endif
#endif
  default:              return "confused";
  default:              return "confused";
  }
  }
}
}
#endif  /* 0 */
#endif  /* 0 */
#endif  /* DEBUG */
#endif  /* DEBUG */
 
 
 
 
/* Read the contents of _thr_debug into *DEBUGP.  Return success. */
/* Read the contents of _thr_debug into *DEBUGP.  Return success. */
 
 
static int
static int
read_thr_debug (struct thread_debug *debugp)
read_thr_debug (struct thread_debug *debugp)
{
{
  return base_ops.to_xfer_memory (thr_debug_addr, (char *)debugp,
  return base_ops.to_xfer_memory (thr_debug_addr, (char *)debugp,
                                  sizeof (*debugp), 0, &base_ops);
                                  sizeof (*debugp), 0, &base_ops);
}
}
 
 
/* Read into MAP the contents of the thread map at inferior process address
/* Read into MAP the contents of the thread map at inferior process address
   MAPP.  Return success. */
   MAPP.  Return success. */
 
 
static int
static int
read_map (CORE_ADDR mapp, struct thread_map *map)
read_map (CORE_ADDR mapp, struct thread_map *map)
{
{
  return base_ops.to_xfer_memory ((CORE_ADDR)THR_MAP (mapp), (char *)map,
  return base_ops.to_xfer_memory ((CORE_ADDR)THR_MAP (mapp), (char *)map,
                                  sizeof (*map), 0, &base_ops);
                                  sizeof (*map), 0, &base_ops);
}
}
 
 
/* Read into LWP the contents of the lwp decriptor at inferior process address
/* Read into LWP the contents of the lwp decriptor at inferior process address
   LWPP.  Return success. */
   LWPP.  Return success. */
 
 
static int
static int
read_lwp (CORE_ADDR lwpp, __lwp_desc_t *lwp)
read_lwp (CORE_ADDR lwpp, __lwp_desc_t *lwp)
{
{
  return base_ops.to_xfer_memory (lwpp, (char *)lwp,
  return base_ops.to_xfer_memory (lwpp, (char *)lwp,
                                  sizeof (*lwp), 0, &base_ops);
                                  sizeof (*lwp), 0, &base_ops);
}
}
 
 
/* Iterate through all user threads, applying FUNC(<map>, <lwp>, DATA) until
/* Iterate through all user threads, applying FUNC(<map>, <lwp>, DATA) until
     (a) FUNC returns nonzero,
     (a) FUNC returns nonzero,
     (b) FUNC has been applied to all threads, or
     (b) FUNC has been applied to all threads, or
     (c) an error occurs,
     (c) an error occurs,
   where <map> is the thread's struct thread_map and <lwp> if non-null is the
   where <map> is the thread's struct thread_map and <lwp> if non-null is the
   thread's current __lwp_desc_t.
   thread's current __lwp_desc_t.
 
 
   If a call to FUNC returns nonzero, return that value; otherwise, return 0. */
   If a call to FUNC returns nonzero, return that value; otherwise, return 0. */
 
 
static int
static int
thread_iter (int (*func)(iter_t *, void *), void *data)
thread_iter (int (*func)(iter_t *, void *), void *data)
{
{
  struct thread_debug debug;
  struct thread_debug debug;
  CORE_ADDR first, mapp;
  CORE_ADDR first, mapp;
  iter_t iter;
  iter_t iter;
  int ret;
  int ret;
 
 
  if (!read_thr_debug (&debug))
  if (!read_thr_debug (&debug))
    return 0;
    return 0;
  if (!base_ops.to_xfer_memory ((CORE_ADDR)debug.thr_map, (char *)&mapp,
  if (!base_ops.to_xfer_memory ((CORE_ADDR)debug.thr_map, (char *)&mapp,
                                sizeof (mapp), 0, &base_ops))
                                sizeof (mapp), 0, &base_ops))
    return 0;
    return 0;
  if (!mapp)
  if (!mapp)
    return 0;
    return 0;
 
 
  for (first = mapp;;)
  for (first = mapp;;)
    {
    {
      if (!read_map (mapp, &iter.map))
      if (!read_map (mapp, &iter.map))
        return 0;
        return 0;
 
 
      if (iter.map.thr_lwpp)
      if (iter.map.thr_lwpp)
        if (!read_lwp ((CORE_ADDR)iter.map.thr_lwpp, &iter.lwp))
        if (!read_lwp ((CORE_ADDR)iter.map.thr_lwpp, &iter.lwp))
          return 0;
          return 0;
 
 
      iter.mapp = mapp;
      iter.mapp = mapp;
      if ((ret = func (&iter, data)))
      if ((ret = func (&iter, data)))
        return ret;
        return ret;
 
 
      mapp = (CORE_ADDR)iter.map.thr_next;
      mapp = (CORE_ADDR)iter.map.thr_next;
      if (mapp == first)
      if (mapp == first)
        return 0;
        return 0;
    }
    }
}
}
 
 
/* Deactivate user-mode thread support. */
/* Deactivate user-mode thread support. */
 
 
static void
static void
deactivate_uw_thread (void)
deactivate_uw_thread (void)
{
{
  remove_thread_event_breakpoints ();
  remove_thread_event_breakpoints ();
  uw_thread_active = 0;
  uw_thread_active = 0;
  unpush_target (&uw_thread_ops);
  unpush_target (&uw_thread_ops);
}
}
 
 
/* Return the composite lwp/process id corresponding to composite
/* Return the composite lwp/process id corresponding to composite
   id PID.  If PID is a thread with no lwp, return 0. */
   id PID.  If PID is a thread with no lwp, return 0. */
 
 
static int
static int
thr_to_lwp (int pid)
thr_to_lwp (int pid)
{
{
  struct thread_info *info;
  struct thread_info *info;
  int lid;
  int lid;
 
 
  if (!ISTID (pid))
  if (!ISTID (pid))
    lid = pid;
    lid = pid;
  else if (!(info = find_thread_pid (pid)))
  else if (!(info = find_thread_pid (pid)))
    lid = 0;
    lid = 0;
  else if (!info->private->lwpid)
  else if (!info->private->lwpid)
    lid = 0;
    lid = 0;
  else
  else
    lid = MKLID (pid, info->private->lwpid);
    lid = MKLID (pid, info->private->lwpid);
 
 
  DBG2(("  thr_to_lwp(%s) = %s", dbgpid (pid), dbgpid (lid)));
  DBG2(("  thr_to_lwp(%s) = %s", dbgpid (pid), dbgpid (lid)));
  return lid;
  return lid;
}
}
 
 
/* find_thread_lwp() callback: return whether TP describes a thread
/* find_thread_lwp() callback: return whether TP describes a thread
   associated with lwp id DATA. */
   associated with lwp id DATA. */
 
 
static int
static int
find_thread_lwp_callback (struct thread_info *tp, void *data)
find_thread_lwp_callback (struct thread_info *tp, void *data)
{
{
  int lwpid = (int)data;
  int lwpid = (int)data;
 
 
  if (!ISTID (tp->pid))
  if (!ISTID (tp->pid))
    return 0;
    return 0;
  if (!tp->private->stable)
  if (!tp->private->stable)
    return 0;
    return 0;
  if (lwpid != tp->private->lwpid)
  if (lwpid != tp->private->lwpid)
    return 0;
    return 0;
 
 
  /* match */
  /* match */
  return 1;
  return 1;
}
}
 
 
/* If a thread is associated with lwp id LWPID, return the corresponding
/* If a thread is associated with lwp id LWPID, return the corresponding
   member of the global thread list; otherwise, return null. */
   member of the global thread list; otherwise, return null. */
 
 
static struct thread_info *
static struct thread_info *
find_thread_lwp (int lwpid)
find_thread_lwp (int lwpid)
{
{
  return iterate_over_threads (find_thread_lwp_callback, (void *)lwpid);
  return iterate_over_threads (find_thread_lwp_callback, (void *)lwpid);
}
}
 
 
/* Return the composite thread/process id corresponding to composite
/* Return the composite thread/process id corresponding to composite
   id PID.  If PID is an lwp with no thread, return PID. */
   id PID.  If PID is an lwp with no thread, return PID. */
 
 
static int
static int
lwp_to_thr (int pid)
lwp_to_thr (int pid)
{
{
  struct thread_info *info;
  struct thread_info *info;
  int tid = pid, lwpid;
  int tid = pid, lwpid;
 
 
  if (ISTID (pid))
  if (ISTID (pid))
    goto done;
    goto done;
  if (!(lwpid = LIDGET (pid)))
  if (!(lwpid = LIDGET (pid)))
    goto done;
    goto done;
  if (!(info = find_thread_lwp (lwpid)))
  if (!(info = find_thread_lwp (lwpid)))
    goto done;
    goto done;
  tid = MKTID (pid, info->private->thrid);
  tid = MKTID (pid, info->private->thrid);
 
 
 done:
 done:
  DBG2((ISTID (tid) ? NULL : "lwp_to_thr: no thr for %s", dbgpid (pid)));
  DBG2((ISTID (tid) ? NULL : "lwp_to_thr: no thr for %s", dbgpid (pid)));
  return tid;
  return tid;
}
}
 
 
/* do_cleanups() callback: convert inferior_pid to a composite
/* do_cleanups() callback: convert inferior_pid to a composite
   thread/process id after having made a procfs call. */
   thread/process id after having made a procfs call. */
 
 
static void
static void
thr_infpid (void *unused)
thr_infpid (void *unused)
{
{
  int pid = lwp_to_thr (inferior_pid);
  int pid = lwp_to_thr (inferior_pid);
  DBG2((" inferior_pid from procfs: %s => %s",
  DBG2((" inferior_pid from procfs: %s => %s",
        dbgpid (inferior_pid), dbgpid (pid)));
        dbgpid (inferior_pid), dbgpid (pid)));
  inferior_pid = pid;
  inferior_pid = pid;
}
}
 
 
/* If possible, convert inferior_pid to a composite lwp/process id in
/* If possible, convert inferior_pid to a composite lwp/process id in
   preparation for making a procfs call.  Return success. */
   preparation for making a procfs call.  Return success. */
 
 
static int
static int
lwp_infpid (void)
lwp_infpid (void)
{
{
  int pid = thr_to_lwp (inferior_pid);
  int pid = thr_to_lwp (inferior_pid);
  DBG2((" inferior_pid to procfs: %s => %s",
  DBG2((" inferior_pid to procfs: %s => %s",
        dbgpid (inferior_pid), dbgpid (pid)));
        dbgpid (inferior_pid), dbgpid (pid)));
 
 
  if (!pid)
  if (!pid)
    return 0;
    return 0;
 
 
  inferior_pid = pid;
  inferior_pid = pid;
  infpid_cleanup = make_cleanup (thr_infpid, NULL);
  infpid_cleanup = make_cleanup (thr_infpid, NULL);
  return 1;
  return 1;
}
}
 
 
/* Add to the global thread list a new user-mode thread with system id THRID,
/* Add to the global thread list a new user-mode thread with system id THRID,
   lwp id LWPID, map address MAPP, and composite thread/process PID. */
   lwp id LWPID, map address MAPP, and composite thread/process PID. */
 
 
static void
static void
add_thread_uw (int thrid, int lwpid, CORE_ADDR mapp, int pid)
add_thread_uw (int thrid, int lwpid, CORE_ADDR mapp, int pid)
{
{
  struct thread_info *newthread;
  struct thread_info *newthread;
 
 
  if ((newthread = add_thread (pid)) == NULL)
  if ((newthread = add_thread (pid)) == NULL)
    error ("failed to create new thread structure");
    error ("failed to create new thread structure");
 
 
  newthread->private = xmalloc (sizeof (struct private_thread_info));
  newthread->private = xmalloc (sizeof (struct private_thread_info));
  newthread->private->stable = 1;
  newthread->private->stable = 1;
  newthread->private->thrid = thrid;
  newthread->private->thrid = thrid;
  newthread->private->lwpid = lwpid;
  newthread->private->lwpid = lwpid;
  newthread->private->mapp = mapp;
  newthread->private->mapp = mapp;
 
 
  if (target_has_execution)
  if (target_has_execution)
    printf_unfiltered ("[New %s]\n", target_pid_to_str (pid));
    printf_unfiltered ("[New %s]\n", target_pid_to_str (pid));
}
}
 
 
/* notice_threads() and find_main() callback: if the thread list doesn't
/* notice_threads() and find_main() callback: if the thread list doesn't
   already contain the thread described by ITER, add it if it's the main
   already contain the thread described by ITER, add it if it's the main
   thread or if !DATA. */
   thread or if !DATA. */
 
 
static int
static int
notice_thread (iter_t *iter, void *data)
notice_thread (iter_t *iter, void *data)
{
{
  int thrid = iter->map.thr_tid;
  int thrid = iter->map.thr_tid;
  int lwpid = !iter->map.thr_lwpp ? 0 : iter->lwp.lwp_id;
  int lwpid = !iter->map.thr_lwpp ? 0 : iter->lwp.lwp_id;
  int pid = MKTID (inferior_pid, thrid);
  int pid = MKTID (inferior_pid, thrid);
 
 
  if (!find_thread_pid (pid) && (!data || thrid == 1))
  if (!find_thread_pid (pid) && (!data || thrid == 1))
    add_thread_uw (thrid, lwpid, iter->mapp, pid);
    add_thread_uw (thrid, lwpid, iter->mapp, pid);
 
 
  return 0;
  return 0;
}
}
 
 
/* Add to the thread list any threads it doesn't already contain. */
/* Add to the thread list any threads it doesn't already contain. */
 
 
static void
static void
notice_threads (void)
notice_threads (void)
{
{
  thread_iter (notice_thread, NULL);
  thread_iter (notice_thread, NULL);
}
}
 
 
/* Return the address of the main thread's map.  On error, return 0. */
/* Return the address of the main thread's map.  On error, return 0. */
 
 
static CORE_ADDR
static CORE_ADDR
find_main (void)
find_main (void)
{
{
  if (!thr_map_main)
  if (!thr_map_main)
    {
    {
      struct thread_info *info;
      struct thread_info *info;
      thread_iter (notice_thread, (void *)1);
      thread_iter (notice_thread, (void *)1);
      if ((info = find_thread_pid (MKTID (inferior_pid, 1))))
      if ((info = find_thread_pid (MKTID (inferior_pid, 1))))
        thr_map_main = info->private->mapp;
        thr_map_main = info->private->mapp;
    }
    }
  return thr_map_main;
  return thr_map_main;
}
}
 
 
/* Attach to process specified by ARGS, then initialize for debugging it
/* Attach to process specified by ARGS, 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.
 
 
   This function only gets called with uw_thread_active == 0. */
   This function only gets called with uw_thread_active == 0. */
 
 
static void
static void
uw_thread_attach (char *args, int from_tty)
uw_thread_attach (char *args, int from_tty)
{
{
  procfs_ops.to_attach (args, from_tty);
  procfs_ops.to_attach (args, from_tty);
  if (uw_thread_active)
  if (uw_thread_active)
    thr_infpid (NULL);
    thr_infpid (NULL);
}
}
 
 
/* Detach from the process attached to by uw_thread_attach(). */
/* Detach from the process attached to by uw_thread_attach(). */
 
 
static void
static void
uw_thread_detach (char *args, int from_tty)
uw_thread_detach (char *args, int from_tty)
{
{
  deactivate_uw_thread ();
  deactivate_uw_thread ();
  base_ops.to_detach (args, from_tty);
  base_ops.to_detach (args, from_tty);
}
}
 
 
/* Tell the inferior process to continue running thread PID if >= 0
/* Tell the inferior process to continue running thread PID if >= 0
   and all threads otherwise. */
   and all threads otherwise. */
 
 
static void
static void
uw_thread_resume (int pid, int step, enum target_signal signo)
uw_thread_resume (int pid, int step, enum target_signal signo)
{
{
  if (pid > 0 && !(pid = thr_to_lwp (pid)))
  if (pid > 0 && !(pid = thr_to_lwp (pid)))
    pid = -1;
    pid = -1;
 
 
  CALL_BASE (base_ops.to_resume (pid, step, signo));
  CALL_BASE (base_ops.to_resume (pid, step, signo));
}
}
 
 
/* If the trap we just received from lwp PID was due to a breakpoint
/* If the trap we just received from lwp PID was due to a breakpoint
   on the libthread.so debugging stub, update this module's state
   on the libthread.so debugging stub, update this module's state
   accordingly. */
   accordingly. */
 
 
static void
static void
libthread_stub (int pid)
libthread_stub (int pid)
{
{
  CORE_ADDR sp, mapp, mapp_main;
  CORE_ADDR sp, mapp, mapp_main;
  enum thread_change change;
  enum thread_change change;
  struct thread_map map;
  struct thread_map map;
  __lwp_desc_t lwp;
  __lwp_desc_t lwp;
  int tid = 0, lwpid;
  int tid = 0, lwpid;
  struct thread_info *info;
  struct thread_info *info;
 
 
  /* Check for stub breakpoint. */
  /* Check for stub breakpoint. */
  if (read_pc_pid (pid) - DECR_PC_AFTER_BREAK != thr_brk_addr)
  if (read_pc_pid (pid) - DECR_PC_AFTER_BREAK != thr_brk_addr)
    return;
    return;
 
 
  /* Retrieve stub args. */
  /* Retrieve stub args. */
  sp = read_register_pid (SP_REGNUM, pid);
  sp = read_register_pid (SP_REGNUM, pid);
  if (!base_ops.to_xfer_memory (sp + SP_ARG0, (char *)&mapp,
  if (!base_ops.to_xfer_memory (sp + SP_ARG0, (char *)&mapp,
                                sizeof (mapp), 0, &base_ops))
                                sizeof (mapp), 0, &base_ops))
    goto err;
    goto err;
  if (!base_ops.to_xfer_memory (sp + SP_ARG0 + sizeof (mapp), (char *)&change,
  if (!base_ops.to_xfer_memory (sp + SP_ARG0 + sizeof (mapp), (char *)&change,
                                sizeof (change), 0, &base_ops))
                                sizeof (change), 0, &base_ops))
    goto err;
    goto err;
 
 
  /* create_inferior() may not have finished yet, so notice the main
  /* create_inferior() may not have finished yet, so notice the main
     thread to ensure that it's displayed first by add_thread(). */
     thread to ensure that it's displayed first by add_thread(). */
  mapp_main = find_main ();
  mapp_main = find_main ();
 
 
  /* Notice thread creation, deletion, or stability change. */
  /* Notice thread creation, deletion, or stability change. */
  switch (change) {
  switch (change) {
  case tc_switch_begin:
  case tc_switch_begin:
    if (!mapp)                          /* usually means main thread */
    if (!mapp)                          /* usually means main thread */
      mapp = mapp_main;
      mapp = mapp_main;
    /* fall through */
    /* fall through */
 
 
  case tc_thread_create:
  case tc_thread_create:
  case tc_thread_exit:
  case tc_thread_exit:
    if (!mapp)
    if (!mapp)
      break;
      break;
    if (!read_map (mapp, &map))
    if (!read_map (mapp, &map))
      goto err;
      goto err;
    tid = MKTID (pid, map.thr_tid);
    tid = MKTID (pid, map.thr_tid);
 
 
    switch (change) {
    switch (change) {
    case tc_thread_create:              /* new thread */
    case tc_thread_create:              /* new thread */
      if (!map.thr_lwpp)
      if (!map.thr_lwpp)
        lwpid = 0;
        lwpid = 0;
      else if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
      else if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
        goto err;
        goto err;
      else
      else
        lwpid = lwp.lwp_id;
        lwpid = lwp.lwp_id;
      add_thread_uw (map.thr_tid, lwpid, mapp, tid);
      add_thread_uw (map.thr_tid, lwpid, mapp, tid);
      break;
      break;
 
 
    case tc_thread_exit:                /* thread has exited */
    case tc_thread_exit:                /* thread has exited */
      printf_unfiltered ("[Exited %s]\n", target_pid_to_str (tid));
      printf_unfiltered ("[Exited %s]\n", target_pid_to_str (tid));
      delete_thread (tid);
      delete_thread (tid);
      if (tid == inferior_pid)
      if (tid == inferior_pid)
        inferior_pid = pid;
        inferior_pid = pid;
      break;
      break;
 
 
    case tc_switch_begin:               /* lwp is switching threads */
    case tc_switch_begin:               /* lwp is switching threads */
      if (switchto_thread)
      if (switchto_thread)
        goto err;
        goto err;
      if (!(switchto_thread = find_thread_pid (tid)))
      if (!(switchto_thread = find_thread_pid (tid)))
        goto err;
        goto err;
      switchto_thread->private->stable = 0;
      switchto_thread->private->stable = 0;
      break;
      break;
 
 
    default:
    default:
      break;
      break;
    }
    }
    break;
    break;
 
 
  case tc_switch_complete:              /* lwp has switched threads */
  case tc_switch_complete:              /* lwp has switched threads */
  case tc_cancel_complete:              /* lwp didn't switch threads */
  case tc_cancel_complete:              /* lwp didn't switch threads */
    if (!switchto_thread)
    if (!switchto_thread)
      goto err;
      goto err;
 
 
    if (change == tc_switch_complete)
    if (change == tc_switch_complete)
      {
      {
        /* If switchto_thread is the main thread, then (a) the corresponding
        /* If switchto_thread is the main thread, then (a) the corresponding
           tc_switch_begin probably received a null map argument and therefore
           tc_switch_begin probably received a null map argument and therefore
           (b) it may have been a spurious switch following a tc_thread_exit.
           (b) it may have been a spurious switch following a tc_thread_exit.
 
 
           Therefore, explicitly query the thread's lwp before caching it in
           Therefore, explicitly query the thread's lwp before caching it in
           its thread list entry. */
           its thread list entry. */
 
 
        if (!read_map (switchto_thread->private->mapp, &map))
        if (!read_map (switchto_thread->private->mapp, &map))
          goto err;
          goto err;
        if (map.thr_lwpp)
        if (map.thr_lwpp)
          {
          {
            if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
            if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
              goto err;
              goto err;
            if ((info = find_thread_lwp (lwp.lwp_id)))
            if ((info = find_thread_lwp (lwp.lwp_id)))
              info->private->lwpid = 0;
              info->private->lwpid = 0;
            switchto_thread->private->lwpid = lwp.lwp_id;
            switchto_thread->private->lwpid = lwp.lwp_id;
          }
          }
      }
      }
 
 
    switchto_thread->private->stable = 1;
    switchto_thread->private->stable = 1;
    switchto_thread = NULL;
    switchto_thread = NULL;
    break;
    break;
 
 
  case tc_invalid:
  case tc_invalid:
  case tc_thread_suspend:
  case tc_thread_suspend:
  case tc_thread_suspend_pending:
  case tc_thread_suspend_pending:
  case tc_thread_continue:
  case tc_thread_continue:
  err:
  err:
    DBG(("unexpected condition in libthread_stub()"));
    DBG(("unexpected condition in libthread_stub()"));
    break;
    break;
  }
  }
 
 
  DBG2(("libthread_stub(%s): %s %s %s", dbgpid (pid), dbgpid (tid),
  DBG2(("libthread_stub(%s): %s %s %s", dbgpid (pid), dbgpid (tid),
        dbgchange (change), tid ? dbgstate (map.thr_state) : ""));
        dbgchange (change), tid ? dbgstate (map.thr_state) : ""));
}
}
 
 
/* Wait for thread/lwp/process ID if >= 0 or for any thread otherwise. */
/* Wait for thread/lwp/process ID if >= 0 or for any thread otherwise. */
 
 
static int
static int
uw_thread_wait (int pid, struct target_waitstatus *status)
uw_thread_wait (int pid, struct target_waitstatus *status)
{
{
  if (pid > 0)
  if (pid > 0)
    pid = thr_to_lwp (pid);
    pid = thr_to_lwp (pid);
  CALL_BASE (pid = base_ops.to_wait (pid > 0 ? pid : -1, status));
  CALL_BASE (pid = base_ops.to_wait (pid > 0 ? pid : -1, status));
 
 
  if (status->kind == TARGET_WAITKIND_STOPPED &&
  if (status->kind == TARGET_WAITKIND_STOPPED &&
      status->value.sig == TARGET_SIGNAL_TRAP)
      status->value.sig == TARGET_SIGNAL_TRAP)
    libthread_stub (pid);
    libthread_stub (pid);
 
 
  return lwp_to_thr (pid);
  return lwp_to_thr (pid);
}
}
 
 
/* Tell gdb about the registers in the thread/lwp/process specified by
/* Tell gdb about the registers in the thread/lwp/process specified by
   inferior_pid. */
   inferior_pid. */
 
 
static void
static void
uw_thread_fetch_registers (int regno)
uw_thread_fetch_registers (int regno)
{
{
  int called;
  int called;
  struct thread_info *info;
  struct thread_info *info;
  struct thread_map map;
  struct thread_map map;
 
 
  TRY_BASE (base_ops.to_fetch_registers (regno), &called);
  TRY_BASE (base_ops.to_fetch_registers (regno), &called);
  if (called)
  if (called)
    return;
    return;
 
 
  if (!(info = find_thread_pid (inferior_pid)))
  if (!(info = find_thread_pid (inferior_pid)))
    return;
    return;
  if (!read_map (info->private->mapp, &map))
  if (!read_map (info->private->mapp, &map))
    return;
    return;
 
 
  supply_gregset (&map.thr_ucontext.uc_mcontext.gregs);
  supply_gregset (&map.thr_ucontext.uc_mcontext.gregs);
  supply_fpregset (&map.thr_ucontext.uc_mcontext.fpregs);
  supply_fpregset (&map.thr_ucontext.uc_mcontext.fpregs);
}
}
 
 
/* Store gdb's current view of the register set into the thread/lwp/process
/* Store gdb's current view of the register set into the thread/lwp/process
   specified by inferior_pid. */
   specified by inferior_pid. */
 
 
static void
static void
uw_thread_store_registers (int regno)
uw_thread_store_registers (int regno)
{
{
  CALL_BASE (base_ops.to_store_registers (regno));
  CALL_BASE (base_ops.to_store_registers (regno));
}
}
 
 
/* Prepare to modify the registers array. */
/* Prepare to modify the registers array. */
 
 
static void
static void
uw_thread_prepare_to_store (void)
uw_thread_prepare_to_store (void)
{
{
  CALL_BASE (base_ops.to_prepare_to_store ());
  CALL_BASE (base_ops.to_prepare_to_store ());
}
}
 
 
/* Fork an inferior process and start debugging it.
/* Fork an inferior process and start debugging it.
 
 
   This function only gets called with uw_thread_active == 0. */
   This function only gets called with uw_thread_active == 0. */
 
 
static void
static void
uw_thread_create_inferior (char *exec_file, char *allargs, char **env)
uw_thread_create_inferior (char *exec_file, char *allargs, char **env)
{
{
  if (uw_thread_active)
  if (uw_thread_active)
    deactivate_uw_thread ();
    deactivate_uw_thread ();
 
 
  procfs_ops.to_create_inferior (exec_file, allargs, env);
  procfs_ops.to_create_inferior (exec_file, allargs, env);
  if (uw_thread_active)
  if (uw_thread_active)
    {
    {
      find_main ();
      find_main ();
      thr_infpid (NULL);
      thr_infpid (NULL);
    }
    }
}
}
 
 
/* Kill and forget about the inferior process. */
/* Kill and forget about the inferior process. */
 
 
static void
static void
uw_thread_kill (void)
uw_thread_kill (void)
{
{
  base_ops.to_kill ();
  base_ops.to_kill ();
}
}
 
 
/* Clean up after the inferior exits. */
/* Clean up after the inferior exits. */
 
 
static void
static void
uw_thread_mourn_inferior (void)
uw_thread_mourn_inferior (void)
{
{
  deactivate_uw_thread ();
  deactivate_uw_thread ();
  base_ops.to_mourn_inferior ();
  base_ops.to_mourn_inferior ();
}
}
 
 
/* Return whether this module can attach to and run processes.
/* Return whether this module can attach to and run processes.
 
 
   This function only gets called with uw_thread_active == 0. */
   This function only gets called with uw_thread_active == 0. */
 
 
static int
static int
uw_thread_can_run (void)
uw_thread_can_run (void)
{
{
  return procfs_suppress_run;
  return procfs_suppress_run;
}
}
 
 
/* Return whether thread PID is still valid. */
/* Return whether thread PID is still valid. */
 
 
static int
static int
uw_thread_alive (int pid)
uw_thread_alive (int pid)
{
{
  if (!ISTID (pid))
  if (!ISTID (pid))
    return base_ops.to_thread_alive (pid);
    return base_ops.to_thread_alive (pid);
 
 
  /* If it's in the thread list, it's valid, because otherwise
  /* If it's in the thread list, it's valid, because otherwise
     libthread_stub() would have deleted it. */
     libthread_stub() would have deleted it. */
  return in_thread_list (pid);
  return in_thread_list (pid);
}
}
 
 
/* Add to the thread list any threads and lwps it doesn't already contain. */
/* Add to the thread list any threads and lwps it doesn't already contain. */
 
 
static void
static void
uw_thread_find_new_threads (void)
uw_thread_find_new_threads (void)
{
{
  CALL_BASE (if (base_ops.to_find_new_threads)
  CALL_BASE (if (base_ops.to_find_new_threads)
               base_ops.to_find_new_threads ());
               base_ops.to_find_new_threads ());
  notice_threads ();
  notice_threads ();
}
}
 
 
/* Return a string for pretty-printing PID in "info threads" output.
/* Return a string for pretty-printing PID in "info threads" output.
   This may be called by either procfs.c or by generic gdb. */
   This may be called by either procfs.c or by generic gdb. */
 
 
static char *
static char *
uw_thread_pid_to_str (int pid)
uw_thread_pid_to_str (int pid)
{
{
#define FMT "Thread %d"
#define FMT "Thread %d"
  static char buf[sizeof (FMT) + 3 * sizeof (pid)];
  static char buf[sizeof (FMT) + 3 * sizeof (pid)];
 
 
  if (!ISTID (pid))
  if (!ISTID (pid))
    /* core_ops says "process foo", so call procfs_ops explicitly. */
    /* core_ops says "process foo", so call procfs_ops explicitly. */
    return procfs_ops.to_pid_to_str (pid);
    return procfs_ops.to_pid_to_str (pid);
 
 
  sprintf (buf, FMT, TIDGET (pid));
  sprintf (buf, FMT, TIDGET (pid));
#undef FMT
#undef FMT
  return buf;
  return buf;
}
}
 
 
/* Return a string displaying INFO state information in "info threads"
/* Return a string displaying INFO state information in "info threads"
   output. */
   output. */
 
 
static char *
static char *
uw_extra_thread_info (struct thread_info *info)
uw_extra_thread_info (struct thread_info *info)
{
{
  static char buf[80];
  static char buf[80];
  struct thread_map map;
  struct thread_map map;
  __lwp_desc_t lwp;
  __lwp_desc_t lwp;
  int lwpid;
  int lwpid;
  char *name;
  char *name;
 
 
  if (!ISTID (info->pid))
  if (!ISTID (info->pid))
    return NULL;
    return NULL;
 
 
  if (!info->private->stable)
  if (!info->private->stable)
    return "switching";
    return "switching";
 
 
  if (!read_map (info->private->mapp, &map))
  if (!read_map (info->private->mapp, &map))
    return NULL;
    return NULL;
 
 
  if (!map.thr_lwpp || !read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
  if (!map.thr_lwpp || !read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
    lwpid = 0;
    lwpid = 0;
  else
  else
    lwpid = lwp.lwp_id;
    lwpid = lwp.lwp_id;
 
 
  switch (map.thr_state) {
  switch (map.thr_state) {
  case TS_ONPROC:       name = "running";       break;
  case TS_ONPROC:       name = "running";       break;
  case TS_SLEEP:        name = "sleeping";      break;
  case TS_SLEEP:        name = "sleeping";      break;
  case TS_RUNNABLE:     name = "runnable";      break;
  case TS_RUNNABLE:     name = "runnable";      break;
  case TS_ZOMBIE:       name = "zombie";        break;
  case TS_ZOMBIE:       name = "zombie";        break;
  case TS_SUSPENDED:    name = "suspended";     break;
  case TS_SUSPENDED:    name = "suspended";     break;
#ifdef TS_FORK
#ifdef TS_FORK
  case TS_FORK:         name = "forking";       break;
  case TS_FORK:         name = "forking";       break;
#endif
#endif
  default:              name = "confused";      break;
  default:              name = "confused";      break;
  }
  }
 
 
  if (!lwpid)
  if (!lwpid)
    return name;
    return name;
 
 
  sprintf (buf, "%s, LWP %d", name, lwpid);
  sprintf (buf, "%s, LWP %d", name, lwpid);
  return buf;
  return buf;
}
}
 
 
/* Check whether libthread.so has just been loaded, and if so, try to
/* Check whether libthread.so has just been loaded, and if so, try to
   initialize user-space thread debugging support.
   initialize user-space thread debugging support.
 
 
   libthread.so loading happens while (a) an inferior process is being
   libthread.so loading happens while (a) an inferior process is being
   started by procfs and (b) a core image is being loaded.
   started by procfs and (b) a core image is being loaded.
 
 
   This function often gets called with uw_thread_active == 0. */
   This function often gets called with uw_thread_active == 0. */
 
 
static void
static void
libthread_init (void)
libthread_init (void)
{
{
  struct minimal_symbol *ms;
  struct minimal_symbol *ms;
  struct thread_debug debug;
  struct thread_debug debug;
  CORE_ADDR onp;
  CORE_ADDR onp;
  struct breakpoint *b;
  struct breakpoint *b;
  int one = 1;
  int one = 1;
 
 
  /* Don't initialize twice. */
  /* Don't initialize twice. */
  if (uw_thread_active)
  if (uw_thread_active)
    return;
    return;
 
 
  /* Check whether libthread.so has been loaded. */
  /* Check whether libthread.so has been loaded. */
  if (!(ms = lookup_minimal_symbol ("_thr_debug", NULL, NULL)))
  if (!(ms = lookup_minimal_symbol ("_thr_debug", NULL, NULL)))
    return;
    return;
 
 
  /* Cache _thr_debug's address. */
  /* Cache _thr_debug's address. */
  if (!(thr_debug_addr = SYMBOL_VALUE_ADDRESS (ms)))
  if (!(thr_debug_addr = SYMBOL_VALUE_ADDRESS (ms)))
    return;
    return;
 
 
  /* Initialize base_ops.to_xfer_memory(). */
  /* Initialize base_ops.to_xfer_memory(). */
  base_ops = current_target;
  base_ops = current_target;
 
 
  /* Load _thr_debug's current contents. */
  /* Load _thr_debug's current contents. */
  if (!read_thr_debug (&debug))
  if (!read_thr_debug (&debug))
    return;
    return;
 
 
  /* User code (e.g. my test programs) may dereference _thr_debug,
  /* User code (e.g. my test programs) may dereference _thr_debug,
     making it availble to GDB before shared libs are loaded. */
     making it availble to GDB before shared libs are loaded. */
  if (!debug.thr_map)
  if (!debug.thr_map)
    return;
    return;
 
 
  /* libthread.so has been loaded, and the current_target should now
  /* libthread.so has been loaded, and the current_target should now
     reflect core_ops or procfs_ops. */
     reflect core_ops or procfs_ops. */
  push_target (&uw_thread_ops);         /* must precede notice_threads() */
  push_target (&uw_thread_ops);         /* must precede notice_threads() */
  uw_thread_active = 1;
  uw_thread_active = 1;
 
 
  if (!target_has_execution)
  if (!target_has_execution)
 
 
    /* Locate threads in core file. */
    /* Locate threads in core file. */
    notice_threads ();
    notice_threads ();
 
 
  else
  else
    {
    {
      /* Set a breakpoint on the stub function provided by libthread.so. */
      /* Set a breakpoint on the stub function provided by libthread.so. */
      thr_brk_addr = (CORE_ADDR)debug.thr_brk;
      thr_brk_addr = (CORE_ADDR)debug.thr_brk;
      if (!(b = create_thread_event_breakpoint (thr_brk_addr)))
      if (!(b = create_thread_event_breakpoint (thr_brk_addr)))
        goto err;
        goto err;
 
 
      /* Activate the stub function. */
      /* Activate the stub function. */
      onp = (CORE_ADDR)&((struct thread_debug *)thr_debug_addr)->thr_debug_on;
      onp = (CORE_ADDR)&((struct thread_debug *)thr_debug_addr)->thr_debug_on;
      if (!base_ops.to_xfer_memory ((CORE_ADDR)onp, (char *)&one,
      if (!base_ops.to_xfer_memory ((CORE_ADDR)onp, (char *)&one,
                                    sizeof (one), 1, &base_ops))
                                    sizeof (one), 1, &base_ops))
        {
        {
          delete_breakpoint (b);
          delete_breakpoint (b);
          goto err;
          goto err;
        }
        }
 
 
      /* Prepare for finding the main thread, which doesn't yet exist. */
      /* Prepare for finding the main thread, which doesn't yet exist. */
      thr_map_main = 0;
      thr_map_main = 0;
    }
    }
 
 
  return;
  return;
 
 
 err:
 err:
  warning ("uw-thread: unable to initialize user-mode thread debugging\n");
  warning ("uw-thread: unable to initialize user-mode thread debugging\n");
  deactivate_uw_thread ();
  deactivate_uw_thread ();
}
}
 
 
/* target_new_objfile_hook callback.
/* target_new_objfile_hook callback.
 
 
   If OBJFILE is non-null, check whether libthread.so was just loaded,
   If OBJFILE is non-null, check whether libthread.so was just loaded,
   and if so, prepare for user-mode thread debugging.
   and if so, prepare for user-mode thread debugging.
 
 
   If OBJFILE is null, libthread.so has gone away, so stop debugging
   If OBJFILE is null, libthread.so has gone away, so stop debugging
   user-mode threads.
   user-mode threads.
 
 
   This function often gets called with uw_thread_active == 0. */
   This function often gets called with uw_thread_active == 0. */
 
 
static void
static void
uw_thread_new_objfile (struct objfile *objfile)
uw_thread_new_objfile (struct objfile *objfile)
{
{
  if (objfile)
  if (objfile)
    libthread_init ();
    libthread_init ();
 
 
  else if (uw_thread_active)
  else if (uw_thread_active)
    deactivate_uw_thread ();
    deactivate_uw_thread ();
 
 
  if (target_new_objfile_chain)
  if (target_new_objfile_chain)
    target_new_objfile_chain (objfile);
    target_new_objfile_chain (objfile);
}
}
 
 
/* Initialize uw_thread_ops. */
/* Initialize uw_thread_ops. */
 
 
static void
static void
init_uw_thread_ops (void)
init_uw_thread_ops (void)
{
{
  uw_thread_ops.to_shortname          = "unixware-threads";
  uw_thread_ops.to_shortname          = "unixware-threads";
  uw_thread_ops.to_longname           = "UnixWare threads and pthread.";
  uw_thread_ops.to_longname           = "UnixWare threads and pthread.";
  uw_thread_ops.to_doc        = "UnixWare threads and pthread support.";
  uw_thread_ops.to_doc        = "UnixWare threads and pthread support.";
  uw_thread_ops.to_attach             = uw_thread_attach;
  uw_thread_ops.to_attach             = uw_thread_attach;
  uw_thread_ops.to_detach             = uw_thread_detach;
  uw_thread_ops.to_detach             = uw_thread_detach;
  uw_thread_ops.to_resume             = uw_thread_resume;
  uw_thread_ops.to_resume             = uw_thread_resume;
  uw_thread_ops.to_wait               = uw_thread_wait;
  uw_thread_ops.to_wait               = uw_thread_wait;
  uw_thread_ops.to_fetch_registers    = uw_thread_fetch_registers;
  uw_thread_ops.to_fetch_registers    = uw_thread_fetch_registers;
  uw_thread_ops.to_store_registers    = uw_thread_store_registers;
  uw_thread_ops.to_store_registers    = uw_thread_store_registers;
  uw_thread_ops.to_prepare_to_store   = uw_thread_prepare_to_store;
  uw_thread_ops.to_prepare_to_store   = uw_thread_prepare_to_store;
  uw_thread_ops.to_create_inferior    = uw_thread_create_inferior;
  uw_thread_ops.to_create_inferior    = uw_thread_create_inferior;
  uw_thread_ops.to_kill               = uw_thread_kill;
  uw_thread_ops.to_kill               = uw_thread_kill;
  uw_thread_ops.to_mourn_inferior     = uw_thread_mourn_inferior;
  uw_thread_ops.to_mourn_inferior     = uw_thread_mourn_inferior;
  uw_thread_ops.to_can_run            = uw_thread_can_run;
  uw_thread_ops.to_can_run            = uw_thread_can_run;
  uw_thread_ops.to_thread_alive       = uw_thread_alive;
  uw_thread_ops.to_thread_alive       = uw_thread_alive;
  uw_thread_ops.to_find_new_threads   = uw_thread_find_new_threads;
  uw_thread_ops.to_find_new_threads   = uw_thread_find_new_threads;
  uw_thread_ops.to_pid_to_str         = uw_thread_pid_to_str;
  uw_thread_ops.to_pid_to_str         = uw_thread_pid_to_str;
  uw_thread_ops.to_extra_thread_info  = uw_extra_thread_info;
  uw_thread_ops.to_extra_thread_info  = uw_extra_thread_info;
  uw_thread_ops.to_stratum            = thread_stratum;
  uw_thread_ops.to_stratum            = thread_stratum;
  uw_thread_ops.to_magic              = OPS_MAGIC;
  uw_thread_ops.to_magic              = OPS_MAGIC;
}
}
 
 
/* Module startup initialization function, automagically called by
/* Module startup initialization function, automagically called by
   init.c. */
   init.c. */
 
 
void
void
_initialize_uw_thread (void)
_initialize_uw_thread (void)
{
{
  init_uw_thread_ops ();
  init_uw_thread_ops ();
  add_target (&uw_thread_ops);
  add_target (&uw_thread_ops);
 
 
  procfs_suppress_run = 1;
  procfs_suppress_run = 1;
 
 
  /* Notice when libthread.so gets loaded. */
  /* Notice when libthread.so gets loaded. */
  target_new_objfile_chain = target_new_objfile_hook;
  target_new_objfile_chain = target_new_objfile_hook;
  target_new_objfile_hook = uw_thread_new_objfile;
  target_new_objfile_hook = uw_thread_new_objfile;
}
}
 
 

powered by: WebSVN 2.1.0

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