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