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