static void store_register (const struct regcache *regcache, int regno) { struct gdbarch *gdbarch = get_regcache_arch (regcache); long regaddr, val; int i; int tid; char buf[MAX_REGISTER_SIZE]; /* Overload thread id onto process id. */ tid = TIDGET (inferior_ptid); if (tid == 0) tid = PIDGET (inferior_ptid); /* no thread id, just use process id. */ regaddr = 4 * regmap[regno]; /* Put the contents of regno into a local buffer. */ regcache_raw_collect (regcache, regno, buf); /* Store the local buffer into the inferior a chunk at the time. */ for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long)) { errno = 0; memcpy (&val, &buf[i], sizeof (long)); ptrace (PTRACE_POKEUSER, tid, regaddr, val); regaddr += sizeof (long); if (errno != 0) error (_("Couldn't write register %s (#%d): %s."), gdbarch_register_name (gdbarch, regno), regno, safe_strerror (errno)); } }
static ptid_t thread_to_lwp (ptid_t thread_id, int default_lwp) { td_thrinfo_t ti; td_thrhandle_t th; td_err_e val; if (is_lwp (thread_id)) return thread_id; /* It's already an LWP id */ /* It's a thread. Convert to lwp */ val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th); if (val == TD_NOTHR) return pid_to_ptid (-1); /* thread must have terminated */ else if (val != TD_OK) error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val)); val = p_td_thr_get_info (&th, &ti); if (val == TD_NOTHR) return pid_to_ptid (-1); /* thread must have terminated */ else if (val != TD_OK) error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val)); if (ti.ti_state != TD_THR_ACTIVE) { if (default_lwp != -1) return pid_to_ptid (default_lwp); error ("thread_to_lwp: thread state not active: %s", td_state_string (ti.ti_state)); } return BUILD_LWP (ti.ti_lid, PIDGET (thread_id)); }
static void fetch_register (struct regcache *regcache, int regno) { struct gdbarch *gdbarch = get_regcache_arch (regcache); long regaddr, val; int i; char buf[MAX_REGISTER_SIZE]; int tid; /* Overload thread id onto process id. */ tid = TIDGET (inferior_ptid); if (tid == 0) tid = PIDGET (inferior_ptid); /* no thread id, just use process id. */ regaddr = 4 * regmap[regno]; for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long)) { errno = 0; val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0); memcpy (&buf[i], &val, sizeof (long)); regaddr += sizeof (long); if (errno != 0) error (_("Couldn't read register %s (#%d): %s."), gdbarch_register_name (gdbarch, regno), regno, safe_strerror (errno)); } regcache_raw_supply (regcache, regno, buf); }
static void fetch_fp_register (int regno) { struct fpreg inferior_fp_registers; #ifndef CROSS_DEBUGGER int ret; ret = ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0); if (ret < 0) { warning ("unable to fetch floating-point register"); return; } #endif switch (regno) { case ARM_FPS_REGNUM: supply_register (ARM_FPS_REGNUM, (char *) &inferior_fp_registers.fpr_fpsr); break; default: supply_register (regno, (char *) &inferior_fp_registers.fpr[regno - ARM_F0_REGNUM]); break; } }
void store_inferior_registers (int regnum) { struct reg regs; if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) ®s, 0) == -1) perror_with_name ("Couldn't get registers"); ppcobsd_collect_gregset (&ppcobsd_gregset, current_regcache, regnum, ®s, sizeof regs); if (ptrace (PT_SETREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) ®s, 0) == -1) perror_with_name ("Couldn't write registers"); }
static void sol_thread_detach (char *args, int from_tty) { inferior_ptid = pid_to_ptid (PIDGET (main_ph.ptid)); unpush_target (&sol_thread_ops); procfs_ops.to_detach (args, from_tty); }
static void pass_signal (int signo) { #ifndef _WIN32 kill (PIDGET (inferior_ptid), SIGINT); #endif }
static void fetch_fp_register (struct regcache *regcache, int regno) { struct fpreg inferior_fp_registers; int ret; ret = ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) &inferior_fp_registers, TIDGET (inferior_ptid)); if (ret < 0) { warning (_("unable to fetch floating-point register")); return; } switch (regno) { case ARM_FPS_REGNUM: regcache_raw_supply (regcache, ARM_FPS_REGNUM, (char *) &inferior_fp_registers.fpr_fpsr); break; default: regcache_raw_supply (regcache, regno, (char *) &inferior_fp_registers.fpr[regno - ARM_F0_REGNUM]); break; } }
static void i386bsd_fetch_inferior_registers (struct target_ops *ops, struct regcache *regcache, int regnum) { if (regnum == -1 || GETREGS_SUPPLIES (regnum)) { struct reg regs; if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) ®s, 0) == -1) perror_with_name (_("Couldn't get registers")); i386bsd_supply_gregset (regcache, ®s); if (regnum != -1) return; } if (regnum == -1 || regnum >= I386_ST0_REGNUM) { struct fpreg fpregs; #ifdef HAVE_PT_GETXMMREGS char xmmregs[512]; if (have_ptrace_xmmregs != 0 && ptrace(PT_GETXMMREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) xmmregs, 0) == 0) { have_ptrace_xmmregs = 1; i387_supply_fxsave (regcache, -1, xmmregs); } else { if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) perror_with_name (_("Couldn't get floating point status")); i387_supply_fsave (regcache, -1, &fpregs); } #else if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) perror_with_name (_("Couldn't get floating point status")); i387_supply_fsave (regcache, -1, &fpregs); #endif } }
int get_thread_id (ptid_t ptid) { int tid = TIDGET (ptid); if (0 == tid) tid = PIDGET (ptid); return tid; }
char * child_pid_to_str (ptid_t ptid) { static char buf[40]; sprintf (buf, "process %d thread %d", PIDGET (ptid), TIDGET (ptid)); return buf; }
void fetch_inferior_registers (int regno) { struct reg inferior_registers; struct fpreg inferior_fp_registers; ptrace (PT_GETREGS, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) & inferior_registers, 0); memcpy (®isters[REGISTER_BYTE (0)], &inferior_registers, sizeof (inferior_registers)); ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0); memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, sizeof (inferior_fp_registers)); registers_fetched (); }
/* Store register REGNO back into the child process. If REGNO is -1, do this for all registers (including the floating point and SSE registers). */ static void i386_linux_store_inferior_registers (struct target_ops *ops, struct regcache *regcache, int regno) { int tid; /* Use the old method of poking around in `struct user' if the SETREGS request isn't available. */ if (!have_ptrace_getregs) { int i; for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) if (regno == -1 || regno == i) store_register (regcache, i); return; } /* GNU/Linux LWP ID's are process ID's. */ tid = TIDGET (inferior_ptid); if (tid == 0) tid = PIDGET (inferior_ptid); /* Not a threaded program. */ /* Use the PTRACE_SETFPXREGS requests whenever possible, since it transfers more registers in one system call. But remember that store_fpxregs can fail, and return zero. */ if (regno == -1) { store_regs (regcache, tid, regno); if (store_fpxregs (regcache, tid, regno)) return; store_fpregs (regcache, tid, regno); return; } if (GETREGS_SUPPLIES (regno)) { store_regs (regcache, tid, regno); return; } if (GETFPXREGS_SUPPLIES (regno)) { if (store_fpxregs (regcache, tid, regno)) return; /* Either our processor or our kernel doesn't support the SSE registers, so just write the FP registers in the traditional way. */ store_fpregs (regcache, tid, regno); return; } internal_error (__FILE__, __LINE__, _("Got request to store bad register number %d."), regno); }
static ptid_t child_wait (ptid_t ptid, struct target_waitstatus *ourstatus) { int save_errno; int status; char *execd_pathname = NULL; int exit_status; int syscall_id; enum target_waitkind kind; int pid; do { set_sigint_trap (); /* Causes SIGINT to be passed on to the attached process. */ set_sigio_trap (); pid = wait (&status); save_errno = errno; clear_sigio_trap (); clear_sigint_trap (); if (pid == -1) { if (save_errno == EINTR) continue; fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n", safe_strerror (save_errno)); /* Claim it exited with unknown signal. */ ourstatus->kind = TARGET_WAITKIND_SIGNALLED; ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; return pid_to_ptid (-1); } /* Did it exit? */ if (target_has_exited (pid, status, &exit_status)) { /* ??rehrauer: For now, ignore this. */ continue; } if (!target_thread_alive (pid_to_ptid (pid))) { ourstatus->kind = TARGET_WAITKIND_SPURIOUS; return pid_to_ptid (pid); } } while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */ store_waitstatus (ourstatus, status); return pid_to_ptid (pid); }
/* Find the TID for the current inferior thread to use with ptrace. */ static int s390_inferior_tid (void) { /* GNU/Linux LWP ID's are process ID's. */ int tid = TIDGET (inferior_ptid); if (tid == 0) tid = PIDGET (inferior_ptid); /* Not a threaded program. */ return tid; }
void fetch_inferior_registers (int regno) { int reglo, reghi; int i; unsigned long ecp; if (regno == -1) { reglo = 0; reghi = NUM_REGS - 1; } else reglo = reghi = regno; ecp = registers_addr (PIDGET (inferior_ptid)); { char buf[MAX_REGISTER_SIZE]; for (regno = reglo; regno <= reghi; regno++) { int ptrace_fun = PTRACE_PEEKTHREAD; #ifdef M68K ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD; #endif for (i = 0; i < DEPRECATED_REGISTER_RAW_SIZE (regno); i += sizeof (int)) { unsigned int reg; errno = 0; reg = ptrace (ptrace_fun, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), 0); if (errno) perror_with_name ("ptrace(PTRACE_PEEKUSP)"); *(int *) &buf[i] = reg; } supply_register (regno, buf); } } }
char * hpux_pid_to_str (ptid_t ptid) { static char buf[100]; int pid = PIDGET (ptid); sprintf (buf, "Thread %ld", ptid_get_tid (ptid)); return buf; }
void store_inferior_registers (int regno) { int reglo, reghi; int i; unsigned long ecp; if (regno == -1) { reglo = 0; reghi = NUM_REGS - 1; } else reglo = reghi = regno; ecp = registers_addr (PIDGET (inferior_ptid)); for (regno = reglo; regno <= reghi; regno++) { int ptrace_fun = PTRACE_POKEUSER; if (CANNOT_STORE_REGISTER (regno)) continue; #ifdef M68K ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER; #endif for (i = 0; i < DEPRECATED_REGISTER_RAW_SIZE (regno); i += sizeof (int)) { unsigned int reg; reg = *(unsigned int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno) + i]; errno = 0; ptrace (ptrace_fun, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), reg); if (errno) perror_with_name ("ptrace(PTRACE_POKEUSP)"); } } }
static void store_debug_register (ptid_t ptid, int idx, long val) { int tid; tid = TIDGET (ptid); if (tid == 0) tid = PIDGET (ptid); (void) ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) (PT_DBR + 8 * idx), val); }
LONGEST sparc_xfer_wcookie (struct target_ops *ops, enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, LONGEST len) { unsigned long wcookie = 0; char *buf = (char *)&wcookie; gdb_assert (object == TARGET_OBJECT_WCOOKIE); gdb_assert (readbuf && writebuf == NULL); if (offset == sizeof (unsigned long)) return 0; /* Signal EOF. */ if (offset > sizeof (unsigned long)) return -1; #ifdef PT_WCOOKIE /* If PT_WCOOKIE is defined (by <sys/ptrace.h>), assume we're running on an OpenBSD release that uses StackGhost (3.1 or later). Since release 3.6, OpenBSD uses a fully randomized cookie. */ { int pid; pid = TIDGET (inferior_ptid); if (pid == 0) pid = PIDGET (inferior_ptid); /* Sanity check. The proper type for a cookie is register_t, but we can't assume that this type exists on all systems supported by the code in this file. */ gdb_assert (sizeof (wcookie) == sizeof (register_t)); /* Fetch the cookie. */ if (ptrace (PT_WCOOKIE, pid, (PTRACE_TYPE_ARG3) &wcookie, 0) == -1) { if (errno != EINVAL) perror_with_name (_("Couldn't get StackGhost cookie")); /* Although PT_WCOOKIE is defined on OpenBSD 3.1 and later, the request wasn't implemented until after OpenBSD 3.4. If the kernel doesn't support the PT_WCOOKIE request, assume we're running on a kernel that uses non-randomized cookies. */ wcookie = 0x3; } } #endif /* PT_WCOOKIE */ if (len > sizeof (unsigned long) - offset) len = sizeof (unsigned long) - offset; memcpy (readbuf, buf + offset, len); return len; }
void sparc_fetch_inferior_registers (struct target_ops *ops, struct regcache *regcache, int regnum) { struct gdbarch *gdbarch = get_regcache_arch (regcache); int pid; /* NOTE: cagney/2002-12-03: This code assumes that the currently selected light weight processes' registers can be written directly into the selected thread's register cache. This works fine when given an 1:1 LWP:thread model (such as found on GNU/Linux) but will, likely, have problems when used on an N:1 (userland threads) or N:M (userland multiple LWP) model. In the case of the latter two, the LWP's registers do not necessarily belong to the selected thread (the LWP could be in the middle of executing the thread switch code). These functions should instead be paramaterized with an explicit object (struct regcache, struct thread_info?) into which the LWPs registers can be written. */ pid = TIDGET (inferior_ptid); if (pid == 0) pid = PIDGET (inferior_ptid); if (regnum == SPARC_G0_REGNUM) { gdb_byte zero[8] = { 0 }; regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero); return; } if (regnum == -1 || sparc_gregset_supplies_p (gdbarch, regnum)) { gregset_t regs; if (ptrace (PTRACE_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) perror_with_name (_("Couldn't get registers")); sparc_supply_gregset (sparc_gregset, regcache, -1, ®s); if (regnum != -1) return; } if (regnum == -1 || sparc_fpregset_supplies_p (gdbarch, regnum)) { fpregset_t fpregs; if (ptrace (PTRACE_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) perror_with_name (_("Couldn't get floating point status")); sparc_supply_fpregset (regcache, -1, &fpregs); } }
static const struct target_desc * i386_linux_read_description (struct target_ops *ops) { int tid; static uint64_t xcr0; /* GNU/Linux LWP ID's are process ID's. */ tid = TIDGET (inferior_ptid); if (tid == 0) tid = PIDGET (inferior_ptid); /* Not a threaded program. */ #ifdef HAVE_PTRACE_GETFPXREGS if (have_ptrace_getfpxregs == -1) { elf_fpxregset_t fpxregs; if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) { have_ptrace_getfpxregs = 0; have_ptrace_getregset = 0; return tdesc_i386_mmx_linux; } } #endif if (have_ptrace_getregset == -1) { uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))]; struct iovec iov; iov.iov_base = xstateregs; iov.iov_len = sizeof (xstateregs); /* Check if PTRACE_GETREGSET works. */ if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, &iov) < 0) have_ptrace_getregset = 0; else { have_ptrace_getregset = 1; /* Get XCR0 from XSAVE extended state. */ xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET / sizeof (long long))]; } } /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ if (have_ptrace_getregset && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK) return tdesc_i386_avx_linux; else return tdesc_i386_linux; }
static void vaxbsd_fetch_inferior_registers (struct regcache *regcache, int regnum) { struct reg regs; if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) ®s, 0) == -1) perror_with_name (_("Couldn't get registers")); vaxbsd_supply_gregset (regcache, ®s); }
static void i386bsd_dr_set (int regnum, unsigned int value) { struct dbreg dbregs; if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) &dbregs, 0) == -1) perror_with_name (_("Couldn't get debug registers")); /* For some mysterious reason, some of the reserved bits in the debug control register get set. Mask these off, otherwise the ptrace call below will fail. */ DBREG_DRX ((&dbregs), 7) &= ~(0x0000fc00); DBREG_DRX ((&dbregs), regnum) = value; if (ptrace (PT_SETDBREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) &dbregs, 0) == -1) perror_with_name (_("Couldn't write debug registers")); }
static unsigned long i386bsd_dr_get (ptid_t ptid, int regnum) { struct dbreg dbregs; if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) &dbregs, 0) == -1) perror_with_name (_("Couldn't read debug registers")); return DBREG_DRX ((&dbregs), regnum); }
void reinit_frame_cache (void) { flush_cached_frames (); /* FIXME: The inferior_ptid test is wrong if there is a corefile. */ if (PIDGET (inferior_ptid) != 0) { select_frame (get_current_frame (), 0); } }
static void exec_one_dummy_insn (struct regcache *regcache) { #define DUMMY_INSN_ADDR AIX_TEXT_SEGMENT_BASE+0x200 struct gdbarch *gdbarch = get_regcache_arch (regcache); int ret, status, pid; CORE_ADDR prev_pc; void *bp; /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that this address will never be executed again by the real code. */ bp = deprecated_insert_raw_breakpoint (gdbarch, NULL, DUMMY_INSN_ADDR); /* You might think this could be done with a single ptrace call, and you'd be correct for just about every platform I've ever worked on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up -- the inferior never hits the breakpoint (it's also worth noting powerpc-ibm-aix4.1.3 works correctly). */ prev_pc = regcache_read_pc (regcache); regcache_write_pc (regcache, DUMMY_INSN_ADDR); if (ARCH64 ()) ret = rs6000_ptrace64 (PT_CONTINUE, PIDGET (inferior_ptid), 1, 0, NULL); else ret = rs6000_ptrace32 (PT_CONTINUE, PIDGET (inferior_ptid), (int *) 1, 0, NULL); if (ret != 0) perror (_("pt_continue")); do { pid = waitpid (PIDGET (inferior_ptid), &status, 0); } while (pid != PIDGET (inferior_ptid)); regcache_write_pc (regcache, prev_pc); deprecated_remove_raw_breakpoint (gdbarch, bp); }
ps_err_e ps_lgetxmmregs (struct ps_prochandle *ph, lwpid_t lwpid, char *xmmregs) { struct cleanup *old_chain; old_chain = save_inferior_ptid (); inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); target_fetch_registers (-1); i387_fill_fxsave (xmmregs, -1); do_cleanups (old_chain); return PS_OK; }
static void m68k_linux_fetch_inferior_registers (struct target_ops *ops, struct regcache *regcache, int regno) { int tid; /* Use the old method of peeking around in `struct user' if the GETREGS request isn't available. */ if (! have_ptrace_getregs) { old_fetch_inferior_registers (regcache, regno); return; } /* GNU/Linux LWP ID's are process ID's. */ tid = TIDGET (inferior_ptid); if (tid == 0) tid = PIDGET (inferior_ptid); /* Not a threaded program. */ /* Use the PTRACE_GETFPXREGS request whenever possible, since it transfers more registers in one system call, and we'll cache the results. But remember that fetch_fpxregs can fail, and return zero. */ if (regno == -1) { fetch_regs (regcache, tid); /* The call above might reset `have_ptrace_getregs'. */ if (! have_ptrace_getregs) { old_fetch_inferior_registers (regcache, -1); return; } fetch_fpregs (regcache, tid); return; } if (getregs_supplies (regno)) { fetch_regs (regcache, tid); return; } if (getfpregs_supplies (regno)) { fetch_fpregs (regcache, tid); return; } internal_error (__FILE__, __LINE__, _("Got request for bad register number %d."), regno); }
ps_err_e ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset) { struct cleanup *old_chain; old_chain = save_inferior_ptid (); inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); target_fetch_registers (-1); fill_fpregset (fpregset, -1); do_cleanups (old_chain); return PS_OK; }