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