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 | 57 | static struct thread_info *find_thread_id (int num); |
c906108c | 58 | |
a14ed312 KB |
59 | static void thread_command (char *tidstr, int from_tty); |
60 | static void thread_apply_all_command (char *, int); | |
61 | static int thread_alive (struct thread_info *); | |
62 | static void info_threads_command (char *, int); | |
63 | static void thread_apply_command (char *, int); | |
39f77062 | 64 | static void restore_current_thread (ptid_t); |
a14ed312 | 65 | static void prune_threads (void); |
c906108c | 66 | |
4f8d22e3 PA |
67 | /* Frontend view of the thread state. Possible extensions: stepping, |
68 | finishing, until(ling),... */ | |
69 | enum thread_state | |
70 | { | |
71 | THREAD_STOPPED, | |
72 | THREAD_RUNNING, | |
73 | THREAD_EXITED, | |
74 | }; | |
75 | ||
76 | static enum thread_state main_thread_state = THREAD_STOPPED; | |
8ea051c5 PA |
77 | static int main_thread_executing = 0; |
78 | ||
8601f500 MS |
79 | void |
80 | delete_step_resume_breakpoint (void *arg) | |
81 | { | |
82 | struct breakpoint **breakpointp = (struct breakpoint **) arg; | |
83 | struct thread_info *tp; | |
84 | ||
85 | if (*breakpointp != NULL) | |
86 | { | |
87 | delete_breakpoint (*breakpointp); | |
88 | for (tp = thread_list; tp; tp = tp->next) | |
89 | if (tp->step_resume_breakpoint == *breakpointp) | |
90 | tp->step_resume_breakpoint = NULL; | |
91 | ||
92 | *breakpointp = NULL; | |
93 | } | |
94 | } | |
95 | ||
7c952b6d | 96 | static void |
4f8d22e3 | 97 | clear_thread_inferior_resources (struct thread_info *tp) |
7c952b6d ND |
98 | { |
99 | /* NOTE: this will take care of any left-over step_resume breakpoints, | |
4d8453a5 DJ |
100 | but not any user-specified thread-specific breakpoints. We can not |
101 | delete the breakpoint straight-off, because the inferior might not | |
102 | be stopped at the moment. */ | |
7c952b6d | 103 | if (tp->step_resume_breakpoint) |
4f8d22e3 PA |
104 | { |
105 | tp->step_resume_breakpoint->disposition = disp_del_at_next_stop; | |
106 | tp->step_resume_breakpoint = NULL; | |
107 | } | |
7c952b6d | 108 | |
a474d7c2 | 109 | bpstat_clear (&tp->stop_bpstat); |
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; |
4f8d22e3 | 131 | main_thread_state = THREAD_STOPPED; |
8ea051c5 PA |
132 | main_thread_executing = 0; |
133 | ||
c906108c SS |
134 | if (!thread_list) |
135 | return; | |
136 | ||
137 | for (tp = thread_list; tp; tp = tpnext) | |
138 | { | |
139 | tpnext = tp->next; | |
7c952b6d | 140 | free_thread (tp); |
c906108c SS |
141 | } |
142 | ||
143 | thread_list = NULL; | |
c906108c SS |
144 | } |
145 | ||
0d06e24b | 146 | struct thread_info * |
93815fbf | 147 | add_thread_silent (ptid_t ptid) |
c906108c SS |
148 | { |
149 | struct thread_info *tp; | |
150 | ||
4f8d22e3 PA |
151 | tp = find_thread_pid (ptid); |
152 | if (tp) | |
153 | /* Found an old thread with the same id. It has to be dead, | |
154 | otherwise we wouldn't be adding a new thread with the same id. | |
155 | The OS is reusing this id --- delete it, and recreate a new | |
156 | one. */ | |
157 | { | |
158 | /* In addition to deleting the thread, if this is the current | |
159 | thread, then we need to also get rid of the current infrun | |
160 | context, and take care that delete_thread doesn't really | |
161 | delete the thread if it is inferior_ptid. Create a new | |
162 | template thread in the list with an invalid ptid, context | |
163 | switch to it, delete the original thread, reset the new | |
164 | thread's ptid, and switch to it. */ | |
165 | ||
166 | if (ptid_equal (inferior_ptid, ptid)) | |
167 | { | |
168 | tp = xmalloc (sizeof (*tp)); | |
169 | memset (tp, 0, sizeof (*tp)); | |
170 | tp->ptid = minus_one_ptid; | |
171 | tp->num = ++highest_thread_num; | |
172 | tp->next = thread_list; | |
173 | thread_list = tp; | |
174 | context_switch_to (minus_one_ptid); | |
175 | ||
176 | /* Now we can delete it. */ | |
177 | delete_thread (ptid); | |
178 | ||
179 | /* Since the context is already set to this new thread, | |
180 | reset its ptid, and reswitch inferior_ptid to it. */ | |
181 | tp->ptid = ptid; | |
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 | ||
c906108c | 292 | static 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; |
8b93c638 | 413 | |
7990a578 EZ |
414 | prune_threads (); |
415 | target_find_new_threads (); | |
416 | ||
3b31d625 | 417 | cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids"); |
8b93c638 JM |
418 | |
419 | for (tp = thread_list; tp; tp = tp->next) | |
420 | { | |
4f8d22e3 PA |
421 | if (tp->state_ == THREAD_EXITED) |
422 | continue; | |
8b93c638 JM |
423 | num++; |
424 | ui_out_field_int (uiout, "thread-id", tp->num); | |
425 | } | |
426 | ||
3b31d625 | 427 | do_cleanups (cleanup_chain); |
8b93c638 JM |
428 | ui_out_field_int (uiout, "number-of-threads", num); |
429 | return GDB_RC_OK; | |
430 | } | |
431 | ||
432 | /* Official gdblib interface function to get a list of thread ids and | |
433 | the total number. */ | |
434 | enum gdb_rc | |
ce43223b | 435 | gdb_list_thread_ids (struct ui_out *uiout, char **error_message) |
8b93c638 | 436 | { |
b0b13bb4 DJ |
437 | if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL, |
438 | error_message, RETURN_MASK_ALL) < 0) | |
439 | return GDB_RC_FAIL; | |
440 | return GDB_RC_OK; | |
8b93c638 | 441 | } |
c906108c SS |
442 | |
443 | /* Load infrun state for the thread PID. */ | |
444 | ||
c5aa993b | 445 | void |
6949171e JJ |
446 | load_infrun_state (ptid_t ptid, |
447 | CORE_ADDR *prev_pc, | |
6c0d3f6a | 448 | int *trap_expected, |
fba45db2 | 449 | struct breakpoint **step_resume_breakpoint, |
6949171e | 450 | CORE_ADDR *step_range_start, |
6c0d3f6a | 451 | CORE_ADDR *step_range_end, |
6949171e | 452 | struct frame_id *step_frame_id, |
ca67fcb8 | 453 | int *stepping_over_breakpoint, |
6c0d3f6a | 454 | int *stepping_through_solib_after_catch, |
fba45db2 | 455 | bpstat *stepping_through_solib_catchpoints, |
6949171e | 456 | int *current_line, |
a474d7c2 PA |
457 | struct symtab **current_symtab, |
458 | struct continuation **continuations, | |
459 | struct continuation **intermediate_continuations, | |
460 | int *proceed_to_finish, | |
461 | enum step_over_calls_kind *step_over_calls, | |
462 | int *stop_step, | |
463 | int *step_multi, | |
464 | enum target_signal *stop_signal, | |
465 | bpstat *stop_bpstat) | |
c906108c SS |
466 | { |
467 | struct thread_info *tp; | |
468 | ||
469 | /* If we can't find the thread, then we're debugging a single threaded | |
470 | process. No need to do anything in that case. */ | |
39f77062 | 471 | tp = find_thread_id (pid_to_thread_id (ptid)); |
c906108c SS |
472 | if (tp == NULL) |
473 | return; | |
474 | ||
475 | *prev_pc = tp->prev_pc; | |
6c0d3f6a | 476 | *trap_expected = tp->trap_expected; |
c906108c SS |
477 | *step_resume_breakpoint = tp->step_resume_breakpoint; |
478 | *step_range_start = tp->step_range_start; | |
479 | *step_range_end = tp->step_range_end; | |
aa0cd9c1 | 480 | *step_frame_id = tp->step_frame_id; |
ca67fcb8 | 481 | *stepping_over_breakpoint = tp->stepping_over_breakpoint; |
6949171e JJ |
482 | *stepping_through_solib_after_catch = |
483 | tp->stepping_through_solib_after_catch; | |
484 | *stepping_through_solib_catchpoints = | |
485 | tp->stepping_through_solib_catchpoints; | |
6c0d3f6a MS |
486 | *current_line = tp->current_line; |
487 | *current_symtab = tp->current_symtab; | |
a474d7c2 PA |
488 | |
489 | /* In all-stop mode, these are global state, while in non-stop mode, | |
490 | they are per thread. */ | |
491 | if (non_stop) | |
492 | { | |
493 | *continuations = tp->continuations; | |
494 | tp->continuations = NULL; | |
495 | *intermediate_continuations = tp->intermediate_continuations; | |
496 | tp->intermediate_continuations = NULL; | |
497 | *proceed_to_finish = tp->proceed_to_finish; | |
498 | *step_over_calls = tp->step_over_calls; | |
499 | *stop_step = tp->stop_step; | |
500 | *step_multi = tp->step_multi; | |
501 | *stop_signal = tp->stop_signal; | |
502 | ||
503 | /* Swap instead of copy, so we only have to update one of | |
504 | them. */ | |
505 | *stop_bpstat = tp->stop_bpstat; | |
506 | tp->stop_bpstat = 0; | |
507 | } | |
c906108c SS |
508 | } |
509 | ||
510 | /* Save infrun state for the thread PID. */ | |
511 | ||
c5aa993b | 512 | void |
6949171e JJ |
513 | save_infrun_state (ptid_t ptid, |
514 | CORE_ADDR prev_pc, | |
6c0d3f6a | 515 | int trap_expected, |
fba45db2 | 516 | struct breakpoint *step_resume_breakpoint, |
6949171e | 517 | CORE_ADDR step_range_start, |
6c0d3f6a | 518 | CORE_ADDR step_range_end, |
6949171e | 519 | const struct frame_id *step_frame_id, |
ca67fcb8 | 520 | int stepping_over_breakpoint, |
6c0d3f6a | 521 | int stepping_through_solib_after_catch, |
fba45db2 | 522 | bpstat stepping_through_solib_catchpoints, |
6c0d3f6a | 523 | int current_line, |
a474d7c2 PA |
524 | struct symtab *current_symtab, |
525 | struct continuation *continuations, | |
526 | struct continuation *intermediate_continuations, | |
527 | int proceed_to_finish, | |
528 | enum step_over_calls_kind step_over_calls, | |
529 | int stop_step, | |
530 | int step_multi, | |
531 | enum target_signal stop_signal, | |
532 | bpstat stop_bpstat) | |
c906108c SS |
533 | { |
534 | struct thread_info *tp; | |
535 | ||
536 | /* If we can't find the thread, then we're debugging a single-threaded | |
537 | process. Nothing to do in that case. */ | |
39f77062 | 538 | tp = find_thread_id (pid_to_thread_id (ptid)); |
c906108c SS |
539 | if (tp == NULL) |
540 | return; | |
541 | ||
542 | tp->prev_pc = prev_pc; | |
6c0d3f6a | 543 | tp->trap_expected = trap_expected; |
c906108c SS |
544 | tp->step_resume_breakpoint = step_resume_breakpoint; |
545 | tp->step_range_start = step_range_start; | |
546 | tp->step_range_end = step_range_end; | |
aa0cd9c1 | 547 | tp->step_frame_id = (*step_frame_id); |
ca67fcb8 | 548 | tp->stepping_over_breakpoint = stepping_over_breakpoint; |
c906108c SS |
549 | tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch; |
550 | tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints; | |
6c0d3f6a MS |
551 | tp->current_line = current_line; |
552 | tp->current_symtab = current_symtab; | |
a474d7c2 PA |
553 | |
554 | /* In all-stop mode, these are global state, while in non-stop mode, | |
555 | they are per thread. */ | |
556 | if (non_stop) | |
557 | { | |
558 | tp->continuations = continuations; | |
559 | tp->intermediate_continuations = intermediate_continuations; | |
560 | tp->proceed_to_finish = proceed_to_finish; | |
561 | tp->step_over_calls = step_over_calls; | |
562 | tp->stop_step = stop_step; | |
563 | tp->step_multi = step_multi; | |
564 | tp->stop_signal = stop_signal; | |
565 | tp->stop_bpstat = stop_bpstat; | |
566 | } | |
c906108c SS |
567 | } |
568 | ||
569 | /* Return true if TP is an active thread. */ | |
570 | static int | |
fba45db2 | 571 | thread_alive (struct thread_info *tp) |
c906108c | 572 | { |
4f8d22e3 | 573 | if (tp->state_ == THREAD_EXITED) |
c906108c | 574 | return 0; |
39f77062 | 575 | if (!target_thread_alive (tp->ptid)) |
4f8d22e3 | 576 | return 0; |
c906108c SS |
577 | return 1; |
578 | } | |
579 | ||
580 | static void | |
fba45db2 | 581 | prune_threads (void) |
c906108c | 582 | { |
d4f3574e | 583 | struct thread_info *tp, *next; |
c906108c | 584 | |
c906108c SS |
585 | for (tp = thread_list; tp; tp = next) |
586 | { | |
587 | next = tp->next; | |
588 | if (!thread_alive (tp)) | |
39f77062 | 589 | delete_thread (tp->ptid); |
c906108c SS |
590 | } |
591 | } | |
592 | ||
e1ac3328 VP |
593 | void |
594 | set_running (ptid_t ptid, int running) | |
595 | { | |
596 | struct thread_info *tp; | |
597 | ||
598 | if (!thread_list) | |
599 | { | |
600 | /* This is one of the targets that does not add main | |
601 | thread to the thread list. Just use a single | |
602 | global flag to indicate that a thread is running. | |
603 | ||
604 | This problem is unique to ST programs. For MT programs, | |
605 | the main thread is always present in the thread list. If it's | |
606 | not, the first call to context_switch will mess up GDB internal | |
607 | state. */ | |
4f8d22e3 PA |
608 | if (running |
609 | && main_thread_state != THREAD_RUNNING | |
610 | && !suppress_resume_observer) | |
e1ac3328 | 611 | observer_notify_target_resumed (ptid); |
4f8d22e3 | 612 | main_thread_state = running ? THREAD_RUNNING : THREAD_STOPPED; |
e1ac3328 VP |
613 | return; |
614 | } | |
615 | ||
616 | /* We try not to notify the observer if no thread has actually changed | |
617 | the running state -- merely to reduce the number of messages to | |
618 | frontend. Frontend is supposed to handle multiple *running just fine. */ | |
619 | if (PIDGET (ptid) == -1) | |
620 | { | |
621 | int any_started = 0; | |
622 | for (tp = thread_list; tp; tp = tp->next) | |
623 | { | |
4f8d22e3 PA |
624 | if (tp->state_ == THREAD_EXITED) |
625 | continue; | |
626 | if (running && tp->state_ == THREAD_STOPPED) | |
627 | any_started = 1; | |
628 | tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED; | |
e1ac3328 | 629 | } |
8f6a8e84 | 630 | if (any_started && !suppress_resume_observer) |
e1ac3328 VP |
631 | observer_notify_target_resumed (ptid); |
632 | } | |
633 | else | |
634 | { | |
4f8d22e3 | 635 | int started = 0; |
e1ac3328 VP |
636 | tp = find_thread_pid (ptid); |
637 | gdb_assert (tp); | |
4f8d22e3 PA |
638 | gdb_assert (tp->state_ != THREAD_EXITED); |
639 | if (running && tp->state_ == THREAD_STOPPED) | |
640 | started = 1; | |
641 | tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED; | |
642 | if (started && !suppress_resume_observer) | |
643 | observer_notify_target_resumed (ptid); | |
644 | } | |
e1ac3328 VP |
645 | } |
646 | ||
4f8d22e3 PA |
647 | static int |
648 | is_thread_state (ptid_t ptid, enum thread_state state) | |
e1ac3328 VP |
649 | { |
650 | struct thread_info *tp; | |
651 | ||
8ea051c5 PA |
652 | if (!target_has_execution) |
653 | return 0; | |
654 | ||
e1ac3328 | 655 | if (!thread_list) |
4f8d22e3 | 656 | return main_thread_state == state; |
e1ac3328 VP |
657 | |
658 | tp = find_thread_pid (ptid); | |
659 | gdb_assert (tp); | |
4f8d22e3 PA |
660 | return tp->state_ == state; |
661 | } | |
662 | ||
663 | int | |
664 | is_stopped (ptid_t ptid) | |
665 | { | |
666 | /* Without execution, this property is always true. */ | |
667 | if (!target_has_execution) | |
668 | return 1; | |
669 | ||
670 | return is_thread_state (ptid, THREAD_STOPPED); | |
671 | } | |
672 | ||
673 | int | |
674 | is_exited (ptid_t ptid) | |
675 | { | |
676 | /* Without execution, this property is always false. */ | |
677 | if (!target_has_execution) | |
678 | return 0; | |
679 | ||
680 | return is_thread_state (ptid, THREAD_EXITED); | |
681 | } | |
682 | ||
683 | int | |
684 | is_running (ptid_t ptid) | |
685 | { | |
686 | /* Without execution, this property is always false. */ | |
687 | if (!target_has_execution) | |
688 | return 0; | |
689 | ||
690 | return is_thread_state (ptid, THREAD_RUNNING); | |
e1ac3328 VP |
691 | } |
692 | ||
8ea051c5 PA |
693 | int |
694 | any_running (void) | |
695 | { | |
696 | struct thread_info *tp; | |
697 | ||
698 | if (!target_has_execution) | |
699 | return 0; | |
700 | ||
701 | if (!thread_list) | |
4f8d22e3 | 702 | return main_thread_state == THREAD_RUNNING; |
8ea051c5 PA |
703 | |
704 | for (tp = thread_list; tp; tp = tp->next) | |
4f8d22e3 | 705 | if (tp->state_ == THREAD_RUNNING) |
8ea051c5 PA |
706 | return 1; |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
711 | int | |
712 | is_executing (ptid_t ptid) | |
713 | { | |
714 | struct thread_info *tp; | |
715 | ||
716 | if (!target_has_execution) | |
717 | return 0; | |
718 | ||
719 | if (!thread_list) | |
720 | return main_thread_executing; | |
721 | ||
722 | tp = find_thread_pid (ptid); | |
723 | gdb_assert (tp); | |
724 | return tp->executing_; | |
725 | } | |
726 | ||
727 | void | |
728 | set_executing (ptid_t ptid, int executing) | |
729 | { | |
730 | struct thread_info *tp; | |
731 | ||
732 | if (!thread_list) | |
733 | { | |
734 | /* This target does not add the main thread to the thread list. | |
735 | Use a global flag to indicate that the thread is | |
736 | executing. */ | |
737 | main_thread_executing = executing; | |
738 | return; | |
739 | } | |
740 | ||
741 | if (PIDGET (ptid) == -1) | |
742 | { | |
743 | for (tp = thread_list; tp; tp = tp->next) | |
744 | tp->executing_ = executing; | |
745 | } | |
746 | else | |
747 | { | |
748 | tp = find_thread_pid (ptid); | |
749 | gdb_assert (tp); | |
750 | tp->executing_ = executing; | |
751 | } | |
752 | } | |
753 | ||
8e8901c5 VP |
754 | /* Prints the list of threads and their details on UIOUT. |
755 | This is a version of 'info_thread_command' suitable for | |
756 | use from MI. | |
4f8d22e3 | 757 | If REQUESTED_THREAD is not -1, it's the GDB id of the thread |
8e8901c5 VP |
758 | that should be printed. Otherwise, all threads are |
759 | printed. */ | |
760 | void | |
761 | print_thread_info (struct ui_out *uiout, int requested_thread) | |
c906108c SS |
762 | { |
763 | struct thread_info *tp; | |
39f77062 | 764 | ptid_t current_ptid; |
99b3d574 | 765 | struct cleanup *old_chain; |
0d06e24b | 766 | char *extra_info; |
8e8901c5 | 767 | int current_thread = -1; |
c906108c | 768 | |
c906108c | 769 | prune_threads (); |
b83266a0 | 770 | target_find_new_threads (); |
39f77062 | 771 | current_ptid = inferior_ptid; |
4f8d22e3 PA |
772 | |
773 | /* We'll be switching threads temporarily. */ | |
774 | old_chain = make_cleanup_restore_current_thread (); | |
775 | ||
776 | make_cleanup_ui_out_list_begin_end (uiout, "threads"); | |
c906108c SS |
777 | for (tp = thread_list; tp; tp = tp->next) |
778 | { | |
8e8901c5 VP |
779 | struct cleanup *chain2; |
780 | ||
781 | if (requested_thread != -1 && tp->num != requested_thread) | |
782 | continue; | |
783 | ||
4f8d22e3 PA |
784 | if (ptid_equal (tp->ptid, current_ptid)) |
785 | current_thread = tp->num; | |
786 | ||
787 | if (tp->state_ == THREAD_EXITED) | |
788 | continue; | |
789 | ||
8e8901c5 VP |
790 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
791 | ||
39f77062 | 792 | if (ptid_equal (tp->ptid, current_ptid)) |
4f8d22e3 | 793 | ui_out_text (uiout, "* "); |
c906108c | 794 | else |
8e8901c5 | 795 | ui_out_text (uiout, " "); |
c906108c | 796 | |
8e8901c5 VP |
797 | ui_out_field_int (uiout, "id", tp->num); |
798 | ui_out_text (uiout, " "); | |
799 | ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid)); | |
0d06e24b | 800 | |
4f8d22e3 | 801 | if (tp->state_ != THREAD_EXITED) |
8e8901c5 | 802 | { |
4f8d22e3 PA |
803 | extra_info = target_extra_thread_info (tp); |
804 | if (extra_info) | |
805 | { | |
806 | ui_out_text (uiout, " ("); | |
807 | ui_out_field_string (uiout, "details", extra_info); | |
808 | ui_out_text (uiout, ")"); | |
809 | } | |
810 | ui_out_text (uiout, " "); | |
8e8901c5 | 811 | } |
4f8d22e3 PA |
812 | |
813 | if (tp->state_ == THREAD_RUNNING) | |
94cc34af PA |
814 | ui_out_text (uiout, "(running)\n"); |
815 | else | |
816 | { | |
817 | /* The switch below puts us at the top of the stack (leaf | |
818 | frame). */ | |
819 | switch_to_thread (tp->ptid); | |
820 | print_stack_frame (get_selected_frame (NULL), | |
821 | /* For MI output, print frame level. */ | |
822 | ui_out_is_mi_like_p (uiout), | |
823 | LOCATION); | |
824 | } | |
8e8901c5 | 825 | |
90139f7d VP |
826 | if (ui_out_is_mi_like_p (uiout)) |
827 | { | |
828 | char *state = "stopped"; | |
829 | if (tp->state_ == THREAD_EXITED) | |
830 | state = "exited"; | |
831 | else if (tp->state_ == THREAD_RUNNING) | |
832 | state = "running"; | |
833 | ui_out_field_string (uiout, "state", state); | |
834 | } | |
835 | ||
8e8901c5 | 836 | do_cleanups (chain2); |
c906108c SS |
837 | } |
838 | ||
99b3d574 DP |
839 | /* Restores the current thread and the frame selected before |
840 | the "info threads" command. */ | |
841 | do_cleanups (old_chain); | |
c906108c | 842 | |
8e8901c5 VP |
843 | if (requested_thread == -1) |
844 | { | |
4f8d22e3 PA |
845 | gdb_assert (current_thread != -1 |
846 | || !thread_list); | |
0bcd3e20 | 847 | if (current_thread != -1 && ui_out_is_mi_like_p (uiout)) |
8e8901c5 | 848 | ui_out_field_int (uiout, "current-thread-id", current_thread); |
94cc34af | 849 | |
4f8d22e3 PA |
850 | if (current_thread != -1 && is_exited (current_ptid)) |
851 | ui_out_message (uiout, 0, "\n\ | |
852 | The current thread <Thread ID %d> has terminated. See `help thread'.\n", | |
853 | current_thread); | |
c906108c | 854 | } |
c906108c SS |
855 | } |
856 | ||
8e8901c5 VP |
857 | |
858 | /* Print information about currently known threads | |
859 | ||
860 | * Note: this has the drawback that it _really_ switches | |
861 | * threads, which frees the frame cache. A no-side | |
862 | * effects info-threads command would be nicer. | |
863 | */ | |
864 | ||
865 | static void | |
866 | info_threads_command (char *arg, int from_tty) | |
867 | { | |
868 | print_thread_info (uiout, -1); | |
869 | } | |
870 | ||
c906108c SS |
871 | /* Switch from one thread to another. */ |
872 | ||
6a6b96b9 | 873 | void |
39f77062 | 874 | switch_to_thread (ptid_t ptid) |
c906108c | 875 | { |
39f77062 | 876 | if (ptid_equal (ptid, inferior_ptid)) |
c906108c SS |
877 | return; |
878 | ||
39f77062 | 879 | inferior_ptid = ptid; |
35f196d9 | 880 | reinit_frame_cache (); |
c906108c | 881 | registers_changed (); |
94cc34af | 882 | |
4f8d22e3 PA |
883 | /* We don't check for is_stopped, because we're called at times |
884 | while in the TARGET_RUNNING state, e.g., while handling an | |
885 | internal event. */ | |
886 | if (!is_exited (ptid) && !is_executing (ptid)) | |
94cc34af PA |
887 | stop_pc = read_pc (); |
888 | else | |
889 | stop_pc = ~(CORE_ADDR) 0; | |
c906108c SS |
890 | } |
891 | ||
892 | static void | |
39f77062 | 893 | restore_current_thread (ptid_t ptid) |
c906108c | 894 | { |
6949171e | 895 | if (!ptid_equal (ptid, inferior_ptid)) |
c906108c | 896 | { |
4f8d22e3 PA |
897 | if (non_stop) |
898 | context_switch_to (ptid); | |
899 | else | |
900 | switch_to_thread (ptid); | |
99b3d574 DP |
901 | } |
902 | } | |
903 | ||
904 | static void | |
4f8d22e3 | 905 | restore_selected_frame (struct frame_id a_frame_id, int frame_level) |
99b3d574 | 906 | { |
4f8d22e3 PA |
907 | struct frame_info *frame = NULL; |
908 | int count; | |
909 | ||
910 | gdb_assert (frame_level >= 0); | |
911 | ||
912 | /* Restore by level first, check if the frame id is the same as | |
913 | expected. If that fails, try restoring by frame id. If that | |
914 | fails, nothing to do, just warn the user. */ | |
915 | ||
916 | count = frame_level; | |
917 | frame = find_relative_frame (get_current_frame (), &count); | |
918 | if (count == 0 | |
919 | && frame != NULL | |
920 | /* Either the frame ids match, of they're both invalid. The | |
921 | latter case is not failsafe, but since it's highly unlikely | |
922 | the search by level finds the wrong frame, it's 99.9(9)% of | |
923 | the time (for all practical purposes) safe. */ | |
924 | && (frame_id_eq (get_frame_id (frame), a_frame_id) | |
925 | /* Note: could be better to check every frame_id | |
926 | member for equality here. */ | |
927 | || (!frame_id_p (get_frame_id (frame)) | |
928 | && !frame_id_p (a_frame_id)))) | |
929 | { | |
930 | /* Cool, all is fine. */ | |
931 | select_frame (frame); | |
932 | return; | |
933 | } | |
99b3d574 | 934 | |
4f8d22e3 PA |
935 | frame = frame_find_by_id (a_frame_id); |
936 | if (frame != NULL) | |
937 | { | |
938 | /* Cool, refound it. */ | |
939 | select_frame (frame); | |
940 | return; | |
941 | } | |
99b3d574 | 942 | |
4f8d22e3 PA |
943 | /* Nothing else to do, the frame layout really changed. |
944 | Tell the user. */ | |
945 | if (!ui_out_is_mi_like_p (uiout)) | |
99b3d574 | 946 | { |
4f8d22e3 PA |
947 | warning (_("\ |
948 | Couldn't restore frame #%d in current thread, at reparsed frame #0\n"), | |
949 | frame_level); | |
950 | /* For MI, we should probably have a notification about | |
951 | current frame change. But this error is not very | |
952 | likely, so don't bother for now. */ | |
953 | print_stack_frame (frame, 1, SRC_LINE); | |
c906108c SS |
954 | } |
955 | } | |
956 | ||
6ecce94d AC |
957 | struct current_thread_cleanup |
958 | { | |
39f77062 | 959 | ptid_t inferior_ptid; |
99b3d574 | 960 | struct frame_id selected_frame_id; |
4f8d22e3 PA |
961 | int selected_frame_level; |
962 | int was_stopped; | |
6ecce94d AC |
963 | }; |
964 | ||
965 | static void | |
966 | do_restore_current_thread_cleanup (void *arg) | |
967 | { | |
4f8d22e3 | 968 | struct thread_info *tp; |
6ecce94d | 969 | struct current_thread_cleanup *old = arg; |
39f77062 | 970 | restore_current_thread (old->inferior_ptid); |
94cc34af | 971 | |
4f8d22e3 PA |
972 | /* The running state of the originally selected thread may have |
973 | changed, so we have to recheck it here. */ | |
974 | if (old->was_stopped | |
975 | && is_stopped (inferior_ptid) | |
976 | && target_has_registers | |
977 | && target_has_stack | |
978 | && target_has_memory) | |
979 | restore_selected_frame (old->selected_frame_id, | |
980 | old->selected_frame_level); | |
981 | } | |
982 | ||
983 | static void | |
984 | restore_current_thread_cleanup_dtor (void *arg) | |
985 | { | |
986 | struct current_thread_cleanup *old = arg; | |
987 | struct thread_info *tp; | |
988 | tp = find_thread_pid (old->inferior_ptid); | |
989 | if (tp) | |
990 | tp->refcount--; | |
b8c9b27d | 991 | xfree (old); |
6ecce94d AC |
992 | } |
993 | ||
6208b47d | 994 | struct cleanup * |
4f8d22e3 | 995 | make_cleanup_restore_current_thread (void) |
6ecce94d | 996 | { |
4f8d22e3 PA |
997 | struct thread_info *tp; |
998 | struct frame_info *frame; | |
999 | struct current_thread_cleanup *old; | |
1000 | ||
1001 | old = xmalloc (sizeof (struct current_thread_cleanup)); | |
39f77062 | 1002 | old->inferior_ptid = inferior_ptid; |
4f8d22e3 PA |
1003 | old->was_stopped = is_stopped (inferior_ptid); |
1004 | if (old->was_stopped | |
1005 | && target_has_registers | |
1006 | && target_has_stack | |
1007 | && target_has_memory) | |
1008 | frame = get_selected_frame (NULL); | |
1009 | else | |
1010 | frame = NULL; | |
1011 | ||
1012 | old->selected_frame_id = get_frame_id (frame); | |
1013 | old->selected_frame_level = frame_relative_level (frame); | |
1014 | ||
1015 | tp = find_thread_pid (inferior_ptid); | |
1016 | if (tp) | |
1017 | tp->refcount++; | |
1018 | ||
1019 | return make_cleanup_dtor (do_restore_current_thread_cleanup, old, | |
1020 | restore_current_thread_cleanup_dtor); | |
6ecce94d AC |
1021 | } |
1022 | ||
c906108c SS |
1023 | /* Apply a GDB command to a list of threads. List syntax is a whitespace |
1024 | seperated list of numbers, or ranges, or the keyword `all'. Ranges consist | |
1025 | of two numbers seperated by a hyphen. Examples: | |
1026 | ||
c5aa993b JM |
1027 | thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4 |
1028 | thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9 | |
1029 | thread apply all p x/i $pc Apply x/i $pc cmd to all threads | |
1030 | */ | |
c906108c SS |
1031 | |
1032 | static void | |
fba45db2 | 1033 | thread_apply_all_command (char *cmd, int from_tty) |
c906108c SS |
1034 | { |
1035 | struct thread_info *tp; | |
4f8d22e3 | 1036 | struct cleanup *old_chain; |
e35ce267 | 1037 | char *saved_cmd; |
c906108c SS |
1038 | |
1039 | if (cmd == NULL || *cmd == '\000') | |
8a3fe4f8 | 1040 | error (_("Please specify a command following the thread ID list")); |
94cc34af | 1041 | |
4f8d22e3 | 1042 | prune_threads (); |
e9d196c5 MS |
1043 | target_find_new_threads (); |
1044 | ||
4f8d22e3 PA |
1045 | old_chain = make_cleanup_restore_current_thread (); |
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); |
94cc34af | 1050 | make_cleanup (xfree, saved_cmd); |
c906108c SS |
1051 | for (tp = thread_list; tp; tp = tp->next) |
1052 | if (thread_alive (tp)) | |
1053 | { | |
94cc34af PA |
1054 | if (non_stop) |
1055 | context_switch_to (tp->ptid); | |
1056 | else | |
1057 | switch_to_thread (tp->ptid); | |
1058 | ||
a3f17187 | 1059 | printf_filtered (_("\nThread %d (%s):\n"), |
6949171e | 1060 | tp->num, target_tid_to_str (inferior_ptid)); |
c906108c | 1061 | execute_command (cmd, from_tty); |
6949171e | 1062 | strcpy (cmd, saved_cmd); /* Restore exact command used previously */ |
c906108c | 1063 | } |
6ecce94d AC |
1064 | |
1065 | do_cleanups (old_chain); | |
c906108c SS |
1066 | } |
1067 | ||
1068 | static void | |
fba45db2 | 1069 | thread_apply_command (char *tidlist, int from_tty) |
c906108c SS |
1070 | { |
1071 | char *cmd; | |
1072 | char *p; | |
1073 | struct cleanup *old_chain; | |
e35ce267 | 1074 | char *saved_cmd; |
c906108c SS |
1075 | |
1076 | if (tidlist == NULL || *tidlist == '\000') | |
8a3fe4f8 | 1077 | error (_("Please specify a thread ID list")); |
c906108c | 1078 | |
c5aa993b | 1079 | for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++); |
c906108c SS |
1080 | |
1081 | if (*cmd == '\000') | |
8a3fe4f8 | 1082 | error (_("Please specify a command following the thread ID list")); |
c906108c | 1083 | |
e35ce267 CF |
1084 | /* Save a copy of the command in case it is clobbered by |
1085 | execute_command */ | |
5b616ba1 | 1086 | saved_cmd = xstrdup (cmd); |
4f8d22e3 | 1087 | old_chain = make_cleanup (xfree, saved_cmd); |
c906108c SS |
1088 | while (tidlist < cmd) |
1089 | { | |
1090 | struct thread_info *tp; | |
1091 | int start, end; | |
1092 | ||
1093 | start = strtol (tidlist, &p, 10); | |
1094 | if (p == tidlist) | |
8a3fe4f8 | 1095 | error (_("Error parsing %s"), tidlist); |
c906108c SS |
1096 | tidlist = p; |
1097 | ||
1098 | while (*tidlist == ' ' || *tidlist == '\t') | |
1099 | tidlist++; | |
1100 | ||
1101 | if (*tidlist == '-') /* Got a range of IDs? */ | |
1102 | { | |
c5aa993b | 1103 | tidlist++; /* Skip the - */ |
c906108c SS |
1104 | end = strtol (tidlist, &p, 10); |
1105 | if (p == tidlist) | |
8a3fe4f8 | 1106 | error (_("Error parsing %s"), tidlist); |
c906108c SS |
1107 | tidlist = p; |
1108 | ||
1109 | while (*tidlist == ' ' || *tidlist == '\t') | |
1110 | tidlist++; | |
1111 | } | |
1112 | else | |
1113 | end = start; | |
1114 | ||
65fc9b77 PA |
1115 | make_cleanup_restore_current_thread (); |
1116 | ||
c906108c SS |
1117 | for (; start <= end; start++) |
1118 | { | |
1119 | tp = find_thread_id (start); | |
1120 | ||
1121 | if (!tp) | |
8a3fe4f8 | 1122 | warning (_("Unknown thread %d."), start); |
c906108c | 1123 | else if (!thread_alive (tp)) |
8a3fe4f8 | 1124 | warning (_("Thread %d has terminated."), start); |
c906108c SS |
1125 | else |
1126 | { | |
94cc34af PA |
1127 | if (non_stop) |
1128 | context_switch_to (tp->ptid); | |
1129 | else | |
1130 | switch_to_thread (tp->ptid); | |
4f8d22e3 | 1131 | |
a3f17187 | 1132 | printf_filtered (_("\nThread %d (%s):\n"), tp->num, |
39f77062 | 1133 | target_tid_to_str (inferior_ptid)); |
c906108c | 1134 | execute_command (cmd, from_tty); |
4f8d22e3 PA |
1135 | |
1136 | /* Restore exact command used previously. */ | |
1137 | strcpy (cmd, saved_cmd); | |
c906108c SS |
1138 | } |
1139 | } | |
1140 | } | |
6ecce94d AC |
1141 | |
1142 | do_cleanups (old_chain); | |
c906108c SS |
1143 | } |
1144 | ||
1145 | /* Switch to the specified thread. Will dispatch off to thread_apply_command | |
1146 | if prefix of arg is `apply'. */ | |
1147 | ||
1148 | static void | |
fba45db2 | 1149 | thread_command (char *tidstr, int from_tty) |
c906108c | 1150 | { |
c906108c SS |
1151 | if (!tidstr) |
1152 | { | |
c906108c | 1153 | if (target_has_stack) |
4f8d22e3 PA |
1154 | { |
1155 | if (is_exited (inferior_ptid)) | |
1156 | printf_filtered (_("[Current thread is %d (%s) (exited)]\n"), | |
1157 | pid_to_thread_id (inferior_ptid), | |
1158 | target_tid_to_str (inferior_ptid)); | |
1159 | else | |
1160 | printf_filtered (_("[Current thread is %d (%s)]\n"), | |
1161 | pid_to_thread_id (inferior_ptid), | |
1162 | target_tid_to_str (inferior_ptid)); | |
1163 | } | |
c906108c | 1164 | else |
8a3fe4f8 | 1165 | error (_("No stack.")); |
c906108c SS |
1166 | return; |
1167 | } | |
c5394b80 | 1168 | |
b8fa951a | 1169 | annotate_thread_changed (); |
ce43223b | 1170 | gdb_thread_select (uiout, tidstr, NULL); |
c5394b80 JM |
1171 | } |
1172 | ||
93815fbf VP |
1173 | /* Print notices when new threads are attached and detached. */ |
1174 | int print_thread_events = 1; | |
1175 | static void | |
1176 | show_print_thread_events (struct ui_file *file, int from_tty, | |
1177 | struct cmd_list_element *c, const char *value) | |
1178 | { | |
1179 | fprintf_filtered (file, _("\ | |
1180 | Printing of thread events is %s.\n"), | |
1181 | value); | |
1182 | } | |
1183 | ||
c5394b80 | 1184 | static int |
6949171e | 1185 | do_captured_thread_select (struct ui_out *uiout, void *tidstr) |
c5394b80 JM |
1186 | { |
1187 | int num; | |
1188 | struct thread_info *tp; | |
1189 | ||
81490ea1 | 1190 | num = value_as_long (parse_and_eval (tidstr)); |
c906108c SS |
1191 | |
1192 | tp = find_thread_id (num); | |
1193 | ||
8b93c638 | 1194 | if (!tp) |
8a3fe4f8 | 1195 | error (_("Thread ID %d not known."), num); |
c906108c SS |
1196 | |
1197 | if (!thread_alive (tp)) | |
8a3fe4f8 | 1198 | error (_("Thread ID %d has terminated."), num); |
c906108c | 1199 | |
94cc34af PA |
1200 | if (non_stop) |
1201 | context_switch_to (tp->ptid); | |
1202 | else | |
1203 | switch_to_thread (tp->ptid); | |
c906108c | 1204 | |
8b93c638 | 1205 | ui_out_text (uiout, "[Switching to thread "); |
39f77062 | 1206 | ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid)); |
8b93c638 | 1207 | ui_out_text (uiout, " ("); |
39f77062 | 1208 | ui_out_text (uiout, target_tid_to_str (inferior_ptid)); |
8b93c638 | 1209 | ui_out_text (uiout, ")]"); |
c5394b80 | 1210 | |
4f8d22e3 PA |
1211 | /* Note that we can't reach this with an exited thread, due to the |
1212 | thread_alive check above. */ | |
1213 | if (tp->state_ == THREAD_RUNNING) | |
94cc34af | 1214 | ui_out_text (uiout, "(running)\n"); |
4f8d22e3 PA |
1215 | else |
1216 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); | |
1217 | ||
1218 | /* Since the current thread may have changed, see if there is any | |
1219 | exited thread we can now delete. */ | |
1220 | prune_threads (); | |
94cc34af | 1221 | |
c5394b80 JM |
1222 | return GDB_RC_OK; |
1223 | } | |
1224 | ||
1225 | enum gdb_rc | |
ce43223b | 1226 | gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message) |
c5394b80 | 1227 | { |
b0b13bb4 DJ |
1228 | if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr, |
1229 | error_message, RETURN_MASK_ALL) < 0) | |
1230 | return GDB_RC_FAIL; | |
1231 | return GDB_RC_OK; | |
c906108c SS |
1232 | } |
1233 | ||
1234 | /* Commands with a prefix of `thread'. */ | |
1235 | struct cmd_list_element *thread_cmd_list = NULL; | |
1236 | ||
1237 | void | |
fba45db2 | 1238 | _initialize_thread (void) |
c906108c SS |
1239 | { |
1240 | static struct cmd_list_element *thread_apply_list = NULL; | |
94cc34af | 1241 | struct cmd_list_element *c; |
c906108c | 1242 | |
94cc34af PA |
1243 | c = add_info ("threads", info_threads_command, |
1244 | _("IDs of currently known threads.")); | |
1245 | set_cmd_async_ok (c); | |
4f8d22e3 | 1246 | set_cmd_no_selected_thread_ok (c); |
c906108c | 1247 | |
94cc34af | 1248 | c = add_prefix_cmd ("thread", class_run, thread_command, _("\ |
1bedd215 AC |
1249 | Use this command to switch between threads.\n\ |
1250 | The new thread ID must be currently known."), | |
94cc34af PA |
1251 | &thread_cmd_list, "thread ", 1, &cmdlist); |
1252 | set_cmd_async_ok (c); | |
4f8d22e3 | 1253 | set_cmd_no_selected_thread_ok (c); |
c906108c | 1254 | |
4f8d22e3 PA |
1255 | c = add_prefix_cmd ("apply", class_run, thread_apply_command, |
1256 | _("Apply a command to a list of threads."), | |
1257 | &thread_apply_list, "thread apply ", 1, &thread_cmd_list); | |
1258 | set_cmd_async_ok (c); | |
1259 | set_cmd_no_selected_thread_ok (c); | |
c906108c | 1260 | |
4f8d22e3 PA |
1261 | c = add_cmd ("all", class_run, thread_apply_all_command, |
1262 | _("Apply a command to all threads."), &thread_apply_list); | |
1263 | set_cmd_async_ok (c); | |
1264 | set_cmd_no_selected_thread_ok (c); | |
c906108c SS |
1265 | |
1266 | if (!xdb_commands) | |
1267 | add_com_alias ("t", "thread", class_run, 1); | |
93815fbf VP |
1268 | |
1269 | add_setshow_boolean_cmd ("thread-events", no_class, | |
1270 | &print_thread_events, _("\ | |
11c68c47 EZ |
1271 | Set printing of thread events (such as thread start and exit)."), _("\ |
1272 | Show printing of thread events (such as thread start and exit)."), NULL, | |
93815fbf VP |
1273 | NULL, |
1274 | show_print_thread_events, | |
1275 | &setprintlist, &showprintlist); | |
c906108c | 1276 | } |