#include "defs.h"
#include "gdb_assert.h"
#include <errno.h>
#include <signal.h>
#include <sys/ptrace.h>
#include "gdb_wait.h"
#include "gdbthread.h"
#include "inferior.h"
#include "target.h"
#define DEBUG 1
#if DEBUG
extern const char *strsignal (int sig);
#endif
struct lwp_info
{
int pid;
int signalled;
int stopped;
int status;
int step;
struct lwp_info *next;
};
static struct lwp_info *lwp_list;
static int num_lwps;
static int threaded;
#ifndef TIDGET
#define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
#define PIDGET(PID) (((PID) & 0xffff))
#define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
#endif
#define THREAD_FLAG 0x80000000
#define is_lwp(pid) (((pid) & THREAD_FLAG) == 0 && TIDGET (pid))
#define GET_LWP(pid) TIDGET (pid)
#define GET_PID(pid) PIDGET (pid)
#define BUILD_LWP(tid, pid) MERGEPID (pid, tid)
#define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
int trap_pid;
static struct target_ops lin_lwp_ops;
extern struct target_ops child_ops;
static sigset_t normal_mask;
static sigset_t suspend_mask;
static sigset_t blocked_mask;
static void lin_lwp_mourn_inferior (void);
static void
init_lwp_list (void)
{
struct lwp_info *lp, *lpnext;
for (lp = lwp_list; lp; lp = lpnext)
{
lpnext = lp->next;
free (lp);
}
lwp_list = NULL;
num_lwps = 0;
threaded = 0;
}
static struct lwp_info *
add_lwp (int pid)
{
struct lwp_info *lp;
gdb_assert (is_lwp (pid));
lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
memset (lp, 0, sizeof (struct lwp_info));
lp->pid = pid;
lp->next = lwp_list;
lwp_list = lp;
if (++num_lwps > 1)
threaded = 1;
return lp;
}
static void
delete_lwp (int pid)
{
struct lwp_info *lp, *lpprev;
lpprev = NULL;
for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
if (lp->pid == pid)
break;
if (!lp)
return;
num_lwps--;
if (lpprev)
lpprev->next = lp->next;
else
lwp_list = lp->next;
free (lp);
}
static struct lwp_info *
find_lwp_pid (int pid)
{
struct lwp_info *lp;
if (is_lwp (pid))
pid = GET_LWP (pid);
for (lp = lwp_list; lp; lp = lp->next)
if (pid == GET_LWP (lp->pid))
return lp;
return NULL;
}
struct lwp_info *
iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
{
struct lwp_info *lp;
for (lp = lwp_list; lp; lp = lp->next)
if ((*callback) (lp, data))
return lp;
return NULL;
}
static void
restore_inferior_pid (void *arg)
{
int *saved_pid_ptr = arg;
inferior_pid = *saved_pid_ptr;
free (arg);
}
static struct cleanup *
save_inferior_pid (void)
{
int *saved_pid_ptr;
saved_pid_ptr = xmalloc (sizeof (int));
*saved_pid_ptr = inferior_pid;
return make_cleanup (restore_inferior_pid, saved_pid_ptr);
}
int
lin_lwp_prepare_to_proceed (void)
{
if (trap_pid && inferior_pid != trap_pid)
{
CORE_ADDR stop_pc = read_pc ();
CORE_ADDR trap_pc;
trap_pc = read_pc_pid (trap_pid);
if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
{
inferior_pid = trap_pid;
flush_cached_frames ();
registers_changed ();
return 1;
}
}
return 0;
}
#if 0
static void
lin_lwp_open (char *args, int from_tty)
{
push_target (&lin_lwp_ops);
}
#endif
void
lin_lwp_attach_lwp (int pid, int verbose)
{
struct lwp_info *lp;
gdb_assert (is_lwp (pid));
if (verbose)
printf_filtered ("[New %s]\n", target_pid_to_str (pid));
if (ptrace (PTRACE_ATTACH, GET_LWP (pid), 0, 0) < 0)
error ("Can't attach %s: %s", target_pid_to_str (pid), strerror (errno));
lp = add_lwp (pid);
lp->signalled = 1;
}
static void
lin_lwp_attach (char *args, int from_tty)
{
error("Not implemented yet");
}
static void
lin_lwp_detach (char *args, int from_tty)
{
error ("Not implemented yet");
}
struct private_thread_info
{
int lwpid;
};
static int
find_lwp_callback (struct thread_info *tp, void *data)
{
struct lwp_info *lp = data;
if (tp->private->lwpid == GET_LWP (lp->pid))
return 1;
return 0;
}
static int
resume_callback (struct lwp_info *lp, void *data)
{
if (lp->stopped && lp->status == 0)
{
struct thread_info *tp;
#if 1
tp = find_thread_pid (lp->pid);
if (tp == NULL)
tp = iterate_over_threads (find_lwp_callback, lp);
gdb_assert (tp);
if (tp->step_range_end && tp->step_resume_breakpoint == NULL)
{
gdb_assert (lp->step);
tp->step_range_start = tp->step_range_end = 0;
}
#endif
child_resume (GET_LWP (lp->pid), 0, TARGET_SIGNAL_0);
lp->stopped = 0;
lp->step = 0;
}
return 0;
}
static void
lin_lwp_resume (int pid, int step, enum target_signal signo)
{
struct lwp_info *lp;
int resume_all;
resume_all = (pid == -1) || !step;
if (pid == -1)
pid = inferior_pid;
lp = find_lwp_pid (pid);
if (lp)
{
pid = GET_LWP (lp->pid);
lp->stopped = 0;
lp->step = step;
if (lp->status)
{
gdb_assert (signo == TARGET_SIGNAL_0);
return;
}
}
if (resume_all)
iterate_over_lwps (resume_callback, NULL);
child_resume (pid, step, signo);
}
static int
stop_callback (struct lwp_info *lp, void *data)
{
if (! lp->stopped && ! lp->signalled)
{
int ret;
ret = kill (GET_LWP (lp->pid), SIGSTOP);
gdb_assert (ret == 0);
lp->signalled = 1;
gdb_assert (lp->status == 0);
}
return 0;
}
static int
stop_wait_callback (struct lwp_info *lp, void *data)
{
if (! lp->stopped && lp->signalled)
{
pid_t pid;
int status;
gdb_assert (lp->status == 0);
pid = waitpid (GET_LWP (lp->pid), &status,
is_cloned (lp->pid) ? __WCLONE : 0);
if (pid == -1 && errno == ECHILD)
return 0;
gdb_assert (pid == GET_LWP (lp->pid));
if (WIFEXITED (status) || WIFSIGNALED (status))
{
gdb_assert (num_lwps > 1);
if (in_thread_list (lp->pid))
{
if (lp->pid != inferior_pid)
delete_thread (lp->pid);
printf_unfiltered ("[%s exited]\n",
target_pid_to_str (lp->pid));
}
#if DEBUG
printf ("%s exited.\n", target_pid_to_str (lp->pid));
#endif
delete_lwp (lp->pid);
return 0;
}
gdb_assert (WIFSTOPPED (status));
lp->stopped = 1;
if (WSTOPSIG (status) != SIGSTOP)
{
if (WSTOPSIG (status) == SIGTRAP
&& breakpoint_inserted_here_p (read_pc_pid (pid)
- DECR_PC_AFTER_BREAK))
{
#if DEBUG
printf ("Tripped breakpoint at %lx in LWP %d"
" while waiting for SIGSTOP.\n",
(long) read_pc_pid (lp->pid), pid);
#endif
if (DECR_PC_AFTER_BREAK)
write_pc_pid (read_pc_pid (pid) - DECR_PC_AFTER_BREAK, pid);
}
else
{
#if DEBUG
printf ("Received %s in LWP %d while waiting for SIGSTOP.\n",
strsignal (WSTOPSIG (status)), pid);
#endif
lp->status = status;
}
}
else
{
lp->signalled = 0;
}
}
return 0;
}
static int
status_callback (struct lwp_info *lp, void *data)
{
return (lp->status != 0);
}
static int
running_callback (struct lwp_info *lp, void *data)
{
return (lp->stopped == 0);
}
static int
lin_lwp_wait (int pid, struct target_waitstatus *ourstatus)
{
struct lwp_info *lp = NULL;
int options = 0;
int status = 0;
if (! sigismember (&blocked_mask, SIGCHLD))
{
sigaddset (&blocked_mask, SIGCHLD);
sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
}
retry:
if (pid == -1)
{
lp = iterate_over_lwps (status_callback, NULL);
if (lp)
{
#if DEBUG
printf ("Using pending wait status for LWP %d.\n",
GET_LWP (lp->pid));
#endif
status = lp->status;
lp->status = 0;
}
options = __WCLONE | WNOHANG;
}
else if (is_lwp (pid))
{
#if DEBUG
printf ("Waiting for specific LWP %d.\n", GET_LWP (pid));
#endif
lp = find_lwp_pid (GET_LWP (pid));
gdb_assert (lp);
status = lp->status;
lp->status = 0;
#if DEBUG
if (status)
printf ("Using pending wait status for LWP %d.\n",
GET_LWP (lp->pid));
#endif
options = is_cloned (lp->pid) ? __WCLONE : 0;
pid = GET_LWP (pid);
}
if (status && lp->signalled)
{
child_resume (GET_LWP (lp->pid), lp->step, TARGET_SIGNAL_0);
lp->stopped = 0;
stop_wait_callback (lp, NULL);
}
set_sigint_trap ();
set_sigio_trap ();
while (status == 0)
{
pid_t lwpid;
lwpid = waitpid (pid, &status, options);
if (lwpid > 0)
{
gdb_assert (pid == -1 || lwpid == pid);
lp = find_lwp_pid (lwpid);
if (! lp)
{
lp = add_lwp (BUILD_LWP (lwpid, inferior_pid));
if (threaded)
{
gdb_assert (WIFSTOPPED (status)
&& WSTOPSIG (status) == SIGSTOP);
lp->signalled = 1;
if (! in_thread_list (inferior_pid))
{
inferior_pid = BUILD_LWP (inferior_pid, inferior_pid);
add_thread (inferior_pid);
}
add_thread (lp->pid);
printf_unfiltered ("[New %s]\n",
target_pid_to_str (lp->pid));
}
}
if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
{
if (in_thread_list (lp->pid))
{
if (lp->pid != inferior_pid)
delete_thread (lp->pid);
printf_unfiltered ("[%s exited]\n",
target_pid_to_str (lp->pid));
}
#if DEBUG
printf ("%s exited.\n", target_pid_to_str (lp->pid));
#endif
delete_lwp (lp->pid);
gdb_assert (iterate_over_lwps (running_callback, NULL));
status = 0;
continue;
}
if (lp->signalled && WIFSTOPPED (status)
&& WSTOPSIG (status) == SIGSTOP)
{
#if DEBUG
printf ("Delayed SIGSTOP caught for %s.\n",
target_pid_to_str (lp->pid));
#endif
lp->signalled = 0;
child_resume (GET_LWP (lp->pid), lp->step, TARGET_SIGNAL_0);
lp->stopped = 0;
status = 0;
continue;
}
break;
}
if (pid == -1)
{
options ^= __WCLONE;
if (options & __WCLONE)
sigsuspend (&suspend_mask);
}
gdb_assert (status == 0);
}
clear_sigio_trap ();
clear_sigint_trap ();
gdb_assert (lp);
if (WIFSTOPPED (status))
{
int signo = target_signal_from_host (WSTOPSIG (status));
if (signal_stop_state (signo) == 0
&& signal_print_state (signo) == 0
&& signal_pass_state (signo) == 1)
{
child_resume (GET_LWP (lp->pid), lp->step, signo);
lp->stopped = 0;
status = 0;
goto retry;
}
}
lp->stopped = 1;
iterate_over_lwps (stop_callback, NULL);
iterate_over_lwps (stop_wait_callback, NULL);
if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
trap_pid = (threaded ? lp->pid : GET_LWP (lp->pid));
else
trap_pid = 0;
store_waitstatus (ourstatus, status);
return (threaded ? lp->pid : GET_LWP (lp->pid));
}
static int
kill_callback (struct lwp_info *lp, void *data)
{
ptrace (PTRACE_KILL, GET_LWP (lp->pid), 0, 0);
return 0;
}
static int
kill_wait_callback (struct lwp_info *lp, void *data)
{
pid_t pid;
if (is_cloned (lp->pid))
{
do
{
pid = waitpid (GET_LWP (lp->pid), NULL, __WCLONE);
}
while (pid == GET_LWP (lp->pid));
gdb_assert (pid == -1 && errno == ECHILD);
}
do
{
pid = waitpid (GET_LWP (lp->pid), NULL, 0);
}
while (pid == GET_LWP (lp->pid));
gdb_assert (pid == -1 && errno == ECHILD);
return 0;
}
static void
lin_lwp_kill (void)
{
iterate_over_lwps (kill_callback, NULL);
iterate_over_lwps (kill_wait_callback, NULL);
target_mourn_inferior ();
}
static void
lin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
{
struct target_ops *target_beneath;
init_lwp_list ();
#if 0
target_beneath = find_target_beneath (&lin_lwp_ops);
#else
target_beneath = &child_ops;
#endif
target_beneath->to_create_inferior (exec_file, allargs, env);
}
static void
lin_lwp_mourn_inferior (void)
{
struct target_ops *target_beneath;
init_lwp_list ();
trap_pid = 0;
sigprocmask (SIG_SETMASK, &normal_mask, NULL);
sigemptyset (&blocked_mask);
#if 0
target_beneath = find_target_beneath (&lin_lwp_ops);
#else
target_beneath = &child_ops;
#endif
target_beneath->to_mourn_inferior ();
}
static void
lin_lwp_fetch_registers (int regno)
{
struct cleanup *old_chain = save_inferior_pid ();
if (is_lwp (inferior_pid))
inferior_pid = GET_LWP (inferior_pid);
fetch_inferior_registers (regno);
do_cleanups (old_chain);
}
static void
lin_lwp_store_registers (int regno)
{
struct cleanup *old_chain = save_inferior_pid ();
if (is_lwp (inferior_pid))
inferior_pid = GET_LWP (inferior_pid);
store_inferior_registers (regno);
do_cleanups (old_chain);
}
static int
lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
struct target_ops *target)
{
struct cleanup *old_chain = save_inferior_pid ();
int xfer;
if (is_lwp (inferior_pid))
inferior_pid = GET_LWP (inferior_pid);
xfer = child_xfer_memory (memaddr, myaddr, len, write, target);
do_cleanups (old_chain);
return xfer;
}
static int
lin_lwp_thread_alive (int pid)
{
gdb_assert (is_lwp (pid));
errno = 0;
ptrace (PTRACE_PEEKUSER, GET_LWP (pid), 0, 0);
if (errno)
return 0;
return 1;
}
static char *
lin_lwp_pid_to_str (int pid)
{
static char buf[64];
if (is_lwp (pid))
{
snprintf (buf, sizeof (buf), "LWP %d", GET_LWP (pid));
return buf;
}
return normal_pid_to_str (pid);
}
static void
init_lin_lwp_ops (void)
{
#if 0
lin_lwp_ops.to_open = lin_lwp_open;
#endif
lin_lwp_ops.to_shortname = "lwp-layer";
lin_lwp_ops.to_longname = "lwp-layer";
lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
lin_lwp_ops.to_attach = lin_lwp_attach;
lin_lwp_ops.to_detach = lin_lwp_detach;
lin_lwp_ops.to_resume = lin_lwp_resume;
lin_lwp_ops.to_wait = lin_lwp_wait;
lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
lin_lwp_ops.to_kill = lin_lwp_kill;
lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
lin_lwp_ops.to_stratum = thread_stratum;
lin_lwp_ops.to_has_thread_control = tc_schedlock;
lin_lwp_ops.to_magic = OPS_MAGIC;
}
static void
sigchld_handler (int signo)
{
}
void
_initialize_lin_lwp (void)
{
struct sigaction action;
extern void thread_db_init (struct target_ops *);
init_lin_lwp_ops ();
add_target (&lin_lwp_ops);
thread_db_init (&lin_lwp_ops);
sigprocmask (SIG_SETMASK, NULL, &normal_mask);
action.sa_handler = sigchld_handler;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
sigaction (SIGCHLD, &action, NULL);
sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
sigdelset (&suspend_mask, SIGCHLD);
sigemptyset (&blocked_mask);
}
static int
get_signo (const char *name)
{
struct minimal_symbol *ms;
int signo;
ms = lookup_minimal_symbol (name, NULL, NULL);
if (ms == NULL)
return 0;
if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
sizeof (signo)) != 0)
return 0;
return signo;
}
void
lin_thread_get_thread_signals (sigset_t *set)
{
struct sigaction action;
int restart, cancel;
sigemptyset (set);
restart = get_signo ("__pthread_sig_restart");
if (restart == 0)
return;
cancel = get_signo ("__pthread_sig_cancel");
if (cancel == 0)
return;
sigaddset (set, restart);
sigaddset (set, cancel);
action.sa_handler = sigchld_handler;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
sigaction (cancel, &action, NULL);
sigaddset (&blocked_mask, cancel);
sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
sigdelset (&suspend_mask, cancel);
}