]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Multi-process/thread control for GDB, the GNU debugger. |
8926118c | 2 | |
213516ef | 3 | Copyright (C) 1986-2023 Free Software Foundation, Inc. |
8926118c | 4 | |
b6ba6518 | 5 | Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "symtab.h" | |
24 | #include "frame.h" | |
25 | #include "inferior.h" | |
268a13a5 | 26 | #include "gdbsupport/environ.h" |
c906108c SS |
27 | #include "value.h" |
28 | #include "target.h" | |
29 | #include "gdbthread.h" | |
30 | #include "command.h" | |
31 | #include "gdbcmd.h" | |
4e052eda | 32 | #include "regcache.h" |
02d27625 | 33 | #include "btrace.h" |
c906108c SS |
34 | |
35 | #include <ctype.h> | |
36 | #include <sys/types.h> | |
37 | #include <signal.h> | |
8b93c638 | 38 | #include "ui-out.h" |
76727919 | 39 | #include "observable.h" |
d4fc5b1e | 40 | #include "annotate.h" |
94cc34af | 41 | #include "cli/cli-decode.h" |
6665660a | 42 | #include "cli/cli-option.h" |
d322d6d6 | 43 | #include "gdbsupport/gdb_regex.h" |
aea5b279 | 44 | #include "cli/cli-utils.h" |
243a9253 | 45 | #include "thread-fsm.h" |
5d5658a1 | 46 | #include "tid-parse.h" |
c6609450 | 47 | #include <algorithm> |
268a13a5 | 48 | #include "gdbsupport/gdb_optional.h" |
08036331 | 49 | #include "inline-frame.h" |
5d707134 | 50 | #include "stack.h" |
94cc34af | 51 | |
5b0a3d62 AB |
52 | /* See gdbthread.h. */ |
53 | ||
54 | bool debug_threads = false; | |
55 | ||
56 | /* Implement 'show debug threads'. */ | |
57 | ||
58 | static void | |
59 | show_debug_threads (struct ui_file *file, int from_tty, | |
60 | struct cmd_list_element *c, const char *value) | |
61 | { | |
6cb06a8c | 62 | gdb_printf (file, _("Thread debugging is \"%s\".\n"), value); |
5b0a3d62 AB |
63 | } |
64 | ||
c378eb4e | 65 | /* Definition of struct thread_info exported to gdbthread.h. */ |
c906108c | 66 | |
c378eb4e | 67 | /* Prototypes for local functions. */ |
c906108c | 68 | |
c906108c SS |
69 | static int highest_thread_num; |
70 | ||
3922b302 PA |
71 | /* The current/selected thread. */ |
72 | static thread_info *current_thread_; | |
73 | ||
3922b302 PA |
74 | /* Returns true if THR is the current thread. */ |
75 | ||
76 | static bool | |
77 | is_current_thread (const thread_info *thr) | |
78 | { | |
79 | return thr == current_thread_; | |
80 | } | |
054e8d9e | 81 | |
a5321aa4 | 82 | struct thread_info* |
4e1c45ea | 83 | inferior_thread (void) |
8601f500 | 84 | { |
3922b302 PA |
85 | gdb_assert (current_thread_ != nullptr); |
86 | return current_thread_; | |
4e1c45ea | 87 | } |
8601f500 | 88 | |
5b834a0a PA |
89 | /* Delete the breakpoint pointed at by BP_P, if there's one. */ |
90 | ||
91 | static void | |
92 | delete_thread_breakpoint (struct breakpoint **bp_p) | |
4e1c45ea | 93 | { |
5b834a0a | 94 | if (*bp_p != NULL) |
8601f500 | 95 | { |
5b834a0a PA |
96 | delete_breakpoint (*bp_p); |
97 | *bp_p = NULL; | |
8601f500 MS |
98 | } |
99 | } | |
100 | ||
5b834a0a PA |
101 | void |
102 | delete_step_resume_breakpoint (struct thread_info *tp) | |
103 | { | |
104 | if (tp != NULL) | |
105 | delete_thread_breakpoint (&tp->control.step_resume_breakpoint); | |
106 | } | |
107 | ||
186c406b TT |
108 | void |
109 | delete_exception_resume_breakpoint (struct thread_info *tp) | |
110 | { | |
5b834a0a PA |
111 | if (tp != NULL) |
112 | delete_thread_breakpoint (&tp->control.exception_resume_breakpoint); | |
113 | } | |
114 | ||
34b7e8a6 PA |
115 | /* See gdbthread.h. */ |
116 | ||
117 | void | |
118 | delete_single_step_breakpoints (struct thread_info *tp) | |
119 | { | |
120 | if (tp != NULL) | |
121 | delete_thread_breakpoint (&tp->control.single_step_breakpoints); | |
122 | } | |
123 | ||
5b834a0a PA |
124 | /* Delete the breakpoint pointed at by BP_P at the next stop, if |
125 | there's one. */ | |
126 | ||
127 | static void | |
128 | delete_at_next_stop (struct breakpoint **bp) | |
129 | { | |
130 | if (*bp != NULL) | |
186c406b | 131 | { |
5b834a0a PA |
132 | (*bp)->disposition = disp_del_at_next_stop; |
133 | *bp = NULL; | |
186c406b TT |
134 | } |
135 | } | |
136 | ||
34b7e8a6 PA |
137 | /* See gdbthread.h. */ |
138 | ||
139 | int | |
140 | thread_has_single_step_breakpoints_set (struct thread_info *tp) | |
141 | { | |
142 | return tp->control.single_step_breakpoints != NULL; | |
143 | } | |
144 | ||
145 | /* See gdbthread.h. */ | |
146 | ||
147 | int | |
148 | thread_has_single_step_breakpoint_here (struct thread_info *tp, | |
accd0bcd | 149 | const address_space *aspace, |
34b7e8a6 PA |
150 | CORE_ADDR addr) |
151 | { | |
152 | struct breakpoint *ss_bps = tp->control.single_step_breakpoints; | |
153 | ||
154 | return (ss_bps != NULL | |
155 | && breakpoint_has_location_inserted_here (ss_bps, aspace, addr)); | |
156 | } | |
157 | ||
243a9253 PA |
158 | /* See gdbthread.h. */ |
159 | ||
160 | void | |
161 | thread_cancel_execution_command (struct thread_info *thr) | |
162 | { | |
573269a8 | 163 | if (thr->thread_fsm () != nullptr) |
243a9253 | 164 | { |
573269a8 LS |
165 | std::unique_ptr<thread_fsm> fsm = thr->release_thread_fsm (); |
166 | fsm->clean_up (thr); | |
243a9253 PA |
167 | } |
168 | } | |
169 | ||
7c952b6d | 170 | static void |
4f8d22e3 | 171 | clear_thread_inferior_resources (struct thread_info *tp) |
7c952b6d ND |
172 | { |
173 | /* NOTE: this will take care of any left-over step_resume breakpoints, | |
4d8453a5 DJ |
174 | but not any user-specified thread-specific breakpoints. We can not |
175 | delete the breakpoint straight-off, because the inferior might not | |
176 | be stopped at the moment. */ | |
5b834a0a PA |
177 | delete_at_next_stop (&tp->control.step_resume_breakpoint); |
178 | delete_at_next_stop (&tp->control.exception_resume_breakpoint); | |
34b7e8a6 | 179 | delete_at_next_stop (&tp->control.single_step_breakpoints); |
186c406b | 180 | |
5d5658a1 | 181 | delete_longjmp_breakpoint_at_next_stop (tp->global_num); |
f59f708a | 182 | |
16c381f0 | 183 | bpstat_clear (&tp->control.stop_bpstat); |
95e54da7 | 184 | |
02d27625 MM |
185 | btrace_teardown (tp); |
186 | ||
243a9253 | 187 | thread_cancel_execution_command (tp); |
08036331 | 188 | |
5b6d1e4f | 189 | clear_inline_frame_state (tp); |
4f8d22e3 PA |
190 | } |
191 | ||
bf809310 | 192 | /* See gdbthread.h. */ |
803bdfe4 | 193 | |
bf809310 | 194 | void |
3922b302 | 195 | set_thread_exited (thread_info *tp, bool silent) |
803bdfe4 | 196 | { |
28d5518b | 197 | /* Dead threads don't need to step-over. Remove from chain. */ |
8b6a69b2 | 198 | if (thread_is_in_step_over_chain (tp)) |
28d5518b | 199 | global_thread_step_over_chain_remove (tp); |
803bdfe4 YQ |
200 | |
201 | if (tp->state != THREAD_EXITED) | |
202 | { | |
a66f7298 SM |
203 | process_stratum_target *proc_target = tp->inf->process_target (); |
204 | ||
205 | /* Some targets unpush themselves from the inferior's target stack before | |
206 | clearing the inferior's thread list (which marks all threads as exited, | |
207 | and therefore leads to this function). In this case, the inferior's | |
208 | process target will be nullptr when we arrive here. | |
209 | ||
210 | See also the comment in inferior::unpush_target. */ | |
211 | if (proc_target != nullptr) | |
212 | proc_target->maybe_remove_resumed_with_pending_wait_status (tp); | |
213 | ||
76727919 | 214 | gdb::observers::thread_exit.notify (tp, silent); |
803bdfe4 YQ |
215 | |
216 | /* Tag it as exited. */ | |
217 | tp->state = THREAD_EXITED; | |
218 | ||
219 | /* Clear breakpoints, etc. associated with this thread. */ | |
220 | clear_thread_inferior_resources (tp); | |
922cc93d SM |
221 | |
222 | /* Remove from the ptid_t map. We don't want for | |
223 | find_thread_ptid to find exited threads. Also, the target | |
224 | may reuse the ptid for a new thread, and there can only be | |
225 | one value per key; adding a new thread with the same ptid_t | |
226 | would overwrite the exited thread's ptid entry. */ | |
227 | size_t nr_deleted = tp->inf->ptid_thread_map.erase (tp->ptid); | |
228 | gdb_assert (nr_deleted == 1); | |
803bdfe4 YQ |
229 | } |
230 | } | |
231 | ||
c906108c | 232 | void |
fba45db2 | 233 | init_thread_list (void) |
c906108c | 234 | { |
7c952b6d | 235 | highest_thread_num = 0; |
8ea051c5 | 236 | |
bf809310 PA |
237 | for (inferior *inf : all_inferiors ()) |
238 | inf->clear_thread_list (true); | |
c906108c SS |
239 | } |
240 | ||
5d5658a1 PA |
241 | /* Allocate a new thread of inferior INF with target id PTID and add |
242 | it to the thread list. */ | |
e58b0e63 PA |
243 | |
244 | static struct thread_info * | |
5d5658a1 | 245 | new_thread (struct inferior *inf, ptid_t ptid) |
e58b0e63 | 246 | { |
12316564 | 247 | thread_info *tp = new thread_info (inf, ptid); |
b05b1202 | 248 | |
5b0a3d62 AB |
249 | threads_debug_printf ("creating a new thread object, inferior %d, ptid %s", |
250 | inf->num, ptid.to_string ().c_str ()); | |
251 | ||
bf809310 | 252 | inf->thread_list.push_back (*tp); |
e58b0e63 | 253 | |
922cc93d SM |
254 | /* A thread with this ptid should not exist in the map yet. */ |
255 | gdb_assert (inf->ptid_thread_map.find (ptid) == inf->ptid_thread_map.end ()); | |
256 | ||
257 | inf->ptid_thread_map[ptid] = tp; | |
258 | ||
e58b0e63 PA |
259 | return tp; |
260 | } | |
261 | ||
0d06e24b | 262 | struct thread_info * |
5b6d1e4f | 263 | add_thread_silent (process_stratum_target *targ, ptid_t ptid) |
c906108c | 264 | { |
f53fc427 SM |
265 | gdb_assert (targ != nullptr); |
266 | ||
3922b302 | 267 | inferior *inf = find_inferior_ptid (targ, ptid); |
5b6d1e4f | 268 | |
5b0a3d62 AB |
269 | threads_debug_printf ("add thread to inferior %d, ptid %s, target %s", |
270 | inf->num, ptid.to_string ().c_str (), | |
271 | targ->shortname ()); | |
272 | ||
3922b302 PA |
273 | /* We may have an old thread with the same id in the thread list. |
274 | If we do, it must be dead, otherwise we wouldn't be adding a new | |
275 | thread with the same id. The OS is reusing this id --- delete | |
276 | the old thread, and create a new one. */ | |
277 | thread_info *tp = find_thread_ptid (inf, ptid); | |
278 | if (tp != nullptr) | |
279 | delete_thread (tp); | |
4f8d22e3 | 280 | |
5d5658a1 | 281 | tp = new_thread (inf, ptid); |
76727919 | 282 | gdb::observers::new_thread.notify (tp); |
cfc01461 | 283 | |
0d06e24b | 284 | return tp; |
c906108c SS |
285 | } |
286 | ||
93815fbf | 287 | struct thread_info * |
5b6d1e4f PA |
288 | add_thread_with_info (process_stratum_target *targ, ptid_t ptid, |
289 | private_thread_info *priv) | |
93815fbf | 290 | { |
5b6d1e4f | 291 | thread_info *result = add_thread_silent (targ, ptid); |
93815fbf | 292 | |
7aabaf9d | 293 | result->priv.reset (priv); |
17faa917 | 294 | |
93815fbf | 295 | if (print_thread_events) |
6cb06a8c | 296 | gdb_printf (_("[New %s]\n"), target_pid_to_str (ptid).c_str ()); |
d4fc5b1e NR |
297 | |
298 | annotate_new_thread (); | |
93815fbf VP |
299 | return result; |
300 | } | |
301 | ||
17faa917 | 302 | struct thread_info * |
5b6d1e4f | 303 | add_thread (process_stratum_target *targ, ptid_t ptid) |
17faa917 | 304 | { |
5b6d1e4f | 305 | return add_thread_with_info (targ, ptid, NULL); |
17faa917 DJ |
306 | } |
307 | ||
7aabaf9d SM |
308 | private_thread_info::~private_thread_info () = default; |
309 | ||
12316564 YQ |
310 | thread_info::thread_info (struct inferior *inf_, ptid_t ptid_) |
311 | : ptid (ptid_), inf (inf_) | |
312 | { | |
313 | gdb_assert (inf_ != NULL); | |
314 | ||
315 | this->global_num = ++highest_thread_num; | |
316 | this->per_inf_num = ++inf_->highest_thread_num; | |
317 | ||
318 | /* Nothing to follow yet. */ | |
183be222 | 319 | this->pending_follow.set_spurious (); |
12316564 YQ |
320 | } |
321 | ||
08036331 PA |
322 | /* See gdbthread.h. */ |
323 | ||
5b0a3d62 AB |
324 | thread_info::~thread_info () |
325 | { | |
326 | threads_debug_printf ("thread %s", this->ptid.to_string ().c_str ()); | |
327 | } | |
328 | ||
329 | /* See gdbthread.h. */ | |
330 | ||
08036331 PA |
331 | bool |
332 | thread_info::deletable () const | |
333 | { | |
334 | /* If this is the current thread, or there's code out there that | |
335 | relies on it existing (refcount > 0) we can't delete yet. */ | |
5b6d1e4f | 336 | return refcount () == 0 && !is_current_thread (this); |
08036331 PA |
337 | } |
338 | ||
c2829269 PA |
339 | /* See gdbthread.h. */ |
340 | ||
611841bb AB |
341 | void |
342 | thread_info::set_executing (bool executing) | |
343 | { | |
344 | m_executing = executing; | |
345 | if (executing) | |
351031f2 | 346 | this->clear_stop_pc (); |
611841bb AB |
347 | } |
348 | ||
349 | /* See gdbthread.h. */ | |
350 | ||
a66f7298 SM |
351 | void |
352 | thread_info::set_resumed (bool resumed) | |
353 | { | |
354 | if (resumed == m_resumed) | |
355 | return; | |
356 | ||
357 | process_stratum_target *proc_target = this->inf->process_target (); | |
358 | ||
359 | /* If we transition from resumed to not resumed, we might need to remove | |
360 | the thread from the resumed threads with pending statuses list. */ | |
361 | if (!resumed) | |
362 | proc_target->maybe_remove_resumed_with_pending_wait_status (this); | |
363 | ||
364 | m_resumed = resumed; | |
365 | ||
366 | /* If we transition from not resumed to resumed, we might need to add | |
367 | the thread to the resumed threads with pending statuses list. */ | |
368 | if (resumed) | |
369 | proc_target->maybe_add_resumed_with_pending_wait_status (this); | |
370 | } | |
371 | ||
372 | /* See gdbthread.h. */ | |
373 | ||
1edb66d8 SM |
374 | void |
375 | thread_info::set_pending_waitstatus (const target_waitstatus &ws) | |
376 | { | |
377 | gdb_assert (!this->has_pending_waitstatus ()); | |
378 | ||
379 | m_suspend.waitstatus = ws; | |
380 | m_suspend.waitstatus_pending_p = 1; | |
a66f7298 SM |
381 | |
382 | process_stratum_target *proc_target = this->inf->process_target (); | |
383 | proc_target->maybe_add_resumed_with_pending_wait_status (this); | |
1edb66d8 SM |
384 | } |
385 | ||
386 | /* See gdbthread.h. */ | |
387 | ||
388 | void | |
389 | thread_info::clear_pending_waitstatus () | |
390 | { | |
391 | gdb_assert (this->has_pending_waitstatus ()); | |
392 | ||
a66f7298 SM |
393 | process_stratum_target *proc_target = this->inf->process_target (); |
394 | proc_target->maybe_remove_resumed_with_pending_wait_status (this); | |
395 | ||
1edb66d8 SM |
396 | m_suspend.waitstatus_pending_p = 0; |
397 | } | |
398 | ||
399 | /* See gdbthread.h. */ | |
400 | ||
c2829269 PA |
401 | int |
402 | thread_is_in_step_over_chain (struct thread_info *tp) | |
403 | { | |
8b6a69b2 | 404 | return tp->step_over_list_node.is_linked (); |
c2829269 PA |
405 | } |
406 | ||
407 | /* See gdbthread.h. */ | |
408 | ||
187b041e | 409 | int |
8b6a69b2 | 410 | thread_step_over_chain_length (const thread_step_over_list &l) |
187b041e | 411 | { |
c316c0b2 | 412 | int num = 0; |
187b041e | 413 | |
8b6a69b2 | 414 | for (const thread_info &thread ATTRIBUTE_UNUSED : l) |
187b041e SM |
415 | ++num; |
416 | ||
417 | return num; | |
418 | } | |
419 | ||
420 | /* See gdbthread.h. */ | |
421 | ||
c2829269 | 422 | void |
28d5518b | 423 | global_thread_step_over_chain_enqueue (struct thread_info *tp) |
c2829269 | 424 | { |
187b041e | 425 | infrun_debug_printf ("enqueueing thread %s in global step over chain", |
e53c95d4 | 426 | tp->ptid.to_string ().c_str ()); |
187b041e | 427 | |
8b6a69b2 SM |
428 | gdb_assert (!thread_is_in_step_over_chain (tp)); |
429 | global_thread_step_over_list.push_back (*tp); | |
c2829269 PA |
430 | } |
431 | ||
432 | /* See gdbthread.h. */ | |
433 | ||
187b041e | 434 | void |
8b6a69b2 | 435 | global_thread_step_over_chain_enqueue_chain (thread_step_over_list &&list) |
187b041e | 436 | { |
8b6a69b2 | 437 | global_thread_step_over_list.splice (std::move (list)); |
187b041e SM |
438 | } |
439 | ||
440 | /* See gdbthread.h. */ | |
441 | ||
c2829269 | 442 | void |
28d5518b | 443 | global_thread_step_over_chain_remove (struct thread_info *tp) |
c2829269 | 444 | { |
187b041e | 445 | infrun_debug_printf ("removing thread %s from global step over chain", |
e53c95d4 | 446 | tp->ptid.to_string ().c_str ()); |
187b041e | 447 | |
8b6a69b2 SM |
448 | gdb_assert (thread_is_in_step_over_chain (tp)); |
449 | auto it = global_thread_step_over_list.iterator_to (*tp); | |
450 | global_thread_step_over_list.erase (it); | |
c2829269 PA |
451 | } |
452 | ||
86be3050 PA |
453 | /* Delete the thread referenced by THR. If SILENT, don't notify |
454 | the observer of this exit. | |
455 | ||
456 | THR must not be NULL or a failed assertion will be raised. */ | |
00431a78 | 457 | |
86be3050 PA |
458 | static void |
459 | delete_thread_1 (thread_info *thr, bool silent) | |
c906108c | 460 | { |
2eab46b1 | 461 | gdb_assert (thr != nullptr); |
c906108c | 462 | |
86be3050 PA |
463 | threads_debug_printf ("deleting thread %s, silent = %d", |
464 | thr->ptid.to_string ().c_str (), silent); | |
5b0a3d62 | 465 | |
86be3050 | 466 | set_thread_exited (thr, silent); |
c906108c | 467 | |
bf809310 | 468 | if (!thr->deletable ()) |
8c25b497 | 469 | { |
4f8d22e3 PA |
470 | /* Will be really deleted some other time. */ |
471 | return; | |
472 | } | |
473 | ||
bf809310 PA |
474 | auto it = thr->inf->thread_list.iterator_to (*thr); |
475 | thr->inf->thread_list.erase (it); | |
c906108c | 476 | |
bf809310 | 477 | delete thr; |
c906108c SS |
478 | } |
479 | ||
86be3050 PA |
480 | /* See gdbthread.h. */ |
481 | ||
482 | void | |
483 | delete_thread (thread_info *thread) | |
484 | { | |
485 | delete_thread_1 (thread, false /* not silent */); | |
486 | } | |
487 | ||
488 | void | |
489 | delete_thread_silent (thread_info *thread) | |
490 | { | |
491 | delete_thread_1 (thread, true /* silent */); | |
492 | } | |
493 | ||
1e92afda | 494 | struct thread_info * |
5d5658a1 | 495 | find_thread_global_id (int global_id) |
c906108c | 496 | { |
08036331 | 497 | for (thread_info *tp : all_threads ()) |
5d5658a1 PA |
498 | if (tp->global_num == global_id) |
499 | return tp; | |
500 | ||
501 | return NULL; | |
502 | } | |
503 | ||
504 | static struct thread_info * | |
505 | find_thread_id (struct inferior *inf, int thr_num) | |
506 | { | |
08036331 PA |
507 | for (thread_info *tp : inf->threads ()) |
508 | if (tp->per_inf_num == thr_num) | |
c906108c SS |
509 | return tp; |
510 | ||
511 | return NULL; | |
512 | } | |
513 | ||
5b6d1e4f | 514 | /* See gdbthread.h. */ |
e04ee09e | 515 | |
0d06e24b | 516 | struct thread_info * |
5b6d1e4f | 517 | find_thread_ptid (process_stratum_target *targ, ptid_t ptid) |
0d06e24b | 518 | { |
5b6d1e4f | 519 | inferior *inf = find_inferior_ptid (targ, ptid); |
08036331 PA |
520 | if (inf == NULL) |
521 | return NULL; | |
522 | return find_thread_ptid (inf, ptid); | |
523 | } | |
0d06e24b | 524 | |
08036331 PA |
525 | /* See gdbthread.h. */ |
526 | ||
527 | struct thread_info * | |
528 | find_thread_ptid (inferior *inf, ptid_t ptid) | |
529 | { | |
f53fc427 SM |
530 | gdb_assert (inf != nullptr); |
531 | ||
922cc93d SM |
532 | auto it = inf->ptid_thread_map.find (ptid); |
533 | if (it != inf->ptid_thread_map.end ()) | |
534 | return it->second; | |
535 | else | |
536 | return nullptr; | |
0d06e24b JM |
537 | } |
538 | ||
e04ee09e KB |
539 | /* See gdbthread.h. */ |
540 | ||
541 | struct thread_info * | |
50a82723 KB |
542 | find_thread_by_handle (gdb::array_view<const gdb_byte> handle, |
543 | struct inferior *inf) | |
e04ee09e | 544 | { |
50a82723 KB |
545 | return target_thread_handle_to_thread_info (handle.data (), |
546 | handle.size (), | |
547 | inf); | |
e04ee09e KB |
548 | } |
549 | ||
0d06e24b JM |
550 | /* |
551 | * Thread iterator function. | |
552 | * | |
553 | * Calls a callback function once for each thread, so long as | |
554 | * the callback function returns false. If the callback function | |
555 | * returns true, the iteration will end and the current thread | |
ae0eee42 | 556 | * will be returned. This can be useful for implementing a |
0d06e24b JM |
557 | * search for a thread with arbitrary attributes, or for applying |
558 | * some operation to every thread. | |
559 | * | |
ae0eee42 | 560 | * FIXME: some of the existing functionality, such as |
0d06e24b JM |
561 | * "Thread apply all", might be rewritten using this functionality. |
562 | */ | |
563 | ||
564 | struct thread_info * | |
fd118b61 KB |
565 | iterate_over_threads (int (*callback) (struct thread_info *, void *), |
566 | void *data) | |
0d06e24b | 567 | { |
08036331 PA |
568 | for (thread_info *tp : all_threads_safe ()) |
569 | if ((*callback) (tp, data)) | |
570 | return tp; | |
0d06e24b JM |
571 | |
572 | return NULL; | |
573 | } | |
574 | ||
08036331 PA |
575 | /* See gdbthread.h. */ |
576 | ||
577 | bool | |
578 | any_thread_p () | |
579 | { | |
580 | for (thread_info *tp ATTRIBUTE_UNUSED : all_threads ()) | |
581 | return true; | |
582 | return false; | |
583 | } | |
584 | ||
20874c92 | 585 | int |
5b6d1e4f | 586 | thread_count (process_stratum_target *proc_target) |
20874c92 | 587 | { |
5b6d1e4f | 588 | auto rng = all_threads (proc_target); |
08036331 | 589 | return std::distance (rng.begin (), rng.end ()); |
20874c92 VP |
590 | } |
591 | ||
c6609450 PA |
592 | /* Return the number of non-exited threads in the thread list. */ |
593 | ||
594 | static int | |
595 | live_threads_count (void) | |
596 | { | |
08036331 PA |
597 | auto rng = all_non_exited_threads (); |
598 | return std::distance (rng.begin (), rng.end ()); | |
c6609450 PA |
599 | } |
600 | ||
c906108c | 601 | int |
5d5658a1 | 602 | valid_global_thread_id (int global_id) |
c906108c | 603 | { |
08036331 | 604 | for (thread_info *tp : all_threads ()) |
5d5658a1 | 605 | if (tp->global_num == global_id) |
c906108c SS |
606 | return 1; |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
5b6d1e4f PA |
611 | bool |
612 | in_thread_list (process_stratum_target *targ, ptid_t ptid) | |
c906108c | 613 | { |
5b6d1e4f | 614 | return find_thread_ptid (targ, ptid) != nullptr; |
c906108c | 615 | } |
8926118c | 616 | |
00431a78 | 617 | /* Finds the first thread of the inferior. */ |
bad34192 | 618 | |
00431a78 PA |
619 | thread_info * |
620 | first_thread_of_inferior (inferior *inf) | |
bad34192 | 621 | { |
bf809310 PA |
622 | if (inf->thread_list.empty ()) |
623 | return nullptr; | |
624 | ||
625 | return &inf->thread_list.front (); | |
bad34192 PA |
626 | } |
627 | ||
00431a78 PA |
628 | thread_info * |
629 | any_thread_of_inferior (inferior *inf) | |
2277426b | 630 | { |
00431a78 | 631 | gdb_assert (inf->pid != 0); |
32990ada | 632 | |
219f56b4 PA |
633 | /* Prefer the current thread, if there's one. */ |
634 | if (inf == current_inferior () && inferior_ptid != null_ptid) | |
32990ada PA |
635 | return inferior_thread (); |
636 | ||
08036331 PA |
637 | for (thread_info *tp : inf->non_exited_threads ()) |
638 | return tp; | |
2277426b PA |
639 | |
640 | return NULL; | |
641 | } | |
642 | ||
00431a78 PA |
643 | thread_info * |
644 | any_live_thread_of_inferior (inferior *inf) | |
6c95b8df | 645 | { |
32990ada | 646 | struct thread_info *curr_tp = NULL; |
9941e0c5 | 647 | struct thread_info *tp_executing = NULL; |
6c95b8df | 648 | |
00431a78 | 649 | gdb_assert (inf != NULL && inf->pid != 0); |
32990ada PA |
650 | |
651 | /* Prefer the current thread if it's not executing. */ | |
00431a78 | 652 | if (inferior_ptid != null_ptid && current_inferior () == inf) |
32990ada PA |
653 | { |
654 | /* If the current thread is dead, forget it. If it's not | |
655 | executing, use it. Otherwise, still choose it (below), but | |
656 | only if no other non-executing thread is found. */ | |
657 | curr_tp = inferior_thread (); | |
658 | if (curr_tp->state == THREAD_EXITED) | |
659 | curr_tp = NULL; | |
611841bb | 660 | else if (!curr_tp->executing ()) |
32990ada PA |
661 | return curr_tp; |
662 | } | |
663 | ||
08036331 PA |
664 | for (thread_info *tp : inf->non_exited_threads ()) |
665 | { | |
611841bb | 666 | if (!tp->executing ()) |
08036331 | 667 | return tp; |
32990ada | 668 | |
08036331 PA |
669 | tp_executing = tp; |
670 | } | |
6c95b8df | 671 | |
32990ada PA |
672 | /* If both the current thread and all live threads are executing, |
673 | prefer the current thread. */ | |
674 | if (curr_tp != NULL) | |
675 | return curr_tp; | |
676 | ||
677 | /* Otherwise, just return an executing thread, if any. */ | |
9941e0c5 | 678 | return tp_executing; |
6c95b8df PA |
679 | } |
680 | ||
c378eb4e | 681 | /* Return true if TP is an active thread. */ |
f3f8ece4 PA |
682 | static bool |
683 | thread_alive (thread_info *tp) | |
c906108c | 684 | { |
30596231 | 685 | if (tp->state == THREAD_EXITED) |
f3f8ece4 PA |
686 | return false; |
687 | ||
688 | /* Ensure we're looking at the right target stack. */ | |
689 | gdb_assert (tp->inf == current_inferior ()); | |
690 | ||
691 | return target_thread_alive (tp->ptid); | |
692 | } | |
693 | ||
bc75fb44 | 694 | /* See gdbthreads.h. */ |
f3f8ece4 | 695 | |
bc75fb44 | 696 | bool |
f3f8ece4 PA |
697 | switch_to_thread_if_alive (thread_info *thr) |
698 | { | |
699 | scoped_restore_current_thread restore_thread; | |
700 | ||
701 | /* Switch inferior first, so that we're looking at the right target | |
702 | stack. */ | |
703 | switch_to_inferior_no_thread (thr->inf); | |
704 | ||
705 | if (thread_alive (thr)) | |
706 | { | |
707 | switch_to_thread (thr); | |
708 | restore_thread.dont_restore (); | |
709 | return true; | |
710 | } | |
711 | ||
712 | return false; | |
c906108c SS |
713 | } |
714 | ||
e8032dde PA |
715 | /* See gdbthreads.h. */ |
716 | ||
717 | void | |
fba45db2 | 718 | prune_threads (void) |
c906108c | 719 | { |
f3f8ece4 PA |
720 | scoped_restore_current_thread restore_thread; |
721 | ||
08036331 | 722 | for (thread_info *tp : all_threads_safe ()) |
f3f8ece4 PA |
723 | { |
724 | switch_to_inferior_no_thread (tp->inf); | |
725 | ||
726 | if (!thread_alive (tp)) | |
727 | delete_thread (tp); | |
728 | } | |
c906108c SS |
729 | } |
730 | ||
8a06aea7 PA |
731 | /* See gdbthreads.h. */ |
732 | ||
733 | void | |
734 | delete_exited_threads (void) | |
735 | { | |
08036331 PA |
736 | for (thread_info *tp : all_threads_safe ()) |
737 | if (tp->state == THREAD_EXITED) | |
738 | delete_thread (tp); | |
8a06aea7 PA |
739 | } |
740 | ||
85102364 | 741 | /* Return true value if stack temporaries are enabled for the thread |
00431a78 | 742 | TP. */ |
6c659fc2 | 743 | |
fdf07f3a | 744 | bool |
00431a78 | 745 | thread_stack_temporaries_enabled_p (thread_info *tp) |
6c659fc2 | 746 | { |
6c659fc2 | 747 | if (tp == NULL) |
fdf07f3a | 748 | return false; |
6c659fc2 SC |
749 | else |
750 | return tp->stack_temporaries_enabled; | |
751 | } | |
752 | ||
753 | /* Push V on to the stack temporaries of the thread with id PTID. */ | |
754 | ||
755 | void | |
00431a78 | 756 | push_thread_stack_temporary (thread_info *tp, struct value *v) |
6c659fc2 | 757 | { |
6c659fc2 | 758 | gdb_assert (tp != NULL && tp->stack_temporaries_enabled); |
fdf07f3a | 759 | tp->stack_temporaries.push_back (v); |
6c659fc2 SC |
760 | } |
761 | ||
fdf07f3a | 762 | /* Return true if VAL is among the stack temporaries of the thread |
00431a78 | 763 | TP. Return false otherwise. */ |
6c659fc2 | 764 | |
fdf07f3a | 765 | bool |
00431a78 | 766 | value_in_thread_stack_temporaries (struct value *val, thread_info *tp) |
6c659fc2 | 767 | { |
6c659fc2 | 768 | gdb_assert (tp != NULL && tp->stack_temporaries_enabled); |
52941706 | 769 | for (value *v : tp->stack_temporaries) |
fdf07f3a TT |
770 | if (v == val) |
771 | return true; | |
6c659fc2 | 772 | |
fdf07f3a | 773 | return false; |
6c659fc2 SC |
774 | } |
775 | ||
776 | /* Return the last of the stack temporaries for thread with id PTID. | |
777 | Return NULL if there are no stack temporaries for the thread. */ | |
778 | ||
00431a78 PA |
779 | value * |
780 | get_last_thread_stack_temporary (thread_info *tp) | |
6c659fc2 SC |
781 | { |
782 | struct value *lastval = NULL; | |
6c659fc2 SC |
783 | |
784 | gdb_assert (tp != NULL); | |
fdf07f3a TT |
785 | if (!tp->stack_temporaries.empty ()) |
786 | lastval = tp->stack_temporaries.back (); | |
6c659fc2 SC |
787 | |
788 | return lastval; | |
789 | } | |
790 | ||
5231c1fd | 791 | void |
5b6d1e4f PA |
792 | thread_change_ptid (process_stratum_target *targ, |
793 | ptid_t old_ptid, ptid_t new_ptid) | |
5231c1fd | 794 | { |
82f73884 PA |
795 | struct inferior *inf; |
796 | struct thread_info *tp; | |
797 | ||
798 | /* It can happen that what we knew as the target inferior id | |
799 | changes. E.g, target remote may only discover the remote process | |
800 | pid after adding the inferior to GDB's list. */ | |
5b6d1e4f | 801 | inf = find_inferior_ptid (targ, old_ptid); |
e99b03dc | 802 | inf->pid = new_ptid.pid (); |
82f73884 | 803 | |
08036331 | 804 | tp = find_thread_ptid (inf, old_ptid); |
922cc93d SM |
805 | gdb_assert (tp != nullptr); |
806 | ||
807 | int num_erased = inf->ptid_thread_map.erase (old_ptid); | |
808 | gdb_assert (num_erased == 1); | |
809 | ||
5231c1fd | 810 | tp->ptid = new_ptid; |
922cc93d | 811 | inf->ptid_thread_map[new_ptid] = tp; |
5231c1fd | 812 | |
b161a60d | 813 | gdb::observers::thread_ptid_changed.notify (targ, old_ptid, new_ptid); |
5231c1fd PA |
814 | } |
815 | ||
372316f1 PA |
816 | /* See gdbthread.h. */ |
817 | ||
818 | void | |
5b6d1e4f | 819 | set_resumed (process_stratum_target *targ, ptid_t ptid, bool resumed) |
372316f1 | 820 | { |
5b6d1e4f | 821 | for (thread_info *tp : all_non_exited_threads (targ, ptid)) |
7846f3aa | 822 | tp->set_resumed (resumed); |
372316f1 PA |
823 | } |
824 | ||
4d9d9d04 PA |
825 | /* Helper for set_running, that marks one thread either running or |
826 | stopped. */ | |
827 | ||
719546c4 SM |
828 | static bool |
829 | set_running_thread (struct thread_info *tp, bool running) | |
4d9d9d04 | 830 | { |
719546c4 | 831 | bool started = false; |
4d9d9d04 PA |
832 | |
833 | if (running && tp->state == THREAD_STOPPED) | |
719546c4 | 834 | started = true; |
4d9d9d04 PA |
835 | tp->state = running ? THREAD_RUNNING : THREAD_STOPPED; |
836 | ||
58fd1204 AB |
837 | threads_debug_printf ("thread: %s, running? %d%s", |
838 | tp->ptid.to_string ().c_str (), running, | |
f9e2163a | 839 | (started ? " (started)" : "")); |
58fd1204 | 840 | |
4d9d9d04 PA |
841 | if (!running) |
842 | { | |
843 | /* If the thread is now marked stopped, remove it from | |
844 | the step-over queue, so that we don't try to resume | |
845 | it until the user wants it to. */ | |
8b6a69b2 | 846 | if (thread_is_in_step_over_chain (tp)) |
28d5518b | 847 | global_thread_step_over_chain_remove (tp); |
4d9d9d04 PA |
848 | } |
849 | ||
850 | return started; | |
851 | } | |
852 | ||
00431a78 PA |
853 | /* See gdbthread.h. */ |
854 | ||
855 | void | |
856 | thread_info::set_running (bool running) | |
857 | { | |
858 | if (set_running_thread (this, running)) | |
859 | gdb::observers::target_resumed.notify (this->ptid); | |
860 | } | |
861 | ||
e1ac3328 | 862 | void |
5b6d1e4f | 863 | set_running (process_stratum_target *targ, ptid_t ptid, bool running) |
e1ac3328 | 864 | { |
08036331 PA |
865 | /* We try not to notify the observer if no thread has actually |
866 | changed the running state -- merely to reduce the number of | |
867 | messages to the MI frontend. A frontend is supposed to handle | |
868 | multiple *running notifications just fine. */ | |
869 | bool any_started = false; | |
e1ac3328 | 870 | |
5b6d1e4f | 871 | for (thread_info *tp : all_non_exited_threads (targ, ptid)) |
08036331 PA |
872 | if (set_running_thread (tp, running)) |
873 | any_started = true; | |
4d9d9d04 | 874 | |
4d9d9d04 | 875 | if (any_started) |
76727919 | 876 | gdb::observers::target_resumed.notify (ptid); |
e1ac3328 VP |
877 | } |
878 | ||
8ea051c5 | 879 | void |
5b6d1e4f | 880 | set_executing (process_stratum_target *targ, ptid_t ptid, bool executing) |
8ea051c5 | 881 | { |
5b6d1e4f | 882 | for (thread_info *tp : all_non_exited_threads (targ, ptid)) |
611841bb | 883 | tp->set_executing (executing); |
8ea051c5 | 884 | |
08036331 | 885 | /* It only takes one running thread to spawn more threads. */ |
b57bacec | 886 | if (executing) |
5b6d1e4f | 887 | targ->threads_executing = true; |
b57bacec PA |
888 | /* Only clear the flag if the caller is telling us everything is |
889 | stopped. */ | |
9295a5a9 | 890 | else if (minus_one_ptid == ptid) |
5b6d1e4f | 891 | targ->threads_executing = false; |
b57bacec PA |
892 | } |
893 | ||
894 | /* See gdbthread.h. */ | |
895 | ||
5b6d1e4f PA |
896 | bool |
897 | threads_are_executing (process_stratum_target *target) | |
b57bacec | 898 | { |
5b6d1e4f | 899 | return target->threads_executing; |
8ea051c5 PA |
900 | } |
901 | ||
252fbfc8 | 902 | void |
5b6d1e4f | 903 | set_stop_requested (process_stratum_target *targ, ptid_t ptid, bool stop) |
252fbfc8 | 904 | { |
5b6d1e4f | 905 | for (thread_info *tp : all_non_exited_threads (targ, ptid)) |
08036331 | 906 | tp->stop_requested = stop; |
252fbfc8 PA |
907 | |
908 | /* Call the stop requested observer so other components of GDB can | |
909 | react to this request. */ | |
910 | if (stop) | |
76727919 | 911 | gdb::observers::thread_stop_requested.notify (ptid); |
252fbfc8 PA |
912 | } |
913 | ||
29f49a6a | 914 | void |
5b6d1e4f | 915 | finish_thread_state (process_stratum_target *targ, ptid_t ptid) |
29f49a6a | 916 | { |
08036331 | 917 | bool any_started = false; |
29f49a6a | 918 | |
5b6d1e4f | 919 | for (thread_info *tp : all_non_exited_threads (targ, ptid)) |
611841bb | 920 | if (set_running_thread (tp, tp->executing ())) |
08036331 | 921 | any_started = true; |
29f49a6a PA |
922 | |
923 | if (any_started) | |
76727919 | 924 | gdb::observers::target_resumed.notify (ptid); |
29f49a6a PA |
925 | } |
926 | ||
a911d87a PA |
927 | /* See gdbthread.h. */ |
928 | ||
929 | void | |
930 | validate_registers_access (void) | |
931 | { | |
932 | /* No selected thread, no registers. */ | |
9295a5a9 | 933 | if (inferior_ptid == null_ptid) |
a911d87a PA |
934 | error (_("No thread selected.")); |
935 | ||
00431a78 PA |
936 | thread_info *tp = inferior_thread (); |
937 | ||
a911d87a | 938 | /* Don't try to read from a dead thread. */ |
00431a78 | 939 | if (tp->state == THREAD_EXITED) |
a911d87a PA |
940 | error (_("The current thread has terminated")); |
941 | ||
942 | /* ... or from a spinning thread. FIXME: This isn't actually fully | |
943 | correct. It'll allow an user-requested access (e.g., "print $pc" | |
944 | at the prompt) when a thread is not executing for some internal | |
945 | reason, but is marked running from the user's perspective. E.g., | |
946 | the thread is waiting for its turn in the step-over queue. */ | |
611841bb | 947 | if (tp->executing ()) |
a911d87a PA |
948 | error (_("Selected thread is running.")); |
949 | } | |
950 | ||
cf77c34e MM |
951 | /* See gdbthread.h. */ |
952 | ||
953 | bool | |
00431a78 | 954 | can_access_registers_thread (thread_info *thread) |
cf77c34e MM |
955 | { |
956 | /* No thread, no registers. */ | |
00431a78 | 957 | if (thread == NULL) |
cf77c34e MM |
958 | return false; |
959 | ||
960 | /* Don't try to read from a dead thread. */ | |
00431a78 | 961 | if (thread->state == THREAD_EXITED) |
cf77c34e MM |
962 | return false; |
963 | ||
964 | /* ... or from a spinning thread. FIXME: see validate_registers_access. */ | |
611841bb | 965 | if (thread->executing ()) |
cf77c34e MM |
966 | return false; |
967 | ||
968 | return true; | |
969 | } | |
970 | ||
ce4c476a PA |
971 | int |
972 | pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread) | |
973 | { | |
974 | return (pc >= thread->control.step_range_start | |
975 | && pc < thread->control.step_range_end); | |
976 | } | |
977 | ||
5d5658a1 PA |
978 | /* Helper for print_thread_info. Returns true if THR should be |
979 | printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not | |
980 | NULL, only print THR if its ID is included in the list. GLOBAL_IDS | |
981 | is true if REQUESTED_THREADS is list of global IDs, false if a list | |
982 | of per-inferior thread ids. If PID is not -1, only print THR if it | |
983 | is a thread from the process PID. Otherwise, threads from all | |
984 | attached PIDs are printed. If both REQUESTED_THREADS is not NULL | |
985 | and PID is not -1, then the thread is printed if it belongs to the | |
986 | specified process. Otherwise, an error is raised. */ | |
987 | ||
988 | static int | |
989 | should_print_thread (const char *requested_threads, int default_inf_num, | |
990 | int global_ids, int pid, struct thread_info *thr) | |
991 | { | |
992 | if (requested_threads != NULL && *requested_threads != '\0') | |
993 | { | |
994 | int in_list; | |
995 | ||
996 | if (global_ids) | |
997 | in_list = number_is_in_list (requested_threads, thr->global_num); | |
998 | else | |
999 | in_list = tid_is_in_list (requested_threads, default_inf_num, | |
1000 | thr->inf->num, thr->per_inf_num); | |
1001 | if (!in_list) | |
1002 | return 0; | |
1003 | } | |
1004 | ||
e99b03dc | 1005 | if (pid != -1 && thr->ptid.pid () != pid) |
5d5658a1 PA |
1006 | { |
1007 | if (requested_threads != NULL && *requested_threads != '\0') | |
1008 | error (_("Requested thread not found in requested process")); | |
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | if (thr->state == THREAD_EXITED) | |
1013 | return 0; | |
1014 | ||
1015 | return 1; | |
1016 | } | |
1017 | ||
75acb486 PA |
1018 | /* Return the string to display in "info threads"'s "Target Id" |
1019 | column, for TP. */ | |
1020 | ||
1021 | static std::string | |
1022 | thread_target_id_str (thread_info *tp) | |
1023 | { | |
a068643d | 1024 | std::string target_id = target_pid_to_str (tp->ptid); |
75acb486 | 1025 | const char *extra_info = target_extra_thread_info (tp); |
25558938 | 1026 | const char *name = thread_name (tp); |
75acb486 PA |
1027 | |
1028 | if (extra_info != nullptr && name != nullptr) | |
a068643d TT |
1029 | return string_printf ("%s \"%s\" (%s)", target_id.c_str (), name, |
1030 | extra_info); | |
75acb486 | 1031 | else if (extra_info != nullptr) |
a068643d | 1032 | return string_printf ("%s (%s)", target_id.c_str (), extra_info); |
75acb486 | 1033 | else if (name != nullptr) |
a068643d | 1034 | return string_printf ("%s \"%s\"", target_id.c_str (), name); |
75acb486 PA |
1035 | else |
1036 | return target_id; | |
1037 | } | |
1038 | ||
5d5658a1 PA |
1039 | /* Like print_thread_info, but in addition, GLOBAL_IDS indicates |
1040 | whether REQUESTED_THREADS is a list of global or per-inferior | |
1041 | thread ids. */ | |
1042 | ||
1043 | static void | |
1d12d88f | 1044 | print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, |
5d5658a1 PA |
1045 | int global_ids, int pid, |
1046 | int show_global_ids) | |
c906108c | 1047 | { |
5d5658a1 | 1048 | int default_inf_num = current_inferior ()->num; |
c906108c | 1049 | |
dc146f7c | 1050 | update_thread_list (); |
00431a78 PA |
1051 | |
1052 | /* Whether we saw any thread. */ | |
1053 | bool any_thread = false; | |
1054 | /* Whether the current thread is exited. */ | |
1055 | bool current_exited = false; | |
1056 | ||
1057 | thread_info *current_thread = (inferior_ptid != null_ptid | |
1058 | ? inferior_thread () : NULL); | |
4f8d22e3 | 1059 | |
f8cc3da6 TT |
1060 | { |
1061 | /* For backward compatibility, we make a list for MI. A table is | |
1062 | preferable for the CLI, though, because it shows table | |
1063 | headers. */ | |
1064 | gdb::optional<ui_out_emit_list> list_emitter; | |
1065 | gdb::optional<ui_out_emit_table> table_emitter; | |
1066 | ||
f3f8ece4 PA |
1067 | /* We'll be switching threads temporarily below. */ |
1068 | scoped_restore_current_thread restore_thread; | |
1069 | ||
f8cc3da6 TT |
1070 | if (uiout->is_mi_like_p ()) |
1071 | list_emitter.emplace (uiout, "threads"); | |
1072 | else | |
1073 | { | |
1074 | int n_threads = 0; | |
75acb486 PA |
1075 | /* The width of the "Target Id" column. Grown below to |
1076 | accommodate the largest entry. */ | |
1077 | size_t target_id_col_width = 17; | |
a7658b96 | 1078 | |
08036331 | 1079 | for (thread_info *tp : all_threads ()) |
f8cc3da6 TT |
1080 | { |
1081 | if (!should_print_thread (requested_threads, default_inf_num, | |
1082 | global_ids, pid, tp)) | |
1083 | continue; | |
a7658b96 | 1084 | |
75acb486 PA |
1085 | if (!uiout->is_mi_like_p ()) |
1086 | { | |
f3f8ece4 PA |
1087 | /* Switch inferiors so we're looking at the right |
1088 | target stack. */ | |
1089 | switch_to_inferior_no_thread (tp->inf); | |
1090 | ||
75acb486 PA |
1091 | target_id_col_width |
1092 | = std::max (target_id_col_width, | |
1093 | thread_target_id_str (tp).size ()); | |
1094 | } | |
1095 | ||
f8cc3da6 TT |
1096 | ++n_threads; |
1097 | } | |
a7658b96 | 1098 | |
f8cc3da6 TT |
1099 | if (n_threads == 0) |
1100 | { | |
1101 | if (requested_threads == NULL || *requested_threads == '\0') | |
1102 | uiout->message (_("No threads.\n")); | |
1103 | else | |
1104 | uiout->message (_("No threads match '%s'.\n"), | |
1105 | requested_threads); | |
1106 | return; | |
1107 | } | |
a7658b96 | 1108 | |
0d64823e | 1109 | table_emitter.emplace (uiout, show_global_ids ? 5 : 4, |
f8cc3da6 | 1110 | n_threads, "threads"); |
a7658b96 | 1111 | |
f8cc3da6 | 1112 | uiout->table_header (1, ui_left, "current", ""); |
0d64823e SM |
1113 | uiout->table_header (4, ui_left, "id-in-tg", "Id"); |
1114 | if (show_global_ids) | |
f8cc3da6 | 1115 | uiout->table_header (4, ui_left, "id", "GId"); |
75acb486 PA |
1116 | uiout->table_header (target_id_col_width, ui_left, |
1117 | "target-id", "Target Id"); | |
f8cc3da6 TT |
1118 | uiout->table_header (1, ui_left, "frame", "Frame"); |
1119 | uiout->table_body (); | |
1120 | } | |
a7658b96 | 1121 | |
08036331 PA |
1122 | for (inferior *inf : all_inferiors ()) |
1123 | for (thread_info *tp : inf->threads ()) | |
3061113b SM |
1124 | { |
1125 | int core; | |
1126 | ||
1127 | any_thread = true; | |
1128 | if (tp == current_thread && tp->state == THREAD_EXITED) | |
1129 | current_exited = true; | |
1130 | ||
1131 | if (!should_print_thread (requested_threads, default_inf_num, | |
1132 | global_ids, pid, tp)) | |
1133 | continue; | |
1134 | ||
1135 | ui_out_emit_tuple tuple_emitter (uiout, NULL); | |
1136 | ||
1137 | if (!uiout->is_mi_like_p ()) | |
1138 | { | |
1139 | if (tp == current_thread) | |
1140 | uiout->field_string ("current", "*"); | |
1141 | else | |
1142 | uiout->field_skip ("current"); | |
1143 | ||
1144 | uiout->field_string ("id-in-tg", print_thread_id (tp)); | |
1145 | } | |
1146 | ||
1147 | if (show_global_ids || uiout->is_mi_like_p ()) | |
1148 | uiout->field_signed ("id", tp->global_num); | |
1149 | ||
f3f8ece4 PA |
1150 | /* Switch to the thread (and inferior / target). */ |
1151 | switch_to_thread (tp); | |
1152 | ||
3061113b SM |
1153 | /* For the CLI, we stuff everything into the target-id field. |
1154 | This is a gross hack to make the output come out looking | |
1155 | correct. The underlying problem here is that ui-out has no | |
1156 | way to specify that a field's space allocation should be | |
1157 | shared by several fields. For MI, we do the right thing | |
1158 | instead. */ | |
1159 | ||
1160 | if (uiout->is_mi_like_p ()) | |
1161 | { | |
1162 | uiout->field_string ("target-id", target_pid_to_str (tp->ptid)); | |
1163 | ||
1164 | const char *extra_info = target_extra_thread_info (tp); | |
1165 | if (extra_info != nullptr) | |
1166 | uiout->field_string ("details", extra_info); | |
1167 | ||
25558938 | 1168 | const char *name = thread_name (tp); |
3061113b SM |
1169 | if (name != NULL) |
1170 | uiout->field_string ("name", name); | |
1171 | } | |
1172 | else | |
1173 | { | |
8dd8c8d4 | 1174 | uiout->field_string ("target-id", thread_target_id_str (tp)); |
3061113b SM |
1175 | } |
1176 | ||
1177 | if (tp->state == THREAD_RUNNING) | |
1178 | uiout->text ("(running)\n"); | |
1179 | else | |
1180 | { | |
f3f8ece4 | 1181 | /* The switch above put us at the top of the stack (leaf |
3061113b | 1182 | frame). */ |
3061113b SM |
1183 | print_stack_frame (get_selected_frame (NULL), |
1184 | /* For MI output, print frame level. */ | |
1185 | uiout->is_mi_like_p (), | |
1186 | LOCATION, 0); | |
1187 | } | |
1188 | ||
1189 | if (uiout->is_mi_like_p ()) | |
1190 | { | |
1191 | const char *state = "stopped"; | |
1192 | ||
1193 | if (tp->state == THREAD_RUNNING) | |
1194 | state = "running"; | |
1195 | uiout->field_string ("state", state); | |
1196 | } | |
1197 | ||
1198 | core = target_core_of_thread (tp->ptid); | |
1199 | if (uiout->is_mi_like_p () && core != -1) | |
1200 | uiout->field_signed ("core", core); | |
1201 | } | |
c906108c | 1202 | |
5ed8105e | 1203 | /* This end scope restores the current thread and the frame |
f8cc3da6 TT |
1204 | selected before the "info threads" command, and it finishes the |
1205 | ui-out list or table. */ | |
5ed8105e PA |
1206 | } |
1207 | ||
aea5b279 | 1208 | if (pid == -1 && requested_threads == NULL) |
8e8901c5 | 1209 | { |
00431a78 | 1210 | if (uiout->is_mi_like_p () && inferior_ptid != null_ptid) |
381befee | 1211 | uiout->field_signed ("current-thread-id", current_thread->global_num); |
5d5658a1 | 1212 | |
00431a78 | 1213 | if (inferior_ptid != null_ptid && current_exited) |
112e8700 | 1214 | uiout->message ("\n\ |
5d5658a1 PA |
1215 | The current thread <Thread ID %s> has terminated. See `help thread'.\n", |
1216 | print_thread_id (inferior_thread ())); | |
00431a78 | 1217 | else if (any_thread && inferior_ptid == null_ptid) |
112e8700 | 1218 | uiout->message ("\n\ |
d729566a | 1219 | No selected thread. See `help thread'.\n"); |
c906108c | 1220 | } |
c906108c SS |
1221 | } |
1222 | ||
5d5658a1 | 1223 | /* See gdbthread.h. */ |
8e8901c5 | 1224 | |
5d5658a1 | 1225 | void |
24c54127 TT |
1226 | print_thread_info (struct ui_out *uiout, const char *requested_threads, |
1227 | int pid) | |
5d5658a1 PA |
1228 | { |
1229 | print_thread_info_1 (uiout, requested_threads, 1, pid, 0); | |
1230 | } | |
1231 | ||
54d66006 PA |
1232 | /* The options for the "info threads" command. */ |
1233 | ||
1234 | struct info_threads_opts | |
1235 | { | |
1236 | /* For "-gid". */ | |
491144b5 | 1237 | bool show_global_ids = false; |
54d66006 PA |
1238 | }; |
1239 | ||
1240 | static const gdb::option::option_def info_threads_option_defs[] = { | |
1241 | ||
1242 | gdb::option::flag_option_def<info_threads_opts> { | |
1243 | "gid", | |
1244 | [] (info_threads_opts *opts) { return &opts->show_global_ids; }, | |
1245 | N_("Show global thread IDs."), | |
1246 | }, | |
1247 | ||
1248 | }; | |
1249 | ||
1250 | /* Create an option_def_group for the "info threads" options, with | |
1251 | IT_OPTS as context. */ | |
1252 | ||
1253 | static inline gdb::option::option_def_group | |
1254 | make_info_threads_options_def_group (info_threads_opts *it_opts) | |
1255 | { | |
1256 | return {{info_threads_option_defs}, it_opts}; | |
1257 | } | |
1258 | ||
5d5658a1 | 1259 | /* Implementation of the "info threads" command. |
60f98dde MS |
1260 | |
1261 | Note: this has the drawback that it _really_ switches | |
ae0eee42 PA |
1262 | threads, which frees the frame cache. A no-side |
1263 | effects info-threads command would be nicer. */ | |
8e8901c5 VP |
1264 | |
1265 | static void | |
1d12d88f | 1266 | info_threads_command (const char *arg, int from_tty) |
8e8901c5 | 1267 | { |
54d66006 PA |
1268 | info_threads_opts it_opts; |
1269 | ||
1270 | auto grp = make_info_threads_options_def_group (&it_opts); | |
1271 | gdb::option::process_options | |
1272 | (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp); | |
1273 | ||
1274 | print_thread_info_1 (current_uiout, arg, 0, -1, it_opts.show_global_ids); | |
1275 | } | |
1276 | ||
1277 | /* Completer for the "info threads" command. */ | |
c84f6bbf | 1278 | |
54d66006 PA |
1279 | static void |
1280 | info_threads_command_completer (struct cmd_list_element *ignore, | |
1281 | completion_tracker &tracker, | |
1282 | const char *text, const char *word_ignored) | |
1283 | { | |
1284 | const auto grp = make_info_threads_options_def_group (nullptr); | |
1285 | ||
1286 | if (gdb::option::complete_options | |
1287 | (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp)) | |
1288 | return; | |
1289 | ||
1290 | /* Convenience to let the user know what the option can accept. */ | |
1291 | if (*text == '\0') | |
c84f6bbf | 1292 | { |
54d66006 PA |
1293 | gdb::option::complete_on_all_options (tracker, grp); |
1294 | /* Keep this "ID" in sync with what "help info threads" | |
1295 | says. */ | |
1296 | tracker.add_completion (make_unique_xstrdup ("ID")); | |
c84f6bbf | 1297 | } |
8e8901c5 VP |
1298 | } |
1299 | ||
6efcd9a8 PA |
1300 | /* See gdbthread.h. */ |
1301 | ||
1302 | void | |
1303 | switch_to_thread_no_regs (struct thread_info *thread) | |
1304 | { | |
58fd1204 AB |
1305 | gdb_assert (thread != nullptr); |
1306 | threads_debug_printf ("thread = %s", thread->ptid.to_string ().c_str ()); | |
1307 | ||
3a3fd0fd | 1308 | struct inferior *inf = thread->inf; |
6efcd9a8 | 1309 | |
6efcd9a8 PA |
1310 | set_current_program_space (inf->pspace); |
1311 | set_current_inferior (inf); | |
1312 | ||
3922b302 PA |
1313 | current_thread_ = thread; |
1314 | inferior_ptid = current_thread_->ptid; | |
6efcd9a8 PA |
1315 | } |
1316 | ||
00431a78 | 1317 | /* See gdbthread.h. */ |
c906108c | 1318 | |
00431a78 | 1319 | void |
3a3fd0fd | 1320 | switch_to_no_thread () |
c906108c | 1321 | { |
3922b302 | 1322 | if (current_thread_ == nullptr) |
3a3fd0fd | 1323 | return; |
6c95b8df | 1324 | |
58fd1204 AB |
1325 | threads_debug_printf ("thread = NONE"); |
1326 | ||
3922b302 | 1327 | current_thread_ = nullptr; |
3a3fd0fd PA |
1328 | inferior_ptid = null_ptid; |
1329 | reinit_frame_cache (); | |
3a3fd0fd PA |
1330 | } |
1331 | ||
00431a78 | 1332 | /* See gdbthread.h. */ |
6c95b8df | 1333 | |
00431a78 | 1334 | void |
3a3fd0fd PA |
1335 | switch_to_thread (thread_info *thr) |
1336 | { | |
1337 | gdb_assert (thr != NULL); | |
1338 | ||
5b6d1e4f | 1339 | if (is_current_thread (thr)) |
c906108c SS |
1340 | return; |
1341 | ||
3a3fd0fd PA |
1342 | switch_to_thread_no_regs (thr); |
1343 | ||
35f196d9 | 1344 | reinit_frame_cache (); |
c906108c SS |
1345 | } |
1346 | ||
268a13a5 | 1347 | /* See gdbsupport/common-gdbthread.h. */ |
3a3fd0fd PA |
1348 | |
1349 | void | |
5b6d1e4f | 1350 | switch_to_thread (process_stratum_target *proc_target, ptid_t ptid) |
c906108c | 1351 | { |
5b6d1e4f | 1352 | thread_info *thr = find_thread_ptid (proc_target, ptid); |
00431a78 | 1353 | switch_to_thread (thr); |
99b3d574 DP |
1354 | } |
1355 | ||
79952e69 PA |
1356 | /* See frame.h. */ |
1357 | ||
873657b9 PA |
1358 | void |
1359 | scoped_restore_current_thread::restore () | |
6ecce94d | 1360 | { |
803bdfe4 YQ |
1361 | /* If an entry of thread_info was previously selected, it won't be |
1362 | deleted because we've increased its refcount. The thread represented | |
1363 | by this thread_info entry may have already exited (due to normal exit, | |
1364 | detach, etc), so the thread_info.state is THREAD_EXITED. */ | |
5ed8105e | 1365 | if (m_thread != NULL |
803bdfe4 YQ |
1366 | /* If the previously selected thread belonged to a process that has |
1367 | in the mean time exited (or killed, detached, etc.), then don't revert | |
1368 | back to it, but instead simply drop back to no thread selected. */ | |
5ed8105e | 1369 | && m_inf->pid != 0) |
cce20f10 | 1370 | switch_to_thread (m_thread.get ()); |
88fc996f | 1371 | else |
cce20f10 | 1372 | switch_to_inferior_no_thread (m_inf.get ()); |
94cc34af | 1373 | |
4f8d22e3 PA |
1374 | /* The running state of the originally selected thread may have |
1375 | changed, so we have to recheck it here. */ | |
9295a5a9 | 1376 | if (inferior_ptid != null_ptid |
5ed8105e | 1377 | && m_was_stopped |
00431a78 | 1378 | && m_thread->state == THREAD_STOPPED |
9dccd06e | 1379 | && target_has_registers () |
841de120 | 1380 | && target_has_stack () |
a739972c | 1381 | && target_has_memory ()) |
5ed8105e | 1382 | restore_selected_frame (m_selected_frame_id, m_selected_frame_level); |
79952e69 PA |
1383 | |
1384 | set_language (m_lang); | |
873657b9 PA |
1385 | } |
1386 | ||
1387 | scoped_restore_current_thread::~scoped_restore_current_thread () | |
1388 | { | |
1389 | if (!m_dont_restore) | |
79952e69 | 1390 | restore (); |
6ecce94d AC |
1391 | } |
1392 | ||
5ed8105e | 1393 | scoped_restore_current_thread::scoped_restore_current_thread () |
6ecce94d | 1394 | { |
cce20f10 | 1395 | m_inf = inferior_ref::new_reference (current_inferior ()); |
4f8d22e3 | 1396 | |
79952e69 PA |
1397 | m_lang = current_language->la_language; |
1398 | ||
9295a5a9 | 1399 | if (inferior_ptid != null_ptid) |
d729566a | 1400 | { |
cce20f10 PA |
1401 | m_thread = thread_info_ref::new_reference (inferior_thread ()); |
1402 | ||
cce20f10 | 1403 | m_was_stopped = m_thread->state == THREAD_STOPPED; |
79952e69 | 1404 | save_selected_frame (&m_selected_frame_id, &m_selected_frame_level); |
d729566a | 1405 | } |
6ecce94d AC |
1406 | } |
1407 | ||
43792cf0 PA |
1408 | /* See gdbthread.h. */ |
1409 | ||
f303dbd6 PA |
1410 | int |
1411 | show_thread_that_caused_stop (void) | |
1412 | { | |
1413 | return highest_thread_num > 1; | |
1414 | } | |
1415 | ||
1416 | /* See gdbthread.h. */ | |
1417 | ||
5d5658a1 PA |
1418 | int |
1419 | show_inferior_qualified_tids (void) | |
1420 | { | |
08bdefb5 PA |
1421 | auto inf = inferior_list.begin (); |
1422 | if (inf->num != 1) | |
1423 | return true; | |
1424 | ++inf; | |
1425 | return inf != inferior_list.end (); | |
5d5658a1 PA |
1426 | } |
1427 | ||
1428 | /* See gdbthread.h. */ | |
1429 | ||
43792cf0 PA |
1430 | const char * |
1431 | print_thread_id (struct thread_info *thr) | |
1432 | { | |
1433 | char *s = get_print_cell (); | |
1434 | ||
5d5658a1 PA |
1435 | if (show_inferior_qualified_tids ()) |
1436 | xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num); | |
1437 | else | |
1438 | xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num); | |
43792cf0 PA |
1439 | return s; |
1440 | } | |
1441 | ||
6665660a PA |
1442 | /* Sort an array of struct thread_info pointers by thread ID (first by |
1443 | inferior number, and then by per-inferior thread number). Sorts in | |
1444 | ascending order. */ | |
253828f1 | 1445 | |
6665660a | 1446 | static bool |
bfcb9db8 | 1447 | tp_array_compar_ascending (const thread_info_ref &a, const thread_info_ref &b) |
6665660a PA |
1448 | { |
1449 | if (a->inf->num != b->inf->num) | |
1450 | return a->inf->num < b->inf->num; | |
1451 | ||
1452 | return (a->per_inf_num < b->per_inf_num); | |
1453 | } | |
253828f1 | 1454 | |
6665660a PA |
1455 | /* Sort an array of struct thread_info pointers by thread ID (first by |
1456 | inferior number, and then by per-inferior thread number). Sorts in | |
1457 | descending order. */ | |
253828f1 | 1458 | |
c6609450 | 1459 | static bool |
bfcb9db8 | 1460 | tp_array_compar_descending (const thread_info_ref &a, const thread_info_ref &b) |
253828f1 | 1461 | { |
5d5658a1 | 1462 | if (a->inf->num != b->inf->num) |
6665660a | 1463 | return a->inf->num > b->inf->num; |
253828f1 | 1464 | |
6665660a | 1465 | return (a->per_inf_num > b->per_inf_num); |
253828f1 JK |
1466 | } |
1467 | ||
bc75fb44 | 1468 | /* See gdbthread.h. */ |
1fe75df7 | 1469 | |
bc75fb44 TT |
1470 | void |
1471 | thread_try_catch_cmd (thread_info *thr, gdb::optional<int> ada_task, | |
1472 | const char *cmd, int from_tty, | |
1473 | const qcs_flags &flags) | |
1fe75df7 | 1474 | { |
0f93c3a2 | 1475 | gdb_assert (is_current_thread (thr)); |
b65ce565 JG |
1476 | |
1477 | /* The thread header is computed before running the command since | |
1478 | the command can change the inferior, which is not permitted | |
1479 | by thread_target_id_str. */ | |
bc75fb44 TT |
1480 | std::string thr_header; |
1481 | if (ada_task.has_value ()) | |
1482 | thr_header = string_printf (_("\nTask ID %d:\n"), *ada_task); | |
1483 | else | |
1484 | thr_header = string_printf (_("\nThread %s (%s):\n"), | |
1485 | print_thread_id (thr), | |
1486 | thread_target_id_str (thr).c_str ()); | |
b65ce565 | 1487 | |
a70b8144 | 1488 | try |
1fe75df7 | 1489 | { |
84a6adfd TV |
1490 | std::string cmd_result; |
1491 | execute_command_to_string | |
1492 | (cmd_result, cmd, from_tty, gdb_stdout->term_out ()); | |
1fe75df7 PW |
1493 | if (!flags.silent || cmd_result.length () > 0) |
1494 | { | |
1495 | if (!flags.quiet) | |
6cb06a8c TT |
1496 | gdb_printf ("%s", thr_header.c_str ()); |
1497 | gdb_printf ("%s", cmd_result.c_str ()); | |
1fe75df7 PW |
1498 | } |
1499 | } | |
230d2906 | 1500 | catch (const gdb_exception_error &ex) |
1fe75df7 PW |
1501 | { |
1502 | if (!flags.silent) | |
1503 | { | |
1504 | if (!flags.quiet) | |
6cb06a8c | 1505 | gdb_printf ("%s", thr_header.c_str ()); |
1fe75df7 | 1506 | if (flags.cont) |
6cb06a8c | 1507 | gdb_printf ("%s\n", ex.what ()); |
1fe75df7 | 1508 | else |
eedc3f4f | 1509 | throw; |
1fe75df7 PW |
1510 | } |
1511 | } | |
1fe75df7 PW |
1512 | } |
1513 | ||
6665660a PA |
1514 | /* Option definition of "thread apply"'s "-ascending" option. */ |
1515 | ||
1516 | static const gdb::option::flag_option_def<> ascending_option_def = { | |
1517 | "ascending", | |
1518 | N_("\ | |
1519 | Call COMMAND for all threads in ascending order.\n\ | |
1520 | The default is descending order."), | |
1521 | }; | |
1522 | ||
1523 | /* The qcs command line flags for the "thread apply" commands. Keep | |
1524 | this in sync with the "frame apply" commands. */ | |
1525 | ||
1526 | using qcs_flag_option_def | |
1527 | = gdb::option::flag_option_def<qcs_flags>; | |
1528 | ||
1529 | static const gdb::option::option_def thr_qcs_flags_option_defs[] = { | |
1530 | qcs_flag_option_def { | |
1531 | "q", [] (qcs_flags *opt) { return &opt->quiet; }, | |
1532 | N_("Disables printing the thread information."), | |
1533 | }, | |
1534 | ||
1535 | qcs_flag_option_def { | |
1536 | "c", [] (qcs_flags *opt) { return &opt->cont; }, | |
1537 | N_("Print any error raised by COMMAND and continue."), | |
1538 | }, | |
1539 | ||
1540 | qcs_flag_option_def { | |
1541 | "s", [] (qcs_flags *opt) { return &opt->silent; }, | |
1542 | N_("Silently ignore any errors or empty output produced by COMMAND."), | |
1543 | }, | |
1544 | }; | |
1545 | ||
1546 | /* Create an option_def_group for the "thread apply all" options, with | |
1547 | ASCENDING and FLAGS as context. */ | |
1548 | ||
1549 | static inline std::array<gdb::option::option_def_group, 2> | |
491144b5 | 1550 | make_thread_apply_all_options_def_group (bool *ascending, |
6665660a PA |
1551 | qcs_flags *flags) |
1552 | { | |
1553 | return {{ | |
66eb1ed3 PA |
1554 | { {ascending_option_def.def ()}, ascending}, |
1555 | { {thr_qcs_flags_option_defs}, flags }, | |
6665660a PA |
1556 | }}; |
1557 | } | |
1558 | ||
1559 | /* Create an option_def_group for the "thread apply" options, with | |
1560 | FLAGS as context. */ | |
1561 | ||
1562 | static inline gdb::option::option_def_group | |
1563 | make_thread_apply_options_def_group (qcs_flags *flags) | |
1564 | { | |
66eb1ed3 | 1565 | return {{thr_qcs_flags_option_defs}, flags}; |
6665660a PA |
1566 | } |
1567 | ||
c906108c | 1568 | /* Apply a GDB command to a list of threads. List syntax is a whitespace |
224608c3 PW |
1569 | separated list of numbers, or ranges, or the keyword `all'. Ranges consist |
1570 | of two numbers separated by a hyphen. Examples: | |
c906108c | 1571 | |
c5aa993b JM |
1572 | thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4 |
1573 | thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9 | |
224608c3 | 1574 | thread apply all x/i $pc Apply x/i $pc cmd to all threads. */ |
c906108c SS |
1575 | |
1576 | static void | |
5fed81ff | 1577 | thread_apply_all_command (const char *cmd, int from_tty) |
c906108c | 1578 | { |
491144b5 | 1579 | bool ascending = false; |
1fe75df7 PW |
1580 | qcs_flags flags; |
1581 | ||
6665660a PA |
1582 | auto group = make_thread_apply_all_options_def_group (&ascending, |
1583 | &flags); | |
1584 | gdb::option::process_options | |
1585 | (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group); | |
1fe75df7 | 1586 | |
6665660a | 1587 | validate_flags_qcs ("thread apply all", &flags); |
253828f1 | 1588 | |
c906108c | 1589 | if (cmd == NULL || *cmd == '\000') |
1fe75df7 | 1590 | error (_("Please specify a command at the end of 'thread apply all'")); |
94cc34af | 1591 | |
dc146f7c | 1592 | update_thread_list (); |
e9d196c5 | 1593 | |
c6609450 | 1594 | int tc = live_threads_count (); |
a25d8bf9 | 1595 | if (tc != 0) |
054e8d9e | 1596 | { |
c6609450 PA |
1597 | /* Save a copy of the thread list and increment each thread's |
1598 | refcount while executing the command in the context of each | |
1599 | thread, in case the command is one that wipes threads. E.g., | |
1600 | detach, kill, disconnect, etc., or even normally continuing | |
1601 | over an inferior or thread exit. */ | |
bfcb9db8 | 1602 | std::vector<thread_info_ref> thr_list_cpy; |
c6609450 | 1603 | thr_list_cpy.reserve (tc); |
054e8d9e | 1604 | |
08036331 | 1605 | for (thread_info *tp : all_non_exited_threads ()) |
bfcb9db8 | 1606 | thr_list_cpy.push_back (thread_info_ref::new_reference (tp)); |
08036331 | 1607 | gdb_assert (thr_list_cpy.size () == tc); |
054e8d9e | 1608 | |
6665660a PA |
1609 | auto *sorter = (ascending |
1610 | ? tp_array_compar_ascending | |
1611 | : tp_array_compar_descending); | |
1612 | std::sort (thr_list_cpy.begin (), thr_list_cpy.end (), sorter); | |
054e8d9e | 1613 | |
5ed8105e PA |
1614 | scoped_restore_current_thread restore_thread; |
1615 | ||
bfcb9db8 TT |
1616 | for (thread_info_ref &thr : thr_list_cpy) |
1617 | if (switch_to_thread_if_alive (thr.get ())) | |
bc75fb44 | 1618 | thread_try_catch_cmd (thr.get (), {}, cmd, from_tty, flags); |
054e8d9e | 1619 | } |
c906108c SS |
1620 | } |
1621 | ||
6665660a PA |
1622 | /* Completer for "thread apply [ID list]". */ |
1623 | ||
1624 | static void | |
1625 | thread_apply_command_completer (cmd_list_element *ignore, | |
1626 | completion_tracker &tracker, | |
1627 | const char *text, const char * /*word*/) | |
1628 | { | |
1629 | /* Don't leave this to complete_options because there's an early | |
1630 | return below. */ | |
1631 | tracker.set_use_custom_word_point (true); | |
1632 | ||
1633 | tid_range_parser parser; | |
1634 | parser.init (text, current_inferior ()->num); | |
1635 | ||
1636 | try | |
1637 | { | |
1638 | while (!parser.finished ()) | |
1639 | { | |
1640 | int inf_num, thr_start, thr_end; | |
1641 | ||
1642 | if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end)) | |
1643 | break; | |
1644 | ||
1645 | if (parser.in_star_range () || parser.in_thread_range ()) | |
1646 | parser.skip_range (); | |
1647 | } | |
1648 | } | |
1649 | catch (const gdb_exception_error &ex) | |
1650 | { | |
1651 | /* get_tid_range throws if it parses a negative number, for | |
1652 | example. But a seemingly negative number may be the start of | |
1653 | an option instead. */ | |
1654 | } | |
1655 | ||
1656 | const char *cmd = parser.cur_tok (); | |
1657 | ||
1658 | if (cmd == text) | |
1659 | { | |
1660 | /* No thread ID list yet. */ | |
1661 | return; | |
1662 | } | |
1663 | ||
1664 | /* Check if we're past a valid thread ID list already. */ | |
1665 | if (parser.finished () | |
1666 | && cmd > text && !isspace (cmd[-1])) | |
1667 | return; | |
1668 | ||
1669 | /* We're past the thread ID list, advance word point. */ | |
1670 | tracker.advance_custom_word_point_by (cmd - text); | |
1671 | text = cmd; | |
1672 | ||
1673 | const auto group = make_thread_apply_options_def_group (nullptr); | |
1674 | if (gdb::option::complete_options | |
1675 | (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group)) | |
1676 | return; | |
1677 | ||
1678 | complete_nested_command_line (tracker, text); | |
1679 | } | |
1680 | ||
1681 | /* Completer for "thread apply all". */ | |
1682 | ||
1683 | static void | |
1684 | thread_apply_all_command_completer (cmd_list_element *ignore, | |
1685 | completion_tracker &tracker, | |
1686 | const char *text, const char *word) | |
1687 | { | |
1688 | const auto group = make_thread_apply_all_options_def_group (nullptr, | |
1689 | nullptr); | |
1690 | if (gdb::option::complete_options | |
1691 | (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group)) | |
1692 | return; | |
1693 | ||
1694 | complete_nested_command_line (tracker, text); | |
1695 | } | |
1696 | ||
43792cf0 PA |
1697 | /* Implementation of the "thread apply" command. */ |
1698 | ||
c906108c | 1699 | static void |
981a3fb3 | 1700 | thread_apply_command (const char *tidlist, int from_tty) |
c906108c | 1701 | { |
1fe75df7 | 1702 | qcs_flags flags; |
95a6b0a1 | 1703 | const char *cmd = NULL; |
bfd28288 | 1704 | tid_range_parser parser; |
c906108c SS |
1705 | |
1706 | if (tidlist == NULL || *tidlist == '\000') | |
8a3fe4f8 | 1707 | error (_("Please specify a thread ID list")); |
c906108c | 1708 | |
bfd28288 PA |
1709 | parser.init (tidlist, current_inferior ()->num); |
1710 | while (!parser.finished ()) | |
3f5b7598 PA |
1711 | { |
1712 | int inf_num, thr_start, thr_end; | |
c906108c | 1713 | |
bfd28288 | 1714 | if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end)) |
b9a3f842 | 1715 | break; |
3f5b7598 PA |
1716 | } |
1717 | ||
b9a3f842 PA |
1718 | cmd = parser.cur_tok (); |
1719 | ||
6665660a PA |
1720 | auto group = make_thread_apply_options_def_group (&flags); |
1721 | gdb::option::process_options | |
1722 | (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group); | |
1723 | ||
1724 | validate_flags_qcs ("thread apply", &flags); | |
1fe75df7 | 1725 | |
b9a3f842 | 1726 | if (*cmd == '\0') |
8a3fe4f8 | 1727 | error (_("Please specify a command following the thread ID list")); |
c906108c | 1728 | |
f7e13587 | 1729 | if (tidlist == cmd || isdigit (cmd[0])) |
3f5b7598 PA |
1730 | invalid_thread_id_error (cmd); |
1731 | ||
5ed8105e | 1732 | scoped_restore_current_thread restore_thread; |
c906108c | 1733 | |
bfd28288 | 1734 | parser.init (tidlist, current_inferior ()->num); |
b9a3f842 | 1735 | while (!parser.finished ()) |
5d5658a1 PA |
1736 | { |
1737 | struct thread_info *tp = NULL; | |
1738 | struct inferior *inf; | |
1739 | int inf_num, thr_num; | |
65fc9b77 | 1740 | |
bfd28288 | 1741 | parser.get_tid (&inf_num, &thr_num); |
5d5658a1 PA |
1742 | inf = find_inferior_id (inf_num); |
1743 | if (inf != NULL) | |
1744 | tp = find_thread_id (inf, thr_num); | |
71ef29a8 | 1745 | |
bfd28288 | 1746 | if (parser.in_star_range ()) |
71ef29a8 PA |
1747 | { |
1748 | if (inf == NULL) | |
1749 | { | |
1750 | warning (_("Unknown inferior %d"), inf_num); | |
bfd28288 | 1751 | parser.skip_range (); |
71ef29a8 PA |
1752 | continue; |
1753 | } | |
1754 | ||
1755 | /* No use looking for threads past the highest thread number | |
1756 | the inferior ever had. */ | |
1757 | if (thr_num >= inf->highest_thread_num) | |
bfd28288 | 1758 | parser.skip_range (); |
71ef29a8 PA |
1759 | |
1760 | /* Be quiet about unknown threads numbers. */ | |
1761 | if (tp == NULL) | |
1762 | continue; | |
1763 | } | |
1764 | ||
5d5658a1 PA |
1765 | if (tp == NULL) |
1766 | { | |
bfd28288 | 1767 | if (show_inferior_qualified_tids () || parser.tid_is_qualified ()) |
5d5658a1 PA |
1768 | warning (_("Unknown thread %d.%d"), inf_num, thr_num); |
1769 | else | |
1770 | warning (_("Unknown thread %d"), thr_num); | |
1771 | continue; | |
1772 | } | |
c906108c | 1773 | |
f3f8ece4 | 1774 | if (!switch_to_thread_if_alive (tp)) |
65ebfb52 | 1775 | { |
5d5658a1 PA |
1776 | warning (_("Thread %s has terminated."), print_thread_id (tp)); |
1777 | continue; | |
1778 | } | |
4f8d22e3 | 1779 | |
bc75fb44 | 1780 | thread_try_catch_cmd (tp, {}, cmd, from_tty, flags); |
c906108c SS |
1781 | } |
1782 | } | |
1783 | ||
1fe75df7 PW |
1784 | |
1785 | /* Implementation of the "taas" command. */ | |
1786 | ||
1787 | static void | |
1788 | taas_command (const char *cmd, int from_tty) | |
1789 | { | |
e0fad1ea PW |
1790 | if (cmd == NULL || *cmd == '\0') |
1791 | error (_("Please specify a command to apply on all threads")); | |
1fe75df7 PW |
1792 | std::string expanded = std::string ("thread apply all -s ") + cmd; |
1793 | execute_command (expanded.c_str (), from_tty); | |
1794 | } | |
1795 | ||
1796 | /* Implementation of the "tfaas" command. */ | |
1797 | ||
1798 | static void | |
1799 | tfaas_command (const char *cmd, int from_tty) | |
1800 | { | |
e0fad1ea PW |
1801 | if (cmd == NULL || *cmd == '\0') |
1802 | error (_("Please specify a command to apply on all frames of all threads")); | |
1fe75df7 | 1803 | std::string expanded |
5d707134 | 1804 | = std::string ("thread apply all -s -- frame apply all -s ") + cmd; |
1fe75df7 PW |
1805 | execute_command (expanded.c_str (), from_tty); |
1806 | } | |
1807 | ||
224608c3 | 1808 | /* Switch to the specified thread, or print the current thread. */ |
c906108c | 1809 | |
f0e8c4c5 | 1810 | void |
981a3fb3 | 1811 | thread_command (const char *tidstr, int from_tty) |
c906108c | 1812 | { |
4034d0ff | 1813 | if (tidstr == NULL) |
c906108c | 1814 | { |
9295a5a9 | 1815 | if (inferior_ptid == null_ptid) |
d729566a PA |
1816 | error (_("No thread selected")); |
1817 | ||
841de120 | 1818 | if (target_has_stack ()) |
4f8d22e3 | 1819 | { |
43792cf0 PA |
1820 | struct thread_info *tp = inferior_thread (); |
1821 | ||
00431a78 | 1822 | if (tp->state == THREAD_EXITED) |
6cb06a8c TT |
1823 | gdb_printf (_("[Current thread is %s (%s) (exited)]\n"), |
1824 | print_thread_id (tp), | |
1825 | target_pid_to_str (inferior_ptid).c_str ()); | |
4f8d22e3 | 1826 | else |
6cb06a8c TT |
1827 | gdb_printf (_("[Current thread is %s (%s)]\n"), |
1828 | print_thread_id (tp), | |
1829 | target_pid_to_str (inferior_ptid).c_str ()); | |
4f8d22e3 | 1830 | } |
c906108c | 1831 | else |
8a3fe4f8 | 1832 | error (_("No stack.")); |
c906108c | 1833 | } |
4034d0ff AT |
1834 | else |
1835 | { | |
1836 | ptid_t previous_ptid = inferior_ptid; | |
4034d0ff | 1837 | |
65630365 | 1838 | thread_select (tidstr, parse_thread_id (tidstr, NULL)); |
c5394b80 | 1839 | |
ae0eee42 PA |
1840 | /* Print if the thread has not changed, otherwise an event will |
1841 | be sent. */ | |
9295a5a9 | 1842 | if (inferior_ptid == previous_ptid) |
4034d0ff AT |
1843 | { |
1844 | print_selected_thread_frame (current_uiout, | |
1845 | USER_SELECTED_THREAD | |
1846 | | USER_SELECTED_FRAME); | |
1847 | } | |
1848 | else | |
1849 | { | |
76727919 TT |
1850 | gdb::observers::user_selected_context_changed.notify |
1851 | (USER_SELECTED_THREAD | USER_SELECTED_FRAME); | |
4034d0ff AT |
1852 | } |
1853 | } | |
c5394b80 JM |
1854 | } |
1855 | ||
4694da01 TT |
1856 | /* Implementation of `thread name'. */ |
1857 | ||
1858 | static void | |
fc41a75b | 1859 | thread_name_command (const char *arg, int from_tty) |
4694da01 TT |
1860 | { |
1861 | struct thread_info *info; | |
1862 | ||
9295a5a9 | 1863 | if (inferior_ptid == null_ptid) |
4694da01 TT |
1864 | error (_("No thread selected")); |
1865 | ||
529480d0 | 1866 | arg = skip_spaces (arg); |
4694da01 TT |
1867 | |
1868 | info = inferior_thread (); | |
25558938 | 1869 | info->set_name (arg != nullptr ? make_unique_xstrdup (arg) : nullptr); |
4694da01 TT |
1870 | } |
1871 | ||
60f98dde MS |
1872 | /* Find thread ids with a name, target pid, or extra info matching ARG. */ |
1873 | ||
1874 | static void | |
fc41a75b | 1875 | thread_find_command (const char *arg, int from_tty) |
60f98dde | 1876 | { |
73ede765 | 1877 | const char *tmp; |
60f98dde MS |
1878 | unsigned long match = 0; |
1879 | ||
1880 | if (arg == NULL || *arg == '\0') | |
1881 | error (_("Command requires an argument.")); | |
1882 | ||
1883 | tmp = re_comp (arg); | |
1884 | if (tmp != 0) | |
1885 | error (_("Invalid regexp (%s): %s"), tmp, arg); | |
1886 | ||
e8ef12b9 PA |
1887 | /* We're going to be switching threads. */ |
1888 | scoped_restore_current_thread restore_thread; | |
1889 | ||
60f98dde | 1890 | update_thread_list (); |
e8ef12b9 | 1891 | |
08036331 | 1892 | for (thread_info *tp : all_threads ()) |
60f98dde | 1893 | { |
e8ef12b9 PA |
1894 | switch_to_inferior_no_thread (tp->inf); |
1895 | ||
25558938 | 1896 | if (tp->name () != nullptr && re_exec (tp->name ())) |
60f98dde | 1897 | { |
6cb06a8c TT |
1898 | gdb_printf (_("Thread %s has name '%s'\n"), |
1899 | print_thread_id (tp), tp->name ()); | |
60f98dde MS |
1900 | match++; |
1901 | } | |
1902 | ||
1903 | tmp = target_thread_name (tp); | |
1904 | if (tmp != NULL && re_exec (tmp)) | |
1905 | { | |
6cb06a8c TT |
1906 | gdb_printf (_("Thread %s has target name '%s'\n"), |
1907 | print_thread_id (tp), tmp); | |
60f98dde MS |
1908 | match++; |
1909 | } | |
1910 | ||
a068643d TT |
1911 | std::string name = target_pid_to_str (tp->ptid); |
1912 | if (!name.empty () && re_exec (name.c_str ())) | |
60f98dde | 1913 | { |
6cb06a8c TT |
1914 | gdb_printf (_("Thread %s has target id '%s'\n"), |
1915 | print_thread_id (tp), name.c_str ()); | |
60f98dde MS |
1916 | match++; |
1917 | } | |
1918 | ||
1919 | tmp = target_extra_thread_info (tp); | |
1920 | if (tmp != NULL && re_exec (tmp)) | |
1921 | { | |
6cb06a8c TT |
1922 | gdb_printf (_("Thread %s has extra info '%s'\n"), |
1923 | print_thread_id (tp), tmp); | |
60f98dde MS |
1924 | match++; |
1925 | } | |
1926 | } | |
1927 | if (!match) | |
6cb06a8c | 1928 | gdb_printf (_("No threads match '%s'\n"), arg); |
60f98dde MS |
1929 | } |
1930 | ||
93815fbf | 1931 | /* Print notices when new threads are attached and detached. */ |
491144b5 | 1932 | bool print_thread_events = true; |
93815fbf VP |
1933 | static void |
1934 | show_print_thread_events (struct ui_file *file, int from_tty, | |
ae0eee42 | 1935 | struct cmd_list_element *c, const char *value) |
93815fbf | 1936 | { |
6cb06a8c TT |
1937 | gdb_printf (file, |
1938 | _("Printing of thread events is %s.\n"), | |
1939 | value); | |
93815fbf VP |
1940 | } |
1941 | ||
65630365 | 1942 | /* See gdbthread.h. */ |
c906108c | 1943 | |
65630365 PA |
1944 | void |
1945 | thread_select (const char *tidstr, thread_info *tp) | |
1946 | { | |
f3f8ece4 | 1947 | if (!switch_to_thread_if_alive (tp)) |
5d5658a1 | 1948 | error (_("Thread ID %s has terminated."), tidstr); |
c906108c | 1949 | |
db5a7484 NR |
1950 | annotate_thread_changed (); |
1951 | ||
4034d0ff AT |
1952 | /* Since the current thread may have changed, see if there is any |
1953 | exited thread we can now delete. */ | |
a05575d3 | 1954 | delete_exited_threads (); |
4034d0ff AT |
1955 | } |
1956 | ||
1957 | /* Print thread and frame switch command response. */ | |
1958 | ||
1959 | void | |
1960 | print_selected_thread_frame (struct ui_out *uiout, | |
1961 | user_selected_what selection) | |
1962 | { | |
1963 | struct thread_info *tp = inferior_thread (); | |
4034d0ff AT |
1964 | |
1965 | if (selection & USER_SELECTED_THREAD) | |
5d5658a1 | 1966 | { |
112e8700 | 1967 | if (uiout->is_mi_like_p ()) |
4034d0ff | 1968 | { |
381befee TT |
1969 | uiout->field_signed ("new-thread-id", |
1970 | inferior_thread ()->global_num); | |
4034d0ff AT |
1971 | } |
1972 | else | |
1973 | { | |
112e8700 SM |
1974 | uiout->text ("[Switching to thread "); |
1975 | uiout->field_string ("new-thread-id", print_thread_id (tp)); | |
1976 | uiout->text (" ("); | |
4915bfdc | 1977 | uiout->text (target_pid_to_str (inferior_ptid)); |
112e8700 | 1978 | uiout->text (")]"); |
4034d0ff | 1979 | } |
5d5658a1 | 1980 | } |
c5394b80 | 1981 | |
30596231 | 1982 | if (tp->state == THREAD_RUNNING) |
98871305 | 1983 | { |
4034d0ff | 1984 | if (selection & USER_SELECTED_THREAD) |
112e8700 | 1985 | uiout->text ("(running)\n"); |
98871305 | 1986 | } |
4034d0ff AT |
1987 | else if (selection & USER_SELECTED_FRAME) |
1988 | { | |
1989 | if (selection & USER_SELECTED_THREAD) | |
112e8700 | 1990 | uiout->text ("\n"); |
4f8d22e3 | 1991 | |
4034d0ff AT |
1992 | if (has_stack_frames ()) |
1993 | print_stack_frame_to_uiout (uiout, get_selected_frame (NULL), | |
1994 | 1, SRC_AND_LOC, 1); | |
1995 | } | |
c5394b80 JM |
1996 | } |
1997 | ||
b57bacec | 1998 | /* Update the 'threads_executing' global based on the threads we know |
5b6d1e4f PA |
1999 | about right now. This is used by infrun to tell whether we should |
2000 | pull events out of the current target. */ | |
b57bacec PA |
2001 | |
2002 | static void | |
2003 | update_threads_executing (void) | |
2004 | { | |
5b6d1e4f PA |
2005 | process_stratum_target *targ = current_inferior ()->process_target (); |
2006 | ||
2007 | if (targ == NULL) | |
2008 | return; | |
2009 | ||
2010 | targ->threads_executing = false; | |
2011 | ||
2012 | for (inferior *inf : all_non_exited_inferiors (targ)) | |
b57bacec | 2013 | { |
5b6d1e4f PA |
2014 | if (!inf->has_execution ()) |
2015 | continue; | |
2016 | ||
2017 | /* If the process has no threads, then it must be we have a | |
2018 | process-exit event pending. */ | |
bf809310 | 2019 | if (inf->thread_list.empty ()) |
5b6d1e4f PA |
2020 | { |
2021 | targ->threads_executing = true; | |
2022 | return; | |
2023 | } | |
2024 | ||
2025 | for (thread_info *tp : inf->non_exited_threads ()) | |
b57bacec | 2026 | { |
611841bb | 2027 | if (tp->executing ()) |
5b6d1e4f PA |
2028 | { |
2029 | targ->threads_executing = true; | |
2030 | return; | |
2031 | } | |
b57bacec PA |
2032 | } |
2033 | } | |
2034 | } | |
2035 | ||
dc146f7c VP |
2036 | void |
2037 | update_thread_list (void) | |
2038 | { | |
e8032dde | 2039 | target_update_thread_list (); |
b57bacec | 2040 | update_threads_executing (); |
dc146f7c VP |
2041 | } |
2042 | ||
25558938 SM |
2043 | /* See gdbthread.h. */ |
2044 | ||
2045 | const char * | |
2046 | thread_name (thread_info *thread) | |
2047 | { | |
2048 | /* Use the manually set name if there is one. */ | |
2049 | const char *name = thread->name (); | |
2050 | if (name != nullptr) | |
2051 | return name; | |
2052 | ||
2053 | /* Otherwise, ask the target. Ensure we query the right target stack. */ | |
2054 | scoped_restore_current_thread restore_thread; | |
2055 | if (thread->inf != current_inferior ()) | |
2056 | switch_to_inferior_no_thread (thread->inf); | |
2057 | ||
2058 | return target_thread_name (thread); | |
2059 | } | |
2060 | ||
993248f4 AB |
2061 | /* See gdbthread.h. */ |
2062 | ||
2063 | const char * | |
2064 | thread_state_string (enum thread_state state) | |
2065 | { | |
2066 | switch (state) | |
2067 | { | |
2068 | case THREAD_STOPPED: | |
2069 | return "STOPPED"; | |
2070 | ||
2071 | case THREAD_RUNNING: | |
2072 | return "RUNNING"; | |
2073 | ||
2074 | case THREAD_EXITED: | |
2075 | return "EXITED"; | |
2076 | } | |
2077 | ||
2078 | gdb_assert_not_reached ("unknown thread state"); | |
2079 | } | |
2080 | ||
663f6d42 PA |
2081 | /* Return a new value for the selected thread's id. Return a value of |
2082 | 0 if no thread is selected. If GLOBAL is true, return the thread's | |
2083 | global number. Otherwise return the per-inferior number. */ | |
2084 | ||
2085 | static struct value * | |
2086 | thread_num_make_value_helper (struct gdbarch *gdbarch, int global) | |
2087 | { | |
663f6d42 PA |
2088 | int int_val; |
2089 | ||
00431a78 | 2090 | if (inferior_ptid == null_ptid) |
663f6d42 | 2091 | int_val = 0; |
663f6d42 | 2092 | else |
00431a78 PA |
2093 | { |
2094 | thread_info *tp = inferior_thread (); | |
2095 | if (global) | |
2096 | int_val = tp->global_num; | |
2097 | else | |
2098 | int_val = tp->per_inf_num; | |
2099 | } | |
663f6d42 PA |
2100 | |
2101 | return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val); | |
2102 | } | |
2103 | ||
5d5658a1 PA |
2104 | /* Return a new value for the selected thread's per-inferior thread |
2105 | number. Return a value of 0 if no thread is selected, or no | |
2106 | threads exist. */ | |
6aed2dbc SS |
2107 | |
2108 | static struct value * | |
ae0eee42 PA |
2109 | thread_id_per_inf_num_make_value (struct gdbarch *gdbarch, |
2110 | struct internalvar *var, | |
663f6d42 | 2111 | void *ignore) |
6aed2dbc | 2112 | { |
663f6d42 PA |
2113 | return thread_num_make_value_helper (gdbarch, 0); |
2114 | } | |
2115 | ||
2116 | /* Return a new value for the selected thread's global id. Return a | |
2117 | value of 0 if no thread is selected, or no threads exist. */ | |
6aed2dbc | 2118 | |
663f6d42 PA |
2119 | static struct value * |
2120 | global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, | |
2121 | void *ignore) | |
2122 | { | |
2123 | return thread_num_make_value_helper (gdbarch, 1); | |
6aed2dbc SS |
2124 | } |
2125 | ||
cbda14de AB |
2126 | /* Return a new value for the number of non-exited threads in the current |
2127 | inferior. If there are no threads in the current inferior return a | |
2128 | value of 0. */ | |
2129 | ||
2130 | static struct value * | |
2131 | inferior_thread_count_make_value (struct gdbarch *gdbarch, | |
2132 | struct internalvar *var, void *ignore) | |
2133 | { | |
2134 | int int_val = 0; | |
2135 | ||
2136 | if (inferior_ptid != null_ptid) | |
2137 | int_val = current_inferior ()->non_exited_threads ().size (); | |
2138 | ||
2139 | return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val); | |
2140 | } | |
2141 | ||
c906108c SS |
2142 | /* Commands with a prefix of `thread'. */ |
2143 | struct cmd_list_element *thread_cmd_list = NULL; | |
2144 | ||
22d2b532 SDJ |
2145 | /* Implementation of `thread' variable. */ |
2146 | ||
2147 | static const struct internalvar_funcs thread_funcs = | |
2148 | { | |
663f6d42 PA |
2149 | thread_id_per_inf_num_make_value, |
2150 | NULL, | |
663f6d42 PA |
2151 | }; |
2152 | ||
2153 | /* Implementation of `gthread' variable. */ | |
2154 | ||
2155 | static const struct internalvar_funcs gthread_funcs = | |
2156 | { | |
2157 | global_thread_id_make_value, | |
22d2b532 | 2158 | NULL, |
22d2b532 SDJ |
2159 | }; |
2160 | ||
cbda14de AB |
2161 | /* Implementation of `_inferior_thread_count` convenience variable. */ |
2162 | ||
2163 | static const struct internalvar_funcs inferior_thread_count_funcs = | |
2164 | { | |
2165 | inferior_thread_count_make_value, | |
2166 | NULL, | |
2167 | }; | |
2168 | ||
6c265988 | 2169 | void _initialize_thread (); |
c906108c | 2170 | void |
6c265988 | 2171 | _initialize_thread () |
c906108c SS |
2172 | { |
2173 | static struct cmd_list_element *thread_apply_list = NULL; | |
5d707134 | 2174 | cmd_list_element *c; |
c906108c | 2175 | |
54d66006 PA |
2176 | const auto info_threads_opts = make_info_threads_options_def_group (nullptr); |
2177 | ||
2178 | /* Note: keep this "ID" in sync with what "info threads [TAB]" | |
2179 | suggests. */ | |
2180 | static std::string info_threads_help | |
2181 | = gdb::option::build_help (_("\ | |
2182 | Display currently known threads.\n\ | |
2183 | Usage: info threads [OPTION]... [ID]...\n\ | |
3c6eb4d4 TBA |
2184 | If ID is given, it is a space-separated list of IDs of threads to display.\n\ |
2185 | Otherwise, all threads are displayed.\n\ | |
54d66006 PA |
2186 | \n\ |
2187 | Options:\n\ | |
3c6eb4d4 | 2188 | %OPTIONS%"), |
54d66006 PA |
2189 | info_threads_opts); |
2190 | ||
2191 | c = add_info ("threads", info_threads_command, info_threads_help.c_str ()); | |
2192 | set_cmd_completer_handle_brkchars (c, info_threads_command_completer); | |
c906108c | 2193 | |
3947f654 SM |
2194 | cmd_list_element *thread_cmd |
2195 | = add_prefix_cmd ("thread", class_run, thread_command, _("\ | |
1bedd215 AC |
2196 | Use this command to switch between threads.\n\ |
2197 | The new thread ID must be currently known."), | |
3947f654 SM |
2198 | &thread_cmd_list, 1, &cmdlist); |
2199 | ||
2200 | add_com_alias ("t", thread_cmd, class_run, 1); | |
c906108c | 2201 | |
6665660a | 2202 | #define THREAD_APPLY_OPTION_HELP "\ |
1fe75df7 PW |
2203 | Prints per-inferior thread number and target system's thread id\n\ |
2204 | followed by COMMAND output.\n\ | |
6665660a PA |
2205 | \n\ |
2206 | By default, an error raised during the execution of COMMAND\n\ | |
2207 | aborts \"thread apply\".\n\ | |
2208 | \n\ | |
2209 | Options:\n\ | |
2210 | %OPTIONS%" | |
2211 | ||
2212 | const auto thread_apply_opts = make_thread_apply_options_def_group (nullptr); | |
2213 | ||
8abfcabc | 2214 | static std::string thread_apply_help = gdb::option::build_help (_("\ |
6665660a PA |
2215 | Apply a command to a list of threads.\n\ |
2216 | Usage: thread apply ID... [OPTION]... COMMAND\n\ | |
1fe75df7 | 2217 | ID is a space-separated list of IDs of threads to apply COMMAND on.\n" |
6665660a PA |
2218 | THREAD_APPLY_OPTION_HELP), |
2219 | thread_apply_opts); | |
2220 | ||
2221 | c = add_prefix_cmd ("apply", class_run, thread_apply_command, | |
2222 | thread_apply_help.c_str (), | |
2f822da5 | 2223 | &thread_apply_list, 1, |
6665660a PA |
2224 | &thread_cmd_list); |
2225 | set_cmd_completer_handle_brkchars (c, thread_apply_command_completer); | |
c906108c | 2226 | |
6665660a PA |
2227 | const auto thread_apply_all_opts |
2228 | = make_thread_apply_all_options_def_group (nullptr, nullptr); | |
2229 | ||
8abfcabc | 2230 | static std::string thread_apply_all_help = gdb::option::build_help (_("\ |
253828f1 JK |
2231 | Apply a command to all threads.\n\ |
2232 | \n\ | |
6665660a PA |
2233 | Usage: thread apply all [OPTION]... COMMAND\n" |
2234 | THREAD_APPLY_OPTION_HELP), | |
2235 | thread_apply_all_opts); | |
2236 | ||
2237 | c = add_cmd ("all", class_run, thread_apply_all_command, | |
2238 | thread_apply_all_help.c_str (), | |
2239 | &thread_apply_list); | |
2240 | set_cmd_completer_handle_brkchars (c, thread_apply_all_command_completer); | |
c906108c | 2241 | |
6665660a | 2242 | c = add_com ("taas", class_run, taas_command, _("\ |
1fe75df7 | 2243 | Apply a command to all threads (ignoring errors and empty output).\n\ |
6665660a PA |
2244 | Usage: taas [OPTION]... COMMAND\n\ |
2245 | shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\ | |
2246 | See \"help thread apply all\" for available options.")); | |
2247 | set_cmd_completer_handle_brkchars (c, thread_apply_all_command_completer); | |
1fe75df7 | 2248 | |
5d707134 | 2249 | c = add_com ("tfaas", class_run, tfaas_command, _("\ |
1fe75df7 | 2250 | Apply a command to all frames of all threads (ignoring errors and empty output).\n\ |
5d707134 PA |
2251 | Usage: tfaas [OPTION]... COMMAND\n\ |
2252 | shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\ | |
2253 | See \"help frame apply all\" for available options.")); | |
2254 | set_cmd_completer_handle_brkchars (c, frame_apply_all_cmd_completer); | |
1fe75df7 | 2255 | |
4694da01 TT |
2256 | add_cmd ("name", class_run, thread_name_command, |
2257 | _("Set the current thread's name.\n\ | |
2258 | Usage: thread name [NAME]\n\ | |
2259 | If NAME is not given, then any existing name is removed."), &thread_cmd_list); | |
2260 | ||
60f98dde MS |
2261 | add_cmd ("find", class_run, thread_find_command, _("\ |
2262 | Find threads that match a regular expression.\n\ | |
2263 | Usage: thread find REGEXP\n\ | |
2264 | Will display thread ids whose name, target ID, or extra info matches REGEXP."), | |
2265 | &thread_cmd_list); | |
2266 | ||
93815fbf | 2267 | add_setshow_boolean_cmd ("thread-events", no_class, |
ae0eee42 | 2268 | &print_thread_events, _("\ |
11c68c47 EZ |
2269 | Set printing of thread events (such as thread start and exit)."), _("\ |
2270 | Show printing of thread events (such as thread start and exit)."), NULL, | |
ae0eee42 PA |
2271 | NULL, |
2272 | show_print_thread_events, | |
2273 | &setprintlist, &showprintlist); | |
6aed2dbc | 2274 | |
5b0a3d62 AB |
2275 | add_setshow_boolean_cmd ("threads", class_maintenance, &debug_threads, _("\ |
2276 | Set thread debugging."), _("\ | |
2277 | Show thread debugging."), _("\ | |
2278 | When on messages about thread creation and deletion are printed."), | |
2279 | nullptr, | |
2280 | show_debug_threads, | |
2281 | &setdebuglist, &showdebuglist); | |
2282 | ||
22d2b532 | 2283 | create_internalvar_type_lazy ("_thread", &thread_funcs, NULL); |
663f6d42 | 2284 | create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL); |
cbda14de AB |
2285 | create_internalvar_type_lazy ("_inferior_thread_count", |
2286 | &inferior_thread_count_funcs, NULL); | |
c906108c | 2287 | } |