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