gdb/inf-ptrace.c - gdb
Global variables defined
Functions defined
Source code
- #include "defs.h"
- #include "command.h"
- #include "inferior.h"
- #include "inflow.h"
- #include "terminal.h"
- #include "gdbcore.h"
- #include "regcache.h"
- #include "gdb_ptrace.h"
- #include "gdb_wait.h"
- #include <signal.h>
- #include "inf-ptrace.h"
- #include "inf-child.h"
- #include "gdbthread.h"
- #ifdef PT_GET_PROCESS_STATE
- static int
- inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
- int detach_fork)
- {
- if (!follow_child)
- {
- pid_t child_pid = inferior_thread->pending_follow.value.related_pid;
-
- if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
- perror_with_name (("ptrace"));
- }
- return 0;
- }
- #endif
- static void
- inf_ptrace_me (void)
- {
-
- ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
- }
- static void
- inf_ptrace_create_inferior (struct target_ops *ops,
- char *exec_file, char *allargs, char **env,
- int from_tty)
- {
- int pid;
-
- int ops_already_pushed = target_is_pushed (ops);
- struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
- if (! ops_already_pushed)
- {
-
- push_target (ops);
- make_cleanup_unpush_target (ops);
- }
- pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
- NULL, NULL, NULL);
- discard_cleanups (back_to);
- startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
-
- target_post_startup_inferior (pid_to_ptid (pid));
- }
- #ifdef PT_GET_PROCESS_STATE
- static void
- inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
- {
- ptrace_event_t pe;
-
- memset (&pe, 0, sizeof pe);
- pe.pe_set_event |= PTRACE_FORK;
- if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
- (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
- perror_with_name (("ptrace"));
- }
- #endif
- static void
- inf_ptrace_mourn_inferior (struct target_ops *ops)
- {
- int status;
-
- waitpid (ptid_get_pid (inferior_ptid), &status, 0);
- inf_child_mourn_inferior (ops);
- }
- static void
- inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
- {
- char *exec_file;
- pid_t pid;
- struct inferior *inf;
-
- int ops_already_pushed = target_is_pushed (ops);
- struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
- pid = parse_pid_to_attach (args);
- if (pid == getpid ())
- error (_("I refuse to debug myself!"));
- if (! ops_already_pushed)
- {
-
- push_target (ops);
- make_cleanup_unpush_target (ops);
- }
- if (from_tty)
- {
- exec_file = get_exec_file (0);
- if (exec_file)
- printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
- target_pid_to_str (pid_to_ptid (pid)));
- else
- printf_unfiltered (_("Attaching to %s\n"),
- target_pid_to_str (pid_to_ptid (pid)));
- gdb_flush (gdb_stdout);
- }
- #ifdef PT_ATTACH
- errno = 0;
- ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
- if (errno != 0)
- perror_with_name (("ptrace"));
- #else
- error (_("This system does not support attaching to a process"));
- #endif
- inf = current_inferior ();
- inferior_appeared (inf, pid);
- inf->attach_flag = 1;
- inferior_ptid = pid_to_ptid (pid);
-
- add_thread_silent (inferior_ptid);
- discard_cleanups (back_to);
- }
- #ifdef PT_GET_PROCESS_STATE
- static void
- inf_ptrace_post_attach (struct target_ops *self, int pid)
- {
- ptrace_event_t pe;
-
- memset (&pe, 0, sizeof pe);
- pe.pe_set_event |= PTRACE_FORK;
- if (ptrace (PT_SET_EVENT_MASK, pid,
- (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
- perror_with_name (("ptrace"));
- }
- #endif
- static void
- inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
- {
- pid_t pid = ptid_get_pid (inferior_ptid);
- int sig = 0;
- if (from_tty)
- {
- char *exec_file = get_exec_file (0);
- if (exec_file == 0)
- exec_file = "";
- printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
- target_pid_to_str (pid_to_ptid (pid)));
- gdb_flush (gdb_stdout);
- }
- if (args)
- sig = atoi (args);
- #ifdef PT_DETACH
-
- errno = 0;
- ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
- if (errno != 0)
- perror_with_name (("ptrace"));
- #else
- error (_("This system does not support detaching from a process"));
- #endif
- inferior_ptid = null_ptid;
- detach_inferior (pid);
- inf_child_maybe_unpush_target (ops);
- }
- static void
- inf_ptrace_kill (struct target_ops *ops)
- {
- pid_t pid = ptid_get_pid (inferior_ptid);
- int status;
- if (pid == 0)
- return;
- ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
- waitpid (pid, &status, 0);
- target_mourn_inferior ();
- }
- static void
- inf_ptrace_stop (struct target_ops *self, ptid_t ptid)
- {
-
- kill (-inferior_process_group (), SIGINT);
- }
- static void
- inf_ptrace_resume (struct target_ops *ops,
- ptid_t ptid, int step, enum gdb_signal signal)
- {
- pid_t pid = ptid_get_pid (ptid);
- int request;
- if (pid == -1)
-
- pid = ptid_get_pid (inferior_ptid);
- if (catch_syscall_enabled () > 0)
- request = PT_SYSCALL;
- else
- request = PT_CONTINUE;
- if (step)
- {
-
- request = PT_STEP;
- }
-
- errno = 0;
- ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
- if (errno != 0)
- perror_with_name (("ptrace"));
- }
- static ptid_t
- inf_ptrace_wait (struct target_ops *ops,
- ptid_t ptid, struct target_waitstatus *ourstatus, int options)
- {
- pid_t pid;
- int status, save_errno;
- do
- {
- set_sigint_trap ();
- do
- {
- pid = waitpid (ptid_get_pid (ptid), &status, 0);
- save_errno = errno;
- }
- while (pid == -1 && errno == EINTR);
- clear_sigint_trap ();
- if (pid == -1)
- {
- fprintf_unfiltered (gdb_stderr,
- _("Child process unexpectedly missing: %s.\n"),
- safe_strerror (save_errno));
-
- ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
- ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
- return inferior_ptid;
- }
-
- if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
- pid = -1;
- }
- while (pid == -1);
- #ifdef PT_GET_PROCESS_STATE
- if (WIFSTOPPED (status))
- {
- ptrace_state_t pe;
- pid_t fpid;
- if (ptrace (PT_GET_PROCESS_STATE, pid,
- (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
- perror_with_name (("ptrace"));
- switch (pe.pe_report_event)
- {
- case PTRACE_FORK:
- ourstatus->kind = TARGET_WAITKIND_FORKED;
- ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
-
- fpid = waitpid (pe.pe_other_pid, &status, 0);
- if (fpid == -1)
- perror_with_name (("waitpid"));
- if (ptrace (PT_GET_PROCESS_STATE, fpid,
- (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
- perror_with_name (("ptrace"));
- gdb_assert (pe.pe_report_event == PTRACE_FORK);
- gdb_assert (pe.pe_other_pid == pid);
- if (fpid == ptid_get_pid (inferior_ptid))
- {
- ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
- return pid_to_ptid (fpid);
- }
- return pid_to_ptid (pid);
- }
- }
- #endif
- store_waitstatus (ourstatus, status);
- return pid_to_ptid (pid);
- }
- static enum target_xfer_status
- inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
- const char *annex, gdb_byte *readbuf,
- const gdb_byte *writebuf,
- ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
- {
- pid_t pid = ptid_get_pid (inferior_ptid);
- switch (object)
- {
- case TARGET_OBJECT_MEMORY:
- #ifdef PT_IO
-
- {
- struct ptrace_io_desc piod;
-
- piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
- piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
- piod.piod_offs = (void *) (long) offset;
- piod.piod_len = len;
- errno = 0;
- if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
- {
-
- *xfered_len = piod.piod_len;
- return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
- }
-
- if (errno != EINVAL)
- return TARGET_XFER_EOF;
- }
- #endif
- {
- union
- {
- PTRACE_TYPE_RET word;
- gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
- } buffer;
- ULONGEST rounded_offset;
- ULONGEST partial_len;
-
- rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
-
- partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
- if (partial_len > len)
- partial_len = len;
- if (writebuf)
- {
-
- if (rounded_offset < offset
- || (offset + partial_len
- < rounded_offset + sizeof (PTRACE_TYPE_RET)))
-
- buffer.word = ptrace (PT_READ_I, pid,
- (PTRACE_TYPE_ARG3)(uintptr_t)
- rounded_offset, 0);
-
- memcpy (buffer.byte + (offset - rounded_offset),
- writebuf, partial_len);
- errno = 0;
- ptrace (PT_WRITE_D, pid,
- (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
- buffer.word);
- if (errno)
- {
-
- errno = 0;
- ptrace (PT_WRITE_I, pid,
- (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
- buffer.word);
- if (errno)
- return TARGET_XFER_EOF;
- }
- }
- if (readbuf)
- {
- errno = 0;
- buffer.word = ptrace (PT_READ_I, pid,
- (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
- 0);
- if (errno)
- return TARGET_XFER_EOF;
-
- memcpy (readbuf, buffer.byte + (offset - rounded_offset),
- partial_len);
- }
- *xfered_len = partial_len;
- return TARGET_XFER_OK;
- }
- case TARGET_OBJECT_UNWIND_TABLE:
- return TARGET_XFER_E_IO;
- case TARGET_OBJECT_AUXV:
- #if defined (PT_IO) && defined (PIOD_READ_AUXV)
-
- {
- struct ptrace_io_desc piod;
- if (writebuf)
- return TARGET_XFER_E_IO;
- piod.piod_op = PIOD_READ_AUXV;
- piod.piod_addr = readbuf;
- piod.piod_offs = (void *) (long) offset;
- piod.piod_len = len;
- errno = 0;
- if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
- {
-
- *xfered_len = piod.piod_len;
- return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
- }
- }
- #endif
- return TARGET_XFER_E_IO;
- case TARGET_OBJECT_WCOOKIE:
- return TARGET_XFER_E_IO;
- default:
- return TARGET_XFER_E_IO;
- }
- }
- static int
- inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
- {
-
- return (kill (ptid_get_pid (ptid), 0) != -1);
- }
- static void
- inf_ptrace_files_info (struct target_ops *ignore)
- {
- struct inferior *inf = current_inferior ();
- printf_filtered (_("\tUsing the running image of %s %s.\n"),
- inf->attach_flag ? "attached" : "child",
- target_pid_to_str (inferior_ptid));
- }
- static char *
- inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
- {
- return normal_pid_to_str (ptid);
- }
- #if defined (PT_IO) && defined (PIOD_READ_AUXV)
- static int
- inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
- gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
- {
- struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
- struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
- const int sizeof_auxv_type = TYPE_LENGTH (int_type);
- const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
- enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
- gdb_byte *ptr = *readptr;
- if (endptr == ptr)
- return 0;
- if (endptr - ptr < 2 * sizeof_auxv_val)
- return -1;
- *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
- ptr += sizeof_auxv_val;
- *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
- ptr += sizeof_auxv_val;
- *readptr = ptr;
- return 1;
- }
- #endif
- struct target_ops *
- inf_ptrace_target (void)
- {
- struct target_ops *t = inf_child_target ();
- t->to_attach = inf_ptrace_attach;
- t->to_detach = inf_ptrace_detach;
- t->to_resume = inf_ptrace_resume;
- t->to_wait = inf_ptrace_wait;
- t->to_files_info = inf_ptrace_files_info;
- t->to_kill = inf_ptrace_kill;
- t->to_create_inferior = inf_ptrace_create_inferior;
- #ifdef PT_GET_PROCESS_STATE
- t->to_follow_fork = inf_ptrace_follow_fork;
- t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
- t->to_post_attach = inf_ptrace_post_attach;
- #endif
- t->to_mourn_inferior = inf_ptrace_mourn_inferior;
- t->to_thread_alive = inf_ptrace_thread_alive;
- t->to_pid_to_str = inf_ptrace_pid_to_str;
- t->to_stop = inf_ptrace_stop;
- t->to_xfer_partial = inf_ptrace_xfer_partial;
- #if defined (PT_IO) && defined (PIOD_READ_AUXV)
- t->to_auxv_parse = inf_ptrace_auxv_parse;
- #endif
- return t;
- }
- static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
- static void
- inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
- {
- struct gdbarch *gdbarch = get_regcache_arch (regcache);
- CORE_ADDR addr;
- size_t size;
- PTRACE_TYPE_RET *buf;
- int pid, i;
-
- addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
- if (addr == (CORE_ADDR)-1
- || gdbarch_cannot_fetch_register (gdbarch, regnum))
- {
- regcache_raw_supply (regcache, regnum, NULL);
- return;
- }
-
- pid = ptid_get_lwp (inferior_ptid);
- if (pid == 0)
- pid = ptid_get_pid (inferior_ptid);
- size = register_size (gdbarch, regnum);
- gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
- buf = alloca (size);
-
- for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
- {
- errno = 0;
- buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
- if (errno != 0)
- error (_("Couldn't read register %s (#%d): %s."),
- gdbarch_register_name (gdbarch, regnum),
- regnum, safe_strerror (errno));
- addr += sizeof (PTRACE_TYPE_RET);
- }
- regcache_raw_supply (regcache, regnum, buf);
- }
- static void
- inf_ptrace_fetch_registers (struct target_ops *ops,
- struct regcache *regcache, int regnum)
- {
- if (regnum == -1)
- for (regnum = 0;
- regnum < gdbarch_num_regs (get_regcache_arch (regcache));
- regnum++)
- inf_ptrace_fetch_register (regcache, regnum);
- else
- inf_ptrace_fetch_register (regcache, regnum);
- }
- static void
- inf_ptrace_store_register (const struct regcache *regcache, int regnum)
- {
- struct gdbarch *gdbarch = get_regcache_arch (regcache);
- CORE_ADDR addr;
- size_t size;
- PTRACE_TYPE_RET *buf;
- int pid, i;
-
- addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
- if (addr == (CORE_ADDR)-1
- || gdbarch_cannot_store_register (gdbarch, regnum))
- return;
-
- pid = ptid_get_lwp (inferior_ptid);
- if (pid == 0)
- pid = ptid_get_pid (inferior_ptid);
- size = register_size (gdbarch, regnum);
- gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
- buf = alloca (size);
-
- regcache_raw_collect (regcache, regnum, buf);
- for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
- {
- errno = 0;
- ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
- if (errno != 0)
- error (_("Couldn't write register %s (#%d): %s."),
- gdbarch_register_name (gdbarch, regnum),
- regnum, safe_strerror (errno));
- addr += sizeof (PTRACE_TYPE_RET);
- }
- }
- static void
- inf_ptrace_store_registers (struct target_ops *ops,
- struct regcache *regcache, int regnum)
- {
- if (regnum == -1)
- for (regnum = 0;
- regnum < gdbarch_num_regs (get_regcache_arch (regcache));
- regnum++)
- inf_ptrace_store_register (regcache, regnum);
- else
- inf_ptrace_store_register (regcache, regnum);
- }
- struct target_ops *
- inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
- (struct gdbarch *, int, int))
- {
- struct target_ops *t = inf_ptrace_target();
- gdb_assert (register_u_offset);
- inf_ptrace_register_u_offset = register_u_offset;
- t->to_fetch_registers = inf_ptrace_fetch_registers;
- t->to_store_registers = inf_ptrace_store_registers;
- return t;
- }