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