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