/* Select target systems and architectures at runtime for GDB.
Copyright (C) 1990-2025 Free Software Foundation, Inc.
Contributed by Cygnus Support.
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 "target.h"
#include "extract-store-integer.h"
#include "target-dcache.h"
#include "cli/cli-cmds.h"
#include "symtab.h"
#include "inferior.h"
#include "infrun.h"
#include "observable.h"
#include "bfd.h"
#include "symfile.h"
#include "objfiles.h"
#include "dcache.h"
#include <signal.h>
#include "regcache.h"
#include "gdbcore.h"
#include "target-descriptions.h"
#include "gdbthread.h"
#include "solib.h"
#include "exec.h"
#include "inline-frame.h"
#include "tracepoint.h"
#include "gdbsupport/fileio.h"
#include "gdbsupport/agent.h"
#include "auxv.h"
#include "target-debug.h"
#include "ui.h"
#include "event-top.h"
#include <algorithm>
#include "gdbsupport/byte-vector.h"
#include "gdbsupport/search.h"
#include "terminal.h"
#include "gdbsupport/unordered_map.h"
#include "target-connection.h"
#include "valprint.h"
#include "cli/cli-decode.h"
#include "cli/cli-style.h"
[[noreturn]] static void generic_tls_error (void);
static void default_rcmd (struct target_ops *, const char *, struct ui_file *);
static int default_verify_memory (struct target_ops *self,
const gdb_byte *data,
CORE_ADDR memaddr, ULONGEST size);
[[noreturn]] static void tcomplain (void);
/* Mapping between target_info objects (which have address identity)
and corresponding open/factory function/callback. Each add_target
call adds one entry to this map, and registers a "target
TARGET_NAME" command that when invoked calls the factory registered
here. The target_info object is associated with the command via
the command's context. */
static gdb::unordered_map<const target_info *, target_open_ftype *>
target_factories;
/* The singleton debug target. */
static struct target_ops *the_debug_target;
/* Command list for target. */
static struct cmd_list_element *targetlist = NULL;
/* See target.h. */
bool trust_readonly = false;
/* Nonzero if we should show true memory content including
memory breakpoint inserted by gdb. */
static int show_memory_breakpoints = 0;
/* These globals control whether GDB attempts to perform these
operations; they are useful for targets that need to prevent
inadvertent disruption, such as in non-stop mode. */
bool may_write_registers = true;
bool may_write_memory = true;
bool may_insert_breakpoints = true;
bool may_insert_tracepoints = true;
bool may_insert_fast_tracepoints = true;
bool may_stop = true;
/* Non-zero if we want to see trace of target level stuff. */
static unsigned int targetdebug = 0;
/* Print a "target" debug statement with the function name prefix. */
#define target_debug_printf(fmt, ...) \
debug_prefixed_printf_cond (targetdebug > 0, "target", fmt, ##__VA_ARGS__)
/* Print a "target" debug statement without the function name prefix. */
#define target_debug_printf_nofunc(fmt, ...) \
debug_prefixed_printf_cond_nofunc (targetdebug > 0, "target", fmt, ##__VA_ARGS__)
static void
set_targetdebug (const char *args, int from_tty, struct cmd_list_element *c)
{
if (targetdebug)
current_inferior ()->push_target (the_debug_target);
else
current_inferior ()->unpush_target (the_debug_target);
}
static void
show_targetdebug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
gdb_printf (file, _("Target debugging is %s.\n"), value);
}
int
target_has_memory ()
{
for (target_ops *t = current_inferior ()->top_target ();
t != NULL;
t = t->beneath ())
if (t->has_memory ())
return 1;
return 0;
}
int
target_has_stack ()
{
for (target_ops *t = current_inferior ()->top_target ();
t != NULL;
t = t->beneath ())
if (t->has_stack ())
return 1;
return 0;
}
int
target_has_registers ()
{
for (target_ops *t = current_inferior ()->top_target ();
t != NULL;
t = t->beneath ())
if (t->has_registers ())
return 1;
return 0;
}
bool
target_has_execution (inferior *inf)
{
if (inf == nullptr)
inf = current_inferior ();
for (target_ops *t = inf->top_target ();
t != nullptr;
t = inf->find_target_beneath (t))
if (t->has_execution (inf))
return true;
return false;
}
const char *
target_shortname ()
{
return current_inferior ()->top_target ()->shortname ();
}
/* See target.h. */
bool
target_attach_no_wait ()
{
return current_inferior ()->top_target ()->attach_no_wait ();
}
/* See target.h. */
void
target_post_attach (int pid)
{
return current_inferior ()->top_target ()->post_attach (pid);
}
/* See target.h. */
void
target_prepare_to_store (regcache *regcache)
{
return current_inferior ()->top_target ()->prepare_to_store (regcache);
}
/* See target.h. */
bool
target_supports_enable_disable_tracepoint ()
{
target_ops *target = current_inferior ()->top_target ();
return target->supports_enable_disable_tracepoint ();
}
bool
target_supports_string_tracing ()
{
return current_inferior ()->top_target ()->supports_string_tracing ();
}
/* See target.h. */
bool
target_supports_evaluation_of_breakpoint_conditions ()
{
target_ops *target = current_inferior ()->top_target ();
return target->supports_evaluation_of_breakpoint_conditions ();
}
/* See target.h. */
bool
target_supports_dumpcore ()
{
return current_inferior ()->top_target ()->supports_dumpcore ();
}
/* See target.h. */
void
target_dumpcore (const char *filename)
{
return current_inferior ()->top_target ()->dumpcore (filename);
}
/* See target.h. */
bool
target_can_run_breakpoint_commands ()
{
return current_inferior ()->top_target ()->can_run_breakpoint_commands ();
}
/* See target.h. */
void
target_files_info ()
{
return current_inferior ()->top_target ()->files_info ();
}
/* See target.h. */
int
target_insert_fork_catchpoint (int pid)
{
return current_inferior ()->top_target ()->insert_fork_catchpoint (pid);
}
/* See target.h. */
int
target_remove_fork_catchpoint (int pid)
{
return current_inferior ()->top_target ()->remove_fork_catchpoint (pid);
}
/* See target.h. */
int
target_insert_vfork_catchpoint (int pid)
{
return current_inferior ()->top_target ()->insert_vfork_catchpoint (pid);
}
/* See target.h. */
int
target_remove_vfork_catchpoint (int pid)
{
return current_inferior ()->top_target ()->remove_vfork_catchpoint (pid);
}
/* See target.h. */
int
target_insert_exec_catchpoint (int pid)
{
return current_inferior ()->top_target ()->insert_exec_catchpoint (pid);
}
/* See target.h. */
int
target_remove_exec_catchpoint (int pid)
{
return current_inferior ()->top_target ()->remove_exec_catchpoint (pid);
}
/* See target.h. */
int
target_set_syscall_catchpoint (int pid, bool needed, int any_count,
gdb::array_view<const int> syscall_counts)
{
target_ops *target = current_inferior ()->top_target ();
return target->set_syscall_catchpoint (pid, needed, any_count,
syscall_counts);
}
/* See target.h. */
void
target_rcmd (const char *command, struct ui_file *outbuf)
{
return current_inferior ()->top_target ()->rcmd (command, outbuf);
}
/* See target.h. */
bool
target_can_lock_scheduler ()
{
target_ops *target = current_inferior ()->top_target ();
return (target->get_thread_control_capabilities ()& tc_schedlock) != 0;
}
/* See target.h. */
bool
target_can_async_p ()
{
return target_can_async_p (current_inferior ()->top_target ());
}
/* See target.h. */
bool
target_can_async_p (struct target_ops *target)
{
if (!target_async_permitted)
return false;
return target->can_async_p ();
}
/* See target.h. */
bool
target_is_async_p ()
{
bool result = current_inferior ()->top_target ()->is_async_p ();
gdb_assert (target_async_permitted || !result);
return result;
}
exec_direction_kind
target_execution_direction ()
{
return current_inferior ()->top_target ()->execution_direction ();
}
/* See target.h. */
const char *
target_extra_thread_info (thread_info *tp)
{
return current_inferior ()->top_target ()->extra_thread_info (tp);
}
/* See target.h. */
const char *
target_pid_to_exec_file (int pid)
{
return current_inferior ()->top_target ()->pid_to_exec_file (pid);
}
/* See target.h. */
gdbarch *
target_thread_architecture (ptid_t ptid)
{
return current_inferior ()->top_target ()->thread_architecture (ptid);
}
/* See target.h. */
int
target_find_memory_regions (find_memory_region_ftype func, void *data)
{
return current_inferior ()->top_target ()->find_memory_regions (func, data);
}
/* See target.h. */
gdb::unique_xmalloc_ptr<char>
target_make_corefile_notes (bfd *bfd, int *size_p)
{
return current_inferior ()->top_target ()->make_corefile_notes (bfd, size_p);
}
gdb_byte *
target_get_bookmark (const char *args, int from_tty)
{
return current_inferior ()->top_target ()->get_bookmark (args, from_tty);
}
void
target_goto_bookmark (const gdb_byte *arg, int from_tty)
{
return current_inferior ()->top_target ()->goto_bookmark (arg, from_tty);
}
/* See target.h. */
bool
target_stopped_by_watchpoint ()
{
return current_inferior ()->top_target ()->stopped_by_watchpoint ();
}
/* See target.h. */
bool
target_stopped_by_sw_breakpoint ()
{
return current_inferior ()->top_target ()->stopped_by_sw_breakpoint ();
}
bool
target_supports_stopped_by_sw_breakpoint ()
{
target_ops *target = current_inferior ()->top_target ();
return target->supports_stopped_by_sw_breakpoint ();
}
bool
target_stopped_by_hw_breakpoint ()
{
return current_inferior ()->top_target ()->stopped_by_hw_breakpoint ();
}
bool
target_supports_stopped_by_hw_breakpoint ()
{
target_ops *target = current_inferior ()->top_target ();
return target->supports_stopped_by_hw_breakpoint ();
}
/* See target.h. */
bool
target_have_steppable_watchpoint ()
{
return current_inferior ()->top_target ()->have_steppable_watchpoint ();
}
/* See target.h. */
int
target_can_use_hardware_watchpoint (bptype type, int cnt, int othertype)
{
target_ops *target = current_inferior ()->top_target ();
return target->can_use_hw_breakpoint (type, cnt, othertype);
}
/* See target.h. */
int
target_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
{
target_ops *target = current_inferior ()->top_target ();
return target->region_ok_for_hw_watchpoint (addr, len);
}
int
target_can_do_single_step ()
{
return current_inferior ()->top_target ()->can_do_single_step ();
}
/* See target.h. */
int
target_insert_watchpoint (CORE_ADDR addr, int len, target_hw_bp_type type,
expression *cond)
{
target_ops *target = current_inferior ()->top_target ();
return target->insert_watchpoint (addr, len, type, cond);
}
/* See target.h. */
int
target_remove_watchpoint (CORE_ADDR addr, int len, target_hw_bp_type type,
expression *cond)
{
target_ops *target = current_inferior ()->top_target ();
return target->remove_watchpoint (addr, len, type, cond);
}
/* See target.h. */
int
target_insert_hw_breakpoint (gdbarch *gdbarch, bp_target_info *bp_tgt)
{
target_ops *target = current_inferior ()->top_target ();
return target->insert_hw_breakpoint (gdbarch, bp_tgt);
}
/* See target.h. */
int
target_remove_hw_breakpoint (gdbarch *gdbarch, bp_target_info *bp_tgt)
{
target_ops *target = current_inferior ()->top_target ();
return target->remove_hw_breakpoint (gdbarch, bp_tgt);
}
/* See target.h. */
bool
target_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int type,
expression *cond)
{
target_ops *target = current_inferior ()->top_target ();
return target->can_accel_watchpoint_condition (addr, len, type, cond);
}
/* See target.h. */
bool
target_can_execute_reverse ()
{
return current_inferior ()->top_target ()->can_execute_reverse ();
}
ptid_t
target_get_ada_task_ptid (long lwp, ULONGEST tid)
{
return current_inferior ()->top_target ()->get_ada_task_ptid (lwp, tid);
}
bool
target_filesystem_is_local ()
{
return current_inferior ()->top_target ()->filesystem_is_local ();
}
void
target_trace_init ()
{
return current_inferior ()->top_target ()->trace_init ();
}
void
target_download_tracepoint (bp_location *location)
{
return current_inferior ()->top_target ()->download_tracepoint (location);
}
bool
target_can_download_tracepoint ()
{
return current_inferior ()->top_target ()->can_download_tracepoint ();
}
void
target_download_trace_state_variable (const trace_state_variable &tsv)
{
target_ops *target = current_inferior ()->top_target ();
return target->download_trace_state_variable (tsv);
}
void
target_enable_tracepoint (bp_location *loc)
{
return current_inferior ()->top_target ()->enable_tracepoint (loc);
}
void
target_disable_tracepoint (bp_location *loc)
{
return current_inferior ()->top_target ()->disable_tracepoint (loc);
}
void
target_trace_start ()
{
return current_inferior ()->top_target ()->trace_start ();
}
void
target_trace_set_readonly_regions ()
{
return current_inferior ()->top_target ()->trace_set_readonly_regions ();
}
int
target_get_trace_status (trace_status *ts)
{
return current_inferior ()->top_target ()->get_trace_status (ts);
}
void
target_get_tracepoint_status (tracepoint *tp, uploaded_tp *utp)
{
return current_inferior ()->top_target ()->get_tracepoint_status (tp, utp);
}
void
target_trace_stop ()
{
return current_inferior ()->top_target ()->trace_stop ();
}
int
target_trace_find (trace_find_type type, int num,
CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
{
target_ops *target = current_inferior ()->top_target ();
return target->trace_find (type, num, addr1, addr2, tpp);
}
bool
target_get_trace_state_variable_value (int tsv, LONGEST *val)
{
target_ops *target = current_inferior ()->top_target ();
return target->get_trace_state_variable_value (tsv, val);
}
int
target_save_trace_data (const char *filename)
{
return current_inferior ()->top_target ()->save_trace_data (filename);
}
int
target_upload_tracepoints (uploaded_tp **utpp)
{
return current_inferior ()->top_target ()->upload_tracepoints (utpp);
}
int
target_upload_trace_state_variables (uploaded_tsv **utsvp)
{
target_ops *target = current_inferior ()->top_target ();
return target->upload_trace_state_variables (utsvp);
}
LONGEST
target_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
{
target_ops *target = current_inferior ()->top_target ();
return target->get_raw_trace_data (buf, offset, len);
}
int
target_get_min_fast_tracepoint_insn_len ()
{
target_ops *target = current_inferior ()->top_target ();
return target->get_min_fast_tracepoint_insn_len ();
}
void
target_set_disconnected_tracing (int val)
{
return current_inferior ()->top_target ()->set_disconnected_tracing (val);
}
void
target_set_circular_trace_buffer (int val)
{
return current_inferior ()->top_target ()->set_circular_trace_buffer (val);
}
void
target_set_trace_buffer_size (LONGEST val)
{
return current_inferior ()->top_target ()->set_trace_buffer_size (val);
}
bool
target_set_trace_notes (const char *user, const char *notes,
const char *stopnotes)
{
target_ops *target = current_inferior ()->top_target ();
return target->set_trace_notes (user, notes, stopnotes);
}
bool
target_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
{
return current_inferior ()->top_target ()->get_tib_address (ptid, addr);
}
void
target_set_permissions ()
{
return current_inferior ()->top_target ()->set_permissions ();
}
bool
target_static_tracepoint_marker_at (CORE_ADDR addr,
static_tracepoint_marker *marker)
{
target_ops *target = current_inferior ()->top_target ();
return target->static_tracepoint_marker_at (addr, marker);
}
std::vector<static_tracepoint_marker>
target_static_tracepoint_markers_by_strid (const char *marker_id)
{
target_ops *target = current_inferior ()->top_target ();
return target->static_tracepoint_markers_by_strid (marker_id);
}
traceframe_info_up
target_traceframe_info ()
{
return current_inferior ()->top_target ()->traceframe_info ();
}
bool
target_use_agent (bool use)
{
return current_inferior ()->top_target ()->use_agent (use);
}
bool
target_can_use_agent ()
{
return current_inferior ()->top_target ()->can_use_agent ();
}
bool
target_augmented_libraries_svr4_read ()
{
return current_inferior ()->top_target ()->augmented_libraries_svr4_read ();
}
bool
target_supports_memory_tagging ()
{
return current_inferior ()->top_target ()->supports_memory_tagging ();
}
bool
target_fetch_memtags (CORE_ADDR address, size_t len, gdb::byte_vector &tags,
int type)
{
return current_inferior ()->top_target ()->fetch_memtags (address, len, tags, type);
}
bool
target_store_memtags (CORE_ADDR address, size_t len,
const gdb::byte_vector &tags, int type)
{
return current_inferior ()->top_target ()->store_memtags (address, len, tags, type);
}
bool
target_is_address_tagged (gdbarch *gdbarch, CORE_ADDR address)
{
return current_inferior ()->top_target ()->is_address_tagged (gdbarch, address);
}
x86_xsave_layout
target_fetch_x86_xsave_layout ()
{
return current_inferior ()->top_target ()->fetch_x86_xsave_layout ();
}
void
target_log_command (const char *p)
{
return current_inferior ()->top_target ()->log_command (p);
}
/* This is used to implement the various target commands. */
static void
open_target (const char *args, int from_tty, struct cmd_list_element *command)
{
auto *ti = static_cast<target_info *> (command->context ());
target_open_ftype *func = target_factories[ti];
target_debug_printf_nofunc ("-> %s->open (...)", ti->shortname);
func (args, from_tty);
target_debug_printf_nofunc ("<- %s->open (%s, %d)", ti->shortname, args, from_tty);
}
/* See target.h. */
void
add_target (const target_info &t, target_open_ftype *func,
completer_ftype *completer)
{
struct cmd_list_element *c;
auto &func_slot = target_factories[&t];
if (func_slot != nullptr)
internal_error (_("target already added (\"%s\")."), t.shortname);
func_slot = func;
if (targetlist == NULL)
add_basic_prefix_cmd ("target", class_run, _("\
Connect to a target machine or process.\n\
The first argument is the type or protocol of the target machine.\n\
Remaining arguments are interpreted by the target protocol. For more\n\
information on the arguments for a particular protocol, type\n\
`help target ' followed by the protocol name."),
&targetlist, 0, &cmdlist);
c = add_cmd (t.shortname, no_class, t.doc, &targetlist);
c->set_context ((void *) &t);
c->func = open_target;
if (completer != NULL)
set_cmd_completer (c, completer);
}
/* See target.h. */
void
add_deprecated_target_alias (const target_info &tinfo, const char *alias)
{
struct cmd_list_element *c;
/* If we use add_alias_cmd, here, we do not get the deprecated warning,
see PR cli/15104. */
c = add_cmd (alias, no_class, tinfo.doc, &targetlist);
c->func = open_target;
c->set_context ((void *) &tinfo);
gdb::unique_xmalloc_ptr<char> alt
= xstrprintf ("target %s", tinfo.shortname);
deprecate_cmd (c, alt.release ());
}
/* Stub functions */
void
target_kill (void)
{
/* If the commit_resume_state of the to-be-killed-inferior's process stratum
is true, and this inferior is the last live inferior with resumed threads
of that target, then we want to leave commit_resume_state to false, as the
target won't have any resumed threads anymore. We achieve this with
this scoped_disable_commit_resumed. On construction, it will set the flag
to false. On destruction, it will only set it to true if there are resumed
threads left. */
scoped_disable_commit_resumed disable ("killing");
current_inferior ()->top_target ()->kill ();
}
void
target_load (const char *arg, int from_tty)
{
target_dcache_invalidate (current_program_space->aspace);
current_inferior ()->top_target ()->load (arg, from_tty);
}
/* Define it. */
target_terminal_state target_terminal::m_terminal_state
= target_terminal_state::is_ours;
/* See target/target.h. */
void
target_terminal::init (void)
{
current_inferior ()->top_target ()->terminal_init ();
m_terminal_state = target_terminal_state::is_ours;
}
/* See target/target.h. */
void
target_terminal::inferior (void)
{
struct ui *ui = current_ui;
/* A background resume (``run&'') should leave GDB in control of the
terminal. */
if (ui->prompt_state != PROMPT_BLOCKED)
return;
/* Since we always run the inferior in the main console (unless "set
inferior-tty" is in effect), when some UI other than the main one
calls target_terminal::inferior, then we leave the main UI's
terminal settings as is. */
if (ui != main_ui)
return;
/* If GDB is resuming the inferior in the foreground, install
inferior's terminal modes. */
struct inferior *inf = current_inferior ();
if (inf->terminal_state != target_terminal_state::is_inferior)
{
current_inferior ()->top_target ()->terminal_inferior ();
inf->terminal_state = target_terminal_state::is_inferior;
}
m_terminal_state = target_terminal_state::is_inferior;
/* If the user hit C-c before, pretend that it was hit right
here. */
if (check_quit_flag ())
target_pass_ctrlc ();
}
/* See target/target.h. */
void
target_terminal::restore_inferior (void)
{
struct ui *ui = current_ui;
/* See target_terminal::inferior(). */
if (ui->prompt_state != PROMPT_BLOCKED || ui != main_ui)
return;
/* Restore the terminal settings of inferiors that were in the
foreground but are now ours_for_output due to a temporary
target_target::ours_for_output() call. */
{
scoped_restore_current_inferior restore_inferior;
for (::inferior *inf : all_inferiors ())
{
if (inf->terminal_state == target_terminal_state::is_ours_for_output)
{
set_current_inferior (inf);
current_inferior ()->top_target ()->terminal_inferior ();
inf->terminal_state = target_terminal_state::is_inferior;
}
}
}
m_terminal_state = target_terminal_state::is_inferior;
|