#include "server.h"
#include "linux-low.h"
#include "../inlining.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>
#include <errno.h>
#include <sys/syscall.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 (struct thread_resume *resume_info);
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
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 (unsigned long 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, pid);
return pid;
}
void
linux_attach_lwp (unsigned long pid, unsigned long tid)
{
struct process_info *new_process;
if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
{
fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
strerror (errno), errno);
fflush (stderr);
if (!using_threads)
_exit (0177);
return;
}
new_process = (struct process_info *) add_process (pid);
add_thread (tid, new_process, pid);
if (! stopping_threads)
new_process->stop_expected = 1;
}
int
linux_attach (unsigned long 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;
if (entry == all_threads.head)
return;
do
{
ptrace (PTRACE_KILL, pid_of (process), 0, 0);
wstat = linux_wait_for_event (thread);
} while (WIFSTOPPED (wstat));
}
static void
linux_kill (void)
{
struct thread_info *thread = (struct thread_info *) all_threads.head;
struct process_info *process = get_thread_process (thread);
int wstat;
for_each_inferior (&all_threads, linux_kill_one_process);
do
{
ptrace (PTRACE_KILL, pid_of (process), 0, 0);
wstat = linux_wait_for_event (thread);
} while (WIFSTOPPED (wstat));
}
static void
linux_detach_one_process (struct inferior_list_entry *entry)
{
struct thread_info *thread = (struct thread_info *) entry;
struct process_info *process = get_thread_process (thread);
ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
}
static void
linux_detach (void)
{
for_each_inferior (&all_threads, linux_detach_one_process);
}
static int
linux_thread_alive (unsigned long 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 != inlined_function_call_stack_pc ())
inlined_function_update_call_stack (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->pending_is_breakpoint = 0;
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 %ld\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 %ld (%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 %ld (LWP %ld) 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 %ld (LWP %ld).\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 (stop_pc != inlined_function_call_stack_pc ())
inlined_function_update_call_stack (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 && cont_thread != -1)
{
child = (struct thread_info *) find_inferior_id (&all_threads,
cont_thread);
if (child == NULL)
{
struct thread_resume resume_info;
resume_info.thread = -1;
resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
linux_resume (&resume_info);
}
}
enable_async_io ();
unblock_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 ();
free (all_processes.head);
all_processes.head = all_processes.tail = NULL;
return ((unsigned char) WEXITSTATUS (w));
}
else if (!WIFSTOPPED (w))
{
fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
*status = 'X';
clear_inferiors ();
free (all_processes.head);
all_processes.head = all_processes.tail = NULL;
return ((unsigned char) WTERMSIG (w));
}
}
else
{
if (!WIFSTOPPED (w))
goto retry;
}
*status = 'T';
return ((unsigned char) WSTOPSIG (w));
}
static int
kill_lwp (unsigned long lwpid, int signo)
{
static int tkill_failed;
errno = 0;
#ifdef SYS_tkill
if (!tkill_failed)
{
int ret = syscall (SYS_tkill, lwpid, signo);
if (errno != ENOSYS)
return ret;
errno = 0;
tkill_failed = 1;
}
#endif
return kill (lwpid, signo);
}
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 %ld\n", process->head.id);
kill_lwp (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;
unsigned long 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 && !check_removed_breakpoint (process))
return;
saved_inferior = current_inferior;
current_inferior = get_process_thread (process);
if (debug_threads)
fprintf (stderr, "Resuming process %ld (%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 struct thread_resume *resume_ptr;
static void
linux_set_resume_request (struct inferior_list_entry *entry)
{
struct process_info *process;
struct thread_info *thread;
int ndx;
thread = (struct thread_info *) entry;
process = get_thread_process (thread);
ndx = 0;
while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id)
ndx++;
process->resume = &resume_ptr[ndx];
}
static void
linux_continue_one_thread (struct inferior_list_entry *entry)
{
struct process_info *process;
struct thread_info *thread;
int step;
thread = (struct thread_info *) entry;
process = get_thread_process (thread);
if (process->resume->leave_stopped)
return;
if (process->resume->thread == -1)
step = process->stepping || process->resume->step;
else
step = process->resume->step;
linux_resume_one_process (&process->head, step, process->resume->sig);
process->resume = NULL;
}
static void
linux_queue_one_thread (struct inferior_list_entry *entry)
{
struct process_info *process;
struct thread_info *thread;
thread = (struct thread_info *) entry;
process = get_thread_process (thread);
if (process->resume->leave_stopped)
return;
if (process->resume->sig != 0)
{
struct pending_signals *p_sig;
p_sig = malloc (sizeof (*p_sig));
p_sig->prev = process->pending_signals;
p_sig->signal = process->resume->sig;
process->pending_signals = p_sig;
}
process->resume = NULL;
}
static int
resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
{
struct process_info *process = (struct process_info *) entry;
if (process->resume->leave_stopped)
return 0;
if (process->status_pending_p)
check_removed_breakpoint (process);
if (process->status_pending_p)
* (int *) flag_p = 1;
return 0;
}
static void
linux_resume (struct thread_resume *resume_info)
{
int pending_flag;
resume_ptr = resume_info;
for_each_inferior (&all_threads, linux_set_resume_request);
pending_flag = 0;
find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
if (debug_threads)
{
if (pending_flag)
fprintf (stderr, "Not resuming, pending status\n");
else
fprintf (stderr, "Resuming, no pending status\n");
}
if (pending_flag)
for_each_inferior (&all_threads, linux_queue_one_thread);
else
{
block_async_io ();
enable_async_io ();
for_each_inferior (&all_threads, linux_continue_one_thread);
}
}
#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;
int i, size;
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;
size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
& - sizeof (PTRACE_XFER_TYPE);
buf = alloca (size);
for (i = 0; i < size; 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;
}
}
if (the_low_target.left_pad_xfer
&& register_size (regno) < sizeof (PTRACE_XFER_TYPE))
supply_register (regno, (buf + sizeof (PTRACE_XFER_TYPE)
- register_size (regno)));
else
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, size;
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;
size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
& - sizeof (PTRACE_XFER_TYPE);
buf = alloca (size);
memset (buf, 0, size);
if (the_low_target.left_pad_xfer
&& register_size (regno) < sizeof (PTRACE_XFER_TYPE))
collect_register (regno, (buf + sizeof (PTRACE_XFER_TYPE)
- register_size (regno)));
else
collect_register (regno, buf);
for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
{
errno = 0;
ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
*(PTRACE_XFER_TYPE *) (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 (PTRACE_XFER_TYPE);
}
}
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;
int saw_general_regs = 0;
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=%ld",
inferior_pid);
perror (s);
}
}
else if (regset->type == GENERAL_REGS)
saw_general_regs = 1;
regset->store_function (buf);
regset ++;
}
if (saw_general_regs)
return 0;
else
return 1;
}
static int
regsets_store_inferior_registers ()
{
struct regset_info *regset;
int saw_general_regs = 0;
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)");
}
}
else if (regset->type == GENERAL_REGS)
saw_general_regs = 1;
regset ++;
free (buf);
}
if (saw_general_regs)
return 0;
else
return 1;
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 int
linux_read_memory (CORE_ADDR memaddr, unsigned 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))
{
errno = 0;
buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
if (errno)
return errno;
}
memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
return 0;
}
static int
linux_write_memory (CORE_ADDR memaddr, const unsigned 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 void
linux_send_signal (int signum)
{
extern unsigned long signal_pid;
if (cont_thread != 0 && cont_thread != -1)
{
struct process_info *process;
process = get_thread_process (current_inferior);
kill_lwp (process->lwpid, signum);
}
else
kill_lwp (signal_pid, signum);
}
static int
linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
{
char filename[PATH_MAX];
int fd, n;
snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid);
fd = open (filename, O_RDONLY);
if (fd < 0)
return -1;
if (offset != (CORE_ADDR) 0
&& lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
n = -1;
else
n = read (fd, myaddr, len);
close (fd);
return n;
}
static int
linux_insert_watchpoint (char type, CORE_ADDR addr, int len)
{
if (the_low_target.insert_watchpoint != NULL)
return the_low_target.insert_watchpoint (type, addr, len);
else
return 1;
}
static int
linux_remove_watchpoint (char type, CORE_ADDR addr, int len)
{
if (the_low_target.remove_watchpoint != NULL)
return the_low_target.remove_watchpoint (type, addr, len);
else
return 1;
}
static int
linux_stopped_by_watchpoint (void)
{
if (the_low_target.stopped_by_watchpoint != NULL)
return the_low_target.stopped_by_watchpoint ();
else
return 0;
}
static CORE_ADDR
linux_stopped_data_address (void)
{
if (the_low_target.stopped_data_address != NULL)
return the_low_target.stopped_data_address ();
else
return 0;
}
static struct target_ops linux_target_ops = {
linux_create_inferior,
linux_attach,
linux_kill,
linux_detach,
linux_thread_alive,
linux_resume,
linux_wait,
linux_fetch_registers,
linux_store_registers,
linux_read_memory,
linux_write_memory,
linux_look_up_symbols,
linux_send_signal,
linux_read_auxv,
linux_insert_watchpoint,
linux_remove_watchpoint,
linux_stopped_by_watchpoint,
linux_stopped_data_address,
};
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 ();
}