binutils-gdb/gdb/ada-tasks.c
Pedro Alves 5b6d1e4fa4 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 20:06:08 +00:00

1473 lines
48 KiB
C

/* Copyright (C) 1992-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 "defs.h"
#include "observable.h"
#include "gdbcmd.h"
#include "target.h"
#include "ada-lang.h"
#include "gdbcore.h"
#include "inferior.h"
#include "gdbthread.h"
#include "progspace.h"
#include "objfiles.h"
#include "cli/cli-style.h"
static int ada_build_task_list ();
/* The name of the array in the GNAT runtime where the Ada Task Control
Block of each task is stored. */
#define KNOWN_TASKS_NAME "system__tasking__debug__known_tasks"
/* The maximum number of tasks known to the Ada runtime. */
static const int MAX_NUMBER_OF_KNOWN_TASKS = 1000;
/* The name of the variable in the GNAT runtime where the head of a task
chain is saved. This is an alternate mechanism to find the list of known
tasks. */
#define KNOWN_TASKS_LIST "system__tasking__debug__first_task"
enum task_states
{
Unactivated,
Runnable,
Terminated,
Activator_Sleep,
Acceptor_Sleep,
Entry_Caller_Sleep,
Async_Select_Sleep,
Delay_Sleep,
Master_Completion_Sleep,
Master_Phase_2_Sleep,
Interrupt_Server_Idle_Sleep,
Interrupt_Server_Blocked_Interrupt_Sleep,
Timer_Server_Sleep,
AST_Server_Sleep,
Asynchronous_Hold,
Interrupt_Server_Blocked_On_Event_Flag,
Activating,
Acceptor_Delay_Sleep
};
/* A short description corresponding to each possible task state. */
static const char *task_states[] = {
N_("Unactivated"),
N_("Runnable"),
N_("Terminated"),
N_("Child Activation Wait"),
N_("Accept or Select Term"),
N_("Waiting on entry call"),
N_("Async Select Wait"),
N_("Delay Sleep"),
N_("Child Termination Wait"),
N_("Wait Child in Term Alt"),
"",
"",
"",
"",
N_("Asynchronous Hold"),
"",
N_("Activating"),
N_("Selective Wait")
};
/* A longer description corresponding to each possible task state. */
static const char *long_task_states[] = {
N_("Unactivated"),
N_("Runnable"),
N_("Terminated"),
N_("Waiting for child activation"),
N_("Blocked in accept or select with terminate"),
N_("Waiting on entry call"),
N_("Asynchronous Selective Wait"),
N_("Delay Sleep"),
N_("Waiting for children termination"),
N_("Waiting for children in terminate alternative"),
"",
"",
"",
"",
N_("Asynchronous Hold"),
"",
N_("Activating"),
N_("Blocked in selective wait statement")
};
/* The index of certain important fields in the Ada Task Control Block
record and sub-records. */
struct atcb_fieldnos
{
/* Fields in record Ada_Task_Control_Block. */
int common;
int entry_calls;
int atc_nesting_level;
/* Fields in record Common_ATCB. */
int state;
int parent;
int priority;
int image;
int image_len; /* This field may be missing. */
int activation_link;
int call;
int ll;
int base_cpu;
/* Fields in Task_Primitives.Private_Data. */
int ll_thread;
int ll_lwp; /* This field may be missing. */
/* Fields in Common_ATCB.Call.all. */
int call_self;
};
/* This module's per-program-space data. */
struct ada_tasks_pspace_data
{
/* Nonzero if the data has been initialized. If set to zero,
it means that the data has either not been initialized, or
has potentially become stale. */
int initialized_p = 0;
/* The ATCB record type. */
struct type *atcb_type = nullptr;
/* The ATCB "Common" component type. */
struct type *atcb_common_type = nullptr;
/* The type of the "ll" field, from the atcb_common_type. */
struct type *atcb_ll_type = nullptr;
/* The type of the "call" field, from the atcb_common_type. */
struct type *atcb_call_type = nullptr;
/* The index of various fields in the ATCB record and sub-records. */
struct atcb_fieldnos atcb_fieldno {};
};
/* Key to our per-program-space data. */
static const struct program_space_key<ada_tasks_pspace_data>
ada_tasks_pspace_data_handle;
/* The kind of data structure used by the runtime to store the list
of Ada tasks. */
enum ada_known_tasks_kind
{
/* Use this value when we haven't determined which kind of structure
is being used, or when we need to recompute it.
We set the value of this enumerate to zero on purpose: This allows
us to use this enumerate in a structure where setting all fields
to zero will result in this kind being set to unknown. */
ADA_TASKS_UNKNOWN = 0,
/* This value means that we did not find any task list. Unless
there is a bug somewhere, this means that the inferior does not
use tasking. */
ADA_TASKS_NOT_FOUND,
/* This value means that the task list is stored as an array.
This is the usual method, as it causes very little overhead.
But this method is not always used, as it does use a certain
amount of memory, which might be scarse in certain environments. */
ADA_TASKS_ARRAY,
/* This value means that the task list is stored as a linked list.
This has more runtime overhead than the array approach, but
also require less memory when the number of tasks is small. */
ADA_TASKS_LIST,
};
/* This module's per-inferior data. */
struct ada_tasks_inferior_data
{
/* The type of data structure used by the runtime to store
the list of Ada tasks. The value of this field influences
the interpretation of the known_tasks_addr field below:
- ADA_TASKS_UNKNOWN: The value of known_tasks_addr hasn't
been determined yet;
- ADA_TASKS_NOT_FOUND: The program probably does not use tasking
and the known_tasks_addr is irrelevant;
- ADA_TASKS_ARRAY: The known_tasks is an array;
- ADA_TASKS_LIST: The known_tasks is a list. */
enum ada_known_tasks_kind known_tasks_kind = ADA_TASKS_UNKNOWN;
/* The address of the known_tasks structure. This is where
the runtime stores the information for all Ada tasks.
The interpretation of this field depends on KNOWN_TASKS_KIND
above. */
CORE_ADDR known_tasks_addr = 0;
/* Type of elements of the known task. Usually a pointer. */
struct type *known_tasks_element = nullptr;
/* Number of elements in the known tasks array. */
unsigned int known_tasks_length = 0;
/* When nonzero, this flag indicates that the task_list field
below is up to date. When set to zero, the list has either
not been initialized, or has potentially become stale. */
bool task_list_valid_p = false;
/* The list of Ada tasks.
Note: To each task we associate a number that the user can use to
reference it - this number is printed beside each task in the tasks
info listing displayed by "info tasks". This number is equal to
its index in the vector + 1. Reciprocally, to compute the index
of a task in the vector, we need to substract 1 from its number. */
std::vector<ada_task_info> task_list;
};
/* Key to our per-inferior data. */
static const struct inferior_key<ada_tasks_inferior_data>
ada_tasks_inferior_data_handle;
/* Return a string with TASKNO followed by the task name if TASK_INFO
contains a name. */
static std::string
task_to_str (int taskno, const ada_task_info *task_info)
{
if (task_info->name[0] == '\0')
return string_printf ("%d", taskno);
else
return string_printf ("%d \"%s\"", taskno, task_info->name);
}
/* Return the ada-tasks module's data for the given program space (PSPACE).
If none is found, add a zero'ed one now.
This function always returns a valid object. */
static struct ada_tasks_pspace_data *
get_ada_tasks_pspace_data (struct program_space *pspace)
{
struct ada_tasks_pspace_data *data;
data = ada_tasks_pspace_data_handle.get (pspace);
if (data == NULL)
data = ada_tasks_pspace_data_handle.emplace (pspace);
return data;
}
/* Return the ada-tasks module's data for the given inferior (INF).
If none is found, add a zero'ed one now.
This function always returns a valid object.
Note that we could use an observer of the inferior-created event
to make sure that the ada-tasks per-inferior data always exists.
But we prefered this approach, as it avoids this entirely as long
as the user does not use any of the tasking features. This is
quite possible, particularly in the case where the inferior does
not use tasking. */
static struct ada_tasks_inferior_data *
get_ada_tasks_inferior_data (struct inferior *inf)
{
struct ada_tasks_inferior_data *data;
data = ada_tasks_inferior_data_handle.get (inf);
if (data == NULL)
data = ada_tasks_inferior_data_handle.emplace (inf);
return data;
}
/* Return the task number of the task whose thread is THREAD, or zero
if the task could not be found. */
int
ada_get_task_number (thread_info *thread)
{
struct inferior *inf = thread->inf;
struct ada_tasks_inferior_data *data;
gdb_assert (inf != NULL);
data = get_ada_tasks_inferior_data (inf);
for (int i = 0; i < data->task_list.size (); i++)
if (data->task_list[i].ptid == thread->ptid)
return i + 1;
return 0; /* No matching task found. */
}
/* Return the task number of the task running in inferior INF which
matches TASK_ID , or zero if the task could not be found. */
static int
get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
{
struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
for (int i = 0; i < data->task_list.size (); i++)
{
if (data->task_list[i].task_id == task_id)
return i + 1;
}
/* Task not found. Return 0. */
return 0;
}
/* Return non-zero if TASK_NUM is a valid task number. */
int
valid_task_id (int task_num)
{
struct ada_tasks_inferior_data *data;
ada_build_task_list ();
data = get_ada_tasks_inferior_data (current_inferior ());
return task_num > 0 && task_num <= data->task_list.size ();
}
/* Return non-zero iff the task STATE corresponds to a non-terminated
task state. */
static int
ada_task_is_alive (const struct ada_task_info *task_info)
{
return (task_info->state != Terminated);
}
/* Search through the list of known tasks for the one whose ptid is
PTID, and return it. Return NULL if the task was not found. */
struct ada_task_info *
ada_get_task_info_from_ptid (ptid_t ptid)
{
struct ada_tasks_inferior_data *data;
ada_build_task_list ();
data = get_ada_tasks_inferior_data (current_inferior ());
for (ada_task_info &task : data->task_list)
{
if (task.ptid == ptid)
return &task;
}
return NULL;
}
/* Call the ITERATOR function once for each Ada task that hasn't been
terminated yet. */
void
iterate_over_live_ada_tasks (ada_task_list_iterator_ftype *iterator)
{
struct ada_tasks_inferior_data *data;
ada_build_task_list ();
data = get_ada_tasks_inferior_data (current_inferior ());
for (ada_task_info &task : data->task_list)
{
if (!ada_task_is_alive (&task))
continue;
iterator (&task);
}
}
/* Extract the contents of the value as a string whose length is LENGTH,
and store the result in DEST. */
static void
value_as_string (char *dest, struct value *val, int length)
{
memcpy (dest, value_contents (val), length);
dest[length] = '\0';
}
/* Extract the string image from the fat string corresponding to VAL,
and store it in DEST. If the string length is greater than MAX_LEN,
then truncate the result to the first MAX_LEN characters of the fat
string. */
static void
read_fat_string_value (char *dest, struct value *val, int max_len)
{
struct value *array_val;
struct value *bounds_val;
int len;
/* The following variables are made static to avoid recomputing them
each time this function is called. */
static int initialize_fieldnos = 1;
static int array_fieldno;
static int bounds_fieldno;
static int upper_bound_fieldno;
/* Get the index of the fields that we will need to read in order
to extract the string from the fat string. */
if (initialize_fieldnos)
{
struct type *type = value_type (val);
struct type *bounds_type;
array_fieldno = ada_get_field_index (type, "P_ARRAY", 0);
bounds_fieldno = ada_get_field_index (type, "P_BOUNDS", 0);
bounds_type = TYPE_FIELD_TYPE (type, bounds_fieldno);
if (TYPE_CODE (bounds_type) == TYPE_CODE_PTR)
bounds_type = TYPE_TARGET_TYPE (bounds_type);
if (TYPE_CODE (bounds_type) != TYPE_CODE_STRUCT)
error (_("Unknown task name format. Aborting"));
upper_bound_fieldno = ada_get_field_index (bounds_type, "UB0", 0);
initialize_fieldnos = 0;
}
/* Get the size of the task image by checking the value of the bounds.
The lower bound is always 1, so we only need to read the upper bound. */
bounds_val = value_ind (value_field (val, bounds_fieldno));
len = value_as_long (value_field (bounds_val, upper_bound_fieldno));
/* Make sure that we do not read more than max_len characters... */
if (len > max_len)
len = max_len;
/* Extract LEN characters from the fat string. */
array_val = value_ind (value_field (val, array_fieldno));
read_memory (value_address (array_val), (gdb_byte *) dest, len);
/* Add the NUL character to close the string. */
dest[len] = '\0';
}
/* Get, from the debugging information, the type description of all types
related to the Ada Task Control Block that are needed in order to
read the list of known tasks in the Ada runtime. If all of the info
needed to do so is found, then save that info in the module's per-
program-space data, and return NULL. Otherwise, if any information
cannot be found, leave the per-program-space data untouched, and
return an error message explaining what was missing (that error
message does NOT need to be deallocated). */
const char *
ada_get_tcb_types_info (void)
{
struct type *type;
struct type *common_type;
struct type *ll_type;
struct type *call_type;
struct atcb_fieldnos fieldnos;
struct ada_tasks_pspace_data *pspace_data;
const char *atcb_name = "system__tasking__ada_task_control_block___XVE";
const char *atcb_name_fixed = "system__tasking__ada_task_control_block";
const char *common_atcb_name = "system__tasking__common_atcb";
const char *private_data_name = "system__task_primitives__private_data";
const char *entry_call_record_name = "system__tasking__entry_call_record";
/* ATCB symbols may be found in several compilation units. As we
are only interested in one instance, use standard (literal,
C-like) lookups to get the first match. */
struct symbol *atcb_sym =
lookup_symbol_in_language (atcb_name, NULL, STRUCT_DOMAIN,
language_c, NULL).symbol;
const struct symbol *common_atcb_sym =
lookup_symbol_in_language (common_atcb_name, NULL, STRUCT_DOMAIN,
language_c, NULL).symbol;
const struct symbol *private_data_sym =
lookup_symbol_in_language (private_data_name, NULL, STRUCT_DOMAIN,
language_c, NULL).symbol;
const struct symbol *entry_call_record_sym =
lookup_symbol_in_language (entry_call_record_name, NULL, STRUCT_DOMAIN,
language_c, NULL).symbol;
if (atcb_sym == NULL || atcb_sym->type == NULL)
{
/* In Ravenscar run-time libs, the ATCB does not have a dynamic
size, so the symbol name differs. */
atcb_sym = lookup_symbol_in_language (atcb_name_fixed, NULL,
STRUCT_DOMAIN, language_c,
NULL).symbol;
if (atcb_sym == NULL || atcb_sym->type == NULL)
return _("Cannot find Ada_Task_Control_Block type");
type = atcb_sym->type;
}
else
{
/* Get a static representation of the type record
Ada_Task_Control_Block. */
type = atcb_sym->type;
type = ada_template_to_fixed_record_type_1 (type, NULL, 0, NULL, 0);
}
if (common_atcb_sym == NULL || common_atcb_sym->type == NULL)
return _("Cannot find Common_ATCB type");
if (private_data_sym == NULL || private_data_sym->type == NULL)
return _("Cannot find Private_Data type");
if (entry_call_record_sym == NULL || entry_call_record_sym->type == NULL)
return _("Cannot find Entry_Call_Record type");
/* Get the type for Ada_Task_Control_Block.Common. */
common_type = common_atcb_sym->type;
/* Get the type for Ada_Task_Control_Bloc.Common.Call.LL. */
ll_type = private_data_sym->type;
/* Get the type for Common_ATCB.Call.all. */
call_type = entry_call_record_sym->type;
/* Get the field indices. */
fieldnos.common = ada_get_field_index (type, "common", 0);
fieldnos.entry_calls = ada_get_field_index (type, "entry_calls", 1);
fieldnos.atc_nesting_level =
ada_get_field_index (type, "atc_nesting_level", 1);
fieldnos.state = ada_get_field_index (common_type, "state", 0);
fieldnos.parent = ada_get_field_index (common_type, "parent", 1);
fieldnos.priority = ada_get_field_index (common_type, "base_priority", 0);
fieldnos.image = ada_get_field_index (common_type, "task_image", 1);
fieldnos.image_len = ada_get_field_index (common_type, "task_image_len", 1);
fieldnos.activation_link = ada_get_field_index (common_type,
"activation_link", 1);
fieldnos.call = ada_get_field_index (common_type, "call", 1);
fieldnos.ll = ada_get_field_index (common_type, "ll", 0);
fieldnos.base_cpu = ada_get_field_index (common_type, "base_cpu", 0);
fieldnos.ll_thread = ada_get_field_index (ll_type, "thread", 0);
fieldnos.ll_lwp = ada_get_field_index (ll_type, "lwp", 1);
fieldnos.call_self = ada_get_field_index (call_type, "self", 0);
/* On certain platforms such as x86-windows, the "lwp" field has been
named "thread_id". This field will likely be renamed in the future,
but we need to support both possibilities to avoid an unnecessary
dependency on a recent compiler. We therefore try locating the
"thread_id" field in place of the "lwp" field if we did not find
the latter. */
if (fieldnos.ll_lwp < 0)
fieldnos.ll_lwp = ada_get_field_index (ll_type, "thread_id", 1);
/* Set all the out parameters all at once, now that we are certain
that there are no potential error() anymore. */
pspace_data = get_ada_tasks_pspace_data (current_program_space);
pspace_data->initialized_p = 1;
pspace_data->atcb_type = type;
pspace_data->atcb_common_type = common_type;
pspace_data->atcb_ll_type = ll_type;
pspace_data->atcb_call_type = call_type;
pspace_data->atcb_fieldno = fieldnos;
return NULL;
}
/* Build the PTID of the task from its COMMON_VALUE, which is the "Common"
component of its ATCB record. This PTID needs to match the PTID used
by the thread layer. */
static ptid_t
ptid_from_atcb_common (struct value *common_value)
{
long thread = 0;
CORE_ADDR lwp = 0;
struct value *ll_value;
ptid_t ptid;
const struct ada_tasks_pspace_data *pspace_data
= get_ada_tasks_pspace_data (current_program_space);
ll_value = value_field (common_value, pspace_data->atcb_fieldno.ll);
if (pspace_data->atcb_fieldno.ll_lwp >= 0)
lwp = value_as_address (value_field (ll_value,
pspace_data->atcb_fieldno.ll_lwp));
thread = value_as_long (value_field (ll_value,
pspace_data->atcb_fieldno.ll_thread));
ptid = target_get_ada_task_ptid (lwp, thread);
return ptid;
}
/* Read the ATCB data of a given task given its TASK_ID (which is in practice
the address of its associated ATCB record), and store the result inside
TASK_INFO. */
static void
read_atcb (CORE_ADDR task_id, struct ada_task_info *task_info)
{
struct value *tcb_value;
struct value *common_value;
struct value *atc_nesting_level_value;
struct value *entry_calls_value;
struct value *entry_calls_value_element;
int called_task_fieldno = -1;
static const char ravenscar_task_name[] = "Ravenscar task";
const struct ada_tasks_pspace_data *pspace_data
= get_ada_tasks_pspace_data (current_program_space);
/* Clear the whole structure to start with, so that everything
is always initialized the same. */
memset (task_info, 0, sizeof (struct ada_task_info));
if (!pspace_data->initialized_p)
{
const char *err_msg = ada_get_tcb_types_info ();
if (err_msg != NULL)
error (_("%s. Aborting"), err_msg);
}
tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
NULL, task_id);
common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
/* Fill in the task_id. */
task_info->task_id = task_id;
/* Compute the name of the task.
Depending on the GNAT version used, the task image is either a fat
string, or a thin array of characters. Older versions of GNAT used
to use fat strings, and therefore did not need an extra field in
the ATCB to store the string length. For efficiency reasons, newer
versions of GNAT replaced the fat string by a static buffer, but this
also required the addition of a new field named "Image_Len" containing
the length of the task name. The method used to extract the task name
is selected depending on the existence of this field.
In some run-time libs (e.g. Ravenscar), the name is not in the ATCB;
we may want to get it from the first user frame of the stack. For now,
we just give a dummy name. */
if (pspace_data->atcb_fieldno.image_len == -1)
{
if (pspace_data->atcb_fieldno.image >= 0)
read_fat_string_value (task_info->name,
value_field (common_value,
pspace_data->atcb_fieldno.image),
sizeof (task_info->name) - 1);
else
{
struct bound_minimal_symbol msym;
msym = lookup_minimal_symbol_by_pc (task_id);
if (msym.minsym)
{
const char *full_name = msym.minsym->linkage_name ();
const char *task_name = full_name;
const char *p;
/* Strip the prefix. */
for (p = full_name; *p; p++)
if (p[0] == '_' && p[1] == '_')
task_name = p + 2;
/* Copy the task name. */
strncpy (task_info->name, task_name, sizeof (task_info->name));
task_info->name[sizeof (task_info->name) - 1] = 0;
}
else
{
/* No symbol found. Use a default name. */
strcpy (task_info->name, ravenscar_task_name);
}
}
}
else
{
int len = value_as_long
(value_field (common_value,
pspace_data->atcb_fieldno.image_len));
value_as_string (task_info->name,
value_field (common_value,
pspace_data->atcb_fieldno.image),
len);
}
/* Compute the task state and priority. */
task_info->state =
value_as_long (value_field (common_value,
pspace_data->atcb_fieldno.state));
task_info->priority =
value_as_long (value_field (common_value,
pspace_data->atcb_fieldno.priority));
/* If the ATCB contains some information about the parent task,
then compute it as well. Otherwise, zero. */
if (pspace_data->atcb_fieldno.parent >= 0)
task_info->parent =
value_as_address (value_field (common_value,
pspace_data->atcb_fieldno.parent));
/* If the task is in an entry call waiting for another task,
then determine which task it is. */
if (task_info->state == Entry_Caller_Sleep
&& pspace_data->atcb_fieldno.atc_nesting_level > 0
&& pspace_data->atcb_fieldno.entry_calls > 0)
{
/* Let My_ATCB be the Ada task control block of a task calling the
entry of another task; then the Task_Id of the called task is
in My_ATCB.Entry_Calls (My_ATCB.ATC_Nesting_Level).Called_Task. */
atc_nesting_level_value =
value_field (tcb_value, pspace_data->atcb_fieldno.atc_nesting_level);
entry_calls_value =
ada_coerce_to_simple_array_ptr
(value_field (tcb_value, pspace_data->atcb_fieldno.entry_calls));
entry_calls_value_element =
value_subscript (entry_calls_value,
value_as_long (atc_nesting_level_value));
called_task_fieldno =
ada_get_field_index (value_type (entry_calls_value_element),
"called_task", 0);
task_info->called_task =
value_as_address (value_field (entry_calls_value_element,
called_task_fieldno));
}
/* If the ATCB contains some information about RV callers, then
compute the "caller_task". Otherwise, leave it as zero. */
if (pspace_data->atcb_fieldno.call >= 0)
{
/* Get the ID of the caller task from Common_ATCB.Call.all.Self.
If Common_ATCB.Call is null, then there is no caller. */
const CORE_ADDR call =
value_as_address (value_field (common_value,
pspace_data->atcb_fieldno.call));
struct value *call_val;
if (call != 0)
{
call_val =
value_from_contents_and_address (pspace_data->atcb_call_type,
NULL, call);
task_info->caller_task =
value_as_address
(value_field (call_val, pspace_data->atcb_fieldno.call_self));
}
}
task_info->base_cpu
= value_as_long (value_field (common_value,
pspace_data->atcb_fieldno.base_cpu));
/* And finally, compute the task ptid. Note that there is not point
in computing it if the task is no longer alive, in which case
it is good enough to set its ptid to the null_ptid. */
if (ada_task_is_alive (task_info))
task_info->ptid = ptid_from_atcb_common (common_value);
else
task_info->ptid = null_ptid;
}
/* Read the ATCB info of the given task (identified by TASK_ID), and
add the result to the given inferior's TASK_LIST. */
static void
add_ada_task (CORE_ADDR task_id, struct inferior *inf)
{
struct ada_task_info task_info;
struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
read_atcb (task_id, &task_info);
data->task_list.push_back (task_info);
}
/* Read the Known_Tasks array from the inferior memory, and store
it in the current inferior's TASK_LIST. Return true upon success. */
static bool
read_known_tasks_array (struct ada_tasks_inferior_data *data)
{
const int target_ptr_byte = TYPE_LENGTH (data->known_tasks_element);
const int known_tasks_size = target_ptr_byte * data->known_tasks_length;
gdb_byte *known_tasks = (gdb_byte *) alloca (known_tasks_size);
int i;
/* Build a new list by reading the ATCBs from the Known_Tasks array
in the Ada runtime. */
read_memory (data->known_tasks_addr, known_tasks, known_tasks_size);
for (i = 0; i < data->known_tasks_length; i++)
{
CORE_ADDR task_id =
extract_typed_address (known_tasks + i * target_ptr_byte,
data->known_tasks_element);
if (task_id != 0)
add_ada_task (task_id, current_inferior ());
}
return true;
}
/* Read the known tasks from the inferior memory, and store it in
the current inferior's TASK_LIST. Return true upon success. */
static bool
read_known_tasks_list (struct ada_tasks_inferior_data *data)
{
const int target_ptr_byte = TYPE_LENGTH (data->known_tasks_element);
gdb_byte *known_tasks = (gdb_byte *) alloca (target_ptr_byte);
CORE_ADDR task_id;
const struct ada_tasks_pspace_data *pspace_data
= get_ada_tasks_pspace_data (current_program_space);
/* Sanity check. */
if (pspace_data->atcb_fieldno.activation_link < 0)
return false;
/* Build a new list by reading the ATCBs. Read head of the list. */
read_memory (data->known_tasks_addr, known_tasks, target_ptr_byte);
task_id = extract_typed_address (known_tasks, data->known_tasks_element);
while (task_id != 0)
{
struct value *tcb_value;
struct value *common_value;
add_ada_task (task_id, current_inferior ());
/* Read the chain. */
tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
NULL, task_id);
common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
task_id = value_as_address
(value_field (common_value,
pspace_data->atcb_fieldno.activation_link));
}
return true;
}
/* Set all fields of the current inferior ada-tasks data pointed by DATA.
Do nothing if those fields are already set and still up to date. */
static void
ada_tasks_inferior_data_sniffer (struct ada_tasks_inferior_data *data)
{
struct bound_minimal_symbol msym;
struct symbol *sym;
/* Return now if already set. */
if (data->known_tasks_kind != ADA_TASKS_UNKNOWN)
return;
/* Try array. */
msym = lookup_minimal_symbol (KNOWN_TASKS_NAME, NULL, NULL);
if (msym.minsym != NULL)
{
data->known_tasks_kind = ADA_TASKS_ARRAY;
data->known_tasks_addr = BMSYMBOL_VALUE_ADDRESS (msym);
/* Try to get pointer type and array length from the symtab. */
sym = lookup_symbol_in_language (KNOWN_TASKS_NAME, NULL, VAR_DOMAIN,
language_c, NULL).symbol;
if (sym != NULL)
{
/* Validate. */
struct type *type = check_typedef (SYMBOL_TYPE (sym));
struct type *eltype = NULL;
struct type *idxtype = NULL;
if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
eltype = check_typedef (TYPE_TARGET_TYPE (type));
if (eltype != NULL
&& TYPE_CODE (eltype) == TYPE_CODE_PTR)
idxtype = check_typedef (TYPE_INDEX_TYPE (type));
if (idxtype != NULL
&& !TYPE_LOW_BOUND_UNDEFINED (idxtype)
&& !TYPE_HIGH_BOUND_UNDEFINED (idxtype))
{
data->known_tasks_element = eltype;
data->known_tasks_length =
TYPE_HIGH_BOUND (idxtype) - TYPE_LOW_BOUND (idxtype) + 1;
return;
}
}
/* Fallback to default values. The runtime may have been stripped (as
in some distributions), but it is likely that the executable still
contains debug information on the task type (due to implicit with of
Ada.Tasking). */
data->known_tasks_element =
builtin_type (target_gdbarch ())->builtin_data_ptr;
data->known_tasks_length = MAX_NUMBER_OF_KNOWN_TASKS;
return;
}
/* Try list. */
msym = lookup_minimal_symbol (KNOWN_TASKS_LIST, NULL, NULL);
if (msym.minsym != NULL)
{
data->known_tasks_kind = ADA_TASKS_LIST;
data->known_tasks_addr = BMSYMBOL_VALUE_ADDRESS (msym);
data->known_tasks_length = 1;
sym = lookup_symbol_in_language (KNOWN_TASKS_LIST, NULL, VAR_DOMAIN,
language_c, NULL).symbol;
if (sym != NULL && SYMBOL_VALUE_ADDRESS (sym) != 0)
{
/* Validate. */
struct type *type = check_typedef (SYMBOL_TYPE (sym));
if (TYPE_CODE (type) == TYPE_CODE_PTR)
{
data->known_tasks_element = type;
return;
}
}
/* Fallback to default values. */
data->known_tasks_element =
builtin_type (target_gdbarch ())->builtin_data_ptr;
data->known_tasks_length = 1;
return;
}
/* Can't find tasks. */
data->known_tasks_kind = ADA_TASKS_NOT_FOUND;
data->known_tasks_addr = 0;
}
/* Read the known tasks from the current inferior's memory, and store it
in the current inferior's data TASK_LIST. */
static void
read_known_tasks ()
{
struct ada_tasks_inferior_data *data =
get_ada_tasks_inferior_data (current_inferior ());
/* Step 1: Clear the current list, if necessary. */
data->task_list.clear ();
/* Step 2: do the real work.
If the application does not use task, then no more needs to be done.
It is important to have the task list cleared (see above) before we
return, as we don't want a stale task list to be used... This can
happen for instance when debugging a non-multitasking program after
having debugged a multitasking one. */
ada_tasks_inferior_data_sniffer (data);
gdb_assert (data->known_tasks_kind != ADA_TASKS_UNKNOWN);
/* Step 3: Set task_list_valid_p, to avoid re-reading the Known_Tasks
array unless needed. */
switch (data->known_tasks_kind)
{
case ADA_TASKS_NOT_FOUND: /* Tasking not in use in inferior. */
break;
case ADA_TASKS_ARRAY:
data->task_list_valid_p = read_known_tasks_array (data);
break;
case ADA_TASKS_LIST:
data->task_list_valid_p = read_known_tasks_list (data);
break;
}
}
/* Build the task_list by reading the Known_Tasks array from
the inferior, and return the number of tasks in that list
(zero means that the program is not using tasking at all). */
static int
ada_build_task_list ()
{
struct ada_tasks_inferior_data *data;
if (!target_has_stack)
error (_("Cannot inspect Ada tasks when program is not running"));
data = get_ada_tasks_inferior_data (current_inferior ());
if (!data->task_list_valid_p)
read_known_tasks ();
return data->task_list.size ();
}
/* Print a table providing a short description of all Ada tasks
running inside inferior INF. If ARG_STR is set, it will be
interpreted as a task number, and the table will be limited to
that task only. */
void
print_ada_task_info (struct ui_out *uiout,
const char *arg_str,
struct inferior *inf)
{
struct ada_tasks_inferior_data *data;
int taskno, nb_tasks;
int taskno_arg = 0;
int nb_columns;
if (ada_build_task_list () == 0)
{
uiout->message (_("Your application does not use any Ada tasks.\n"));
return;
}
if (arg_str != NULL && arg_str[0] != '\0')
taskno_arg = value_as_long (parse_and_eval (arg_str));
if (uiout->is_mi_like_p ())
/* In GDB/MI mode, we want to provide the thread ID corresponding
to each task. This allows clients to quickly find the thread
associated to any task, which is helpful for commands that
take a --thread argument. However, in order to be able to
provide that thread ID, the thread list must be up to date
first. */
target_update_thread_list ();
data = get_ada_tasks_inferior_data (inf);
/* Compute the number of tasks that are going to be displayed
in the output. If an argument was given, there will be
at most 1 entry. Otherwise, there will be as many entries
as we have tasks. */
if (taskno_arg)
{
if (taskno_arg > 0 && taskno_arg <= data->task_list.size ())
nb_tasks = 1;
else
nb_tasks = 0;
}
else
nb_tasks = data->task_list.size ();
nb_columns = uiout->is_mi_like_p () ? 8 : 7;
ui_out_emit_table table_emitter (uiout, nb_columns, nb_tasks, "tasks");
uiout->table_header (1, ui_left, "current", "");
uiout->table_header (3, ui_right, "id", "ID");
{
size_t tid_width = 9;
/* Grown below in case the largest entry is bigger. */
if (!uiout->is_mi_like_p ())
{
for (taskno = 1; taskno <= data->task_list.size (); taskno++)
{
const struct ada_task_info *const task_info
= &data->task_list[taskno - 1];
gdb_assert (task_info != NULL);
tid_width = std::max (tid_width,
1 + strlen (phex_nz (task_info->task_id,
sizeof (CORE_ADDR))));
}
}
uiout->table_header (tid_width, ui_right, "task-id", "TID");
}
/* The following column is provided in GDB/MI mode only because
it is only really useful in that mode, and also because it
allows us to keep the CLI output shorter and more compact. */
if (uiout->is_mi_like_p ())
uiout->table_header (4, ui_right, "thread-id", "");
uiout->table_header (4, ui_right, "parent-id", "P-ID");
uiout->table_header (3, ui_right, "priority", "Pri");
uiout->table_header (22, ui_left, "state", "State");
/* Use ui_noalign for the last column, to prevent the CLI uiout
from printing an extra space at the end of each row. This
is a bit of a hack, but does get the job done. */
uiout->table_header (1, ui_noalign, "name", "Name");
uiout->table_body ();
for (taskno = 1; taskno <= data->task_list.size (); taskno++)
{
const struct ada_task_info *const task_info =
&data->task_list[taskno - 1];
int parent_id;
gdb_assert (task_info != NULL);
/* If the user asked for the output to be restricted
to one task only, and this is not the task, skip
to the next one. */
if (taskno_arg && taskno != taskno_arg)
continue;
ui_out_emit_tuple tuple_emitter (uiout, NULL);
/* Print a star if this task is the current task (or the task
currently selected). */
if (task_info->ptid == inferior_ptid)
uiout->field_string ("current", "*");
else
uiout->field_skip ("current");
/* Print the task number. */
uiout->field_signed ("id", taskno);
/* Print the Task ID. */
uiout->field_string ("task-id", phex_nz (task_info->task_id,
sizeof (CORE_ADDR)));
/* Print the associated Thread ID. */
if (uiout->is_mi_like_p ())
{
thread_info *thread = (ada_task_is_alive (task_info)
? find_thread_ptid (inf, task_info->ptid)
: nullptr);
if (thread != NULL)
uiout->field_signed ("thread-id", thread->global_num);
else
{
/* This can happen if the thread is no longer alive. */
uiout->field_skip ("thread-id");
}
}
/* Print the ID of the parent task. */
parent_id = get_task_number_from_id (task_info->parent, inf);
if (parent_id)
uiout->field_signed ("parent-id", parent_id);
else
uiout->field_skip ("parent-id");
/* Print the base priority of the task. */
uiout->field_signed ("priority", task_info->priority);
/* Print the task current state. */
if (task_info->caller_task)
uiout->field_fmt ("state",
_("Accepting RV with %-4d"),
get_task_number_from_id (task_info->caller_task,
inf));
else if (task_info->called_task)
uiout->field_fmt ("state",
_("Waiting on RV with %-3d"),
get_task_number_from_id (task_info->called_task,
inf));
else
uiout->field_string ("state", task_states[task_info->state]);
/* Finally, print the task name, without quotes around it, as mi like
is not expecting quotes, and in non mi-like no need for quotes
as there is a specific column for the name. */
uiout->field_fmt ("name",
(task_info->name[0] != '\0'
? ui_file_style ()
: metadata_style.style ()),
"%s",
(task_info->name[0] != '\0'
? task_info->name
: _("<no name>")));
uiout->text ("\n");
}
}
/* Print a detailed description of the Ada task whose ID is TASKNO_STR
for the given inferior (INF). */
static void
info_task (struct ui_out *uiout, const char *taskno_str, struct inferior *inf)
{
const int taskno = value_as_long (parse_and_eval (taskno_str));
struct ada_task_info *task_info;
int parent_taskno = 0;
struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
if (ada_build_task_list () == 0)
{
uiout->message (_("Your application does not use any Ada tasks.\n"));
return;
}
if (taskno <= 0 || taskno > data->task_list.size ())
error (_("Task ID %d not known. Use the \"info tasks\" command to\n"
"see the IDs of currently known tasks"), taskno);
task_info = &data->task_list[taskno - 1];
/* Print the Ada task ID. */
printf_filtered (_("Ada Task: %s\n"),
paddress (target_gdbarch (), task_info->task_id));
/* Print the name of the task. */
if (task_info->name[0] != '\0')
printf_filtered (_("Name: %s\n"), task_info->name);
else
fprintf_styled (gdb_stdout, metadata_style.style (), _("<no name>\n"));
/* Print the TID and LWP. */
printf_filtered (_("Thread: %#lx\n"), task_info->ptid.tid ());
printf_filtered (_("LWP: %#lx\n"), task_info->ptid.lwp ());
/* If set, print the base CPU. */
if (task_info->base_cpu != 0)
printf_filtered (_("Base CPU: %d\n"), task_info->base_cpu);
/* Print who is the parent (if any). */
if (task_info->parent != 0)
parent_taskno = get_task_number_from_id (task_info->parent, inf);
if (parent_taskno)
{
struct ada_task_info *parent = &data->task_list[parent_taskno - 1];
printf_filtered (_("Parent: %d"), parent_taskno);
if (parent->name[0] != '\0')
printf_filtered (" (%s)", parent->name);
printf_filtered ("\n");
}
else
printf_filtered (_("No parent\n"));
/* Print the base priority. */
printf_filtered (_("Base Priority: %d\n"), task_info->priority);
/* print the task current state. */
{
int target_taskno = 0;
if (task_info->caller_task)
{
target_taskno = get_task_number_from_id (task_info->caller_task, inf);
printf_filtered (_("State: Accepting rendezvous with %d"),
target_taskno);
}
else if (task_info->called_task)
{
target_taskno = get_task_number_from_id (task_info->called_task, inf);
printf_filtered (_("State: Waiting on task %d's entry"),
target_taskno);
}
else
printf_filtered (_("State: %s"), _(long_task_states[task_info->state]));
if (target_taskno)
{
ada_task_info *target_task_info = &data->task_list[target_taskno - 1];
if (target_task_info->name[0] != '\0')
printf_filtered (" (%s)", target_task_info->name);
}
printf_filtered ("\n");
}
}
/* If ARG is empty or null, then print a list of all Ada tasks.
Otherwise, print detailed information about the task whose ID
is ARG.
Does nothing if the program doesn't use Ada tasking. */
static void
info_tasks_command (const char *arg, int from_tty)
{
struct ui_out *uiout = current_uiout;
if (arg == NULL || *arg == '\0')
print_ada_task_info (uiout, NULL, current_inferior ());
else
info_task (uiout, arg, current_inferior ());
}
/* Print a message telling the user id of the current task.
This function assumes that tasking is in use in the inferior. */
static void
display_current_task_id (void)
{
const int current_task = ada_get_task_number (inferior_thread ());
if (current_task == 0)
printf_filtered (_("[Current task is unknown]\n"));
else
{
struct ada_tasks_inferior_data *data
= get_ada_tasks_inferior_data (current_inferior ());
struct ada_task_info *task_info = &data->task_list[current_task - 1];
printf_filtered (_("[Current task is %s]\n"),
task_to_str (current_task, task_info).c_str ());
}
}
/* Parse and evaluate TIDSTR into a task id, and try to switch to
that task. Print an error message if the task switch failed. */
static void
task_command_1 (const char *taskno_str, int from_tty, struct inferior *inf)
{
const int taskno = value_as_long (parse_and_eval (taskno_str));
struct ada_task_info *task_info;
struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
if (taskno <= 0 || taskno > data->task_list.size ())
error (_("Task ID %d not known. Use the \"info tasks\" command to\n"
"see the IDs of currently known tasks"), taskno);
task_info = &data->task_list[taskno - 1];
if (!ada_task_is_alive (task_info))
error (_("Cannot switch to task %s: Task is no longer running"),
task_to_str (taskno, task_info).c_str ());
/* On some platforms, the thread list is not updated until the user
performs a thread-related operation (by using the "info threads"
command, for instance). So this thread list may not be up to date
when the user attempts this task switch. Since we cannot switch
to the thread associated to our task if GDB does not know about
that thread, we need to make sure that any new threads gets added
to the thread list. */
target_update_thread_list ();
/* Verify that the ptid of the task we want to switch to is valid
(in other words, a ptid that GDB knows about). Otherwise, we will
cause an assertion failure later on, when we try to determine
the ptid associated thread_info data. We should normally never
encounter such an error, but the wrong ptid can actually easily be
computed if target_get_ada_task_ptid has not been implemented for
our target (yet). Rather than cause an assertion error in that case,
it's nicer for the user to just refuse to perform the task switch. */
thread_info *tp = find_thread_ptid (inf, task_info->ptid);
if (tp == NULL)
error (_("Unable to compute thread ID for task %s.\n"
"Cannot switch to this task."),
task_to_str (taskno, task_info).c_str ());
switch_to_thread (tp);
ada_find_printable_frame (get_selected_frame (NULL));
printf_filtered (_("[Switching to task %s]\n"),
task_to_str (taskno, task_info).c_str ());
print_stack_frame (get_selected_frame (NULL),
frame_relative_level (get_selected_frame (NULL)),
SRC_AND_LOC, 1);
}
/* Print the ID of the current task if TASKNO_STR is empty or NULL.
Otherwise, switch to the task indicated by TASKNO_STR. */
static void
task_command (const char *taskno_str, int from_tty)
{
struct ui_out *uiout = current_uiout;
if (ada_build_task_list () == 0)
{
uiout->message (_("Your application does not use any Ada tasks.\n"));
return;
}
if (taskno_str == NULL || taskno_str[0] == '\0')
display_current_task_id ();
else
task_command_1 (taskno_str, from_tty, current_inferior ());
}
/* Indicate that the given inferior's task list may have changed,
so invalidate the cache. */
static void
ada_task_list_changed (struct inferior *inf)
{
struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
data->task_list_valid_p = false;
}
/* Invalidate the per-program-space data. */
static void
ada_tasks_invalidate_pspace_data (struct program_space *pspace)
{
get_ada_tasks_pspace_data (pspace)->initialized_p = 0;
}
/* Invalidate the per-inferior data. */
static void
ada_tasks_invalidate_inferior_data (struct inferior *inf)
{
struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
data->known_tasks_kind = ADA_TASKS_UNKNOWN;
data->task_list_valid_p = false;
}
/* The 'normal_stop' observer notification callback. */
static void
ada_tasks_normal_stop_observer (struct bpstats *unused_args, int unused_args2)
{
/* The inferior has been resumed, and just stopped. This means that
our task_list needs to be recomputed before it can be used again. */
ada_task_list_changed (current_inferior ());
}
/* A routine to be called when the objfiles have changed. */
static void
ada_tasks_new_objfile_observer (struct objfile *objfile)
{
struct inferior *inf;
/* Invalidate the relevant data in our program-space data. */
if (objfile == NULL)
{
/* All objfiles are being cleared, so we should clear all
our caches for all program spaces. */
struct program_space *pspace;
for (pspace = program_spaces; pspace != NULL; pspace = pspace->next)
ada_tasks_invalidate_pspace_data (pspace);
}
else
{
/* The associated program-space data might have changed after
this objfile was added. Invalidate all cached data. */
ada_tasks_invalidate_pspace_data (objfile->pspace);
}
/* Invalidate the per-inferior cache for all inferiors using
this objfile (or, in other words, for all inferiors who have
the same program-space as the objfile's program space).
If all objfiles are being cleared (OBJFILE is NULL), then
clear the caches for all inferiors. */
for (inf = inferior_list; inf != NULL; inf = inf->next)
if (objfile == NULL || inf->pspace == objfile->pspace)
ada_tasks_invalidate_inferior_data (inf);
}
void
_initialize_tasks (void)
{
/* Attach various observers. */
gdb::observers::normal_stop.attach (ada_tasks_normal_stop_observer);
gdb::observers::new_objfile.attach (ada_tasks_new_objfile_observer);
/* Some new commands provided by this module. */
add_info ("tasks", info_tasks_command,
_("Provide information about all known Ada tasks."));
add_cmd ("task", class_run, task_command,
_("Use this command to switch between Ada tasks.\n\
Without argument, this command simply prints the current task ID."),
&cmdlist);
}