ravenscar-thread.c 22 KB

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