binutils-gdb/gdbserver/nto-low.cc

993 lines
24 KiB
C++
Raw Normal View History

/* QNX Neutrino specific low level interface, for the remote server
for GDB.
Copyright (C) 2009-2020 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 "server.h"
#include "gdbthread.h"
#include "nto-low.h"
#include "hostio.h"
#include "debug.h"
#include <limits.h>
#include <fcntl.h>
#include <spawn.h>
#include <sys/procfs.h>
#include <sys/auxv.h>
#include <sys/iomgr.h>
#include <sys/neutrino.h>
int using_threads = 1;
[GDBserver] Multi-process + multi-arch This patch makes GDBserver support multi-process + biarch. Currently, if you're debugging more than one process at once with a single gdbserver (in extended-remote mode), then all processes must have the same architecture (e.g., 64-bit vs 32-bit). Otherwise, you see this: Added inferior 2 [Switching to inferior 2 [<null>] (<noexec>)] Reading symbols from /home/pedro/gdb/tests/main32...done. Temporary breakpoint 2 at 0x4004cf: main. (2 locations) Starting program: /home/pedro/gdb/tests/main32 warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64 warning: Architecture rejected target-supplied description Remote 'g' packet reply is too long: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090cfffff0000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000b042f7460000000000020000230000002b0000002b0000002b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f03000000000000ffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000801f00003b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ... etc, etc ... Even though the process was running a 32-bit program, GDBserver sent back to GDB a register set in 64-bit layout. A patch (http://sourceware.org/ml/gdb-patches/2012-11/msg00228.html) a while ago made GDB track a target_gdbarch per inferior, and as consequence, fetch a target description per-inferior. This patch is the GDBserver counterpart, that makes GDBserver keep track of each process'es XML target description and register layout. So in the example above, GDBserver will send the correct register set in 32-bit layout to GDB. A new "struct target_desc" object (tdesc for short) is added, that holds the target description and register layout information about each process. Each `struct process_info' holds a pointer to a target description. The regcache also gains a pointer to a target description, mainly for convenience, and parallel with GDB (and possible future support for programs that flip processor modes). The low target's arch_setup routines are responsible for setting the process'es correct tdesc. This isn't that much different to how things were done before, except that instead of detecting the inferior process'es architecture and calling the corresponding init_registers_FOO routine, which would change the regcache layout globals and recreate the threads' regcaches, the regcache.c globals are gone, and the init_registers_$BAR routines now each initialize a separate global struct target_desc object (one for each arch variant GDBserver supports), and so all the init_registers_$BAR routines that are built into GDBserver are called early at GDBserver startup time (similarly to how GDB handles its built-in target descriptions), and then the arch_setup routine is responsible for making process_info->tdesc point to one of these target description globals. The regcache module is all parameterized to get the regcache's layout from the tdesc object instead of the old register_bytes, etc. globals. The threads' regcaches are now created lazily. The old scheme where we created each of them when we added a new thread doesn't work anymore, because we add the main thread/lwp before we see it stop for the first time, and it is only when we see the thread stop for the first time that we have a chance of determining the inferior's architecture (through the_low_target.arch_setup). Therefore when we add the main thread we don't know which architecture/tdesc its regcache should have. This patch makes the gdb.multi/multi-arch.exp test now pass against (extended-remote) GDBserver. It currently fails, without this patch. The IPA also uses the regcache, so it gains a new global struct target_desc pointer, which points at the description of the process it is loaded in. Re. the linux-low.c & friends changes. Since the register map etc. may differ between processes (64-bit vs 32-bit) etc., the linux_target_ops num_regs, regmap and regset_bitmap data fields are no longer sufficient. A new method is added in their place that returns a pointer to a new struct that includes all info linux-low.c needs to access registers of the current inferior. The patch/discussion that originally introduced linux-low.c:disabled_regsets mentions that the disabled_regsets set may be different per mode (in a biarch setup), and indeed that is cleared whenever we start a new (first) inferior, so that global is moved as well behind the new `struct regs_info'. On the x86 side: I simply replaced the i387-fp.c:num_xmm_registers global with a check for 64-bit or 32-bit process, which is equivalent to how the global was set. This avoided coming up with some more general mechanism that would work for all targets that use this module (GNU/Linux, Windows, etc.). Tested: GNU/Linux IA64 GNU/Linux MIPS64 GNU/Linux PowerPC (Fedora 16) GNU/Linux s390x (Fedora 16) GNU/Linux sparc64 (Debian) GNU/Linux x86_64, -m64 and -m32 (Fedora 17) Cross built, and smoke tested: i686-w64-mingw32, under Wine. GNU/Linux TI C6x, by Yao Qi. Cross built but otherwise not tested: aarch64-linux-gnu arm-linux-gnu m68k-linux nios2-linux-gnu sh-linux-gnu spu tilegx-unknown-linux-gnu Completely untested: GNU/Linux Blackfin GNU/Linux CRIS GNU/Linux CRISv32 GNU/Linux TI Xtensa GNU/Linux M32R LynxOS QNX NTO gdb/gdbserver/ 2013-06-07 Pedro Alves <palves@redhat.com> * Makefile.in (OBS): Add tdesc.o. (IPA_OBJS): Add tdesc-ipa.o. (tdesc-ipa.o): New rule. * ax.c (gdb_eval_agent_expr): Adjust register_size call to new interface. * linux-low.c (new_inferior): Delete. (disabled_regsets, num_regsets): Delete. (linux_add_process): Adjust to set the new per-process new_inferior flag. (linux_detach_one_lwp): Adjust to call regcache_invalidate_thread. (linux_wait_for_lwp): Adjust. Only call arch_setup if the event was a stop. When calling arch_setup, switch the current inferior to the thread that got an event. (linux_resume_one_lwp): Adjust to call regcache_invalidate_thread. (regsets_fetch_inferior_registers) (regsets_store_inferior_registers): New regsets_info parameter. Adjust to use it. (linux_register_in_regsets): New regs_info parameter. Adjust to use it. (register_addr, fetch_register, store_register): New usrregs_info parameter. Adjust to use it. (usr_fetch_inferior_registers, usr_store_inferior_registers): New parameter regs_info. Adjust to use it. (linux_fetch_registers): Get the current inferior's regs_info, and adjust to use it. (linux_store_registers): Ditto. [HAVE_LINUX_REGSETS] (initialize_regsets_info): New. (initialize_low): Don't initialize the target_regsets here. Call initialize_low_arch. * linux-low.h (target_regsets): Delete declaration. (struct regsets_info): New. (struct usrregs_info): New. (struct regs_info): New. (struct process_info_private) <new_inferior>: New field. (struct linux_target_ops): Delete the num_regs, regmap, and regset_bitmap fields. New field regs_info. [HAVE_LINUX_REGSETS] (initialize_regsets_info): Declare. * i387-fp.c (num_xmm_registers): Delete. (i387_cache_to_fsave, i387_fsave_to_cache): Adjust find_regno calls to new interface. (i387_cache_to_fxsave, i387_cache_to_xsave, i387_fxsave_to_cache) (i387_xsave_to_cache): Adjust find_regno calls to new interface. Infer the number of xmm registers from the regcache's target description. * i387-fp.h (num_xmm_registers): Delete. * inferiors.c (add_thread): Don't install the thread's regcache here. * proc-service.c (gregset_info): Fetch the current inferior's regs_info. Adjust to use it. * regcache.c: Include tdesc.h. (register_bytes, reg_defs, num_registers) (gdbserver_expedite_regs): Delete. (get_thread_regcache): If the thread doesn't have a regcache yet, create one, instead of aborting gdbserver. (regcache_invalidate_one): Rename to ... (regcache_invalidate_thread): ... this. (regcache_invalidate_one): New. (regcache_invalidate): Only invalidate registers of the current process. (init_register_cache): Add target_desc parameter, and use it. (new_register_cache): Ditto. Assert the target description has a non zero registers_size. (regcache_cpy): Add assertions. Adjust. (realloc_register_cache, set_register_cache): Delete. (registers_to_string, registers_from_string): Adjust. (find_register_by_name, find_regno, find_register_by_number) (register_cache_size): Add target_desc parameter, and use it. (free_register_cache_thread, free_register_cache_thread_one) (regcache_release, register_cache_size): New. (register_size): Add target_desc parameter, and use it. (register_data, supply_register, supply_register_zeroed) (supply_regblock, supply_register_by_name, collect_register) (collect_register_as_string, collect_register_by_name): Adjust. * regcache.h (struct target_desc): Forward declare. (struct regcache) <tdesc>: New field. (init_register_cache, new_register_cache): Add target_desc parameter. (regcache_invalidate_thread): Declare. (regcache_invalidate_one): Delete declaration. (regcache_release): Declare. (find_register_by_number, register_cache_size, register_size) (find_regno): Add target_desc parameter. (gdbserver_expedite_regs, gdbserver_xmltarget): Delete declarations. * remote-utils.c: Include tdesc.h. (outreg, prepare_resume_reply): Adjust. * server.c: Include tdesc.h. (gdbserver_xmltarget): Delete declaration. (get_features_xml, process_serial_event): Adjust. * server.h [IN_PROCESS_AGENT] (struct target_desc): Forward declare. (struct process_info) <tdesc>: New field. (ipa_tdesc): Declare. * tdesc.c: New file. * tdesc.h: New file. * tracepoint.c: Include tdesc.h. [IN_PROCESS_AGENT] (ipa_tdesc): Define. (get_context_regcache): Adjust to pass ipa_tdesc down. (do_action_at_tracepoint): Adjust to get the register cache size from the context regcache's description. (traceframe_walk_blocks): Adjust to get the register cache size from the current trace frame's description. (traceframe_get_pc): Adjust to get current trace frame's description and pass it down. (gdb_collect): Adjust to get the register cache size from the IPA's description. * linux-amd64-ipa.c (tdesc_amd64_linux): Declare. (gdbserver_xmltarget): Delete. (initialize_low_tracepoint): Set the ipa's target description. * linux-i386-ipa.c (tdesc_i386_linux): Declare. (initialize_low_tracepoint): Set the ipa's target description. * linux-x86-low.c: Include tdesc.h. [__x86_64__] (is_64bit_tdesc): New. (ps_get_thread_area, x86_get_thread_area): Use it. (i386_cannot_store_register): Rename to ... (x86_cannot_store_register): ... this. Use is_64bit_tdesc. (i386_cannot_fetch_register): Rename to ... (x86_cannot_fetch_register): ... this. Use is_64bit_tdesc. (x86_fill_gregset, x86_store_gregset): Adjust register_size calls to new interface. (target_regsets): Rename to ... (x86_regsets): ... this. (x86_get_pc, x86_set_pc): Adjust register_size calls to new interface. (x86_siginfo_fixup): Use is_64bit_tdesc. [__x86_64__] (tdesc_amd64_linux, tdesc_amd64_avx_linux) (tdesc_x32_avx_linux, tdesc_x32_linux) (tdesc_i386_linux, tdesc_i386_mmx_linux, tdesc_i386_avx_linux): Declare. (x86_linux_update_xmltarget): Delete. (I386_LINUX_XSAVE_XCR0_OFFSET): Define. (have_ptrace_getfpxregs, have_ptrace_getregset): New. (AMD64_LINUX_USER64_CS): New. (x86_linux_read_description): New, based on x86_linux_update_xmltarget. (same_process_callback): New. (x86_arch_setup_process_callback): New. (x86_linux_update_xmltarget): New. (x86_regsets_info): New. (amd64_linux_regs_info): New. (i386_linux_usrregs_info): New. (i386_linux_regs_info): New. (x86_linux_regs_info): New. (x86_arch_setup): Reimplement. (x86_install_fast_tracepoint_jump_pad): Use is_64bit_tdesc. (x86_emit_ops): Ditto. (the_low_target): Adjust. Install x86_linux_regs_info, x86_cannot_fetch_register, and x86_cannot_store_register. (initialize_low_arch): New. * linux-ia64-low.c (tdesc_ia64): Declare. (ia64_fetch_register): Adjust. (ia64_usrregs_info, regs_info): New globals. (ia64_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-sparc-low.c (tdesc_sparc64): Declare. (sparc_fill_gregset_to_stack, sparc_store_gregset_from_stack): Adjust. (sparc_arch_setup): New function. (sparc_regsets_info, sparc_usrregs_info, regs_info): New globals. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-ppc-low.c (tdesc_powerpc_32l, tdesc_powerpc_altivec32l) (tdesc_powerpc_cell32l, tdesc_powerpc_vsx32l) (tdesc_powerpc_isa205_32l, tdesc_powerpc_isa205_altivec32l) (tdesc_powerpc_isa205_vsx32l, tdesc_powerpc_e500l) (tdesc_powerpc_64l, tdesc_powerpc_altivec64l) (tdesc_powerpc_cell64l, tdesc_powerpc_vsx64l) (tdesc_powerpc_isa205_64l, tdesc_powerpc_isa205_altivec64l) (tdesc_powerpc_isa205_vsx64l): Declare. (ppc_cannot_store_register, ppc_collect_ptrace_register) (ppc_supply_ptrace_register, parse_spufs_run, ppc_get_pc) (ppc_set_pc, ppc_get_hwcap): Adjust. (ppc_usrregs_info): Forward declare. (!__powerpc64__) ppc_regmap_adjusted: New global. (ppc_arch_setup): Adjust to the current process'es target description. (ppc_fill_vsxregset, ppc_store_vsxregset, ppc_fill_vrregset) (ppc_store_vrregset, ppc_fill_evrregset, ppc_store_evrregse) (ppc_store_evrregset): Adjust. (target_regsets): Rename to ... (ppc_regsets): ... this, and make static. (ppc_usrregs_info, ppc_regsets_info, regs_info): New globals. (ppc_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-s390-low.c (tdesc_s390_linux32, tdesc_s390_linux32v1) (tdesc_s390_linux32v2, tdesc_s390_linux64, tdesc_s390_linux64v1) (tdesc_s390_linux64v2, tdesc_s390x_linux64, tdesc_s390x_linux64v1) (tdesc_s390x_linux64v2): Declare. (s390_collect_ptrace_register, s390_supply_ptrace_register) (s390_fill_gregset, s390_store_last_break): Adjust. (target_regsets): Rename to ... (s390_regsets): ... this, and make static. (s390_get_pc, s390_set_pc): Adjust. (s390_get_hwcap): New target_desc parameter, and use it. [__s390x__] (have_hwcap_s390_high_gprs): New global. (s390_arch_setup): Adjust to set the current process'es target description. Don't adjust the regmap. (s390_usrregs_info, s390_regsets_info, regs_info): New globals. [__s390x__] (s390_usrregs_info_3264, s390_regsets_info_3264) (regs_info_3264): New globals. (s390_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-mips-low.c (tdesc_mips_linux, tdesc_mips_dsp_linux) (tdesc_mips64_linux, tdesc_mips64_dsp_linux): Declare. [__mips64] (init_registers_mips_linux) (init_registers_mips_dsp_linux): Delete defines. [__mips64] (tdesc_mips_linux, tdesc_mips_dsp_linux): New defines. (have_dsp): New global. (mips_read_description): New, based on mips_arch_setup. (mips_arch_setup): Reimplement. (get_usrregs_info): New function. (mips_cannot_fetch_register, mips_cannot_store_register) (mips_get_pc, mips_set_pc, mips_fill_gregset, mips_store_gregset) (mips_fill_fpregset, mips_store_fpregset): Adjust. (target_regsets): Rename to ... (mips_regsets): ... this, and make static. (mips_regsets_info, mips_dsp_usrregs_info, mips_usrregs_info) (dsp_regs_info, regs_info): New globals. (mips_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-arm-low.c (tdesc_arm, tdesc_arm_with_iwmmxt) (tdesc_arm_with_vfpv2, tdesc_arm_with_vfpv3, tdesc_arm_with_neon): Declare. (arm_fill_vfpregset, arm_store_vfpregset): Adjust. (arm_read_description): New, with bits factored from arm_arch_setup. (arm_arch_setup): Reimplement. (target_regsets): Rename to ... (arm_regsets): ... this, and make static. (arm_regsets_info, arm_usrregs_info, regs_info): New globals. (arm_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-m68k-low.c (tdesc_m68k): Declare. (target_regsets): Rename to ... (m68k_regsets): ... this, and make static. (m68k_regsets_info, m68k_usrregs_info, regs_info): New globals. (m68k_regs_info): New function. (m68k_arch_setup): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-sh-low.c (tdesc_sharch): Declare. (target_regsets): Rename to ... (sh_regsets): ... this, and make static. (sh_regsets_info, sh_usrregs_info, regs_info): New globals. (sh_regs_info, sh_arch_setup): New functions. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-bfin-low.c (tdesc_bfin): Declare. (bfin_arch_setup): New function. (bfin_usrregs_info, regs_info): New globals. (bfin_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-cris-low.c (tdesc_cris): Declare. (cris_arch_setup): New function. (cris_usrregs_info, regs_info): New globals. (cris_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-cris-low.c (tdesc_crisv32): Declare. (cris_arch_setup): New function. (cris_regsets_info, cris_usrregs_info, regs_info): New globals. (cris_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-m32r-low.c (tdesc_m32r): Declare. (m32r_arch_setup): New function. (m32r_usrregs_info, regs_info): New globals. (m32r_regs_info): Adjust. (initialize_low_arch): New function. * linux-tic6x-low.c (tdesc_tic6x_c64xp_linux) (tdesc_tic6x_c64x_linux, tdesc_tic6x_c62x_linux): Declare. (tic6x_usrregs_info): Forward declare. (tic6x_read_description): New function, based on ... (tic6x_arch_setup): ... this. Reimplement. (target_regsets): Rename to ... (tic6x_regsets): ... this, and make static. (tic6x_regsets_info, tic6x_usrregs_info, regs_info): New globals. (tic6x_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-xtensa-low.c (tdesc_xtensa): Declare. (xtensa_fill_gregset, xtensa_store_gregset): Adjust. (target_regsets): Rename to ... (xtensa_regsets): ... this, and make static. (xtensa_regsets_info, xtensa_usrregs_info, regs_info): New globals. (xtensa_arch_setup, xtensa_regs_info): New functions. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-nios2-low.c (tdesc_nios2_linux): Declare. (nios2_arch_setup): Set the current process'es tdesc. (target_regsets): Rename to ... (nios2_regsets): ... this. (nios2_regsets_info, nios2_usrregs_info, regs_info): New globals. (nios2_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-aarch64-low.c (tdesc_aarch64): Declare. (aarch64_arch_setup): Set the current process'es tdesc. (target_regsets): Rename to ... (aarch64_regsets): ... this. (aarch64_regsets_info, aarch64_usrregs_info, regs_info): New globals. (aarch64_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-tile-low.c (tdesc_tilegx, tdesc_tilegx32): Declare globals. (target_regsets): Rename to ... (tile_regsets): ... this. (tile_regsets_info, tile_usrregs_info, regs_info): New globals. (tile_regs_info): New function. (tile_arch_setup): Set the current process'es tdesc. (the_low_target): Adjust. (initialize_low_arch): New function. * spu-low.c (tdesc_spu): Declare. (spu_create_inferior, spu_attach): Set the new process'es tdesc. * win32-arm-low.c (tdesc_arm): Declare. (arm_arch_setup): New function. (the_low_target): Install arm_arch_setup instead of init_registers_arm. * win32-i386-low.c (tdesc_i386, tdesc_amd64): Declare. (init_windows_x86): Rename to ... (i386_arch_setup): ... this. Set `win32_tdesc'. (the_low_target): Adjust. * win32-low.c (win32_tdesc): New global. (child_add_thread): Don't create the thread cache here. (do_initial_child_stuff): Set the new process'es tdesc. * win32-low.h (struct target_desc): Forward declare. (win32_tdesc): Declare. * lynx-i386-low.c (tdesc_i386): Declare global. (lynx_i386_arch_setup): Set `lynx_tdesc'. * lynx-low.c (lynx_tdesc): New global. (lynx_add_process): Set the new process'es tdesc. * lynx-low.h (struct target_desc): Forward declare. (lynx_tdesc): Declare global. * lynx-ppc-low.c (tdesc_powerpc_32): Declare global. (lynx_ppc_arch_setup): Set `lynx_tdesc'. * nto-low.c (nto_tdesc): New global. (do_attach): Set the new process'es tdesc. * nto-low.h (struct target_desc): Forward declare. (nto_tdesc): Declare. * nto-x86-low.c (tdesc_i386): Declare. (nto_x86_arch_setup): Set `nto_tdesc'. gdb/ 2013-06-07 Pedro Alves <palves@redhat.com> * regformats/regdat.sh: Output #include tdesc.h. Make globals static. Output a global target description pointer. (init_registers_${name}): Adjust to initialize a target description structure.
2013-06-07 12:46:59 +02:00
const struct target_desc *nto_tdesc;
static void
nto_trace (const char *fmt, ...)
{
va_list arg_list;
if (debug_threads == 0)
return;
fprintf (stderr, "nto:");
va_start (arg_list, fmt);
vfprintf (stderr, fmt, arg_list);
va_end (arg_list);
}
#define TRACE nto_trace
/* Structure holding neutrino specific information about
inferior. */
struct nto_inferior
{
char nto_procfs_path[PATH_MAX];
int ctl_fd;
pid_t pid;
int exit_signo; /* For tracking exit status. */
};
static struct nto_inferior nto_inferior;
static void
init_nto_inferior (struct nto_inferior *nto_inferior)
{
memset (nto_inferior, 0, sizeof (struct nto_inferior));
nto_inferior->ctl_fd = -1;
nto_inferior->pid = -1;
}
static void
do_detach (void)
{
if (nto_inferior.ctl_fd != -1)
{
nto_trace ("Closing fd\n");
close (nto_inferior.ctl_fd);
init_nto_inferior (&nto_inferior);
}
}
/* Set current thread. Return 1 on success, 0 otherwise. */
static int
nto_set_thread (ptid_t ptid)
{
int res = 0;
Remove ptid_get_pid This removes ptid_get_pid in favor of calling the ptid_t::pid method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_pid): Remove. * common/ptid.h (ptid_get_pid): Don't declare. * aarch64-linux-nat.c: Update. * ada-lang.c: Update. * aix-thread.c: Update. * alpha-bsd-nat.c: Update. * amd64-fbsd-nat.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * arm-nbsd-nat.c: Update. * auxv.c: Update. * break-catch-syscall.c: Update. * breakpoint.c: Update. * bsd-uthread.c: Update. * corelow.c: Update. * ctf.c: Update. * darwin-nat.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gcore.c: Update. * gnu-nat.c: Update. * hppa-nbsd-nat.c: Update. * hppa-obsd-nat.c: Update. * i386-fbsd-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infcmd.c: Update. * inferior.c: Update. * inferior.h: Update. * inflow.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * m68k-bsd-nat.c: Update. * mi/mi-interp.c: Update. * mi/mi-main.c: Update. * mips-linux-nat.c: Update. * mips-nbsd-nat.c: Update. * mips64-obsd-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * nto-procfs.c: Update. * obsd-nat.c: Update. * ppc-linux-nat.c: Update. * ppc-nbsd-nat.c: Update. * ppc-obsd-nat.c: Update. * proc-service.c: Update. * procfs.c: Update. * python/py-inferior.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * record.c: Update. * remote-sim.c: Update. * remote.c: Update. * rs6000-nat.c: Update. * s390-linux-nat.c: Update. * sh-nbsd-nat.c: Update. * sol-thread.c: Update. * sparc-nat.c: Update. * sparc64-tdep.c: Update. * spu-linux-nat.c: Update. * spu-tdep.c: Update. * target-debug.h: Update. * target.c: Update. * thread.c: Update. * tid-parse.c: Update. * tracefile-tfile.c: Update. * vax-bsd-nat.c: Update. * windows-nat.c: Update. * x86-linux-nat.c: Update. * x86-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * mem-break.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * tracepoint.c: Update.
2018-06-11 20:05:27 +02:00
TRACE ("%s pid: %d tid: %ld\n", __func__, ptid.pid (),
Remove ptid_get_lwp This removes ptid_get_lwp in favor of calling the ptid_t::lwp method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_lwp): Remove. * common/ptid.h (ptid_get_lwp): Don't declare. * aarch64-linux-nat.c: Update. * ada-tasks.c: Update. * aix-thread.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * corelow.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gnu-nat.c: Update. * i386-cygwin-tdep.c: Update. * i386-gnu-nat.c: Update. * i386-linux-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * mips-linux-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * obsd-nat.c: Update. * ppc-fbsd-nat.c: Update. * ppc-linux-nat.c: Update. * procfs.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * remote.c: Update. * s390-linux-nat.c: Update. * sol-thread.c: Update. * sol2-tdep.c: Update. * spu-linux-nat.c: Update. * x86-linux-nat.c: Update. * xtensa-linux-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * thread-db.c: Update.
2018-06-11 20:10:09 +02:00
ptid.lwp ());
if (nto_inferior.ctl_fd != -1
2018-06-11 22:45:22 +02:00
&& ptid != null_ptid
&& ptid != minus_one_ptid)
{
Remove ptid_get_lwp This removes ptid_get_lwp in favor of calling the ptid_t::lwp method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_lwp): Remove. * common/ptid.h (ptid_get_lwp): Don't declare. * aarch64-linux-nat.c: Update. * ada-tasks.c: Update. * aix-thread.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * corelow.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gnu-nat.c: Update. * i386-cygwin-tdep.c: Update. * i386-gnu-nat.c: Update. * i386-linux-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * mips-linux-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * obsd-nat.c: Update. * ppc-fbsd-nat.c: Update. * ppc-linux-nat.c: Update. * procfs.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * remote.c: Update. * s390-linux-nat.c: Update. * sol-thread.c: Update. * sol2-tdep.c: Update. * spu-linux-nat.c: Update. * x86-linux-nat.c: Update. * xtensa-linux-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * thread-db.c: Update.
2018-06-11 20:10:09 +02:00
pthread_t tid = ptid.lwp ();
if (EOK == devctl (nto_inferior.ctl_fd, DCMD_PROC_CURTHREAD, &tid,
sizeof (tid), 0))
res = 1;
else
TRACE ("%s: Error: failed to set current thread\n", __func__);
}
return res;
}
/* This function will determine all alive threads. Note that we do not list
dead but unjoined threads even though they are still in the process' thread
list.
NTO_INFERIOR must not be NULL. */
static void
nto_find_new_threads (struct nto_inferior *nto_inferior)
{
pthread_t tid;
TRACE ("%s pid:%d\n", __func__, nto_inferior->pid);
if (nto_inferior->ctl_fd == -1)
return;
for (tid = 1;; ++tid)
{
procfs_status status;
ptid_t ptid;
int err;
status.tid = tid;
err = devctl (nto_inferior->ctl_fd, DCMD_PROC_TIDSTATUS, &status,
sizeof (status), 0);
if (err != EOK || status.tid == 0)
break;
/* All threads in between are gone. */
while (tid != status.tid || status.state == STATE_DEAD)
{
struct thread_info *ti;
ptid = ptid_t (nto_inferior->pid, tid, 0);
ti = find_thread_ptid (ptid);
if (ti != NULL)
{
TRACE ("Removing thread %d\n", tid);
remove_thread (ti);
}
if (tid == status.tid)
break;
++tid;
}
if (status.state != STATE_DEAD)
{
TRACE ("Adding thread %d\n", tid);
ptid = ptid_t (nto_inferior->pid, tid, 0);
if (!find_thread_ptid (ptid))
add_thread (ptid, NULL);
}
}
}
/* Given pid, open procfs path. */
static pid_t
do_attach (pid_t pid)
{
procfs_status status;
struct sigevent event;
if (nto_inferior.ctl_fd != -1)
{
close (nto_inferior.ctl_fd);
init_nto_inferior (&nto_inferior);
}
xsnprintf (nto_inferior.nto_procfs_path, PATH_MAX - 1, "/proc/%d/as", pid);
nto_inferior.ctl_fd = open (nto_inferior.nto_procfs_path, O_RDWR);
if (nto_inferior.ctl_fd == -1)
{
TRACE ("Failed to open %s\n", nto_inferior.nto_procfs_path);
init_nto_inferior (&nto_inferior);
return -1;
}
if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0)
!= EOK)
{
do_detach ();
return -1;
}
nto_inferior.pid = pid;
/* Define a sigevent for process stopped notification. */
event.sigev_notify = SIGEV_SIGNAL_THREAD;
event.sigev_signo = SIGUSR1;
event.sigev_code = 0;
event.sigev_value.sival_ptr = NULL;
event.sigev_priority = -1;
devctl (nto_inferior.ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
0) == EOK
&& (status.flags & _DEBUG_FLAG_STOPPED))
{
ptid_t ptid;
[GDBserver] Multi-process + multi-arch This patch makes GDBserver support multi-process + biarch. Currently, if you're debugging more than one process at once with a single gdbserver (in extended-remote mode), then all processes must have the same architecture (e.g., 64-bit vs 32-bit). Otherwise, you see this: Added inferior 2 [Switching to inferior 2 [<null>] (<noexec>)] Reading symbols from /home/pedro/gdb/tests/main32...done. Temporary breakpoint 2 at 0x4004cf: main. (2 locations) Starting program: /home/pedro/gdb/tests/main32 warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64 warning: Architecture rejected target-supplied description Remote 'g' packet reply is too long: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090cfffff0000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000b042f7460000000000020000230000002b0000002b0000002b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f03000000000000ffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000801f00003b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ... etc, etc ... Even though the process was running a 32-bit program, GDBserver sent back to GDB a register set in 64-bit layout. A patch (http://sourceware.org/ml/gdb-patches/2012-11/msg00228.html) a while ago made GDB track a target_gdbarch per inferior, and as consequence, fetch a target description per-inferior. This patch is the GDBserver counterpart, that makes GDBserver keep track of each process'es XML target description and register layout. So in the example above, GDBserver will send the correct register set in 32-bit layout to GDB. A new "struct target_desc" object (tdesc for short) is added, that holds the target description and register layout information about each process. Each `struct process_info' holds a pointer to a target description. The regcache also gains a pointer to a target description, mainly for convenience, and parallel with GDB (and possible future support for programs that flip processor modes). The low target's arch_setup routines are responsible for setting the process'es correct tdesc. This isn't that much different to how things were done before, except that instead of detecting the inferior process'es architecture and calling the corresponding init_registers_FOO routine, which would change the regcache layout globals and recreate the threads' regcaches, the regcache.c globals are gone, and the init_registers_$BAR routines now each initialize a separate global struct target_desc object (one for each arch variant GDBserver supports), and so all the init_registers_$BAR routines that are built into GDBserver are called early at GDBserver startup time (similarly to how GDB handles its built-in target descriptions), and then the arch_setup routine is responsible for making process_info->tdesc point to one of these target description globals. The regcache module is all parameterized to get the regcache's layout from the tdesc object instead of the old register_bytes, etc. globals. The threads' regcaches are now created lazily. The old scheme where we created each of them when we added a new thread doesn't work anymore, because we add the main thread/lwp before we see it stop for the first time, and it is only when we see the thread stop for the first time that we have a chance of determining the inferior's architecture (through the_low_target.arch_setup). Therefore when we add the main thread we don't know which architecture/tdesc its regcache should have. This patch makes the gdb.multi/multi-arch.exp test now pass against (extended-remote) GDBserver. It currently fails, without this patch. The IPA also uses the regcache, so it gains a new global struct target_desc pointer, which points at the description of the process it is loaded in. Re. the linux-low.c & friends changes. Since the register map etc. may differ between processes (64-bit vs 32-bit) etc., the linux_target_ops num_regs, regmap and regset_bitmap data fields are no longer sufficient. A new method is added in their place that returns a pointer to a new struct that includes all info linux-low.c needs to access registers of the current inferior. The patch/discussion that originally introduced linux-low.c:disabled_regsets mentions that the disabled_regsets set may be different per mode (in a biarch setup), and indeed that is cleared whenever we start a new (first) inferior, so that global is moved as well behind the new `struct regs_info'. On the x86 side: I simply replaced the i387-fp.c:num_xmm_registers global with a check for 64-bit or 32-bit process, which is equivalent to how the global was set. This avoided coming up with some more general mechanism that would work for all targets that use this module (GNU/Linux, Windows, etc.). Tested: GNU/Linux IA64 GNU/Linux MIPS64 GNU/Linux PowerPC (Fedora 16) GNU/Linux s390x (Fedora 16) GNU/Linux sparc64 (Debian) GNU/Linux x86_64, -m64 and -m32 (Fedora 17) Cross built, and smoke tested: i686-w64-mingw32, under Wine. GNU/Linux TI C6x, by Yao Qi. Cross built but otherwise not tested: aarch64-linux-gnu arm-linux-gnu m68k-linux nios2-linux-gnu sh-linux-gnu spu tilegx-unknown-linux-gnu Completely untested: GNU/Linux Blackfin GNU/Linux CRIS GNU/Linux CRISv32 GNU/Linux TI Xtensa GNU/Linux M32R LynxOS QNX NTO gdb/gdbserver/ 2013-06-07 Pedro Alves <palves@redhat.com> * Makefile.in (OBS): Add tdesc.o. (IPA_OBJS): Add tdesc-ipa.o. (tdesc-ipa.o): New rule. * ax.c (gdb_eval_agent_expr): Adjust register_size call to new interface. * linux-low.c (new_inferior): Delete. (disabled_regsets, num_regsets): Delete. (linux_add_process): Adjust to set the new per-process new_inferior flag. (linux_detach_one_lwp): Adjust to call regcache_invalidate_thread. (linux_wait_for_lwp): Adjust. Only call arch_setup if the event was a stop. When calling arch_setup, switch the current inferior to the thread that got an event. (linux_resume_one_lwp): Adjust to call regcache_invalidate_thread. (regsets_fetch_inferior_registers) (regsets_store_inferior_registers): New regsets_info parameter. Adjust to use it. (linux_register_in_regsets): New regs_info parameter. Adjust to use it. (register_addr, fetch_register, store_register): New usrregs_info parameter. Adjust to use it. (usr_fetch_inferior_registers, usr_store_inferior_registers): New parameter regs_info. Adjust to use it. (linux_fetch_registers): Get the current inferior's regs_info, and adjust to use it. (linux_store_registers): Ditto. [HAVE_LINUX_REGSETS] (initialize_regsets_info): New. (initialize_low): Don't initialize the target_regsets here. Call initialize_low_arch. * linux-low.h (target_regsets): Delete declaration. (struct regsets_info): New. (struct usrregs_info): New. (struct regs_info): New. (struct process_info_private) <new_inferior>: New field. (struct linux_target_ops): Delete the num_regs, regmap, and regset_bitmap fields. New field regs_info. [HAVE_LINUX_REGSETS] (initialize_regsets_info): Declare. * i387-fp.c (num_xmm_registers): Delete. (i387_cache_to_fsave, i387_fsave_to_cache): Adjust find_regno calls to new interface. (i387_cache_to_fxsave, i387_cache_to_xsave, i387_fxsave_to_cache) (i387_xsave_to_cache): Adjust find_regno calls to new interface. Infer the number of xmm registers from the regcache's target description. * i387-fp.h (num_xmm_registers): Delete. * inferiors.c (add_thread): Don't install the thread's regcache here. * proc-service.c (gregset_info): Fetch the current inferior's regs_info. Adjust to use it. * regcache.c: Include tdesc.h. (register_bytes, reg_defs, num_registers) (gdbserver_expedite_regs): Delete. (get_thread_regcache): If the thread doesn't have a regcache yet, create one, instead of aborting gdbserver. (regcache_invalidate_one): Rename to ... (regcache_invalidate_thread): ... this. (regcache_invalidate_one): New. (regcache_invalidate): Only invalidate registers of the current process. (init_register_cache): Add target_desc parameter, and use it. (new_register_cache): Ditto. Assert the target description has a non zero registers_size. (regcache_cpy): Add assertions. Adjust. (realloc_register_cache, set_register_cache): Delete. (registers_to_string, registers_from_string): Adjust. (find_register_by_name, find_regno, find_register_by_number) (register_cache_size): Add target_desc parameter, and use it. (free_register_cache_thread, free_register_cache_thread_one) (regcache_release, register_cache_size): New. (register_size): Add target_desc parameter, and use it. (register_data, supply_register, supply_register_zeroed) (supply_regblock, supply_register_by_name, collect_register) (collect_register_as_string, collect_register_by_name): Adjust. * regcache.h (struct target_desc): Forward declare. (struct regcache) <tdesc>: New field. (init_register_cache, new_register_cache): Add target_desc parameter. (regcache_invalidate_thread): Declare. (regcache_invalidate_one): Delete declaration. (regcache_release): Declare. (find_register_by_number, register_cache_size, register_size) (find_regno): Add target_desc parameter. (gdbserver_expedite_regs, gdbserver_xmltarget): Delete declarations. * remote-utils.c: Include tdesc.h. (outreg, prepare_resume_reply): Adjust. * server.c: Include tdesc.h. (gdbserver_xmltarget): Delete declaration. (get_features_xml, process_serial_event): Adjust. * server.h [IN_PROCESS_AGENT] (struct target_desc): Forward declare. (struct process_info) <tdesc>: New field. (ipa_tdesc): Declare. * tdesc.c: New file. * tdesc.h: New file. * tracepoint.c: Include tdesc.h. [IN_PROCESS_AGENT] (ipa_tdesc): Define. (get_context_regcache): Adjust to pass ipa_tdesc down. (do_action_at_tracepoint): Adjust to get the register cache size from the context regcache's description. (traceframe_walk_blocks): Adjust to get the register cache size from the current trace frame's description. (traceframe_get_pc): Adjust to get current trace frame's description and pass it down. (gdb_collect): Adjust to get the register cache size from the IPA's description. * linux-amd64-ipa.c (tdesc_amd64_linux): Declare. (gdbserver_xmltarget): Delete. (initialize_low_tracepoint): Set the ipa's target description. * linux-i386-ipa.c (tdesc_i386_linux): Declare. (initialize_low_tracepoint): Set the ipa's target description. * linux-x86-low.c: Include tdesc.h. [__x86_64__] (is_64bit_tdesc): New. (ps_get_thread_area, x86_get_thread_area): Use it. (i386_cannot_store_register): Rename to ... (x86_cannot_store_register): ... this. Use is_64bit_tdesc. (i386_cannot_fetch_register): Rename to ... (x86_cannot_fetch_register): ... this. Use is_64bit_tdesc. (x86_fill_gregset, x86_store_gregset): Adjust register_size calls to new interface. (target_regsets): Rename to ... (x86_regsets): ... this. (x86_get_pc, x86_set_pc): Adjust register_size calls to new interface. (x86_siginfo_fixup): Use is_64bit_tdesc. [__x86_64__] (tdesc_amd64_linux, tdesc_amd64_avx_linux) (tdesc_x32_avx_linux, tdesc_x32_linux) (tdesc_i386_linux, tdesc_i386_mmx_linux, tdesc_i386_avx_linux): Declare. (x86_linux_update_xmltarget): Delete. (I386_LINUX_XSAVE_XCR0_OFFSET): Define. (have_ptrace_getfpxregs, have_ptrace_getregset): New. (AMD64_LINUX_USER64_CS): New. (x86_linux_read_description): New, based on x86_linux_update_xmltarget. (same_process_callback): New. (x86_arch_setup_process_callback): New. (x86_linux_update_xmltarget): New. (x86_regsets_info): New. (amd64_linux_regs_info): New. (i386_linux_usrregs_info): New. (i386_linux_regs_info): New. (x86_linux_regs_info): New. (x86_arch_setup): Reimplement. (x86_install_fast_tracepoint_jump_pad): Use is_64bit_tdesc. (x86_emit_ops): Ditto. (the_low_target): Adjust. Install x86_linux_regs_info, x86_cannot_fetch_register, and x86_cannot_store_register. (initialize_low_arch): New. * linux-ia64-low.c (tdesc_ia64): Declare. (ia64_fetch_register): Adjust. (ia64_usrregs_info, regs_info): New globals. (ia64_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-sparc-low.c (tdesc_sparc64): Declare. (sparc_fill_gregset_to_stack, sparc_store_gregset_from_stack): Adjust. (sparc_arch_setup): New function. (sparc_regsets_info, sparc_usrregs_info, regs_info): New globals. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-ppc-low.c (tdesc_powerpc_32l, tdesc_powerpc_altivec32l) (tdesc_powerpc_cell32l, tdesc_powerpc_vsx32l) (tdesc_powerpc_isa205_32l, tdesc_powerpc_isa205_altivec32l) (tdesc_powerpc_isa205_vsx32l, tdesc_powerpc_e500l) (tdesc_powerpc_64l, tdesc_powerpc_altivec64l) (tdesc_powerpc_cell64l, tdesc_powerpc_vsx64l) (tdesc_powerpc_isa205_64l, tdesc_powerpc_isa205_altivec64l) (tdesc_powerpc_isa205_vsx64l): Declare. (ppc_cannot_store_register, ppc_collect_ptrace_register) (ppc_supply_ptrace_register, parse_spufs_run, ppc_get_pc) (ppc_set_pc, ppc_get_hwcap): Adjust. (ppc_usrregs_info): Forward declare. (!__powerpc64__) ppc_regmap_adjusted: New global. (ppc_arch_setup): Adjust to the current process'es target description. (ppc_fill_vsxregset, ppc_store_vsxregset, ppc_fill_vrregset) (ppc_store_vrregset, ppc_fill_evrregset, ppc_store_evrregse) (ppc_store_evrregset): Adjust. (target_regsets): Rename to ... (ppc_regsets): ... this, and make static. (ppc_usrregs_info, ppc_regsets_info, regs_info): New globals. (ppc_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-s390-low.c (tdesc_s390_linux32, tdesc_s390_linux32v1) (tdesc_s390_linux32v2, tdesc_s390_linux64, tdesc_s390_linux64v1) (tdesc_s390_linux64v2, tdesc_s390x_linux64, tdesc_s390x_linux64v1) (tdesc_s390x_linux64v2): Declare. (s390_collect_ptrace_register, s390_supply_ptrace_register) (s390_fill_gregset, s390_store_last_break): Adjust. (target_regsets): Rename to ... (s390_regsets): ... this, and make static. (s390_get_pc, s390_set_pc): Adjust. (s390_get_hwcap): New target_desc parameter, and use it. [__s390x__] (have_hwcap_s390_high_gprs): New global. (s390_arch_setup): Adjust to set the current process'es target description. Don't adjust the regmap. (s390_usrregs_info, s390_regsets_info, regs_info): New globals. [__s390x__] (s390_usrregs_info_3264, s390_regsets_info_3264) (regs_info_3264): New globals. (s390_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-mips-low.c (tdesc_mips_linux, tdesc_mips_dsp_linux) (tdesc_mips64_linux, tdesc_mips64_dsp_linux): Declare. [__mips64] (init_registers_mips_linux) (init_registers_mips_dsp_linux): Delete defines. [__mips64] (tdesc_mips_linux, tdesc_mips_dsp_linux): New defines. (have_dsp): New global. (mips_read_description): New, based on mips_arch_setup. (mips_arch_setup): Reimplement. (get_usrregs_info): New function. (mips_cannot_fetch_register, mips_cannot_store_register) (mips_get_pc, mips_set_pc, mips_fill_gregset, mips_store_gregset) (mips_fill_fpregset, mips_store_fpregset): Adjust. (target_regsets): Rename to ... (mips_regsets): ... this, and make static. (mips_regsets_info, mips_dsp_usrregs_info, mips_usrregs_info) (dsp_regs_info, regs_info): New globals. (mips_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-arm-low.c (tdesc_arm, tdesc_arm_with_iwmmxt) (tdesc_arm_with_vfpv2, tdesc_arm_with_vfpv3, tdesc_arm_with_neon): Declare. (arm_fill_vfpregset, arm_store_vfpregset): Adjust. (arm_read_description): New, with bits factored from arm_arch_setup. (arm_arch_setup): Reimplement. (target_regsets): Rename to ... (arm_regsets): ... this, and make static. (arm_regsets_info, arm_usrregs_info, regs_info): New globals. (arm_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-m68k-low.c (tdesc_m68k): Declare. (target_regsets): Rename to ... (m68k_regsets): ... this, and make static. (m68k_regsets_info, m68k_usrregs_info, regs_info): New globals. (m68k_regs_info): New function. (m68k_arch_setup): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-sh-low.c (tdesc_sharch): Declare. (target_regsets): Rename to ... (sh_regsets): ... this, and make static. (sh_regsets_info, sh_usrregs_info, regs_info): New globals. (sh_regs_info, sh_arch_setup): New functions. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-bfin-low.c (tdesc_bfin): Declare. (bfin_arch_setup): New function. (bfin_usrregs_info, regs_info): New globals. (bfin_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-cris-low.c (tdesc_cris): Declare. (cris_arch_setup): New function. (cris_usrregs_info, regs_info): New globals. (cris_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-cris-low.c (tdesc_crisv32): Declare. (cris_arch_setup): New function. (cris_regsets_info, cris_usrregs_info, regs_info): New globals. (cris_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-m32r-low.c (tdesc_m32r): Declare. (m32r_arch_setup): New function. (m32r_usrregs_info, regs_info): New globals. (m32r_regs_info): Adjust. (initialize_low_arch): New function. * linux-tic6x-low.c (tdesc_tic6x_c64xp_linux) (tdesc_tic6x_c64x_linux, tdesc_tic6x_c62x_linux): Declare. (tic6x_usrregs_info): Forward declare. (tic6x_read_description): New function, based on ... (tic6x_arch_setup): ... this. Reimplement. (target_regsets): Rename to ... (tic6x_regsets): ... this, and make static. (tic6x_regsets_info, tic6x_usrregs_info, regs_info): New globals. (tic6x_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-xtensa-low.c (tdesc_xtensa): Declare. (xtensa_fill_gregset, xtensa_store_gregset): Adjust. (target_regsets): Rename to ... (xtensa_regsets): ... this, and make static. (xtensa_regsets_info, xtensa_usrregs_info, regs_info): New globals. (xtensa_arch_setup, xtensa_regs_info): New functions. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-nios2-low.c (tdesc_nios2_linux): Declare. (nios2_arch_setup): Set the current process'es tdesc. (target_regsets): Rename to ... (nios2_regsets): ... this. (nios2_regsets_info, nios2_usrregs_info, regs_info): New globals. (nios2_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-aarch64-low.c (tdesc_aarch64): Declare. (aarch64_arch_setup): Set the current process'es tdesc. (target_regsets): Rename to ... (aarch64_regsets): ... this. (aarch64_regsets_info, aarch64_usrregs_info, regs_info): New globals. (aarch64_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-tile-low.c (tdesc_tilegx, tdesc_tilegx32): Declare globals. (target_regsets): Rename to ... (tile_regsets): ... this. (tile_regsets_info, tile_usrregs_info, regs_info): New globals. (tile_regs_info): New function. (tile_arch_setup): Set the current process'es tdesc. (the_low_target): Adjust. (initialize_low_arch): New function. * spu-low.c (tdesc_spu): Declare. (spu_create_inferior, spu_attach): Set the new process'es tdesc. * win32-arm-low.c (tdesc_arm): Declare. (arm_arch_setup): New function. (the_low_target): Install arm_arch_setup instead of init_registers_arm. * win32-i386-low.c (tdesc_i386, tdesc_amd64): Declare. (init_windows_x86): Rename to ... (i386_arch_setup): ... this. Set `win32_tdesc'. (the_low_target): Adjust. * win32-low.c (win32_tdesc): New global. (child_add_thread): Don't create the thread cache here. (do_initial_child_stuff): Set the new process'es tdesc. * win32-low.h (struct target_desc): Forward declare. (win32_tdesc): Declare. * lynx-i386-low.c (tdesc_i386): Declare global. (lynx_i386_arch_setup): Set `lynx_tdesc'. * lynx-low.c (lynx_tdesc): New global. (lynx_add_process): Set the new process'es tdesc. * lynx-low.h (struct target_desc): Forward declare. (lynx_tdesc): Declare global. * lynx-ppc-low.c (tdesc_powerpc_32): Declare global. (lynx_ppc_arch_setup): Set `lynx_tdesc'. * nto-low.c (nto_tdesc): New global. (do_attach): Set the new process'es tdesc. * nto-low.h (struct target_desc): Forward declare. (nto_tdesc): Declare. * nto-x86-low.c (tdesc_i386): Declare. (nto_x86_arch_setup): Set `nto_tdesc'. gdb/ 2013-06-07 Pedro Alves <palves@redhat.com> * regformats/regdat.sh: Output #include tdesc.h. Make globals static. Output a global target description pointer. (init_registers_${name}): Adjust to initialize a target description structure.
2013-06-07 12:46:59 +02:00
struct process_info *proc;
kill (pid, SIGCONT);
ptid = ptid_t (status.pid, status.tid, 0);
the_low_target.arch_setup ();
[GDBserver] Multi-process + multi-arch This patch makes GDBserver support multi-process + biarch. Currently, if you're debugging more than one process at once with a single gdbserver (in extended-remote mode), then all processes must have the same architecture (e.g., 64-bit vs 32-bit). Otherwise, you see this: Added inferior 2 [Switching to inferior 2 [<null>] (<noexec>)] Reading symbols from /home/pedro/gdb/tests/main32...done. Temporary breakpoint 2 at 0x4004cf: main. (2 locations) Starting program: /home/pedro/gdb/tests/main32 warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64 warning: Architecture rejected target-supplied description Remote 'g' packet reply is too long: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090cfffff0000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000b042f7460000000000020000230000002b0000002b0000002b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f03000000000000ffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000801f00003b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ... etc, etc ... Even though the process was running a 32-bit program, GDBserver sent back to GDB a register set in 64-bit layout. A patch (http://sourceware.org/ml/gdb-patches/2012-11/msg00228.html) a while ago made GDB track a target_gdbarch per inferior, and as consequence, fetch a target description per-inferior. This patch is the GDBserver counterpart, that makes GDBserver keep track of each process'es XML target description and register layout. So in the example above, GDBserver will send the correct register set in 32-bit layout to GDB. A new "struct target_desc" object (tdesc for short) is added, that holds the target description and register layout information about each process. Each `struct process_info' holds a pointer to a target description. The regcache also gains a pointer to a target description, mainly for convenience, and parallel with GDB (and possible future support for programs that flip processor modes). The low target's arch_setup routines are responsible for setting the process'es correct tdesc. This isn't that much different to how things were done before, except that instead of detecting the inferior process'es architecture and calling the corresponding init_registers_FOO routine, which would change the regcache layout globals and recreate the threads' regcaches, the regcache.c globals are gone, and the init_registers_$BAR routines now each initialize a separate global struct target_desc object (one for each arch variant GDBserver supports), and so all the init_registers_$BAR routines that are built into GDBserver are called early at GDBserver startup time (similarly to how GDB handles its built-in target descriptions), and then the arch_setup routine is responsible for making process_info->tdesc point to one of these target description globals. The regcache module is all parameterized to get the regcache's layout from the tdesc object instead of the old register_bytes, etc. globals. The threads' regcaches are now created lazily. The old scheme where we created each of them when we added a new thread doesn't work anymore, because we add the main thread/lwp before we see it stop for the first time, and it is only when we see the thread stop for the first time that we have a chance of determining the inferior's architecture (through the_low_target.arch_setup). Therefore when we add the main thread we don't know which architecture/tdesc its regcache should have. This patch makes the gdb.multi/multi-arch.exp test now pass against (extended-remote) GDBserver. It currently fails, without this patch. The IPA also uses the regcache, so it gains a new global struct target_desc pointer, which points at the description of the process it is loaded in. Re. the linux-low.c & friends changes. Since the register map etc. may differ between processes (64-bit vs 32-bit) etc., the linux_target_ops num_regs, regmap and regset_bitmap data fields are no longer sufficient. A new method is added in their place that returns a pointer to a new struct that includes all info linux-low.c needs to access registers of the current inferior. The patch/discussion that originally introduced linux-low.c:disabled_regsets mentions that the disabled_regsets set may be different per mode (in a biarch setup), and indeed that is cleared whenever we start a new (first) inferior, so that global is moved as well behind the new `struct regs_info'. On the x86 side: I simply replaced the i387-fp.c:num_xmm_registers global with a check for 64-bit or 32-bit process, which is equivalent to how the global was set. This avoided coming up with some more general mechanism that would work for all targets that use this module (GNU/Linux, Windows, etc.). Tested: GNU/Linux IA64 GNU/Linux MIPS64 GNU/Linux PowerPC (Fedora 16) GNU/Linux s390x (Fedora 16) GNU/Linux sparc64 (Debian) GNU/Linux x86_64, -m64 and -m32 (Fedora 17) Cross built, and smoke tested: i686-w64-mingw32, under Wine. GNU/Linux TI C6x, by Yao Qi. Cross built but otherwise not tested: aarch64-linux-gnu arm-linux-gnu m68k-linux nios2-linux-gnu sh-linux-gnu spu tilegx-unknown-linux-gnu Completely untested: GNU/Linux Blackfin GNU/Linux CRIS GNU/Linux CRISv32 GNU/Linux TI Xtensa GNU/Linux M32R LynxOS QNX NTO gdb/gdbserver/ 2013-06-07 Pedro Alves <palves@redhat.com> * Makefile.in (OBS): Add tdesc.o. (IPA_OBJS): Add tdesc-ipa.o. (tdesc-ipa.o): New rule. * ax.c (gdb_eval_agent_expr): Adjust register_size call to new interface. * linux-low.c (new_inferior): Delete. (disabled_regsets, num_regsets): Delete. (linux_add_process): Adjust to set the new per-process new_inferior flag. (linux_detach_one_lwp): Adjust to call regcache_invalidate_thread. (linux_wait_for_lwp): Adjust. Only call arch_setup if the event was a stop. When calling arch_setup, switch the current inferior to the thread that got an event. (linux_resume_one_lwp): Adjust to call regcache_invalidate_thread. (regsets_fetch_inferior_registers) (regsets_store_inferior_registers): New regsets_info parameter. Adjust to use it. (linux_register_in_regsets): New regs_info parameter. Adjust to use it. (register_addr, fetch_register, store_register): New usrregs_info parameter. Adjust to use it. (usr_fetch_inferior_registers, usr_store_inferior_registers): New parameter regs_info. Adjust to use it. (linux_fetch_registers): Get the current inferior's regs_info, and adjust to use it. (linux_store_registers): Ditto. [HAVE_LINUX_REGSETS] (initialize_regsets_info): New. (initialize_low): Don't initialize the target_regsets here. Call initialize_low_arch. * linux-low.h (target_regsets): Delete declaration. (struct regsets_info): New. (struct usrregs_info): New. (struct regs_info): New. (struct process_info_private) <new_inferior>: New field. (struct linux_target_ops): Delete the num_regs, regmap, and regset_bitmap fields. New field regs_info. [HAVE_LINUX_REGSETS] (initialize_regsets_info): Declare. * i387-fp.c (num_xmm_registers): Delete. (i387_cache_to_fsave, i387_fsave_to_cache): Adjust find_regno calls to new interface. (i387_cache_to_fxsave, i387_cache_to_xsave, i387_fxsave_to_cache) (i387_xsave_to_cache): Adjust find_regno calls to new interface. Infer the number of xmm registers from the regcache's target description. * i387-fp.h (num_xmm_registers): Delete. * inferiors.c (add_thread): Don't install the thread's regcache here. * proc-service.c (gregset_info): Fetch the current inferior's regs_info. Adjust to use it. * regcache.c: Include tdesc.h. (register_bytes, reg_defs, num_registers) (gdbserver_expedite_regs): Delete. (get_thread_regcache): If the thread doesn't have a regcache yet, create one, instead of aborting gdbserver. (regcache_invalidate_one): Rename to ... (regcache_invalidate_thread): ... this. (regcache_invalidate_one): New. (regcache_invalidate): Only invalidate registers of the current process. (init_register_cache): Add target_desc parameter, and use it. (new_register_cache): Ditto. Assert the target description has a non zero registers_size. (regcache_cpy): Add assertions. Adjust. (realloc_register_cache, set_register_cache): Delete. (registers_to_string, registers_from_string): Adjust. (find_register_by_name, find_regno, find_register_by_number) (register_cache_size): Add target_desc parameter, and use it. (free_register_cache_thread, free_register_cache_thread_one) (regcache_release, register_cache_size): New. (register_size): Add target_desc parameter, and use it. (register_data, supply_register, supply_register_zeroed) (supply_regblock, supply_register_by_name, collect_register) (collect_register_as_string, collect_register_by_name): Adjust. * regcache.h (struct target_desc): Forward declare. (struct regcache) <tdesc>: New field. (init_register_cache, new_register_cache): Add target_desc parameter. (regcache_invalidate_thread): Declare. (regcache_invalidate_one): Delete declaration. (regcache_release): Declare. (find_register_by_number, register_cache_size, register_size) (find_regno): Add target_desc parameter. (gdbserver_expedite_regs, gdbserver_xmltarget): Delete declarations. * remote-utils.c: Include tdesc.h. (outreg, prepare_resume_reply): Adjust. * server.c: Include tdesc.h. (gdbserver_xmltarget): Delete declaration. (get_features_xml, process_serial_event): Adjust. * server.h [IN_PROCESS_AGENT] (struct target_desc): Forward declare. (struct process_info) <tdesc>: New field. (ipa_tdesc): Declare. * tdesc.c: New file. * tdesc.h: New file. * tracepoint.c: Include tdesc.h. [IN_PROCESS_AGENT] (ipa_tdesc): Define. (get_context_regcache): Adjust to pass ipa_tdesc down. (do_action_at_tracepoint): Adjust to get the register cache size from the context regcache's description. (traceframe_walk_blocks): Adjust to get the register cache size from the current trace frame's description. (traceframe_get_pc): Adjust to get current trace frame's description and pass it down. (gdb_collect): Adjust to get the register cache size from the IPA's description. * linux-amd64-ipa.c (tdesc_amd64_linux): Declare. (gdbserver_xmltarget): Delete. (initialize_low_tracepoint): Set the ipa's target description. * linux-i386-ipa.c (tdesc_i386_linux): Declare. (initialize_low_tracepoint): Set the ipa's target description. * linux-x86-low.c: Include tdesc.h. [__x86_64__] (is_64bit_tdesc): New. (ps_get_thread_area, x86_get_thread_area): Use it. (i386_cannot_store_register): Rename to ... (x86_cannot_store_register): ... this. Use is_64bit_tdesc. (i386_cannot_fetch_register): Rename to ... (x86_cannot_fetch_register): ... this. Use is_64bit_tdesc. (x86_fill_gregset, x86_store_gregset): Adjust register_size calls to new interface. (target_regsets): Rename to ... (x86_regsets): ... this. (x86_get_pc, x86_set_pc): Adjust register_size calls to new interface. (x86_siginfo_fixup): Use is_64bit_tdesc. [__x86_64__] (tdesc_amd64_linux, tdesc_amd64_avx_linux) (tdesc_x32_avx_linux, tdesc_x32_linux) (tdesc_i386_linux, tdesc_i386_mmx_linux, tdesc_i386_avx_linux): Declare. (x86_linux_update_xmltarget): Delete. (I386_LINUX_XSAVE_XCR0_OFFSET): Define. (have_ptrace_getfpxregs, have_ptrace_getregset): New. (AMD64_LINUX_USER64_CS): New. (x86_linux_read_description): New, based on x86_linux_update_xmltarget. (same_process_callback): New. (x86_arch_setup_process_callback): New. (x86_linux_update_xmltarget): New. (x86_regsets_info): New. (amd64_linux_regs_info): New. (i386_linux_usrregs_info): New. (i386_linux_regs_info): New. (x86_linux_regs_info): New. (x86_arch_setup): Reimplement. (x86_install_fast_tracepoint_jump_pad): Use is_64bit_tdesc. (x86_emit_ops): Ditto. (the_low_target): Adjust. Install x86_linux_regs_info, x86_cannot_fetch_register, and x86_cannot_store_register. (initialize_low_arch): New. * linux-ia64-low.c (tdesc_ia64): Declare. (ia64_fetch_register): Adjust. (ia64_usrregs_info, regs_info): New globals. (ia64_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-sparc-low.c (tdesc_sparc64): Declare. (sparc_fill_gregset_to_stack, sparc_store_gregset_from_stack): Adjust. (sparc_arch_setup): New function. (sparc_regsets_info, sparc_usrregs_info, regs_info): New globals. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-ppc-low.c (tdesc_powerpc_32l, tdesc_powerpc_altivec32l) (tdesc_powerpc_cell32l, tdesc_powerpc_vsx32l) (tdesc_powerpc_isa205_32l, tdesc_powerpc_isa205_altivec32l) (tdesc_powerpc_isa205_vsx32l, tdesc_powerpc_e500l) (tdesc_powerpc_64l, tdesc_powerpc_altivec64l) (tdesc_powerpc_cell64l, tdesc_powerpc_vsx64l) (tdesc_powerpc_isa205_64l, tdesc_powerpc_isa205_altivec64l) (tdesc_powerpc_isa205_vsx64l): Declare. (ppc_cannot_store_register, ppc_collect_ptrace_register) (ppc_supply_ptrace_register, parse_spufs_run, ppc_get_pc) (ppc_set_pc, ppc_get_hwcap): Adjust. (ppc_usrregs_info): Forward declare. (!__powerpc64__) ppc_regmap_adjusted: New global. (ppc_arch_setup): Adjust to the current process'es target description. (ppc_fill_vsxregset, ppc_store_vsxregset, ppc_fill_vrregset) (ppc_store_vrregset, ppc_fill_evrregset, ppc_store_evrregse) (ppc_store_evrregset): Adjust. (target_regsets): Rename to ... (ppc_regsets): ... this, and make static. (ppc_usrregs_info, ppc_regsets_info, regs_info): New globals. (ppc_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-s390-low.c (tdesc_s390_linux32, tdesc_s390_linux32v1) (tdesc_s390_linux32v2, tdesc_s390_linux64, tdesc_s390_linux64v1) (tdesc_s390_linux64v2, tdesc_s390x_linux64, tdesc_s390x_linux64v1) (tdesc_s390x_linux64v2): Declare. (s390_collect_ptrace_register, s390_supply_ptrace_register) (s390_fill_gregset, s390_store_last_break): Adjust. (target_regsets): Rename to ... (s390_regsets): ... this, and make static. (s390_get_pc, s390_set_pc): Adjust. (s390_get_hwcap): New target_desc parameter, and use it. [__s390x__] (have_hwcap_s390_high_gprs): New global. (s390_arch_setup): Adjust to set the current process'es target description. Don't adjust the regmap. (s390_usrregs_info, s390_regsets_info, regs_info): New globals. [__s390x__] (s390_usrregs_info_3264, s390_regsets_info_3264) (regs_info_3264): New globals. (s390_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-mips-low.c (tdesc_mips_linux, tdesc_mips_dsp_linux) (tdesc_mips64_linux, tdesc_mips64_dsp_linux): Declare. [__mips64] (init_registers_mips_linux) (init_registers_mips_dsp_linux): Delete defines. [__mips64] (tdesc_mips_linux, tdesc_mips_dsp_linux): New defines. (have_dsp): New global. (mips_read_description): New, based on mips_arch_setup. (mips_arch_setup): Reimplement. (get_usrregs_info): New function. (mips_cannot_fetch_register, mips_cannot_store_register) (mips_get_pc, mips_set_pc, mips_fill_gregset, mips_store_gregset) (mips_fill_fpregset, mips_store_fpregset): Adjust. (target_regsets): Rename to ... (mips_regsets): ... this, and make static. (mips_regsets_info, mips_dsp_usrregs_info, mips_usrregs_info) (dsp_regs_info, regs_info): New globals. (mips_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-arm-low.c (tdesc_arm, tdesc_arm_with_iwmmxt) (tdesc_arm_with_vfpv2, tdesc_arm_with_vfpv3, tdesc_arm_with_neon): Declare. (arm_fill_vfpregset, arm_store_vfpregset): Adjust. (arm_read_description): New, with bits factored from arm_arch_setup. (arm_arch_setup): Reimplement. (target_regsets): Rename to ... (arm_regsets): ... this, and make static. (arm_regsets_info, arm_usrregs_info, regs_info): New globals. (arm_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-m68k-low.c (tdesc_m68k): Declare. (target_regsets): Rename to ... (m68k_regsets): ... this, and make static. (m68k_regsets_info, m68k_usrregs_info, regs_info): New globals. (m68k_regs_info): New function. (m68k_arch_setup): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-sh-low.c (tdesc_sharch): Declare. (target_regsets): Rename to ... (sh_regsets): ... this, and make static. (sh_regsets_info, sh_usrregs_info, regs_info): New globals. (sh_regs_info, sh_arch_setup): New functions. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-bfin-low.c (tdesc_bfin): Declare. (bfin_arch_setup): New function. (bfin_usrregs_info, regs_info): New globals. (bfin_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-cris-low.c (tdesc_cris): Declare. (cris_arch_setup): New function. (cris_usrregs_info, regs_info): New globals. (cris_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-cris-low.c (tdesc_crisv32): Declare. (cris_arch_setup): New function. (cris_regsets_info, cris_usrregs_info, regs_info): New globals. (cris_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-m32r-low.c (tdesc_m32r): Declare. (m32r_arch_setup): New function. (m32r_usrregs_info, regs_info): New globals. (m32r_regs_info): Adjust. (initialize_low_arch): New function. * linux-tic6x-low.c (tdesc_tic6x_c64xp_linux) (tdesc_tic6x_c64x_linux, tdesc_tic6x_c62x_linux): Declare. (tic6x_usrregs_info): Forward declare. (tic6x_read_description): New function, based on ... (tic6x_arch_setup): ... this. Reimplement. (target_regsets): Rename to ... (tic6x_regsets): ... this, and make static. (tic6x_regsets_info, tic6x_usrregs_info, regs_info): New globals. (tic6x_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-xtensa-low.c (tdesc_xtensa): Declare. (xtensa_fill_gregset, xtensa_store_gregset): Adjust. (target_regsets): Rename to ... (xtensa_regsets): ... this, and make static. (xtensa_regsets_info, xtensa_usrregs_info, regs_info): New globals. (xtensa_arch_setup, xtensa_regs_info): New functions. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-nios2-low.c (tdesc_nios2_linux): Declare. (nios2_arch_setup): Set the current process'es tdesc. (target_regsets): Rename to ... (nios2_regsets): ... this. (nios2_regsets_info, nios2_usrregs_info, regs_info): New globals. (nios2_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-aarch64-low.c (tdesc_aarch64): Declare. (aarch64_arch_setup): Set the current process'es tdesc. (target_regsets): Rename to ... (aarch64_regsets): ... this. (aarch64_regsets_info, aarch64_usrregs_info, regs_info): New globals. (aarch64_regs_info): New function. (the_low_target): Adjust. (initialize_low_arch): New function. * linux-tile-low.c (tdesc_tilegx, tdesc_tilegx32): Declare globals. (target_regsets): Rename to ... (tile_regsets): ... this. (tile_regsets_info, tile_usrregs_info, regs_info): New globals. (tile_regs_info): New function. (tile_arch_setup): Set the current process'es tdesc. (the_low_target): Adjust. (initialize_low_arch): New function. * spu-low.c (tdesc_spu): Declare. (spu_create_inferior, spu_attach): Set the new process'es tdesc. * win32-arm-low.c (tdesc_arm): Declare. (arm_arch_setup): New function. (the_low_target): Install arm_arch_setup instead of init_registers_arm. * win32-i386-low.c (tdesc_i386, tdesc_amd64): Declare. (init_windows_x86): Rename to ... (i386_arch_setup): ... this. Set `win32_tdesc'. (the_low_target): Adjust. * win32-low.c (win32_tdesc): New global. (child_add_thread): Don't create the thread cache here. (do_initial_child_stuff): Set the new process'es tdesc. * win32-low.h (struct target_desc): Forward declare. (win32_tdesc): Declare. * lynx-i386-low.c (tdesc_i386): Declare global. (lynx_i386_arch_setup): Set `lynx_tdesc'. * lynx-low.c (lynx_tdesc): New global. (lynx_add_process): Set the new process'es tdesc. * lynx-low.h (struct target_desc): Forward declare. (lynx_tdesc): Declare global. * lynx-ppc-low.c (tdesc_powerpc_32): Declare global. (lynx_ppc_arch_setup): Set `lynx_tdesc'. * nto-low.c (nto_tdesc): New global. (do_attach): Set the new process'es tdesc. * nto-low.h (struct target_desc): Forward declare. (nto_tdesc): Declare. * nto-x86-low.c (tdesc_i386): Declare. (nto_x86_arch_setup): Set `nto_tdesc'. gdb/ 2013-06-07 Pedro Alves <palves@redhat.com> * regformats/regdat.sh: Output #include tdesc.h. Make globals static. Output a global target description pointer. (init_registers_${name}): Adjust to initialize a target description structure.
2013-06-07 12:46:59 +02:00
proc = add_process (status.pid, 1);
proc->tdesc = nto_tdesc;
TRACE ("Adding thread: pid=%d tid=%ld\n", status.pid,
Remove ptid_get_lwp This removes ptid_get_lwp in favor of calling the ptid_t::lwp method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_lwp): Remove. * common/ptid.h (ptid_get_lwp): Don't declare. * aarch64-linux-nat.c: Update. * ada-tasks.c: Update. * aix-thread.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * corelow.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gnu-nat.c: Update. * i386-cygwin-tdep.c: Update. * i386-gnu-nat.c: Update. * i386-linux-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * mips-linux-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * obsd-nat.c: Update. * ppc-fbsd-nat.c: Update. * ppc-linux-nat.c: Update. * procfs.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * remote.c: Update. * s390-linux-nat.c: Update. * sol-thread.c: Update. * sol2-tdep.c: Update. * spu-linux-nat.c: Update. * x86-linux-nat.c: Update. * xtensa-linux-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * thread-db.c: Update.
2018-06-11 20:10:09 +02:00
ptid.lwp ());
nto_find_new_threads (&nto_inferior);
}
else
{
do_detach ();
return -1;
}
return pid;
}
/* Read or write LEN bytes from/to inferior's MEMADDR memory address
into gdbservers's MYADDR buffer. Return number of bytes actually
transfered. */
static int
nto_xfer_memory (off_t memaddr, unsigned char *myaddr, int len,
int dowrite)
{
int nbytes = 0;
if (lseek (nto_inferior.ctl_fd, memaddr, SEEK_SET) == memaddr)
{
if (dowrite)
nbytes = write (nto_inferior.ctl_fd, myaddr, len);
else
nbytes = read (nto_inferior.ctl_fd, myaddr, len);
if (nbytes < 0)
nbytes = 0;
}
if (nbytes == 0)
{
int e = errno;
TRACE ("Error in %s : errno=%d (%s)\n", __func__, e, safe_strerror (e));
}
return nbytes;
}
/* Insert or remove breakpoint or watchpoint at address ADDR.
TYPE can be one of Neutrino breakpoint types. SIZE must be 0 for
inserting the point, -1 for removing it.
Return 0 on success, 1 otherwise. */
static int
nto_breakpoint (CORE_ADDR addr, int type, int size)
{
procfs_break brk;
brk.type = type;
brk.addr = addr;
brk.size = size;
if (devctl (nto_inferior.ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0)
!= EOK)
return 1;
return 0;
}
/* Read auxiliary vector from inferior's initial stack into gdbserver's
MYADDR buffer, up to LEN bytes.
Return number of bytes read. */
static int
nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack,
unsigned char *myaddr,
unsigned int len)
{
int data_ofs = 0;
int anint;
unsigned int len_read = 0;
/* Skip over argc, argv and envp... Comment from ldd.c:
The startup frame is set-up so that we have:
auxv
NULL
...
envp2
envp1 <----- void *frame + (argc + 2) * sizeof(char *)
NULL
...
argv2
argv1
argc <------ void * frame
On entry to ldd, frame gives the address of argc on the stack. */
if (nto_xfer_memory (initial_stack, (unsigned char *)&anint,
sizeof (anint), 0) != sizeof (anint))
return 0;
/* Size of pointer is assumed to be 4 bytes (32 bit arch. ) */
data_ofs += (anint + 2) * sizeof (void *); /* + 2 comes from argc itself and
NULL terminating pointer in
argv. */
/* Now loop over env table: */
while (nto_xfer_memory (initial_stack + data_ofs,
(unsigned char *)&anint, sizeof (anint), 0)
== sizeof (anint))
{
data_ofs += sizeof (anint);
if (anint == 0)
break;
}
initial_stack += data_ofs;
memset (myaddr, 0, len);
while (len_read <= len - sizeof (auxv_t))
{
auxv_t *auxv = (auxv_t *)myaddr;
/* Search backwards until we have read AT_PHDR (num. 3),
AT_PHENT (num 4), AT_PHNUM (num 5) */
if (nto_xfer_memory (initial_stack, (unsigned char *)auxv,
sizeof (auxv_t), 0) == sizeof (auxv_t))
{
if (auxv->a_type != AT_NULL)
{
auxv++;
len_read += sizeof (auxv_t);
}
if (auxv->a_type == AT_PHNUM) /* That's all we need. */
break;
initial_stack += sizeof (auxv_t);
}
else
break;
}
TRACE ("auxv: len_read: %d\n", len_read);
return len_read;
}
Share fork_inferior et al with gdbserver This is the most important (and the biggest, sorry) patch of the series. It moves fork_inferior from gdb/fork-child.c to nat/fork-inferior.c and makes all the necessary adjustments to both GDB and gdbserver to make sure everything works OK. There is no "most important change" with this patch; all changes are made in a progressive way, making sure that gdbserver had the necessary features while not breaking GDB at the same time. I decided to go ahead and implement a partial support for starting the inferior with a shell on gdbserver, although the full feature comes in the next patch. The user won't have the option to disable the startup-with-shell, and also won't be able to change which shell gdbserver will use (other than setting the $SHELL environment variable, that is). Everything is working as expected, and no regressions were present during the tests. gdb/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> Pedro Alves <palves@redhat.com> * Makefile.in (HFILES_NO_SRCDIR): Add "common/common-inferior.h" and "nat/fork-inferior.h". * common/common-inferior.h: New file, with contents from "gdb/inferior.h". * commom/common-utils.c: Include "common-utils.h". (stringify_argv): New function. * common/common-utils.h (stringify_argv): New prototype. * configure.nat: Add "fork-inferior.o" as a dependency for "*linux*", "fbsd*" and "nbsd*" hosts. * corefile.c (get_exec_file): Update comment. * darwin-nat.c (darwin_ptrace_him): Call "gdb_startup_inferior" instead of "startup_inferior". (darwin_create_inferior): Call "add_thread_silent" after "fork_inferior". * fork-child.c: Cleanup unnecessary includes. (SHELL_FILE): Move to "common/common-fork-child.c". (environ): Likewise. (exec_wrapper): Initialize. (get_exec_wrapper): New function. (breakup_args): Move to "common/common-fork-child.c"; rename to "breakup_args_for_exec". (escape_bang_in_quoted_argument): Move to "common/common-fork-child.c". (saved_ui): New variable. (prefork_hook): New function. (postfork_hook): Likewise. (postfork_child_hook): Likewise. (gdb_startup_inferior): Likewise. (fork_inferior): Move to "common/common-fork-child.c". Update function to support gdbserver. (startup_inferior): Likewise. * gdbcore.h (get_exec_file): Remove declaration. * gnu-nat.c (gnu_create_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * inf-ptrace.c: Include "nat/fork-inferior.h" and "utils.h". (inf_ptrace_create_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * inferior.h: Include "common-inferior.h". (trace_start_error): Move to "common/common-utils.h". (trace_start_error_with_name): Likewise. (fork_inferior): Move prototype to "nat/fork-inferior.h". (startup_inferior): Likewise. (gdb_startup_inferior): New prototype. * nat/fork-inferior.c: New file, with contents from "fork-child.c". * nat/fork-inferior.h: New file. * procfs.c (procfs_init_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * target.h (target_terminal_init): Move prototype to "target/target.h". (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * target/target.h (target_terminal_init): New prototype, moved from "target.h". (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * utils.c (gdb_flush_out_err): New function. gdb/gdbserver/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> Pedro Alves <palves@redhat.com> * Makefile.in (SFILES): Add "nat/fork-inferior.o". * configure: Regenerate. * configure.srv (srv_linux_obj): Add "fork-child.o" and "fork-inferior.o". (i[34567]86-*-lynxos*): Likewise. (spu*-*-*): Likewise. * fork-child.c: New file. * linux-low.c: Include "common-inferior.h", "nat/fork-inferior.h" and "environ.h". (linux_ptrace_fun): New function. (linux_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". (linux_request_interrupt): Delete "signal_pid". * lynx-low.c: Include "common-inferior.h" and "nat/fork-inferior.h". (lynx_ptrace_fun): New function. (lynx_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". * nto-low.c (nto_create_inferior): Adjust function prototype and code to reflect change on "target.h". Update comments. * server.c: Include "common-inferior.h", "nat/fork-inferior.h", "common-terminal.h" and "environ.h". (terminal_fd): Moved to fork-child.c. (old_foreground_pgrp): Likewise. (restore_old_foreground_pgrp): Likewise. (last_status): Make it global. (last_ptid): Likewise. (our_environ): New variable. (startup_with_shell): Likewise. (program_name): Likewise. (program_argv): Rename to... (program_args): ...this. (wrapper_argv): New variable. (start_inferior): Delete function. (get_exec_wrapper): New function. (get_exec_file): Likewise. (get_environ): Likewise. (prefork_hook): Likewise. (post_fork_inferior): Likewise. (postfork_hook): Likewise. (postfork_child_hook): Likewise. (handle_v_run): Update code to deal with arguments coming from the remote host. Update calls from "start_inferior" to "create_inferior". (captured_main): Likewise. Initialize environment variable. Call "have_job_control". * server.h (post_fork_inferior): New prototype. (get_environ): Likewise. (last_status): Declare. (last_ptid): Likewise. (signal_pid): Likewise. * spu-low.c: Include "common-inferior.h" and "nat/fork-inferior.h". (spu_ptrace_fun): New function. (spu_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". * target.c (target_terminal_init): New function. (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * target.h: Include <vector>. (struct target_ops) <create_inferior>: Update prototype. (create_inferior): Update macro. * utils.c (gdb_flush_out_err): New function. * win32-low.c (win32_create_inferior): Adjust function prototype and code to reflect change on "target.h". gdb/testsuite/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> * gdb.server/non-existing-program.exp: Update regex in order to reflect the fact that gdbserver is now using fork_inferior (with a shell) to startup the inferior.
2016-12-23 03:11:11 +01:00
/* Start inferior specified by PROGRAM, using PROGRAM_ARGS as its
arguments. */
int
nto_process_target::create_inferior (const char *program,
const std::vector<char *> &program_args)
{
struct inheritance inherit;
pid_t pid;
sigset_t set;
Share fork_inferior et al with gdbserver This is the most important (and the biggest, sorry) patch of the series. It moves fork_inferior from gdb/fork-child.c to nat/fork-inferior.c and makes all the necessary adjustments to both GDB and gdbserver to make sure everything works OK. There is no "most important change" with this patch; all changes are made in a progressive way, making sure that gdbserver had the necessary features while not breaking GDB at the same time. I decided to go ahead and implement a partial support for starting the inferior with a shell on gdbserver, although the full feature comes in the next patch. The user won't have the option to disable the startup-with-shell, and also won't be able to change which shell gdbserver will use (other than setting the $SHELL environment variable, that is). Everything is working as expected, and no regressions were present during the tests. gdb/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> Pedro Alves <palves@redhat.com> * Makefile.in (HFILES_NO_SRCDIR): Add "common/common-inferior.h" and "nat/fork-inferior.h". * common/common-inferior.h: New file, with contents from "gdb/inferior.h". * commom/common-utils.c: Include "common-utils.h". (stringify_argv): New function. * common/common-utils.h (stringify_argv): New prototype. * configure.nat: Add "fork-inferior.o" as a dependency for "*linux*", "fbsd*" and "nbsd*" hosts. * corefile.c (get_exec_file): Update comment. * darwin-nat.c (darwin_ptrace_him): Call "gdb_startup_inferior" instead of "startup_inferior". (darwin_create_inferior): Call "add_thread_silent" after "fork_inferior". * fork-child.c: Cleanup unnecessary includes. (SHELL_FILE): Move to "common/common-fork-child.c". (environ): Likewise. (exec_wrapper): Initialize. (get_exec_wrapper): New function. (breakup_args): Move to "common/common-fork-child.c"; rename to "breakup_args_for_exec". (escape_bang_in_quoted_argument): Move to "common/common-fork-child.c". (saved_ui): New variable. (prefork_hook): New function. (postfork_hook): Likewise. (postfork_child_hook): Likewise. (gdb_startup_inferior): Likewise. (fork_inferior): Move to "common/common-fork-child.c". Update function to support gdbserver. (startup_inferior): Likewise. * gdbcore.h (get_exec_file): Remove declaration. * gnu-nat.c (gnu_create_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * inf-ptrace.c: Include "nat/fork-inferior.h" and "utils.h". (inf_ptrace_create_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * inferior.h: Include "common-inferior.h". (trace_start_error): Move to "common/common-utils.h". (trace_start_error_with_name): Likewise. (fork_inferior): Move prototype to "nat/fork-inferior.h". (startup_inferior): Likewise. (gdb_startup_inferior): New prototype. * nat/fork-inferior.c: New file, with contents from "fork-child.c". * nat/fork-inferior.h: New file. * procfs.c (procfs_init_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * target.h (target_terminal_init): Move prototype to "target/target.h". (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * target/target.h (target_terminal_init): New prototype, moved from "target.h". (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * utils.c (gdb_flush_out_err): New function. gdb/gdbserver/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> Pedro Alves <palves@redhat.com> * Makefile.in (SFILES): Add "nat/fork-inferior.o". * configure: Regenerate. * configure.srv (srv_linux_obj): Add "fork-child.o" and "fork-inferior.o". (i[34567]86-*-lynxos*): Likewise. (spu*-*-*): Likewise. * fork-child.c: New file. * linux-low.c: Include "common-inferior.h", "nat/fork-inferior.h" and "environ.h". (linux_ptrace_fun): New function. (linux_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". (linux_request_interrupt): Delete "signal_pid". * lynx-low.c: Include "common-inferior.h" and "nat/fork-inferior.h". (lynx_ptrace_fun): New function. (lynx_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". * nto-low.c (nto_create_inferior): Adjust function prototype and code to reflect change on "target.h". Update comments. * server.c: Include "common-inferior.h", "nat/fork-inferior.h", "common-terminal.h" and "environ.h". (terminal_fd): Moved to fork-child.c. (old_foreground_pgrp): Likewise. (restore_old_foreground_pgrp): Likewise. (last_status): Make it global. (last_ptid): Likewise. (our_environ): New variable. (startup_with_shell): Likewise. (program_name): Likewise. (program_argv): Rename to... (program_args): ...this. (wrapper_argv): New variable. (start_inferior): Delete function. (get_exec_wrapper): New function. (get_exec_file): Likewise. (get_environ): Likewise. (prefork_hook): Likewise. (post_fork_inferior): Likewise. (postfork_hook): Likewise. (postfork_child_hook): Likewise. (handle_v_run): Update code to deal with arguments coming from the remote host. Update calls from "start_inferior" to "create_inferior". (captured_main): Likewise. Initialize environment variable. Call "have_job_control". * server.h (post_fork_inferior): New prototype. (get_environ): Likewise. (last_status): Declare. (last_ptid): Likewise. (signal_pid): Likewise. * spu-low.c: Include "common-inferior.h" and "nat/fork-inferior.h". (spu_ptrace_fun): New function. (spu_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". * target.c (target_terminal_init): New function. (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * target.h: Include <vector>. (struct target_ops) <create_inferior>: Update prototype. (create_inferior): Update macro. * utils.c (gdb_flush_out_err): New function. * win32-low.c (win32_create_inferior): Adjust function prototype and code to reflect change on "target.h". gdb/testsuite/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> * gdb.server/non-existing-program.exp: Update regex in order to reflect the fact that gdbserver is now using fork_inferior (with a shell) to startup the inferior.
2016-12-23 03:11:11 +01:00
std::string str_program_args = stringify_argv (program_args);
TRACE ("%s %s\n", __func__, program);
/* Clear any pending SIGUSR1's but keep the behavior the same. */
signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
sigemptyset (&set);
sigaddset (&set, SIGUSR1);
sigprocmask (SIG_UNBLOCK, &set, NULL);
memset (&inherit, 0, sizeof (inherit));
inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
inherit.pgroup = SPAWN_NEWPGROUP;
Share fork_inferior et al with gdbserver This is the most important (and the biggest, sorry) patch of the series. It moves fork_inferior from gdb/fork-child.c to nat/fork-inferior.c and makes all the necessary adjustments to both GDB and gdbserver to make sure everything works OK. There is no "most important change" with this patch; all changes are made in a progressive way, making sure that gdbserver had the necessary features while not breaking GDB at the same time. I decided to go ahead and implement a partial support for starting the inferior with a shell on gdbserver, although the full feature comes in the next patch. The user won't have the option to disable the startup-with-shell, and also won't be able to change which shell gdbserver will use (other than setting the $SHELL environment variable, that is). Everything is working as expected, and no regressions were present during the tests. gdb/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> Pedro Alves <palves@redhat.com> * Makefile.in (HFILES_NO_SRCDIR): Add "common/common-inferior.h" and "nat/fork-inferior.h". * common/common-inferior.h: New file, with contents from "gdb/inferior.h". * commom/common-utils.c: Include "common-utils.h". (stringify_argv): New function. * common/common-utils.h (stringify_argv): New prototype. * configure.nat: Add "fork-inferior.o" as a dependency for "*linux*", "fbsd*" and "nbsd*" hosts. * corefile.c (get_exec_file): Update comment. * darwin-nat.c (darwin_ptrace_him): Call "gdb_startup_inferior" instead of "startup_inferior". (darwin_create_inferior): Call "add_thread_silent" after "fork_inferior". * fork-child.c: Cleanup unnecessary includes. (SHELL_FILE): Move to "common/common-fork-child.c". (environ): Likewise. (exec_wrapper): Initialize. (get_exec_wrapper): New function. (breakup_args): Move to "common/common-fork-child.c"; rename to "breakup_args_for_exec". (escape_bang_in_quoted_argument): Move to "common/common-fork-child.c". (saved_ui): New variable. (prefork_hook): New function. (postfork_hook): Likewise. (postfork_child_hook): Likewise. (gdb_startup_inferior): Likewise. (fork_inferior): Move to "common/common-fork-child.c". Update function to support gdbserver. (startup_inferior): Likewise. * gdbcore.h (get_exec_file): Remove declaration. * gnu-nat.c (gnu_create_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * inf-ptrace.c: Include "nat/fork-inferior.h" and "utils.h". (inf_ptrace_create_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * inferior.h: Include "common-inferior.h". (trace_start_error): Move to "common/common-utils.h". (trace_start_error_with_name): Likewise. (fork_inferior): Move prototype to "nat/fork-inferior.h". (startup_inferior): Likewise. (gdb_startup_inferior): New prototype. * nat/fork-inferior.c: New file, with contents from "fork-child.c". * nat/fork-inferior.h: New file. * procfs.c (procfs_init_inferior): Call "gdb_startup_inferior" instead of "startup_inferior". Call "add_thread_silent" after "fork_inferior". * target.h (target_terminal_init): Move prototype to "target/target.h". (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * target/target.h (target_terminal_init): New prototype, moved from "target.h". (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * utils.c (gdb_flush_out_err): New function. gdb/gdbserver/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> Pedro Alves <palves@redhat.com> * Makefile.in (SFILES): Add "nat/fork-inferior.o". * configure: Regenerate. * configure.srv (srv_linux_obj): Add "fork-child.o" and "fork-inferior.o". (i[34567]86-*-lynxos*): Likewise. (spu*-*-*): Likewise. * fork-child.c: New file. * linux-low.c: Include "common-inferior.h", "nat/fork-inferior.h" and "environ.h". (linux_ptrace_fun): New function. (linux_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". (linux_request_interrupt): Delete "signal_pid". * lynx-low.c: Include "common-inferior.h" and "nat/fork-inferior.h". (lynx_ptrace_fun): New function. (lynx_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". * nto-low.c (nto_create_inferior): Adjust function prototype and code to reflect change on "target.h". Update comments. * server.c: Include "common-inferior.h", "nat/fork-inferior.h", "common-terminal.h" and "environ.h". (terminal_fd): Moved to fork-child.c. (old_foreground_pgrp): Likewise. (restore_old_foreground_pgrp): Likewise. (last_status): Make it global. (last_ptid): Likewise. (our_environ): New variable. (startup_with_shell): Likewise. (program_name): Likewise. (program_argv): Rename to... (program_args): ...this. (wrapper_argv): New variable. (start_inferior): Delete function. (get_exec_wrapper): New function. (get_exec_file): Likewise. (get_environ): Likewise. (prefork_hook): Likewise. (post_fork_inferior): Likewise. (postfork_hook): Likewise. (postfork_child_hook): Likewise. (handle_v_run): Update code to deal with arguments coming from the remote host. Update calls from "start_inferior" to "create_inferior". (captured_main): Likewise. Initialize environment variable. Call "have_job_control". * server.h (post_fork_inferior): New prototype. (get_environ): Likewise. (last_status): Declare. (last_ptid): Likewise. (signal_pid): Likewise. * spu-low.c: Include "common-inferior.h" and "nat/fork-inferior.h". (spu_ptrace_fun): New function. (spu_create_inferior): Adjust function prototype to reflect change on "target.h". Adjust function code to use "fork_inferior". * target.c (target_terminal_init): New function. (target_terminal_inferior): Likewise. (target_terminal_ours): Likewise. * target.h: Include <vector>. (struct target_ops) <create_inferior>: Update prototype. (create_inferior): Update macro. * utils.c (gdb_flush_out_err): New function. * win32-low.c (win32_create_inferior): Adjust function prototype and code to reflect change on "target.h". gdb/testsuite/ChangeLog: 2017-06-07 Sergio Durigan Junior <sergiodj@redhat.com> * gdb.server/non-existing-program.exp: Update regex in order to reflect the fact that gdbserver is now using fork_inferior (with a shell) to startup the inferior.
2016-12-23 03:11:11 +01:00
pid = spawnp (program, 0, NULL, &inherit,
(char *) str_program_args.c_str (), 0);
sigprocmask (SIG_BLOCK, &set, NULL);
if (pid == -1)
return -1;
if (do_attach (pid) != pid)
return -1;
return pid;
}
/* Attach to process PID. */
int
nto_process_target::attach (unsigned long pid)
{
TRACE ("%s %ld\n", __func__, pid);
if (do_attach (pid) != pid)
error ("Unable to attach to %ld\n", pid);
return 0;
}
/* Send signal to process PID. */
int
nto_process_target::kill (process_info *proc)
{
int pid = proc->pid;
TRACE ("%s %d\n", __func__, pid);
kill (pid, SIGKILL);
do_detach ();
return 0;
}
/* Detach from process PID. */
int
nto_process_target::detach (process_info *proc)
{
TRACE ("%s %d\n", __func__, proc->pid);
do_detach ();
return 0;
}
void
nto_process_target::mourn (struct process_info *process)
{
remove_process (process);
}
void
nto_process_target::join (int pid)
{
error (_("nto target does not implement the join op"));
}
/* Check if the given thread is alive.
Return true if alive, false otherwise. */
bool
nto_process_target::thread_alive (ptid_t ptid)
{
int res;
Remove ptid_get_pid This removes ptid_get_pid in favor of calling the ptid_t::pid method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_pid): Remove. * common/ptid.h (ptid_get_pid): Don't declare. * aarch64-linux-nat.c: Update. * ada-lang.c: Update. * aix-thread.c: Update. * alpha-bsd-nat.c: Update. * amd64-fbsd-nat.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * arm-nbsd-nat.c: Update. * auxv.c: Update. * break-catch-syscall.c: Update. * breakpoint.c: Update. * bsd-uthread.c: Update. * corelow.c: Update. * ctf.c: Update. * darwin-nat.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gcore.c: Update. * gnu-nat.c: Update. * hppa-nbsd-nat.c: Update. * hppa-obsd-nat.c: Update. * i386-fbsd-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infcmd.c: Update. * inferior.c: Update. * inferior.h: Update. * inflow.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * m68k-bsd-nat.c: Update. * mi/mi-interp.c: Update. * mi/mi-main.c: Update. * mips-linux-nat.c: Update. * mips-nbsd-nat.c: Update. * mips64-obsd-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * nto-procfs.c: Update. * obsd-nat.c: Update. * ppc-linux-nat.c: Update. * ppc-nbsd-nat.c: Update. * ppc-obsd-nat.c: Update. * proc-service.c: Update. * procfs.c: Update. * python/py-inferior.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * record.c: Update. * remote-sim.c: Update. * remote.c: Update. * rs6000-nat.c: Update. * s390-linux-nat.c: Update. * sh-nbsd-nat.c: Update. * sol-thread.c: Update. * sparc-nat.c: Update. * sparc64-tdep.c: Update. * spu-linux-nat.c: Update. * spu-tdep.c: Update. * target-debug.h: Update. * target.c: Update. * thread.c: Update. * tid-parse.c: Update. * tracefile-tfile.c: Update. * vax-bsd-nat.c: Update. * windows-nat.c: Update. * x86-linux-nat.c: Update. * x86-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * mem-break.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * tracepoint.c: Update.
2018-06-11 20:05:27 +02:00
TRACE ("%s pid:%d tid:%d\n", __func__, ptid.pid (),
Remove ptid_get_lwp This removes ptid_get_lwp in favor of calling the ptid_t::lwp method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_lwp): Remove. * common/ptid.h (ptid_get_lwp): Don't declare. * aarch64-linux-nat.c: Update. * ada-tasks.c: Update. * aix-thread.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * corelow.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gnu-nat.c: Update. * i386-cygwin-tdep.c: Update. * i386-gnu-nat.c: Update. * i386-linux-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * mips-linux-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * obsd-nat.c: Update. * ppc-fbsd-nat.c: Update. * ppc-linux-nat.c: Update. * procfs.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * remote.c: Update. * s390-linux-nat.c: Update. * sol-thread.c: Update. * sol2-tdep.c: Update. * spu-linux-nat.c: Update. * x86-linux-nat.c: Update. * xtensa-linux-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * thread-db.c: Update.
2018-06-11 20:10:09 +02:00
ptid.lwp ());
if (SignalKill (0, ptid.pid (), ptid.lwp (),
0, 0, 0) == -1)
res = 0;
else
res = 1;
TRACE ("%s: %s\n", __func__, res ? "yes" : "no");
return res;
}
/* Resume inferior's execution. */
void
nto_process_target::resume (thread_resume *resume_info, size_t n)
{
/* We can only work in all-stop mode. */
procfs_status status;
procfs_run run;
int err;
TRACE ("%s\n", __func__);
/* Workaround for aliasing rules violation. */
sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
nto_set_thread (resume_info->thread);
run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
if (resume_info->kind == resume_step)
run.flags |= _DEBUG_RUN_STEP;
run.flags |= _DEBUG_RUN_ARM;
sigemptyset (run_fault);
sigaddset (run_fault, FLTBPT);
sigaddset (run_fault, FLTTRACE);
sigaddset (run_fault, FLTILL);
sigaddset (run_fault, FLTPRIV);
sigaddset (run_fault, FLTBOUNDS);
sigaddset (run_fault, FLTIOVF);
sigaddset (run_fault, FLTIZDIV);
sigaddset (run_fault, FLTFPE);
sigaddset (run_fault, FLTPAGE);
sigaddset (run_fault, FLTSTACK);
sigaddset (run_fault, FLTACCESS);
sigemptyset (&run.trace);
if (resume_info->sig)
{
int signal_to_pass;
devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
0);
signal_to_pass = resume_info->sig;
if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
{
if (signal_to_pass != status.info.si_signo)
{
kill (status.pid, signal_to_pass);
run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
}
else /* Let it kill the program without telling us. */
sigdelset (&run.trace, signal_to_pass);
}
}
else
run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
sigfillset (&run.trace);
regcache_invalidate ();
err = devctl (nto_inferior.ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
if (err != EOK)
TRACE ("Error: %d \"%s\"\n", err, safe_strerror (err));
}
/* Wait for inferior's event.
Return ptid of thread that caused the event. */
ptid_t
nto_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
int target_options)
{
sigset_t set;
siginfo_t info;
procfs_status status;
const int trace_mask = (_DEBUG_FLAG_TRACE_EXEC | _DEBUG_FLAG_TRACE_RD
| _DEBUG_FLAG_TRACE_WR | _DEBUG_FLAG_TRACE_MODIFY);
TRACE ("%s\n", __func__);
ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
sigemptyset (&set);
sigaddset (&set, SIGUSR1);
devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
while (!(status.flags & _DEBUG_FLAG_ISTOP))
{
sigwaitinfo (&set, &info);
devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
0);
}
nto_find_new_threads (&nto_inferior);
if (status.flags & _DEBUG_FLAG_SSTEP)
{
TRACE ("SSTEP\n");
ourstatus->kind = TARGET_WAITKIND_STOPPED;
gdb/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. gdb/gdbserver/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. include/gdb/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 * gdb/signals.def: Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/arm/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/avr/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/common/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/cr16/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/d10v/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/erc32/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/m32c/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/ppc/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/rl78/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/rx/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout.
2012-05-24 18:51:47 +02:00
ourstatus->value.sig = GDB_SIGNAL_TRAP;
}
/* Was it a breakpoint? */
else if (status.flags & trace_mask)
{
TRACE ("STOPPED\n");
ourstatus->kind = TARGET_WAITKIND_STOPPED;
gdb/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. gdb/gdbserver/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. include/gdb/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 * gdb/signals.def: Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/arm/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/avr/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/common/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/cr16/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/d10v/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/erc32/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/m32c/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/ppc/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/rl78/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/rx/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout.
2012-05-24 18:51:47 +02:00
ourstatus->value.sig = GDB_SIGNAL_TRAP;
}
else if (status.flags & _DEBUG_FLAG_ISTOP)
{
TRACE ("ISTOP\n");
switch (status.why)
{
case _DEBUG_WHY_SIGNALLED:
TRACE (" SIGNALLED\n");
ourstatus->kind = TARGET_WAITKIND_STOPPED;
ourstatus->value.sig =
gdb_signal_from_host (status.info.si_signo);
nto_inferior.exit_signo = ourstatus->value.sig;
break;
case _DEBUG_WHY_FAULTED:
TRACE (" FAULTED\n");
ourstatus->kind = TARGET_WAITKIND_STOPPED;
if (status.info.si_signo == SIGTRAP)
{
ourstatus->value.sig = 0;
nto_inferior.exit_signo = 0;
}
else
{
ourstatus->value.sig =
gdb_signal_from_host (status.info.si_signo);
nto_inferior.exit_signo = ourstatus->value.sig;
}
break;
case _DEBUG_WHY_TERMINATED:
{
int waitval = 0;
TRACE (" TERMINATED\n");
Remove ptid_get_pid This removes ptid_get_pid in favor of calling the ptid_t::pid method. gdb/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * common/ptid.c (ptid_get_pid): Remove. * common/ptid.h (ptid_get_pid): Don't declare. * aarch64-linux-nat.c: Update. * ada-lang.c: Update. * aix-thread.c: Update. * alpha-bsd-nat.c: Update. * amd64-fbsd-nat.c: Update. * amd64-linux-nat.c: Update. * arm-linux-nat.c: Update. * arm-nbsd-nat.c: Update. * auxv.c: Update. * break-catch-syscall.c: Update. * breakpoint.c: Update. * bsd-uthread.c: Update. * corelow.c: Update. * ctf.c: Update. * darwin-nat.c: Update. * fbsd-nat.c: Update. * fbsd-tdep.c: Update. * gcore.c: Update. * gnu-nat.c: Update. * hppa-nbsd-nat.c: Update. * hppa-obsd-nat.c: Update. * i386-fbsd-nat.c: Update. * ia64-linux-nat.c: Update. * inf-ptrace.c: Update. * infcmd.c: Update. * inferior.c: Update. * inferior.h: Update. * inflow.c: Update. * infrun.c: Update. * linux-fork.c: Update. * linux-nat.c: Update. * linux-tdep.c: Update. * linux-thread-db.c: Update. * m68k-bsd-nat.c: Update. * mi/mi-interp.c: Update. * mi/mi-main.c: Update. * mips-linux-nat.c: Update. * mips-nbsd-nat.c: Update. * mips64-obsd-nat.c: Update. * nat/aarch64-linux-hw-point.c: Update. * nat/aarch64-linux.c: Update. * nat/linux-btrace.c: Update. * nat/linux-osdata.c: Update. * nat/linux-procfs.c: Update. * nat/x86-linux-dregs.c: Update. * nto-procfs.c: Update. * obsd-nat.c: Update. * ppc-linux-nat.c: Update. * ppc-nbsd-nat.c: Update. * ppc-obsd-nat.c: Update. * proc-service.c: Update. * procfs.c: Update. * python/py-inferior.c: Update. * python/py-infthread.c: Update. * ravenscar-thread.c: Update. * record.c: Update. * remote-sim.c: Update. * remote.c: Update. * rs6000-nat.c: Update. * s390-linux-nat.c: Update. * sh-nbsd-nat.c: Update. * sol-thread.c: Update. * sparc-nat.c: Update. * sparc64-tdep.c: Update. * spu-linux-nat.c: Update. * spu-tdep.c: Update. * target-debug.h: Update. * target.c: Update. * thread.c: Update. * tid-parse.c: Update. * tracefile-tfile.c: Update. * vax-bsd-nat.c: Update. * windows-nat.c: Update. * x86-linux-nat.c: Update. * x86-nat.c: Update. gdb/gdbserver/ChangeLog 2018-07-03 Tom Tromey <tom@tromey.com> * linux-low.c: Update. * linux-mips-low.c: Update. * lynx-low.c: Update. * mem-break.c: Update. * nto-low.c: Update. * remote-utils.c: Update. * server.c: Update. * spu-low.c: Update. * target.c: Update. * tracepoint.c: Update.
2018-06-11 20:05:27 +02:00
waitpid (ptid.pid (), &waitval, WNOHANG);
if (nto_inferior.exit_signo)
{
/* Abnormal death. */
ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
ourstatus->value.sig = nto_inferior.exit_signo;
}
else
{
/* Normal death. */
ourstatus->kind = TARGET_WAITKIND_EXITED;
ourstatus->value.integer = WEXITSTATUS (waitval);
}
nto_inferior.exit_signo = 0;
break;
}
case _DEBUG_WHY_REQUESTED:
TRACE ("REQUESTED\n");
/* We are assuming a requested stop is due to a SIGINT. */
ourstatus->kind = TARGET_WAITKIND_STOPPED;
gdb/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. gdb/gdbserver/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. include/gdb/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 * gdb/signals.def: Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/arm/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/avr/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/common/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/cr16/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/d10v/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/erc32/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/m32c/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/ppc/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/rl78/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout. sim/rx/ 2012-05-24 Pedro Alves <palves@redhat.com> PR gdb/7205 Replace TARGET_SIGNAL_ with GDB_SIGNAL_ throughout.
2012-05-24 18:51:47 +02:00
ourstatus->value.sig = GDB_SIGNAL_INT;
nto_inferior.exit_signo = 0;
break;
}
}
return ptid_t (status.pid, status.tid, 0);
}
/* Fetch inferior's registers for currently selected thread (CURRENT_INFERIOR).
If REGNO is -1, fetch all registers, or REGNO register only otherwise. */
gdbserver: turn target ops 'fetch_registers' and 'store_registers' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's fetch_registers and store_registers ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. (fetch_inferior_registers): Update the macro. (store_inferior_registers): Update the macro. Update the derived classes and callers below. * linux-low.cc (linux_target_ops): Update. (linux_fetch_registers): Turn into ... (linux_process_target::fetch_registers): ... this. (linux_store_registers): Turn into ... (linux_process_target::store_registers): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. (lynx_fetch_registers): Turn into ... (lynx_process_target::fetch_registers): ... this. (lynx_store_registers): Turn into ... (lynx_process_target::store_registers): ... this. * lynx-low.h (class lynx_process_target): Update. * nto-low.cc (nto_target_ops): Update. (nto_fetch_registers): Turn into ... (nto_process_target::fetch_registers): ... this. (nto_store_registers): Turn into ... (nto_process_target::store_registers): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_fetch_inferior_registers): Turn into ... (win32_process_target::fetch_registers): ... this. (win32_store_inferior_registers): Turn into ... (win32_process_target::store_registers): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:53 +01:00
void
nto_process_target::fetch_registers (regcache *regcache, int regno)
{
int regsize;
procfs_greg greg;
TRACE ("%s (regno=%d)\n", __func__, regno);
if (regno >= the_low_target.num_regs)
return;
Rename current_inferior as current_thread in gdbserver GDB has a function named "current_inferior" and gdbserver has a global variable named "current_inferior", but the two are not equivalent; indeed, gdbserver does not have any real equivalent of what GDB calls an inferior. What gdbserver's "current_inferior" is actually pointing to is a structure describing the current thread. This commit renames current_inferior as current_thread in gdbserver to clarify this. It also renames the function "set_desired_inferior" to "set_desired_thread" and renames various local variables from foo_inferior to foo_thread. gdb/gdbserver/ChangeLog: * inferiors.h (current_inferior): Renamed as... (current_thread): New variable. All uses updated. * linux-low.c (get_pc): Renamed saved_inferior as saved_thread. (maybe_move_out_of_jump_pad): Likewise. (cancel_breakpoint): Likewise. (linux_low_filter_event): Likewise. (wait_for_sigstop): Likewise. (linux_resume_one_lwp): Likewise. (need_step_over_p): Likewise. (start_step_over): Likewise. (linux_stabilize_threads): Renamed save_inferior as saved_thread. * linux-x86-low.c (x86_linux_update_xmltarget): Likewise. * proc-service.c (ps_lgetregs): Renamed reg_inferior as reg_thread and save_inferior as saved_thread. * regcache.c (get_thread_regcache): Renamed saved_inferior as saved_thread. (regcache_invalidate_thread): Likewise. * remote-utils.c (prepare_resume_reply): Likewise. * thread-db.c (thread_db_get_tls_address): Likewise. (disable_thread_event_reporting): Likewise. (remove_thread_event_breakpoints): Likewise. * tracepoint.c (gdb_agent_about_to_close): Renamed save_inferior as saved_thread. * target.h (set_desired_inferior): Renamed as... (set_desired_thread): New declaration. All uses updated. * server.c (myresume): Updated comment to reference thread instead of inferior. (handle_serial_event): Likewise. (handle_target_event): Likewise.
2014-09-10 11:37:11 +02:00
if (current_thread == NULL)
{
Rename current_inferior as current_thread in gdbserver GDB has a function named "current_inferior" and gdbserver has a global variable named "current_inferior", but the two are not equivalent; indeed, gdbserver does not have any real equivalent of what GDB calls an inferior. What gdbserver's "current_inferior" is actually pointing to is a structure describing the current thread. This commit renames current_inferior as current_thread in gdbserver to clarify this. It also renames the function "set_desired_inferior" to "set_desired_thread" and renames various local variables from foo_inferior to foo_thread. gdb/gdbserver/ChangeLog: * inferiors.h (current_inferior): Renamed as... (current_thread): New variable. All uses updated. * linux-low.c (get_pc): Renamed saved_inferior as saved_thread. (maybe_move_out_of_jump_pad): Likewise. (cancel_breakpoint): Likewise. (linux_low_filter_event): Likewise. (wait_for_sigstop): Likewise. (linux_resume_one_lwp): Likewise. (need_step_over_p): Likewise. (start_step_over): Likewise. (linux_stabilize_threads): Renamed save_inferior as saved_thread. * linux-x86-low.c (x86_linux_update_xmltarget): Likewise. * proc-service.c (ps_lgetregs): Renamed reg_inferior as reg_thread and save_inferior as saved_thread. * regcache.c (get_thread_regcache): Renamed saved_inferior as saved_thread. (regcache_invalidate_thread): Likewise. * remote-utils.c (prepare_resume_reply): Likewise. * thread-db.c (thread_db_get_tls_address): Likewise. (disable_thread_event_reporting): Likewise. (remove_thread_event_breakpoints): Likewise. * tracepoint.c (gdb_agent_about_to_close): Renamed save_inferior as saved_thread. * target.h (set_desired_inferior): Renamed as... (set_desired_thread): New declaration. All uses updated. * server.c (myresume): Updated comment to reference thread instead of inferior. (handle_serial_event): Likewise. (handle_target_event): Likewise.
2014-09-10 11:37:11 +02:00
TRACE ("current_thread is NULL\n");
return;
}
ptid_t ptid = ptid_of (current_thread);
if (!nto_set_thread (ptid))
return;
if (devctl (nto_inferior.ctl_fd, DCMD_PROC_GETGREG, &greg, sizeof (greg),
&regsize) == EOK)
{
if (regno == -1) /* All registers. */
{
for (regno = 0; regno != the_low_target.num_regs; ++regno)
{
const unsigned int registeroffset
= the_low_target.register_offset (regno);
supply_register (regcache, regno,
((char *)&greg) + registeroffset);
}
}
else
{
const unsigned int registeroffset
= the_low_target.register_offset (regno);
if (registeroffset == -1)
return;
* regcache.h (struct thread_info): Forward declare. (struct regcache): New. (new_register_cache): Adjust prototype. (get_thread_regcache): Declare. (free_register_cache): Adjust prototype. (registers_to_string, registers_from_string): Ditto. (supply_register, supply_register_by_name, collect_register) (collect_register_as_string, collect_register_by_name): Ditto. * regcache.c (struct inferior_regcache_data): Delete. (get_regcache): Rename to ... (get_thread_regcache): ... this. Adjust. Switch inferior before fetching registers. (regcache_invalidate_one): Adjust. (regcache_invalidate): Fix prototype. (new_register_cache): Return the new register cache. (free_register_cache): Change prototype. (realloc_register_cache): Adjust. (registers_to_string): Change prototype to take a regcache. Adjust. (registers_from_string): Ditto. (register_data): Ditto. (supply_register): Ditto. (supply_register_by_name): Ditto. (collect_register): Ditto. (collect_register_as_string): Ditto. (collect_register_by_name): Ditto. * server.c (process_serial_event): Adjust. * linux-low.h (regset_fill_func, regset_store_func): Change prototype. (get_pc, set_pc, collect_ptrace_register, supply_ptrace_register): Change prototype. * linux-low.c (get_stop_pc): Adjust. (check_removed_breakpoint): Adjust. (linux_wait_for_event): Adjust. (linux_resume_one_lwp): Adjust. (fetch_register): Add regcache parameter. Adjust. (usr_store_inferior_registers): Ditto. (regsets_fetch_inferior_registers): Ditto. (regsets_store_inferior_registers): Ditto. (linux_fetch_registers, linux_store_registers): Ditto. * i387-fp.c (i387_cache_to_fsave): Change prototype to take a regcache. Adjust. (i387_fsave_to_cache, i387_cache_to_fxsave, i387_fxsave_to_cache): Ditto. * i387-fp.h (i387_cache_to_fsave, i387_fsave_to_cache): Change prototype to take a regcache. (i387_cache_to_fxsave, i387_fxsave_to_cache): Ditto. * remote-utils.c (convert_ascii_to_int, outreg) (prepare_resume_reply): Change prototype to take a regcache. Adjust. * target.h (struct target_ops) <fetch_registers, store_registers>: Change prototype to take a regcache. (fetch_inferior_registers, store_inferior_registers): Change prototype to take a regcache. Adjust. * proc-service.c (ps_lgetregs): Adjust. * linux-x86-low.c (x86_fill_gregset, x86_store_gregset) (x86_fill_fpregset, x86_store_fpregset, x86_fill_fpxregset) (x86_store_fpxregset, x86_get_pc, x86_set_pc): Change prototype to take a regcache. Adjust. * linux-arm-low.c (arm_fill_gregset, arm_store_gregset) (arm_fill_wmmxregset, arm_store_wmmxregset, arm_fill_vfpregset) (arm_store_vfpregset, arm_get_pc, arm_set_pc): (arm_breakpoint_at): Change prototype to take a regcache. Adjust. * linux-cris-low.c (cris_get_pc, cris_set_pc) (cris_cannot_fetch_register): (cris_breakpoint_at): Change prototype to take a regcache. Adjust. * linux-crisv32-low.c (cris_get_pc, cris_set_pc, cris_reinsert_addr, cris_write_data_breakpoint): Change prototype to take a regcache. Adjust. (cris_breakpoint_at, cris_insert_point, cris_remove_point): Adjust. * linux-m32r-low.c (m32r_get_pc, m32r_set_pc): Change prototype to take a regcache. Adjust. * linux-m68k-low.c (m68k_fill_gregset, m68k_store_gregset) (m68k_fill_fpregset, m68k_store_fpregset, m68k_get_pc, (m68k_set_pc): Change prototype to take a regcache. Adjust. * linux-mips-low.c (mips_get_pc): (mips_set_pc): Change prototype to take a regcache. Adjust. (mips_reinsert_addr): Adjust. (mips_collect_register): Change prototype to take a regcache. Adjust. (mips_supply_register): (mips_collect_register_32bit, mips_supply_register_32bit) (mips_fill_gregset, mips_store_gregset, mips_fill_fpregset) (mips_store_fpregset): Ditto. * linux-ppc-low.c (ppc_supply_ptrace_register, ppc_supply_ptrace_register): Ditto. (parse_spufs_run): Adjust. (ppc_get_pc, ppc_set_pc, ppc_fill_gregset, ppc_fill_vsxregset) (ppc_store_vsxregset, ppc_fill_vrregset, ppc_store_vrregset) (ppc_fill_evrregset, ppc_store_evrregset): Change prototype to take a regcache. Adjust. * linux-s390-low.c (s390_collect_ptrace_register) (s390_supply_ptrace_register, s390_fill_gregset, s390_get_pc) (s390_set_pc): Change prototype to take a regcache. Adjust. (s390_arch_setup): Adjust. * linux-sh-low.c (sh_get_pc, sh_breakpoint_at) (sh_fill_gregset): Change prototype to take a regcache. Adjust. * linux-sparc-low.c (sparc_fill_gregset_to_stack) (sparc_fill_gregset, sparc_store_gregset_from_stack) (sparc_store_gregset, sparc_get_pc): Change prototype to take a regcache. Adjust. (sparc_breakpoint_at): Adjust. * linux-xtensa-low.c (xtensa_fill_gregset): (xtensa_store_gregset): (xtensa_fill_xtregset, xtensa_store_xtregset, xtensa_get_pc) (xtensa_set_pc): Change prototype to take a regcache. Adjust. * nto-low.c (nto_fetch_registers, nto_store_registers): Change prototype to take a regcache. Adjust. * win32-arm-low.c (arm_fetch_inferior_register) (arm_store_inferior_register): Change prototype to take a regcache. Adjust. * win32-i386-low.c (i386_fetch_inferior_register) (i386_store_inferior_register): Change prototype to take a regcache. Adjust. * win32-low.c (child_fetch_inferior_registers) (child_store_inferior_registers): Change prototype to take a regcache. Adjust. (win32_wait): Adjust. (win32_fetch_inferior_registers): Change prototype to take a regcache. Adjust. (win32_store_inferior_registers): Adjust. * win32-low.h (struct win32_target_ops) <fetch_inferior_register, store_inferior_register>: Change prototype to take a regcache.
2010-01-20 23:55:38 +01:00
supply_register (regcache, regno, ((char *)&greg) + registeroffset);
}
}
else
TRACE ("ERROR reading registers from inferior.\n");
}
/* Store registers for currently selected thread (CURRENT_INFERIOR).
We always store all registers, regardless of REGNO. */
gdbserver: turn target ops 'fetch_registers' and 'store_registers' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's fetch_registers and store_registers ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. (fetch_inferior_registers): Update the macro. (store_inferior_registers): Update the macro. Update the derived classes and callers below. * linux-low.cc (linux_target_ops): Update. (linux_fetch_registers): Turn into ... (linux_process_target::fetch_registers): ... this. (linux_store_registers): Turn into ... (linux_process_target::store_registers): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. (lynx_fetch_registers): Turn into ... (lynx_process_target::fetch_registers): ... this. (lynx_store_registers): Turn into ... (lynx_process_target::store_registers): ... this. * lynx-low.h (class lynx_process_target): Update. * nto-low.cc (nto_target_ops): Update. (nto_fetch_registers): Turn into ... (nto_process_target::fetch_registers): ... this. (nto_store_registers): Turn into ... (nto_process_target::store_registers): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_fetch_inferior_registers): Turn into ... (win32_process_target::fetch_registers): ... this. (win32_store_inferior_registers): Turn into ... (win32_process_target::store_registers): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:53 +01:00
void
nto_process_target::store_registers (regcache *regcache, int regno)
{
procfs_greg greg;
int err;
TRACE ("%s (regno:%d)\n", __func__, regno);
Rename current_inferior as current_thread in gdbserver GDB has a function named "current_inferior" and gdbserver has a global variable named "current_inferior", but the two are not equivalent; indeed, gdbserver does not have any real equivalent of what GDB calls an inferior. What gdbserver's "current_inferior" is actually pointing to is a structure describing the current thread. This commit renames current_inferior as current_thread in gdbserver to clarify this. It also renames the function "set_desired_inferior" to "set_desired_thread" and renames various local variables from foo_inferior to foo_thread. gdb/gdbserver/ChangeLog: * inferiors.h (current_inferior): Renamed as... (current_thread): New variable. All uses updated. * linux-low.c (get_pc): Renamed saved_inferior as saved_thread. (maybe_move_out_of_jump_pad): Likewise. (cancel_breakpoint): Likewise. (linux_low_filter_event): Likewise. (wait_for_sigstop): Likewise. (linux_resume_one_lwp): Likewise. (need_step_over_p): Likewise. (start_step_over): Likewise. (linux_stabilize_threads): Renamed save_inferior as saved_thread. * linux-x86-low.c (x86_linux_update_xmltarget): Likewise. * proc-service.c (ps_lgetregs): Renamed reg_inferior as reg_thread and save_inferior as saved_thread. * regcache.c (get_thread_regcache): Renamed saved_inferior as saved_thread. (regcache_invalidate_thread): Likewise. * remote-utils.c (prepare_resume_reply): Likewise. * thread-db.c (thread_db_get_tls_address): Likewise. (disable_thread_event_reporting): Likewise. (remove_thread_event_breakpoints): Likewise. * tracepoint.c (gdb_agent_about_to_close): Renamed save_inferior as saved_thread. * target.h (set_desired_inferior): Renamed as... (set_desired_thread): New declaration. All uses updated. * server.c (myresume): Updated comment to reference thread instead of inferior. (handle_serial_event): Likewise. (handle_target_event): Likewise.
2014-09-10 11:37:11 +02:00
if (current_thread == NULL)
{
Rename current_inferior as current_thread in gdbserver GDB has a function named "current_inferior" and gdbserver has a global variable named "current_inferior", but the two are not equivalent; indeed, gdbserver does not have any real equivalent of what GDB calls an inferior. What gdbserver's "current_inferior" is actually pointing to is a structure describing the current thread. This commit renames current_inferior as current_thread in gdbserver to clarify this. It also renames the function "set_desired_inferior" to "set_desired_thread" and renames various local variables from foo_inferior to foo_thread. gdb/gdbserver/ChangeLog: * inferiors.h (current_inferior): Renamed as... (current_thread): New variable. All uses updated. * linux-low.c (get_pc): Renamed saved_inferior as saved_thread. (maybe_move_out_of_jump_pad): Likewise. (cancel_breakpoint): Likewise. (linux_low_filter_event): Likewise. (wait_for_sigstop): Likewise. (linux_resume_one_lwp): Likewise. (need_step_over_p): Likewise. (start_step_over): Likewise. (linux_stabilize_threads): Renamed save_inferior as saved_thread. * linux-x86-low.c (x86_linux_update_xmltarget): Likewise. * proc-service.c (ps_lgetregs): Renamed reg_inferior as reg_thread and save_inferior as saved_thread. * regcache.c (get_thread_regcache): Renamed saved_inferior as saved_thread. (regcache_invalidate_thread): Likewise. * remote-utils.c (prepare_resume_reply): Likewise. * thread-db.c (thread_db_get_tls_address): Likewise. (disable_thread_event_reporting): Likewise. (remove_thread_event_breakpoints): Likewise. * tracepoint.c (gdb_agent_about_to_close): Renamed save_inferior as saved_thread. * target.h (set_desired_inferior): Renamed as... (set_desired_thread): New declaration. All uses updated. * server.c (myresume): Updated comment to reference thread instead of inferior. (handle_serial_event): Likewise. (handle_target_event): Likewise.
2014-09-10 11:37:11 +02:00
TRACE ("current_thread is NULL\n");
return;
}
ptid_t ptid = ptid_of (current_thread);
if (!nto_set_thread (ptid))
return;
memset (&greg, 0, sizeof (greg));
for (regno = 0; regno != the_low_target.num_regs; ++regno)
{
const unsigned int regoffset
= the_low_target.register_offset (regno);
* regcache.h (struct thread_info): Forward declare. (struct regcache): New. (new_register_cache): Adjust prototype. (get_thread_regcache): Declare. (free_register_cache): Adjust prototype. (registers_to_string, registers_from_string): Ditto. (supply_register, supply_register_by_name, collect_register) (collect_register_as_string, collect_register_by_name): Ditto. * regcache.c (struct inferior_regcache_data): Delete. (get_regcache): Rename to ... (get_thread_regcache): ... this. Adjust. Switch inferior before fetching registers. (regcache_invalidate_one): Adjust. (regcache_invalidate): Fix prototype. (new_register_cache): Return the new register cache. (free_register_cache): Change prototype. (realloc_register_cache): Adjust. (registers_to_string): Change prototype to take a regcache. Adjust. (registers_from_string): Ditto. (register_data): Ditto. (supply_register): Ditto. (supply_register_by_name): Ditto. (collect_register): Ditto. (collect_register_as_string): Ditto. (collect_register_by_name): Ditto. * server.c (process_serial_event): Adjust. * linux-low.h (regset_fill_func, regset_store_func): Change prototype. (get_pc, set_pc, collect_ptrace_register, supply_ptrace_register): Change prototype. * linux-low.c (get_stop_pc): Adjust. (check_removed_breakpoint): Adjust. (linux_wait_for_event): Adjust. (linux_resume_one_lwp): Adjust. (fetch_register): Add regcache parameter. Adjust. (usr_store_inferior_registers): Ditto. (regsets_fetch_inferior_registers): Ditto. (regsets_store_inferior_registers): Ditto. (linux_fetch_registers, linux_store_registers): Ditto. * i387-fp.c (i387_cache_to_fsave): Change prototype to take a regcache. Adjust. (i387_fsave_to_cache, i387_cache_to_fxsave, i387_fxsave_to_cache): Ditto. * i387-fp.h (i387_cache_to_fsave, i387_fsave_to_cache): Change prototype to take a regcache. (i387_cache_to_fxsave, i387_fxsave_to_cache): Ditto. * remote-utils.c (convert_ascii_to_int, outreg) (prepare_resume_reply): Change prototype to take a regcache. Adjust. * target.h (struct target_ops) <fetch_registers, store_registers>: Change prototype to take a regcache. (fetch_inferior_registers, store_inferior_registers): Change prototype to take a regcache. Adjust. * proc-service.c (ps_lgetregs): Adjust. * linux-x86-low.c (x86_fill_gregset, x86_store_gregset) (x86_fill_fpregset, x86_store_fpregset, x86_fill_fpxregset) (x86_store_fpxregset, x86_get_pc, x86_set_pc): Change prototype to take a regcache. Adjust. * linux-arm-low.c (arm_fill_gregset, arm_store_gregset) (arm_fill_wmmxregset, arm_store_wmmxregset, arm_fill_vfpregset) (arm_store_vfpregset, arm_get_pc, arm_set_pc): (arm_breakpoint_at): Change prototype to take a regcache. Adjust. * linux-cris-low.c (cris_get_pc, cris_set_pc) (cris_cannot_fetch_register): (cris_breakpoint_at): Change prototype to take a regcache. Adjust. * linux-crisv32-low.c (cris_get_pc, cris_set_pc, cris_reinsert_addr, cris_write_data_breakpoint): Change prototype to take a regcache. Adjust. (cris_breakpoint_at, cris_insert_point, cris_remove_point): Adjust. * linux-m32r-low.c (m32r_get_pc, m32r_set_pc): Change prototype to take a regcache. Adjust. * linux-m68k-low.c (m68k_fill_gregset, m68k_store_gregset) (m68k_fill_fpregset, m68k_store_fpregset, m68k_get_pc, (m68k_set_pc): Change prototype to take a regcache. Adjust. * linux-mips-low.c (mips_get_pc): (mips_set_pc): Change prototype to take a regcache. Adjust. (mips_reinsert_addr): Adjust. (mips_collect_register): Change prototype to take a regcache. Adjust. (mips_supply_register): (mips_collect_register_32bit, mips_supply_register_32bit) (mips_fill_gregset, mips_store_gregset, mips_fill_fpregset) (mips_store_fpregset): Ditto. * linux-ppc-low.c (ppc_supply_ptrace_register, ppc_supply_ptrace_register): Ditto. (parse_spufs_run): Adjust. (ppc_get_pc, ppc_set_pc, ppc_fill_gregset, ppc_fill_vsxregset) (ppc_store_vsxregset, ppc_fill_vrregset, ppc_store_vrregset) (ppc_fill_evrregset, ppc_store_evrregset): Change prototype to take a regcache. Adjust. * linux-s390-low.c (s390_collect_ptrace_register) (s390_supply_ptrace_register, s390_fill_gregset, s390_get_pc) (s390_set_pc): Change prototype to take a regcache. Adjust. (s390_arch_setup): Adjust. * linux-sh-low.c (sh_get_pc, sh_breakpoint_at) (sh_fill_gregset): Change prototype to take a regcache. Adjust. * linux-sparc-low.c (sparc_fill_gregset_to_stack) (sparc_fill_gregset, sparc_store_gregset_from_stack) (sparc_store_gregset, sparc_get_pc): Change prototype to take a regcache. Adjust. (sparc_breakpoint_at): Adjust. * linux-xtensa-low.c (xtensa_fill_gregset): (xtensa_store_gregset): (xtensa_fill_xtregset, xtensa_store_xtregset, xtensa_get_pc) (xtensa_set_pc): Change prototype to take a regcache. Adjust. * nto-low.c (nto_fetch_registers, nto_store_registers): Change prototype to take a regcache. Adjust. * win32-arm-low.c (arm_fetch_inferior_register) (arm_store_inferior_register): Change prototype to take a regcache. Adjust. * win32-i386-low.c (i386_fetch_inferior_register) (i386_store_inferior_register): Change prototype to take a regcache. Adjust. * win32-low.c (child_fetch_inferior_registers) (child_store_inferior_registers): Change prototype to take a regcache. Adjust. (win32_wait): Adjust. (win32_fetch_inferior_registers): Change prototype to take a regcache. Adjust. (win32_store_inferior_registers): Adjust. * win32-low.h (struct win32_target_ops) <fetch_inferior_register, store_inferior_register>: Change prototype to take a regcache.
2010-01-20 23:55:38 +01:00
collect_register (regcache, regno, ((char *)&greg) + regoffset);
}
err = devctl (nto_inferior.ctl_fd, DCMD_PROC_SETGREG, &greg, sizeof (greg),
0);
if (err != EOK)
TRACE ("Error: setting registers.\n");
}
/* Read LEN bytes from inferior's memory address MEMADDR into
gdbserver's MYADDR buffer.
Return 0 on success -1 otherwise. */
gdbserver: turn target ops 'read_memory' and 'write_memory' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's read_memory and write_memory ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. Update the derived classes and callers below. * linux-aarch32-low.cc (arm_breakpoint_at): Update. * linux-aarch64-low.cc (aarch64_breakpoint_at): Update. * linux-arm-low.cc (arm_sigreturn_next_pc): Update. (arm_get_syscall_trapinfo): Update. * linux-cris-low.cc (cris_breakpoint_at): Update. * linux-crisv32-low.cc (cris_breakpoint_at): Update. * linux-m32r-low.cc (m32r_breakpoint_at): Update. * linux-mips-low.cc (mips_breakpoint_at): Update. * linux-nios2-low.cc (nios2_breakpoint_at): Update. * linux-ppc-low.cc (ppc_breakpoint_at): Update. * linux-sh-low.cc (sh_breakpoint_at): Update. * linux-sparc-low.cc (sparc_fill_gregset_to_stack): Update. (sparc_store_gregset_from_stack): Update. (sparc_breakpoint_at): Update. * linux-tic6x-low.cc (tic6x_breakpoint_at): Update. * linux-tile-low.cc (tile_breakpoint_at): Update. * linux-x86-low.cc (x86_breakpoint_at): Update. * linux-xtensa-low.cc (xtensa_breakpoint_at): Update. * mem-brea.cc (insert_memory_breakpoint): Update. (validate_inserted_breakpoint): Update. * target.cc (read_inferior_memory): Update. (target_write_memory): Update. * linux-low.cc (linux_target_ops): Update. (linux_read_memory): Make a wrapper around the read_memory target op call. (linux_process_target::read_memory): Rename from linux_read_memory. (linux_write_memory): Turn into ... (linux_process_target::write_memory): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. (lynx_read_memory): Turn into ... (lynx_process_target::read_memory): ... this. (lynx_write_memory): Turn into ... (lynx_process_target::write_memory): ... this. * lynx-low.h (class lynx_process_target): Update. * nto-low.cc (nto_target_ops): Update. (nto_read_memory): Turn into ... (nto_process_target::read_memory): ... this. (nto_write_memory): Turn into ... (nto_process_target::write_memory): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_read_inferior_memory): Turn into ... (win32_process_target::read_memory): ... this. (win32_write_inferior_memory): Turn into ... (win32_process_target::write_memory): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:53 +01:00
int
nto_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
int len)
{
TRACE ("%s memaddr:0x%08lx, len:%d\n", __func__, memaddr, len);
if (nto_xfer_memory (memaddr, myaddr, len, 0) != len)
{
TRACE ("Failed to read memory\n");
return -1;
}
return 0;
}
/* Write LEN bytes from gdbserver's buffer MYADDR into inferior's
memory at address MEMADDR.
Return 0 on success -1 otherwise. */
gdbserver: turn target ops 'read_memory' and 'write_memory' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's read_memory and write_memory ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. Update the derived classes and callers below. * linux-aarch32-low.cc (arm_breakpoint_at): Update. * linux-aarch64-low.cc (aarch64_breakpoint_at): Update. * linux-arm-low.cc (arm_sigreturn_next_pc): Update. (arm_get_syscall_trapinfo): Update. * linux-cris-low.cc (cris_breakpoint_at): Update. * linux-crisv32-low.cc (cris_breakpoint_at): Update. * linux-m32r-low.cc (m32r_breakpoint_at): Update. * linux-mips-low.cc (mips_breakpoint_at): Update. * linux-nios2-low.cc (nios2_breakpoint_at): Update. * linux-ppc-low.cc (ppc_breakpoint_at): Update. * linux-sh-low.cc (sh_breakpoint_at): Update. * linux-sparc-low.cc (sparc_fill_gregset_to_stack): Update. (sparc_store_gregset_from_stack): Update. (sparc_breakpoint_at): Update. * linux-tic6x-low.cc (tic6x_breakpoint_at): Update. * linux-tile-low.cc (tile_breakpoint_at): Update. * linux-x86-low.cc (x86_breakpoint_at): Update. * linux-xtensa-low.cc (xtensa_breakpoint_at): Update. * mem-brea.cc (insert_memory_breakpoint): Update. (validate_inserted_breakpoint): Update. * target.cc (read_inferior_memory): Update. (target_write_memory): Update. * linux-low.cc (linux_target_ops): Update. (linux_read_memory): Make a wrapper around the read_memory target op call. (linux_process_target::read_memory): Rename from linux_read_memory. (linux_write_memory): Turn into ... (linux_process_target::write_memory): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. (lynx_read_memory): Turn into ... (lynx_process_target::read_memory): ... this. (lynx_write_memory): Turn into ... (lynx_process_target::write_memory): ... this. * lynx-low.h (class lynx_process_target): Update. * nto-low.cc (nto_target_ops): Update. (nto_read_memory): Turn into ... (nto_process_target::read_memory): ... this. (nto_write_memory): Turn into ... (nto_process_target::write_memory): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_read_inferior_memory): Turn into ... (win32_process_target::read_memory): ... this. (win32_write_inferior_memory): Turn into ... (win32_process_target::write_memory): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:53 +01:00
int
nto_process_target::write_memory (CORE_ADDR memaddr,
const unsigned char *myaddr, int len)
{
int len_written;
TRACE ("%s memaddr: 0x%08llx len: %d\n", __func__, memaddr, len);
if ((len_written = nto_xfer_memory (memaddr, (unsigned char *)myaddr, len,
1))
!= len)
{
TRACE ("Wanted to write: %d but written: %d\n", len, len_written);
return -1;
}
return 0;
}
/* Stop inferior. We always stop all threads. */
void
nto_process_target::request_interrupt ()
{
TRACE ("%s\n", __func__);
nto_set_thread (ptid_t (nto_inferior.pid, 1, 0));
if (EOK != devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, NULL, 0, 0))
TRACE ("Error stopping inferior.\n");
}
bool
nto_process_target::supports_read_auxv ()
{
return true;
}
/* Read auxiliary vector from inferior's memory into gdbserver's buffer
MYADDR. We always read whole auxv.
Return number of bytes stored in MYADDR buffer, 0 if OFFSET > 0
or -1 on error. */
int
nto_process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
unsigned int len)
{
int err;
CORE_ADDR initial_stack;
procfs_info procinfo;
TRACE ("%s\n", __func__);
if (offset > 0)
return 0;
err = devctl (nto_inferior.ctl_fd, DCMD_PROC_INFO, &procinfo,
sizeof procinfo, 0);
if (err != EOK)
return -1;
initial_stack = procinfo.initial_stack;
return nto_read_auxv_from_initial_stack (initial_stack, myaddr, len);
}
bool
nto_process_target::supports_z_point_type (char z_type)
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
{
switch (z_type)
{
case Z_PACKET_SW_BP:
case Z_PACKET_HW_BP:
case Z_PACKET_WRITE_WP:
case Z_PACKET_READ_WP:
case Z_PACKET_ACCESS_WP:
return true;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
default:
return false;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
}
}
/* Insert {break/watch}point at address ADDR. SIZE is not used. */
int
nto_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
int size, raw_breakpoint *bp)
{
int wtype = _DEBUG_BREAK_HW; /* Always request HW. */
TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, size);
switch (type)
{
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_sw:
wtype = _DEBUG_BREAK_EXEC;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_hw:
wtype |= _DEBUG_BREAK_EXEC;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_write_wp:
wtype |= _DEBUG_BREAK_RW;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_read_wp:
wtype |= _DEBUG_BREAK_RD;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_access_wp:
wtype |= _DEBUG_BREAK_RW;
break;
default:
return 1; /* Not supported. */
}
return nto_breakpoint (addr, wtype, 0);
}
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
/* Remove {break/watch}point at address ADDR. SIZE is not used. */
int
nto_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
int size, raw_breakpoint *bp)
{
int wtype = _DEBUG_BREAK_HW; /* Always request HW. */
TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, size);
switch (type)
{
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_sw:
wtype = _DEBUG_BREAK_EXEC;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_hw:
wtype |= _DEBUG_BREAK_EXEC;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_write_wp:
wtype |= _DEBUG_BREAK_RW;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_read_wp:
wtype |= _DEBUG_BREAK_RD;
break;
[GDBserver] Make Zx/zx packet handling idempotent. This patch fixes hardware breakpoint regressions exposed by my fix for "PR breakpoints/7143 - Watchpoint does not trigger when first set", at https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html The testsuite caught them on Linux/x86_64, at least. gdb.sum: gdb.sum: FAIL: gdb.base/hbreak2.exp: next over recursive call FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1) FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test gdb.log: (gdb) next Program received signal SIGTRAP, Trace/breakpoint trap. factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113 113 if (value > 1) { /* set breakpoint 7 here */ (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call Actually, that patch just exposed a latent issue to "breakpoints always-inserted off" mode, not really caused it. After that patch, GDB no longer removes breakpoints at each internal event, thus making some scenarios behave like breakpoint always-inserted on. The bug is easy to trigger with always-inserted on. The issue is that since the target-side breakpoint conditions support, if the stub/server supports evaluating breakpoint conditions on the target side, then GDB is sending duplicate Zx packets to the target without removing them before, and GDBserver is not really expecting that for Z packets other than Z0/z0. E.g., with "set breakpoint always-inserted on" and "set debug remote 1": (gdb) b main Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) b main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z0,410943,1#48...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $Z0,410943,1#48...Packet received: OK Sending packet: $z0,410943,1#68...Packet received: OK And for Z1, similarly: (gdb) hbreak main Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Packet Z1 (hardware-breakpoint) is supported (gdb) hbreak main Note: breakpoint 4 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) hbreak main Note: breakpoints 4 and 5 also set at pc 0x410943. Sending packet: $m410943,1#ff...Packet received: 48 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028. Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ (gdb) del Delete all breakpoints? (y or n) y Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $Z1,410943,1#49...Packet received: OK ^^^^^^^^^^^^ Sending packet: $z1,410943,1#69...Packet received: OK ^^^^^^^^^^^^ So GDB sent a bunch of Z1 packets, and then when finally removing the breakpoint, only one z1 packet was sent. On the GDBserver side (with monitor set debug-hw-points 1), in the Z1 case, we see: $ ./gdbserver :9999 ./gdbserver Process ./gdbserver created; pid = 8629 Listening on port 9999 Remote debugging from host 127.0.0.1 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=1 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=2 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=3 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 insert_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=5 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 remove_watchpoint (addr=410943, len=1, type=instruction-execute): CONTROL (DR7): 00000101 STATUS (DR6): 00000000 DR0: addr=0x410943, ref.count=4 DR1: addr=0x0, ref.count=0 DR2: addr=0x0, ref.count=0 DR3: addr=0x0, ref.count=0 That's one insert_watchpoint call for each Z1 packet, and then one remove_watchpoint call for the z1 packet. Notice how ref.count increased for each insert_watchpoint call, and then in the end, after GDB told GDBserver to forget about the hardware breakpoint, GDBserver ends with the the first debug register still with ref.count=4! IOW, the hardware breakpoint is left armed on the target, while on the GDB end it's gone. If the program happens to execute 0x410943 afterwards, then the CPU traps, GDBserver reports the trap to GDB, and GDB not having a breakpoint set at that address anymore, reports to the user a spurious SIGTRAP. This is exactly what is happening in the hbreak2.exp test, though in that case, it's a shared library event that triggers a breakpoint_re_set, when breakpoints are still inserted (because nowadays GDB doesn't remove breakpoints while handling internal events), and that recreates breakpoint locations, which likewise forces breakpoint reinsertion and Zx packet resends... That is a lot of bogus Zx duplication that should possibly be addressed on the GDB side. GDB resends Zx packets because the way to change the target-side condition, is to resend the breakpoint to the server with the new condition. (That's an option in the packet: e.g., "Z1,410943,1;X3,220027" for "hbreak main if 0". The packets in the examples above are shorter because the breakpoints don't have conditions attached). GDB doesn't remove the breakpoint first before reinserting it because that'd be bad for non-stop, as it'd open a window where the inferior could miss the breakpoint. The conditions actually haven't changed between the resends, but GDB isn't smart enough to realize that. (TBC, if the target doesn't support target-side conditions, then GDB doesn't trigger these resends (init_bp_location calls mark_breakpoint_location_modified, and that does nothing if condition evaluation is on the host side. The resends are caused by the 'loc->condition_changed = condition_modified.' line.) But, even if GDB was made smarter, GDBserver should really still handle the resends anyway. So target-side conditions also aren't really to blame. The documentation of the Z/z packets says: "To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way." As such, we may want to fix GDB, but we should definitely fix GDBserver. The fix is a prerequisite for target-side conditions on hardware breakpoints anyway (and while at it, on watchpoints too). GDBserver indeed already treats duplicate Z0 packets in an idempotent way. mem-break.c has the concept of high-level and low-level breakpoints, somewhat similar to GDB's split of breakpoints vs breakpoint locations, and keeps track of multiple breakpoints referencing the same address/location, for the case of an internal GDBserver breakpoint or a tracepoint being set at the same address as a GDB breakpoint. But, it only allows GDB to ever contribute one reference to a software breakpoint location. IOW, if gdbserver sees a Z0 packet for the same address where it already had a GDB breakpoint set, then GDBserver won't create another high-level GDB breakpoint. However, mem-break.c only tracks GDB Z0 breakpoints. The same logic should apply to all kinds of Zx packets. Currently, gdbserver passes down each duplicate Zx (other than Z0) request directly to the target->insert_point routine. The x86 watchpoint support itself refcounts watchpoint / hw breakpoint requests, to handle overlapping watchpoints, and save debug registers. But that code doesn't (and really shouldn't) handle the duplicate requests, assuming that for each insert there will be a corresponding remove. So the fix is to generalize mem-break.c to track all kinds of Zx breakpoints, and filter out duplicates. As mentioned, this ends up adding support for target-side conditions on hardware breakpoints and watchpoints too (though GDB itself doesn't support the latter yet). Probably the least obvious change in the patch is that it kind of turns the breakpoint insert/remove APIs inside out. Before, the target methods were only called for GDB breakpoints. The internal breakpoint set/delete methods inserted memory breakpoints directly bypassing the insert/remove target methods. That's not good when the target should use a debug API to set software breakpoints, instead of relying on GDBserver patching memory with breakpoint instructions, as is the case of NTO. Now removal/insertion of all kinds of breakpoints/watchpoints, either internal, or from GDB, always go through the target methods. The insert_point/remove_point methods no longer get passed a Z packet type, but an internal/raw breakpoint type. They're also passed a pointer to the raw breakpoint itself (note that's still opaque outside mem-break.c), so that insert_memory_breakpoint / remove_memory_breakpoint have access to the breakpoint's shadow buffer. I first tried passing down a new structure based on GDB's "struct bp_target_info" (actually with that name exactly), but then decided against it as unnecessary complication. As software/memory breakpoints work by poking at memory, when setting a GDB Z0 breakpoint (but not internal breakpoints, as those can assume the conditions are already right), we need to tell the target to prepare to access memory (which on Linux means stop threads). If that operation fails, we need to return error to GDB. Seeing an error, if this is the first breakpoint of that type that GDB tries to insert, GDB would then assume the breakpoint type is supported, but it may actually not be. So we need to check whether the type is supported at all before preparing to access memory. And to solve that, the patch adds a new target->supports_z_point_type method that is called before actually trying to insert the breakpoint. Other than that, hopefully the change is more or less obvious. New test added that exercises the hbreak2.exp regression in a more direct way, without relying on a breakpoint re-set happening before main is reached. Tested by building GDBserver for: aarch64-linux-gnu arm-linux-gnueabihf i686-pc-linux-gnu i686-w64-mingw32 m68k-linux-gnu mips-linux-gnu mips-uclinux nios2-linux-gnu powerpc-linux-gnu sh-linux-gnu tilegx-unknown-linux-gnu x86_64-redhat-linux x86_64-w64-mingw32 And also regression tested on x86_64 Fedora 20. gdb/gdbserver/ 2014-05-20 Pedro Alves <palves@redhat.com> * linux-aarch64-low.c (aarch64_insert_point) (aarch64_remove_point): No longer check whether the type is supported here. Adjust to new interface. (the_low_target): Install aarch64_supports_z_point_type as supports_z_point_type method. * linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function. (arm_linux_hw_point_initialize): Take an enum raw_bkpt_type instead of a Z packet char. Adjust. (arm_supports_z_point_type): New function. (arm_insert_point, arm_remove_point): Adjust to new interface. (the_low_target): Install arm_supports_z_point_type. * linux-crisv32-low.c (cris_supports_z_point_type): New function. (cris_insert_point, cris_remove_point): Adjust to new interface. Don't check whether the type is supported here. (the_low_target): Install cris_supports_z_point_type. * linux-low.c (linux_supports_z_point_type): New function. (linux_insert_point, linux_remove_point): Adjust to new interface. * linux-low.h (struct linux_target_ops) <insert_point, remove_point>: Take an enum raw_bkpt_type instead of a char. Add raw_breakpoint pointer parameter. <supports_z_point_type>: New method. * linux-mips-low.c (mips_supports_z_point_type): New function. (mips_insert_point, mips_remove_point): Adjust to new interface. Use mips_supports_z_point_type. (the_low_target): Install mips_supports_z_point_type. * linux-ppc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-s390-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-sparc-low.c (the_low_target): Install NULL as supports_z_point_type method. * linux-x86-low.c (x86_supports_z_point_type): New function. (x86_insert_point): Adjust to new insert_point interface. Use insert_memory_breakpoint. Adjust to new i386_low_insert_watchpoint interface. (x86_remove_point): Adjust to remove_point interface. Use remove_memory_breakpoint. Adjust to new i386_low_remove_watchpoint interface. (the_low_target): Install x86_supports_z_point_type. * lynx-low.c (lynx_target_ops): Install NULL as supports_z_point_type callback. * nto-low.c (nto_supports_z_point_type): New. (nto_insert_point, nto_remove_point): Adjust to new interface. (nto_target_ops): Install nto_supports_z_point_type. * mem-break.c: Adjust intro comment. (struct raw_breakpoint) <raw_type, size>: New fields. <inserted>: Update comment. <shlib_disabled>: Delete field. (enum bkpt_type) <gdb_breakpoint>: Delete value. <gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2, gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values. (raw_bkpt_type_to_target_hw_bp_type): New function. (find_enabled_raw_code_breakpoint_at): New function. (find_raw_breakpoint_at): New type and size parameters. Use them. (insert_memory_breakpoint): New function, based off set_raw_breakpoint_at. (remove_memory_breakpoint): New function. (set_raw_breakpoint_at): Reimplement. (set_breakpoint): New, based on set_breakpoint_at. (set_breakpoint_at): Reimplement. (delete_raw_breakpoint): Go through the_target->remove_point instead of assuming memory breakpoints. (find_gdb_breakpoint_at): Delete. (Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions. (find_gdb_breakpoint): New function. (set_gdb_breakpoint_at): Delete. (z_type_supported): New function. (set_gdb_breakpoint_1): New function, loosely based off set_gdb_breakpoint_at. (check_gdb_bp_preconditions, set_gdb_breakpoint): New functions. (delete_gdb_breakpoint_at): Delete. (delete_gdb_breakpoint_1): New function, loosely based off delete_gdb_breakpoint_at. (delete_gdb_breakpoint): New function. (clear_gdb_breakpoint_conditions): Rename to ... (clear_breakpoint_conditions): ... this. Don't handle a NULL breakpoint. (add_condition_to_breakpoint): Make static. (add_breakpoint_condition): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_condition_true_at_breakpoint): Rename to ... (gdb_condition_true_at_breakpoint_z_type): ... this, and add z_type parameter. (gdb_condition_true_at_breakpoint): Reimplement. (add_breakpoint_commands): Take a struct breakpoint pointer instead of an address. Adjust. (gdb_no_commands_at_breakpoint): Rename to ... (gdb_no_commands_at_breakpoint_z_type): ... this. Add z_type parameter. Return true if no breakpoint was found. Change debug output. (gdb_no_commands_at_breakpoint): Reimplement. (run_breakpoint_commands): Rename to ... (run_breakpoint_commands_z_type): ... this. Add z_type parameter, and change return type to boolean. (run_breakpoint_commands): New function. (gdb_breakpoint_here): Also check for Z1 breakpoints. (uninsert_raw_breakpoint): Don't try to reinsert a disabled breakpoint. Go through the_target->remove_point instead of assuming memory breakpoint. (uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert software and hardware breakpoints. (reinsert_raw_breakpoint): Go through the_target->insert_point instead of assuming memory breakpoint. (reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert software and hardware breakpoints. (check_breakpoints, breakpoint_here, breakpoint_inserted_here): Check both software and hardware breakpoints. (validate_inserted_breakpoint): Assert the breakpoint is a software breakpoint. Set the inserted flag to -1 instead of setting shlib_disabled. (delete_disabled_breakpoints): Adjust. (validate_breakpoints): Only validate software breakpoints. Adjust to inserted flag change. (check_mem_read, check_mem_write): Skip breakpoint types other than software breakpoints. Adjust to inserted flag change. * mem-break.h (enum raw_bkpt_type): New enum. (raw_breakpoint, struct process_info): Forward declare. (Z_packet_to_target_hw_bp_type): Delete declaration. (raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type) (set_gdb_breakpoint, delete_gdb_breakpoint) (clear_breakpoint_conditions): New declarations. (set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete. (breakpoint_inserted_here): Update comment. (add_breakpoint_condition, add_breakpoint_commands): Replace address parameter with a breakpoint pointer parameter. (gdb_breakpoint_here): Update comment. (delete_gdb_breakpoint_at): Delete. (insert_memory_breakpoint, remove_memory_breakpoint): Declare. * server.c (process_point_options): Take a struct breakpoint pointer instead of an address. Adjust. (process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and delete_gdb_breakpoint. * spu-low.c (spu_target_ops): Install NULL as supports_z_point_type method. * target.h: Include mem-break.h. (struct target_ops) <prepare_to_access_memory>: Update comment. <supports_z_point_type>: New field. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. * win32-arm-low.c (the_low_target): Install NULL as supports_z_point_type. * win32-i386-low.c (i386_supports_z_point_type): New function. (i386_insert_point, i386_remove_point): Adjust to new interface. (the_low_target): Install i386_supports_z_point_type. * win32-low.c (win32_supports_z_point_type): New function. (win32_insert_point, win32_remove_point): Adjust to new interface. (win32_target_ops): Install win32_supports_z_point_type. * win32-low.h (struct win32_target_ops): <supports_z_point_type>: New method. <insert_point, remove_point>: Take an enum raw_bkpt_type argument instead of a char. Also take a raw breakpoint pointer. gdb/testsuite/ 2014-05-20 Pedro Alves <palves@redhat.com> * gdb.base/break-idempotent.c: New file. * gdb.base/break-idempotent.exp: New file.
2014-05-20 19:24:28 +02:00
case raw_bkpt_type_access_wp:
wtype |= _DEBUG_BREAK_RW;
break;
default:
return 1; /* Not supported. */
}
return nto_breakpoint (addr, wtype, -1);
}
bool
nto_process_target::supports_hardware_single_step ()
{
return true;
}
/* Check if the reason of stop for current thread (CURRENT_INFERIOR) is
a watchpoint.
gdbserver: turn target ops 'stopped_by_watchpoint' and 'stopped_data_address' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's stopped_by_watchpoint and stopped_data_address ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. * target.cc (process_target::stopped_by_watchpoint): Define. (process_target::stopped_data_address): Define. Update the derived classes and callers below. * remote-utils.cc (prepare_resume_reply): Update. * linux-low.cc (linux_target_ops): Update. (linux_stopped_by_watchpoint): Turn into ... (linux_process_target::stopped_by_watchpoint): ... this. (linux_stopped_data_address): Turn into ... (linux_process_target::stopped_data_address): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. * nto-low.cc (nto_target_ops): Update. (nto_stopped_by_watchpoint): Turn into ... (nto_process_target::stopped_by_watchpoint): ... this. (nto_stopped_data_address): Turn into ... (nto_process_target::stopped_data_address): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_stopped_by_watchpoint): Turn into ... (win32_process_target::stopped_by_watchpoint): ... this. (win32_stopped_data_address): Turn into ... (win32_process_target::stopped_data_address): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:55 +01:00
Return true if stopped by watchpoint, false otherwise. */
gdbserver: turn target ops 'stopped_by_watchpoint' and 'stopped_data_address' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's stopped_by_watchpoint and stopped_data_address ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. * target.cc (process_target::stopped_by_watchpoint): Define. (process_target::stopped_data_address): Define. Update the derived classes and callers below. * remote-utils.cc (prepare_resume_reply): Update. * linux-low.cc (linux_target_ops): Update. (linux_stopped_by_watchpoint): Turn into ... (linux_process_target::stopped_by_watchpoint): ... this. (linux_stopped_data_address): Turn into ... (linux_process_target::stopped_data_address): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. * nto-low.cc (nto_target_ops): Update. (nto_stopped_by_watchpoint): Turn into ... (nto_process_target::stopped_by_watchpoint): ... this. (nto_stopped_data_address): Turn into ... (nto_process_target::stopped_data_address): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_stopped_by_watchpoint): Turn into ... (win32_process_target::stopped_by_watchpoint): ... this. (win32_stopped_data_address): Turn into ... (win32_process_target::stopped_data_address): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:55 +01:00
bool
nto_process_target::stopped_by_watchpoint ()
{
gdbserver: turn target ops 'stopped_by_watchpoint' and 'stopped_data_address' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's stopped_by_watchpoint and stopped_data_address ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. * target.cc (process_target::stopped_by_watchpoint): Define. (process_target::stopped_data_address): Define. Update the derived classes and callers below. * remote-utils.cc (prepare_resume_reply): Update. * linux-low.cc (linux_target_ops): Update. (linux_stopped_by_watchpoint): Turn into ... (linux_process_target::stopped_by_watchpoint): ... this. (linux_stopped_data_address): Turn into ... (linux_process_target::stopped_data_address): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. * nto-low.cc (nto_target_ops): Update. (nto_stopped_by_watchpoint): Turn into ... (nto_process_target::stopped_by_watchpoint): ... this. (nto_stopped_data_address): Turn into ... (nto_process_target::stopped_data_address): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_stopped_by_watchpoint): Turn into ... (win32_process_target::stopped_by_watchpoint): ... this. (win32_stopped_data_address): Turn into ... (win32_process_target::stopped_data_address): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:55 +01:00
bool ret = false;
TRACE ("%s\n", __func__);
Rename current_inferior as current_thread in gdbserver GDB has a function named "current_inferior" and gdbserver has a global variable named "current_inferior", but the two are not equivalent; indeed, gdbserver does not have any real equivalent of what GDB calls an inferior. What gdbserver's "current_inferior" is actually pointing to is a structure describing the current thread. This commit renames current_inferior as current_thread in gdbserver to clarify this. It also renames the function "set_desired_inferior" to "set_desired_thread" and renames various local variables from foo_inferior to foo_thread. gdb/gdbserver/ChangeLog: * inferiors.h (current_inferior): Renamed as... (current_thread): New variable. All uses updated. * linux-low.c (get_pc): Renamed saved_inferior as saved_thread. (maybe_move_out_of_jump_pad): Likewise. (cancel_breakpoint): Likewise. (linux_low_filter_event): Likewise. (wait_for_sigstop): Likewise. (linux_resume_one_lwp): Likewise. (need_step_over_p): Likewise. (start_step_over): Likewise. (linux_stabilize_threads): Renamed save_inferior as saved_thread. * linux-x86-low.c (x86_linux_update_xmltarget): Likewise. * proc-service.c (ps_lgetregs): Renamed reg_inferior as reg_thread and save_inferior as saved_thread. * regcache.c (get_thread_regcache): Renamed saved_inferior as saved_thread. (regcache_invalidate_thread): Likewise. * remote-utils.c (prepare_resume_reply): Likewise. * thread-db.c (thread_db_get_tls_address): Likewise. (disable_thread_event_reporting): Likewise. (remove_thread_event_breakpoints): Likewise. * tracepoint.c (gdb_agent_about_to_close): Renamed save_inferior as saved_thread. * target.h (set_desired_inferior): Renamed as... (set_desired_thread): New declaration. All uses updated. * server.c (myresume): Updated comment to reference thread instead of inferior. (handle_serial_event): Likewise. (handle_target_event): Likewise.
2014-09-10 11:37:11 +02:00
if (nto_inferior.ctl_fd != -1 && current_thread != NULL)
{
ptid_t ptid = ptid_of (current_thread);
if (nto_set_thread (ptid))
{
const int watchmask = _DEBUG_FLAG_TRACE_RD | _DEBUG_FLAG_TRACE_WR
| _DEBUG_FLAG_TRACE_MODIFY;
procfs_status status;
int err;
err = devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status,
sizeof (status), 0);
if (err == EOK && (status.flags & watchmask))
gdbserver: turn target ops 'stopped_by_watchpoint' and 'stopped_data_address' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's stopped_by_watchpoint and stopped_data_address ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. * target.cc (process_target::stopped_by_watchpoint): Define. (process_target::stopped_data_address): Define. Update the derived classes and callers below. * remote-utils.cc (prepare_resume_reply): Update. * linux-low.cc (linux_target_ops): Update. (linux_stopped_by_watchpoint): Turn into ... (linux_process_target::stopped_by_watchpoint): ... this. (linux_stopped_data_address): Turn into ... (linux_process_target::stopped_data_address): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. * nto-low.cc (nto_target_ops): Update. (nto_stopped_by_watchpoint): Turn into ... (nto_process_target::stopped_by_watchpoint): ... this. (nto_stopped_data_address): Turn into ... (nto_process_target::stopped_data_address): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_stopped_by_watchpoint): Turn into ... (win32_process_target::stopped_by_watchpoint): ... this. (win32_stopped_data_address): Turn into ... (win32_process_target::stopped_data_address): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:55 +01:00
ret = true;
}
}
TRACE ("%s: %s\n", __func__, ret ? "yes" : "no");
return ret;
}
/* Get instruction pointer for CURRENT_INFERIOR thread.
Return inferior's instruction pointer value, or 0 on error. */
gdbserver: turn target ops 'stopped_by_watchpoint' and 'stopped_data_address' into methods gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> Turn process_stratum_target's stopped_by_watchpoint and stopped_data_address ops into methods of process_target. * target.h (struct process_stratum_target): Remove the target ops. (class process_target): Add the target ops. * target.cc (process_target::stopped_by_watchpoint): Define. (process_target::stopped_data_address): Define. Update the derived classes and callers below. * remote-utils.cc (prepare_resume_reply): Update. * linux-low.cc (linux_target_ops): Update. (linux_stopped_by_watchpoint): Turn into ... (linux_process_target::stopped_by_watchpoint): ... this. (linux_stopped_data_address): Turn into ... (linux_process_target::stopped_data_address): ... this. * linux-low.h (class linux_process_target): Update. * lynx-low.cc (lynx_target_ops): Update. * nto-low.cc (nto_target_ops): Update. (nto_stopped_by_watchpoint): Turn into ... (nto_process_target::stopped_by_watchpoint): ... this. (nto_stopped_data_address): Turn into ... (nto_process_target::stopped_data_address): ... this. * nto-low.h (class nto_process_target): Update. * win32-low.cc (win32_target_ops): Update. (win32_stopped_by_watchpoint): Turn into ... (win32_process_target::stopped_by_watchpoint): ... this. (win32_stopped_data_address): Turn into ... (win32_process_target::stopped_data_address): ... this. * win32-low.h (class win32_process_target): Update.
2020-02-17 16:11:55 +01:00
CORE_ADDR
nto_process_target::stopped_data_address ()
{
CORE_ADDR ret = (CORE_ADDR)0;
TRACE ("%s\n", __func__);
Rename current_inferior as current_thread in gdbserver GDB has a function named "current_inferior" and gdbserver has a global variable named "current_inferior", but the two are not equivalent; indeed, gdbserver does not have any real equivalent of what GDB calls an inferior. What gdbserver's "current_inferior" is actually pointing to is a structure describing the current thread. This commit renames current_inferior as current_thread in gdbserver to clarify this. It also renames the function "set_desired_inferior" to "set_desired_thread" and renames various local variables from foo_inferior to foo_thread. gdb/gdbserver/ChangeLog: * inferiors.h (current_inferior): Renamed as... (current_thread): New variable. All uses updated. * linux-low.c (get_pc): Renamed saved_inferior as saved_thread. (maybe_move_out_of_jump_pad): Likewise. (cancel_breakpoint): Likewise. (linux_low_filter_event): Likewise. (wait_for_sigstop): Likewise. (linux_resume_one_lwp): Likewise. (need_step_over_p): Likewise. (start_step_over): Likewise. (linux_stabilize_threads): Renamed save_inferior as saved_thread. * linux-x86-low.c (x86_linux_update_xmltarget): Likewise. * proc-service.c (ps_lgetregs): Renamed reg_inferior as reg_thread and save_inferior as saved_thread. * regcache.c (get_thread_regcache): Renamed saved_inferior as saved_thread. (regcache_invalidate_thread): Likewise. * remote-utils.c (prepare_resume_reply): Likewise. * thread-db.c (thread_db_get_tls_address): Likewise. (disable_thread_event_reporting): Likewise. (remove_thread_event_breakpoints): Likewise. * tracepoint.c (gdb_agent_about_to_close): Renamed save_inferior as saved_thread. * target.h (set_desired_inferior): Renamed as... (set_desired_thread): New declaration. All uses updated. * server.c (myresume): Updated comment to reference thread instead of inferior. (handle_serial_event): Likewise. (handle_target_event): Likewise.
2014-09-10 11:37:11 +02:00
if (nto_inferior.ctl_fd != -1 && current_thread != NULL)
{
ptid_t ptid = ptid_of (current_thread);
if (nto_set_thread (ptid))
{
procfs_status status;
if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status,
sizeof (status), 0) == EOK)
ret = status.ip;
}
}
TRACE ("%s: 0x%08lx\n", __func__, ret);
return ret;
}
/* Implementation of the target_ops method "sw_breakpoint_from_kind". */
static const gdb_byte *
nto_sw_breakpoint_from_kind (int kind, int *size)
{
*size = the_low_target.breakpoint_len;
return the_low_target.breakpoint;
}
gdbserver: start turning the target ops vector into a class This is the beginning of a series of patches where the goal is to turn the target ops vector into a class and all the target op function pointers into methods of this class. Currently, the target ops is a struct of function pointers. At the end of the series, it becomes a class with methods, and the existing low target definitions become subclasses. That is, we end up with the following class hierarchy: process_stratum_target ^ |-- linux-low |-- lynx-low |-- nto-low |-- win32-low process_stratum_target either defines the default behavior for the target ops or leaves them as pure virtual for the subclasses to override. The transformation is done by first introducing a helper class, called 'process_target', that is initially empty. An instance of this class is added to the end of the current target ops vector. This new field is called 'pt'. We will gradually carry target ops to the new class, one by one, whereas the invocation of the target op will be converted to a method call on 'pt'. For instance, target op 'attach' is currently invoked as (*the_target->attach) (args) After moving 'attach' as a method to 'process_target', it will be invoked as the_target->pt->attach (args) In this process, the concrete target vector definitions (e.g. linux-low, win32-low, nto-low, etc.) are turned into derived classes of 'process_target', so that they can either inherit the default behavior of the target ops or can override the method. We prefer to make this transition gradually rather than in a single giant patch, to yield bite-size patches. The goal is that after each patch gdbserver will still be buildable and testable. The general rule of thumb when converting a target op to a method is this: (1) If the function call is protected with a NULL-check with an obvious default behavior, simply implement that default behavior in the base class (e.g.: supports_non_stop). (2) If there is no NULL-check guard, the method becomes pure virtual, and the derived targets are required to implement the method (e.g.: attach). (3) If there is a NULL-check but no apparent default behavior, or if the NULL-check is utilized to populate a feature support packet, introduce a 'supports_XYZ' method (e.g.: pid_to_exec_file). The overall strategy is to preserve the existing behavior as much as possible. When we're done moving all the target ops into 'process_target', the target op vector will contain nothing but the field 'pt'. At that point, the auxiliary class 'process_target' will simply meld into 'process_stratum_target' and the method calls of the form 'the_target->pt->xyz' will be turned into 'the_target->xyz'. The "linux-low" target has been built and reg-tested on X86_64 Linux (Ubuntu). The "win32-low" target has been built (but not tested) via cross-compilation to a x86_64-w64-mingw32 target. The "lynx-low" and "nto-low" targets were neither built nor tested. gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> * target.h (class process_target): New class definition. (struct process_stratum_target) <pt>: New field with type 'process_target*'. * linux-low.h (class linux_process_target): Define as a derived class of 'process_target'. * linux-low.cc (linux_target_ops): Add a linux_process_target* as the 'pt' field. * lynx-low.h (class lynx_process_target): Define as a derived class of 'process_target'. * lynx-low.cc (lynx_target_ops): Add a lynx_process_target* as the 'pt' field. * nto-low.h (class nto_process_target): Define as a derived class of 'process_target'. * nto-low.cc (nto_target_ops): Add an nto_process_target* as the 'pt' field. * win32-low.h (class win32_process_target): Define as a derived class of 'process_target'. * win32-low.cc (win32_target_ops): Add a win32_process_target* as the 'pt' field.
2020-02-17 16:11:50 +01:00
/* The QNX Neutrino target ops object. */
static nto_process_target the_nto_target;
Multi-target support This commit adds multi-target support to GDB. What this means is that with this commit, GDB can now be connected to different targets at the same time. E.g., you can debug a live native process and a core dump at the same time, connect to multiple gdbservers, etc. Actually, the word "target" is overloaded in gdb. We already have a target stack, with pushes several target_ops instances on top of one another. We also have "info target" already, which means something completely different to what this patch does. So from here on, I'll be using the "target connections" term, to mean an open process_stratum target, pushed on a target stack. This patch makes gdb have multiple target stacks, and multiple process_stratum targets open simultaneously. The user-visible changes / commands will also use this terminology, but of course it's all open to debate. User-interface-wise, not that much changes. The main difference is that each inferior may have its own target connection. A target connection (e.g., a target extended-remote connection) may support debugging multiple processes, just as before. Say you're debugging against gdbserver in extended-remote mode, and you do "add-inferior" to prepare to spawn a new process, like: (gdb) target extended-remote :9999 ... (gdb) start ... (gdb) add-inferior Added inferior 2 (gdb) inferior 2 [Switching to inferior 2 [<null>] (<noexec>)] (gdb) file a.out ... (gdb) start ... At this point, you have two inferiors connected to the same gdbserver. With this commit, GDB will maintain a target stack per inferior, instead of a global target stack. To preserve the behavior above, by default, "add-inferior" makes the new inferior inherit a copy of the target stack of the current inferior. Same across a fork - the child inherits a copy of the target stack of the parent. While the target stacks are copied, the targets themselves are not. Instead, target_ops is made a refcounted_object, which means that target_ops instances are refcounted, which each inferior counting for a reference. What if you want to create an inferior and connect it to some _other_ target? For that, this commit introduces a new "add-inferior -no-connection" option that makes the new inferior not share the current inferior's target. So you could do: (gdb) target extended-remote :9999 Remote debugging using :9999 ... (gdb) add-inferior -no-connection [New inferior 2] Added inferior 2 (gdb) inferior 2 [Switching to inferior 2 [<null>] (<noexec>)] (gdb) info inferiors Num Description Executable 1 process 18401 target:/home/pedro/tmp/main * 2 <null> (gdb) tar extended-remote :10000 Remote debugging using :10000 ... (gdb) info inferiors Num Description Executable 1 process 18401 target:/home/pedro/tmp/main * 2 process 18450 target:/home/pedro/tmp/main (gdb) A following patch will extended "info inferiors" to include a column indicating which connection an inferior is bound to, along with a couple other UI tweaks. Other than that, debugging is the same as before. Users interact with inferiors and threads as before. The only difference is that inferiors may be bound to processes running in different machines. That's pretty much all there is to it in terms of noticeable UI changes. On to implementation. Since we can be connected to different systems at the same time, a ptid_t is no longer a unique identifier. Instead a thread can be identified by a pair of ptid_t and 'process_stratum_target *', the later being the instance of the process_stratum target that owns the process/thread. Note that process_stratum_target inherits from target_ops, and all process_stratum targets inherit from process_stratum_target. In earlier patches, many places in gdb were converted to refer to threads by thread_info pointer instead of ptid_t, but there are still places in gdb where we start with a pid/tid and need to find the corresponding inferior or thread_info objects. So you'll see in the patch many places adding a process_stratum_target parameter to functions that used to take only a ptid_t. Since each inferior has its own target stack now, we can always find the process_stratum target for an inferior. That is done via a inf->process_target() convenience method. Since each inferior has its own target stack, we need to handle the "beneath" calls when servicing target calls. The solution I settled with is just to make sure to switch the current inferior to the inferior you want before making a target call. Not relying on global context is just not feasible in current GDB. Fortunately, there aren't that many places that need to do that, because generally most code that calls target methods already has the current context pointing to the right inferior/thread. Note, to emphasize -- there's no method to "switch to this target stack". Instead, you switch the current inferior, and that implicitly switches the target stack. In some spots, we need to iterate over all inferiors so that we reach all target stacks. Native targets are still singletons. There's always only a single instance of such targets. Remote targets however, we'll have one instance per remote connection. The exec target is still a singleton. There's only one instance. I did not see the point of instanciating more than one exec_target object. After vfork, we need to make sure to push the exec target on the new inferior. See exec_on_vfork. For type safety, functions that need a {target, ptid} pair to identify a thread, take a process_stratum_target pointer for target parameter instead of target_ops *. Some shared code in gdb/nat/ also need to gain a target pointer parameter. This poses an issue, since gdbserver doesn't have process_stratum_target, only target_ops. To fix this, this commit renames gdbserver's target_ops to process_stratum_target. I think this makes sense. There's no concept of target stack in gdbserver, and gdbserver's target_ops really implements a process_stratum-like target. The thread and inferior iterator functions also gain process_stratum_target parameters. These are used to be able to iterate over threads and inferiors of a given target. Following usual conventions, if the target pointer is null, then we iterate over threads and inferiors of all targets. I tried converting "add-inferior" to the gdb::option framework, as a preparatory patch, but that stumbled on the fact that gdb::option does not support file options yet, for "add-inferior -exec". I have a WIP patchset that adds that, but it's not a trivial patch, mainly due to need to integrate readline's filename completion, so I deferred that to some other time. In infrun.c/infcmd.c, the main change is that we need to poll events out of all targets. See do_target_wait. Right after collecting an event, we switch the current inferior to an inferior bound to the target that reported the event, so that target methods can be used while handling the event. This makes most of the code transparent to multi-targets. See fetch_inferior_event. infrun.c:stop_all_threads is interesting -- in this function we need to stop all threads of all targets. What the function does is send an asynchronous stop request to all threads, and then synchronously waits for events, with target_wait, rinse repeat, until all it finds are stopped threads. Now that we have multiple targets, it's not efficient to synchronously block in target_wait waiting for events out of one target. Instead, we implement a mini event loop, with interruptible_select, select'ing on one file descriptor per target. For this to work, we need to be able to ask the target for a waitable file descriptor. Such file descriptors already exist, they are the descriptors registered in the main event loop with add_file_handler, inside the target_async implementations. This commit adds a new target_async_wait_fd target method that just returns the file descriptor in question. See wait_one / stop_all_threads in infrun.c. The 'threads_executing' global is made a per-target variable. Since it is only relevant to process_stratum_target targets, this is where it is put, instead of in target_ops. You'll notice that remote.c includes some FIXME notes. These refer to the fact that the global arrays that hold data for the remote packets supported are still globals. For example, if we connect to two different servers/stubs, then each might support different remote protocol features. They might even be different architectures, like e.g., one ARM baremetal stub, and a x86 gdbserver, to debug a host/controller scenario as a single program. That isn't going to work correctly today, because of said globals. I'm leaving fixing that for another pass, since it does not appear to be trivial, and I'd rather land the base work first. It's already useful to be able to debug multiple instances of the same server (e.g., a distributed cluster, where you have full control over the servers installed), so I think as is it's already reasonable incremental progress. Current limitations: - You can only resume more that one target at the same time if all targets support asynchronous debugging, and support non-stop mode. It should be possible to support mixed all-stop + non-stop backends, but that is left for another time. This means that currently in order to do multi-target with gdbserver you need to issue "maint set target-non-stop on". I would like to make that mode be the default, but we're not there yet. Note that I'm talking about how the target backend works, only. User-visible all-stop mode works just fine. - As explained above, connecting to different remote servers at the same time is likely to produce bad results if they don't support the exact set of RSP features. FreeBSD updates courtesy of John Baldwin. gdb/ChangeLog: 2020-01-10 Pedro Alves <palves@redhat.com> John Baldwin <jhb@FreeBSD.org> * aarch64-linux-nat.c (aarch64_linux_nat_target::thread_architecture): Adjust. * ada-tasks.c (print_ada_task_info): Adjust find_thread_ptid call. (task_command_1): Likewise. * aix-thread.c (sync_threadlists, aix_thread_target::resume) (aix_thread_target::wait, aix_thread_target::fetch_registers) (aix_thread_target::store_registers) (aix_thread_target::thread_alive): Adjust. * amd64-fbsd-tdep.c: Include "inferior.h". (amd64fbsd_get_thread_local_address): Pass down target. * amd64-linux-nat.c (ps_get_thread_area): Use ps_prochandle thread's gdbarch instead of target_gdbarch. * break-catch-sig.c (signal_catchpoint_print_it): Adjust call to get_last_target_status. * break-catch-syscall.c (print_it_catch_syscall): Likewise. * breakpoint.c (breakpoints_should_be_inserted_now): Consider all inferiors. (update_inserted_breakpoint_locations): Skip if inferiors with no execution. (update_global_location_list): When handling moribund locations, find representative inferior for location's pspace, and use thread count of its process_stratum target. * bsd-kvm.c (bsd_kvm_target_open): Pass target down. * bsd-uthread.c (bsd_uthread_target::wait): Use as_process_stratum_target and adjust thread_change_ptid and add_thread calls. (bsd_uthread_target::update_thread_list): Use as_process_stratum_target and adjust find_thread_ptid, thread_change_ptid and add_thread calls. * btrace.c (maint_btrace_packet_history_cmd): Adjust find_thread_ptid call. * corelow.c (add_to_thread_list): Adjust add_thread call. (core_target_open): Adjust add_thread_silent and thread_count calls. (core_target::pid_to_str): Adjust find_inferior_ptid call. * ctf.c (ctf_target_open): Adjust add_thread_silent call. * event-top.c (async_disconnect): Pop targets from all inferiors. * exec.c (add_target_sections): Push exec target on all inferiors sharing the program space. (remove_target_sections): Remove the exec target from all inferiors sharing the program space. (exec_on_vfork): New. * exec.h (exec_on_vfork): Declare. * fbsd-nat.c (fbsd_add_threads): Add fbsd_nat_target parameter. Pass it down. (fbsd_nat_target::update_thread_list): Adjust. (fbsd_nat_target::resume): Adjust. (fbsd_handle_debug_trap): Add fbsd_nat_target parameter. Pass it down. (fbsd_nat_target::wait, fbsd_nat_target::post_attach): Adjust. * fbsd-tdep.c (fbsd_corefile_thread): Adjust get_thread_arch_regcache call. * fork-child.c (gdb_startup_inferior): Pass target down to startup_inferior and set_executing. * gdbthread.h (struct process_stratum_target): Forward declare. (add_thread, add_thread_silent, add_thread_with_info) (in_thread_list): Add process_stratum_target parameter. (find_thread_ptid(inferior*, ptid_t)): New overload. (find_thread_ptid, thread_change_ptid): Add process_stratum_target parameter. (all_threads()): Delete overload. (all_threads, all_non_exited_threads): Add process_stratum_target parameter. (all_threads_safe): Use brace initialization. (thread_count): Add process_stratum_target parameter. (set_resumed, set_running, set_stop_requested, set_executing) (threads_are_executing, finish_thread_state): Add process_stratum_target parameter. (switch_to_thread): Use is_current_thread. * i386-fbsd-tdep.c: Include "inferior.h". (i386fbsd_get_thread_local_address): Pass down target. * i386-linux-nat.c (i386_linux_nat_target::low_resume): Adjust. * inf-child.c (inf_child_target::maybe_unpush_target): Remove have_inferiors check. * inf-ptrace.c (inf_ptrace_target::create_inferior) (inf_ptrace_target::attach): Adjust. * infcall.c (run_inferior_call): Adjust. * infcmd.c (run_command_1): Pass target to scoped_finish_thread_state. (proceed_thread_callback): Skip inferiors with no execution. (continue_command): Rename 'all_threads' local to avoid hiding 'all_threads' function. Adjust get_last_target_status call. (prepare_one_step): Adjust set_running call. (signal_command): Use user_visible_resume_target. Compare thread pointers instead of inferior_ptid. (info_program_command): Adjust to pass down target. (attach_command): Mark target's 'thread_executing' flag. (stop_current_target_threads_ns): New, factored out from ... (interrupt_target_1): ... this. Switch inferior before making target calls. * inferior-iter.h (struct all_inferiors_iterator, struct all_inferiors_range) (struct all_inferiors_safe_range) (struct all_non_exited_inferiors_range): Filter on process_stratum_target too. Remove explicit. * inferior.c (inferior::inferior): Push dummy target on target stack. (find_inferior_pid, find_inferior_ptid, number_of_live_inferiors): Add process_stratum_target parameter, and pass it down. (have_live_inferiors): Adjust. (switch_to_inferior_and_push_target): New. (add_inferior_command, clone_inferior_command): Handle "-no-connection" parameter. Use switch_to_inferior_and_push_target. (_initialize_inferior): Mention "-no-connection" option in the help of "add-inferior" and "clone-inferior" commands. * inferior.h: Include "process-stratum-target.h". (interrupt_target_1): Use bool. (struct inferior) <push_target, unpush_target, target_is_pushed, find_target_beneath, top_target, process_target, target_at, m_stack>: New. (discard_all_inferiors): Delete. (find_inferior_pid, find_inferior_ptid, number_of_live_inferiors) (all_inferiors, all_non_exited_inferiors): Add process_stratum_target parameter. * infrun.c: Include "gdb_select.h" and <unordered_map>. (target_last_proc_target): New global. (follow_fork_inferior): Push target on new inferior. Pass target to add_thread_silent. Call exec_on_vfork. Handle target's reference count. (follow_fork): Adjust get_last_target_status call. Also consider target. (follow_exec): Push target on new inferior. (struct execution_control_state) <target>: New field. (user_visible_resume_target): New. (do_target_resume): Call target_async. (resume_1): Set target's threads_executing flag. Consider resume target. (commit_resume_all_targets): New. (proceed): Also consider resume target. Skip threads of inferiors with no execution. Commit resumtion in all targets. (start_remote): Pass current inferior to wait_for_inferior. (infrun_thread_stop_requested): Consider target as well. Pass thread_info pointer to clear_inline_frame_state instead of ptid. (infrun_thread_thread_exit): Consider target as well. (random_pending_event_thread): New inferior parameter. Use it. (do_target_wait): Rename to ... (do_target_wait_1): ... this. Add inferior parameter, and pass it down. (threads_are_resumed_pending_p, do_target_wait): New. (prepare_for_detach): Adjust calls. (wait_for_inferior): New inferior parameter. Handle it. Use do_target_wait_1 instead of do_target_wait. (fetch_inferior_event): Adjust. Switch to representative inferior. Pass target down. (set_last_target_status): Add process_stratum_target parameter. Save target in global. (get_last_target_status): Add process_stratum_target parameter and handle it. (nullify_last_target_wait_ptid): Clear 'target_last_proc_target'. (context_switch): Check inferior_ptid == null_ptid before calling inferior_thread(). (get_inferior_stop_soon): Pass down target. (wait_one): Rename to ... (poll_one_curr_target): ... this. (struct wait_one_event): New. (wait_one): New. (stop_all_threads): Adjust. (handle_no_resumed, handle_inferior_event): Adjust to consider the event's target. (switch_back_to_stepped_thread): Also consider target. (print_stop_event): Update. (normal_stop): Update. Also consider the resume target. * infrun.h (wait_for_inferior): Remove declaration. (user_visible_resume_target): New declaration. (get_last_target_status, set_last_target_status): New process_stratum_target parameter. * inline-frame.c (clear_inline_frame_state(ptid_t)): Add process_stratum_target parameter, and use it. (clear_inline_frame_state (thread_info*)): New. * inline-frame.c (clear_inline_frame_state(ptid_t)): Add process_stratum_target parameter. (clear_inline_frame_state (thread_info*)): Declare. * linux-fork.c (delete_checkpoint_command): Pass target down to find_thread_ptid. (checkpoint_command): Adjust. * linux-nat.c (linux_nat_target::follow_fork): Switch to thread instead of just tweaking inferior_ptid. (linux_nat_switch_fork): Pass target down to thread_change_ptid. (exit_lwp): Pass target down to find_thread_ptid. (attach_proc_task_lwp_callback): Pass target down to add_thread/set_running/set_executing. (linux_nat_target::attach): Pass target down to thread_change_ptid. (get_detach_signal): Pass target down to find_thread_ptid. Consider last target status's target. (linux_resume_one_lwp_throw, resume_lwp) (linux_handle_syscall_trap, linux_handle_extended_wait, wait_lwp) (stop_wait_callback, save_stop_reason, linux_nat_filter_event) (linux_nat_wait_1, resume_stopped_resumed_lwps): Pass target down. (linux_nat_target::async_wait_fd): New. (linux_nat_stop_lwp, linux_nat_target::thread_address_space): Pass target down. * linux-nat.h (linux_nat_target::async_wait_fd): Declare. * linux-tdep.c (get_thread_arch_regcache): Pass target down. * linux-thread-db.c (struct thread_db_info::process_target): New field. (add_thread_db_info): Save target. (get_thread_db_info): New process_stratum_target parameter. Also match target. (delete_thread_db_info): New process_stratum_target parameter. Also match target. (thread_from_lwp): Adjust to pass down target. (thread_db_notice_clone): Pass down target. (check_thread_db_callback): Pass down target. (try_thread_db_load_1): Always push the thread_db target. (try_thread_db_load, record_thread): Pass target down. (thread_db_target::detach): Pass target down. Always unpush the thread_db target. (thread_db_target::wait, thread_db_target::mourn_inferior): Pass target down. Always unpush the thread_db target. (find_new_threads_callback, thread_db_find_new_threads_2) (thread_db_target::update_thread_list): Pass target down. (thread_db_target::pid_to_str): Pass current inferior down. (thread_db_target::get_thread_local_address): Pass target down. (thread_db_target::resume, maintenance_check_libthread_db): Pass target down. * nto-procfs.c (nto_procfs_target::update_thread_list): Adjust. * procfs.c (procfs_target::procfs_init_inferior): Declare. (proc_set_current_signal, do_attach, procfs_target::wait): Adjust. (procfs_init_inferior): Rename to ... (procfs_target::procfs_init_inferior): ... this and adjust. (procfs_target::create_inferior, procfs_notice_thread) (procfs_do_thread_registers): Adjust. * ppc-fbsd-tdep.c: Include "inferior.h". (ppcfbsd_get_thread_local_address): Pass down target. * proc-service.c (ps_xfer_memory): Switch current inferior and program space as well. (get_ps_regcache): Pass target down. * process-stratum-target.c (process_stratum_target::thread_address_space) (process_stratum_target::thread_architecture): Pass target down. * process-stratum-target.h (process_stratum_target::threads_executing): New field. (as_process_stratum_target): New. * ravenscar-thread.c (ravenscar_thread_target::update_inferior_ptid): Pass target down. (ravenscar_thread_target::wait, ravenscar_add_thread): Pass target down. * record-btrace.c (record_btrace_target::info_record): Adjust. (record_btrace_target::record_method) (record_btrace_target::record_is_replaying) (record_btrace_target::fetch_registers) (get_thread_current_frame_id, record_btrace_target::resume) (record_btrace_target::wait, record_btrace_target::stop): Pass target down. * record-full.c (record_full_wait_1): Switch to event thread. Pass target down. * regcache.c (regcache::regcache) (get_thread_arch_aspace_regcache, get_thread_arch_regcache): Add process_stratum_target parameter and handle it. (current_thread_target): New global. (get_thread_regcache): Add process_stratum_target parameter and handle it. Switch inferior before calling target method. (get_thread_regcache): Pass target down. (get_thread_regcache_for_ptid): Pass target down. (registers_changed_ptid): Add process_stratum_target parameter and handle it. (registers_changed_thread, registers_changed): Pass target down. (test_get_thread_arch_aspace_regcache): New. (current_regcache_test): Define a couple local test_target_ops instances and use them for testing. (readwrite_regcache): Pass process_stratum_target parameter. (cooked_read_test, cooked_write_test): Pass mock_target down. * regcache.h (get_thread_regcache, get_thread_arch_regcache) (get_thread_arch_aspace_regcache): Add process_stratum_target parameter. (regcache::target): New method. (regcache::regcache, regcache::get_thread_arch_aspace_regcache) (regcache::registers_changed_ptid): Add process_stratum_target parameter. (regcache::m_target): New field. (registers_changed_ptid): Add process_stratum_target parameter. * remote.c (remote_state::supports_vCont_probed): New field. (remote_target::async_wait_fd): New method. (remote_unpush_and_throw): Add remote_target parameter. (get_current_remote_target): Adjust. (remote_target::remote_add_inferior): Push target. (remote_target::remote_add_thread) (remote_target::remote_notice_new_inferior) (get_remote_thread_info): Pass target down. (remote_target::update_thread_list): Skip threads of inferiors bound to other targets. (remote_target::close): Don't discard inferiors. (remote_target::add_current_inferior_and_thread) (remote_target::process_initial_stop_replies) (remote_target::start_remote) (remote_target::remote_serial_quit_handler): Pass down target. (remote_target::remote_unpush_target): New remote_target parameter. Unpush the target from all inferiors. (remote_target::remote_unpush_and_throw): New remote_target parameter. Pass it down. (remote_target::open_1): Check whether the current inferior has execution instead of checking whether any inferior is live. Pass target down. (remote_target::remote_detach_1): Pass down target. Use remote_unpush_target. (extended_remote_target::attach): Pass down target. (remote_target::remote_vcont_probe): Set supports_vCont_probed. (remote_target::append_resumption): Pass down target. (remote_target::append_pending_thread_resumptions) (remote_target::remote_resume_with_hc, remote_target::resume) (remote_target::commit_resume): Pass down target. (remote_target::remote_stop_ns): Check supports_vCont_probed. (remote_target::interrupt_query) (remote_target::remove_new_fork_children) (remote_target::check_pending_events_prevent_wildcard_vcont) (remote_target::remote_parse_stop_reply) (remote_target::process_stop_reply): Pass down target. (first_remote_resumed_thread): New remote_target parameter. Pass it down. (remote_target::wait_as): Pass down target. (unpush_and_perror): New remote_target parameter. Pass it down. (remote_target::readchar, remote_target::remote_serial_write) (remote_target::getpkt_or_notif_sane_1) (remote_target::kill_new_fork_children, remote_target::kill): Pass down target. (remote_target::mourn_inferior): Pass down target. Use remote_unpush_target. (remote_target::core_of_thread) (remote_target::remote_btrace_maybe_reopen): Pass down target. (remote_target::pid_to_exec_file) (remote_target::thread_handle_to_thread_info): Pass down target. (remote_target::async_wait_fd): New. * riscv-fbsd-tdep.c: Include "inferior.h". (riscv_fbsd_get_thread_local_address): Pass down target. * sol2-tdep.c (sol2_core_pid_to_str): Pass down target. * sol-thread.c (sol_thread_target::wait, ps_lgetregs, ps_lsetregs) (ps_lgetfpregs, ps_lsetfpregs, sol_update_thread_list_callback): Adjust. * solib-spu.c (spu_skip_standalone_loader): Pass down target. * solib-svr4.c (enable_break): Pass down target. * spu-multiarch.c (parse_spufs_run): Pass down target. * spu-tdep.c (spu2ppu_sniffer): Pass down target. * target-delegates.c: Regenerate. * target.c (g_target_stack): Delete. (current_top_target): Return the current inferior's top target. (target_has_execution_1): Refer to the passed-in inferior's top target. (target_supports_terminal_ours): Check whether the initial inferior was already created. (decref_target): New. (target_stack::push): Incref/decref the target. (push_target, push_target, unpush_target): Adjust. (target_stack::unpush): Defref target. (target_is_pushed): Return bool. Adjust to refer to the current inferior's target stack. (dispose_inferior): Delete, and inline parts ... (target_preopen): ... here. Only dispose of the current inferior. (target_detach): Hold strong target reference while detaching. Pass target down. (target_thread_name): Add assertion. (target_resume): Pass down target. (target_ops::beneath, find_target_at): Adjust to refer to the current inferior's target stack. (get_dummy_target): New. (target_pass_ctrlc): Pass the Ctrl-C to the first inferior that has a thread running. (initialize_targets): Rename to ... (_initialize_target): ... this. * target.h: Include "gdbsupport/refcounted-object.h". (struct target_ops): Inherit refcounted_object. (target_ops::shortname, target_ops::longname): Make const. (target_ops::async_wait_fd): New method. (decref_target): Declare. (struct target_ops_ref_policy): New. (target_ops_ref): New typedef. (get_dummy_target): Declare function. (target_is_pushed): Return bool. * thread-iter.c (all_matching_threads_iterator::m_inf_matches) (all_matching_threads_iterator::all_matching_threads_iterator): Handle filter target. * thread-iter.h (struct all_matching_threads_iterator, struct all_matching_threads_range, class all_non_exited_threads_range): Filter by target too. Remove explicit. * thread.c (threads_executing): Delete. (inferior_thread): Pass down current inferior. (clear_thread_inferior_resources): Pass down thread pointer instead of ptid_t. (add_thread_silent, add_thread_with_info, add_thread): Add process_stratum_target parameter. Use it for thread and inferior searches. (is_current_thread): New. (thread_info::deletable): Use it. (find_thread_ptid, thread_count, in_thread_list) (thread_change_ptid, set_resumed, set_running): New process_stratum_target parameter. Pass it down. (set_executing): New process_stratum_target parameter. Pass it down. Adjust reference to 'threads_executing'. (threads_are_executing): New process_stratum_target parameter. Adjust reference to 'threads_executing'. (set_stop_requested, finish_thread_state): New process_stratum_target parameter. Pass it down. (switch_to_thread): Also match inferior. (switch_to_thread): New process_stratum_target parameter. Pass it down. (update_threads_executing): Reimplement. * top.c (quit_force): Pop targets from all inferior. (gdb_init): Don't call initialize_targets. * windows-nat.c (windows_nat_target) <get_windows_debug_event>: Declare. (windows_add_thread, windows_delete_thread): Adjust. (get_windows_debug_event): Rename to ... (windows_nat_target::get_windows_debug_event): ... this. Adjust. * tracefile-tfile.c (tfile_target_open): Pass down target. * gdbsupport/common-gdbthread.h (struct process_stratum_target): Forward declare. (switch_to_thread): Add process_stratum_target parameter. * mi/mi-interp.c (mi_on_resume_1): Add process_stratum_target parameter. Use it. (mi_on_resume): Pass target down. * nat/fork-inferior.c (startup_inferior): Add process_stratum_target parameter. Pass it down. * nat/fork-inferior.h (startup_inferior): Add process_stratum_target parameter. * python/py-threadevent.c (py_get_event_thread): Pass target down. gdb/gdbserver/ChangeLog: 2020-01-10 Pedro Alves <palves@redhat.com> * fork-child.c (post_fork_inferior): Pass target down to startup_inferior. * inferiors.c (switch_to_thread): Add process_stratum_target parameter. * lynx-low.c (lynx_target_ops): Now a process_stratum_target. * nto-low.c (nto_target_ops): Now a process_stratum_target. * linux-low.c (linux_target_ops): Now a process_stratum_target. * remote-utils.c (prepare_resume_reply): Pass the target to switch_to_thread. * target.c (the_target): Now a process_stratum_target. (done_accessing_memory): Pass the target to switch_to_thread. (set_target_ops): Ajust to use process_stratum_target. * target.h (struct target_ops): Rename to ... (struct process_stratum_target): ... this. (the_target, set_target_ops): Adjust. (prepare_to_access_memory): Adjust comment. * win32-low.c (child_xfer_memory): Adjust to use process_stratum_target. (win32_target_ops): Now a process_stratum_target.
2020-01-10 21:06:08 +01:00
static process_stratum_target nto_target_ops = {
NULL, /* emit_ops */
NULL, /* supports_disable_randomization */
NULL, /* qxfer_libraries_svr4 */
NULL, /* support_agent */
NULL, /* enable_btrace */
NULL, /* disable_btrace */
NULL, /* read_btrace */
NULL, /* read_btrace_conf */
NULL, /* supports_range_stepping */
NULL, /* pid_to_exec_file */
NULL, /* multifs_open */
NULL, /* multifs_unlink */
NULL, /* multifs_readlink */
NULL, /* breakpoint_kind_from_pc */
nto_sw_breakpoint_from_kind,
gdbserver: start turning the target ops vector into a class This is the beginning of a series of patches where the goal is to turn the target ops vector into a class and all the target op function pointers into methods of this class. Currently, the target ops is a struct of function pointers. At the end of the series, it becomes a class with methods, and the existing low target definitions become subclasses. That is, we end up with the following class hierarchy: process_stratum_target ^ |-- linux-low |-- lynx-low |-- nto-low |-- win32-low process_stratum_target either defines the default behavior for the target ops or leaves them as pure virtual for the subclasses to override. The transformation is done by first introducing a helper class, called 'process_target', that is initially empty. An instance of this class is added to the end of the current target ops vector. This new field is called 'pt'. We will gradually carry target ops to the new class, one by one, whereas the invocation of the target op will be converted to a method call on 'pt'. For instance, target op 'attach' is currently invoked as (*the_target->attach) (args) After moving 'attach' as a method to 'process_target', it will be invoked as the_target->pt->attach (args) In this process, the concrete target vector definitions (e.g. linux-low, win32-low, nto-low, etc.) are turned into derived classes of 'process_target', so that they can either inherit the default behavior of the target ops or can override the method. We prefer to make this transition gradually rather than in a single giant patch, to yield bite-size patches. The goal is that after each patch gdbserver will still be buildable and testable. The general rule of thumb when converting a target op to a method is this: (1) If the function call is protected with a NULL-check with an obvious default behavior, simply implement that default behavior in the base class (e.g.: supports_non_stop). (2) If there is no NULL-check guard, the method becomes pure virtual, and the derived targets are required to implement the method (e.g.: attach). (3) If there is a NULL-check but no apparent default behavior, or if the NULL-check is utilized to populate a feature support packet, introduce a 'supports_XYZ' method (e.g.: pid_to_exec_file). The overall strategy is to preserve the existing behavior as much as possible. When we're done moving all the target ops into 'process_target', the target op vector will contain nothing but the field 'pt'. At that point, the auxiliary class 'process_target' will simply meld into 'process_stratum_target' and the method calls of the form 'the_target->pt->xyz' will be turned into 'the_target->xyz'. The "linux-low" target has been built and reg-tested on X86_64 Linux (Ubuntu). The "win32-low" target has been built (but not tested) via cross-compilation to a x86_64-w64-mingw32 target. The "lynx-low" and "nto-low" targets were neither built nor tested. gdbserver/ChangeLog: 2020-02-20 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> * target.h (class process_target): New class definition. (struct process_stratum_target) <pt>: New field with type 'process_target*'. * linux-low.h (class linux_process_target): Define as a derived class of 'process_target'. * linux-low.cc (linux_target_ops): Add a linux_process_target* as the 'pt' field. * lynx-low.h (class lynx_process_target): Define as a derived class of 'process_target'. * lynx-low.cc (lynx_target_ops): Add a lynx_process_target* as the 'pt' field. * nto-low.h (class nto_process_target): Define as a derived class of 'process_target'. * nto-low.cc (nto_target_ops): Add an nto_process_target* as the 'pt' field. * win32-low.h (class win32_process_target): Define as a derived class of 'process_target'. * win32-low.cc (win32_target_ops): Add a win32_process_target* as the 'pt' field.
2020-02-17 16:11:50 +01:00
NULL, /* thread_name */
NULL, /* breakpoint_kind_from_current_state */
NULL, /* supports_software_single_step */
NULL, /* supports_catch_syscall */
NULL, /* get_ipa_tdesc_idx */
NULL, /* thread_handle */
&the_nto_target,
};
/* Global function called by server.c. Initializes QNX Neutrino
gdbserver. */
void
initialize_low (void)
{
sigset_t set;
TRACE ("%s\n", __func__);
set_target_ops (&nto_target_ops);
/* We use SIGUSR1 to gain control after we block waiting for a process.
We use sigwaitevent to wait. */
sigemptyset (&set);
sigaddset (&set, SIGUSR1);
sigprocmask (SIG_BLOCK, &set, NULL);
}