Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Multi-process/thread control for GDB, the GNU debugger. |
8926118c | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
0fb0cc75 JB |
4 | 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009 |
5 | Free Software Foundation, Inc. | |
8926118c | 6 | |
b6ba6518 | 7 | Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA. |
c906108c | 8 | |
c5aa993b | 9 | This file is part of GDB. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 13 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 14 | (at your option) any later version. |
c906108c | 15 | |
c5aa993b JM |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
c906108c | 20 | |
c5aa993b | 21 | You should have received a copy of the GNU General Public License |
a9762ec7 | 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "symtab.h" | |
26 | #include "frame.h" | |
27 | #include "inferior.h" | |
28 | #include "environ.h" | |
29 | #include "value.h" | |
30 | #include "target.h" | |
31 | #include "gdbthread.h" | |
60250e8b | 32 | #include "exceptions.h" |
c906108c SS |
33 | #include "command.h" |
34 | #include "gdbcmd.h" | |
4e052eda | 35 | #include "regcache.h" |
5b7f31a4 | 36 | #include "gdb.h" |
b66d6d2e | 37 | #include "gdb_string.h" |
c906108c SS |
38 | |
39 | #include <ctype.h> | |
40 | #include <sys/types.h> | |
41 | #include <signal.h> | |
8b93c638 | 42 | #include "ui-out.h" |
683f2885 | 43 | #include "observer.h" |
d4fc5b1e | 44 | #include "annotate.h" |
94cc34af PA |
45 | #include "cli/cli-decode.h" |
46 | ||
0d06e24b | 47 | /* Definition of struct thread_info exported to gdbthread.h */ |
c906108c SS |
48 | |
49 | /* Prototypes for exported functions. */ | |
50 | ||
a14ed312 | 51 | void _initialize_thread (void); |
c906108c SS |
52 | |
53 | /* Prototypes for local functions. */ | |
54 | ||
c906108c SS |
55 | static struct thread_info *thread_list = NULL; |
56 | static int highest_thread_num; | |
57 | ||
a14ed312 KB |
58 | static void thread_command (char *tidstr, int from_tty); |
59 | static void thread_apply_all_command (char *, int); | |
60 | static int thread_alive (struct thread_info *); | |
61 | static void info_threads_command (char *, int); | |
62 | static void thread_apply_command (char *, int); | |
39f77062 | 63 | static void restore_current_thread (ptid_t); |
a14ed312 | 64 | static void prune_threads (void); |
c906108c | 65 | |
4f8d22e3 PA |
66 | /* Frontend view of the thread state. Possible extensions: stepping, |
67 | finishing, until(ling),... */ | |
68 | enum thread_state | |
69 | { | |
70 | THREAD_STOPPED, | |
71 | THREAD_RUNNING, | |
72 | THREAD_EXITED, | |
73 | }; | |
74 | ||
4e1c45ea PA |
75 | extern struct thread_info* |
76 | inferior_thread (void) | |
8601f500 | 77 | { |
4e1c45ea PA |
78 | struct thread_info *tp = find_thread_pid (inferior_ptid); |
79 | gdb_assert (tp); | |
80 | return tp; | |
81 | } | |
8601f500 | 82 | |
4e1c45ea PA |
83 | void |
84 | delete_step_resume_breakpoint (struct thread_info *tp) | |
85 | { | |
86 | if (tp && tp->step_resume_breakpoint) | |
8601f500 | 87 | { |
4e1c45ea PA |
88 | delete_breakpoint (tp->step_resume_breakpoint); |
89 | tp->step_resume_breakpoint = NULL; | |
8601f500 MS |
90 | } |
91 | } | |
92 | ||
7c952b6d | 93 | static void |
4f8d22e3 | 94 | clear_thread_inferior_resources (struct thread_info *tp) |
7c952b6d ND |
95 | { |
96 | /* NOTE: this will take care of any left-over step_resume breakpoints, | |
4d8453a5 DJ |
97 | but not any user-specified thread-specific breakpoints. We can not |
98 | delete the breakpoint straight-off, because the inferior might not | |
99 | be stopped at the moment. */ | |
7c952b6d | 100 | if (tp->step_resume_breakpoint) |
4f8d22e3 PA |
101 | { |
102 | tp->step_resume_breakpoint->disposition = disp_del_at_next_stop; | |
103 | tp->step_resume_breakpoint = NULL; | |
104 | } | |
7c952b6d | 105 | |
a474d7c2 | 106 | bpstat_clear (&tp->stop_bpstat); |
95e54da7 PA |
107 | |
108 | discard_all_intermediate_continuations_thread (tp); | |
109 | discard_all_continuations_thread (tp); | |
4f8d22e3 PA |
110 | } |
111 | ||
112 | static void | |
113 | free_thread (struct thread_info *tp) | |
114 | { | |
115 | clear_thread_inferior_resources (tp); | |
a474d7c2 | 116 | |
7c952b6d ND |
117 | /* FIXME: do I ever need to call the back-end to give it a |
118 | chance at this private data before deleting the thread? */ | |
119 | if (tp->private) | |
b8c9b27d | 120 | xfree (tp->private); |
7c952b6d | 121 | |
b8c9b27d | 122 | xfree (tp); |
7c952b6d ND |
123 | } |
124 | ||
c906108c | 125 | void |
fba45db2 | 126 | init_thread_list (void) |
c906108c SS |
127 | { |
128 | struct thread_info *tp, *tpnext; | |
129 | ||
7c952b6d | 130 | highest_thread_num = 0; |
8ea051c5 | 131 | |
c906108c SS |
132 | if (!thread_list) |
133 | return; | |
134 | ||
135 | for (tp = thread_list; tp; tp = tpnext) | |
136 | { | |
137 | tpnext = tp->next; | |
7c952b6d | 138 | free_thread (tp); |
c906108c SS |
139 | } |
140 | ||
141 | thread_list = NULL; | |
c906108c SS |
142 | } |
143 | ||
0d06e24b | 144 | struct thread_info * |
93815fbf | 145 | add_thread_silent (ptid_t ptid) |
c906108c SS |
146 | { |
147 | struct thread_info *tp; | |
148 | ||
4f8d22e3 PA |
149 | tp = find_thread_pid (ptid); |
150 | if (tp) | |
151 | /* Found an old thread with the same id. It has to be dead, | |
152 | otherwise we wouldn't be adding a new thread with the same id. | |
153 | The OS is reusing this id --- delete it, and recreate a new | |
154 | one. */ | |
155 | { | |
156 | /* In addition to deleting the thread, if this is the current | |
dcf4fbde PA |
157 | thread, then we need to take care that delete_thread doesn't |
158 | really delete the thread if it is inferior_ptid. Create a | |
159 | new template thread in the list with an invalid ptid, switch | |
160 | to it, delete the original thread, reset the new thread's | |
161 | ptid, and switch to it. */ | |
4f8d22e3 PA |
162 | |
163 | if (ptid_equal (inferior_ptid, ptid)) | |
164 | { | |
165 | tp = xmalloc (sizeof (*tp)); | |
166 | memset (tp, 0, sizeof (*tp)); | |
167 | tp->ptid = minus_one_ptid; | |
168 | tp->num = ++highest_thread_num; | |
169 | tp->next = thread_list; | |
170 | thread_list = tp; | |
dcf4fbde PA |
171 | |
172 | /* Make switch_to_thread not read from the thread. */ | |
173 | tp->state_ = THREAD_EXITED; | |
174 | switch_to_thread (minus_one_ptid); | |
4f8d22e3 PA |
175 | |
176 | /* Now we can delete it. */ | |
177 | delete_thread (ptid); | |
178 | ||
dcf4fbde | 179 | /* Now reset its ptid, and reswitch inferior_ptid to it. */ |
4f8d22e3 | 180 | tp->ptid = ptid; |
dcf4fbde | 181 | tp->state_ = THREAD_STOPPED; |
4f8d22e3 PA |
182 | switch_to_thread (ptid); |
183 | ||
184 | observer_notify_new_thread (tp); | |
185 | ||
186 | /* All done. */ | |
187 | return tp; | |
188 | } | |
189 | else | |
190 | /* Just go ahead and delete it. */ | |
191 | delete_thread (ptid); | |
192 | } | |
193 | ||
6c0d3f6a MS |
194 | tp = (struct thread_info *) xmalloc (sizeof (*tp)); |
195 | memset (tp, 0, sizeof (*tp)); | |
39f77062 | 196 | tp->ptid = ptid; |
c906108c | 197 | tp->num = ++highest_thread_num; |
c906108c SS |
198 | tp->next = thread_list; |
199 | thread_list = tp; | |
cfc01461 VP |
200 | |
201 | observer_notify_new_thread (tp); | |
202 | ||
0d06e24b | 203 | return tp; |
c906108c SS |
204 | } |
205 | ||
93815fbf | 206 | struct thread_info * |
17faa917 | 207 | add_thread_with_info (ptid_t ptid, struct private_thread_info *private) |
93815fbf VP |
208 | { |
209 | struct thread_info *result = add_thread_silent (ptid); | |
210 | ||
17faa917 DJ |
211 | result->private = private; |
212 | ||
93815fbf | 213 | if (print_thread_events) |
fd532e2e | 214 | printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid)); |
d4fc5b1e NR |
215 | |
216 | annotate_new_thread (); | |
93815fbf VP |
217 | return result; |
218 | } | |
219 | ||
17faa917 DJ |
220 | struct thread_info * |
221 | add_thread (ptid_t ptid) | |
222 | { | |
223 | return add_thread_with_info (ptid, NULL); | |
224 | } | |
225 | ||
5e0b29c1 PA |
226 | /* Delete thread PTID. If SILENT, don't notify the observer of this |
227 | exit. */ | |
228 | static void | |
229 | delete_thread_1 (ptid_t ptid, int silent) | |
c906108c SS |
230 | { |
231 | struct thread_info *tp, *tpprev; | |
232 | ||
233 | tpprev = NULL; | |
234 | ||
235 | for (tp = thread_list; tp; tpprev = tp, tp = tp->next) | |
39f77062 | 236 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
237 | break; |
238 | ||
239 | if (!tp) | |
240 | return; | |
241 | ||
4f8d22e3 PA |
242 | /* If this is the current thread, or there's code out there that |
243 | relies on it existing (refcount > 0) we can't delete yet. Mark | |
244 | it as exited, and notify it. */ | |
245 | if (tp->refcount > 0 | |
246 | || ptid_equal (tp->ptid, inferior_ptid)) | |
247 | { | |
248 | if (tp->state_ != THREAD_EXITED) | |
249 | { | |
250 | if (!silent) | |
251 | observer_notify_thread_exit (tp); | |
252 | ||
253 | /* Tag it as exited. */ | |
254 | tp->state_ = THREAD_EXITED; | |
255 | ||
256 | /* Clear breakpoints, etc. associated with this thread. */ | |
257 | clear_thread_inferior_resources (tp); | |
258 | } | |
259 | ||
260 | /* Will be really deleted some other time. */ | |
261 | return; | |
262 | } | |
263 | ||
c906108c SS |
264 | if (tpprev) |
265 | tpprev->next = tp->next; | |
266 | else | |
267 | thread_list = tp->next; | |
268 | ||
4f8d22e3 PA |
269 | /* Notify thread exit, but only if we haven't already. */ |
270 | if (!silent && tp->state_ != THREAD_EXITED) | |
5e0b29c1 | 271 | observer_notify_thread_exit (tp); |
063bfe2e | 272 | |
7c952b6d | 273 | free_thread (tp); |
c906108c SS |
274 | } |
275 | ||
4f8d22e3 PA |
276 | /* Delete thread PTID and notify of thread exit. If this is |
277 | inferior_ptid, don't actually delete it, but tag it as exited and | |
278 | do the notification. If PTID is the user selected thread, clear | |
279 | it. */ | |
5e0b29c1 PA |
280 | void |
281 | delete_thread (ptid_t ptid) | |
282 | { | |
283 | delete_thread_1 (ptid, 0 /* not silent */); | |
284 | } | |
285 | ||
286 | void | |
287 | delete_thread_silent (ptid_t ptid) | |
288 | { | |
289 | delete_thread_1 (ptid, 1 /* silent */); | |
290 | } | |
291 | ||
1e92afda | 292 | struct thread_info * |
fba45db2 | 293 | find_thread_id (int num) |
c906108c SS |
294 | { |
295 | struct thread_info *tp; | |
296 | ||
297 | for (tp = thread_list; tp; tp = tp->next) | |
298 | if (tp->num == num) | |
299 | return tp; | |
300 | ||
301 | return NULL; | |
302 | } | |
303 | ||
39f77062 | 304 | /* Find a thread_info by matching PTID. */ |
0d06e24b | 305 | struct thread_info * |
39f77062 | 306 | find_thread_pid (ptid_t ptid) |
0d06e24b JM |
307 | { |
308 | struct thread_info *tp; | |
309 | ||
310 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 311 | if (ptid_equal (tp->ptid, ptid)) |
0d06e24b JM |
312 | return tp; |
313 | ||
314 | return NULL; | |
315 | } | |
316 | ||
317 | /* | |
318 | * Thread iterator function. | |
319 | * | |
320 | * Calls a callback function once for each thread, so long as | |
321 | * the callback function returns false. If the callback function | |
322 | * returns true, the iteration will end and the current thread | |
323 | * will be returned. This can be useful for implementing a | |
324 | * search for a thread with arbitrary attributes, or for applying | |
325 | * some operation to every thread. | |
326 | * | |
327 | * FIXME: some of the existing functionality, such as | |
328 | * "Thread apply all", might be rewritten using this functionality. | |
329 | */ | |
330 | ||
331 | struct thread_info * | |
fd118b61 KB |
332 | iterate_over_threads (int (*callback) (struct thread_info *, void *), |
333 | void *data) | |
0d06e24b | 334 | { |
4f8d22e3 | 335 | struct thread_info *tp, *next; |
0d06e24b | 336 | |
4f8d22e3 PA |
337 | for (tp = thread_list; tp; tp = next) |
338 | { | |
339 | next = tp->next; | |
340 | if ((*callback) (tp, data)) | |
341 | return tp; | |
342 | } | |
0d06e24b JM |
343 | |
344 | return NULL; | |
345 | } | |
346 | ||
20874c92 VP |
347 | int |
348 | thread_count (void) | |
349 | { | |
350 | int result = 0; | |
351 | struct thread_info *tp; | |
352 | ||
353 | for (tp = thread_list; tp; tp = tp->next) | |
354 | ++result; | |
355 | ||
356 | return result; | |
357 | } | |
358 | ||
c906108c | 359 | int |
fba45db2 | 360 | valid_thread_id (int num) |
c906108c SS |
361 | { |
362 | struct thread_info *tp; | |
363 | ||
364 | for (tp = thread_list; tp; tp = tp->next) | |
365 | if (tp->num == num) | |
366 | return 1; | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
371 | int | |
39f77062 | 372 | pid_to_thread_id (ptid_t ptid) |
c906108c SS |
373 | { |
374 | struct thread_info *tp; | |
375 | ||
376 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 377 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
378 | return tp->num; |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
39f77062 | 383 | ptid_t |
fba45db2 | 384 | thread_id_to_pid (int num) |
c906108c SS |
385 | { |
386 | struct thread_info *thread = find_thread_id (num); | |
387 | if (thread) | |
39f77062 | 388 | return thread->ptid; |
c906108c | 389 | else |
39f77062 | 390 | return pid_to_ptid (-1); |
c906108c SS |
391 | } |
392 | ||
393 | int | |
39f77062 | 394 | in_thread_list (ptid_t ptid) |
c906108c SS |
395 | { |
396 | struct thread_info *tp; | |
397 | ||
398 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 399 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
400 | return 1; |
401 | ||
402 | return 0; /* Never heard of 'im */ | |
403 | } | |
8926118c | 404 | |
8b93c638 JM |
405 | /* Print a list of thread ids currently known, and the total number of |
406 | threads. To be used from within catch_errors. */ | |
6949171e JJ |
407 | static int |
408 | do_captured_list_thread_ids (struct ui_out *uiout, void *arg) | |
8b93c638 JM |
409 | { |
410 | struct thread_info *tp; | |
411 | int num = 0; | |
3b31d625 | 412 | struct cleanup *cleanup_chain; |
592375cd | 413 | int current_thread = -1; |
8b93c638 | 414 | |
7990a578 EZ |
415 | prune_threads (); |
416 | target_find_new_threads (); | |
417 | ||
3b31d625 | 418 | cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids"); |
8b93c638 JM |
419 | |
420 | for (tp = thread_list; tp; tp = tp->next) | |
421 | { | |
4f8d22e3 PA |
422 | if (tp->state_ == THREAD_EXITED) |
423 | continue; | |
592375cd VP |
424 | |
425 | if (ptid_equal (tp->ptid, inferior_ptid)) | |
426 | current_thread = tp->num; | |
427 | ||
8b93c638 JM |
428 | num++; |
429 | ui_out_field_int (uiout, "thread-id", tp->num); | |
430 | } | |
431 | ||
3b31d625 | 432 | do_cleanups (cleanup_chain); |
592375cd VP |
433 | |
434 | if (current_thread != -1) | |
435 | ui_out_field_int (uiout, "current-thread-id", current_thread); | |
8b93c638 JM |
436 | ui_out_field_int (uiout, "number-of-threads", num); |
437 | return GDB_RC_OK; | |
438 | } | |
439 | ||
440 | /* Official gdblib interface function to get a list of thread ids and | |
441 | the total number. */ | |
442 | enum gdb_rc | |
ce43223b | 443 | gdb_list_thread_ids (struct ui_out *uiout, char **error_message) |
8b93c638 | 444 | { |
b0b13bb4 DJ |
445 | if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL, |
446 | error_message, RETURN_MASK_ALL) < 0) | |
447 | return GDB_RC_FAIL; | |
448 | return GDB_RC_OK; | |
8b93c638 | 449 | } |
c906108c | 450 | |
c906108c SS |
451 | /* Return true if TP is an active thread. */ |
452 | static int | |
fba45db2 | 453 | thread_alive (struct thread_info *tp) |
c906108c | 454 | { |
4f8d22e3 | 455 | if (tp->state_ == THREAD_EXITED) |
c906108c | 456 | return 0; |
39f77062 | 457 | if (!target_thread_alive (tp->ptid)) |
4f8d22e3 | 458 | return 0; |
c906108c SS |
459 | return 1; |
460 | } | |
461 | ||
462 | static void | |
fba45db2 | 463 | prune_threads (void) |
c906108c | 464 | { |
d4f3574e | 465 | struct thread_info *tp, *next; |
c906108c | 466 | |
c906108c SS |
467 | for (tp = thread_list; tp; tp = next) |
468 | { | |
469 | next = tp->next; | |
470 | if (!thread_alive (tp)) | |
39f77062 | 471 | delete_thread (tp->ptid); |
c906108c SS |
472 | } |
473 | } | |
474 | ||
5231c1fd PA |
475 | void |
476 | thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid) | |
477 | { | |
82f73884 PA |
478 | struct inferior *inf; |
479 | struct thread_info *tp; | |
480 | ||
481 | /* It can happen that what we knew as the target inferior id | |
482 | changes. E.g, target remote may only discover the remote process | |
483 | pid after adding the inferior to GDB's list. */ | |
484 | inf = find_inferior_pid (ptid_get_pid (old_ptid)); | |
485 | inf->pid = ptid_get_pid (new_ptid); | |
486 | ||
487 | tp = find_thread_pid (old_ptid); | |
5231c1fd PA |
488 | tp->ptid = new_ptid; |
489 | ||
490 | observer_notify_thread_ptid_changed (old_ptid, new_ptid); | |
491 | } | |
492 | ||
e1ac3328 VP |
493 | void |
494 | set_running (ptid_t ptid, int running) | |
495 | { | |
496 | struct thread_info *tp; | |
497 | ||
e1ac3328 VP |
498 | /* We try not to notify the observer if no thread has actually changed |
499 | the running state -- merely to reduce the number of messages to | |
500 | frontend. Frontend is supposed to handle multiple *running just fine. */ | |
501 | if (PIDGET (ptid) == -1) | |
502 | { | |
503 | int any_started = 0; | |
504 | for (tp = thread_list; tp; tp = tp->next) | |
505 | { | |
4f8d22e3 PA |
506 | if (tp->state_ == THREAD_EXITED) |
507 | continue; | |
508 | if (running && tp->state_ == THREAD_STOPPED) | |
509 | any_started = 1; | |
510 | tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED; | |
e1ac3328 | 511 | } |
8f6a8e84 | 512 | if (any_started && !suppress_resume_observer) |
e1ac3328 VP |
513 | observer_notify_target_resumed (ptid); |
514 | } | |
515 | else | |
516 | { | |
4f8d22e3 | 517 | int started = 0; |
e1ac3328 VP |
518 | tp = find_thread_pid (ptid); |
519 | gdb_assert (tp); | |
4f8d22e3 PA |
520 | gdb_assert (tp->state_ != THREAD_EXITED); |
521 | if (running && tp->state_ == THREAD_STOPPED) | |
522 | started = 1; | |
523 | tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED; | |
524 | if (started && !suppress_resume_observer) | |
525 | observer_notify_target_resumed (ptid); | |
526 | } | |
e1ac3328 VP |
527 | } |
528 | ||
4f8d22e3 PA |
529 | static int |
530 | is_thread_state (ptid_t ptid, enum thread_state state) | |
e1ac3328 VP |
531 | { |
532 | struct thread_info *tp; | |
533 | ||
8ea051c5 PA |
534 | if (!target_has_execution) |
535 | return 0; | |
536 | ||
e1ac3328 VP |
537 | tp = find_thread_pid (ptid); |
538 | gdb_assert (tp); | |
4f8d22e3 PA |
539 | return tp->state_ == state; |
540 | } | |
541 | ||
542 | int | |
543 | is_stopped (ptid_t ptid) | |
544 | { | |
545 | /* Without execution, this property is always true. */ | |
546 | if (!target_has_execution) | |
547 | return 1; | |
548 | ||
549 | return is_thread_state (ptid, THREAD_STOPPED); | |
550 | } | |
551 | ||
552 | int | |
553 | is_exited (ptid_t ptid) | |
554 | { | |
555 | /* Without execution, this property is always false. */ | |
556 | if (!target_has_execution) | |
557 | return 0; | |
558 | ||
559 | return is_thread_state (ptid, THREAD_EXITED); | |
560 | } | |
561 | ||
562 | int | |
563 | is_running (ptid_t ptid) | |
564 | { | |
565 | /* Without execution, this property is always false. */ | |
566 | if (!target_has_execution) | |
567 | return 0; | |
568 | ||
569 | return is_thread_state (ptid, THREAD_RUNNING); | |
e1ac3328 VP |
570 | } |
571 | ||
8ea051c5 PA |
572 | int |
573 | any_running (void) | |
574 | { | |
575 | struct thread_info *tp; | |
576 | ||
577 | if (!target_has_execution) | |
578 | return 0; | |
579 | ||
8ea051c5 | 580 | for (tp = thread_list; tp; tp = tp->next) |
4f8d22e3 | 581 | if (tp->state_ == THREAD_RUNNING) |
8ea051c5 PA |
582 | return 1; |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | int | |
588 | is_executing (ptid_t ptid) | |
589 | { | |
590 | struct thread_info *tp; | |
591 | ||
592 | if (!target_has_execution) | |
593 | return 0; | |
594 | ||
8ea051c5 PA |
595 | tp = find_thread_pid (ptid); |
596 | gdb_assert (tp); | |
597 | return tp->executing_; | |
598 | } | |
599 | ||
600 | void | |
601 | set_executing (ptid_t ptid, int executing) | |
602 | { | |
603 | struct thread_info *tp; | |
604 | ||
8ea051c5 PA |
605 | if (PIDGET (ptid) == -1) |
606 | { | |
607 | for (tp = thread_list; tp; tp = tp->next) | |
608 | tp->executing_ = executing; | |
609 | } | |
610 | else | |
611 | { | |
612 | tp = find_thread_pid (ptid); | |
613 | gdb_assert (tp); | |
614 | tp->executing_ = executing; | |
615 | } | |
616 | } | |
617 | ||
252fbfc8 PA |
618 | void |
619 | set_stop_requested (ptid_t ptid, int stop) | |
620 | { | |
621 | struct thread_info *tp; | |
622 | int all = ptid_equal (ptid, minus_one_ptid); | |
623 | ||
624 | if (all || ptid_is_pid (ptid)) | |
625 | { | |
626 | for (tp = thread_list; tp; tp = tp->next) | |
627 | if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid)) | |
628 | tp->stop_requested = stop; | |
629 | } | |
630 | else | |
631 | { | |
632 | tp = find_thread_pid (ptid); | |
633 | gdb_assert (tp); | |
634 | tp->stop_requested = stop; | |
635 | } | |
636 | ||
637 | /* Call the stop requested observer so other components of GDB can | |
638 | react to this request. */ | |
639 | if (stop) | |
640 | observer_notify_thread_stop_requested (ptid); | |
641 | } | |
642 | ||
29f49a6a PA |
643 | void |
644 | finish_thread_state (ptid_t ptid) | |
645 | { | |
646 | struct thread_info *tp; | |
647 | int all; | |
648 | int any_started = 0; | |
649 | ||
650 | all = ptid_equal (ptid, minus_one_ptid); | |
651 | ||
652 | if (all || ptid_is_pid (ptid)) | |
653 | { | |
654 | for (tp = thread_list; tp; tp = tp->next) | |
655 | { | |
656 | if (tp->state_ == THREAD_EXITED) | |
657 | continue; | |
658 | if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid)) | |
659 | { | |
660 | if (tp->executing_ && tp->state_ == THREAD_STOPPED) | |
661 | any_started = 1; | |
662 | tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED; | |
663 | } | |
664 | } | |
665 | } | |
666 | else | |
667 | { | |
668 | tp = find_thread_pid (ptid); | |
669 | gdb_assert (tp); | |
670 | if (tp->state_ != THREAD_EXITED) | |
671 | { | |
672 | if (tp->executing_ && tp->state_ == THREAD_STOPPED) | |
673 | any_started = 1; | |
674 | tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED; | |
675 | } | |
676 | } | |
677 | ||
678 | if (any_started) | |
679 | observer_notify_target_resumed (ptid); | |
680 | } | |
681 | ||
682 | void | |
683 | finish_thread_state_cleanup (void *arg) | |
684 | { | |
685 | ptid_t *ptid_p = arg; | |
686 | ||
687 | gdb_assert (arg); | |
688 | ||
689 | finish_thread_state (*ptid_p); | |
690 | } | |
691 | ||
8e8901c5 VP |
692 | /* Prints the list of threads and their details on UIOUT. |
693 | This is a version of 'info_thread_command' suitable for | |
694 | use from MI. | |
4f8d22e3 | 695 | If REQUESTED_THREAD is not -1, it's the GDB id of the thread |
8e8901c5 | 696 | that should be printed. Otherwise, all threads are |
3ee1c036 VP |
697 | printed. |
698 | If PID is not -1, only print threads from the process PID. | |
699 | Otherwise, threads from all attached PIDs are printed. | |
700 | If both REQUESTED_THREAD and PID are not -1, then the thread | |
701 | is printed if it belongs to the specified process. Otherwise, | |
702 | an error is raised. */ | |
8e8901c5 | 703 | void |
3ee1c036 | 704 | print_thread_info (struct ui_out *uiout, int requested_thread, int pid) |
c906108c SS |
705 | { |
706 | struct thread_info *tp; | |
39f77062 | 707 | ptid_t current_ptid; |
99b3d574 | 708 | struct cleanup *old_chain; |
0d06e24b | 709 | char *extra_info; |
8e8901c5 | 710 | int current_thread = -1; |
c906108c | 711 | |
c906108c | 712 | prune_threads (); |
b83266a0 | 713 | target_find_new_threads (); |
39f77062 | 714 | current_ptid = inferior_ptid; |
4f8d22e3 PA |
715 | |
716 | /* We'll be switching threads temporarily. */ | |
717 | old_chain = make_cleanup_restore_current_thread (); | |
718 | ||
719 | make_cleanup_ui_out_list_begin_end (uiout, "threads"); | |
c906108c SS |
720 | for (tp = thread_list; tp; tp = tp->next) |
721 | { | |
8e8901c5 VP |
722 | struct cleanup *chain2; |
723 | ||
724 | if (requested_thread != -1 && tp->num != requested_thread) | |
725 | continue; | |
726 | ||
3ee1c036 VP |
727 | if (pid != -1 && PIDGET (tp->ptid) != pid) |
728 | { | |
729 | if (requested_thread != -1) | |
730 | error (_("Requested thread not found in requested process")); | |
731 | continue; | |
732 | } | |
733 | ||
4f8d22e3 PA |
734 | if (ptid_equal (tp->ptid, current_ptid)) |
735 | current_thread = tp->num; | |
736 | ||
737 | if (tp->state_ == THREAD_EXITED) | |
738 | continue; | |
739 | ||
8e8901c5 VP |
740 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
741 | ||
39f77062 | 742 | if (ptid_equal (tp->ptid, current_ptid)) |
4f8d22e3 | 743 | ui_out_text (uiout, "* "); |
c906108c | 744 | else |
8e8901c5 | 745 | ui_out_text (uiout, " "); |
c906108c | 746 | |
8e8901c5 VP |
747 | ui_out_field_int (uiout, "id", tp->num); |
748 | ui_out_text (uiout, " "); | |
54ba13f7 | 749 | ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid)); |
0d06e24b | 750 | |
ed406532 VP |
751 | extra_info = target_extra_thread_info (tp); |
752 | if (extra_info) | |
8e8901c5 | 753 | { |
ed406532 VP |
754 | ui_out_text (uiout, " ("); |
755 | ui_out_field_string (uiout, "details", extra_info); | |
756 | ui_out_text (uiout, ")"); | |
8e8901c5 | 757 | } |
ed406532 | 758 | ui_out_text (uiout, " "); |
4f8d22e3 PA |
759 | |
760 | if (tp->state_ == THREAD_RUNNING) | |
94cc34af PA |
761 | ui_out_text (uiout, "(running)\n"); |
762 | else | |
763 | { | |
764 | /* The switch below puts us at the top of the stack (leaf | |
765 | frame). */ | |
766 | switch_to_thread (tp->ptid); | |
767 | print_stack_frame (get_selected_frame (NULL), | |
768 | /* For MI output, print frame level. */ | |
769 | ui_out_is_mi_like_p (uiout), | |
770 | LOCATION); | |
771 | } | |
8e8901c5 | 772 | |
90139f7d VP |
773 | if (ui_out_is_mi_like_p (uiout)) |
774 | { | |
775 | char *state = "stopped"; | |
ed406532 | 776 | if (tp->state_ == THREAD_RUNNING) |
90139f7d VP |
777 | state = "running"; |
778 | ui_out_field_string (uiout, "state", state); | |
779 | } | |
780 | ||
8e8901c5 | 781 | do_cleanups (chain2); |
c906108c SS |
782 | } |
783 | ||
99b3d574 DP |
784 | /* Restores the current thread and the frame selected before |
785 | the "info threads" command. */ | |
786 | do_cleanups (old_chain); | |
c906108c | 787 | |
3ee1c036 | 788 | if (pid == -1 && requested_thread == -1 ) |
8e8901c5 | 789 | { |
4f8d22e3 PA |
790 | gdb_assert (current_thread != -1 |
791 | || !thread_list); | |
0bcd3e20 | 792 | if (current_thread != -1 && ui_out_is_mi_like_p (uiout)) |
8e8901c5 | 793 | ui_out_field_int (uiout, "current-thread-id", current_thread); |
94cc34af | 794 | |
4f8d22e3 PA |
795 | if (current_thread != -1 && is_exited (current_ptid)) |
796 | ui_out_message (uiout, 0, "\n\ | |
797 | The current thread <Thread ID %d> has terminated. See `help thread'.\n", | |
798 | current_thread); | |
c906108c | 799 | } |
c906108c SS |
800 | } |
801 | ||
8e8901c5 VP |
802 | |
803 | /* Print information about currently known threads | |
804 | ||
805 | * Note: this has the drawback that it _really_ switches | |
806 | * threads, which frees the frame cache. A no-side | |
807 | * effects info-threads command would be nicer. | |
808 | */ | |
809 | ||
810 | static void | |
811 | info_threads_command (char *arg, int from_tty) | |
812 | { | |
3ee1c036 | 813 | print_thread_info (uiout, -1, -1); |
8e8901c5 VP |
814 | } |
815 | ||
c906108c SS |
816 | /* Switch from one thread to another. */ |
817 | ||
6a6b96b9 | 818 | void |
39f77062 | 819 | switch_to_thread (ptid_t ptid) |
c906108c | 820 | { |
39f77062 | 821 | if (ptid_equal (ptid, inferior_ptid)) |
c906108c SS |
822 | return; |
823 | ||
39f77062 | 824 | inferior_ptid = ptid; |
35f196d9 | 825 | reinit_frame_cache (); |
c906108c | 826 | registers_changed (); |
94cc34af | 827 | |
4f8d22e3 PA |
828 | /* We don't check for is_stopped, because we're called at times |
829 | while in the TARGET_RUNNING state, e.g., while handling an | |
830 | internal event. */ | |
831 | if (!is_exited (ptid) && !is_executing (ptid)) | |
94cc34af PA |
832 | stop_pc = read_pc (); |
833 | else | |
834 | stop_pc = ~(CORE_ADDR) 0; | |
c906108c SS |
835 | } |
836 | ||
837 | static void | |
39f77062 | 838 | restore_current_thread (ptid_t ptid) |
c906108c | 839 | { |
dcf4fbde | 840 | switch_to_thread (ptid); |
99b3d574 DP |
841 | } |
842 | ||
843 | static void | |
4f8d22e3 | 844 | restore_selected_frame (struct frame_id a_frame_id, int frame_level) |
99b3d574 | 845 | { |
4f8d22e3 PA |
846 | struct frame_info *frame = NULL; |
847 | int count; | |
848 | ||
849 | gdb_assert (frame_level >= 0); | |
850 | ||
851 | /* Restore by level first, check if the frame id is the same as | |
852 | expected. If that fails, try restoring by frame id. If that | |
853 | fails, nothing to do, just warn the user. */ | |
854 | ||
855 | count = frame_level; | |
856 | frame = find_relative_frame (get_current_frame (), &count); | |
857 | if (count == 0 | |
858 | && frame != NULL | |
859 | /* Either the frame ids match, of they're both invalid. The | |
860 | latter case is not failsafe, but since it's highly unlikely | |
861 | the search by level finds the wrong frame, it's 99.9(9)% of | |
862 | the time (for all practical purposes) safe. */ | |
863 | && (frame_id_eq (get_frame_id (frame), a_frame_id) | |
864 | /* Note: could be better to check every frame_id | |
865 | member for equality here. */ | |
866 | || (!frame_id_p (get_frame_id (frame)) | |
867 | && !frame_id_p (a_frame_id)))) | |
868 | { | |
869 | /* Cool, all is fine. */ | |
870 | select_frame (frame); | |
871 | return; | |
872 | } | |
99b3d574 | 873 | |
4f8d22e3 PA |
874 | frame = frame_find_by_id (a_frame_id); |
875 | if (frame != NULL) | |
876 | { | |
877 | /* Cool, refound it. */ | |
878 | select_frame (frame); | |
879 | return; | |
880 | } | |
99b3d574 | 881 | |
0c501536 PA |
882 | /* Nothing else to do, the frame layout really changed. Select the |
883 | innermost stack frame. */ | |
884 | select_frame (get_current_frame ()); | |
885 | ||
886 | /* Warn the user. */ | |
4f8d22e3 | 887 | if (!ui_out_is_mi_like_p (uiout)) |
99b3d574 | 888 | { |
4f8d22e3 PA |
889 | warning (_("\ |
890 | Couldn't restore frame #%d in current thread, at reparsed frame #0\n"), | |
891 | frame_level); | |
892 | /* For MI, we should probably have a notification about | |
893 | current frame change. But this error is not very | |
894 | likely, so don't bother for now. */ | |
0c501536 | 895 | print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE); |
c906108c SS |
896 | } |
897 | } | |
898 | ||
6ecce94d AC |
899 | struct current_thread_cleanup |
900 | { | |
39f77062 | 901 | ptid_t inferior_ptid; |
99b3d574 | 902 | struct frame_id selected_frame_id; |
4f8d22e3 PA |
903 | int selected_frame_level; |
904 | int was_stopped; | |
6ecce94d AC |
905 | }; |
906 | ||
907 | static void | |
908 | do_restore_current_thread_cleanup (void *arg) | |
909 | { | |
4f8d22e3 | 910 | struct thread_info *tp; |
6ecce94d | 911 | struct current_thread_cleanup *old = arg; |
39f77062 | 912 | restore_current_thread (old->inferior_ptid); |
94cc34af | 913 | |
4f8d22e3 PA |
914 | /* The running state of the originally selected thread may have |
915 | changed, so we have to recheck it here. */ | |
916 | if (old->was_stopped | |
917 | && is_stopped (inferior_ptid) | |
918 | && target_has_registers | |
919 | && target_has_stack | |
920 | && target_has_memory) | |
921 | restore_selected_frame (old->selected_frame_id, | |
922 | old->selected_frame_level); | |
923 | } | |
924 | ||
925 | static void | |
926 | restore_current_thread_cleanup_dtor (void *arg) | |
927 | { | |
928 | struct current_thread_cleanup *old = arg; | |
929 | struct thread_info *tp; | |
930 | tp = find_thread_pid (old->inferior_ptid); | |
931 | if (tp) | |
932 | tp->refcount--; | |
b8c9b27d | 933 | xfree (old); |
6ecce94d AC |
934 | } |
935 | ||
6208b47d | 936 | struct cleanup * |
4f8d22e3 | 937 | make_cleanup_restore_current_thread (void) |
6ecce94d | 938 | { |
4f8d22e3 PA |
939 | struct thread_info *tp; |
940 | struct frame_info *frame; | |
941 | struct current_thread_cleanup *old; | |
942 | ||
943 | old = xmalloc (sizeof (struct current_thread_cleanup)); | |
39f77062 | 944 | old->inferior_ptid = inferior_ptid; |
4f8d22e3 PA |
945 | old->was_stopped = is_stopped (inferior_ptid); |
946 | if (old->was_stopped | |
947 | && target_has_registers | |
948 | && target_has_stack | |
949 | && target_has_memory) | |
950 | frame = get_selected_frame (NULL); | |
951 | else | |
952 | frame = NULL; | |
953 | ||
954 | old->selected_frame_id = get_frame_id (frame); | |
955 | old->selected_frame_level = frame_relative_level (frame); | |
956 | ||
957 | tp = find_thread_pid (inferior_ptid); | |
958 | if (tp) | |
959 | tp->refcount++; | |
960 | ||
961 | return make_cleanup_dtor (do_restore_current_thread_cleanup, old, | |
962 | restore_current_thread_cleanup_dtor); | |
6ecce94d AC |
963 | } |
964 | ||
c906108c SS |
965 | /* Apply a GDB command to a list of threads. List syntax is a whitespace |
966 | seperated list of numbers, or ranges, or the keyword `all'. Ranges consist | |
967 | of two numbers seperated by a hyphen. Examples: | |
968 | ||
c5aa993b JM |
969 | thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4 |
970 | thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9 | |
971 | thread apply all p x/i $pc Apply x/i $pc cmd to all threads | |
972 | */ | |
c906108c SS |
973 | |
974 | static void | |
fba45db2 | 975 | thread_apply_all_command (char *cmd, int from_tty) |
c906108c SS |
976 | { |
977 | struct thread_info *tp; | |
4f8d22e3 | 978 | struct cleanup *old_chain; |
e35ce267 | 979 | char *saved_cmd; |
c906108c SS |
980 | |
981 | if (cmd == NULL || *cmd == '\000') | |
8a3fe4f8 | 982 | error (_("Please specify a command following the thread ID list")); |
94cc34af | 983 | |
4f8d22e3 | 984 | prune_threads (); |
e9d196c5 MS |
985 | target_find_new_threads (); |
986 | ||
4f8d22e3 PA |
987 | old_chain = make_cleanup_restore_current_thread (); |
988 | ||
e35ce267 CF |
989 | /* Save a copy of the command in case it is clobbered by |
990 | execute_command */ | |
5b616ba1 | 991 | saved_cmd = xstrdup (cmd); |
94cc34af | 992 | make_cleanup (xfree, saved_cmd); |
c906108c SS |
993 | for (tp = thread_list; tp; tp = tp->next) |
994 | if (thread_alive (tp)) | |
995 | { | |
dcf4fbde | 996 | switch_to_thread (tp->ptid); |
94cc34af | 997 | |
a3f17187 | 998 | printf_filtered (_("\nThread %d (%s):\n"), |
54ba13f7 | 999 | tp->num, target_pid_to_str (inferior_ptid)); |
c906108c | 1000 | execute_command (cmd, from_tty); |
6949171e | 1001 | strcpy (cmd, saved_cmd); /* Restore exact command used previously */ |
c906108c | 1002 | } |
6ecce94d AC |
1003 | |
1004 | do_cleanups (old_chain); | |
c906108c SS |
1005 | } |
1006 | ||
1007 | static void | |
fba45db2 | 1008 | thread_apply_command (char *tidlist, int from_tty) |
c906108c SS |
1009 | { |
1010 | char *cmd; | |
1011 | char *p; | |
1012 | struct cleanup *old_chain; | |
e35ce267 | 1013 | char *saved_cmd; |
c906108c SS |
1014 | |
1015 | if (tidlist == NULL || *tidlist == '\000') | |
8a3fe4f8 | 1016 | error (_("Please specify a thread ID list")); |
c906108c | 1017 | |
c5aa993b | 1018 | for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++); |
c906108c SS |
1019 | |
1020 | if (*cmd == '\000') | |
8a3fe4f8 | 1021 | error (_("Please specify a command following the thread ID list")); |
c906108c | 1022 | |
e35ce267 CF |
1023 | /* Save a copy of the command in case it is clobbered by |
1024 | execute_command */ | |
5b616ba1 | 1025 | saved_cmd = xstrdup (cmd); |
4f8d22e3 | 1026 | old_chain = make_cleanup (xfree, saved_cmd); |
c906108c SS |
1027 | while (tidlist < cmd) |
1028 | { | |
1029 | struct thread_info *tp; | |
1030 | int start, end; | |
1031 | ||
1032 | start = strtol (tidlist, &p, 10); | |
1033 | if (p == tidlist) | |
8a3fe4f8 | 1034 | error (_("Error parsing %s"), tidlist); |
c906108c SS |
1035 | tidlist = p; |
1036 | ||
1037 | while (*tidlist == ' ' || *tidlist == '\t') | |
1038 | tidlist++; | |
1039 | ||
1040 | if (*tidlist == '-') /* Got a range of IDs? */ | |
1041 | { | |
c5aa993b | 1042 | tidlist++; /* Skip the - */ |
c906108c SS |
1043 | end = strtol (tidlist, &p, 10); |
1044 | if (p == tidlist) | |
8a3fe4f8 | 1045 | error (_("Error parsing %s"), tidlist); |
c906108c SS |
1046 | tidlist = p; |
1047 | ||
1048 | while (*tidlist == ' ' || *tidlist == '\t') | |
1049 | tidlist++; | |
1050 | } | |
1051 | else | |
1052 | end = start; | |
1053 | ||
65fc9b77 PA |
1054 | make_cleanup_restore_current_thread (); |
1055 | ||
c906108c SS |
1056 | for (; start <= end; start++) |
1057 | { | |
1058 | tp = find_thread_id (start); | |
1059 | ||
1060 | if (!tp) | |
8a3fe4f8 | 1061 | warning (_("Unknown thread %d."), start); |
c906108c | 1062 | else if (!thread_alive (tp)) |
8a3fe4f8 | 1063 | warning (_("Thread %d has terminated."), start); |
c906108c SS |
1064 | else |
1065 | { | |
dcf4fbde | 1066 | switch_to_thread (tp->ptid); |
4f8d22e3 | 1067 | |
a3f17187 | 1068 | printf_filtered (_("\nThread %d (%s):\n"), tp->num, |
54ba13f7 | 1069 | target_pid_to_str (inferior_ptid)); |
c906108c | 1070 | execute_command (cmd, from_tty); |
4f8d22e3 PA |
1071 | |
1072 | /* Restore exact command used previously. */ | |
1073 | strcpy (cmd, saved_cmd); | |
c906108c SS |
1074 | } |
1075 | } | |
1076 | } | |
6ecce94d AC |
1077 | |
1078 | do_cleanups (old_chain); | |
c906108c SS |
1079 | } |
1080 | ||
1081 | /* Switch to the specified thread. Will dispatch off to thread_apply_command | |
1082 | if prefix of arg is `apply'. */ | |
1083 | ||
1084 | static void | |
fba45db2 | 1085 | thread_command (char *tidstr, int from_tty) |
c906108c | 1086 | { |
c906108c SS |
1087 | if (!tidstr) |
1088 | { | |
c906108c | 1089 | if (target_has_stack) |
4f8d22e3 PA |
1090 | { |
1091 | if (is_exited (inferior_ptid)) | |
1092 | printf_filtered (_("[Current thread is %d (%s) (exited)]\n"), | |
1093 | pid_to_thread_id (inferior_ptid), | |
54ba13f7 | 1094 | target_pid_to_str (inferior_ptid)); |
4f8d22e3 PA |
1095 | else |
1096 | printf_filtered (_("[Current thread is %d (%s)]\n"), | |
1097 | pid_to_thread_id (inferior_ptid), | |
54ba13f7 | 1098 | target_pid_to_str (inferior_ptid)); |
4f8d22e3 | 1099 | } |
c906108c | 1100 | else |
8a3fe4f8 | 1101 | error (_("No stack.")); |
c906108c SS |
1102 | return; |
1103 | } | |
c5394b80 | 1104 | |
ce43223b | 1105 | gdb_thread_select (uiout, tidstr, NULL); |
c5394b80 JM |
1106 | } |
1107 | ||
93815fbf VP |
1108 | /* Print notices when new threads are attached and detached. */ |
1109 | int print_thread_events = 1; | |
1110 | static void | |
1111 | show_print_thread_events (struct ui_file *file, int from_tty, | |
1112 | struct cmd_list_element *c, const char *value) | |
1113 | { | |
1114 | fprintf_filtered (file, _("\ | |
1115 | Printing of thread events is %s.\n"), | |
1116 | value); | |
1117 | } | |
1118 | ||
c5394b80 | 1119 | static int |
6949171e | 1120 | do_captured_thread_select (struct ui_out *uiout, void *tidstr) |
c5394b80 JM |
1121 | { |
1122 | int num; | |
1123 | struct thread_info *tp; | |
1124 | ||
81490ea1 | 1125 | num = value_as_long (parse_and_eval (tidstr)); |
c906108c SS |
1126 | |
1127 | tp = find_thread_id (num); | |
1128 | ||
8b93c638 | 1129 | if (!tp) |
8a3fe4f8 | 1130 | error (_("Thread ID %d not known."), num); |
c906108c SS |
1131 | |
1132 | if (!thread_alive (tp)) | |
8a3fe4f8 | 1133 | error (_("Thread ID %d has terminated."), num); |
c906108c | 1134 | |
dcf4fbde | 1135 | switch_to_thread (tp->ptid); |
c906108c | 1136 | |
db5a7484 NR |
1137 | annotate_thread_changed (); |
1138 | ||
8b93c638 | 1139 | ui_out_text (uiout, "[Switching to thread "); |
39f77062 | 1140 | ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid)); |
8b93c638 | 1141 | ui_out_text (uiout, " ("); |
54ba13f7 | 1142 | ui_out_text (uiout, target_pid_to_str (inferior_ptid)); |
8b93c638 | 1143 | ui_out_text (uiout, ")]"); |
c5394b80 | 1144 | |
4f8d22e3 PA |
1145 | /* Note that we can't reach this with an exited thread, due to the |
1146 | thread_alive check above. */ | |
1147 | if (tp->state_ == THREAD_RUNNING) | |
94cc34af | 1148 | ui_out_text (uiout, "(running)\n"); |
4f8d22e3 PA |
1149 | else |
1150 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); | |
1151 | ||
1152 | /* Since the current thread may have changed, see if there is any | |
1153 | exited thread we can now delete. */ | |
1154 | prune_threads (); | |
94cc34af | 1155 | |
c5394b80 JM |
1156 | return GDB_RC_OK; |
1157 | } | |
1158 | ||
1159 | enum gdb_rc | |
ce43223b | 1160 | gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message) |
c5394b80 | 1161 | { |
b0b13bb4 DJ |
1162 | if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr, |
1163 | error_message, RETURN_MASK_ALL) < 0) | |
1164 | return GDB_RC_FAIL; | |
1165 | return GDB_RC_OK; | |
c906108c SS |
1166 | } |
1167 | ||
1168 | /* Commands with a prefix of `thread'. */ | |
1169 | struct cmd_list_element *thread_cmd_list = NULL; | |
1170 | ||
1171 | void | |
fba45db2 | 1172 | _initialize_thread (void) |
c906108c SS |
1173 | { |
1174 | static struct cmd_list_element *thread_apply_list = NULL; | |
94cc34af | 1175 | struct cmd_list_element *c; |
c906108c | 1176 | |
94cc34af PA |
1177 | c = add_info ("threads", info_threads_command, |
1178 | _("IDs of currently known threads.")); | |
4f8d22e3 | 1179 | set_cmd_no_selected_thread_ok (c); |
c906108c | 1180 | |
94cc34af | 1181 | c = add_prefix_cmd ("thread", class_run, thread_command, _("\ |
1bedd215 AC |
1182 | Use this command to switch between threads.\n\ |
1183 | The new thread ID must be currently known."), | |
94cc34af | 1184 | &thread_cmd_list, "thread ", 1, &cmdlist); |
4f8d22e3 | 1185 | set_cmd_no_selected_thread_ok (c); |
c906108c | 1186 | |
4f8d22e3 PA |
1187 | c = add_prefix_cmd ("apply", class_run, thread_apply_command, |
1188 | _("Apply a command to a list of threads."), | |
1189 | &thread_apply_list, "thread apply ", 1, &thread_cmd_list); | |
4f8d22e3 | 1190 | set_cmd_no_selected_thread_ok (c); |
c906108c | 1191 | |
4f8d22e3 PA |
1192 | c = add_cmd ("all", class_run, thread_apply_all_command, |
1193 | _("Apply a command to all threads."), &thread_apply_list); | |
4f8d22e3 | 1194 | set_cmd_no_selected_thread_ok (c); |
c906108c SS |
1195 | |
1196 | if (!xdb_commands) | |
1197 | add_com_alias ("t", "thread", class_run, 1); | |
93815fbf VP |
1198 | |
1199 | add_setshow_boolean_cmd ("thread-events", no_class, | |
1200 | &print_thread_events, _("\ | |
11c68c47 EZ |
1201 | Set printing of thread events (such as thread start and exit)."), _("\ |
1202 | Show printing of thread events (such as thread start and exit)."), NULL, | |
93815fbf VP |
1203 | NULL, |
1204 | show_print_thread_events, | |
1205 | &setprintlist, &showprintlist); | |
c906108c | 1206 | } |