/* Native-dependent code for x86 (i386 and x86-64).
- Copyright (C) 2001-2017 Free Software Foundation, Inc.
+ Copyright (C) 2001-2023 Free Software Foundation, Inc.
This file is part of GDB.
#include "gdbcmd.h"
#include "inferior.h"
+#include <unordered_map>
+
/* Support for hardware watchpoints and breakpoints using the x86
debug registers.
/* Low-level function vector. */
struct x86_dr_low_type x86_dr_low;
-/* Per-process data. We don't bind this to a per-inferior registry
- because of targets like x86 GNU/Linux that need to keep track of
- processes that aren't bound to any inferior (e.g., fork children,
- checkpoints). */
-
-struct x86_process_info
-{
- /* Linked list. */
- struct x86_process_info *next;
-
- /* The process identifier. */
- pid_t pid;
-
- /* Copy of x86 hardware debug registers. */
- struct x86_debug_reg_state state;
-};
-
-static struct x86_process_info *x86_process_list = NULL;
-
-/* Find process data for process PID. */
-
-static struct x86_process_info *
-x86_find_process_pid (pid_t pid)
-{
- struct x86_process_info *proc;
-
- for (proc = x86_process_list; proc; proc = proc->next)
- if (proc->pid == pid)
- return proc;
+/* Hash table storing per-process data. We don't bind this to a
+ per-inferior registry because of targets like x86 GNU/Linux that
+ need to keep track of processes that aren't bound to any inferior
+ (e.g., fork children, checkpoints). */
- return NULL;
-}
+static std::unordered_map<pid_t,
+ struct x86_debug_reg_state> x86_debug_process_state;
-/* Add process data for process PID. Returns newly allocated info
- object. */
+/* See x86-nat.h. */
-static struct x86_process_info *
-x86_add_process (pid_t pid)
+struct x86_debug_reg_state *
+x86_lookup_debug_reg_state (pid_t pid)
{
- struct x86_process_info *proc = XCNEW (struct x86_process_info);
+ auto it = x86_debug_process_state.find (pid);
+ if (it != x86_debug_process_state.end ())
+ return &it->second;
- proc->pid = pid;
- proc->next = x86_process_list;
- x86_process_list = proc;
-
- return proc;
-}
-
-/* Get data specific info for process PID, creating it if necessary.
- Never returns NULL. */
-
-static struct x86_process_info *
-x86_process_info_get (pid_t pid)
-{
- struct x86_process_info *proc;
-
- proc = x86_find_process_pid (pid);
- if (proc == NULL)
- proc = x86_add_process (pid);
-
- return proc;
+ return nullptr;
}
/* Get debug registers state for process PID. */
struct x86_debug_reg_state *
x86_debug_reg_state (pid_t pid)
{
- return &x86_process_info_get (pid)->state;
+ return &x86_debug_process_state[pid];
}
/* See declaration in x86-nat.h. */
void
x86_forget_process (pid_t pid)
{
- struct x86_process_info *proc, **proc_link;
-
- proc = x86_process_list;
- proc_link = &x86_process_list;
-
- while (proc != NULL)
- {
- if (proc->pid == pid)
- {
- *proc_link = proc->next;
-
- xfree (proc);
- return;
- }
-
- proc_link = &proc->next;
- proc = *proc_link;
- }
+ x86_debug_process_state.erase (pid);
}
/* Clear the reference counts and forget everything we knew about the
x86_cleanup_dregs (void)
{
/* Starting from scratch has the same effect. */
- x86_forget_process (ptid_get_pid (inferior_ptid));
+ x86_forget_process (inferior_ptid.pid ());
}
/* Insert a watchpoint to watch a memory region which starts at
address ADDR and whose length is LEN bytes. Watch memory accesses
of the type TYPE. Return 0 on success, -1 on failure. */
-static int
-x86_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
+int
+x86_insert_watchpoint (CORE_ADDR addr, int len,
enum target_hw_bp_type type, struct expression *cond)
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
return x86_dr_insert_watchpoint (state, type, addr, len);
}
/* Remove a watchpoint that watched the memory region which starts at
address ADDR, whose length is LEN bytes, and for accesses of the
type TYPE. Return 0 on success, -1 on failure. */
-static int
-x86_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
+int
+x86_remove_watchpoint (CORE_ADDR addr, int len,
enum target_hw_bp_type type, struct expression *cond)
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
return x86_dr_remove_watchpoint (state, type, addr, len);
}
/* Return non-zero if we can watch a memory region that starts at
address ADDR and whose length is LEN bytes. */
-static int
-x86_region_ok_for_watchpoint (struct target_ops *self,
- CORE_ADDR addr, int len)
+int
+x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
return x86_dr_region_ok_for_watchpoint (state, addr, len);
}
address associated with that break/watchpoint and return non-zero.
Otherwise, return zero. */
-static int
-x86_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
+int
+x86_stopped_data_address (CORE_ADDR *addr_p)
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
return x86_dr_stopped_data_address (state, addr_p);
}
/* Return non-zero if the inferior has some watchpoint that triggered.
Otherwise return zero. */
-static int
-x86_stopped_by_watchpoint (struct target_ops *ops)
+int
+x86_stopped_by_watchpoint ()
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
return x86_dr_stopped_by_watchpoint (state);
}
/* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
Return 0 on success, EBUSY on failure. */
-static int
-x86_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
- struct bp_target_info *bp_tgt)
+int
+x86_insert_hw_breakpoint (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
bp_tgt->placed_address = bp_tgt->reqstd_address;
return x86_dr_insert_watchpoint (state, hw_execute,
/* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
Return 0 on success, -1 on failure. */
-static int
-x86_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
+int
+x86_remove_hw_breakpoint (struct gdbarch *gdbarch,
struct bp_target_info *bp_tgt)
{
struct x86_debug_reg_state *state
- = x86_debug_reg_state (ptid_get_pid (inferior_ptid));
+ = x86_debug_reg_state (inferior_ptid.pid ());
return x86_dr_remove_watchpoint (state, hw_execute,
bp_tgt->placed_address, 1);
virtually unlimited number of watchpoints, due to debug register
sharing implemented via reference counts in x86-nat.c. */
-static int
-x86_can_use_hw_breakpoint (struct target_ops *self,
- enum bptype type, int cnt, int othertype)
+int
+x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype)
{
return 1;
}
+/* Return non-zero if the inferior has some breakpoint that triggered.
+ Otherwise return zero. */
+
+int
+x86_stopped_by_hw_breakpoint ()
+{
+ struct x86_debug_reg_state *state
+ = x86_debug_reg_state (inferior_ptid.pid ());
+
+ return x86_dr_stopped_by_hw_breakpoint (state);
+}
+
static void
add_show_debug_regs_command (void)
{
&maintenance_show_cmdlist);
}
-/* There are only two global functions left. */
-
-void
-x86_use_watchpoints (struct target_ops *t)
-{
- /* After a watchpoint trap, the PC points to the instruction after the
- one that caused the trap. Therefore we don't need to step over it.
- But we do need to reset the status register to avoid another trap. */
- t->to_have_continuable_watchpoint = 1;
-
- t->to_can_use_hw_breakpoint = x86_can_use_hw_breakpoint;
- t->to_region_ok_for_hw_watchpoint = x86_region_ok_for_watchpoint;
- t->to_stopped_by_watchpoint = x86_stopped_by_watchpoint;
- t->to_stopped_data_address = x86_stopped_data_address;
- t->to_insert_watchpoint = x86_insert_watchpoint;
- t->to_remove_watchpoint = x86_remove_watchpoint;
- t->to_insert_hw_breakpoint = x86_insert_hw_breakpoint;
- t->to_remove_hw_breakpoint = x86_remove_hw_breakpoint;
-}
+/* See x86-nat.h. */
void
x86_set_debug_register_length (int len)