#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"
#include "regcache.h"
#include "gdbcmd.h"
static int debug_lin_lwp;
extern const char *strsignal (int sig);
struct lwp_info
{
ptid_t ptid;
int cloned;
int signalled;
int stopped;
int resumed;
int status;
int step;
struct lwp_info *next;
};
static struct lwp_info *lwp_list;
static int num_lwps;
static int threaded;
#define GET_LWP(ptid) ptid_get_lwp (ptid)
#define GET_PID(ptid) ptid_get_pid (ptid)
#define is_lwp(ptid) (GET_LWP (ptid) != 0)
#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
ptid_t trap_ptid;
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 int stop_wait_callback (struct lwp_info *lp, void *data);
static char *
status_to_str (int status)
{
static char buf[64];
if (WIFSTOPPED (status))
snprintf (buf, sizeof (buf), "%s (stopped)",
strsignal (WSTOPSIG (status)));
else if (WIFSIGNALED (status))
snprintf (buf, sizeof (buf), "%s (terminated)",
strsignal (WSTOPSIG (status)));
else
snprintf (buf, sizeof (buf), "%d (exited)",
WEXITSTATUS (status));
return buf;
}
static void
init_lwp_list (void)
{
struct lwp_info *lp, *lpnext;
for (lp = lwp_list; lp; lp = lpnext)
{
lpnext = lp->next;
xfree (lp);
}
lwp_list = NULL;
num_lwps = 0;
threaded = 0;
}
static struct lwp_info *
add_lwp (ptid_t ptid)
{
struct lwp_info *lp;
gdb_assert (is_lwp (ptid));
lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
memset (lp, 0, sizeof (struct lwp_info));
lp->ptid = ptid;
lp->next = lwp_list;
lwp_list = lp;
if (++num_lwps > 1)
threaded = 1;
return lp;
}
static void
delete_lwp (ptid_t ptid)
{
struct lwp_info *lp, *lpprev;
lpprev = NULL;
for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
if (ptid_equal (lp->ptid, ptid))
break;
if (!lp)
return;
num_lwps--;
if (lpprev)
lpprev->next = lp->next;
else
lwp_list = lp->next;
xfree (lp);
}
static struct lwp_info *
find_lwp_pid (ptid_t ptid)
{
struct lwp_info *lp;
int lwp;
if (is_lwp (ptid))
lwp = GET_LWP (ptid);
else
lwp = GET_PID (ptid);
for (lp = lwp_list; lp; lp = lp->next)
if (lwp == GET_LWP (lp->ptid))
return lp;
return NULL;
}
struct lwp_info *
iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
{
struct lwp_info *lp, *lpnext;
for (lp = lwp_list; lp; lp = lpnext)
{
lpnext = lp->next;
if ((*callback) (lp, data))
return lp;
}
return NULL;
}
int
lin_lwp_prepare_to_proceed (void)
{
if (! ptid_equal (trap_ptid, null_ptid)
&& ! ptid_equal (inferior_ptid, trap_ptid))
{
CORE_ADDR stop_pc = read_pc ();
CORE_ADDR trap_pc;
trap_pc = read_pc_pid (trap_ptid);
if (trap_pc != stop_pc && breakpoint_here_p (trap_pc))
{
inferior_ptid = trap_ptid;
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 (ptid_t ptid, int verbose)
{
struct lwp_info *lp;
gdb_assert (is_lwp (ptid));
if (! sigismember (&blocked_mask, SIGCHLD))
{
sigaddset (&blocked_mask, SIGCHLD);
sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
}
if (verbose)
printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
lp = find_lwp_pid (ptid);
if (lp == NULL)
lp = add_lwp (ptid);
if (GET_LWP (ptid) != GET_PID (ptid))
{
pid_t pid;
int status;
if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
error ("Can't attach %s: %s", target_pid_to_str (ptid),
strerror (errno));
pid = waitpid (GET_LWP (ptid), &status, 0);
if (pid == -1 && errno == ECHILD)
{
pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
lp->cloned = 1;
}
gdb_assert (pid == GET_LWP (ptid)
&& WIFSTOPPED (status) && WSTOPSIG (status));
lp->stopped = 1;
}
else
{
lp->stopped = 1;
}
}
static void
lin_lwp_attach (char *args, int from_tty)
{
struct lwp_info *lp;
pid_t pid;
int status;
child_ops.to_attach (args, from_tty);
lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
pid = waitpid (GET_PID (inferior_ptid), &status, 0);
if (pid == -1 && errno == ECHILD)
{
warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
lp->cloned = 1;
}
gdb_assert (pid == GET_PID (inferior_ptid)
&& WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
lp->stopped = 1;
lp->status = W_STOPCODE (SIGSTOP);
lp->resumed = 1;
}
static int
detach_callback (struct lwp_info *lp, void *data)
{
gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
if (debug_lin_lwp && lp->status)
fprintf_unfiltered (gdb_stdlog, "Pending %s for LWP %ld on detach.\n",
strsignal (WSTOPSIG (lp->status)), GET_LWP (lp->ptid));
while (lp->signalled && lp->stopped)
{
if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
WSTOPSIG (lp->status)) < 0)
error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
strerror (errno));
lp->stopped = 0;
lp->signalled = 0;
lp->status = 0;
stop_wait_callback (lp, NULL);
gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
}
if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
{
if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
WSTOPSIG (lp->status)) < 0)
error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
strerror (errno));
delete_lwp (lp->ptid);
}
return 0;
}
static void
lin_lwp_detach (char *args, int from_tty)
{
iterate_over_lwps (detach_callback, NULL);
gdb_assert (num_lwps == 1);
trap_ptid = null_ptid;
init_lwp_list ();
sigprocmask (SIG_SETMASK, &normal_mask, NULL);
sigemptyset (&blocked_mask);
inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
child_ops.to_detach (args, from_tty);
}
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->ptid))
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 0
tp = find_thread_pid (lp->ptid);
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 (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
lp->stopped = 0;
lp->step = 0;
}
return 0;
}
static int
resume_clear_callback (struct lwp_info *lp, void *data)
{
lp->resumed = 0;
return 0;
}
static int
resume_set_callback (struct lwp_info *lp, void *data)
{
lp->resumed = 1;
return 0;
}
static void
lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
{
struct lwp_info *lp;
int resume_all;
resume_all = (PIDGET (ptid) == -1) || !step;
if (resume_all)
iterate_over_lwps (resume_set_callback, NULL);
else
iterate_over_lwps (resume_clear_callback, NULL);
if (PIDGET (ptid) == -1)
ptid = inferior_ptid;
lp = find_lwp_pid (ptid);
if (lp)
{
ptid = pid_to_ptid (GET_LWP (lp->ptid));
lp->step = step;
lp->resumed = 1;
if (lp->status)
{
gdb_assert (signo == TARGET_SIGNAL_0);
return;
}
lp->stopped = 0;
}
if (resume_all)
iterate_over_lwps (resume_callback, NULL);
child_resume (ptid, step, signo);
}
static int
stop_callback (struct lwp_info *lp, void *data)
{
if (! lp->stopped && ! lp->signalled)
{
int ret;
ret = kill (GET_LWP (lp->ptid), 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)
{
sigset_t *flush_mask = data;
if (! lp->stopped && lp->signalled)
{
pid_t pid;
int status;
gdb_assert (lp->status == 0);
pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0);
if (pid == -1 && errno == ECHILD)
return 0;
gdb_assert (pid == GET_LWP (lp->ptid));
if (WIFEXITED (status) || WIFSIGNALED (status))
{
gdb_assert (num_lwps > 1);
if (in_thread_list (lp->ptid))
{
if (!ptid_equal (lp->ptid, inferior_ptid))
delete_thread (lp->ptid);
printf_unfiltered ("[%s exited]\n",
target_pid_to_str (lp->ptid));
}
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"%s exited.\n", target_pid_to_str (lp->ptid));
delete_lwp (lp->ptid);
return 0;
}
gdb_assert (WIFSTOPPED (status));
if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
{
ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
return stop_wait_callback (lp, flush_mask);
}
if (WSTOPSIG (status) != SIGSTOP)
{
if (WSTOPSIG (status) == SIGTRAP)
{
ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
if (debug_lin_lwp)
{
fprintf_unfiltered (gdb_stderr,
"SWC: Candidate SIGTRAP event in %ld\n",
GET_LWP (lp->ptid));
}
stop_wait_callback (lp, data);
if (lp->status)
kill (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
lp->status = status;
return 0;
}
else
{
if (debug_lin_lwp)
{
fprintf_unfiltered (gdb_stderr,
"SWC: Pending event %d in %ld\n",
WSTOPSIG (status), GET_LWP (lp->ptid));
}
ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
stop_wait_callback (lp, data);
if (lp->status == 0)
lp->status = status;
else
kill (GET_LWP (lp->ptid), WSTOPSIG (status));
return 0;
}
}
else
{
lp->stopped = 1;
lp->signalled = 0;
}
}
return 0;
}
static int
status_callback (struct lwp_info *lp, void *data)
{
return (lp->status != 0 && lp->resumed);
}
static int
running_callback (struct lwp_info *lp, void *data)
{
return (lp->stopped == 0);
}
static int
count_events_callback (struct lwp_info *lp, void *data)
{
int *count = data;
gdb_assert (count != NULL);
if (lp->status != 0
&& WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
(*count)++;
return 0;
}
static int
select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
{
if (lp->step && lp->status != 0)
return 1;
else
return 0;
}
static int
select_event_lwp_callback (struct lwp_info *lp, void *data)
{
int *selector = data;
gdb_assert (selector != NULL);
if (lp->status != 0
&& WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
if ((*selector)-- == 0)
return 1;
return 0;
}
static int
cancel_breakpoints_callback (struct lwp_info *lp, void *data)
{
struct lwp_info *event_lp = data;
if (lp == event_lp)
return 0;
if (lp->status != 0
&& WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
&& breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
DECR_PC_AFTER_BREAK))
{
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"Push back breakpoint for LWP %ld\n",
GET_LWP (lp->ptid));
if (DECR_PC_AFTER_BREAK)
write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
lp->status = 0;
}
return 0;
}
static void
select_event_lwp (struct lwp_info **orig_lp, int *status)
{
int num_events = 0;
int random_selector;
struct lwp_info *event_lp;
(*orig_lp)->status = *status;
event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
if (event_lp != NULL)
{
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"Select single-step LWP %ld\n",
GET_LWP (event_lp->ptid));
}
else
{
iterate_over_lwps (count_events_callback, &num_events);
random_selector = (int)
((num_events * (double) rand ()) / (RAND_MAX + 1.0));
if (debug_lin_lwp && num_events > 1)
fprintf_unfiltered (gdb_stdlog,
"Found %d SIGTRAP events, selecting #%d\n",
num_events, random_selector);
event_lp = iterate_over_lwps (select_event_lwp_callback,
&random_selector);
}
if (event_lp != NULL)
{
*orig_lp = event_lp;
*status = event_lp->status;
}
(*orig_lp)->status = 0;
}
static int
resumed_callback (struct lwp_info *lp, void *data)
{
return lp->resumed;
}
#ifdef CHILD_WAIT
ptid_t
child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
{
int save_errno;
int status;
pid_t pid;
do
{
set_sigint_trap ();
set_sigio_trap ();
pid = waitpid (GET_PID (ptid), &status, 0);
if (pid == -1 && errno == ECHILD)
pid = waitpid (GET_PID (ptid), &status, __WCLONE);
save_errno = errno;
clear_sigio_trap ();
clear_sigint_trap ();
}
while (pid == -1 && errno == EINTR);
if (pid == -1)
{
warning ("Child process unexpectedly missing: %s", strerror (errno));
ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
return minus_one_ptid;
}
store_waitstatus (ourstatus, status);
return pid_to_ptid (pid);
}
#endif
static ptid_t
lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
{
struct lwp_info *lp = NULL;
int options = 0;
int status = 0;
pid_t pid = PIDGET (ptid);
sigset_t flush_mask;
sigemptyset (&flush_mask);
if (! sigismember (&blocked_mask, SIGCHLD))
{
sigaddset (&blocked_mask, SIGCHLD);
sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
}
retry:
gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
if (pid == -1)
{
lp = iterate_over_lwps (status_callback, NULL);
if (lp)
{
status = lp->status;
lp->status = 0;
if (debug_lin_lwp && status)
fprintf_unfiltered (gdb_stdlog,
"Using pending wait status %s for LWP %ld.\n",
status_to_str (status), GET_LWP (lp->ptid));
}
options = __WCLONE | WNOHANG;
}
else if (is_lwp (ptid))
{
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"Waiting for specific LWP %ld.\n",
GET_LWP (ptid));
lp = find_lwp_pid (ptid);
gdb_assert (lp);
status = lp->status;
lp->status = 0;
if (debug_lin_lwp && status)
fprintf_unfiltered (gdb_stdlog,
"Using pending wait status %s for LWP %ld.\n",
status_to_str (status), GET_LWP (lp->ptid));
options = lp->cloned ? __WCLONE : 0;
pid = GET_LWP (ptid);
}
if (status && lp->signalled)
{
child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
TARGET_SIGNAL_0);
lp->stopped = 0;
gdb_assert (lp->resumed);
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 (pid_to_ptid (lwpid));
if (! lp)
{
lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
if (options & __WCLONE)
lp->cloned = 1;
if (threaded)
{
gdb_assert (WIFSTOPPED (status)
&& WSTOPSIG (status) == SIGSTOP);
lp->signalled = 1;
if (! in_thread_list (inferior_ptid))
{
inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
GET_PID (inferior_ptid));
add_thread (inferior_ptid);
}
add_thread (lp->ptid);
printf_unfiltered ("[New %s]\n",
target_pid_to_str (lp->ptid));
}
}
if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
{
if (in_thread_list (lp->ptid))
{
if (! ptid_equal (lp->ptid, inferior_ptid))
delete_thread (lp->ptid);
printf_unfiltered ("[%s exited]\n",
target_pid_to_str (lp->ptid));
}
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"%s exited.\n",
target_pid_to_str (lp->ptid));
delete_lwp (lp->ptid);
gdb_assert (iterate_over_lwps (running_callback, NULL));
status = 0;
continue;
}
if (lp->signalled && WIFSTOPPED (status)
&& WSTOPSIG (status) == SIGSTOP)
{
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"Delayed SIGSTOP caught for %s.\n",
target_pid_to_str (lp->ptid));
lp->signalled = 0;
child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
TARGET_SIGNAL_0);
lp->stopped = 0;
gdb_assert (lp->resumed);
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 (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
lp->stopped = 0;
status = 0;
goto retry;
}
if (signo == TARGET_SIGNAL_INT
&& signal_pass_state (signo) == 0)
{
sigaddset (&flush_mask, SIGINT);
}
}
lp->stopped = 1;
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog, "Candidate event %s in LWP %ld.\n",
status_to_str (status), GET_LWP (lp->ptid));
iterate_over_lwps (stop_callback, NULL);
iterate_over_lwps (stop_wait_callback, &flush_mask);
if (pid == -1)
select_event_lwp (&lp, &status);
iterate_over_lwps (cancel_breakpoints_callback, lp);
if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
{
trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
if (debug_lin_lwp)
fprintf_unfiltered (gdb_stdlog,
"LLW: trap_ptid is %ld\n",
GET_LWP (trap_ptid));
}
else
trap_ptid = null_ptid;
store_waitstatus (ourstatus, status);
return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
}
static int
kill_callback (struct lwp_info *lp, void *data)
{
ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
return 0;
}
static int
kill_wait_callback (struct lwp_info *lp, void *data)
{
pid_t pid;
if (lp->cloned)
{
do
{
pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
}
while (pid == GET_LWP (lp->ptid));
gdb_assert (pid == -1 && errno == ECHILD);
}
do
{
pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
}
while (pid == GET_LWP (lp->ptid));
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)
{
child_ops.to_create_inferior (exec_file, allargs, env);
}
static void
lin_lwp_mourn_inferior (void)
{
trap_ptid = null_ptid;
init_lwp_list ();
sigprocmask (SIG_SETMASK, &normal_mask, NULL);
sigemptyset (&blocked_mask);
child_ops.to_mourn_inferior ();
}
static void
lin_lwp_fetch_registers (int regno)
{
struct cleanup *old_chain = save_inferior_ptid ();
if (is_lwp (inferior_ptid))
inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
fetch_inferior_registers (regno);
do_cleanups (old_chain);
}
static void
lin_lwp_store_registers (int regno)
{
struct cleanup *old_chain = save_inferior_ptid ();
if (is_lwp (inferior_ptid))
inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
store_inferior_registers (regno);
do_cleanups (old_chain);
}
static int
lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
struct mem_attrib *attrib,
struct target_ops *target)
{
struct cleanup *old_chain = save_inferior_ptid ();
int xfer;
if (is_lwp (inferior_ptid))
inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
do_cleanups (old_chain);
return xfer;
}
static int
lin_lwp_thread_alive (ptid_t ptid)
{
gdb_assert (is_lwp (ptid));
errno = 0;
ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
if (errno)
return 0;
return 1;
}
static char *
lin_lwp_pid_to_str (ptid_t ptid)
{
static char buf[64];
if (is_lwp (ptid))
{
snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
return buf;
}
return normal_pid_to_str (ptid);
}
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);
add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
(char *) &debug_lin_lwp,
"Set debugging of linux lwp module.\n\
Enables printf debugging output.\n",
&setdebuglist),
&showdebuglist);
}
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);
}