Fetch registers from correct thread in ravenscar-thread.c
[deliverable/binutils-gdb.git] / gdb / ravenscar-thread.c
CommitLineData
036b1ba8
JB
1/* Ada Ravenscar thread support.
2
b811d2c2 3 Copyright (C) 2004-2020 Free Software Foundation, Inc.
036b1ba8
JB
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "gdbcore.h"
22#include "gdbthread.h"
23#include "ada-lang.h"
24#include "target.h"
25#include "inferior.h"
26#include "command.h"
27#include "ravenscar-thread.h"
76727919 28#include "observable.h"
036b1ba8
JB
29#include "gdbcmd.h"
30#include "top.h"
31#include "regcache.h"
77e371c0 32#include "objfiles.h"
a8ac85bb 33#include <unordered_map>
036b1ba8 34
9edcc12f
JB
35/* This module provides support for "Ravenscar" tasks (Ada) when
36 debugging on bare-metal targets.
37
38 The typical situation is when debugging a bare-metal target over
39 the remote protocol. In that situation, the system does not know
e397fd39 40 about high-level concepts such as threads, only about some code
9edcc12f
JB
41 running on one or more CPUs. And since the remote protocol does not
42 provide any handling for CPUs, the de facto standard for handling
43 them is to have one thread per CPU, where the thread's ptid has
44 its lwp field set to the CPU number (eg: 1 for the first CPU,
45 2 for the second one, etc). This module will make that assumption.
46
47 This module then creates and maintains the list of threads based
e397fd39 48 on the list of Ada tasks, with one thread per Ada task. The convention
9edcc12f 49 is that threads corresponding to the CPUs (see assumption above)
e397fd39 50 have a ptid_t of the form (PID, LWP, 0), while threads corresponding
9edcc12f
JB
51 to our Ada tasks have a ptid_t of the form (PID, 0, TID) where TID
52 is the Ada task's ID as extracted from Ada runtime information.
53
e397fd39
TT
54 Switching to a given Ada task (or its underlying thread) is performed
55 by fetching the registers of that task from the memory area where
9edcc12f
JB
56 the registers were saved. For any of the other operations, the
57 operation is performed by first finding the CPU on which the task
58 is running, switching to its corresponding ptid, and then performing
59 the operation on that ptid using the target beneath us. */
60
491144b5
CB
61/* If true, ravenscar task support is enabled. */
62static bool ravenscar_task_support = true;
036b1ba8 63
7f39f34a 64static const char running_thread_name[] = "__gnat_running_thread_table";
036b1ba8
JB
65
66static const char known_tasks_name[] = "system__tasking__debug__known_tasks";
6040a59d 67static const char first_task_name[] = "system__tasking__debug__first_task";
036b1ba8 68
6cbcc006
TT
69static const char ravenscar_runtime_initializer[]
70 = "system__bb__threads__initialize";
036b1ba8 71
d9f719f1
PA
72static const target_info ravenscar_target_info = {
73 "ravenscar",
74 N_("Ravenscar tasks."),
75 N_("Ravenscar tasks support.")
76};
77
f6ac5f3d
PA
78struct ravenscar_thread_target final : public target_ops
79{
0b790b1e 80 ravenscar_thread_target ()
2da4b788 81 : m_base_ptid (inferior_ptid)
0b790b1e 82 {
0b790b1e
TT
83 }
84
d9f719f1
PA
85 const target_info &info () const override
86 { return ravenscar_target_info; }
f6ac5f3d 87
66b4deae
PA
88 strata stratum () const override { return thread_stratum; }
89
f6ac5f3d
PA
90 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
91 void resume (ptid_t, int, enum gdb_signal) override;
92
93 void fetch_registers (struct regcache *, int) override;
94 void store_registers (struct regcache *, int) override;
95
96 void prepare_to_store (struct regcache *) override;
97
57810aa7 98 bool stopped_by_sw_breakpoint () override;
f6ac5f3d 99
57810aa7 100 bool stopped_by_hw_breakpoint () override;
f6ac5f3d 101
57810aa7 102 bool stopped_by_watchpoint () override;
f6ac5f3d 103
57810aa7 104 bool stopped_data_address (CORE_ADDR *) override;
f6ac5f3d 105
2080266b
TT
106 enum target_xfer_status xfer_partial (enum target_object object,
107 const char *annex,
108 gdb_byte *readbuf,
109 const gdb_byte *writebuf,
110 ULONGEST offset, ULONGEST len,
111 ULONGEST *xfered_len) override;
112
57810aa7 113 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
114
115 int core_of_thread (ptid_t ptid) override;
116
117 void update_thread_list () override;
118
a068643d 119 std::string pid_to_str (ptid_t) override;
f6ac5f3d
PA
120
121 ptid_t get_ada_task_ptid (long lwp, long thread) override;
122
2080266b
TT
123 struct btrace_target_info *enable_btrace (ptid_t ptid,
124 const struct btrace_config *conf)
125 override
126 {
127 ptid = get_base_thread_from_ravenscar_task (ptid);
128 return beneath ()->enable_btrace (ptid, conf);
129 }
130
f6ac5f3d 131 void mourn_inferior () override;
f6ac5f3d 132
0b790b1e
TT
133 void close () override
134 {
135 delete this;
136 }
137
3d4470e5
TT
138 thread_info *add_active_thread ();
139
0b790b1e 140private:
f6ac5f3d 141
0b790b1e
TT
142 /* PTID of the last thread that received an event.
143 This can be useful to determine the associated task that received
144 the event, to make it the current task. */
2da4b788 145 ptid_t m_base_ptid;
0b790b1e 146
0b790b1e
TT
147 ptid_t active_task (int cpu);
148 bool task_is_currently_active (ptid_t ptid);
149 bool runtime_initialized ();
a8ac85bb
TT
150 int get_thread_base_cpu (ptid_t ptid);
151 ptid_t get_base_thread_from_ravenscar_task (ptid_t ptid);
2080266b 152 void add_thread (struct ada_task_info *task);
a8ac85bb
TT
153
154 /* This maps a TID to the CPU on which it was running. This is
155 needed because sometimes the runtime will report an active task
156 that hasn't yet been put on the list of tasks that is read by
157 ada-tasks.c. */
158 std::unordered_map<long, int> m_cpu_map;
0b790b1e 159};
036b1ba8 160
989f3c58 161/* Return true iff PTID corresponds to a ravenscar task. */
9edcc12f 162
989f3c58 163static bool
9edcc12f
JB
164is_ravenscar_task (ptid_t ptid)
165{
54aa6c67
JB
166 /* By construction, ravenscar tasks have their LWP set to zero.
167 Also make sure that the TID is nonzero, as some remotes, when
168 asked for the list of threads, will return the first thread
169 as having its TID set to zero. For instance, TSIM version
170 2.0.48 for LEON3 sends 'm0' as a reply to the 'qfThreadInfo'
171 query, which the remote protocol layer then treats as a thread
172 whose TID is 0. This is obviously not a ravenscar task. */
cc6bcb54 173 return ptid.lwp () == 0 && ptid.tid () != 0;
9edcc12f
JB
174}
175
176/* Given PTID, which can be either a ravenscar task or a CPU thread,
177 return which CPU that ptid is running on.
178
179 This assume that PTID is a valid ptid_t. Otherwise, a gdb_assert
180 will be triggered. */
181
a8ac85bb
TT
182int
183ravenscar_thread_target::get_thread_base_cpu (ptid_t ptid)
9edcc12f
JB
184{
185 int base_cpu;
186
187 if (is_ravenscar_task (ptid))
188 {
2080266b
TT
189 /* Prefer to not read inferior memory if possible, to avoid
190 reentrancy problems with xfer_partial. */
191 auto iter = m_cpu_map.find (ptid.tid ());
9edcc12f 192
2080266b
TT
193 if (iter != m_cpu_map.end ())
194 base_cpu = iter->second;
a8ac85bb
TT
195 else
196 {
2080266b 197 struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid);
a8ac85bb 198
2080266b
TT
199 gdb_assert (task_info != NULL);
200 base_cpu = task_info->base_cpu;
a8ac85bb 201 }
9edcc12f
JB
202 }
203 else
204 {
205 /* We assume that the LWP of the PTID is equal to the CPU number. */
e38504b3 206 base_cpu = ptid.lwp ();
9edcc12f
JB
207 }
208
209 return base_cpu;
210}
211
989f3c58 212/* Given a ravenscar task (identified by its ptid_t PTID), return true
9edcc12f
JB
213 if this task is the currently active task on the cpu that task is
214 running on.
215
216 In other words, this function determine which CPU this task is
217 currently running on, and then return nonzero if the CPU in question
218 is executing the code for that task. If that's the case, then
219 that task's registers are in the CPU bank. Otherwise, the task
220 is currently suspended, and its registers have been saved in memory. */
221
0b790b1e
TT
222bool
223ravenscar_thread_target::task_is_currently_active (ptid_t ptid)
9edcc12f 224{
a8ac85bb 225 ptid_t active_task_ptid = active_task (get_thread_base_cpu (ptid));
9edcc12f 226
d7e15655 227 return ptid == active_task_ptid;
9edcc12f
JB
228}
229
230/* Return the CPU thread (as a ptid_t) on which the given ravenscar
231 task is running.
232
233 This is the thread that corresponds to the CPU on which the task
234 is running. */
235
a8ac85bb
TT
236ptid_t
237ravenscar_thread_target::get_base_thread_from_ravenscar_task (ptid_t ptid)
9edcc12f
JB
238{
239 int base_cpu;
240
241 if (!is_ravenscar_task (ptid))
242 return ptid;
243
a8ac85bb 244 base_cpu = get_thread_base_cpu (ptid);
e99b03dc 245 return ptid_t (ptid.pid (), base_cpu, 0);
9edcc12f
JB
246}
247
2da4b788
PA
248/* Fetch the ravenscar running thread from target memory, make sure
249 there's a corresponding thread in the thread list, and return it.
250 If the runtime is not initialized, return NULL. */
036b1ba8 251
2da4b788
PA
252thread_info *
253ravenscar_thread_target::add_active_thread ()
036b1ba8 254{
5b6d1e4f
PA
255 process_stratum_target *proc_target
256 = as_process_stratum_target (this->beneath ());
257
9edcc12f
JB
258 int base_cpu;
259
2da4b788 260 gdb_assert (!is_ravenscar_task (m_base_ptid));
a8ac85bb 261 base_cpu = get_thread_base_cpu (m_base_ptid);
9edcc12f 262
0b790b1e 263 if (!runtime_initialized ())
2da4b788 264 return nullptr;
036b1ba8 265
0b790b1e 266 /* Make sure we set m_base_ptid before calling active_task
036b1ba8 267 as the latter relies on it. */
2da4b788
PA
268 ptid_t active_ptid = active_task (base_cpu);
269 gdb_assert (active_ptid != null_ptid);
036b1ba8
JB
270
271 /* The running thread may not have been added to
e8032dde 272 system.tasking.debug's list yet; so ravenscar_update_thread_list
036b1ba8 273 may not always add it to the thread list. Add it here. */
2da4b788
PA
274 thread_info *active_thr = find_thread_ptid (proc_target, active_ptid);
275 if (active_thr == nullptr)
a8ac85bb
TT
276 {
277 active_thr = ::add_thread (proc_target, active_ptid);
278 m_cpu_map[active_ptid.tid ()] = base_cpu;
279 }
2da4b788 280 return active_thr;
036b1ba8
JB
281}
282
7f39f34a
JB
283/* The Ravenscar Runtime exports a symbol which contains the ID of
284 the thread that is currently running. Try to locate that symbol
285 and return its associated minimal symbol.
286 Return NULL if not found. */
287
3b7344d5 288static struct bound_minimal_symbol
989f3c58 289get_running_thread_msymbol ()
7f39f34a 290{
3b7344d5 291 struct bound_minimal_symbol msym;
7f39f34a
JB
292
293 msym = lookup_minimal_symbol (running_thread_name, NULL, NULL);
3b7344d5 294 if (!msym.minsym)
7f39f34a
JB
295 /* Older versions of the GNAT runtime were using a different
296 (less ideal) name for the symbol where the active thread ID
297 is stored. If we couldn't find the symbol using the latest
298 name, then try the old one. */
299 msym = lookup_minimal_symbol ("running_thread", NULL, NULL);
300
301 return msym;
302}
303
036b1ba8
JB
304/* Return True if the Ada Ravenscar run-time can be found in the
305 application. */
306
989f3c58
TT
307static bool
308has_ravenscar_runtime ()
036b1ba8 309{
6cbcc006
TT
310 struct bound_minimal_symbol msym_ravenscar_runtime_initializer
311 = lookup_minimal_symbol (ravenscar_runtime_initializer, NULL, NULL);
312 struct bound_minimal_symbol msym_known_tasks
313 = lookup_minimal_symbol (known_tasks_name, NULL, NULL);
314 struct bound_minimal_symbol msym_first_task
315 = lookup_minimal_symbol (first_task_name, NULL, NULL);
3b7344d5
TT
316 struct bound_minimal_symbol msym_running_thread
317 = get_running_thread_msymbol ();
036b1ba8 318
3b7344d5
TT
319 return (msym_ravenscar_runtime_initializer.minsym
320 && (msym_known_tasks.minsym || msym_first_task.minsym)
321 && msym_running_thread.minsym);
036b1ba8
JB
322}
323
324/* Return True if the Ada Ravenscar run-time can be found in the
325 application, and if it has been initialized on target. */
326
0b790b1e
TT
327bool
328ravenscar_thread_target::runtime_initialized ()
036b1ba8 329{
0b790b1e 330 return active_task (1) != null_ptid;
036b1ba8
JB
331}
332
7f39f34a
JB
333/* Return the ID of the thread that is currently running.
334 Return 0 if the ID could not be determined. */
036b1ba8
JB
335
336static CORE_ADDR
9edcc12f 337get_running_thread_id (int cpu)
036b1ba8 338{
3b7344d5 339 struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
036b1ba8
JB
340 int object_size;
341 int buf_size;
948f8e3d 342 gdb_byte *buf;
036b1ba8 343 CORE_ADDR object_addr;
6cbcc006
TT
344 struct type *builtin_type_void_data_ptr
345 = builtin_type (target_gdbarch ())->builtin_data_ptr;
036b1ba8 346
3b7344d5 347 if (!object_msym.minsym)
036b1ba8
JB
348 return 0;
349
036b1ba8 350 object_size = TYPE_LENGTH (builtin_type_void_data_ptr);
9edcc12f
JB
351 object_addr = (BMSYMBOL_VALUE_ADDRESS (object_msym)
352 + (cpu - 1) * object_size);
036b1ba8 353 buf_size = object_size;
224c3ddb 354 buf = (gdb_byte *) alloca (buf_size);
036b1ba8
JB
355 read_memory (object_addr, buf, buf_size);
356 return extract_typed_address (buf, builtin_type_void_data_ptr);
357}
358
f6ac5f3d 359void
6cbcc006
TT
360ravenscar_thread_target::resume (ptid_t ptid, int step,
361 enum gdb_signal siggnal)
036b1ba8 362{
485b851b
TT
363 /* If we see a wildcard resume, we simply pass that on. Otherwise,
364 arrange to resume the base ptid. */
0b790b1e 365 inferior_ptid = m_base_ptid;
39e2018a
TT
366 if (ptid.is_pid ())
367 {
368 /* We only have one process, so resume all threads of it. */
369 ptid = minus_one_ptid;
370 }
371 else if (ptid != minus_one_ptid)
485b851b
TT
372 ptid = m_base_ptid;
373 beneath ()->resume (ptid, step, siggnal);
036b1ba8
JB
374}
375
f6ac5f3d
PA
376ptid_t
377ravenscar_thread_target::wait (ptid_t ptid,
378 struct target_waitstatus *status,
379 int options)
036b1ba8 380{
5b6d1e4f
PA
381 process_stratum_target *beneath
382 = as_process_stratum_target (this->beneath ());
3b1b69bf 383 ptid_t event_ptid;
036b1ba8 384
485b851b
TT
385 if (ptid != minus_one_ptid)
386 ptid = m_base_ptid;
5b6d1e4f 387 event_ptid = beneath->wait (ptid, status, 0);
2da4b788
PA
388 /* Find any new threads that might have been created, and return the
389 active thread.
bed0c243
JB
390
391 Only do it if the program is still alive, though. Otherwise,
392 this causes problems when debugging through the remote protocol,
393 because we might try switching threads (and thus sending packets)
394 after the remote has disconnected. */
395 if (status->kind != TARGET_WAITKIND_EXITED
e9546579
TT
396 && status->kind != TARGET_WAITKIND_SIGNALLED
397 && runtime_initialized ())
bed0c243 398 {
2da4b788 399 m_base_ptid = event_ptid;
f6ac5f3d 400 this->update_thread_list ();
2da4b788 401 return this->add_active_thread ()->ptid;
bed0c243 402 }
550ab58d 403 return event_ptid;
036b1ba8
JB
404}
405
406/* Add the thread associated to the given TASK to the thread list
407 (if the thread has already been added, this is a no-op). */
408
2080266b
TT
409void
410ravenscar_thread_target::add_thread (struct ada_task_info *task)
036b1ba8 411{
5b6d1e4f 412 if (find_thread_ptid (current_inferior (), task->ptid) == NULL)
2080266b
TT
413 {
414 ::add_thread (current_inferior ()->process_target (), task->ptid);
415 m_cpu_map[task->ptid.tid ()] = task->base_cpu;
416 }
036b1ba8
JB
417}
418
f6ac5f3d
PA
419void
420ravenscar_thread_target::update_thread_list ()
036b1ba8 421{
036b1ba8
JB
422 /* Do not clear the thread list before adding the Ada task, to keep
423 the thread that the process stratum has included into it
0b790b1e 424 (m_base_ptid) and the running thread, that may not have been included
036b1ba8
JB
425 to system.tasking.debug's list yet. */
426
2080266b
TT
427 iterate_over_live_ada_tasks ([=] (struct ada_task_info *task)
428 {
429 this->add_thread (task);
430 });
036b1ba8
JB
431}
432
0b790b1e
TT
433ptid_t
434ravenscar_thread_target::active_task (int cpu)
036b1ba8 435{
9edcc12f 436 CORE_ADDR tid = get_running_thread_id (cpu);
036b1ba8
JB
437
438 if (tid == 0)
439 return null_ptid;
440 else
0b790b1e 441 return ptid_t (m_base_ptid.pid (), 0, tid);
036b1ba8
JB
442}
443
57810aa7 444bool
f6ac5f3d 445ravenscar_thread_target::thread_alive (ptid_t ptid)
036b1ba8
JB
446{
447 /* Ravenscar tasks are non-terminating. */
57810aa7 448 return true;
036b1ba8
JB
449}
450
a068643d 451std::string
f6ac5f3d 452ravenscar_thread_target::pid_to_str (ptid_t ptid)
036b1ba8 453{
d5d833af
TT
454 if (!is_ravenscar_task (ptid))
455 return beneath ()->pid_to_str (ptid);
456
457 return string_printf ("Ravenscar Thread %#x", (int) ptid.tid ());
036b1ba8
JB
458}
459
592f9bd7
TT
460/* Temporarily set the ptid of a regcache to some other value. When
461 this object is destroyed, the regcache's original ptid is
462 restored. */
463
464class temporarily_change_regcache_ptid
465{
466public:
467
468 temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid)
469 : m_regcache (regcache),
470 m_save_ptid (regcache->ptid ())
471 {
472 m_regcache->set_ptid (new_ptid);
473 }
474
475 ~temporarily_change_regcache_ptid ()
476 {
477 m_regcache->set_ptid (m_save_ptid);
478 }
479
480private:
481
482 /* The regcache. */
483 struct regcache *m_regcache;
484 /* The saved ptid. */
485 ptid_t m_save_ptid;
486};
487
f6ac5f3d
PA
488void
489ravenscar_thread_target::fetch_registers (struct regcache *regcache, int regnum)
036b1ba8 490{
222312d3 491 ptid_t ptid = regcache->ptid ();
036b1ba8 492
592f9bd7 493 if (runtime_initialized () && is_ravenscar_task (ptid))
7e35103a 494 {
592f9bd7
TT
495 if (task_is_currently_active (ptid))
496 {
497 ptid_t base = get_base_thread_from_ravenscar_task (ptid);
498 temporarily_change_regcache_ptid changer (regcache, base);
499 beneath ()->fetch_registers (regcache, regnum);
500 }
501 else
502 {
503 struct gdbarch *gdbarch = regcache->arch ();
504 struct ravenscar_arch_ops *arch_ops
505 = gdbarch_ravenscar_ops (gdbarch);
7e35103a 506
592f9bd7
TT
507 arch_ops->fetch_registers (regcache, regnum);
508 }
7e35103a 509 }
9edcc12f 510 else
d6ca69cd 511 beneath ()->fetch_registers (regcache, regnum);
036b1ba8
JB
512}
513
f6ac5f3d
PA
514void
515ravenscar_thread_target::store_registers (struct regcache *regcache,
516 int regnum)
036b1ba8 517{
222312d3 518 ptid_t ptid = regcache->ptid ();
036b1ba8 519
592f9bd7 520 if (runtime_initialized () && is_ravenscar_task (ptid))
7e35103a 521 {
592f9bd7
TT
522 if (task_is_currently_active (ptid))
523 {
524 ptid_t base = get_base_thread_from_ravenscar_task (ptid);
525 temporarily_change_regcache_ptid changer (regcache, base);
526 beneath ()->store_registers (regcache, regnum);
527 }
528 else
529 {
530 struct gdbarch *gdbarch = regcache->arch ();
531 struct ravenscar_arch_ops *arch_ops
532 = gdbarch_ravenscar_ops (gdbarch);
7e35103a 533
592f9bd7
TT
534 arch_ops->store_registers (regcache, regnum);
535 }
7e35103a 536 }
9edcc12f 537 else
d6ca69cd 538 beneath ()->store_registers (regcache, regnum);
036b1ba8
JB
539}
540
f6ac5f3d
PA
541void
542ravenscar_thread_target::prepare_to_store (struct regcache *regcache)
036b1ba8 543{
222312d3 544 ptid_t ptid = regcache->ptid ();
036b1ba8 545
592f9bd7 546 if (runtime_initialized () && is_ravenscar_task (ptid))
7e35103a 547 {
592f9bd7
TT
548 if (task_is_currently_active (ptid))
549 {
550 ptid_t base = get_base_thread_from_ravenscar_task (ptid);
551 temporarily_change_regcache_ptid changer (regcache, base);
552 beneath ()->prepare_to_store (regcache);
553 }
554 else
555 {
556 /* Nothing. */
557 }
7e35103a 558 }
9edcc12f 559 else
d6ca69cd 560 beneath ()->prepare_to_store (regcache);
036b1ba8
JB
561}
562
e02544b2
JB
563/* Implement the to_stopped_by_sw_breakpoint target_ops "method". */
564
57810aa7 565bool
f6ac5f3d 566ravenscar_thread_target::stopped_by_sw_breakpoint ()
e02544b2 567{
5b6ea500
TT
568 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
569 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
570 return beneath ()->stopped_by_sw_breakpoint ();
e02544b2
JB
571}
572
573/* Implement the to_stopped_by_hw_breakpoint target_ops "method". */
574
57810aa7 575bool
f6ac5f3d 576ravenscar_thread_target::stopped_by_hw_breakpoint ()
e02544b2 577{
5b6ea500
TT
578 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
579 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
580 return beneath ()->stopped_by_hw_breakpoint ();
e02544b2
JB
581}
582
583/* Implement the to_stopped_by_watchpoint target_ops "method". */
584
57810aa7 585bool
f6ac5f3d 586ravenscar_thread_target::stopped_by_watchpoint ()
e02544b2 587{
5b6ea500
TT
588 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
589 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
590 return beneath ()->stopped_by_watchpoint ();
e02544b2
JB
591}
592
593/* Implement the to_stopped_data_address target_ops "method". */
594
57810aa7 595bool
f6ac5f3d 596ravenscar_thread_target::stopped_data_address (CORE_ADDR *addr_p)
e02544b2 597{
5b6ea500
TT
598 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
599 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
600 return beneath ()->stopped_data_address (addr_p);
e02544b2
JB
601}
602
f6ac5f3d
PA
603void
604ravenscar_thread_target::mourn_inferior ()
036b1ba8 605{
0b790b1e 606 m_base_ptid = null_ptid;
fd9faca8 607 target_ops *beneath = this->beneath ();
0b790b1e 608 unpush_target (this);
fd9faca8 609 beneath->mourn_inferior ();
036b1ba8
JB
610}
611
e02544b2
JB
612/* Implement the to_core_of_thread target_ops "method". */
613
f6ac5f3d
PA
614int
615ravenscar_thread_target::core_of_thread (ptid_t ptid)
e02544b2 616{
5b6ea500
TT
617 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
618 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
619 return beneath ()->core_of_thread (inferior_ptid);
e02544b2
JB
620}
621
2080266b
TT
622/* Implement the target xfer_partial method. */
623
624enum target_xfer_status
625ravenscar_thread_target::xfer_partial (enum target_object object,
626 const char *annex,
627 gdb_byte *readbuf,
628 const gdb_byte *writebuf,
629 ULONGEST offset, ULONGEST len,
630 ULONGEST *xfered_len)
631{
632 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
633 /* Calling get_base_thread_from_ravenscar_task can read memory from
634 the inferior. However, that function is written to prefer our
635 internal map, so it should not result in recursive calls in
636 practice. */
637 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
638 return beneath ()->xfer_partial (object, annex, readbuf, writebuf,
639 offset, len, xfered_len);
640}
641
036b1ba8
JB
642/* Observer on inferior_created: push ravenscar thread stratum if needed. */
643
644static void
645ravenscar_inferior_created (struct target_ops *target, int from_tty)
646{
cf3fbed4 647 const char *err_msg;
7e35103a
JB
648
649 if (!ravenscar_task_support
7dc7c195 650 || gdbarch_ravenscar_ops (target_gdbarch ()) == NULL
7e35103a 651 || !has_ravenscar_runtime ())
25abf4de
JB
652 return;
653
cf3fbed4
JB
654 err_msg = ada_get_tcb_types_info ();
655 if (err_msg != NULL)
656 {
8f6cb6c3 657 warning (_("%s. Task/thread support disabled."), err_msg);
cf3fbed4
JB
658 return;
659 }
660
3d4470e5
TT
661 ravenscar_thread_target *rtarget = new ravenscar_thread_target ();
662 push_target (target_ops_up (rtarget));
663 thread_info *thr = rtarget->add_active_thread ();
664 if (thr != nullptr)
665 switch_to_thread (thr);
036b1ba8
JB
666}
667
f6ac5f3d
PA
668ptid_t
669ravenscar_thread_target::get_ada_task_ptid (long lwp, long thread)
036b1ba8 670{
0b790b1e 671 return ptid_t (m_base_ptid.pid (), 0, thread);
036b1ba8
JB
672}
673
036b1ba8
JB
674/* Command-list for the "set/show ravenscar" prefix command. */
675static struct cmd_list_element *set_ravenscar_list;
676static struct cmd_list_element *show_ravenscar_list;
677
036b1ba8
JB
678/* Implement the "show ravenscar task-switching" command. */
679
680static void
681show_ravenscar_task_switching_command (struct ui_file *file, int from_tty,
682 struct cmd_list_element *c,
683 const char *value)
684{
685 if (ravenscar_task_support)
686 fprintf_filtered (file, _("\
b64edec4 687Support for Ravenscar task/thread switching is enabled\n"));
036b1ba8
JB
688 else
689 fprintf_filtered (file, _("\
b64edec4 690Support for Ravenscar task/thread switching is disabled\n"));
036b1ba8
JB
691}
692
693/* Module startup initialization function, automagically called by
694 init.c. */
695
6c265988 696void _initialize_ravenscar ();
036b1ba8 697void
989f3c58 698_initialize_ravenscar ()
036b1ba8 699{
036b1ba8
JB
700 /* Notice when the inferior is created in order to push the
701 ravenscar ops if needed. */
76727919 702 gdb::observers::inferior_created.attach (ravenscar_inferior_created);
036b1ba8 703
0743fc83
TT
704 add_basic_prefix_cmd ("ravenscar", no_class,
705 _("Prefix command for changing Ravenscar-specific settings."),
706 &set_ravenscar_list, "set ravenscar ", 0, &setlist);
036b1ba8 707
0743fc83
TT
708 add_show_prefix_cmd ("ravenscar", no_class,
709 _("Prefix command for showing Ravenscar-specific settings."),
710 &show_ravenscar_list, "show ravenscar ", 0, &showlist);
036b1ba8
JB
711
712 add_setshow_boolean_cmd ("task-switching", class_obscure,
713 &ravenscar_task_support, _("\
590042fc
PW
714Enable or disable support for GNAT Ravenscar tasks."), _("\
715Show whether support for GNAT Ravenscar tasks is enabled."),
036b1ba8
JB
716 _("\
717Enable or disable support for task/thread switching with the GNAT\n\
718Ravenscar run-time library for bareboard configuration."),
719 NULL, show_ravenscar_task_switching_command,
720 &set_ravenscar_list, &show_ravenscar_list);
721}
This page took 1.03423 seconds and 4 git commands to generate.