#include "gdbsupport/gdb_sys_time.h"
-#include "event-loop.h"
+#include "gdbsupport/event-loop.h"
#include "event-top.h"
#include "inf-loop.h"
#include "gdbsupport/scoped_restore.h"
#include "gdbsupport/environ.h"
#include "gdbsupport/byte-vector.h"
+#include "gdbsupport/search.h"
#include <algorithm>
#include <unordered_map>
+#include "async-event.h"
/* The remote target. */
void commit_resume () override;
void resume (ptid_t, int, enum gdb_signal) override;
- ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
+ ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
void fetch_registers (struct regcache *, int) override;
void store_registers (struct regcache *, int) override;
const struct btrace_config *btrace_conf (const struct btrace_target_info *) override;
bool augmented_libraries_svr4_read () override;
- int follow_fork (int, int) override;
+ bool follow_fork (bool, bool) override;
void follow_exec (struct inferior *, const char *) override;
int insert_fork_catchpoint (int) override;
int remove_fork_catchpoint (int) override;
void add_current_inferior_and_thread (char *wait_status);
ptid_t wait_ns (ptid_t ptid, struct target_waitstatus *status,
- int options);
+ target_wait_flags options);
ptid_t wait_as (ptid_t ptid, target_waitstatus *status,
- int options);
+ target_wait_flags options);
ptid_t process_stop_reply (struct stop_reply *stop_reply,
target_waitstatus *status);
thread_change_ptid (this, inferior_ptid, currthread);
else
{
- remote_add_thread (currthread, running, executing);
- inferior_ptid = currthread;
+ thread_info *thr
+ = remote_add_thread (currthread, running, executing);
+ switch_to_thread (thr);
}
return;
}
putpkt (pass_packet);
getpkt (&rs->buf, 0);
packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
- if (rs->last_pass_packet)
- xfree (rs->last_pass_packet);
+ xfree (rs->last_pass_packet);
rs->last_pass_packet = pass_packet;
}
else
return 0;
}
+/* Return true if INF only has one non-exited thread. */
+
+static bool
+has_single_non_exited_thread (inferior *inf)
+{
+ int count = 0;
+ for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
+ if (++count > 1)
+ break;
+ return count == 1;
+}
+
/* Implement the to_update_thread_list function for the remote
targets. */
if (!context.contains_thread (tp->ptid))
{
+ /* Do not remove the thread if it is the last thread in
+ the inferior. This situation happens when we have a
+ pending exit process status to process. Otherwise we
+ may end up with a seemingly live inferior (i.e. pid
+ != 0) that has no threads. */
+ if (has_single_non_exited_thread (tp->inf))
+ continue;
+
/* Not found. */
delete_thread (tp);
}
char *ptr;
int lose, num_segments = 0, do_sections, do_segments;
CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
- struct symfile_segment_data *data;
if (symfile_objfile == NULL)
return;
section_offsets offs = symfile_objfile->section_offsets;
- data = get_symfile_segment_data (symfile_objfile->obfd);
+ symfile_segment_data_up data
+ = get_symfile_segment_data (symfile_objfile->obfd);
do_segments = (data != NULL);
do_sections = num_segments == 0;
by assuming that the .text and .data offsets apply to the whole
text and data segments. Convert the offsets given in the packet
to base addresses for symfile_map_offsets_to_segments. */
- else if (data && data->num_segments == 2)
+ else if (data != nullptr && data->segments.size () == 2)
{
- segments[0] = data->segment_bases[0] + text_addr;
- segments[1] = data->segment_bases[1] + data_addr;
+ segments[0] = data->segments[0].base + text_addr;
+ segments[1] = data->segments[1].base + data_addr;
num_segments = 2;
}
/* If the object file has only one segment, assume that it is text
but programs with no code are useless. Of course the code might
have ended up in the data segment... to detect that we would need
the permissions here. */
- else if (data && data->num_segments == 1)
+ else if (data && data->segments.size () == 1)
{
- segments[0] = data->segment_bases[0] + text_addr;
+ segments[0] = data->segments[0].base + text_addr;
num_segments = 1;
}
/* There's no way to relocate by segment. */
if (do_segments)
{
- int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
- offs, num_segments, segments);
+ int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd,
+ data.get (), offs,
+ num_segments, segments);
if (ret == 0 && !do_sections)
error (_("Can not handle qOffsets TextSeg "
do_sections = 0;
}
- if (data)
- free_symfile_segment_data (data);
-
if (do_sections)
{
offs[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
struct remote_state *rs = get_remote_state ();
bool fake_pid_p = false;
- inferior_ptid = null_ptid;
+ switch_to_no_thread ();
- /* Now, if we have thread information, update inferior_ptid. */
+ /* Now, if we have thread information, update the current thread's
+ ptid. */
ptid_t curr_ptid = get_current_thread (wait_status);
if (curr_ptid != null_ptid)
}
/* If we connected to a live target, do some additional setup. */
- if (target_has_execution)
+ if (target_has_execution ())
{
if (symfile_objfile) /* No use without a symbol-file. */
remote_check_symbols ();
but our current inferior is not running, we should not invite the
remote target to request symbol lookups related to its
(unrelated) current process. */
- if (!target_has_execution)
+ if (!target_has_execution ())
return;
if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
/* If we're connected to a running target, target_preopen will kill it.
Ask this question first, before target_preopen has a chance to kill
anything. */
- if (curr_remote != NULL && !target_has_execution)
+ if (curr_remote != NULL && !target_has_execution ())
{
if (from_tty
&& !query (_("Already connected to a remote target. Disconnect? ")))
/* Register extra event sources in the event loop. */
rs->remote_async_inferior_event_token
- = create_async_event_handler (remote_async_inferior_event_handler,
- remote);
+ = create_async_event_handler (remote_async_inferior_event_handler, remote,
+ "remote");
rs->notif_state = remote_notif_state_allocate (remote);
/* Reset the target state; these things will be queried either by
struct remote_state *rs = get_remote_state ();
int is_fork_parent;
- if (!target_has_execution)
+ if (!target_has_execution ())
error (_("No process to detach from."));
target_announce_detach (from_tty);
}
else
{
- inferior_ptid = null_ptid;
+ switch_to_no_thread ();
detach_inferior (current_inferior ());
}
}
it is named remote_follow_fork in anticipation of using it for the
remote target as well. */
-int
-remote_target::follow_fork (int follow_child, int detach_fork)
+bool
+remote_target::follow_fork (bool follow_child, bool detach_fork)
{
struct remote_state *rs = get_remote_state ();
enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
remote_detach_pid (child_pid);
}
}
- return 0;
+
+ return false;
}
/* Target follow-exec function for remote targets. Save EXECD_PATHNAME
target_pid_to_str (ptid_t (pid)).c_str ());
}
- set_current_inferior (remote_add_inferior (false, pid, 1, 0));
+ switch_to_inferior_no_thread (remote_add_inferior (false, pid, 1, 0));
inferior_ptid = ptid_t (pid);
if (target_is_non_stop_p ())
{
- struct thread_info *thread;
-
/* Get list of threads. */
update_thread_list ();
- thread = first_thread_of_inferior (current_inferior ());
- if (thread)
- inferior_ptid = thread->ptid;
- else
- inferior_ptid = ptid_t (pid);
+ thread_info *thread = first_thread_of_inferior (current_inferior ());
+ if (thread != nullptr)
+ switch_to_thread (thread);
/* Invalidate our notion of the remote current thread. */
record_currthread (rs, minus_one_ptid);
}
else
{
- /* Now, if we have thread information, update inferior_ptid. */
- inferior_ptid = remote_current_thread (inferior_ptid);
+ /* Now, if we have thread information, update the main thread's
+ ptid. */
+ ptid_t curr_ptid = remote_current_thread (ptid_t (pid));
/* Add the main thread to the thread list. */
- thread_info *thr = add_thread_silent (this, inferior_ptid);
+ thread_info *thr = add_thread_silent (this, curr_ptid);
+
+ switch_to_thread (thr);
+
/* Don't consider the thread stopped until we've processed the
saved stop reply. */
set_executing (this, thr->ptid, true);
tb[0] = c;
tb[1] = 0;
- fputs_unfiltered (tb, gdb_stdtarg);
+ gdb_stdtarg->puts (tb);
}
- gdb_flush (gdb_stdtarg);
+ gdb_stdtarg->flush ();
}
struct stop_reply : public notif_event
reported expedited registers. */
if (event->ptid == null_ptid)
{
+ /* If there is no thread-id information then leave
+ the event->ptid as null_ptid. Later in
+ process_stop_reply we will pick a suitable
+ thread. */
const char *thr = strstr (p1 + 1, ";thread:");
if (thr != NULL)
event->ptid = read_ptid (thr + strlen (";thread:"),
NULL);
- else
- {
- /* Either the current thread hasn't changed,
- or the inferior is not multi-threaded.
- The event must be for the thread we last
- set as (or learned as being) current. */
- event->ptid = event->rs->general_thread;
- }
}
if (rsa == NULL)
event->ptid = minus_one_ptid;
break;
}
-
- if (target_is_non_stop_p () && event->ptid == null_ptid)
- error (_("No process or thread specified in stop reply: %s"), buf);
}
/* When the stub wants to tell GDB about a new notification reply, it
*status = stop_reply->ws;
ptid = stop_reply->ptid;
- /* If no thread/process was reported by the stub, assume the current
- inferior. */
+ /* If no thread/process was reported by the stub then use the first
+ non-exited thread in the current target. */
if (ptid == null_ptid)
- ptid = inferior_ptid;
+ {
+ /* Some stop events apply to all threads in an inferior, while others
+ only apply to a single thread. */
+ bool is_stop_for_all_threads
+ = (status->kind == TARGET_WAITKIND_EXITED
+ || status->kind == TARGET_WAITKIND_SIGNALLED);
+
+ for (thread_info *thr : all_non_exited_threads (this))
+ {
+ if (ptid != null_ptid
+ && (!is_stop_for_all_threads
+ || ptid.pid () != thr->ptid.pid ()))
+ {
+ static bool warned = false;
+
+ if (!warned)
+ {
+ /* If you are seeing this warning then the remote target
+ has stopped without specifying a thread-id, but the
+ target does have multiple threads (or inferiors), and
+ so GDB is having to guess which thread stopped.
+
+ Examples of what might cause this are the target
+ sending and 'S' stop packet, or a 'T' stop packet and
+ not including a thread-id.
+
+ Additionally, the target might send a 'W' or 'X
+ packet without including a process-id, when the target
+ has multiple running inferiors. */
+ if (is_stop_for_all_threads)
+ warning (_("multi-inferior target stopped without "
+ "sending a process-id, using first "
+ "non-exited inferior"));
+ else
+ warning (_("multi-threaded target stopped without "
+ "sending a thread-id, using first "
+ "non-exited thread"));
+ warned = true;
+ }
+ break;
+ }
+
+ /* If this is a stop for all threads then don't use a particular
+ threads ptid, instead create a new ptid where only the pid
+ field is set. */
+ if (is_stop_for_all_threads)
+ ptid = ptid_t (thr->ptid.pid ());
+ else
+ ptid = thr->ptid;
+ }
+ gdb_assert (ptid != null_ptid);
+ }
if (status->kind != TARGET_WAITKIND_EXITED
&& status->kind != TARGET_WAITKIND_SIGNALLED
/* The non-stop mode version of target_wait. */
ptid_t
-remote_target::wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
+remote_target::wait_ns (ptid_t ptid, struct target_waitstatus *status,
+ target_wait_flags options)
{
struct remote_state *rs = get_remote_state ();
struct stop_reply *stop_reply;
STATUS just as `wait' would. */
ptid_t
-remote_target::wait_as (ptid_t ptid, target_waitstatus *status, int options)
+remote_target::wait_as (ptid_t ptid, target_waitstatus *status,
+ target_wait_flags options)
{
struct remote_state *rs = get_remote_state ();
ptid_t event_ptid = null_ptid;
STATUS just as `wait' would. */
ptid_t
-remote_target::wait (ptid_t ptid, struct target_waitstatus *status, int options)
+remote_target::wait (ptid_t ptid, struct target_waitstatus *status,
+ target_wait_flags options)
{
ptid_t event_ptid;
ULONGEST *xfered_len)
{
struct target_section *secp;
- struct target_section_table *table;
secp = target_section_by_addr (this, memaddr);
if (secp != NULL
&& (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY))
{
- struct target_section *p;
ULONGEST memend = memaddr + len;
- table = target_get_section_table (this);
-
- for (p = table->sections; p < table->sections_end; p++)
+ target_section_table *table = target_get_section_table (this);
+ for (target_section &p : *table)
{
- if (memaddr >= p->addr)
+ if (memaddr >= p.addr)
{
- if (memend <= p->endaddr)
+ if (memend <= p.endaddr)
{
/* Entire transfer is within this section. */
return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
xfered_len);
}
- else if (memaddr >= p->endaddr)
+ else if (memaddr >= p.endaddr)
{
/* This section ends before the transfer starts. */
continue;
else
{
/* This section overlaps the transfer. Just do half. */
- len = p->endaddr - memaddr;
+ len = p.endaddr - memaddr;
return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
xfered_len);
}
/* It doesn't make sense to use qCRC if the remote target is
connected but not running. */
- if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
+ if (target_has_execution ()
+ && packet_support (PACKET_qCRC) != PACKET_DISABLE)
{
enum packet_result result;
/* If the remote target is connected but not running, we should
pass this request down to a lower stratum (e.g. the executable
file). */
- if (!target_has_execution)
+ if (!target_has_execution ())
return TARGET_XFER_EOF;
if (writebuf != NULL)
int found;
ULONGEST found_addr;
+ auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len)
+ {
+ return (target_read (this, TARGET_OBJECT_MEMORY, NULL, result, addr, len)
+ == len);
+ };
+
/* Don't go to the target if we don't have to. This is done before
checking packet_config_support to avoid the possibility that a
success for this edge case means the facility works in
{
/* Target doesn't provided special support, fall back and use the
standard support (copy memory and do the search here). */
- return simple_search_memory (this, start_addr, search_space_len,
+ return simple_search_memory (read_memory, start_addr, search_space_len,
pattern, pattern_len, found_addrp);
}
supported. If so, fall back to the simple way. */
if (packet_config_support (packet) == PACKET_DISABLE)
{
- return simple_search_memory (this, start_addr, search_space_len,
+ return simple_search_memory (read_memory, start_addr, search_space_len,
pattern, pattern_len, found_addrp);
}
return -1;
/* Do not try this during initial connection, when we do not know
whether there is a running but stopped thread. */
- if (!target_has_execution || inferior_ptid == null_ptid)
+ if (!target_has_execution () || inferior_ptid == null_ptid)
return beneath ()->read_description ();
if (!data->guesses.empty ())
remote_file_delete (argv[0], from_tty);
}
-static void
-remote_command (const char *args, int from_tty)
-{
- help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
-}
-
bool
remote_target::can_execute_reverse ()
{
encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
tpaddr = loc->address;
- sprintf_vma (addrbuf, tpaddr);
+ strcpy (addrbuf, phex (tpaddr, sizeof (CORE_ADDR)));
ret = snprintf (buf.data (), buf.size (), "QTDP:%x:%s:%c:%lx:%x",
b->number, addrbuf, /* address */
(b->enable_state == bp_enabled ? 'E' : 'D'),
remote_target::enable_tracepoint (struct bp_location *location)
{
struct remote_state *rs = get_remote_state ();
- char addr_buf[40];
- sprintf_vma (addr_buf, location->address);
xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTEnable:%x:%s",
- location->owner->number, addr_buf);
+ location->owner->number,
+ phex (location->address, sizeof (CORE_ADDR)));
putpkt (rs->buf);
remote_get_noisy_reply ();
if (rs->buf[0] == '\0')
remote_target::disable_tracepoint (struct bp_location *location)
{
struct remote_state *rs = get_remote_state ();
- char addr_buf[40];
- sprintf_vma (addr_buf, location->address);
xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTDisable:%x:%s",
- location->owner->number, addr_buf);
+ location->owner->number,
+ phex (location->address, sizeof (CORE_ADDR)));
putpkt (rs->buf);
remote_get_noisy_reply ();
if (rs->buf[0] == '\0')
/* If we're not debugging a process yet, the IPA can't be
loaded. */
- if (!target_has_execution)
+ if (!target_has_execution ())
return 0;
/* Make sure the remote is pointing at the right process. */
{
/* Don't propogate error information up to the client. Instead let
the client find out about the error by querying the target. */
- inferior_event_handler (INF_REG_EVENT, NULL);
+ inferior_event_handler (INF_REG_EVENT);
}
static void
remote_async_inferior_event_handler (gdb_client_data data)
{
- inferior_event_handler (INF_REG_EVENT, data);
+ inferior_event_handler (INF_REG_EVENT);
+
+ remote_target *remote = (remote_target *) data;
+ remote_state *rs = remote->get_remote_state ();
+
+ /* inferior_event_handler may have consumed an event pending on the
+ infrun side without calling target_wait on the REMOTE target, or
+ may have pulled an event out of a different target. Keep trying
+ for this remote target as long it still has either pending events
+ or unacknowledged notifications. */
+
+ if (rs->notif_state->pending_event[notif_client_stop.id] != NULL
+ || !rs->stop_reply_queue.empty ())
+ mark_async_event_handler (rs->remote_async_inferior_event_token);
}
int
}
}
-static void
-set_remote_cmd (const char *args, int from_tty)
-{
- help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
-}
-
static void
show_remote_cmd (const char *args, int from_tty)
{
/* set/show remote ... */
- add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
+ add_basic_prefix_cmd ("remote", class_maintenance, _("\
Remote protocol specific variables.\n\
Configure various remote-protocol specific variables such as\n\
the packets being used."),
- &remote_set_cmdlist, "set remote ",
- 0 /* allow-unknown */, &setlist);
+ &remote_set_cmdlist, "set remote ",
+ 0 /* allow-unknown */, &setlist);
add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
Remote protocol specific variables.\n\
Configure various remote-protocol specific variables such as\n\
set_remotebreak, show_remotebreak,
&setlist, &showlist);
cmd_name = "remotebreak";
- cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
+ cmd = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1);
deprecate_cmd (cmd, "set remote interrupt-sequence");
cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
- cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
+ cmd = lookup_cmd (&cmd_name, showlist, "", NULL, -1, 1);
deprecate_cmd (cmd, "show remote interrupt-sequence");
add_setshow_enum_cmd ("interrupt-sequence", class_support,
`Z' packets is %s. */
&remote_set_cmdlist, &remote_show_cmdlist);
- add_prefix_cmd ("remote", class_files, remote_command, _("\
+ add_basic_prefix_cmd ("remote", class_files, _("\
Manipulate files on the remote system.\n\
Transfer files to and from the remote target system."),
- &remote_cmdlist, "remote ",
- 0 /* allow-unknown */, &cmdlist);
+ &remote_cmdlist, "remote ",
+ 0 /* allow-unknown */, &cmdlist);
add_cmd ("put", class_files, remote_put_command,
_("Copy a local file to the remote system."),
&setdebuglist, &showdebuglist);
/* Eventually initialize fileio. See fileio.c */
- initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
+ initialize_remote_fileio (&remote_set_cmdlist, &remote_show_cmdlist);
}