]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ravenscar-thread.c
Return event_ptid from ravenscar_thread_target::wait
[thirdparty/binutils-gdb.git] / gdb / ravenscar-thread.c
1 /* Ada Ravenscar thread support.
2
3 Copyright (C) 2004-2020 Free Software Foundation, Inc.
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"
28 #include "observable.h"
29 #include "gdbcmd.h"
30 #include "top.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33
34 /* This module provides support for "Ravenscar" tasks (Ada) when
35 debugging on bare-metal targets.
36
37 The typical situation is when debugging a bare-metal target over
38 the remote protocol. In that situation, the system does not know
39 about high-level concepts such as threads, only about some code
40 running on one or more CPUs. And since the remote protocol does not
41 provide any handling for CPUs, the de facto standard for handling
42 them is to have one thread per CPU, where the thread's ptid has
43 its lwp field set to the CPU number (eg: 1 for the first CPU,
44 2 for the second one, etc). This module will make that assumption.
45
46 This module then creates and maintains the list of threads based
47 on the list of Ada tasks, with one thread per Ada task. The convention
48 is that threads corresponding to the CPUs (see assumption above)
49 have a ptid_t of the form (PID, LWP, 0), while threads corresponding
50 to our Ada tasks have a ptid_t of the form (PID, 0, TID) where TID
51 is the Ada task's ID as extracted from Ada runtime information.
52
53 Switching to a given Ada task (or its underlying thread) is performed
54 by fetching the registers of that task from the memory area where
55 the registers were saved. For any of the other operations, the
56 operation is performed by first finding the CPU on which the task
57 is running, switching to its corresponding ptid, and then performing
58 the operation on that ptid using the target beneath us. */
59
60 /* If true, ravenscar task support is enabled. */
61 static bool ravenscar_task_support = true;
62
63 static const char running_thread_name[] = "__gnat_running_thread_table";
64
65 static const char known_tasks_name[] = "system__tasking__debug__known_tasks";
66 static const char first_task_name[] = "system__tasking__debug__first_task";
67
68 static const char ravenscar_runtime_initializer[]
69 = "system__bb__threads__initialize";
70
71 static const target_info ravenscar_target_info = {
72 "ravenscar",
73 N_("Ravenscar tasks."),
74 N_("Ravenscar tasks support.")
75 };
76
77 struct ravenscar_thread_target final : public target_ops
78 {
79 ravenscar_thread_target ()
80 : m_base_ptid (inferior_ptid)
81 {
82 }
83
84 const target_info &info () const override
85 { return ravenscar_target_info; }
86
87 strata stratum () const override { return thread_stratum; }
88
89 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
90 void resume (ptid_t, int, enum gdb_signal) override;
91
92 void fetch_registers (struct regcache *, int) override;
93 void store_registers (struct regcache *, int) override;
94
95 void prepare_to_store (struct regcache *) override;
96
97 bool stopped_by_sw_breakpoint () override;
98
99 bool stopped_by_hw_breakpoint () override;
100
101 bool stopped_by_watchpoint () override;
102
103 bool stopped_data_address (CORE_ADDR *) override;
104
105 bool thread_alive (ptid_t ptid) override;
106
107 int core_of_thread (ptid_t ptid) override;
108
109 void update_thread_list () override;
110
111 const char *extra_thread_info (struct thread_info *) override;
112
113 std::string pid_to_str (ptid_t) override;
114
115 ptid_t get_ada_task_ptid (long lwp, long thread) override;
116
117 void mourn_inferior () override;
118
119 void close () override
120 {
121 delete this;
122 }
123
124 thread_info *add_active_thread ();
125
126 private:
127
128 /* PTID of the last thread that received an event.
129 This can be useful to determine the associated task that received
130 the event, to make it the current task. */
131 ptid_t m_base_ptid;
132
133 ptid_t active_task (int cpu);
134 bool task_is_currently_active (ptid_t ptid);
135 bool runtime_initialized ();
136 };
137
138 /* Return true iff PTID corresponds to a ravenscar task. */
139
140 static bool
141 is_ravenscar_task (ptid_t ptid)
142 {
143 /* By construction, ravenscar tasks have their LWP set to zero.
144 Also make sure that the TID is nonzero, as some remotes, when
145 asked for the list of threads, will return the first thread
146 as having its TID set to zero. For instance, TSIM version
147 2.0.48 for LEON3 sends 'm0' as a reply to the 'qfThreadInfo'
148 query, which the remote protocol layer then treats as a thread
149 whose TID is 0. This is obviously not a ravenscar task. */
150 return ptid.lwp () == 0 && ptid.tid () != 0;
151 }
152
153 /* Given PTID, which can be either a ravenscar task or a CPU thread,
154 return which CPU that ptid is running on.
155
156 This assume that PTID is a valid ptid_t. Otherwise, a gdb_assert
157 will be triggered. */
158
159 static int
160 ravenscar_get_thread_base_cpu (ptid_t ptid)
161 {
162 int base_cpu;
163
164 if (is_ravenscar_task (ptid))
165 {
166 struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid);
167
168 gdb_assert (task_info != NULL);
169 base_cpu = task_info->base_cpu;
170 }
171 else
172 {
173 /* We assume that the LWP of the PTID is equal to the CPU number. */
174 base_cpu = ptid.lwp ();
175 }
176
177 return base_cpu;
178 }
179
180 /* Given a ravenscar task (identified by its ptid_t PTID), return true
181 if this task is the currently active task on the cpu that task is
182 running on.
183
184 In other words, this function determine which CPU this task is
185 currently running on, and then return nonzero if the CPU in question
186 is executing the code for that task. If that's the case, then
187 that task's registers are in the CPU bank. Otherwise, the task
188 is currently suspended, and its registers have been saved in memory. */
189
190 bool
191 ravenscar_thread_target::task_is_currently_active (ptid_t ptid)
192 {
193 ptid_t active_task_ptid
194 = active_task (ravenscar_get_thread_base_cpu (ptid));
195
196 return ptid == active_task_ptid;
197 }
198
199 /* Return the CPU thread (as a ptid_t) on which the given ravenscar
200 task is running.
201
202 This is the thread that corresponds to the CPU on which the task
203 is running. */
204
205 static ptid_t
206 get_base_thread_from_ravenscar_task (ptid_t ptid)
207 {
208 int base_cpu;
209
210 if (!is_ravenscar_task (ptid))
211 return ptid;
212
213 base_cpu = ravenscar_get_thread_base_cpu (ptid);
214 return ptid_t (ptid.pid (), base_cpu, 0);
215 }
216
217 /* Fetch the ravenscar running thread from target memory, make sure
218 there's a corresponding thread in the thread list, and return it.
219 If the runtime is not initialized, return NULL. */
220
221 thread_info *
222 ravenscar_thread_target::add_active_thread ()
223 {
224 process_stratum_target *proc_target
225 = as_process_stratum_target (this->beneath ());
226
227 int base_cpu;
228
229 gdb_assert (!is_ravenscar_task (m_base_ptid));
230 base_cpu = ravenscar_get_thread_base_cpu (m_base_ptid);
231
232 if (!runtime_initialized ())
233 return nullptr;
234
235 /* Make sure we set m_base_ptid before calling active_task
236 as the latter relies on it. */
237 ptid_t active_ptid = active_task (base_cpu);
238 gdb_assert (active_ptid != null_ptid);
239
240 /* The running thread may not have been added to
241 system.tasking.debug's list yet; so ravenscar_update_thread_list
242 may not always add it to the thread list. Add it here. */
243 thread_info *active_thr = find_thread_ptid (proc_target, active_ptid);
244 if (active_thr == nullptr)
245 active_thr = add_thread (proc_target, active_ptid);
246 return active_thr;
247 }
248
249 /* The Ravenscar Runtime exports a symbol which contains the ID of
250 the thread that is currently running. Try to locate that symbol
251 and return its associated minimal symbol.
252 Return NULL if not found. */
253
254 static struct bound_minimal_symbol
255 get_running_thread_msymbol ()
256 {
257 struct bound_minimal_symbol msym;
258
259 msym = lookup_minimal_symbol (running_thread_name, NULL, NULL);
260 if (!msym.minsym)
261 /* Older versions of the GNAT runtime were using a different
262 (less ideal) name for the symbol where the active thread ID
263 is stored. If we couldn't find the symbol using the latest
264 name, then try the old one. */
265 msym = lookup_minimal_symbol ("running_thread", NULL, NULL);
266
267 return msym;
268 }
269
270 /* Return True if the Ada Ravenscar run-time can be found in the
271 application. */
272
273 static bool
274 has_ravenscar_runtime ()
275 {
276 struct bound_minimal_symbol msym_ravenscar_runtime_initializer
277 = lookup_minimal_symbol (ravenscar_runtime_initializer, NULL, NULL);
278 struct bound_minimal_symbol msym_known_tasks
279 = lookup_minimal_symbol (known_tasks_name, NULL, NULL);
280 struct bound_minimal_symbol msym_first_task
281 = lookup_minimal_symbol (first_task_name, NULL, NULL);
282 struct bound_minimal_symbol msym_running_thread
283 = get_running_thread_msymbol ();
284
285 return (msym_ravenscar_runtime_initializer.minsym
286 && (msym_known_tasks.minsym || msym_first_task.minsym)
287 && msym_running_thread.minsym);
288 }
289
290 /* Return True if the Ada Ravenscar run-time can be found in the
291 application, and if it has been initialized on target. */
292
293 bool
294 ravenscar_thread_target::runtime_initialized ()
295 {
296 return active_task (1) != null_ptid;
297 }
298
299 /* Return the ID of the thread that is currently running.
300 Return 0 if the ID could not be determined. */
301
302 static CORE_ADDR
303 get_running_thread_id (int cpu)
304 {
305 struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
306 int object_size;
307 int buf_size;
308 gdb_byte *buf;
309 CORE_ADDR object_addr;
310 struct type *builtin_type_void_data_ptr
311 = builtin_type (target_gdbarch ())->builtin_data_ptr;
312
313 if (!object_msym.minsym)
314 return 0;
315
316 object_size = TYPE_LENGTH (builtin_type_void_data_ptr);
317 object_addr = (BMSYMBOL_VALUE_ADDRESS (object_msym)
318 + (cpu - 1) * object_size);
319 buf_size = object_size;
320 buf = (gdb_byte *) alloca (buf_size);
321 read_memory (object_addr, buf, buf_size);
322 return extract_typed_address (buf, builtin_type_void_data_ptr);
323 }
324
325 void
326 ravenscar_thread_target::resume (ptid_t ptid, int step,
327 enum gdb_signal siggnal)
328 {
329 /* If we see a wildcard resume, we simply pass that on. Otherwise,
330 arrange to resume the base ptid. */
331 inferior_ptid = m_base_ptid;
332 if (ptid != minus_one_ptid)
333 ptid = m_base_ptid;
334 beneath ()->resume (ptid, step, siggnal);
335 }
336
337 ptid_t
338 ravenscar_thread_target::wait (ptid_t ptid,
339 struct target_waitstatus *status,
340 int options)
341 {
342 process_stratum_target *beneath
343 = as_process_stratum_target (this->beneath ());
344 ptid_t event_ptid;
345
346 if (ptid != minus_one_ptid)
347 ptid = m_base_ptid;
348 event_ptid = beneath->wait (ptid, status, 0);
349 /* Find any new threads that might have been created, and return the
350 active thread.
351
352 Only do it if the program is still alive, though. Otherwise,
353 this causes problems when debugging through the remote protocol,
354 because we might try switching threads (and thus sending packets)
355 after the remote has disconnected. */
356 if (status->kind != TARGET_WAITKIND_EXITED
357 && status->kind != TARGET_WAITKIND_SIGNALLED
358 && runtime_initialized ())
359 {
360 m_base_ptid = event_ptid;
361 this->update_thread_list ();
362 return this->add_active_thread ()->ptid;
363 }
364 return event_ptid;
365 }
366
367 /* Add the thread associated to the given TASK to the thread list
368 (if the thread has already been added, this is a no-op). */
369
370 static void
371 ravenscar_add_thread (struct ada_task_info *task)
372 {
373 if (find_thread_ptid (current_inferior (), task->ptid) == NULL)
374 add_thread (current_inferior ()->process_target (), task->ptid);
375 }
376
377 void
378 ravenscar_thread_target::update_thread_list ()
379 {
380 /* Do not clear the thread list before adding the Ada task, to keep
381 the thread that the process stratum has included into it
382 (m_base_ptid) and the running thread, that may not have been included
383 to system.tasking.debug's list yet. */
384
385 iterate_over_live_ada_tasks (ravenscar_add_thread);
386 }
387
388 ptid_t
389 ravenscar_thread_target::active_task (int cpu)
390 {
391 CORE_ADDR tid = get_running_thread_id (cpu);
392
393 if (tid == 0)
394 return null_ptid;
395 else
396 return ptid_t (m_base_ptid.pid (), 0, tid);
397 }
398
399 const char *
400 ravenscar_thread_target::extra_thread_info (thread_info *tp)
401 {
402 return "Ravenscar task";
403 }
404
405 bool
406 ravenscar_thread_target::thread_alive (ptid_t ptid)
407 {
408 /* Ravenscar tasks are non-terminating. */
409 return true;
410 }
411
412 std::string
413 ravenscar_thread_target::pid_to_str (ptid_t ptid)
414 {
415 return string_printf ("Thread %#x", (int) ptid.tid ());
416 }
417
418 void
419 ravenscar_thread_target::fetch_registers (struct regcache *regcache, int regnum)
420 {
421 ptid_t ptid = regcache->ptid ();
422
423 if (runtime_initialized ()
424 && is_ravenscar_task (ptid)
425 && !task_is_currently_active (ptid))
426 {
427 struct gdbarch *gdbarch = regcache->arch ();
428 struct ravenscar_arch_ops *arch_ops
429 = gdbarch_ravenscar_ops (gdbarch);
430
431 arch_ops->fetch_registers (regcache, regnum);
432 }
433 else
434 beneath ()->fetch_registers (regcache, regnum);
435 }
436
437 void
438 ravenscar_thread_target::store_registers (struct regcache *regcache,
439 int regnum)
440 {
441 ptid_t ptid = regcache->ptid ();
442
443 if (runtime_initialized ()
444 && is_ravenscar_task (ptid)
445 && !task_is_currently_active (ptid))
446 {
447 struct gdbarch *gdbarch = regcache->arch ();
448 struct ravenscar_arch_ops *arch_ops
449 = gdbarch_ravenscar_ops (gdbarch);
450
451 arch_ops->store_registers (regcache, regnum);
452 }
453 else
454 beneath ()->store_registers (regcache, regnum);
455 }
456
457 void
458 ravenscar_thread_target::prepare_to_store (struct regcache *regcache)
459 {
460 ptid_t ptid = regcache->ptid ();
461
462 if (runtime_initialized ()
463 && is_ravenscar_task (ptid)
464 && !task_is_currently_active (ptid))
465 {
466 /* Nothing. */
467 }
468 else
469 beneath ()->prepare_to_store (regcache);
470 }
471
472 /* Implement the to_stopped_by_sw_breakpoint target_ops "method". */
473
474 bool
475 ravenscar_thread_target::stopped_by_sw_breakpoint ()
476 {
477 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
478 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
479 return beneath ()->stopped_by_sw_breakpoint ();
480 }
481
482 /* Implement the to_stopped_by_hw_breakpoint target_ops "method". */
483
484 bool
485 ravenscar_thread_target::stopped_by_hw_breakpoint ()
486 {
487 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
488 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
489 return beneath ()->stopped_by_hw_breakpoint ();
490 }
491
492 /* Implement the to_stopped_by_watchpoint target_ops "method". */
493
494 bool
495 ravenscar_thread_target::stopped_by_watchpoint ()
496 {
497 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
498 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
499 return beneath ()->stopped_by_watchpoint ();
500 }
501
502 /* Implement the to_stopped_data_address target_ops "method". */
503
504 bool
505 ravenscar_thread_target::stopped_data_address (CORE_ADDR *addr_p)
506 {
507 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
508 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
509 return beneath ()->stopped_data_address (addr_p);
510 }
511
512 void
513 ravenscar_thread_target::mourn_inferior ()
514 {
515 m_base_ptid = null_ptid;
516 target_ops *beneath = this->beneath ();
517 unpush_target (this);
518 beneath->mourn_inferior ();
519 }
520
521 /* Implement the to_core_of_thread target_ops "method". */
522
523 int
524 ravenscar_thread_target::core_of_thread (ptid_t ptid)
525 {
526 scoped_restore save_ptid = make_scoped_restore (&inferior_ptid);
527 inferior_ptid = get_base_thread_from_ravenscar_task (inferior_ptid);
528 return beneath ()->core_of_thread (inferior_ptid);
529 }
530
531 /* Observer on inferior_created: push ravenscar thread stratum if needed. */
532
533 static void
534 ravenscar_inferior_created (struct target_ops *target, int from_tty)
535 {
536 const char *err_msg;
537
538 if (!ravenscar_task_support
539 || gdbarch_ravenscar_ops (target_gdbarch ()) == NULL
540 || !has_ravenscar_runtime ())
541 return;
542
543 err_msg = ada_get_tcb_types_info ();
544 if (err_msg != NULL)
545 {
546 warning (_("%s. Task/thread support disabled."), err_msg);
547 return;
548 }
549
550 ravenscar_thread_target *rtarget = new ravenscar_thread_target ();
551 push_target (target_ops_up (rtarget));
552 thread_info *thr = rtarget->add_active_thread ();
553 if (thr != nullptr)
554 switch_to_thread (thr);
555 }
556
557 ptid_t
558 ravenscar_thread_target::get_ada_task_ptid (long lwp, long thread)
559 {
560 return ptid_t (m_base_ptid.pid (), 0, thread);
561 }
562
563 /* Command-list for the "set/show ravenscar" prefix command. */
564 static struct cmd_list_element *set_ravenscar_list;
565 static struct cmd_list_element *show_ravenscar_list;
566
567 /* Implement the "show ravenscar task-switching" command. */
568
569 static void
570 show_ravenscar_task_switching_command (struct ui_file *file, int from_tty,
571 struct cmd_list_element *c,
572 const char *value)
573 {
574 if (ravenscar_task_support)
575 fprintf_filtered (file, _("\
576 Support for Ravenscar task/thread switching is enabled\n"));
577 else
578 fprintf_filtered (file, _("\
579 Support for Ravenscar task/thread switching is disabled\n"));
580 }
581
582 /* Module startup initialization function, automagically called by
583 init.c. */
584
585 void _initialize_ravenscar ();
586 void
587 _initialize_ravenscar ()
588 {
589 /* Notice when the inferior is created in order to push the
590 ravenscar ops if needed. */
591 gdb::observers::inferior_created.attach (ravenscar_inferior_created);
592
593 add_basic_prefix_cmd ("ravenscar", no_class,
594 _("Prefix command for changing Ravenscar-specific settings."),
595 &set_ravenscar_list, "set ravenscar ", 0, &setlist);
596
597 add_show_prefix_cmd ("ravenscar", no_class,
598 _("Prefix command for showing Ravenscar-specific settings."),
599 &show_ravenscar_list, "show ravenscar ", 0, &showlist);
600
601 add_setshow_boolean_cmd ("task-switching", class_obscure,
602 &ravenscar_task_support, _("\
603 Enable or disable support for GNAT Ravenscar tasks."), _("\
604 Show whether support for GNAT Ravenscar tasks is enabled."),
605 _("\
606 Enable or disable support for task/thread switching with the GNAT\n\
607 Ravenscar run-time library for bareboard configuration."),
608 NULL, show_ravenscar_task_switching_command,
609 &set_ravenscar_list, &show_ravenscar_list);
610 }