]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ravenscar-thread.c
7a80dcf9add19ac44dbfab1954768d090c1355f6
[thirdparty/binutils-gdb.git] / gdb / ravenscar-thread.c
1 /* Ada Ravenscar thread support.
2
3 Copyright 2004, 2009, 2010 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 "gdb_string.h"
30 #include "gdbcmd.h"
31 #include "top.h"
32 #include "regcache.h"
33
34 /* If non-null, ravenscar task support is enabled. */
35 static int ravenscar_task_support = 1;
36
37 /* Non-null if the ravenscar thread layer has been pushed on the target
38 stack. */
39 static int ravenscar_is_open = 0;
40
41 /* This module's target-specific operations. */
42 static struct target_ops ravenscar_ops;
43
44 /* Some base target uses a special value for the null PID (exempli gratia
45 remote). */
46 static ptid_t base_magic_null_ptid;
47
48 /* Ptid of the inferior as seen by the process stratum. */
49 static ptid_t base_ptid;
50
51 static const char running_thread_name[] = "running_thread";
52
53 static const char known_tasks_name[] = "system__tasking__debug__known_tasks";
54
55 static const char ravenscar_runtime_initializer[] = "system__bb__threads__initialize";
56
57 static struct observer *update_target_observer = NULL;
58
59 /* Architecture-specific hooks. */
60 static struct ravenscar_arch_ops* current_arch_ops;
61
62 static CORE_ADDR read_thread_id (const char *symbol_name);
63
64 static void ravenscar_find_new_threads (struct target_ops *ops);
65 static ptid_t ravenscar_running_thread (void);
66 static char *ravenscar_extra_thread_info (struct thread_info *tp);
67 static int ravenscar_thread_alive (struct target_ops *ops, ptid_t ptid);
68 static void ravenscar_fetch_registers (struct target_ops *ops,
69 struct regcache *regcache, int regnum);
70 static void ravenscar_store_registers (struct target_ops *ops,
71 struct regcache *regcache, int regnum);
72 static void ravenscar_prepare_to_store (struct regcache *regcache);
73 static void ravenscar_initialize (char *name, int from_tty);
74 static void ravenscar_resume (struct target_ops *ops, ptid_t ptid, int step,
75 enum target_signal siggnal);
76 static void ravenscar_mourn_inferior (struct target_ops *ops);
77 static void ravenscar_update_inferior_ptid (void);
78 static int has_ravenscar_runtime (void);
79 static int ravenscar_runtime_initialized (void);
80 static void ravenscar_inferior_created (struct target_ops *target,
81 int from_tty);
82
83 /* Fetch the ravenscar running thread from target memory and
84 update inferior_ptid accordingly. */
85
86 static void
87 ravenscar_update_inferior_ptid (void)
88 {
89 base_ptid = inferior_ptid;
90
91 /* If the runtime has not been initialized yet, the inferior_ptid is
92 the only ptid that there is. */
93 if (!ravenscar_runtime_initialized ())
94 return;
95
96 /* Make sure we set base_ptid before calling ravenscar_running_thread
97 as the latter relies on it. */
98 inferior_ptid = ravenscar_running_thread ();
99 gdb_assert (!ptid_equal (inferior_ptid, null_ptid));
100
101 /* The running thread may not have been added to
102 system.tasking.debug's list yet; so ravenscar_find_new_threads
103 may not always add it to the thread list. Add it here. */
104 if (!find_thread_ptid (inferior_ptid))
105 add_thread (inferior_ptid);
106 }
107
108 /* Return True if the Ada Ravenscar run-time can be found in the
109 application. */
110
111 static int
112 has_ravenscar_runtime (void)
113 {
114 struct minimal_symbol *msym_ravenscar_runtime_initializer =
115 lookup_minimal_symbol (ravenscar_runtime_initializer, NULL, NULL);
116 struct minimal_symbol *msym_known_tasks =
117 lookup_minimal_symbol (known_tasks_name, NULL, NULL);
118 struct minimal_symbol *msym_running_thread =
119 lookup_minimal_symbol (running_thread_name, NULL, NULL);
120
121 return (msym_ravenscar_runtime_initializer
122 && msym_known_tasks
123 && msym_running_thread);
124 }
125
126 /* Return True if the Ada Ravenscar run-time can be found in the
127 application, and if it has been initialized on target. */
128
129 static int
130 ravenscar_runtime_initialized (void)
131 {
132 return (!(ptid_equal (ravenscar_running_thread (), null_ptid)));
133 }
134
135 /* Read the thread ID whose symbol name is SYMBOL_NAME. */
136
137 static CORE_ADDR
138 read_thread_id (const char *symbol_name)
139 {
140 const struct minimal_symbol *object_msym =
141 lookup_minimal_symbol (symbol_name, NULL, NULL);
142 int object_size;
143 int buf_size;
144 char *buf;
145 CORE_ADDR object_addr;
146 struct type *builtin_type_void_data_ptr =
147 builtin_type (target_gdbarch)->builtin_data_ptr;
148
149 if (!object_msym)
150 return 0;
151
152 object_addr = SYMBOL_VALUE_ADDRESS (object_msym);
153 object_size = TYPE_LENGTH (builtin_type_void_data_ptr);
154 buf_size = object_size;
155 buf = alloca (buf_size);
156 read_memory (object_addr, buf, buf_size);
157 return extract_typed_address (buf, builtin_type_void_data_ptr);
158 }
159
160 static void
161 ravenscar_close (int quitting)
162 {
163 ravenscar_is_open = 0;
164 }
165
166 static void
167 ravenscar_resume (struct target_ops *ops, ptid_t ptid, int step,
168 enum target_signal siggnal)
169 {
170 struct target_ops *beneath = find_target_beneath (ops);
171
172 inferior_ptid = base_ptid;
173 beneath->to_resume (beneath, base_ptid, step, siggnal);
174 }
175
176 static ptid_t
177 ravenscar_wait (struct target_ops *ops, ptid_t ptid,
178 struct target_waitstatus *status,
179 int options)
180 {
181 struct target_ops *beneath = find_target_beneath (ops);
182
183 inferior_ptid = base_ptid;
184 beneath->to_wait (beneath, base_ptid, status, 0);
185 ravenscar_find_new_threads (ops);
186 ravenscar_update_inferior_ptid ();
187 return inferior_ptid;
188 }
189
190 /* Add the thread associated to the given TASK to the thread list
191 (if the thread has already been added, this is a no-op). */
192
193 static void
194 ravenscar_add_thread (struct ada_task_info *task)
195 {
196 if (find_thread_ptid (task->ptid) == NULL)
197 add_thread (task->ptid);
198 }
199
200 static void
201 ravenscar_find_new_threads (struct target_ops *ops)
202 {
203 ada_build_task_list (0);
204
205 /* Do not clear the thread list before adding the Ada task, to keep
206 the thread that the process stratum has included into it
207 (base_ptid) and the running thread, that may not have been included
208 to system.tasking.debug's list yet. */
209
210 iterate_over_live_ada_tasks (ravenscar_add_thread);
211 }
212
213 static ptid_t
214 ravenscar_running_thread (void)
215 {
216 CORE_ADDR tid = read_thread_id (running_thread_name);
217
218 if (tid == 0)
219 return null_ptid;
220 else
221 return ptid_build (ptid_get_pid (base_ptid), 0, tid);
222 }
223
224 static char *
225 ravenscar_extra_thread_info (struct thread_info *tp)
226 {
227 return "Ravenscar task";
228 }
229
230 static int
231 ravenscar_thread_alive (struct target_ops *ops, ptid_t ptid)
232 {
233 /* Ravenscar tasks are non-terminating. */
234 return 1;
235 }
236
237 static char *
238 ravenscar_pid_to_str (struct target_ops *ops, ptid_t ptid)
239 {
240 static char buf[30];
241
242 snprintf (buf, sizeof (buf), "Thread %#x", (int) ptid_get_tid (ptid));
243 return buf;
244 }
245
246 static void
247 ravenscar_fetch_registers (struct target_ops *ops,
248 struct regcache *regcache, int regnum)
249 {
250 struct target_ops *beneath = find_target_beneath (ops);
251
252 if (!ravenscar_runtime_initialized ()
253 || ptid_equal (inferior_ptid, base_magic_null_ptid)
254 || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
255 beneath->to_fetch_registers (beneath, regcache, regnum);
256 else
257 current_arch_ops->to_fetch_registers (regcache, regnum);
258 }
259
260 static void
261 ravenscar_store_registers (struct target_ops *ops,
262 struct regcache *regcache, int regnum)
263 {
264 struct target_ops *beneath = find_target_beneath (ops);
265
266 if (!ravenscar_runtime_initialized ()
267 || ptid_equal (inferior_ptid, base_magic_null_ptid)
268 || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
269 beneath->to_store_registers (beneath, regcache, regnum);
270 else
271 current_arch_ops->to_store_registers (regcache, regnum);
272 }
273
274 static void
275 ravenscar_prepare_to_store (struct regcache *regcache)
276 {
277 struct target_ops *beneath = find_target_beneath (&ravenscar_ops);
278
279 if (!ravenscar_runtime_initialized ()
280 || ptid_equal (inferior_ptid, base_magic_null_ptid)
281 || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
282 beneath->to_prepare_to_store (regcache);
283 else
284 current_arch_ops->to_prepare_to_store (regcache);
285 }
286
287 static void
288 ravenscar_mourn_inferior (struct target_ops *ops)
289 {
290 struct target_ops *beneath = find_target_beneath (&ravenscar_ops);
291
292 base_ptid = null_ptid;
293 beneath->to_mourn_inferior (beneath);
294 unpush_target (&ravenscar_ops);
295 }
296
297 /* Observer on inferior_created: push ravenscar thread stratum if needed. */
298
299 static void
300 ravenscar_inferior_created (struct target_ops *target, int from_tty)
301 {
302 if (ravenscar_task_support
303 && has_ravenscar_runtime ())
304 ravenscar_initialize (NULL, 0);
305 }
306
307 void
308 ravenscar_register_arch_ops (struct ravenscar_arch_ops *ops)
309 {
310 /* FIXME: To be clean, we would need to handle a list of
311 architectures, just like in remote-wtx-hw.c. However, for now the
312 only Ravenscar run-time for bare board that is implemented in
313 GNAT is for only one architecture: erc32-elf. So no need to care about
314 that for now...*/
315 current_arch_ops = ops;
316 }
317
318 /* Initialize Ravenscar support. */
319
320 static void
321 ravenscar_initialize (char *name, int from_tty)
322 {
323 if (ravenscar_is_open)
324 return;
325
326 base_magic_null_ptid = inferior_ptid;
327 ravenscar_update_inferior_ptid ();
328 push_target (&ravenscar_ops);
329 ravenscar_is_open = 1;
330 }
331
332 static ptid_t
333 ravenscar_get_ada_task_ptid (long lwp, long thread)
334 {
335 return ptid_build (ptid_get_pid (base_ptid), 0, thread);
336 }
337
338 static void
339 init_ravenscar_thread_ops (void)
340 {
341 ravenscar_ops.to_shortname = "ravenscar";
342 ravenscar_ops.to_longname = "Ravenscar tasks.";
343 ravenscar_ops.to_doc = "Ravenscar tasks support.";
344 ravenscar_ops.to_close = ravenscar_close;
345 ravenscar_ops.to_resume = ravenscar_resume;
346 ravenscar_ops.to_wait = ravenscar_wait;
347 ravenscar_ops.to_fetch_registers = ravenscar_fetch_registers;
348 ravenscar_ops.to_store_registers = ravenscar_store_registers;
349 ravenscar_ops.to_prepare_to_store = ravenscar_prepare_to_store;
350 ravenscar_ops.to_thread_alive = ravenscar_thread_alive;
351 ravenscar_ops.to_find_new_threads = ravenscar_find_new_threads;
352 ravenscar_ops.to_pid_to_str = ravenscar_pid_to_str;
353 ravenscar_ops.to_extra_thread_info = ravenscar_extra_thread_info;
354 ravenscar_ops.to_get_ada_task_ptid = ravenscar_get_ada_task_ptid;
355 ravenscar_ops.to_mourn_inferior = ravenscar_mourn_inferior;
356 ravenscar_ops.to_has_all_memory = default_child_has_all_memory;
357 ravenscar_ops.to_has_memory = default_child_has_memory;
358 ravenscar_ops.to_has_stack = default_child_has_stack;
359 ravenscar_ops.to_has_registers = default_child_has_registers;
360 ravenscar_ops.to_has_execution = default_child_has_execution;
361 ravenscar_ops.to_stratum = thread_stratum;
362 ravenscar_ops.to_magic = OPS_MAGIC;
363 }
364
365 /* Command-list for the "set/show ravenscar" prefix command. */
366 static struct cmd_list_element *set_ravenscar_list;
367 static struct cmd_list_element *show_ravenscar_list;
368
369 /* Implement the "set ravenscar" prefix command. */
370
371 static void
372 set_ravenscar_command (char *arg, int from_tty)
373 {
374 printf_unfiltered (_(\
375 "\"set ravenscar\" must be followed by the name of a setting.\n"));
376 help_list (set_ravenscar_list, "set ravenscar ", -1, gdb_stdout);
377 }
378
379 /* Implement the "show ravenscar" prefix command. */
380
381 static void
382 show_ravenscar_command (char *args, int from_tty)
383 {
384 cmd_show_list (show_ravenscar_list, from_tty, "");
385 }
386
387 /* Implement the "show ravenscar task-switching" command. */
388
389 static void
390 show_ravenscar_task_switching_command (struct ui_file *file, int from_tty,
391 struct cmd_list_element *c,
392 const char *value)
393 {
394 if (ravenscar_task_support)
395 fprintf_filtered (file, _("\
396 Support for Ravenscar task/thread switching is enabled"));
397 else
398 fprintf_filtered (file, _("\
399 Support for Ravenscar task/thread switching is disabled"));
400 }
401
402 /* Module startup initialization function, automagically called by
403 init.c. */
404
405 void
406 _initialize_ravenscar (void)
407 {
408 init_ravenscar_thread_ops ();
409 base_ptid = null_ptid;
410
411 /* Notice when the inferior is created in order to push the
412 ravenscar ops if needed. */
413 observer_attach_inferior_created (ravenscar_inferior_created);
414
415 add_target (&ravenscar_ops);
416
417 add_prefix_cmd ("ravenscar", no_class, set_ravenscar_command,
418 _("Prefix command for changing Ravenscar-specific settings"),
419 &set_ravenscar_list, "set ravenscar ", 0, &setlist);
420
421 add_prefix_cmd ("ravenscar", no_class, show_ravenscar_command,
422 _("Prefix command for showing Ravenscar-specific settings"),
423 &show_ravenscar_list, "set ravenscar ", 0, &showlist);
424
425 add_setshow_boolean_cmd ("task-switching", class_obscure,
426 &ravenscar_task_support, _("\
427 Enable or disable support for GNAT Ravenscar tasks"), _("\
428 Show whether support for GNAT Ravenscar tasks is enabled"),
429 _("\
430 Enable or disable support for task/thread switching with the GNAT\n\
431 Ravenscar run-time library for bareboard configuration."),
432 NULL, show_ravenscar_task_switching_command,
433 &set_ravenscar_list, &show_ravenscar_list);
434 }