int hppa_require_attach (int pid) { int pt_status; CORE_ADDR pc; CORE_ADDR pc_addr; unsigned int regs_offset; /* Are we already attached? There appears to be no explicit way to answer this via ptrace, so we try something which should be innocuous if we are attached. If that fails, then we assume we're not attached, and so attempt to make it so. */ errno = 0; regs_offset = U_REGS_OFFSET; pc_addr = register_addr (PC_REGNUM, regs_offset); pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0); if (errno) { errno = 0; pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); if (errno) return -1; /* Now we really are attached. */ errno = 0; } attach_flag = 1; return pid; }
int hpux_has_execd (int pid, char **execd_pathname) { /* This request is only available on HPUX 10.0 and later. */ #if !defined(PT_GET_PROCESS_STATE) *execd_pathname = NULL; return 0; #else int pt_status; ptrace_state_t ptrace_state; errno = 0; pt_status = call_ptrace (PT_GET_PROCESS_STATE, pid, (PTRACE_ARG3_TYPE) & ptrace_state, sizeof (ptrace_state)); if (errno) perror_with_name ("ptrace"); if (pt_status < 0) return 0; if (ptrace_state.pe_report_event & PTRACE_EXEC) { char *exec_file = target_pid_to_exec_file (pid); *execd_pathname = savestring (exec_file, strlen (exec_file)); return 1; } return 0; #endif }
int hpux_has_vforked (int pid, int *childpid) { /* This request is only available on HPUX 10.0 and later. */ #if !defined(PT_GET_PROCESS_STATE) *childpid = 0; return 0; #else int pt_status; ptrace_state_t ptrace_state; errno = 0; pt_status = call_ptrace (PT_GET_PROCESS_STATE, pid, (PTRACE_ARG3_TYPE) & ptrace_state, sizeof (ptrace_state)); if (errno) perror_with_name ("ptrace"); if (pt_status < 0) return 0; if (ptrace_state.pe_report_event & PTRACE_VFORK) { *childpid = ptrace_state.pe_other_pid; return 1; } return 0; #endif }
int hppa_require_detach (int pid, int signal) { errno = 0; call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal); errno = 0; /* Ignore any errors. */ return pid; }
void next_inferior_resume_ptrace (next_inferior_status *s, int nsignal, int val) { CHECK (s != NULL); CHECK ((val == PTRACE_DETACH) || (val == PTRACE_CONT)); CHECK (s->attached_in_ptrace); CHECK (next_inferior_valid (s)); next_inferior_check_stopped (s); CHECK (s->stopped_in_ptrace); next_inferior_suspend_mach (s); if (call_ptrace(val, s->pid, (caddr_t)1, nsignal) != 0) { error("Error calling ptrace (%d, %d, %d, %d): %s\n", val, s->pid, 1, nsignal, xstrerror(errno)); } s->stopped_in_ptrace = 0; if (val == PTRACE_DETACH) { s->attached_in_ptrace = 0; } }
void require_notification_of_exec_events (int pid) { #if defined(PT_SET_EVENT_MASK) int pt_status; ptrace_event_t ptrace_events; /* Instruct the kernel as to the set of events we wish to be informed of. (This support does not exist before HPUX 10.0. We'll assume if PT_SET_EVENT_MASK has not been defined by <sys/ptrace.h>, then we're being built on pre-10.0.) */ memset (&ptrace_events, 0, sizeof (ptrace_events)); /* Note: By default, all signals are visible to us. If we wish the kernel to keep certain signals hidden from us, we do it by calling sigdelset (ptrace_events.pe_signals, signal) for each such signal here, before doing PT_SET_EVENT_MASK. */ sigemptyset (&ptrace_events.pe_signals); ptrace_events.pe_set_event = 0; ptrace_events.pe_set_event |= PTRACE_EXEC; /* ??rehrauer: Add this one when we're prepared to catch it... ptrace_events.pe_set_event |= PTRACE_EXIT; */ errno = 0; pt_status = call_ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_ARG3_TYPE) & ptrace_events, sizeof (ptrace_events)); if (errno) perror_with_name ("ptrace"); if (pt_status < 0) return; #endif }
void require_notification_of_events (int pid) { #if defined(PT_SET_EVENT_MASK) int pt_status; ptrace_event_t ptrace_events; int nsigs; int signum; /* Instruct the kernel as to the set of events we wish to be informed of. (This support does not exist before HPUX 10.0. We'll assume if PT_SET_EVENT_MASK has not been defined by <sys/ptrace.h>, then we're being built on pre-10.0.) */ memset (&ptrace_events, 0, sizeof (ptrace_events)); /* Note: By default, all signals are visible to us. If we wish the kernel to keep certain signals hidden from us, we do it by calling sigdelset (ptrace_events.pe_signals, signal) for each such signal here, before doing PT_SET_EVENT_MASK. */ /* RM: The above comment is no longer true. We start with ignoring all signals, and then add the ones we are interested in. We could do it the other way: start by looking at all signals and then deleting the ones that we aren't interested in, except that multiple gdb signals may be mapped to the same host signal (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to signal 22 on HPUX 10.20) We want to be notified if we are interested in either signal. */ sigfillset (&ptrace_events.pe_signals); /* RM: Let's not bother with signals we don't care about */ nsigs = (int) TARGET_SIGNAL_LAST; for (signum = nsigs; signum > 0; signum--) { if ((signal_stop_state (signum)) || (signal_print_state (signum)) || (!signal_pass_state (signum))) { if (target_signal_to_host_p (signum)) sigdelset (&ptrace_events.pe_signals, target_signal_to_host (signum)); } } ptrace_events.pe_set_event = 0; ptrace_events.pe_set_event |= PTRACE_SIGNAL; ptrace_events.pe_set_event |= PTRACE_EXEC; ptrace_events.pe_set_event |= PTRACE_FORK; ptrace_events.pe_set_event |= PTRACE_VFORK; /* ??rehrauer: Add this one when we're prepared to catch it... ptrace_events.pe_set_event |= PTRACE_EXIT; */ errno = 0; pt_status = call_ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_ARG3_TYPE) & ptrace_events, sizeof (ptrace_events)); if (errno) perror_with_name ("ptrace"); if (pt_status < 0) return; #endif }
int ptrace_traceme(void){ return call_ptrace(PT_TRACE_ME,0,0,0); }
int ptrace_continue(uintptr_t pid, int sig){ return call_ptrace(PT_CONTINUE, pid, 1, sig); }
int ptrace_write(uintptr_t pid, uintptr_t addr, int val){ return call_ptrace(PT_WRITE_D,pid,addr,val); }
int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, struct mem_attrib *mem, struct target_ops *target) { int i; /* Round starting address down to longword boundary. */ CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int)); /* Round ending address up; get number of longwords that makes. */ int count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); /* Allocate buffer of that many longwords. Note -- do not use alloca to allocate this buffer since there is no guarantee of when the buffer will actually be deallocated. This routine can be called over and over with the same call chain; this (in effect) would pile up all those alloca requests until a call to alloca was made from a point higher than this routine in the call chain. */ int *buffer = (int *) xmalloc (count * sizeof (int)); if (write) { /* Fill start and end extra bytes of buffer with existing memory data. */ if (addr != memaddr || len < (int) sizeof (int)) { /* Need part of initial word -- fetch it. */ buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) addr, 0); } if (count > 1) /* FIXME, avoid if even boundary */ { buffer[count - 1] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)), 0); } /* Copy data to be written over corresponding part of buffer */ memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); /* Write the entire buffer. */ for (i = 0; i < count; i++, addr += sizeof (int)) { int pt_status; int pt_request; /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text segment. FIXME -- does it work to write into the data segment using WIUSER, or do these idiots really expect us to figure out which segment the address is in, so we can use a separate system call for it??! */ errno = 0; pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER; pt_status = call_ptrace (pt_request, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) addr, buffer[i]); /* Did we fail? Might we've guessed wrong about which segment this address resides in? Try the other request, and see if that works... */ if ((pt_status == -1) && errno) { errno = 0; pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER; pt_status = call_ptrace (pt_request, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) addr, buffer[i]); /* No, we still fail. Okay, time to punt. */ if ((pt_status == -1) && errno) { xfree (buffer); return 0; } } } } else { /* Read all the longwords */ for (i = 0; i < count; i++, addr += sizeof (int)) { errno = 0; buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) addr, 0); if (errno) { xfree (buffer); return 0; } QUIT; } /* Copy appropriate bytes out of the buffer. */ memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); } xfree (buffer); return len; }
void store_inferior_registers (int regno) { unsigned int regaddr; char buf[80]; int i; unsigned int offset = U_REGS_OFFSET; int scratch; if (regno >= 0) { unsigned int addr, len, offset; if (CANNOT_STORE_REGISTER (regno)) return; offset = 0; len = DEPRECATED_REGISTER_RAW_SIZE (regno); /* Requests for register zero actually want the save_state's ss_flags member. As RM says: "Oh, what a hack!" */ if (regno == 0) { save_state_t ss; addr = HPPAH_OFFSETOF (save_state_t, ss_flags); len = sizeof (ss.ss_flags); /* Note that ss_flags is always an int, no matter what DEPRECATED_REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines are big-endian, put it at the least significant end of the value, and zap the rest of the buffer. */ offset = DEPRECATED_REGISTER_RAW_SIZE (0) - len; } /* Floating-point registers come from the ss_fpblock area. */ else if (regno >= HPPA_FP0_REGNUM) addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (HPPA_FP0_REGNUM))); /* Wide registers come from the ss_wide area. I think it's more PC to test (ss_flags & SS_WIDEREGS) to select between ss_wide and ss_narrow than to use the raw register size. But checking ss_flags would require an extra ptrace call for every register reference. Bleah. */ else if (len == 8) addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) + DEPRECATED_REGISTER_BYTE (regno)); /* Narrow registers come from the ss_narrow area. Note that ss_narrow starts with gr1, not gr0. */ else if (len == 4) addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow) + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1))); else internal_error (__FILE__, __LINE__, "hppah-nat.c (write_register): unexpected register size"); #ifdef GDB_TARGET_IS_HPPA_20W /* Unbelieveable. The PC head and tail must be written in 64bit hunks or we will get an error. Worse yet, the oddball ptrace/ttrace layering will not allow us to perform a 64bit register store. What a crock. */ if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM && len == 8) { CORE_ADDR temp; temp = *(CORE_ADDR *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)]; /* Set the priv level (stored in the low two bits of the PC. */ temp |= 0x3; ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr, (CORE_ADDR)&temp); /* If we fail to write the PC, give a true error instead of just a warning. */ if (errno != 0) { char *err = safe_strerror (errno); char *msg = alloca (strlen (err) + 128); sprintf (msg, "writing `%s' register: %s", REGISTER_NAME (regno), err); perror_with_name (msg); } return; } /* Another crock. HPUX complains if you write a nonzero value to the high part of IPSW. What will it take for HP to catch a clue about building sensible interfaces? */ if (regno == HPPA_IPSW_REGNUM && len == 8) *(int *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)] = 0; #endif for (i = 0; i < len; i += sizeof (int)) { errno = 0; call_ptrace (PT_WUREGS, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) addr + i, *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno) + i]); if (errno != 0) { /* Warning, not error, in case we are attached; sometimes the kernel doesn't let us at the registers. */ char *err = safe_strerror (errno); char *msg = alloca (strlen (err) + 128); sprintf (msg, "writing `%s' register: %s", REGISTER_NAME (regno), err); /* If we fail to write the PC, give a true error instead of just a warning. */ if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM) perror_with_name (msg); else warning (msg); return; } } } else for (regno = 0; regno < NUM_REGS; regno++) store_inferior_registers (regno); }
char * child_pid_to_exec_file (int pid) { static char exec_file_buffer[1024]; int pt_status; CORE_ADDR top_of_stack; char four_chars[4]; int name_index; int i; ptid_t saved_inferior_ptid; int done; #ifdef PT_GET_PROCESS_PATHNAME /* As of 10.x HP-UX, there's an explicit request to get the pathname. */ pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME, pid, (PTRACE_ARG3_TYPE) exec_file_buffer, sizeof (exec_file_buffer) - 1); if (pt_status == 0) return exec_file_buffer; #endif /* It appears that this request is broken prior to 10.30. If it fails, try a really, truly amazingly gross hack that DDE uses, of pawing through the process' data segment to find the pathname. */ top_of_stack = 0x7b03a000; name_index = 0; done = 0; /* On the chance that pid != inferior_ptid, set inferior_ptid to pid, so that (grrrr!) implicit uses of inferior_ptid get the right id. */ saved_inferior_ptid = inferior_ptid; inferior_ptid = pid_to_ptid (pid); /* Try to grab a null-terminated string. */ while (!done) { if (target_read_memory (top_of_stack, four_chars, 4) != 0) { inferior_ptid = saved_inferior_ptid; return NULL; } for (i = 0; i < 4; i++) { exec_file_buffer[name_index++] = four_chars[i]; done = (four_chars[i] == '\0'); if (done) break; } top_of_stack += 4; } if (exec_file_buffer[0] == '\0') { inferior_ptid = saved_inferior_ptid; return NULL; } inferior_ptid = saved_inferior_ptid; return exec_file_buffer; }
/* Fetch a register's value from the process's U area. */ static void fetch_register (int regno) { char buf[MAX_REGISTER_SIZE]; unsigned int addr, len, offset; int i; offset = 0; len = DEPRECATED_REGISTER_RAW_SIZE (regno); /* Requests for register zero actually want the save_state's ss_flags member. As RM says: "Oh, what a hack!" */ if (regno == 0) { save_state_t ss; addr = HPPAH_OFFSETOF (save_state_t, ss_flags); len = sizeof (ss.ss_flags); /* Note that ss_flags is always an int, no matter what DEPRECATED_REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines are big-endian, put it at the least significant end of the value, and zap the rest of the buffer. */ offset = DEPRECATED_REGISTER_RAW_SIZE (0) - len; memset (buf, 0, sizeof (buf)); } /* Floating-point registers come from the ss_fpblock area. */ else if (regno >= HPPA_FP0_REGNUM) addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (HPPA_FP0_REGNUM))); /* Wide registers come from the ss_wide area. I think it's more PC to test (ss_flags & SS_WIDEREGS) to select between ss_wide and ss_narrow than to use the raw register size. But checking ss_flags would require an extra ptrace call for every register reference. Bleah. */ else if (len == 8) addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) + DEPRECATED_REGISTER_BYTE (regno)); /* Narrow registers come from the ss_narrow area. Note that ss_narrow starts with gr1, not gr0. */ else if (len == 4) addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow) + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1))); else internal_error (__FILE__, __LINE__, "hppa-nat.c (fetch_register): unexpected register size"); for (i = 0; i < len; i += sizeof (int)) { errno = 0; /* Copy an int from the U area to buf. Fill the least significant end if len != raw_size. */ * (int *) &buf[offset + i] = call_ptrace (PT_RUREGS, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) addr + i, 0); if (errno != 0) { /* Warning, not error, in case we are attached; sometimes the kernel doesn't let us at the registers. */ char *err = safe_strerror (errno); char *msg = alloca (strlen (err) + 128); sprintf (msg, "reading `%s' register: %s", REGISTER_NAME (regno), err); warning (msg); return; } } /* If we're reading an address from the instruction address queue, mask out the bottom two bits --- they contain the privilege level. */ if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM) buf[len - 1] &= ~0x3; supply_register (regno, buf); }
int ptrace_attach(uintptr_t pid){ return call_ptrace(PT_ATTACH, pid, 0, 0); }
int ptrace_singlestep(uintptr_t pid, int sig){ return call_ptrace(PT_STEP, pid, 1, sig); }
int ptrace_read(uintptr_t pid, uintptr_t addr){ return call_ptrace(PT_READ_D,pid,addr,0); }
static void ptrace_me (void) { /* "Trace me, Dr. Memory!" */ call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0); }
int ptrace_setfpregs(uintptr_t pid, void* addr){ return call_ptrace(PT_SETFPREGS, pid, (uintptr_t)addr, 0); }
int ptrace_getregs(uintptr_t pid, void* addr){ return call_ptrace(PT_GETREGS, pid, (uintptr_t)addr, 0); }
int ptrace_detatch(uintptr_t pid){ return call_ptrace(PT_DETACH, pid, 0, 0); }