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