binutils-gdb/gdb/inf-ptrace.c
Yao Qi ac7936dfd0 s/get_regcache_arch (regcache)/regcache->arch ()/g
This patches removes get_regcache_arch, and use regache->arch () instead.
The motivation of this change is that I am going to move some basic stuff
into a base class of regcache.  I don't need to update "client" code
regcache->arch ().  On the other hand, this patch shortens the code a
little bit.

gdb:

2017-10-25  Yao Qi  <yao.qi@linaro.org>

	* aarch32-linux-nat.c (aarch32_gp_regcache_supply): Use
	regcache->arch () instead get_regcache_arch.
	* aarch64-fbsd-nat.c (aarch64_fbsd_fetch_inferior_registers):
	Likewise.
	(aarch64_fbsd_store_inferior_registers): Likewise.
	* aarch64-linux-nat.c (fetch_gregs_from_thread): Likewise.
	(store_gregs_to_thread): Likewise.
	(fetch_fpregs_from_thread): Likewise.
	(store_fpregs_to_thread): Likewise.
	* aarch64-tdep.c (aarch64_extract_return_value): Likewise.
	(aarch64_store_return_value): Likewise.
	(aarch64_software_single_step): Likewise.
	* aix-thread.c (aix_thread_wait): Likewise.
	(supply_reg32): Likewise.
	(supply_sprs64): Likewise.
	(supply_sprs32): Likewise.
	(fill_gprs64): Likewise.
	(fill_gprs32): Likewise.
	(fill_sprs64): Likewise.
	(fill_sprs32): Likewise.
	(store_regs_user_thread): Likewise.
	(store_regs_kernel_thread): Likewise.
	* alpha-bsd-nat.c (alphabsd_fetch_inferior_registers): Likewise.
	(alphabsd_store_inferior_registers): Likewise.
	* alpha-tdep.c (alpha_extract_return_value): Likewise.
	(alpha_store_return_value): Likewise.
	(alpha_deal_with_atomic_sequence): Likewise.
	(alpha_next_pc): Likewise.
	(alpha_software_single_step): Likewise.
	* amd64-bsd-nat.c (amd64bsd_fetch_inferior_registers): Likewise.
	(amd64bsd_store_inferior_registers): Likewise.
	* amd64-linux-nat.c (amd64_linux_fetch_inferior_registers):
	Likewise.
	(amd64_linux_store_inferior_registers): Likewise.
	* amd64-nat.c (amd64_supply_native_gregset): Likewise.
	(amd64_collect_native_gregset): Likewise.
	* amd64-obsd-tdep.c (amd64obsd_supply_uthread): Likewise.
	(amd64obsd_collect_uthread): Likewise.
	* amd64-tdep.c (amd64_supply_fpregset): Likewise.
	(amd64_collect_fpregset): Likewise.
	(amd64_supply_fxsave): Likewise.
	(amd64_supply_xsave): Likewise.
	(amd64_collect_fxsave): Likewise.
	(amd64_collect_xsave): Likewise.
	* arc-tdep.c (arc_write_pc): Likewise.
	* arch-utils.c (default_skip_permanent_breakpoint): Likewise.
	* arm-fbsd-nat.c (arm_fbsd_fetch_inferior_registers): Likewise.
	(arm_fbsd_store_inferior_registers): Likewise.
	* arm-linux-nat.c (fetch_vfp_regs): Likewise.
	(store_vfp_regs): Likewise.
	(arm_linux_fetch_inferior_registers): Likewise.
	(arm_linux_store_inferior_registers): Likewise.
	* arm-linux-tdep.c (arm_linux_supply_gregset): Likewise.
	(arm_linux_sigreturn_next_pc): Likewise.
	(arm_linux_get_next_pcs_syscall_next_pc): Likewise.
	* arm-nbsd-nat.c (arm_supply_gregset): Likewise.
	(fetch_register): Likewise.
	(store_register): Likewise.
	* arm-tdep.c (arm_is_thumb): Likewise.
	(displaced_in_arm_mode): Likewise.
	(bx_write_pc): Likewise.
	(arm_get_next_pcs_addr_bits_remove): Likewise.
	(arm_software_single_step): Likewise.
	(arm_extract_return_value): Likewise.
	(arm_store_return_value): Likewise.
	(arm_write_pc): Likewise.
	* bfin-tdep.c (bfin_extract_return_value): Likewise.
	* bsd-uthread.c (bsd_uthread_fetch_registers): Likewise.
	(bsd_uthread_store_registers): Likewise.
	* core-regset.c (fetch_core_registers): Likewise.
	* corelow.c (get_core_registers): Likewise.
	* cris-tdep.c (cris_store_return_value): Likewise.
	(cris_extract_return_value): Likewise.
	(find_step_target): Likewise.
	(find_step_target): Likewise.
	(cris_software_single_step): Likewise.
	* ctf.c (ctf_fetch_registers): Likewise.
	* darwin-nat.c (cancel_breakpoint): Likewise.
	* fbsd-tdep.c (fbsd_collect_thread_registers): Likewise.
	* frv-tdep.c (frv_extract_return_value): Likewise.
	* ft32-tdep.c (ft32_store_return_value): Likewise.
	(ft32_extract_return_value): Likewise.
	* go32-nat.c (fetch_register): Likewise.
	(go32_fetch_registers): Likewise.
	(go32_store_registers): Likewise.
	(store_register): Likewise.
	* h8300-tdep.c (h8300_extract_return_value): Likewise.
	(h8300_store_return_value): Likewise.
	* hppa-linux-nat.c (fetch_register): Likewise.
	(store_register): Likewise.
	(hppa_linux_fetch_inferior_registers): Likewise.
	(hppa_linux_store_inferior_registers): Likewise.
	* i386-darwin-nat.c (i386_darwin_fetch_inferior_registers): Likewise.
	(i386_darwin_store_inferior_registers): Likewise.
	* i386-gnu-nat.c (gnu_fetch_registers): Likewise.
	(gnu_store_registers): Likewise.
	* i386-linux-nat.c (fetch_register): Likewise.
	(store_register): Likewise.
	(supply_gregset): Likewise.
	(fill_gregset): Likewise.
	(i386_linux_fetch_inferior_registers): Likewise.
	(i386_linux_store_inferior_registers): Likewise.
	(i386_linux_resume): Likewise.
	* i386-linux-tdep.c (i386_linux_get_syscall_number_from_regcache):
	Likewise.
	* i386-nto-tdep.c (i386nto_supply_gregset): Likewise.
	* i386-obsd-nat.c (i386obsd_supply_pcb): Likewise.
	* i386-obsd-tdep.c (i386obsd_supply_uthread): Likewise.
	(i386obsd_collect_uthread): Likewise.
	* i386-tdep.c (i386_mmx_regnum_to_fp_regnum): Likewise.
	(i386_supply_gregset): Likewise.
	(i386_collect_gregset): Likewise.
	(i386_supply_fpregset): Likewise.
	(i386_collect_fpregset): Likewise.
	(i386_mpx_bd_base): Likewise.
	* i386-v4-nat.c	(supply_fpregset): Likewise.
	(fill_fpregset): Likewise.
	* i387-tdep.c (i387_supply_fsave): Likewise.
	(i387_collect_fsave): Likewise.
	(i387_supply_fxsave): Likewise.
	(i387_collect_fxsave): Likewise.
	(i387_supply_xsave): Likewise.
	(i387_collect_xsave): Likewise.
	* ia64-linux-nat.c (ia64_linux_fetch_registers): Likewise.
	(ia64_linux_store_registers): Likewise.
	* ia64-tdep.c (ia64_access_rse_reg): Likewise.
	(ia64_extract_return_value): Likewise.
	(ia64_store_return_value): Likewise.
	(find_func_descr): Likewise.
	* inf-child.c (inf_child_fetch_inferior_registers): Likewise.
	* inf-ptrace.c (inf_ptrace_fetch_registers): Likewise.
	(inf_ptrace_store_registers): Likewise.
	* infrun.c (use_displaced_stepping): Likewise.
	(displaced_step_prepare_throw): Likewise.
	(resume): Likewise.
	(proceed): Likewise.
	(do_target_wait): Likewise.
	(adjust_pc_after_break): Likewise.
	(handle_inferior_event_1): Likewise.
	(handle_signal_stop): Likewise.
	(save_infcall_suspend_state): Likewise.
	(restore_infcall_suspend_state): Likewise.
	* iq2000-tdep.c (iq2000_extract_return_value): Likewise.
	* jit.c (jit_frame_prev_register): Likewise.
	* linux-nat.c (save_stop_reason): Likewise.
	(linux_nat_wait_1): Likewise.
	(resume_stopped_resumed_lwps): Likewise.
	* linux-record.c (record_linux_sockaddr): Likewise.
	(record_linux_msghdr): Likewise.
	(record_linux_system_call): Likewise.
	* linux-tdep.c (linux_collect_thread_registers): Likewise.
	* lm32-tdep.c (lm32_extract_return_value): Likewise.
	(lm32_store_return_value): Likewise.
	* m32c-tdep.c (m32c_read_flg): Likewise.
	(m32c_pseudo_register_read): Likewise.
	(m32c_pseudo_register_write): Likewise.
	* m32r-linux-tdep.c (m32r_linux_supply_gregset): Likewise.
	(m32r_linux_collect_gregset): Likewise.
	* m32r-tdep.c (m32r_store_return_value): Likewise.
	(m32r_extract_return_value): Likewise.
	* m68k-bsd-nat.c (m68kbsd_supply_fpregset): Likewise.
	(m68kbsd_collect_fpregset): Likewise.
	* m68k-bsd-tdep.c (m68kbsd_supply_fpregset): Likewise.
	* m68k-linux-nat.c (fetch_register): Likewise.
	(old_fetch_inferior_registers): Likewise.
	(old_store_inferior_registers): Likewise.
	(store_regs): Likewise.
	* m68k-tdep.c (m68k_svr4_extract_return_value): Likewise.
	(m68k_svr4_store_return_value): Likewise.
	* m88k-tdep.c (m88k_store_arguments): Likewise.
	* mi/mi-main.c (mi_cmd_data_list_changed_registers): Likewise.
	(mi_cmd_data_write_register_values): Likewise.
	* mips-fbsd-nat.c (mips_fbsd_fetch_inferior_registers): Likewise.
	(mips_fbsd_store_inferior_registers): Likewise.
	* mips-fbsd-tdep.c (mips_fbsd_supply_fpregs): Likewise.
	(mips_fbsd_supply_gregs): Likewise.
	(mips_fbsd_collect_fpregs): Likewise.
	(mips_fbsd_collect_gregs): Likewise.
	(mips_fbsd_supply_fpregset): Likewise.
	(mips_fbsd_collect_fpregset): Likewise.
	(mips_fbsd_supply_gregset): Likewise.
	(mips_fbsd_collect_gregset): Likewise.
	* mips-linux-nat.c (supply_gregset): Likewise.
	(fill_gregset): Likewise.
	(supply_fpregset): Likewise.
	(fill_fpregset): Likewise.
	* mips-linux-tdep.c (mips_supply_gregset): Likewise.
	(mips_fill_gregset): Likewise.
	(mips_supply_fpregset): Likewise.
	(mips_fill_fpregset): Likewise.
	(mips64_supply_gregset): Likewise.
	(micromips_linux_sigframe_validate): Likewise.
	* mips-nbsd-nat.c (mipsnbsd_fetch_inferior_registers): Likewise.
	(mipsnbsd_fetch_inferior_registers): Likewise.
	(mipsnbsd_store_inferior_registers): Likewise.
	* mips-nbsd-tdep.c (mipsnbsd_supply_fpregset): Likewise.
	(mipsnbsd_supply_gregset): Likewise.
	(mipsnbsd_iterate_over_regset_sections): Likewise.
	(mipsnbsd_supply_reg): Likewise.
	(mipsnbsd_supply_fpreg): Likewise.
	* mips-tdep.c (mips_in_frame_stub): Likewise.
	(mips_dummy_id): Likewise.
	(is_octeon_bbit_op): Likewise.
	(micromips_bc1_pc): Likewise.
	(extended_mips16_next_pc): Likewise.
	(mips16_next_pc): Likewise.
	(deal_with_atomic_sequence): Likewise.
	* moxie-tdep.c (moxie_process_readu): Likewise.
	* nios2-tdep.c (nios2_get_next_pc): Likewise.
	* nto-procfs.c (procfs_store_registers): Likewise.
	* ppc-fbsd-nat.c (ppcfbsd_fetch_inferior_registers): Likewise.
	(ppcfbsd_store_inferior_registers): Likewise.
	* ppc-linux-nat.c (fetch_vsx_register): Likewise.
	(fetch_altivec_register): Likewise.
	(get_spe_registers): Likewise.
	(fetch_spe_register): Likewise.
	(fetch_altivec_registers): Likewise.
	(fetch_all_gp_regs): Likewise.
	(fetch_all_fp_regs): Likewise.
	(store_vsx_register): Likewise.
	(store_altivec_register): Likewise.
	(set_spe_registers): Likewise.
	(store_spe_register): Likewise.
	(store_altivec_registers): Likewise.
	(store_all_gp_regs): Likewise.
	(store_all_fp_regs): Likewise.
	* ppc-linux-tdep.c (ppc_linux_supply_gregset): Likewise.
	(ppc_linux_collect_gregset): Likewise.
	(ppc_canonicalize_syscall): Likewise.
	(ppc_linux_record_signal): Likewise.
	(ppu2spu_prev_register): Likewise.
	* ppc-nbsd-nat.c (ppcnbsd_supply_pcb): Likewise.
	* ppc-obsd-nat.c (ppcobsd_fetch_registers): Likewise.
	(ppcobsd_store_registers): Likewise.
	* ppc-ravenscar-thread.c (ppc_ravenscar_generic_fetch_registers):
	Likewise.
	(ppc_ravenscar_generic_store_registers): Likewise.
	* procfs.c (procfs_fetch_registers): Likewise.
	(procfs_store_registers): Likewise.
	* ravenscar-thread.c (ravenscar_fetch_registers): Likewise.
	(ravenscar_store_registers): Likewise.
	(ravenscar_prepare_to_store): Likewise.
	* record-btrace.c (record_btrace_fetch_registers): Likewise.
	* record-full.c (record_full_wait_1): Likewise.
	(record_full_registers_change): Likewise.
	(record_full_store_registers): Likewise.
	(record_full_core_fetch_registers): Likewise.
	(record_full_save): Likewise.
	(record_full_goto_insn): Likewise.
	* regcache.c (regcache_register_size): Likewise.
	(get_regcache_arch): Remove.
	(regcache_read_pc): Likewise.
	* regcache.h (get_regcache_arch): Remove.
	* remote-sim.c (gdbsim_fetch_register): Likewise.
	(gdbsim_store_register): Likewise.
	* remote.c (fetch_register_using_p): Likewise.
	(send_g_packet): Likewise.
	(remote_prepare_to_store): Likewise.
	(store_registers_using_G): Likewise.
	* reverse.c (save_bookmark_command): Likewise.
	(goto_bookmark_command): Likewise.
	* rs6000-aix-tdep.c (branch_dest): Likewise.
	* rs6000-nat.c (rs6000_ptrace64): Likewise.
	(fetch_register): Likewise.
	* rs6000-tdep.c (ppc_supply_reg): Likewise.
	(ppc_collect_reg): Likewise.
	(ppc_collect_gregset): Likewise.
	(ppc_collect_fpregset): Likewise.
	(ppc_collect_vsxregset): Likewise.
	(ppc_collect_vrregset): Likewise.
	(ppc_displaced_step_hw_singlestep): Likewise.
	(rs6000_pseudo_register_read): Likewise.
	(rs6000_pseudo_register_write): Likewise.
	* s390-linux-nat.c (supply_gregset): Likewise.
	(fill_gregset): Likewise.
	(s390_linux_fetch_inferior_registers): Likewise.
	* s390-linux-tdep.c (s390_write_pc): Likewise.
	(s390_software_single_step): Likewise.
	(s390_all_but_pc_registers_record): Likewise.
	(s390_linux_syscall_record): Likewise.
	* sentinel-frame.c (sentinel_frame_prev_arch): Likewise.
	* sh-nbsd-nat.c (shnbsd_fetch_inferior_registers): Likewise.
	(shnbsd_store_inferior_registers): Likewise.
	* sh-tdep.c (sh_extract_return_value_nofpu): Likewise.
	(sh_extract_return_value_fpu): Likewise.
	(sh_store_return_value_nofpu): Likewise.
	(sh_corefile_supply_regset): Likewise.
	(sh_corefile_collect_regset): Likewise.
	* sh64-tdep.c (sh64_extract_return_value): Likewise.
	(sh64_store_return_value): Likewise.
	* sparc-linux-tdep.c (sparc32_linux_collect_core_fpregset): Likewise.
	* sparc-nat.c (sparc_fetch_inferior_registers): Likewise.
	(sparc_store_inferior_registers): Likewise.
	* sparc-ravenscar-thread.c (register_in_thread_descriptor_p): Likewise.
	(sparc_ravenscar_prepare_to_store): Likewise.
	* sparc-tdep.c (sparc32_store_arguments): Likewise.
	(sparc_analyze_control_transfer): Likewise.
	(sparc_step_trap): Likewise.
	(sparc_software_single_step): Likewise.
	(sparc32_gdbarch_init): Likewise.
	(sparc_supply_rwindow): Likewise.
	(sparc_collect_rwindow): Likewise.
	* sparc64-linux-tdep.c (sparc64_linux_collect_core_fpregset): Likewise.
	* sparc64-nbsd-nat.c (sparc64nbsd_supply_gregset): Likewise.
	(sparc64nbsd_collect_gregset): Likewise.
	(sparc64nbsd_supply_fpregset): Likewise.
	(sparc64nbsd_collect_fpregset): Likewise.
	* sparc64-tdep.c (sparc64_store_arguments): Likewise.
	(sparc64_supply_gregset): Likewise.
	(sparc64_collect_gregset): Likewise.
	(sparc64_supply_fpregset): Likewise.
	(sparc64_collect_fpregset): Likewise.
	* spu-linux-nat.c (spu_fetch_inferior_registers): Likewise.
	* spu-tdep.c (spu_unwind_sp): Likewise.
	(spu2ppu_prev_register): Likewise.
	(spu_memory_remove_breakpoint): Likewise.
	* stack.c (return_command): Likewise.
	* tic6x-tdep.c (tic6x_extract_signed_field): Likewise.
	* tracefile-tfile.c (tfile_fetch_registers): Likewise.
	* tracefile.c (trace_save_ctf): Likewise.
	* windows-nat.c (do_windows_fetch_inferior_registers): Likewise.
	(do_windows_store_inferior_registers): Likewise.
	(windows_resume): Likewise.
	* xtensa-linux-nat.c (fill_gregset): Likewise.
	(supply_gregset_reg): Likewise.
	* xtensa-tdep.c (xtensa_register_write_masked): Likewise.
	(xtensa_register_read_masked): Likewise.
	(xtensa_supply_gregset): Likewise.
	(xtensa_extract_return_value): Likewise.
	(xtensa_store_return_value): Likewise.
2017-10-25 16:37:03 +01:00

841 lines
23 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Low-level child interface to ptrace.
Copyright (C) 1988-2017 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "command.h"
#include "inferior.h"
#include "inflow.h"
#include "terminal.h"
#include "gdbcore.h"
#include "regcache.h"
#include "nat/gdb_ptrace.h"
#include "gdb_wait.h"
#include <signal.h>
#include "inf-ptrace.h"
#include "inf-child.h"
#include "gdbthread.h"
#include "nat/fork-inferior.h"
#include "utils.h"
#ifdef PT_GET_PROCESS_STATE
/* Target hook for follow_fork. On entry and at return inferior_ptid is
the ptid of the followed inferior. */
static int
inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
int detach_fork)
{
if (!follow_child)
{
struct thread_info *tp = inferior_thread ();
pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
/* Breakpoints have already been detached from the child by
infrun.c. */
if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
perror_with_name (("ptrace"));
}
return 0;
}
static int
inf_ptrace_insert_fork_catchpoint (struct target_ops *self, int pid)
{
return 0;
}
static int
inf_ptrace_remove_fork_catchpoint (struct target_ops *self, int pid)
{
return 0;
}
#endif /* PT_GET_PROCESS_STATE */
/* Prepare to be traced. */
static void
inf_ptrace_me (void)
{
/* "Trace me, Dr. Memory!" */
if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
trace_start_error_with_name ("ptrace");
}
/* Start a new inferior Unix child process. EXEC_FILE is the file to
run, ALLARGS is a string containing the arguments to the program.
ENV is the environment vector to pass. If FROM_TTY is non-zero, be
chatty about it. */
static void
inf_ptrace_create_inferior (struct target_ops *ops,
const char *exec_file, const std::string &allargs,
char **env, int from_tty)
{
pid_t pid;
ptid_t ptid;
/* Do not change either targets above or the same target if already present.
The reason is the target stack is shared across multiple inferiors. */
int ops_already_pushed = target_is_pushed (ops);
struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
if (! ops_already_pushed)
{
/* Clear possible core file with its process_stratum. */
push_target (ops);
make_cleanup_unpush_target (ops);
}
pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
NULL, NULL, NULL);
ptid = pid_to_ptid (pid);
/* We have something that executes now. We'll be running through
the shell at this point (if startup-with-shell is true), but the
pid shouldn't change. */
add_thread_silent (ptid);
discard_cleanups (back_to);
gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
/* On some targets, there must be some explicit actions taken after
the inferior has been started up. */
target_post_startup_inferior (ptid);
}
#ifdef PT_GET_PROCESS_STATE
static void
inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
{
ptrace_event_t pe;
/* Set the initial event mask. */
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
/* Clean up a rotting corpse of an inferior after it died. */
static void
inf_ptrace_mourn_inferior (struct target_ops *ops)
{
int status;
/* Wait just one more time to collect the inferior's exit status.
Do not check whether this succeeds though, since we may be
dealing with a process that we attached to. Such a process will
only report its exit status to its original parent. */
waitpid (ptid_get_pid (inferior_ptid), &status, 0);
inf_child_mourn_inferior (ops);
}
/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
be chatty about it. */
static void
inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
{
char *exec_file;
pid_t pid;
struct inferior *inf;
/* Do not change either targets above or the same target if already present.
The reason is the target stack is shared across multiple inferiors. */
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 ()) /* Trying to masturbate? */
error (_("I refuse to debug myself!"));
if (! ops_already_pushed)
{
/* target_pid_to_str already uses the target. Also clear possible core
file with its process_stratum. */
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);
/* Always add a main thread. If some target extends the ptrace
target, it should decorate the ptid later with more info. */
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;
/* Set the initial event mask. */
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
/* Detach from the inferior, optionally passing it the signal
specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
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;
target_announce_detach (from_tty);
if (args)
sig = atoi (args);
#ifdef PT_DETACH
/* We'd better not have left any breakpoints in the program or it'll
die when it hits one. Also note that this may only work if we
previously attached to the inferior. It *might* work if we
started the process ourselves. */
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
inf_ptrace_detach_success (ops);
}
/* See inf-ptrace.h. */
void
inf_ptrace_detach_success (struct target_ops *ops)
{
pid_t pid = ptid_get_pid (inferior_ptid);
inferior_ptid = null_ptid;
detach_inferior (pid);
inf_child_maybe_unpush_target (ops);
}
/* Kill the inferior. */
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 (inferior_ptid);
}
/* Interrupt the inferior. */
static void
inf_ptrace_interrupt (struct target_ops *self, ptid_t ptid)
{
/* Send a SIGINT to the process group. This acts just like the user
typed a ^C on the controlling terminal. Note that using a
negative process number in kill() is a System V-ism. The proper
BSD interface is killpg(). However, all modern BSDs support the
System V interface too. */
kill (-inferior_process_group (), SIGINT);
}
/* Return which PID to pass to ptrace in order to observe/control the
tracee identified by PTID. */
pid_t
get_ptrace_pid (ptid_t ptid)
{
pid_t pid;
/* If we have an LWPID to work with, use it. Otherwise, we're
dealing with a non-threaded program/target. */
pid = ptid_get_lwp (ptid);
if (pid == 0)
pid = ptid_get_pid (ptid);
return pid;
}
/* Resume execution of thread PTID, or all threads if PTID is -1. If
STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
that signal. */
static void
inf_ptrace_resume (struct target_ops *ops,
ptid_t ptid, int step, enum gdb_signal signal)
{
pid_t pid;
int request;
if (ptid_equal (minus_one_ptid, ptid))
/* Resume all threads. Traditionally ptrace() only supports
single-threaded processes, so simply resume the inferior. */
pid = ptid_get_pid (inferior_ptid);
else
pid = get_ptrace_pid (ptid);
if (catch_syscall_enabled () > 0)
request = PT_SYSCALL;
else
request = PT_CONTINUE;
if (step)
{
/* If this system does not support PT_STEP, a higher level
function will have called single_step() to transmute the step
request into a continue request (by setting breakpoints on
all possible successor instructions), so we don't have to
worry about that here. */
request = PT_STEP;
}
/* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
where it was. If GDB wanted it to start some other way, we have
already written a new program counter value to the child. */
errno = 0;
ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
if (errno != 0)
perror_with_name (("ptrace"));
}
/* Wait for the child specified by PTID to do something. Return the
process ID of the child, or MINUS_ONE_PTID in case of error; store
the status in *OURSTATUS. */
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));
/* Claim it exited with unknown signal. */
ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
return inferior_ptid;
}
/* Ignore terminated detached child processes. */
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);
/* Make sure the other end of the fork is stopped too. */
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);
}
/* Transfer data via ptrace into process PID's memory from WRITEBUF, or
from process PID's memory into READBUF. Start at target address ADDR
and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
be non-null. Return the number of transferred bytes. */
static ULONGEST
inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf,
const gdb_byte *writebuf,
ULONGEST addr, ULONGEST len)
{
ULONGEST n;
unsigned int chunk;
/* We transfer aligned words. Thus align ADDR down to a word
boundary and determine how many bytes to skip at the
beginning. */
ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
addr -= skip;
for (n = 0;
n < len;
n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
{
/* Restrict to a chunk that fits in the current word. */
chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
/* Use a union for type punning. */
union
{
PTRACE_TYPE_RET word;
gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
} buf;
/* Read the word, also when doing a partial word write. */
if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
{
errno = 0;
buf.word = ptrace (PT_READ_I, pid,
(PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
if (errno != 0)
break;
if (readbuf != NULL)
memcpy (readbuf + n, buf.byte + skip, chunk);
}
if (writebuf != NULL)
{
memcpy (buf.byte + skip, writebuf + n, chunk);
errno = 0;
ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
buf.word);
if (errno != 0)
{
/* Using the appropriate one (I or D) is necessary for
Gould NP1, at least. */
errno = 0;
ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
buf.word);
if (errno != 0)
break;
}
}
}
return n;
}
/* Implement the to_xfer_partial target_ops method. */
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 = get_ptrace_pid (inferior_ptid);
switch (object)
{
case TARGET_OBJECT_MEMORY:
#ifdef PT_IO
/* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
request that promises to be much more efficient in reading
and writing data in the traced process's address space. */
{
struct ptrace_io_desc piod;
/* NOTE: We assume that there are no distinct address spaces
for instruction and data. However, on OpenBSD 3.9 and
later, PIOD_WRITE_D doesn't allow changing memory that's
mapped read-only. Since most code segments will be
read-only, using PIOD_WRITE_D will prevent us from
inserting breakpoints, so we use PIOD_WRITE_I instead. */
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)
{
/* Return the actual number of bytes read or written. */
*xfered_len = piod.piod_len;
return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
}
/* If the PT_IO request is somehow not supported, fallback on
using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
to indicate failure. */
if (errno != EINVAL)
return TARGET_XFER_EOF;
}
#endif
*xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf,
offset, len);
return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
case TARGET_OBJECT_UNWIND_TABLE:
return TARGET_XFER_E_IO;
case TARGET_OBJECT_AUXV:
#if defined (PT_IO) && defined (PIOD_READ_AUXV)
/* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
request that allows us to read the auxilliary vector. Other
BSD's may follow if they feel the need to support PIE. */
{
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)
{
/* Return the actual number of bytes read or written. */
*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;
}
}
/* Return non-zero if the thread specified by PTID is alive. */
static int
inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
{
/* ??? Is kill the right way to do this? */
return (kill (ptid_get_pid (ptid), 0) != -1);
}
/* Print status information about what we're accessing. */
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 const 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)
/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
Return 0 if *READPTR is already at the end of the buffer.
Return -1 if there is insufficient buffer for a whole entry.
Return 1 if an entry was read into *TYPEP and *VALP. */
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; /* Alignment. */
*valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
ptr += sizeof_auxv_val;
*readptr = ptr;
return 1;
}
#endif
/* Create a prototype ptrace target. The client can override it with
local methods. */
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_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
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_interrupt = inf_ptrace_interrupt;
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;
}
/* Pointer to a function that returns the offset within the user area
where a particular register is stored. */
static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
/* Fetch register REGNUM from the inferior. */
static void
inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
{
struct gdbarch *gdbarch = regcache->arch ();
CORE_ADDR addr;
size_t size;
PTRACE_TYPE_RET *buf;
pid_t pid;
int i;
/* This isn't really an address, but ptrace thinks of it as one. */
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 = get_ptrace_pid (regcache_get_ptid (regcache));
size = register_size (gdbarch, regnum);
gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
buf = (PTRACE_TYPE_RET *) alloca (size);
/* Read the register contents from the inferior a chunk at a time. */
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);
}
/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
for all registers. */
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 (regcache->arch ());
regnum++)
inf_ptrace_fetch_register (regcache, regnum);
else
inf_ptrace_fetch_register (regcache, regnum);
}
/* Store register REGNUM into the inferior. */
static void
inf_ptrace_store_register (const struct regcache *regcache, int regnum)
{
struct gdbarch *gdbarch = regcache->arch ();
CORE_ADDR addr;
size_t size;
PTRACE_TYPE_RET *buf;
pid_t pid;
int i;
/* This isn't really an address, but ptrace thinks of it as one. */
addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
if (addr == (CORE_ADDR)-1
|| gdbarch_cannot_store_register (gdbarch, regnum))
return;
pid = get_ptrace_pid (regcache_get_ptid (regcache));
size = register_size (gdbarch, regnum);
gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
buf = (PTRACE_TYPE_RET *) alloca (size);
/* Write the register contents into the inferior a chunk at a time. */
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);
}
}
/* Store register REGNUM back into the inferior. If REGNUM is -1, do
this for all registers. */
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 (regcache->arch ());
regnum++)
inf_ptrace_store_register (regcache, regnum);
else
inf_ptrace_store_register (regcache, regnum);
}
/* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
a function returning the offset within the user area where a
particular register is stored. */
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;
}