#include "server.h"
#include "linux-low.h"
#include <sys/wait.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/dir.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
struct inferior_list all_processes;
int stopping_threads;
int using_threads;
static void linux_resume_one_process (struct inferior_list_entry *entry,
int step, int signal);
static void linux_resume (int step, int signal);
static void stop_all_processes (void);
static int linux_wait_for_event (struct thread_info *child);
struct pending_signals
{
int signal;
struct pending_signals *prev;
};
#define PTRACE_ARG3_TYPE long
#define PTRACE_XFER_TYPE long
#ifdef HAVE_LINUX_REGSETS
static int use_regsets_p = 1;
#endif
extern int errno;
int debug_threads = 0;
#define pid_of(proc) ((proc)->head.id)
#define inferior_pid (pid_of (get_thread_process (current_inferior)))
static CORE_ADDR
get_stop_pc (void)
{
CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
if (get_thread_process (current_inferior)->stepping)
return stop_pc;
else
return stop_pc - the_low_target.decr_pc_after_break;
}
static void *
add_process (int pid)
{
struct process_info *process;
process = (struct process_info *) malloc (sizeof (*process));
memset (process, 0, sizeof (*process));
process->head.id = pid;
process->tid = pid;
process->lwpid = pid;
add_inferior_to_list (&all_processes, &process->head);
return process;
}
static int
linux_create_inferior (char *program, char **allargs)
{
void *new_process;
int pid;
pid = fork ();
if (pid < 0)
perror_with_name ("fork");
if (pid == 0)
{
ptrace (PTRACE_TRACEME, 0, 0, 0);
signal (SIGRTMIN + 1, SIG_DFL);
setpgid (0, 0);
execv (program, allargs);
fprintf (stderr, "Cannot exec %s: %s.\n", program,
strerror (errno));
fflush (stderr);
_exit (0177);
}
new_process = add_process (pid);
add_thread (pid, new_process);
return pid;
}
void
linux_attach_lwp (int pid, int tid)
{
struct process_info *new_process;
if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
{
fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
errno < sys_nerr ? sys_errlist[errno] : "unknown error",
errno);
fflush (stderr);
if (!using_threads)
_exit (0177);
return;
}
new_process = (struct process_info *) add_process (pid);
add_thread (tid, new_process);
if (! stopping_threads)
new_process->stop_expected = 1;
}
int
linux_attach (int pid)
{
struct process_info *process;
linux_attach_lwp (pid, pid);
process = (struct process_info *) find_inferior_id (&all_processes, pid);
process->stop_expected = 0;
return 0;
}
static void
linux_kill_one_process (struct inferior_list_entry *entry)
{
struct thread_info *thread = (struct thread_info *) entry;
struct process_info *process = get_thread_process (thread);
int wstat;
do
{
ptrace (PTRACE_KILL, pid_of (process), 0, 0);
wstat = linux_wait_for_event (thread);
} while (WIFSTOPPED (wstat));
}
static void
linux_kill (void)
{
for_each_inferior (&all_threads, linux_kill_one_process);
}
static int
linux_thread_alive (int tid)
{
if (find_inferior_id (&all_threads, tid) != NULL)
return 1;
else
return 0;
}
static int
check_removed_breakpoint (struct process_info *event_child)
{
CORE_ADDR stop_pc;
struct thread_info *saved_inferior;
if (event_child->pending_is_breakpoint == 0)
return 0;
if (debug_threads)
fprintf (stderr, "Checking for breakpoint.\n");
saved_inferior = current_inferior;
current_inferior = get_process_thread (event_child);
stop_pc = get_stop_pc ();
if (stop_pc != event_child->pending_stop_pc)
{
if (debug_threads)
fprintf (stderr, "Ignoring, PC was changed.\n");
event_child->pending_is_breakpoint = 0;
current_inferior = saved_inferior;
return 0;
}
if ((*the_low_target.breakpoint_at) (stop_pc))
{
if (debug_threads)
fprintf (stderr, "Ignoring, breakpoint is still present.\n");
current_inferior = saved_inferior;
return 0;
}
if (debug_threads)
fprintf (stderr, "Removed breakpoint.\n");
if (the_low_target.set_pc != NULL)
(*the_low_target.set_pc) (stop_pc);
event_child->status_pending_p = 0;
event_child->status_pending = 0;
current_inferior = saved_inferior;
return 1;
}
static int
status_pending_p (struct inferior_list_entry *entry, void *dummy)
{
struct process_info *process = (struct process_info *) entry;
if (process->status_pending_p)
if (check_removed_breakpoint (process))
{
linux_resume_one_process (&process->head, 0, 0);
return 0;
}
return process->status_pending_p;
}
static void
linux_wait_for_process (struct process_info **childp, int *wstatp)
{
int ret;
int to_wait_for = -1;
if (*childp != NULL)
to_wait_for = (*childp)->lwpid;
while (1)
{
ret = waitpid (to_wait_for, wstatp, WNOHANG);
if (ret == -1)
{
if (errno != ECHILD)
perror_with_name ("waitpid");
}
else if (ret > 0)
break;
ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE);
if (ret == -1)
{
if (errno != ECHILD)
perror_with_name ("waitpid (WCLONE)");
}
else if (ret > 0)
break;
usleep (1000);
}
if (debug_threads
&& (!WIFSTOPPED (*wstatp)
|| (WSTOPSIG (*wstatp) != 32
&& WSTOPSIG (*wstatp) != 33)))
fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
if (to_wait_for == -1)
*childp = (struct process_info *) find_inferior_id (&all_processes, ret);
(*childp)->stopped = 1;
(*childp)->pending_is_breakpoint = 0;
if (debug_threads
&& WIFSTOPPED (*wstatp))
{
current_inferior = (struct thread_info *)
find_inferior_id (&all_threads, (*childp)->tid);
if (the_low_target.get_pc != NULL)
get_stop_pc ();
}
}
static int
linux_wait_for_event (struct thread_info *child)
{
CORE_ADDR stop_pc;
struct process_info *event_child;
int wstat;
if (child == NULL)
{
event_child = (struct process_info *)
find_inferior (&all_processes, status_pending_p, NULL);
if (debug_threads && event_child)
fprintf (stderr, "Got a pending child %d\n", event_child->lwpid);
}
else
{
event_child = get_thread_process (child);
if (event_child->status_pending_p
&& check_removed_breakpoint (event_child))
event_child = NULL;
}
if (event_child != NULL)
{
if (event_child->status_pending_p)
{
if (debug_threads)
fprintf (stderr, "Got an event from pending child %d (%04x)\n",
event_child->lwpid, event_child->status_pending);
wstat = event_child->status_pending;
event_child->status_pending_p = 0;
event_child->status_pending = 0;
current_inferior = get_process_thread (event_child);
return wstat;
}
}
while (1)
{
if (child == NULL)
event_child = NULL;
else
event_child = get_thread_process (child);
linux_wait_for_process (&event_child, &wstat);
if (event_child == NULL)
error ("event from unknown child");
current_inferior = (struct thread_info *)
find_inferior_id (&all_threads, event_child->tid);
if (using_threads)
{
if (! WIFSTOPPED (wstat))
{
if (debug_threads)
fprintf (stderr, "Thread %d (LWP %d) exiting\n",
event_child->tid, event_child->head.id);
if (all_threads.head == all_threads.tail)
return wstat;
dead_thread_notify (event_child->tid);
remove_inferior (&all_processes, &event_child->head);
free (event_child);
remove_thread (current_inferior);
current_inferior = (struct thread_info *) all_threads.head;
if (child != NULL)
return wstat;
continue;
}
if (WIFSTOPPED (wstat)
&& WSTOPSIG (wstat) == SIGSTOP
&& event_child->stop_expected)
{
if (debug_threads)
fprintf (stderr, "Expected stop.\n");
event_child->stop_expected = 0;
linux_resume_one_process (&event_child->head,
event_child->stepping, 0);
continue;
}
if (WIFSTOPPED (wstat)
&& (WSTOPSIG (wstat) == SIGRTMIN
|| WSTOPSIG (wstat) == SIGRTMIN + 1))
{
if (debug_threads)
fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n",
WSTOPSIG (wstat), event_child->tid,
event_child->head.id);
linux_resume_one_process (&event_child->head,
event_child->stepping,
WSTOPSIG (wstat));
continue;
}
}
if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
return wstat;
if (the_low_target.get_pc == NULL)
return wstat;
stop_pc = get_stop_pc ();
if (event_child->bp_reinsert != 0)
{
if (debug_threads)
fprintf (stderr, "Reinserted breakpoint.\n");
reinsert_breakpoint (event_child->bp_reinsert);
event_child->bp_reinsert = 0;
linux_resume_one_process (&event_child->head, 0, 0);
continue;
}
if (debug_threads)
fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
if (check_breakpoints (stop_pc) != 0)
{
event_child->pending_is_breakpoint = 1;
event_child->pending_stop_pc = stop_pc;
if (the_low_target.breakpoint_reinsert_addr == NULL)
{
event_child->bp_reinsert = stop_pc;
uninsert_breakpoint (stop_pc);
linux_resume_one_process (&event_child->head, 1, 0);
}
else
{
reinsert_breakpoint_by_bp
(stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
linux_resume_one_process (&event_child->head, 0, 0);
}
continue;
}
if (event_child->stepping)
{
event_child->stepping = 0;
return wstat;
}
if ((*the_low_target.breakpoint_at) (stop_pc))
{
event_child->pending_is_breakpoint = 1;
event_child->pending_stop_pc = stop_pc;
}
return wstat;
}
return 0;
}
static unsigned char
linux_wait (char *status)
{
int w;
struct thread_info *child = NULL;
retry:
if (cont_thread > 0)
{
child = (struct thread_info *) find_inferior_id (&all_threads,
cont_thread);
if (child == NULL)
linux_resume (0, 0);
}
enable_async_io ();
w = linux_wait_for_event (child);
stop_all_processes ();
disable_async_io ();
if (all_threads.head == all_threads.tail)
{
if (WIFEXITED (w))
{
fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
*status = 'W';
clear_inferiors ();
return ((unsigned char) WEXITSTATUS (w));
}
else if (!WIFSTOPPED (w))
{
fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
clear_inferiors ();
*status = 'X';
return ((unsigned char) WTERMSIG (w));
}
}
else
{
if (!WIFSTOPPED (w))
goto retry;
}
*status = 'T';
return ((unsigned char) WSTOPSIG (w));
}
static void
send_sigstop (struct inferior_list_entry *entry)
{
struct process_info *process = (struct process_info *) entry;
if (process->stopped)
return;
if (process->stop_expected)
{
process->stop_expected = 0;
return;
}
if (debug_threads)
fprintf (stderr, "Sending sigstop to process %d\n", process->head.id);
kill (process->head.id, SIGSTOP);
process->sigstop_sent = 1;
}
static void
wait_for_sigstop (struct inferior_list_entry *entry)
{
struct process_info *process = (struct process_info *) entry;
struct thread_info *saved_inferior, *thread;
int wstat, saved_tid;
if (process->stopped)
return;
saved_inferior = current_inferior;
saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
thread = (struct thread_info *) find_inferior_id (&all_threads,
process->tid);
wstat = linux_wait_for_event (thread);
if (WIFSTOPPED (wstat)
&& WSTOPSIG (wstat) != SIGSTOP)
{
if (debug_threads)
fprintf (stderr, "Stopped with non-sigstop signal\n");
process->status_pending_p = 1;
process->status_pending = wstat;
process->stop_expected = 1;
}
if (linux_thread_alive (saved_tid))
current_inferior = saved_inferior;
else
{
if (debug_threads)
fprintf (stderr, "Previously current thread died.\n");
set_desired_inferior (0);
}
}
static void
stop_all_processes (void)
{
stopping_threads = 1;
for_each_inferior (&all_processes, send_sigstop);
for_each_inferior (&all_processes, wait_for_sigstop);
stopping_threads = 0;
}
static void
linux_resume_one_process (struct inferior_list_entry *entry,
int step, int signal)
{
struct process_info *process = (struct process_info *) entry;
struct thread_info *saved_inferior;
if (process->stopped == 0)
return;
if (signal != 0
&& (process->status_pending_p || process->pending_signals != NULL
|| process->bp_reinsert != 0))
{
struct pending_signals *p_sig;
p_sig = malloc (sizeof (*p_sig));
p_sig->prev = process->pending_signals;
p_sig->signal = signal;
process->pending_signals = p_sig;
}
if (process->status_pending_p)
return;
saved_inferior = current_inferior;
current_inferior = get_process_thread (process);
if (debug_threads)
fprintf (stderr, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid,
step ? "step" : "continue", signal,
process->stop_expected ? "expected" : "not expected");
if (process->bp_reinsert != 0)
{
if (debug_threads)
fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
if (step == 0)
fprintf (stderr, "BAD - reinserting but not stepping.\n");
step = 1;
signal = 0;
}
check_removed_breakpoint (process);
if (debug_threads && the_low_target.get_pc != NULL)
{
fprintf (stderr, " ");
(long) (*the_low_target.get_pc) ();
}
if (process->pending_signals != NULL && process->bp_reinsert == 0)
{
struct pending_signals **p_sig;
p_sig = &process->pending_signals;
while ((*p_sig)->prev != NULL)
p_sig = &(*p_sig)->prev;
signal = (*p_sig)->signal;
free (*p_sig);
*p_sig = NULL;
}
regcache_invalidate_one ((struct inferior_list_entry *)
get_process_thread (process));
errno = 0;
process->stopped = 0;
process->stepping = step;
ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
current_inferior = saved_inferior;
if (errno)
perror_with_name ("ptrace");
}
static void
linux_continue_one_process (struct inferior_list_entry *entry)
{
struct process_info *process;
process = (struct process_info *) entry;
linux_resume_one_process (entry, process->stepping, 0);
}
static void
linux_resume (int step, int signal)
{
struct process_info *process;
process = get_thread_process (current_inferior);
linux_resume_one_process (&process->head, step, signal);
if (cont_thread == 0 || cont_thread == -1)
for_each_inferior (&all_processes, linux_continue_one_process);
}
#ifdef HAVE_LINUX_USRREGS
int
register_addr (int regnum)
{
int addr;
if (regnum < 0 || regnum >= the_low_target.num_regs)
error ("Invalid register number %d.", regnum);
addr = the_low_target.regmap[regnum];
return addr;
}
static void
fetch_register (int regno)
{
CORE_ADDR regaddr;
register int i;
char *buf;
if (regno >= the_low_target.num_regs)
return;
if ((*the_low_target.cannot_fetch_register) (regno))
return;
regaddr = register_addr (regno);
if (regaddr == -1)
return;
buf = alloca (register_size (regno));
for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
{
errno = 0;
*(PTRACE_XFER_TYPE *) (buf + i) =
ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
regaddr += sizeof (PTRACE_XFER_TYPE);
if (errno != 0)
{
char *err = strerror (errno);
char *msg = alloca (strlen (err) + 128);
sprintf (msg, "reading register %d: %s", regno, err);
error (msg);
goto error_exit;
}
}
supply_register (regno, buf);
error_exit:;
}
static void
usr_fetch_inferior_registers (int regno)
{
if (regno == -1 || regno == 0)
for (regno = 0; regno < the_low_target.num_regs; regno++)
fetch_register (regno);
else
fetch_register (regno);
}
static void
usr_store_inferior_registers (int regno)
{
CORE_ADDR regaddr;
int i;
char *buf;
if (regno >= 0)
{
if (regno >= the_low_target.num_regs)
return;
if ((*the_low_target.cannot_store_register) (regno) == 1)
return;
regaddr = register_addr (regno);
if (regaddr == -1)
return;
errno = 0;
buf = alloca (register_size (regno));
collect_register (regno, buf);
for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
{
errno = 0;
ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
*(int *) (buf + i));
if (errno != 0)
{
if ((*the_low_target.cannot_store_register) (regno) == 0)
{
char *err = strerror (errno);
char *msg = alloca (strlen (err) + 128);
sprintf (msg, "writing register %d: %s",
regno, err);
error (msg);
return;
}
}
regaddr += sizeof (int);
}
}
else
for (regno = 0; regno < the_low_target.num_regs; regno++)
usr_store_inferior_registers (regno);
}
#endif
#ifdef HAVE_LINUX_REGSETS
static int
regsets_fetch_inferior_registers ()
{
struct regset_info *regset;
regset = target_regsets;
while (regset->size >= 0)
{
void *buf;
int res;
if (regset->size == 0)
{
regset ++;
continue;
}
buf = malloc (regset->size);
res = ptrace (regset->get_request, inferior_pid, 0, buf);
if (res < 0)
{
if (errno == EIO)
{
if (regset == target_regsets)
{
use_regsets_p = 0;
return -1;
}
else
{
regset->size = 0;
continue;
}
}
else
{
char s[256];
sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
inferior_pid);
perror (s);
}
}
regset->store_function (buf);
regset ++;
}
return 0;
}
static int
regsets_store_inferior_registers ()
{
struct regset_info *regset;
regset = target_regsets;
while (regset->size >= 0)
{
void *buf;
int res;
if (regset->size == 0)
{
regset ++;
continue;
}
buf = malloc (regset->size);
regset->fill_function (buf);
res = ptrace (regset->set_request, inferior_pid, 0, buf);
if (res < 0)
{
if (errno == EIO)
{
if (regset == target_regsets)
{
use_regsets_p = 0;
return -1;
}
else
{
regset->size = 0;
continue;
}
}
else
{
perror ("Warning: ptrace(regsets_store_inferior_registers)");
}
}
regset ++;
free (buf);
}
return 0;
}
#endif
void
linux_fetch_registers (int regno)
{
#ifdef HAVE_LINUX_REGSETS
if (use_regsets_p)
{
if (regsets_fetch_inferior_registers () == 0)
return;
}
#endif
#ifdef HAVE_LINUX_USRREGS
usr_fetch_inferior_registers (regno);
#endif
}
void
linux_store_registers (int regno)
{
#ifdef HAVE_LINUX_REGSETS
if (use_regsets_p)
{
if (regsets_store_inferior_registers () == 0)
return;
}
#endif
#ifdef HAVE_LINUX_USRREGS
usr_store_inferior_registers (regno);
#endif
}
static void
linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
{
register int i;
register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
register int count
= (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
/ sizeof (PTRACE_XFER_TYPE);
register PTRACE_XFER_TYPE *buffer
= (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
{
buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
}
memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
}
static int
linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
{
register int i;
register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
register int count
= (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
extern int errno;
if (debug_threads)
{
fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
}
buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
(PTRACE_ARG3_TYPE) addr, 0);
if (count > 1)
{
buffer[count - 1]
= ptrace (PTRACE_PEEKTEXT, inferior_pid,
(PTRACE_ARG3_TYPE) (addr + (count - 1)
* sizeof (PTRACE_XFER_TYPE)),
0);
}
memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
{
errno = 0;
ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
if (errno)
return errno;
}
return 0;
}
static void
linux_look_up_symbols (void)
{
#ifdef USE_THREAD_DB
if (using_threads)
return;
using_threads = thread_db_init ();
#endif
}
static struct target_ops linux_target_ops = {
linux_create_inferior,
linux_attach,
linux_kill,
linux_thread_alive,
linux_resume,
linux_wait,
linux_fetch_registers,
linux_store_registers,
linux_read_memory,
linux_write_memory,
linux_look_up_symbols,
};
static void
linux_init_signals ()
{
signal (SIGRTMIN+1, SIG_IGN);
}
void
initialize_low (void)
{
using_threads = 0;
set_target_ops (&linux_target_ops);
set_breakpoint_data (the_low_target.breakpoint,
the_low_target.breakpoint_len);
init_registers ();
linux_init_signals ();
}