e7922277ce84bb717515b0cb5cb32762a6e6d21a
[deliverable/binutils-gdb.git] / gdb / ravenscar-thread.c
1 /* Ada Ravenscar thread support.
2
3 Copyright (C) 2004-2017 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 "observer.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 comcepts 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 tasks. The convention
48 is that threads corresponding to the CPUs (see assumption above)
49 have a ptid_t of the form (PID, LWP, 0), which 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 tasks (or its underlying thread) is performed
54 by fetching the registers of that tasks 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 non-null, ravenscar task support is enabled. */
61 static int ravenscar_task_support = 1;
62
63 /* This module's target-specific operations. */
64 static struct target_ops ravenscar_ops;
65
66 /* PTID of the last thread that received an event.
67 This can be useful to determine the associated task that received
68 the event, to make it the current task. */
69 static ptid_t base_ptid;
70
71 static const char running_thread_name[] = "__gnat_running_thread_table";
72
73 static const char known_tasks_name[] = "system__tasking__debug__known_tasks";
74 static const char first_task_name[] = "system__tasking__debug__first_task";
75
76 static const char ravenscar_runtime_initializer[] =
77 "system__bb__threads__initialize";
78
79 static void ravenscar_update_thread_list (struct target_ops *ops);
80 static ptid_t ravenscar_active_task (int cpu);
81 static const char *ravenscar_extra_thread_info (struct target_ops *self,
82 struct thread_info *tp);
83 static int ravenscar_thread_alive (struct target_ops *ops, ptid_t ptid);
84 static void ravenscar_fetch_registers (struct target_ops *ops,
85 struct regcache *regcache, int regnum);
86 static void ravenscar_store_registers (struct target_ops *ops,
87 struct regcache *regcache, int regnum);
88 static void ravenscar_prepare_to_store (struct target_ops *self,
89 struct regcache *regcache);
90 static void ravenscar_resume (struct target_ops *ops, ptid_t ptid, int step,
91 enum gdb_signal siggnal);
92 static void ravenscar_mourn_inferior (struct target_ops *ops);
93 static void ravenscar_update_inferior_ptid (void);
94 static int has_ravenscar_runtime (void);
95 static int ravenscar_runtime_initialized (void);
96 static void ravenscar_inferior_created (struct target_ops *target,
97 int from_tty);
98
99 /* Return nonzero iff PTID corresponds to a ravenscar task. */
100
101 static int
102 is_ravenscar_task (ptid_t ptid)
103 {
104 /* By construction, ravenscar tasks have their LWP set to zero. */
105 return ptid_get_lwp (ptid) == 0;
106 }
107
108 /* Given PTID, which can be either a ravenscar task or a CPU thread,
109 return which CPU that ptid is running on.
110
111 This assume that PTID is a valid ptid_t. Otherwise, a gdb_assert
112 will be triggered. */
113
114 static int
115 ravenscar_get_thread_base_cpu (ptid_t ptid)
116 {
117 int base_cpu;
118
119 if (is_ravenscar_task (ptid))
120 {
121 struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid);
122
123 gdb_assert (task_info != NULL);
124 base_cpu = task_info->base_cpu;
125 }
126 else
127 {
128 /* We assume that the LWP of the PTID is equal to the CPU number. */
129 base_cpu = ptid_get_lwp (ptid);
130 }
131
132 return base_cpu;
133 }
134
135 /* Given a ravenscar task (identified by its ptid_t PTID), return nonzero
136 if this task is the currently active task on the cpu that task is
137 running on.
138
139 In other words, this function determine which CPU this task is
140 currently running on, and then return nonzero if the CPU in question
141 is executing the code for that task. If that's the case, then
142 that task's registers are in the CPU bank. Otherwise, the task
143 is currently suspended, and its registers have been saved in memory. */
144
145 static int
146 ravenscar_task_is_currently_active (ptid_t ptid)
147 {
148 ptid_t active_task_ptid
149 = ravenscar_active_task (ravenscar_get_thread_base_cpu (ptid));
150
151 return ptid_equal (ptid, active_task_ptid);
152 }
153
154 /* Return the CPU thread (as a ptid_t) on which the given ravenscar
155 task is running.
156
157 This is the thread that corresponds to the CPU on which the task
158 is running. */
159
160 static ptid_t
161 get_base_thread_from_ravenscar_task (ptid_t ptid)
162 {
163 int base_cpu;
164
165 if (!is_ravenscar_task (ptid))
166 return ptid;
167
168 base_cpu = ravenscar_get_thread_base_cpu (ptid);
169 return ptid_build (ptid_get_pid (ptid), base_cpu, 0);
170 }
171
172 /* Fetch the ravenscar running thread from target memory and
173 update inferior_ptid accordingly. */
174
175 static void
176 ravenscar_update_inferior_ptid (void)
177 {
178 int base_cpu;
179
180 base_ptid = inferior_ptid;
181
182 gdb_assert (!is_ravenscar_task (inferior_ptid));
183 base_cpu = ravenscar_get_thread_base_cpu (base_ptid);
184
185 /* If the runtime has not been initialized yet, the inferior_ptid is
186 the only ptid that there is. */
187 if (!ravenscar_runtime_initialized ())
188 return;
189
190 /* Make sure we set base_ptid before calling ravenscar_active_task
191 as the latter relies on it. */
192 inferior_ptid = ravenscar_active_task (base_cpu);
193 gdb_assert (!ptid_equal (inferior_ptid, null_ptid));
194
195 /* The running thread may not have been added to
196 system.tasking.debug's list yet; so ravenscar_update_thread_list
197 may not always add it to the thread list. Add it here. */
198 if (!find_thread_ptid (inferior_ptid))
199 add_thread (inferior_ptid);
200 }
201
202 /* The Ravenscar Runtime exports a symbol which contains the ID of
203 the thread that is currently running. Try to locate that symbol
204 and return its associated minimal symbol.
205 Return NULL if not found. */
206
207 static struct bound_minimal_symbol
208 get_running_thread_msymbol (void)
209 {
210 struct bound_minimal_symbol msym;
211
212 msym = lookup_minimal_symbol (running_thread_name, NULL, NULL);
213 if (!msym.minsym)
214 /* Older versions of the GNAT runtime were using a different
215 (less ideal) name for the symbol where the active thread ID
216 is stored. If we couldn't find the symbol using the latest
217 name, then try the old one. */
218 msym = lookup_minimal_symbol ("running_thread", NULL, NULL);
219
220 return msym;
221 }
222
223 /* Return True if the Ada Ravenscar run-time can be found in the
224 application. */
225
226 static int
227 has_ravenscar_runtime (void)
228 {
229 struct bound_minimal_symbol msym_ravenscar_runtime_initializer =
230 lookup_minimal_symbol (ravenscar_runtime_initializer, NULL, NULL);
231 struct bound_minimal_symbol msym_known_tasks =
232 lookup_minimal_symbol (known_tasks_name, NULL, NULL);
233 struct bound_minimal_symbol msym_first_task =
234 lookup_minimal_symbol (first_task_name, NULL, NULL);
235 struct bound_minimal_symbol msym_running_thread
236 = get_running_thread_msymbol ();
237
238 return (msym_ravenscar_runtime_initializer.minsym
239 && (msym_known_tasks.minsym || msym_first_task.minsym)
240 && msym_running_thread.minsym);
241 }
242
243 /* Return True if the Ada Ravenscar run-time can be found in the
244 application, and if it has been initialized on target. */
245
246 static int
247 ravenscar_runtime_initialized (void)
248 {
249 return (!(ptid_equal (ravenscar_active_task (1), null_ptid)));
250 }
251
252 /* Return the ID of the thread that is currently running.
253 Return 0 if the ID could not be determined. */
254
255 static CORE_ADDR
256 get_running_thread_id (int cpu)
257 {
258 struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
259 int object_size;
260 int buf_size;
261 gdb_byte *buf;
262 CORE_ADDR object_addr;
263 struct type *builtin_type_void_data_ptr =
264 builtin_type (target_gdbarch ())->builtin_data_ptr;
265
266 if (!object_msym.minsym)
267 return 0;
268
269 object_size = TYPE_LENGTH (builtin_type_void_data_ptr);
270 object_addr = (BMSYMBOL_VALUE_ADDRESS (object_msym)
271 + (cpu - 1) * object_size);
272 buf_size = object_size;
273 buf = (gdb_byte *) alloca (buf_size);
274 read_memory (object_addr, buf, buf_size);
275 return extract_typed_address (buf, builtin_type_void_data_ptr);
276 }
277
278 static void
279 ravenscar_resume (struct target_ops *ops, ptid_t ptid, int step,
280 enum gdb_signal siggnal)
281 {
282 struct target_ops *beneath = find_target_beneath (ops);
283
284 inferior_ptid = base_ptid;
285 beneath->to_resume (beneath, base_ptid, step, siggnal);
286 }
287
288 static ptid_t
289 ravenscar_wait (struct target_ops *ops, ptid_t ptid,
290 struct target_waitstatus *status,
291 int options)
292 {
293 struct target_ops *beneath = find_target_beneath (ops);
294
295 inferior_ptid = base_ptid;
296 beneath->to_wait (beneath, base_ptid, status, 0);
297 /* Find any new threads that might have been created, and update
298 inferior_ptid to the active thread.
299
300 Only do it if the program is still alive, though. Otherwise,
301 this causes problems when debugging through the remote protocol,
302 because we might try switching threads (and thus sending packets)
303 after the remote has disconnected. */
304 if (status->kind != TARGET_WAITKIND_EXITED
305 && status->kind != TARGET_WAITKIND_SIGNALLED)
306 {
307 ravenscar_update_thread_list (ops);
308 ravenscar_update_inferior_ptid ();
309 }
310 return inferior_ptid;
311 }
312
313 /* Add the thread associated to the given TASK to the thread list
314 (if the thread has already been added, this is a no-op). */
315
316 static void
317 ravenscar_add_thread (struct ada_task_info *task)
318 {
319 if (find_thread_ptid (task->ptid) == NULL)
320 add_thread (task->ptid);
321 }
322
323 static void
324 ravenscar_update_thread_list (struct target_ops *ops)
325 {
326 ada_build_task_list ();
327
328 /* Do not clear the thread list before adding the Ada task, to keep
329 the thread that the process stratum has included into it
330 (base_ptid) and the running thread, that may not have been included
331 to system.tasking.debug's list yet. */
332
333 iterate_over_live_ada_tasks (ravenscar_add_thread);
334 }
335
336 static ptid_t
337 ravenscar_active_task (int cpu)
338 {
339 CORE_ADDR tid = get_running_thread_id (cpu);
340
341 if (tid == 0)
342 return null_ptid;
343 else
344 return ptid_build (ptid_get_pid (base_ptid), 0, tid);
345 }
346
347 static const char *
348 ravenscar_extra_thread_info (struct target_ops *self, struct thread_info *tp)
349 {
350 return "Ravenscar task";
351 }
352
353 static int
354 ravenscar_thread_alive (struct target_ops *ops, ptid_t ptid)
355 {
356 /* Ravenscar tasks are non-terminating. */
357 return 1;
358 }
359
360 static const char *
361 ravenscar_pid_to_str (struct target_ops *ops, ptid_t ptid)
362 {
363 static char buf[30];
364
365 snprintf (buf, sizeof (buf), "Thread %#x", (int) ptid_get_tid (ptid));
366 return buf;
367 }
368
369 static void
370 ravenscar_fetch_registers (struct target_ops *ops,
371 struct regcache *regcache, int regnum)
372 {
373 struct target_ops *beneath = find_target_beneath (ops);
374 ptid_t ptid = regcache_get_ptid (regcache);
375
376 if (ravenscar_runtime_initialized ()
377 && is_ravenscar_task (ptid)
378 && !ravenscar_task_is_currently_active (ptid))
379 {
380 struct gdbarch *gdbarch = regcache->arch ();
381 struct ravenscar_arch_ops *arch_ops
382 = gdbarch_ravenscar_ops (gdbarch);
383
384 arch_ops->to_fetch_registers (regcache, regnum);
385 }
386 else
387 beneath->to_fetch_registers (beneath, regcache, regnum);
388 }
389
390 static void
391 ravenscar_store_registers (struct target_ops *ops,
392 struct regcache *regcache, int regnum)
393 {
394 struct target_ops *beneath = find_target_beneath (ops);
395 ptid_t ptid = regcache_get_ptid (regcache);
396
397 if (ravenscar_runtime_initialized ()
398 && is_ravenscar_task (ptid)
399 && !ravenscar_task_is_currently_active (ptid))
400 {
401 struct gdbarch *gdbarch = regcache->arch ();
402 struct ravenscar_arch_ops *arch_ops
403 = gdbarch_ravenscar_ops (gdbarch);
404
405 arch_ops->to_store_registers (regcache, regnum);
406 }
407 else
408 beneath->to_store_registers (beneath, regcache, regnum);
409 }
410
411 static void
412 ravenscar_prepare_to_store (struct target_ops *self,
413 struct regcache *regcache)
414 {
415 struct target_ops *beneath = find_target_beneath (self);
416 ptid_t ptid = regcache_get_ptid (regcache);
417
418 if (ravenscar_runtime_initialized ()
419 && is_ravenscar_task (ptid)
420 && !ravenscar_task_is_currently_active (ptid))
421 {
422 struct gdbarch *gdbarch = regcache->arch ();
423 struct ravenscar_arch_ops *arch_ops
424 = gdbarch_ravenscar_ops (gdbarch);
425
426 arch_ops->to_prepare_to_store (regcache);
427 }
428 else
429 beneath->to_prepare_to_store (beneath, regcache);
430 }
431
432 /* Implement the to_stopped_by_sw_breakpoint target_ops "method". */
433
434 static int
435 ravenscar_stopped_by_sw_breakpoint (struct target_ops *ops)
436 {
437 ptid_t saved_ptid = inferior_ptid;
438 struct target_ops *beneath = find_target_beneath (ops);
439 int result;
440
441 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
442 result = beneath->to_stopped_by_sw_breakpoint (beneath);
443 inferior_ptid = saved_ptid;
444 return result;
445 }
446
447 /* Implement the to_stopped_by_hw_breakpoint target_ops "method". */
448
449 static int
450 ravenscar_stopped_by_hw_breakpoint (struct target_ops *ops)
451 {
452 ptid_t saved_ptid = inferior_ptid;
453 struct target_ops *beneath = find_target_beneath (ops);
454 int result;
455
456 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
457 result = beneath->to_stopped_by_hw_breakpoint (beneath);
458 inferior_ptid = saved_ptid;
459 return result;
460 }
461
462 /* Implement the to_stopped_by_watchpoint target_ops "method". */
463
464 static int
465 ravenscar_stopped_by_watchpoint (struct target_ops *ops)
466 {
467 ptid_t saved_ptid = inferior_ptid;
468 struct target_ops *beneath = find_target_beneath (ops);
469 int result;
470
471 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
472 result = beneath->to_stopped_by_watchpoint (beneath);
473 inferior_ptid = saved_ptid;
474 return result;
475 }
476
477 /* Implement the to_stopped_data_address target_ops "method". */
478
479 static int
480 ravenscar_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
481 {
482 ptid_t saved_ptid = inferior_ptid;
483 struct target_ops *beneath = find_target_beneath (ops);
484 int result;
485
486 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
487 result = beneath->to_stopped_data_address (beneath, addr_p);
488 inferior_ptid = saved_ptid;
489 return result;
490 }
491
492 static void
493 ravenscar_mourn_inferior (struct target_ops *ops)
494 {
495 struct target_ops *beneath = find_target_beneath (ops);
496
497 base_ptid = null_ptid;
498 beneath->to_mourn_inferior (beneath);
499 unpush_target (&ravenscar_ops);
500 }
501
502 /* Implement the to_core_of_thread target_ops "method". */
503
504 static int
505 ravenscar_core_of_thread (struct target_ops *ops, ptid_t ptid)
506 {
507 ptid_t saved_ptid = inferior_ptid;
508 struct target_ops *beneath = find_target_beneath (ops);
509 int result;
510
511 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
512 result = beneath->to_core_of_thread (beneath, inferior_ptid);
513 inferior_ptid = saved_ptid;
514 return result;
515 }
516
517 /* Observer on inferior_created: push ravenscar thread stratum if needed. */
518
519 static void
520 ravenscar_inferior_created (struct target_ops *target, int from_tty)
521 {
522 const char *err_msg;
523
524 if (!ravenscar_task_support
525 || gdbarch_ravenscar_ops (target_gdbarch ()) == NULL
526 || !has_ravenscar_runtime ())
527 return;
528
529 err_msg = ada_get_tcb_types_info ();
530 if (err_msg != NULL)
531 {
532 warning (_("%s. Task/thread support disabled.\n"), err_msg);
533 return;
534 }
535
536 ravenscar_update_inferior_ptid ();
537 push_target (&ravenscar_ops);
538 }
539
540 static ptid_t
541 ravenscar_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
542 {
543 return ptid_build (ptid_get_pid (base_ptid), 0, thread);
544 }
545
546 static void
547 init_ravenscar_thread_ops (void)
548 {
549 ravenscar_ops.to_shortname = "ravenscar";
550 ravenscar_ops.to_longname = "Ravenscar tasks.";
551 ravenscar_ops.to_doc = "Ravenscar tasks support.";
552 ravenscar_ops.to_resume = ravenscar_resume;
553 ravenscar_ops.to_wait = ravenscar_wait;
554 ravenscar_ops.to_fetch_registers = ravenscar_fetch_registers;
555 ravenscar_ops.to_store_registers = ravenscar_store_registers;
556 ravenscar_ops.to_prepare_to_store = ravenscar_prepare_to_store;
557 ravenscar_ops.to_stopped_by_sw_breakpoint
558 = ravenscar_stopped_by_sw_breakpoint;
559 ravenscar_ops.to_stopped_by_hw_breakpoint
560 = ravenscar_stopped_by_hw_breakpoint;
561 ravenscar_ops.to_stopped_by_watchpoint = ravenscar_stopped_by_watchpoint;
562 ravenscar_ops.to_stopped_data_address = ravenscar_stopped_data_address;
563 ravenscar_ops.to_thread_alive = ravenscar_thread_alive;
564 ravenscar_ops.to_update_thread_list = ravenscar_update_thread_list;
565 ravenscar_ops.to_pid_to_str = ravenscar_pid_to_str;
566 ravenscar_ops.to_extra_thread_info = ravenscar_extra_thread_info;
567 ravenscar_ops.to_get_ada_task_ptid = ravenscar_get_ada_task_ptid;
568 ravenscar_ops.to_mourn_inferior = ravenscar_mourn_inferior;
569 ravenscar_ops.to_has_all_memory = default_child_has_all_memory;
570 ravenscar_ops.to_has_memory = default_child_has_memory;
571 ravenscar_ops.to_has_stack = default_child_has_stack;
572 ravenscar_ops.to_has_registers = default_child_has_registers;
573 ravenscar_ops.to_has_execution = default_child_has_execution;
574 ravenscar_ops.to_stratum = thread_stratum;
575 ravenscar_ops.to_core_of_thread = ravenscar_core_of_thread;
576 ravenscar_ops.to_magic = OPS_MAGIC;
577 }
578
579 /* Command-list for the "set/show ravenscar" prefix command. */
580 static struct cmd_list_element *set_ravenscar_list;
581 static struct cmd_list_element *show_ravenscar_list;
582
583 /* Implement the "set ravenscar" prefix command. */
584
585 static void
586 set_ravenscar_command (const char *arg, int from_tty)
587 {
588 printf_unfiltered (_(\
589 "\"set ravenscar\" must be followed by the name of a setting.\n"));
590 help_list (set_ravenscar_list, "set ravenscar ", all_commands, gdb_stdout);
591 }
592
593 /* Implement the "show ravenscar" prefix command. */
594
595 static void
596 show_ravenscar_command (const char *args, int from_tty)
597 {
598 cmd_show_list (show_ravenscar_list, from_tty, "");
599 }
600
601 /* Implement the "show ravenscar task-switching" command. */
602
603 static void
604 show_ravenscar_task_switching_command (struct ui_file *file, int from_tty,
605 struct cmd_list_element *c,
606 const char *value)
607 {
608 if (ravenscar_task_support)
609 fprintf_filtered (file, _("\
610 Support for Ravenscar task/thread switching is enabled\n"));
611 else
612 fprintf_filtered (file, _("\
613 Support for Ravenscar task/thread switching is disabled\n"));
614 }
615
616 /* Module startup initialization function, automagically called by
617 init.c. */
618
619 void
620 _initialize_ravenscar (void)
621 {
622 init_ravenscar_thread_ops ();
623 base_ptid = null_ptid;
624
625 /* Notice when the inferior is created in order to push the
626 ravenscar ops if needed. */
627 observer_attach_inferior_created (ravenscar_inferior_created);
628
629 complete_target_initialization (&ravenscar_ops);
630
631 add_prefix_cmd ("ravenscar", no_class, set_ravenscar_command,
632 _("Prefix command for changing Ravenscar-specific settings"),
633 &set_ravenscar_list, "set ravenscar ", 0, &setlist);
634
635 add_prefix_cmd ("ravenscar", no_class, show_ravenscar_command,
636 _("Prefix command for showing Ravenscar-specific settings"),
637 &show_ravenscar_list, "show ravenscar ", 0, &showlist);
638
639 add_setshow_boolean_cmd ("task-switching", class_obscure,
640 &ravenscar_task_support, _("\
641 Enable or disable support for GNAT Ravenscar tasks"), _("\
642 Show whether support for GNAT Ravenscar tasks is enabled"),
643 _("\
644 Enable or disable support for task/thread switching with the GNAT\n\
645 Ravenscar run-time library for bareboard configuration."),
646 NULL, show_ravenscar_task_switching_command,
647 &set_ravenscar_list, &show_ravenscar_list);
648 }
This page took 0.055181 seconds and 4 git commands to generate.