gdbthread.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /* Multi-process/thread control defs for GDB, the GNU debugger.
  2. Copyright (C) 1987-2022 Free Software Foundation, Inc.
  3. Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef GDBTHREAD_H
  16. #define GDBTHREAD_H
  17. struct symtab;
  18. #include "breakpoint.h"
  19. #include "frame.h"
  20. #include "ui-out.h"
  21. #include "btrace.h"
  22. #include "target/waitstatus.h"
  23. #include "cli/cli-utils.h"
  24. #include "gdbsupport/refcounted-object.h"
  25. #include "gdbsupport/common-gdbthread.h"
  26. #include "gdbsupport/forward-scope-exit.h"
  27. #include "displaced-stepping.h"
  28. #include "gdbsupport/intrusive_list.h"
  29. #include "thread-fsm.h"
  30. struct inferior;
  31. struct process_stratum_target;
  32. /* When true, print debug messages related to GDB thread creation and
  33. deletion. */
  34. extern bool debug_threads;
  35. /* Print a "threads" debug statement. */
  36. #define threads_debug_printf(fmt, ...) \
  37. debug_prefixed_printf_cond (debug_threads, "threads", fmt, ##__VA_ARGS__)
  38. /* Frontend view of the thread state. Possible extensions: stepping,
  39. finishing, until(ling),...
  40. NOTE: Since the thread state is not a boolean, most times, you do
  41. not want to check it with negation. If you really want to check if
  42. the thread is stopped,
  43. use (good):
  44. if (tp->state == THREAD_STOPPED)
  45. instead of (bad):
  46. if (tp->state != THREAD_RUNNING)
  47. The latter is also true for exited threads, most likely not what
  48. you want. */
  49. enum thread_state
  50. {
  51. /* In the frontend's perpective, the thread is stopped. */
  52. THREAD_STOPPED,
  53. /* In the frontend's perpective, the thread is running. */
  54. THREAD_RUNNING,
  55. /* The thread is listed, but known to have exited. We keep it
  56. listed (but not visible) until it's safe to delete it. */
  57. THREAD_EXITED,
  58. };
  59. /* STEP_OVER_ALL means step over all subroutine calls.
  60. STEP_OVER_UNDEBUGGABLE means step over calls to undebuggable functions.
  61. STEP_OVER_NONE means don't step over any subroutine calls. */
  62. enum step_over_calls_kind
  63. {
  64. STEP_OVER_NONE,
  65. STEP_OVER_ALL,
  66. STEP_OVER_UNDEBUGGABLE
  67. };
  68. /* Inferior thread specific part of `struct infcall_control_state'.
  69. Inferior process counterpart is `struct inferior_control_state'. */
  70. struct thread_control_state
  71. {
  72. /* User/external stepping state. */
  73. /* Step-resume or longjmp-resume breakpoint. */
  74. struct breakpoint *step_resume_breakpoint = nullptr;
  75. /* Exception-resume breakpoint. */
  76. struct breakpoint *exception_resume_breakpoint = nullptr;
  77. /* Breakpoints used for software single stepping. Plural, because
  78. it may have multiple locations. E.g., if stepping over a
  79. conditional branch instruction we can't decode the condition for,
  80. we'll need to put a breakpoint at the branch destination, and
  81. another at the instruction after the branch. */
  82. struct breakpoint *single_step_breakpoints = nullptr;
  83. /* Range to single step within.
  84. If this is nonzero, respond to a single-step signal by continuing
  85. to step if the pc is in this range.
  86. If step_range_start and step_range_end are both 1, it means to
  87. step for a single instruction (FIXME: it might clean up
  88. wait_for_inferior in a minor way if this were changed to the
  89. address of the instruction and that address plus one. But maybe
  90. not). */
  91. CORE_ADDR step_range_start = 0; /* Inclusive */
  92. CORE_ADDR step_range_end = 0; /* Exclusive */
  93. /* Function the thread was in as of last it started stepping. */
  94. struct symbol *step_start_function = nullptr;
  95. /* If GDB issues a target step request, and this is nonzero, the
  96. target should single-step this thread once, and then continue
  97. single-stepping it without GDB core involvement as long as the
  98. thread stops in the step range above. If this is zero, the
  99. target should ignore the step range, and only issue one single
  100. step. */
  101. int may_range_step = 0;
  102. /* Stack frame address as of when stepping command was issued.
  103. This is how we know when we step into a subroutine call, and how
  104. to set the frame for the breakpoint used to step out. */
  105. struct frame_id step_frame_id {};
  106. /* Similarly, the frame ID of the underlying stack frame (skipping
  107. any inlined frames). */
  108. struct frame_id step_stack_frame_id {};
  109. /* True if the the thread is presently stepping over a breakpoint or
  110. a watchpoint, either with an inline step over or a displaced (out
  111. of line) step, and we're now expecting it to report a trap for
  112. the finished single step. */
  113. int trap_expected = 0;
  114. /* Nonzero if the thread is being proceeded for a "finish" command
  115. or a similar situation when return value should be printed. */
  116. int proceed_to_finish = 0;
  117. /* Nonzero if the thread is being proceeded for an inferior function
  118. call. */
  119. int in_infcall = 0;
  120. enum step_over_calls_kind step_over_calls = STEP_OVER_NONE;
  121. /* Nonzero if stopped due to a step command. */
  122. int stop_step = 0;
  123. /* Chain containing status of breakpoint(s) the thread stopped
  124. at. */
  125. bpstat *stop_bpstat = nullptr;
  126. /* Whether the command that started the thread was a stepping
  127. command. This is used to decide whether "set scheduler-locking
  128. step" behaves like "on" or "off". */
  129. int stepping_command = 0;
  130. };
  131. /* Inferior thread specific part of `struct infcall_suspend_state'. */
  132. struct thread_suspend_state
  133. {
  134. /* Last signal that the inferior received (why it stopped). When
  135. the thread is resumed, this signal is delivered. Note: the
  136. target should not check whether the signal is in pass state,
  137. because the signal may have been explicitly passed with the
  138. "signal" command, which overrides "handle nopass". If the signal
  139. should be suppressed, the core will take care of clearing this
  140. before the target is resumed. */
  141. enum gdb_signal stop_signal = GDB_SIGNAL_0;
  142. /* The reason the thread last stopped, if we need to track it
  143. (breakpoint, watchpoint, etc.) */
  144. enum target_stop_reason stop_reason = TARGET_STOPPED_BY_NO_REASON;
  145. /* The waitstatus for this thread's last event. */
  146. struct target_waitstatus waitstatus;
  147. /* If true WAITSTATUS hasn't been handled yet. */
  148. int waitstatus_pending_p = 0;
  149. /* Record the pc of the thread the last time it stopped. (This is
  150. not the current thread's PC as that may have changed since the
  151. last stop, e.g., "return" command, or "p $pc = 0xf000").
  152. - If the thread's PC has not changed since the thread last
  153. stopped, then proceed skips a breakpoint at the current PC,
  154. otherwise we let the thread run into the breakpoint.
  155. - If the thread has an unprocessed event pending, as indicated by
  156. waitstatus_pending_p, this is used in coordination with
  157. stop_reason: if the thread's PC has changed since the thread
  158. last stopped, a pending breakpoint waitstatus is discarded.
  159. - If the thread is running, then this field has its value removed by
  160. calling stop_pc.reset() (see thread_info::set_executing()).
  161. Attempting to read a gdb::optional with no value is undefined
  162. behaviour and will trigger an assertion error when _GLIBCXX_DEBUG is
  163. defined, which should make error easier to track down. */
  164. gdb::optional<CORE_ADDR> stop_pc;
  165. };
  166. /* Base class for target-specific thread data. */
  167. struct private_thread_info
  168. {
  169. virtual ~private_thread_info () = 0;
  170. };
  171. /* Threads are intrusively refcounted objects. Being the
  172. user-selected thread is normally considered an implicit strong
  173. reference and is thus not accounted in the refcount, unlike
  174. inferior objects. This is necessary, because there's no "current
  175. thread" pointer. Instead the current thread is inferred from the
  176. inferior_ptid global. However, when GDB needs to remember the
  177. selected thread to later restore it, GDB bumps the thread object's
  178. refcount, to prevent something deleting the thread object before
  179. reverting back (e.g., due to a "kill" command). If the thread
  180. meanwhile exits before being re-selected, then the thread object is
  181. left listed in the thread list, but marked with state
  182. THREAD_EXITED. (See scoped_restore_current_thread and
  183. delete_thread). All other thread references are considered weak
  184. references. Placing a thread in the thread list is an implicit
  185. strong reference, and is thus not accounted for in the thread's
  186. refcount.
  187. The intrusive_list_node base links threads in a per-inferior list. */
  188. class thread_info : public refcounted_object,
  189. public intrusive_list_node<thread_info>
  190. {
  191. public:
  192. explicit thread_info (inferior *inf, ptid_t ptid);
  193. ~thread_info ();
  194. bool deletable () const;
  195. /* Mark this thread as running and notify observers. */
  196. void set_running (bool running);
  197. ptid_t ptid; /* "Actual process id";
  198. In fact, this may be overloaded with
  199. kernel thread id, etc. */
  200. /* Each thread has two GDB IDs.
  201. a) The thread ID (Id). This consists of the pair of:
  202. - the number of the thread's inferior and,
  203. - the thread's thread number in its inferior, aka, the
  204. per-inferior thread number. This number is unique in the
  205. inferior but not unique between inferiors.
  206. b) The global ID (GId). This is a a single integer unique
  207. between all inferiors.
  208. E.g.:
  209. (gdb) info threads -gid
  210. Id GId Target Id Frame
  211. * 1.1 1 Thread A 0x16a09237 in foo () at foo.c:10
  212. 1.2 3 Thread B 0x15ebc6ed in bar () at foo.c:20
  213. 1.3 5 Thread C 0x15ebc6ed in bar () at foo.c:20
  214. 2.1 2 Thread A 0x16a09237 in foo () at foo.c:10
  215. 2.2 4 Thread B 0x15ebc6ed in bar () at foo.c:20
  216. 2.3 6 Thread C 0x15ebc6ed in bar () at foo.c:20
  217. Above, both inferiors 1 and 2 have threads numbered 1-3, but each
  218. thread has its own unique global ID. */
  219. /* The thread's global GDB thread number. This is exposed to MI,
  220. Python/Scheme, visible with "info threads -gid", and is also what
  221. the $_gthread convenience variable is bound to. */
  222. int global_num;
  223. /* The per-inferior thread number. This is unique in the inferior
  224. the thread belongs to, but not unique between inferiors. This is
  225. what the $_thread convenience variable is bound to. */
  226. int per_inf_num;
  227. /* The inferior this thread belongs to. */
  228. struct inferior *inf;
  229. /* The user-given name of the thread.
  230. Returns nullptr if the thread does not have a user-given name. */
  231. const char *name () const
  232. {
  233. return m_name.get ();
  234. }
  235. /* Set the user-given name of the thread.
  236. Pass nullptr to clear the name. */
  237. void set_name (gdb::unique_xmalloc_ptr<char> name)
  238. {
  239. m_name = std::move (name);
  240. }
  241. bool executing () const
  242. { return m_executing; }
  243. /* Set the thread's 'm_executing' field from EXECUTING, and if EXECUTING
  244. is true also clears the thread's stop_pc. */
  245. void set_executing (bool executing);
  246. bool resumed () const
  247. { return m_resumed; }
  248. /* Set the thread's 'm_resumed' field from RESUMED. The thread may also
  249. be added to (when RESUMED is true), or removed from (when RESUMED is
  250. false), the list of threads with a pending wait status. */
  251. void set_resumed (bool resumed);
  252. /* Frontend view of the thread state. Note that the THREAD_RUNNING/
  253. THREAD_STOPPED states are different from EXECUTING. When the
  254. thread is stopped internally while handling an internal event,
  255. like a software single-step breakpoint, EXECUTING will be false,
  256. but STATE will still be THREAD_RUNNING. */
  257. enum thread_state state = THREAD_STOPPED;
  258. /* State of GDB control of inferior thread execution.
  259. See `struct thread_control_state'. */
  260. thread_control_state control;
  261. /* Save M_SUSPEND to SUSPEND. */
  262. void save_suspend_to (thread_suspend_state &suspend) const
  263. {
  264. suspend = m_suspend;
  265. }
  266. /* Restore M_SUSPEND from SUSPEND. */
  267. void restore_suspend_from (const thread_suspend_state &suspend)
  268. {
  269. m_suspend = suspend;
  270. }
  271. /* Return this thread's stop PC. This should only be called when it is
  272. known that stop_pc has a value. If this function is being used in a
  273. situation where a thread may not have had a stop_pc assigned, then
  274. stop_pc_p() can be used to check if the stop_pc is defined. */
  275. CORE_ADDR stop_pc () const
  276. {
  277. gdb_assert (m_suspend.stop_pc.has_value ());
  278. return *m_suspend.stop_pc;
  279. }
  280. /* Set this thread's stop PC. */
  281. void set_stop_pc (CORE_ADDR stop_pc)
  282. {
  283. m_suspend.stop_pc = stop_pc;
  284. }
  285. /* Remove the stop_pc stored on this thread. */
  286. void clear_stop_pc ()
  287. {
  288. m_suspend.stop_pc.reset ();
  289. }
  290. /* Return true if this thread has a cached stop pc value, otherwise
  291. return false. */
  292. bool stop_pc_p () const
  293. {
  294. return m_suspend.stop_pc.has_value ();
  295. }
  296. /* Return true if this thread has a pending wait status. */
  297. bool has_pending_waitstatus () const
  298. {
  299. return m_suspend.waitstatus_pending_p;
  300. }
  301. /* Get this thread's pending wait status.
  302. May only be called if has_pending_waitstatus returns true. */
  303. const target_waitstatus &pending_waitstatus () const
  304. {
  305. gdb_assert (this->has_pending_waitstatus ());
  306. return m_suspend.waitstatus;
  307. }
  308. /* Set this thread's pending wait status.
  309. May only be called if has_pending_waitstatus returns false. */
  310. void set_pending_waitstatus (const target_waitstatus &ws);
  311. /* Clear this thread's pending wait status.
  312. May only be called if has_pending_waitstatus returns true. */
  313. void clear_pending_waitstatus ();
  314. /* Return this thread's stop signal. */
  315. gdb_signal stop_signal () const
  316. {
  317. return m_suspend.stop_signal;
  318. }
  319. /* Set this thread's stop signal. */
  320. void set_stop_signal (gdb_signal sig)
  321. {
  322. m_suspend.stop_signal = sig;
  323. }
  324. /* Return this thread's stop reason. */
  325. target_stop_reason stop_reason () const
  326. {
  327. return m_suspend.stop_reason;
  328. }
  329. /* Set this thread's stop reason. */
  330. void set_stop_reason (target_stop_reason reason)
  331. {
  332. m_suspend.stop_reason = reason;
  333. }
  334. /* Get the FSM associated with the thread. */
  335. struct thread_fsm *thread_fsm () const
  336. {
  337. return m_thread_fsm.get ();
  338. }
  339. /* Get the owning reference to the FSM associated with the thread.
  340. After a call to this method, "thread_fsm () == nullptr". */
  341. std::unique_ptr<struct thread_fsm> release_thread_fsm ()
  342. {
  343. return std::move (m_thread_fsm);
  344. }
  345. /* Set the FSM associated with the current thread.
  346. It is invalid to set the FSM if another FSM is already installed. */
  347. void set_thread_fsm (std::unique_ptr<struct thread_fsm> fsm)
  348. {
  349. gdb_assert (m_thread_fsm == nullptr);
  350. m_thread_fsm = std::move (fsm);
  351. }
  352. int current_line = 0;
  353. struct symtab *current_symtab = NULL;
  354. /* Internal stepping state. */
  355. /* Record the pc of the thread the last time it was resumed. (It
  356. can't be done on stop as the PC may change since the last stop,
  357. e.g., "return" command, or "p $pc = 0xf000"). This is maintained
  358. by proceed and keep_going, and among other things, it's used in
  359. adjust_pc_after_break to distinguish a hardware single-step
  360. SIGTRAP from a breakpoint SIGTRAP. */
  361. CORE_ADDR prev_pc = 0;
  362. /* Did we set the thread stepping a breakpoint instruction? This is
  363. used in conjunction with PREV_PC to decide whether to adjust the
  364. PC. */
  365. int stepped_breakpoint = 0;
  366. /* Should we step over breakpoint next time keep_going is called? */
  367. int stepping_over_breakpoint = 0;
  368. /* Should we step over a watchpoint next time keep_going is called?
  369. This is needed on targets with non-continuable, non-steppable
  370. watchpoints. */
  371. int stepping_over_watchpoint = 0;
  372. /* Set to TRUE if we should finish single-stepping over a breakpoint
  373. after hitting the current step-resume breakpoint. The context here
  374. is that GDB is to do `next' or `step' while signal arrives.
  375. When stepping over a breakpoint and signal arrives, GDB will attempt
  376. to skip signal handler, so it inserts a step_resume_breakpoint at the
  377. signal return address, and resume inferior.
  378. step_after_step_resume_breakpoint is set to TRUE at this moment in
  379. order to keep GDB in mind that there is still a breakpoint to step over
  380. when GDB gets back SIGTRAP from step_resume_breakpoint. */
  381. int step_after_step_resume_breakpoint = 0;
  382. /* This is used to remember when a fork or vfork event was caught by
  383. a catchpoint, and thus the event is to be followed at the next
  384. resume of the thread, and not immediately. */
  385. struct target_waitstatus pending_follow;
  386. /* True if this thread has been explicitly requested to stop. */
  387. int stop_requested = 0;
  388. /* The initiating frame of a nexting operation, used for deciding
  389. which exceptions to intercept. If it is null_frame_id no
  390. bp_longjmp or bp_exception but longjmp has been caught just for
  391. bp_longjmp_call_dummy. */
  392. struct frame_id initiating_frame = null_frame_id;
  393. /* Private data used by the target vector implementation. */
  394. std::unique_ptr<private_thread_info> priv;
  395. /* Branch trace information for this thread. */
  396. struct btrace_thread_info btrace {};
  397. /* Flag which indicates that the stack temporaries should be stored while
  398. evaluating expressions. */
  399. bool stack_temporaries_enabled = false;
  400. /* Values that are stored as temporaries on stack while evaluating
  401. expressions. */
  402. std::vector<struct value *> stack_temporaries;
  403. /* Step-over chain. A thread is in the step-over queue if this node is
  404. linked. */
  405. intrusive_list_node<thread_info> step_over_list_node;
  406. /* Node for list of threads that are resumed and have a pending wait status.
  407. The list head for this is in process_stratum_target, hence all threads in
  408. this list belong to that process target. */
  409. intrusive_list_node<thread_info> resumed_with_pending_wait_status_node;
  410. /* Displaced-step state for this thread. */
  411. displaced_step_thread_state displaced_step_state;
  412. private:
  413. /* True if this thread is resumed from infrun's perspective.
  414. Note that a thread can be marked both as not-executing and
  415. resumed at the same time. This happens if we try to resume a
  416. thread that has a wait status pending. We shouldn't let the
  417. thread really run until that wait status has been processed, but
  418. we should not process that wait status if we didn't try to let
  419. the thread run. */
  420. bool m_resumed = false;
  421. /* True means the thread is executing. Note: this is different
  422. from saying that there is an active target and we are stopped at
  423. a breakpoint, for instance. This is a real indicator whether the
  424. thread is off and running. */
  425. bool m_executing = false;
  426. /* State of inferior thread to restore after GDB is done with an inferior
  427. call. See `struct thread_suspend_state'. */
  428. thread_suspend_state m_suspend;
  429. /* The user-given name of the thread.
  430. Nullptr if the thread does not have a user-given name. */
  431. gdb::unique_xmalloc_ptr<char> m_name;
  432. /* Pointer to the state machine manager object that handles what is
  433. left to do for the thread's execution command after the target
  434. stops. Several execution commands use it. */
  435. std::unique_ptr<struct thread_fsm> m_thread_fsm;
  436. };
  437. using thread_info_resumed_with_pending_wait_status_node
  438. = intrusive_member_node<thread_info,
  439. &thread_info::resumed_with_pending_wait_status_node>;
  440. using thread_info_resumed_with_pending_wait_status_list
  441. = intrusive_list<thread_info,
  442. thread_info_resumed_with_pending_wait_status_node>;
  443. /* A gdb::ref_ptr pointer to a thread_info. */
  444. using thread_info_ref
  445. = gdb::ref_ptr<struct thread_info, refcounted_object_ref_policy>;
  446. /* A gdb::ref_ptr pointer to an inferior. This would ideally be in
  447. inferior.h, but it can't due to header dependencies (inferior.h
  448. includes gdbthread.h). */
  449. using inferior_ref
  450. = gdb::ref_ptr<struct inferior, refcounted_object_ref_policy>;
  451. /* Create an empty thread list, or empty the existing one. */
  452. extern void init_thread_list (void);
  453. /* Add a thread to the thread list, print a message
  454. that a new thread is found, and return the pointer to
  455. the new thread. Caller my use this pointer to
  456. initialize the private thread data. */
  457. extern struct thread_info *add_thread (process_stratum_target *targ,
  458. ptid_t ptid);
  459. /* Same as add_thread, but does not print a message about new
  460. thread. */
  461. extern struct thread_info *add_thread_silent (process_stratum_target *targ,
  462. ptid_t ptid);
  463. /* Same as add_thread, and sets the private info. */
  464. extern struct thread_info *add_thread_with_info (process_stratum_target *targ,
  465. ptid_t ptid,
  466. private_thread_info *);
  467. /* Delete thread THREAD and notify of thread exit. If the thread is
  468. currently not deletable, don't actually delete it but still tag it
  469. as exited and do the notification. */
  470. extern void delete_thread (struct thread_info *thread);
  471. /* Like delete_thread, but be quiet about it. Used when the process
  472. this thread belonged to has already exited, for example. */
  473. extern void delete_thread_silent (struct thread_info *thread);
  474. /* Mark the thread exited, but don't delete it or remove it from the
  475. inferior thread list. */
  476. extern void set_thread_exited (thread_info *tp, bool silent);
  477. /* Delete a step_resume_breakpoint from the thread database. */
  478. extern void delete_step_resume_breakpoint (struct thread_info *);
  479. /* Delete an exception_resume_breakpoint from the thread database. */
  480. extern void delete_exception_resume_breakpoint (struct thread_info *);
  481. /* Delete the single-step breakpoints of thread TP, if any. */
  482. extern void delete_single_step_breakpoints (struct thread_info *tp);
  483. /* Check if the thread has software single stepping breakpoints
  484. set. */
  485. extern int thread_has_single_step_breakpoints_set (struct thread_info *tp);
  486. /* Check whether the thread has software single stepping breakpoints
  487. set at PC. */
  488. extern int thread_has_single_step_breakpoint_here (struct thread_info *tp,
  489. const address_space *aspace,
  490. CORE_ADDR addr);
  491. /* Returns whether to show inferior-qualified thread IDs, or plain
  492. thread numbers. Inferior-qualified IDs are shown whenever we have
  493. multiple inferiors, or the only inferior left has number > 1. */
  494. extern int show_inferior_qualified_tids (void);
  495. /* Return a string version of THR's thread ID. If there are multiple
  496. inferiors, then this prints the inferior-qualifier form, otherwise
  497. it only prints the thread number. The result is stored in a
  498. circular static buffer, NUMCELLS deep. */
  499. const char *print_thread_id (struct thread_info *thr);
  500. /* Boolean test for an already-known ptid. */
  501. extern bool in_thread_list (process_stratum_target *targ, ptid_t ptid);
  502. /* Boolean test for an already-known global thread id (GDB's homegrown
  503. global id, not the system's). */
  504. extern int valid_global_thread_id (int global_id);
  505. /* Find (non-exited) thread PTID of inferior INF. */
  506. extern thread_info *find_thread_ptid (inferior *inf, ptid_t ptid);
  507. /* Search function to lookup a (non-exited) thread by 'ptid'. */
  508. extern struct thread_info *find_thread_ptid (process_stratum_target *targ,
  509. ptid_t ptid);
  510. /* Find thread by GDB global thread ID. */
  511. struct thread_info *find_thread_global_id (int global_id);
  512. /* Find thread by thread library specific handle in inferior INF. */
  513. struct thread_info *find_thread_by_handle
  514. (gdb::array_view<const gdb_byte> handle, struct inferior *inf);
  515. /* Finds the first thread of the specified inferior. */
  516. extern struct thread_info *first_thread_of_inferior (inferior *inf);
  517. /* Returns any thread of inferior INF, giving preference to the
  518. current thread. */
  519. extern struct thread_info *any_thread_of_inferior (inferior *inf);
  520. /* Returns any non-exited thread of inferior INF, giving preference to
  521. the current thread, and to not executing threads. */
  522. extern struct thread_info *any_live_thread_of_inferior (inferior *inf);
  523. /* Change the ptid of thread OLD_PTID to NEW_PTID. */
  524. void thread_change_ptid (process_stratum_target *targ,
  525. ptid_t old_ptid, ptid_t new_ptid);
  526. /* Iterator function to call a user-provided callback function
  527. once for each known thread. */
  528. typedef int (*thread_callback_func) (struct thread_info *, void *);
  529. extern struct thread_info *iterate_over_threads (thread_callback_func, void *);
  530. /* Pull in the internals of the inferiors/threads ranges and
  531. iterators. Must be done after struct thread_info is defined. */
  532. #include "thread-iter.h"
  533. /* Return a range that can be used to walk over threads, with
  534. range-for.
  535. Used like this, it walks over all threads of all inferiors of all
  536. targets:
  537. for (thread_info *thr : all_threads ())
  538. { .... }
  539. FILTER_PTID can be used to filter out threads that don't match.
  540. FILTER_PTID can be:
  541. - minus_one_ptid, meaning walk all threads of all inferiors of
  542. PROC_TARGET. If PROC_TARGET is NULL, then of all targets.
  543. - A process ptid, in which case walk all threads of the specified
  544. process. PROC_TARGET must be non-NULL in this case.
  545. - A thread ptid, in which case walk that thread only. PROC_TARGET
  546. must be non-NULL in this case.
  547. */
  548. inline all_matching_threads_range
  549. all_threads (process_stratum_target *proc_target = nullptr,
  550. ptid_t filter_ptid = minus_one_ptid)
  551. {
  552. return all_matching_threads_range (proc_target, filter_ptid);
  553. }
  554. /* Return a range that can be used to walk over all non-exited threads
  555. of all inferiors, with range-for. Arguments are like all_threads
  556. above. */
  557. inline all_non_exited_threads_range
  558. all_non_exited_threads (process_stratum_target *proc_target = nullptr,
  559. ptid_t filter_ptid = minus_one_ptid)
  560. {
  561. return all_non_exited_threads_range (proc_target, filter_ptid);
  562. }
  563. /* Return a range that can be used to walk over all threads of all
  564. inferiors, with range-for, safely. I.e., it is safe to delete the
  565. currently-iterated thread. When combined with range-for, this
  566. allow convenient patterns like this:
  567. for (thread_info *t : all_threads_safe ())
  568. if (some_condition ())
  569. delete f;
  570. */
  571. inline all_threads_safe_range
  572. all_threads_safe ()
  573. {
  574. return all_threads_safe_range (all_threads_iterator::begin_t {});
  575. }
  576. extern int thread_count (process_stratum_target *proc_target);
  577. /* Return true if we have any thread in any inferior. */
  578. extern bool any_thread_p ();
  579. /* Switch context to thread THR. Also sets the STOP_PC global. */
  580. extern void switch_to_thread (struct thread_info *thr);
  581. /* Switch context to no thread selected. */
  582. extern void switch_to_no_thread ();
  583. /* Switch from one thread to another. Does not read registers. */
  584. extern void switch_to_thread_no_regs (struct thread_info *thread);
  585. /* Marks or clears thread(s) PTID of TARG as resumed. If PTID is
  586. MINUS_ONE_PTID, applies to all threads of TARG. If
  587. ptid_is_pid(PTID) is true, applies to all threads of the process
  588. pointed at by {TARG,PTID}. */
  589. extern void set_resumed (process_stratum_target *targ,
  590. ptid_t ptid, bool resumed);
  591. /* Marks thread PTID of TARG as running, or as stopped. If PTID is
  592. minus_one_ptid, marks all threads of TARG. */
  593. extern void set_running (process_stratum_target *targ,
  594. ptid_t ptid, bool running);
  595. /* Marks or clears thread(s) PTID of TARG as having been requested to
  596. stop. If PTID is MINUS_ONE_PTID, applies to all threads of TARG.
  597. If ptid_is_pid(PTID) is true, applies to all threads of the process
  598. pointed at by {TARG, PTID}. If STOP, then the
  599. THREAD_STOP_REQUESTED observer is called with PTID as argument. */
  600. extern void set_stop_requested (process_stratum_target *targ,
  601. ptid_t ptid, bool stop);
  602. /* Marks thread PTID of TARG as executing, or not. If PTID is
  603. minus_one_ptid, marks all threads of TARG.
  604. Note that this is different from the running state. See the
  605. description of state and executing fields of struct
  606. thread_info. */
  607. extern void set_executing (process_stratum_target *targ,
  608. ptid_t ptid, bool executing);
  609. /* True if any (known or unknown) thread of TARG is or may be
  610. executing. */
  611. extern bool threads_are_executing (process_stratum_target *targ);
  612. /* Merge the executing property of thread PTID of TARG over to its
  613. thread state property (frontend running/stopped view).
  614. "not executing" -> "stopped"
  615. "executing" -> "running"
  616. "exited" -> "exited"
  617. If PTID is minus_one_ptid, go over all threads of TARG.
  618. Notifications are only emitted if the thread state did change. */
  619. extern void finish_thread_state (process_stratum_target *targ, ptid_t ptid);
  620. /* Calls finish_thread_state on scope exit, unless release() is called
  621. to disengage. */
  622. using scoped_finish_thread_state
  623. = FORWARD_SCOPE_EXIT (finish_thread_state);
  624. /* Commands with a prefix of `thread'. */
  625. extern struct cmd_list_element *thread_cmd_list;
  626. extern void thread_command (const char *tidstr, int from_tty);
  627. /* Print notices on thread events (attach, detach, etc.), set with
  628. `set print thread-events'. */
  629. extern bool print_thread_events;
  630. /* Prints the list of threads and their details on UIOUT. If
  631. REQUESTED_THREADS, a list of GDB ids/ranges, is not NULL, only
  632. print threads whose ID is included in the list. If PID is not -1,
  633. only print threads from the process PID. Otherwise, threads from
  634. all attached PIDs are printed. If both REQUESTED_THREADS is not
  635. NULL and PID is not -1, then the thread is printed if it belongs to
  636. the specified process. Otherwise, an error is raised. */
  637. extern void print_thread_info (struct ui_out *uiout,
  638. const char *requested_threads,
  639. int pid);
  640. /* Save/restore current inferior/thread/frame. */
  641. class scoped_restore_current_thread
  642. {
  643. public:
  644. scoped_restore_current_thread ();
  645. ~scoped_restore_current_thread ();
  646. DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread);
  647. /* Cancel restoring on scope exit. */
  648. void dont_restore () { m_dont_restore = true; }
  649. private:
  650. void restore ();
  651. bool m_dont_restore = false;
  652. thread_info_ref m_thread;
  653. inferior_ref m_inf;
  654. frame_id m_selected_frame_id;
  655. int m_selected_frame_level;
  656. bool m_was_stopped;
  657. /* Save/restore the language as well, because selecting a frame
  658. changes the current language to the frame's language if "set
  659. language auto". */
  660. enum language m_lang;
  661. };
  662. /* Returns a pointer into the thread_info corresponding to
  663. INFERIOR_PTID. INFERIOR_PTID *must* be in the thread list. */
  664. extern struct thread_info* inferior_thread (void);
  665. extern void update_thread_list (void);
  666. /* Delete any thread the target says is no longer alive. */
  667. extern void prune_threads (void);
  668. /* Delete threads marked THREAD_EXITED. Unlike prune_threads, this
  669. does not consult the target about whether the thread is alive right
  670. now. */
  671. extern void delete_exited_threads (void);
  672. /* Return true if PC is in the stepping range of THREAD. */
  673. int pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread);
  674. /* Enable storing stack temporaries for thread THR and disable and
  675. clear the stack temporaries on destruction. Holds a strong
  676. reference to THR. */
  677. class enable_thread_stack_temporaries
  678. {
  679. public:
  680. explicit enable_thread_stack_temporaries (struct thread_info *thr)
  681. : m_thr (thread_info_ref::new_reference (thr))
  682. {
  683. m_thr->stack_temporaries_enabled = true;
  684. m_thr->stack_temporaries.clear ();
  685. }
  686. ~enable_thread_stack_temporaries ()
  687. {
  688. m_thr->stack_temporaries_enabled = false;
  689. m_thr->stack_temporaries.clear ();
  690. }
  691. DISABLE_COPY_AND_ASSIGN (enable_thread_stack_temporaries);
  692. private:
  693. thread_info_ref m_thr;
  694. };
  695. extern bool thread_stack_temporaries_enabled_p (struct thread_info *tp);
  696. extern void push_thread_stack_temporary (struct thread_info *tp, struct value *v);
  697. extern value *get_last_thread_stack_temporary (struct thread_info *tp);
  698. extern bool value_in_thread_stack_temporaries (struct value *,
  699. struct thread_info *thr);
  700. /* Thread step-over list type. */
  701. using thread_step_over_list_node
  702. = intrusive_member_node<thread_info, &thread_info::step_over_list_node>;
  703. using thread_step_over_list
  704. = intrusive_list<thread_info, thread_step_over_list_node>;
  705. using thread_step_over_list_iterator
  706. = reference_to_pointer_iterator<thread_step_over_list::iterator>;
  707. using thread_step_over_list_safe_iterator
  708. = basic_safe_iterator<thread_step_over_list_iterator>;
  709. using thread_step_over_list_safe_range
  710. = iterator_range<thread_step_over_list_safe_iterator>;
  711. static inline thread_step_over_list_safe_range
  712. make_thread_step_over_list_safe_range (thread_step_over_list &list)
  713. {
  714. return thread_step_over_list_safe_range
  715. (thread_step_over_list_safe_iterator (list.begin (),
  716. list.end ()),
  717. thread_step_over_list_safe_iterator (list.end (),
  718. list.end ()));
  719. }
  720. /* Add TP to the end of the global pending step-over chain. */
  721. extern void global_thread_step_over_chain_enqueue (thread_info *tp);
  722. /* Append the thread step over list LIST to the global thread step over
  723. chain. */
  724. extern void global_thread_step_over_chain_enqueue_chain
  725. (thread_step_over_list &&list);
  726. /* Remove TP from the global pending step-over chain. */
  727. extern void global_thread_step_over_chain_remove (thread_info *tp);
  728. /* Return true if TP is in any step-over chain. */
  729. extern int thread_is_in_step_over_chain (struct thread_info *tp);
  730. /* Return the length of the the step over chain TP is in.
  731. If TP is non-nullptr, the thread must be in a step over chain.
  732. TP may be nullptr, in which case it denotes an empty list, so a length of
  733. 0. */
  734. extern int thread_step_over_chain_length (const thread_step_over_list &l);
  735. /* Cancel any ongoing execution command. */
  736. extern void thread_cancel_execution_command (struct thread_info *thr);
  737. /* Check whether it makes sense to access a register of the current
  738. thread at this point. If not, throw an error (e.g., the thread is
  739. executing). */
  740. extern void validate_registers_access (void);
  741. /* Check whether it makes sense to access a register of THREAD at this point.
  742. Returns true if registers may be accessed; false otherwise. */
  743. extern bool can_access_registers_thread (struct thread_info *thread);
  744. /* Returns whether to show which thread hit the breakpoint, received a
  745. signal, etc. and ended up causing a user-visible stop. This is
  746. true iff we ever detected multiple threads. */
  747. extern int show_thread_that_caused_stop (void);
  748. /* Print the message for a thread or/and frame selected. */
  749. extern void print_selected_thread_frame (struct ui_out *uiout,
  750. user_selected_what selection);
  751. /* Helper for the CLI's "thread" command and for MI's -thread-select.
  752. Selects thread THR. TIDSTR is the original string the thread ID
  753. was parsed from. This is used in the error message if THR is not
  754. alive anymore. */
  755. extern void thread_select (const char *tidstr, class thread_info *thr);
  756. /* Return THREAD's name.
  757. If THREAD has a user-given name, return it. Otherwise, query the thread's
  758. target to get the name. May return nullptr. */
  759. extern const char *thread_name (thread_info *thread);
  760. /* Switch to thread TP if it is alive. Returns true if successfully
  761. switched, false otherwise. */
  762. extern bool switch_to_thread_if_alive (thread_info *thr);
  763. /* Assuming that THR is the current thread, execute CMD.
  764. If ADA_TASK is not empty, it is the Ada task ID, and will
  765. be printed instead of the thread information.
  766. FLAGS.QUIET controls the printing of the thread information.
  767. FLAGS.CONT and FLAGS.SILENT control how to handle errors. Can throw an
  768. exception if !FLAGS.SILENT and !FLAGS.CONT and CMD fails. */
  769. extern void thread_try_catch_cmd (thread_info *thr,
  770. gdb::optional<int> ada_task,
  771. const char *cmd, int from_tty,
  772. const qcs_flags &flags);
  773. /* Return a string representation of STATE. */
  774. extern const char *thread_state_string (enum thread_state state);
  775. #endif /* GDBTHREAD_H */