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