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