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