]>
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" |
c906108c | 44 | |
0d06e24b | 45 | /* Definition of struct thread_info exported to gdbthread.h */ |
c906108c SS |
46 | |
47 | /* Prototypes for exported functions. */ | |
48 | ||
a14ed312 | 49 | void _initialize_thread (void); |
c906108c SS |
50 | |
51 | /* Prototypes for local functions. */ | |
52 | ||
c906108c SS |
53 | static struct thread_info *thread_list = NULL; |
54 | static int highest_thread_num; | |
55 | ||
a14ed312 | 56 | static struct thread_info *find_thread_id (int num); |
c906108c | 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 | |
8ea051c5 PA |
66 | static int main_thread_running = 0; |
67 | static int main_thread_executing = 0; | |
68 | ||
8601f500 MS |
69 | void |
70 | delete_step_resume_breakpoint (void *arg) | |
71 | { | |
72 | struct breakpoint **breakpointp = (struct breakpoint **) arg; | |
73 | struct thread_info *tp; | |
74 | ||
75 | if (*breakpointp != NULL) | |
76 | { | |
77 | delete_breakpoint (*breakpointp); | |
78 | for (tp = thread_list; tp; tp = tp->next) | |
79 | if (tp->step_resume_breakpoint == *breakpointp) | |
80 | tp->step_resume_breakpoint = NULL; | |
81 | ||
82 | *breakpointp = NULL; | |
83 | } | |
84 | } | |
85 | ||
7c952b6d ND |
86 | static void |
87 | free_thread (struct thread_info *tp) | |
88 | { | |
89 | /* NOTE: this will take care of any left-over step_resume breakpoints, | |
4d8453a5 DJ |
90 | but not any user-specified thread-specific breakpoints. We can not |
91 | delete the breakpoint straight-off, because the inferior might not | |
92 | be stopped at the moment. */ | |
7c952b6d | 93 | if (tp->step_resume_breakpoint) |
4d8453a5 | 94 | tp->step_resume_breakpoint->disposition = disp_del_at_next_stop; |
7c952b6d | 95 | |
a474d7c2 PA |
96 | bpstat_clear (&tp->stop_bpstat); |
97 | ||
7c952b6d ND |
98 | /* FIXME: do I ever need to call the back-end to give it a |
99 | chance at this private data before deleting the thread? */ | |
100 | if (tp->private) | |
b8c9b27d | 101 | xfree (tp->private); |
7c952b6d | 102 | |
b8c9b27d | 103 | xfree (tp); |
7c952b6d ND |
104 | } |
105 | ||
c906108c | 106 | void |
fba45db2 | 107 | init_thread_list (void) |
c906108c SS |
108 | { |
109 | struct thread_info *tp, *tpnext; | |
110 | ||
7c952b6d | 111 | highest_thread_num = 0; |
8ea051c5 PA |
112 | main_thread_running = 0; |
113 | main_thread_executing = 0; | |
114 | ||
c906108c SS |
115 | if (!thread_list) |
116 | return; | |
117 | ||
118 | for (tp = thread_list; tp; tp = tpnext) | |
119 | { | |
120 | tpnext = tp->next; | |
7c952b6d | 121 | free_thread (tp); |
c906108c SS |
122 | } |
123 | ||
124 | thread_list = NULL; | |
c906108c SS |
125 | } |
126 | ||
0d06e24b | 127 | struct thread_info * |
93815fbf | 128 | add_thread_silent (ptid_t ptid) |
c906108c SS |
129 | { |
130 | struct thread_info *tp; | |
131 | ||
6c0d3f6a MS |
132 | tp = (struct thread_info *) xmalloc (sizeof (*tp)); |
133 | memset (tp, 0, sizeof (*tp)); | |
39f77062 | 134 | tp->ptid = ptid; |
c906108c | 135 | tp->num = ++highest_thread_num; |
c906108c SS |
136 | tp->next = thread_list; |
137 | thread_list = tp; | |
cfc01461 VP |
138 | |
139 | observer_notify_new_thread (tp); | |
140 | ||
0d06e24b | 141 | return tp; |
c906108c SS |
142 | } |
143 | ||
93815fbf | 144 | struct thread_info * |
17faa917 | 145 | add_thread_with_info (ptid_t ptid, struct private_thread_info *private) |
93815fbf VP |
146 | { |
147 | struct thread_info *result = add_thread_silent (ptid); | |
148 | ||
17faa917 DJ |
149 | result->private = private; |
150 | ||
93815fbf | 151 | if (print_thread_events) |
fd532e2e | 152 | printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid)); |
d4fc5b1e NR |
153 | |
154 | annotate_new_thread (); | |
93815fbf VP |
155 | return result; |
156 | } | |
157 | ||
17faa917 DJ |
158 | struct thread_info * |
159 | add_thread (ptid_t ptid) | |
160 | { | |
161 | return add_thread_with_info (ptid, NULL); | |
162 | } | |
163 | ||
5e0b29c1 PA |
164 | /* Delete thread PTID. If SILENT, don't notify the observer of this |
165 | exit. */ | |
166 | static void | |
167 | delete_thread_1 (ptid_t ptid, int silent) | |
c906108c SS |
168 | { |
169 | struct thread_info *tp, *tpprev; | |
170 | ||
171 | tpprev = NULL; | |
172 | ||
173 | for (tp = thread_list; tp; tpprev = tp, tp = tp->next) | |
39f77062 | 174 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
175 | break; |
176 | ||
177 | if (!tp) | |
178 | return; | |
179 | ||
180 | if (tpprev) | |
181 | tpprev->next = tp->next; | |
182 | else | |
183 | thread_list = tp->next; | |
184 | ||
5e0b29c1 PA |
185 | if (!silent) |
186 | observer_notify_thread_exit (tp); | |
063bfe2e | 187 | |
7c952b6d | 188 | free_thread (tp); |
c906108c SS |
189 | } |
190 | ||
5e0b29c1 PA |
191 | void |
192 | delete_thread (ptid_t ptid) | |
193 | { | |
194 | delete_thread_1 (ptid, 0 /* not silent */); | |
195 | } | |
196 | ||
197 | void | |
198 | delete_thread_silent (ptid_t ptid) | |
199 | { | |
200 | delete_thread_1 (ptid, 1 /* silent */); | |
201 | } | |
202 | ||
c906108c | 203 | static struct thread_info * |
fba45db2 | 204 | find_thread_id (int num) |
c906108c SS |
205 | { |
206 | struct thread_info *tp; | |
207 | ||
208 | for (tp = thread_list; tp; tp = tp->next) | |
209 | if (tp->num == num) | |
210 | return tp; | |
211 | ||
212 | return NULL; | |
213 | } | |
214 | ||
39f77062 | 215 | /* Find a thread_info by matching PTID. */ |
0d06e24b | 216 | struct thread_info * |
39f77062 | 217 | find_thread_pid (ptid_t ptid) |
0d06e24b JM |
218 | { |
219 | struct thread_info *tp; | |
220 | ||
221 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 222 | if (ptid_equal (tp->ptid, ptid)) |
0d06e24b JM |
223 | return tp; |
224 | ||
225 | return NULL; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Thread iterator function. | |
230 | * | |
231 | * Calls a callback function once for each thread, so long as | |
232 | * the callback function returns false. If the callback function | |
233 | * returns true, the iteration will end and the current thread | |
234 | * will be returned. This can be useful for implementing a | |
235 | * search for a thread with arbitrary attributes, or for applying | |
236 | * some operation to every thread. | |
237 | * | |
238 | * FIXME: some of the existing functionality, such as | |
239 | * "Thread apply all", might be rewritten using this functionality. | |
240 | */ | |
241 | ||
242 | struct thread_info * | |
fd118b61 KB |
243 | iterate_over_threads (int (*callback) (struct thread_info *, void *), |
244 | void *data) | |
0d06e24b JM |
245 | { |
246 | struct thread_info *tp; | |
247 | ||
248 | for (tp = thread_list; tp; tp = tp->next) | |
249 | if ((*callback) (tp, data)) | |
250 | return tp; | |
251 | ||
252 | return NULL; | |
253 | } | |
254 | ||
20874c92 VP |
255 | int |
256 | thread_count (void) | |
257 | { | |
258 | int result = 0; | |
259 | struct thread_info *tp; | |
260 | ||
261 | for (tp = thread_list; tp; tp = tp->next) | |
262 | ++result; | |
263 | ||
264 | return result; | |
265 | } | |
266 | ||
c906108c | 267 | int |
fba45db2 | 268 | valid_thread_id (int num) |
c906108c SS |
269 | { |
270 | struct thread_info *tp; | |
271 | ||
272 | for (tp = thread_list; tp; tp = tp->next) | |
273 | if (tp->num == num) | |
274 | return 1; | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
279 | int | |
39f77062 | 280 | pid_to_thread_id (ptid_t ptid) |
c906108c SS |
281 | { |
282 | struct thread_info *tp; | |
283 | ||
284 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 285 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
286 | return tp->num; |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
39f77062 | 291 | ptid_t |
fba45db2 | 292 | thread_id_to_pid (int num) |
c906108c SS |
293 | { |
294 | struct thread_info *thread = find_thread_id (num); | |
295 | if (thread) | |
39f77062 | 296 | return thread->ptid; |
c906108c | 297 | else |
39f77062 | 298 | return pid_to_ptid (-1); |
c906108c SS |
299 | } |
300 | ||
301 | int | |
39f77062 | 302 | in_thread_list (ptid_t ptid) |
c906108c SS |
303 | { |
304 | struct thread_info *tp; | |
305 | ||
306 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 307 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
308 | return 1; |
309 | ||
310 | return 0; /* Never heard of 'im */ | |
311 | } | |
8926118c | 312 | |
8b93c638 JM |
313 | /* Print a list of thread ids currently known, and the total number of |
314 | threads. To be used from within catch_errors. */ | |
6949171e JJ |
315 | static int |
316 | do_captured_list_thread_ids (struct ui_out *uiout, void *arg) | |
8b93c638 JM |
317 | { |
318 | struct thread_info *tp; | |
319 | int num = 0; | |
3b31d625 | 320 | struct cleanup *cleanup_chain; |
8b93c638 | 321 | |
7990a578 EZ |
322 | prune_threads (); |
323 | target_find_new_threads (); | |
324 | ||
3b31d625 | 325 | cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids"); |
8b93c638 JM |
326 | |
327 | for (tp = thread_list; tp; tp = tp->next) | |
328 | { | |
329 | num++; | |
330 | ui_out_field_int (uiout, "thread-id", tp->num); | |
331 | } | |
332 | ||
3b31d625 | 333 | do_cleanups (cleanup_chain); |
8b93c638 JM |
334 | ui_out_field_int (uiout, "number-of-threads", num); |
335 | return GDB_RC_OK; | |
336 | } | |
337 | ||
338 | /* Official gdblib interface function to get a list of thread ids and | |
339 | the total number. */ | |
340 | enum gdb_rc | |
ce43223b | 341 | gdb_list_thread_ids (struct ui_out *uiout, char **error_message) |
8b93c638 | 342 | { |
b0b13bb4 DJ |
343 | if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL, |
344 | error_message, RETURN_MASK_ALL) < 0) | |
345 | return GDB_RC_FAIL; | |
346 | return GDB_RC_OK; | |
8b93c638 | 347 | } |
c906108c SS |
348 | |
349 | /* Load infrun state for the thread PID. */ | |
350 | ||
c5aa993b | 351 | void |
6949171e JJ |
352 | load_infrun_state (ptid_t ptid, |
353 | CORE_ADDR *prev_pc, | |
6c0d3f6a | 354 | int *trap_expected, |
fba45db2 | 355 | struct breakpoint **step_resume_breakpoint, |
6949171e | 356 | CORE_ADDR *step_range_start, |
6c0d3f6a | 357 | CORE_ADDR *step_range_end, |
6949171e | 358 | struct frame_id *step_frame_id, |
ca67fcb8 | 359 | int *stepping_over_breakpoint, |
6c0d3f6a | 360 | int *stepping_through_solib_after_catch, |
fba45db2 | 361 | bpstat *stepping_through_solib_catchpoints, |
6949171e | 362 | int *current_line, |
a474d7c2 PA |
363 | struct symtab **current_symtab, |
364 | struct continuation **continuations, | |
365 | struct continuation **intermediate_continuations, | |
366 | int *proceed_to_finish, | |
367 | enum step_over_calls_kind *step_over_calls, | |
368 | int *stop_step, | |
369 | int *step_multi, | |
370 | enum target_signal *stop_signal, | |
371 | bpstat *stop_bpstat) | |
c906108c SS |
372 | { |
373 | struct thread_info *tp; | |
374 | ||
375 | /* If we can't find the thread, then we're debugging a single threaded | |
376 | process. No need to do anything in that case. */ | |
39f77062 | 377 | tp = find_thread_id (pid_to_thread_id (ptid)); |
c906108c SS |
378 | if (tp == NULL) |
379 | return; | |
380 | ||
381 | *prev_pc = tp->prev_pc; | |
6c0d3f6a | 382 | *trap_expected = tp->trap_expected; |
c906108c SS |
383 | *step_resume_breakpoint = tp->step_resume_breakpoint; |
384 | *step_range_start = tp->step_range_start; | |
385 | *step_range_end = tp->step_range_end; | |
aa0cd9c1 | 386 | *step_frame_id = tp->step_frame_id; |
ca67fcb8 | 387 | *stepping_over_breakpoint = tp->stepping_over_breakpoint; |
6949171e JJ |
388 | *stepping_through_solib_after_catch = |
389 | tp->stepping_through_solib_after_catch; | |
390 | *stepping_through_solib_catchpoints = | |
391 | tp->stepping_through_solib_catchpoints; | |
6c0d3f6a MS |
392 | *current_line = tp->current_line; |
393 | *current_symtab = tp->current_symtab; | |
a474d7c2 PA |
394 | |
395 | /* In all-stop mode, these are global state, while in non-stop mode, | |
396 | they are per thread. */ | |
397 | if (non_stop) | |
398 | { | |
399 | *continuations = tp->continuations; | |
400 | tp->continuations = NULL; | |
401 | *intermediate_continuations = tp->intermediate_continuations; | |
402 | tp->intermediate_continuations = NULL; | |
403 | *proceed_to_finish = tp->proceed_to_finish; | |
404 | *step_over_calls = tp->step_over_calls; | |
405 | *stop_step = tp->stop_step; | |
406 | *step_multi = tp->step_multi; | |
407 | *stop_signal = tp->stop_signal; | |
408 | ||
409 | /* Swap instead of copy, so we only have to update one of | |
410 | them. */ | |
411 | *stop_bpstat = tp->stop_bpstat; | |
412 | tp->stop_bpstat = 0; | |
413 | } | |
c906108c SS |
414 | } |
415 | ||
416 | /* Save infrun state for the thread PID. */ | |
417 | ||
c5aa993b | 418 | void |
6949171e JJ |
419 | save_infrun_state (ptid_t ptid, |
420 | CORE_ADDR prev_pc, | |
6c0d3f6a | 421 | int trap_expected, |
fba45db2 | 422 | struct breakpoint *step_resume_breakpoint, |
6949171e | 423 | CORE_ADDR step_range_start, |
6c0d3f6a | 424 | CORE_ADDR step_range_end, |
6949171e | 425 | const struct frame_id *step_frame_id, |
ca67fcb8 | 426 | int stepping_over_breakpoint, |
6c0d3f6a | 427 | int stepping_through_solib_after_catch, |
fba45db2 | 428 | bpstat stepping_through_solib_catchpoints, |
6c0d3f6a | 429 | int current_line, |
a474d7c2 PA |
430 | struct symtab *current_symtab, |
431 | struct continuation *continuations, | |
432 | struct continuation *intermediate_continuations, | |
433 | int proceed_to_finish, | |
434 | enum step_over_calls_kind step_over_calls, | |
435 | int stop_step, | |
436 | int step_multi, | |
437 | enum target_signal stop_signal, | |
438 | bpstat stop_bpstat) | |
c906108c SS |
439 | { |
440 | struct thread_info *tp; | |
441 | ||
442 | /* If we can't find the thread, then we're debugging a single-threaded | |
443 | process. Nothing to do in that case. */ | |
39f77062 | 444 | tp = find_thread_id (pid_to_thread_id (ptid)); |
c906108c SS |
445 | if (tp == NULL) |
446 | return; | |
447 | ||
448 | tp->prev_pc = prev_pc; | |
6c0d3f6a | 449 | tp->trap_expected = trap_expected; |
c906108c SS |
450 | tp->step_resume_breakpoint = step_resume_breakpoint; |
451 | tp->step_range_start = step_range_start; | |
452 | tp->step_range_end = step_range_end; | |
aa0cd9c1 | 453 | tp->step_frame_id = (*step_frame_id); |
ca67fcb8 | 454 | tp->stepping_over_breakpoint = stepping_over_breakpoint; |
c906108c SS |
455 | tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch; |
456 | tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints; | |
6c0d3f6a MS |
457 | tp->current_line = current_line; |
458 | tp->current_symtab = current_symtab; | |
a474d7c2 PA |
459 | |
460 | /* In all-stop mode, these are global state, while in non-stop mode, | |
461 | they are per thread. */ | |
462 | if (non_stop) | |
463 | { | |
464 | tp->continuations = continuations; | |
465 | tp->intermediate_continuations = intermediate_continuations; | |
466 | tp->proceed_to_finish = proceed_to_finish; | |
467 | tp->step_over_calls = step_over_calls; | |
468 | tp->stop_step = stop_step; | |
469 | tp->step_multi = step_multi; | |
470 | tp->stop_signal = stop_signal; | |
471 | tp->stop_bpstat = stop_bpstat; | |
472 | } | |
c906108c SS |
473 | } |
474 | ||
475 | /* Return true if TP is an active thread. */ | |
476 | static int | |
fba45db2 | 477 | thread_alive (struct thread_info *tp) |
c906108c | 478 | { |
39f77062 | 479 | if (PIDGET (tp->ptid) == -1) |
c906108c | 480 | return 0; |
39f77062 | 481 | if (!target_thread_alive (tp->ptid)) |
c906108c | 482 | { |
39f77062 | 483 | tp->ptid = pid_to_ptid (-1); /* Mark it as dead */ |
c906108c SS |
484 | return 0; |
485 | } | |
486 | return 1; | |
487 | } | |
488 | ||
489 | static void | |
fba45db2 | 490 | prune_threads (void) |
c906108c | 491 | { |
d4f3574e | 492 | struct thread_info *tp, *next; |
c906108c | 493 | |
c906108c SS |
494 | for (tp = thread_list; tp; tp = next) |
495 | { | |
496 | next = tp->next; | |
497 | if (!thread_alive (tp)) | |
39f77062 | 498 | delete_thread (tp->ptid); |
c906108c SS |
499 | } |
500 | } | |
501 | ||
e1ac3328 VP |
502 | void |
503 | set_running (ptid_t ptid, int running) | |
504 | { | |
505 | struct thread_info *tp; | |
506 | ||
507 | if (!thread_list) | |
508 | { | |
509 | /* This is one of the targets that does not add main | |
510 | thread to the thread list. Just use a single | |
511 | global flag to indicate that a thread is running. | |
512 | ||
513 | This problem is unique to ST programs. For MT programs, | |
514 | the main thread is always present in the thread list. If it's | |
515 | not, the first call to context_switch will mess up GDB internal | |
516 | state. */ | |
8f6a8e84 | 517 | if (running && !main_thread_running && !suppress_resume_observer) |
e1ac3328 VP |
518 | observer_notify_target_resumed (ptid); |
519 | main_thread_running = running; | |
520 | return; | |
521 | } | |
522 | ||
523 | /* We try not to notify the observer if no thread has actually changed | |
524 | the running state -- merely to reduce the number of messages to | |
525 | frontend. Frontend is supposed to handle multiple *running just fine. */ | |
526 | if (PIDGET (ptid) == -1) | |
527 | { | |
528 | int any_started = 0; | |
529 | for (tp = thread_list; tp; tp = tp->next) | |
530 | { | |
531 | if (running && !tp->running_) | |
532 | any_started = 1; | |
533 | tp->running_ = running; | |
534 | } | |
8f6a8e84 | 535 | if (any_started && !suppress_resume_observer) |
e1ac3328 VP |
536 | observer_notify_target_resumed (ptid); |
537 | } | |
538 | else | |
539 | { | |
540 | tp = find_thread_pid (ptid); | |
541 | gdb_assert (tp); | |
8f6a8e84 | 542 | if (running && !tp->running_ && !suppress_resume_observer) |
e1ac3328 VP |
543 | observer_notify_target_resumed (ptid); |
544 | tp->running_ = running; | |
545 | } | |
546 | } | |
547 | ||
548 | int | |
549 | is_running (ptid_t ptid) | |
550 | { | |
551 | struct thread_info *tp; | |
552 | ||
8ea051c5 PA |
553 | if (!target_has_execution) |
554 | return 0; | |
555 | ||
e1ac3328 VP |
556 | if (!thread_list) |
557 | return main_thread_running; | |
558 | ||
559 | tp = find_thread_pid (ptid); | |
560 | gdb_assert (tp); | |
561 | return tp->running_; | |
562 | } | |
563 | ||
8ea051c5 PA |
564 | int |
565 | any_running (void) | |
566 | { | |
567 | struct thread_info *tp; | |
568 | ||
569 | if (!target_has_execution) | |
570 | return 0; | |
571 | ||
572 | if (!thread_list) | |
573 | return main_thread_running; | |
574 | ||
575 | for (tp = thread_list; tp; tp = tp->next) | |
576 | if (tp->running_) | |
577 | return 1; | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
582 | int | |
583 | is_executing (ptid_t ptid) | |
584 | { | |
585 | struct thread_info *tp; | |
586 | ||
587 | if (!target_has_execution) | |
588 | return 0; | |
589 | ||
590 | if (!thread_list) | |
591 | return main_thread_executing; | |
592 | ||
593 | tp = find_thread_pid (ptid); | |
594 | gdb_assert (tp); | |
595 | return tp->executing_; | |
596 | } | |
597 | ||
598 | void | |
599 | set_executing (ptid_t ptid, int executing) | |
600 | { | |
601 | struct thread_info *tp; | |
602 | ||
603 | if (!thread_list) | |
604 | { | |
605 | /* This target does not add the main thread to the thread list. | |
606 | Use a global flag to indicate that the thread is | |
607 | executing. */ | |
608 | main_thread_executing = executing; | |
609 | return; | |
610 | } | |
611 | ||
612 | if (PIDGET (ptid) == -1) | |
613 | { | |
614 | for (tp = thread_list; tp; tp = tp->next) | |
615 | tp->executing_ = executing; | |
616 | } | |
617 | else | |
618 | { | |
619 | tp = find_thread_pid (ptid); | |
620 | gdb_assert (tp); | |
621 | tp->executing_ = executing; | |
622 | } | |
623 | } | |
624 | ||
8e8901c5 VP |
625 | /* Prints the list of threads and their details on UIOUT. |
626 | This is a version of 'info_thread_command' suitable for | |
627 | use from MI. | |
628 | If REQESTED_THREAD is not -1, it's the GDB id of the thread | |
629 | that should be printed. Otherwise, all threads are | |
630 | printed. */ | |
631 | void | |
632 | print_thread_info (struct ui_out *uiout, int requested_thread) | |
c906108c SS |
633 | { |
634 | struct thread_info *tp; | |
39f77062 | 635 | ptid_t current_ptid; |
c5aa993b | 636 | struct frame_info *cur_frame; |
99b3d574 DP |
637 | struct cleanup *old_chain; |
638 | struct frame_id saved_frame_id; | |
0d06e24b | 639 | char *extra_info; |
8e8901c5 | 640 | int current_thread = -1; |
c906108c | 641 | |
99b3d574 DP |
642 | /* Backup current thread and selected frame. */ |
643 | saved_frame_id = get_frame_id (get_selected_frame (NULL)); | |
644 | old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id); | |
645 | ||
8e8901c5 VP |
646 | make_cleanup_ui_out_list_begin_end (uiout, "threads"); |
647 | ||
c906108c | 648 | prune_threads (); |
b83266a0 | 649 | target_find_new_threads (); |
39f77062 | 650 | current_ptid = inferior_ptid; |
c906108c SS |
651 | for (tp = thread_list; tp; tp = tp->next) |
652 | { | |
8e8901c5 VP |
653 | struct cleanup *chain2; |
654 | ||
655 | if (requested_thread != -1 && tp->num != requested_thread) | |
656 | continue; | |
657 | ||
658 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
659 | ||
39f77062 | 660 | if (ptid_equal (tp->ptid, current_ptid)) |
8e8901c5 VP |
661 | { |
662 | current_thread = tp->num; | |
663 | ui_out_text (uiout, "* "); | |
664 | } | |
c906108c | 665 | else |
8e8901c5 | 666 | ui_out_text (uiout, " "); |
c906108c | 667 | |
8e8901c5 VP |
668 | ui_out_field_int (uiout, "id", tp->num); |
669 | ui_out_text (uiout, " "); | |
670 | ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid)); | |
0d06e24b JM |
671 | |
672 | extra_info = target_extra_thread_info (tp); | |
673 | if (extra_info) | |
8e8901c5 VP |
674 | { |
675 | ui_out_text (uiout, " ("); | |
676 | ui_out_field_string (uiout, "details", extra_info); | |
677 | ui_out_text (uiout, ")"); | |
678 | } | |
679 | ui_out_text (uiout, " "); | |
99b3d574 | 680 | /* That switch put us at the top of the stack (leaf frame). */ |
39f77062 | 681 | switch_to_thread (tp->ptid); |
8e8901c5 VP |
682 | print_stack_frame (get_selected_frame (NULL), |
683 | /* For MI output, print frame level. */ | |
684 | ui_out_is_mi_like_p (uiout), | |
685 | LOCATION); | |
686 | ||
687 | do_cleanups (chain2); | |
c906108c SS |
688 | } |
689 | ||
99b3d574 DP |
690 | /* Restores the current thread and the frame selected before |
691 | the "info threads" command. */ | |
692 | do_cleanups (old_chain); | |
c906108c | 693 | |
8e8901c5 VP |
694 | if (requested_thread == -1) |
695 | { | |
0bcd3e20 VP |
696 | gdb_assert (current_thread != -1 || !thread_list); |
697 | if (current_thread != -1 && ui_out_is_mi_like_p (uiout)) | |
8e8901c5 VP |
698 | ui_out_field_int (uiout, "current-thread-id", current_thread); |
699 | } | |
700 | ||
99b3d574 DP |
701 | /* If case we were not able to find the original frame, print the |
702 | new selected frame. */ | |
703 | if (frame_find_by_id (saved_frame_id) == NULL) | |
c906108c | 704 | { |
8a3fe4f8 | 705 | warning (_("Couldn't restore frame in current thread, at frame 0")); |
8e8901c5 VP |
706 | /* For MI, we should probably have a notification about |
707 | current frame change. But this error is not very likely, so | |
708 | don't bother for now. */ | |
709 | if (!ui_out_is_mi_like_p (uiout)) | |
710 | print_stack_frame (get_selected_frame (NULL), 0, LOCATION); | |
c906108c | 711 | } |
c906108c SS |
712 | } |
713 | ||
8e8901c5 VP |
714 | |
715 | /* Print information about currently known threads | |
716 | ||
717 | * Note: this has the drawback that it _really_ switches | |
718 | * threads, which frees the frame cache. A no-side | |
719 | * effects info-threads command would be nicer. | |
720 | */ | |
721 | ||
722 | static void | |
723 | info_threads_command (char *arg, int from_tty) | |
724 | { | |
725 | print_thread_info (uiout, -1); | |
726 | } | |
727 | ||
c906108c SS |
728 | /* Switch from one thread to another. */ |
729 | ||
6a6b96b9 | 730 | void |
39f77062 | 731 | switch_to_thread (ptid_t ptid) |
c906108c | 732 | { |
39f77062 | 733 | if (ptid_equal (ptid, inferior_ptid)) |
c906108c SS |
734 | return; |
735 | ||
39f77062 | 736 | inferior_ptid = ptid; |
35f196d9 | 737 | reinit_frame_cache (); |
c906108c | 738 | registers_changed (); |
c5aa993b | 739 | stop_pc = read_pc (); |
c906108c SS |
740 | } |
741 | ||
742 | static void | |
39f77062 | 743 | restore_current_thread (ptid_t ptid) |
c906108c | 744 | { |
6949171e | 745 | if (!ptid_equal (ptid, inferior_ptid)) |
c906108c | 746 | { |
39f77062 | 747 | switch_to_thread (ptid); |
99b3d574 DP |
748 | } |
749 | } | |
750 | ||
751 | static void | |
752 | restore_selected_frame (struct frame_id a_frame_id) | |
753 | { | |
754 | struct frame_info *selected_frame_info = NULL; | |
755 | ||
756 | if (frame_id_eq (a_frame_id, null_frame_id)) | |
757 | return; | |
758 | ||
759 | if ((selected_frame_info = frame_find_by_id (a_frame_id)) != NULL) | |
760 | { | |
761 | select_frame (selected_frame_info); | |
c906108c SS |
762 | } |
763 | } | |
764 | ||
6ecce94d AC |
765 | struct current_thread_cleanup |
766 | { | |
39f77062 | 767 | ptid_t inferior_ptid; |
99b3d574 | 768 | struct frame_id selected_frame_id; |
6ecce94d AC |
769 | }; |
770 | ||
771 | static void | |
772 | do_restore_current_thread_cleanup (void *arg) | |
773 | { | |
774 | struct current_thread_cleanup *old = arg; | |
39f77062 | 775 | restore_current_thread (old->inferior_ptid); |
99b3d574 | 776 | restore_selected_frame (old->selected_frame_id); |
b8c9b27d | 777 | xfree (old); |
6ecce94d AC |
778 | } |
779 | ||
6208b47d | 780 | struct cleanup * |
99b3d574 DP |
781 | make_cleanup_restore_current_thread (ptid_t inferior_ptid, |
782 | struct frame_id a_frame_id) | |
6ecce94d AC |
783 | { |
784 | struct current_thread_cleanup *old | |
785 | = xmalloc (sizeof (struct current_thread_cleanup)); | |
39f77062 | 786 | old->inferior_ptid = inferior_ptid; |
99b3d574 | 787 | old->selected_frame_id = a_frame_id; |
6ecce94d AC |
788 | return make_cleanup (do_restore_current_thread_cleanup, old); |
789 | } | |
790 | ||
c906108c SS |
791 | /* Apply a GDB command to a list of threads. List syntax is a whitespace |
792 | seperated list of numbers, or ranges, or the keyword `all'. Ranges consist | |
793 | of two numbers seperated by a hyphen. Examples: | |
794 | ||
c5aa993b JM |
795 | thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4 |
796 | thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9 | |
797 | thread apply all p x/i $pc Apply x/i $pc cmd to all threads | |
798 | */ | |
c906108c SS |
799 | |
800 | static void | |
fba45db2 | 801 | thread_apply_all_command (char *cmd, int from_tty) |
c906108c SS |
802 | { |
803 | struct thread_info *tp; | |
804 | struct cleanup *old_chain; | |
e35ce267 CF |
805 | struct cleanup *saved_cmd_cleanup_chain; |
806 | char *saved_cmd; | |
99b3d574 DP |
807 | struct frame_id saved_frame_id; |
808 | ptid_t current_ptid; | |
809 | int thread_has_changed = 0; | |
c906108c SS |
810 | |
811 | if (cmd == NULL || *cmd == '\000') | |
8a3fe4f8 | 812 | error (_("Please specify a command following the thread ID list")); |
99b3d574 DP |
813 | |
814 | current_ptid = inferior_ptid; | |
815 | saved_frame_id = get_frame_id (get_selected_frame (NULL)); | |
816 | old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id); | |
c906108c | 817 | |
e9d196c5 MS |
818 | /* It is safe to update the thread list now, before |
819 | traversing it for "thread apply all". MVS */ | |
820 | target_find_new_threads (); | |
821 | ||
e35ce267 CF |
822 | /* Save a copy of the command in case it is clobbered by |
823 | execute_command */ | |
5b616ba1 | 824 | saved_cmd = xstrdup (cmd); |
b8c9b27d | 825 | saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd); |
c906108c SS |
826 | for (tp = thread_list; tp; tp = tp->next) |
827 | if (thread_alive (tp)) | |
828 | { | |
39f77062 | 829 | switch_to_thread (tp->ptid); |
a3f17187 | 830 | printf_filtered (_("\nThread %d (%s):\n"), |
6949171e | 831 | tp->num, target_tid_to_str (inferior_ptid)); |
c906108c | 832 | execute_command (cmd, from_tty); |
6949171e | 833 | strcpy (cmd, saved_cmd); /* Restore exact command used previously */ |
c906108c | 834 | } |
6ecce94d | 835 | |
99b3d574 DP |
836 | if (!ptid_equal (current_ptid, inferior_ptid)) |
837 | thread_has_changed = 1; | |
838 | ||
e35ce267 | 839 | do_cleanups (saved_cmd_cleanup_chain); |
6ecce94d | 840 | do_cleanups (old_chain); |
99b3d574 DP |
841 | /* Print stack frame only if we changed thread. */ |
842 | if (thread_has_changed) | |
843 | print_stack_frame (get_current_frame (), 1, SRC_LINE); | |
844 | ||
c906108c SS |
845 | } |
846 | ||
847 | static void | |
fba45db2 | 848 | thread_apply_command (char *tidlist, int from_tty) |
c906108c SS |
849 | { |
850 | char *cmd; | |
851 | char *p; | |
852 | struct cleanup *old_chain; | |
e35ce267 CF |
853 | struct cleanup *saved_cmd_cleanup_chain; |
854 | char *saved_cmd; | |
99b3d574 DP |
855 | struct frame_id saved_frame_id; |
856 | ptid_t current_ptid; | |
857 | int thread_has_changed = 0; | |
c906108c SS |
858 | |
859 | if (tidlist == NULL || *tidlist == '\000') | |
8a3fe4f8 | 860 | error (_("Please specify a thread ID list")); |
c906108c | 861 | |
c5aa993b | 862 | for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++); |
c906108c SS |
863 | |
864 | if (*cmd == '\000') | |
8a3fe4f8 | 865 | error (_("Please specify a command following the thread ID list")); |
c906108c | 866 | |
99b3d574 DP |
867 | current_ptid = inferior_ptid; |
868 | saved_frame_id = get_frame_id (get_selected_frame (NULL)); | |
869 | old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id); | |
c906108c | 870 | |
e35ce267 CF |
871 | /* Save a copy of the command in case it is clobbered by |
872 | execute_command */ | |
5b616ba1 | 873 | saved_cmd = xstrdup (cmd); |
b8c9b27d | 874 | saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd); |
c906108c SS |
875 | while (tidlist < cmd) |
876 | { | |
877 | struct thread_info *tp; | |
878 | int start, end; | |
879 | ||
880 | start = strtol (tidlist, &p, 10); | |
881 | if (p == tidlist) | |
8a3fe4f8 | 882 | error (_("Error parsing %s"), tidlist); |
c906108c SS |
883 | tidlist = p; |
884 | ||
885 | while (*tidlist == ' ' || *tidlist == '\t') | |
886 | tidlist++; | |
887 | ||
888 | if (*tidlist == '-') /* Got a range of IDs? */ | |
889 | { | |
c5aa993b | 890 | tidlist++; /* Skip the - */ |
c906108c SS |
891 | end = strtol (tidlist, &p, 10); |
892 | if (p == tidlist) | |
8a3fe4f8 | 893 | error (_("Error parsing %s"), tidlist); |
c906108c SS |
894 | tidlist = p; |
895 | ||
896 | while (*tidlist == ' ' || *tidlist == '\t') | |
897 | tidlist++; | |
898 | } | |
899 | else | |
900 | end = start; | |
901 | ||
902 | for (; start <= end; start++) | |
903 | { | |
904 | tp = find_thread_id (start); | |
905 | ||
906 | if (!tp) | |
8a3fe4f8 | 907 | warning (_("Unknown thread %d."), start); |
c906108c | 908 | else if (!thread_alive (tp)) |
8a3fe4f8 | 909 | warning (_("Thread %d has terminated."), start); |
c906108c SS |
910 | else |
911 | { | |
39f77062 | 912 | switch_to_thread (tp->ptid); |
a3f17187 | 913 | printf_filtered (_("\nThread %d (%s):\n"), tp->num, |
39f77062 | 914 | target_tid_to_str (inferior_ptid)); |
c906108c | 915 | execute_command (cmd, from_tty); |
e35ce267 | 916 | strcpy (cmd, saved_cmd); /* Restore exact command used previously */ |
c906108c SS |
917 | } |
918 | } | |
919 | } | |
6ecce94d | 920 | |
99b3d574 DP |
921 | if (!ptid_equal (current_ptid, inferior_ptid)) |
922 | thread_has_changed = 1; | |
923 | ||
e35ce267 | 924 | do_cleanups (saved_cmd_cleanup_chain); |
6ecce94d | 925 | do_cleanups (old_chain); |
99b3d574 DP |
926 | /* Print stack frame only if we changed thread. */ |
927 | if (thread_has_changed) | |
928 | print_stack_frame (get_current_frame (), 1, SRC_LINE); | |
c906108c SS |
929 | } |
930 | ||
931 | /* Switch to the specified thread. Will dispatch off to thread_apply_command | |
932 | if prefix of arg is `apply'. */ | |
933 | ||
934 | static void | |
fba45db2 | 935 | thread_command (char *tidstr, int from_tty) |
c906108c | 936 | { |
c906108c SS |
937 | if (!tidstr) |
938 | { | |
939 | /* Don't generate an error, just say which thread is current. */ | |
940 | if (target_has_stack) | |
a3f17187 | 941 | printf_filtered (_("[Current thread is %d (%s)]\n"), |
39f77062 | 942 | pid_to_thread_id (inferior_ptid), |
007d08bb | 943 | target_tid_to_str (inferior_ptid)); |
c906108c | 944 | else |
8a3fe4f8 | 945 | error (_("No stack.")); |
c906108c SS |
946 | return; |
947 | } | |
c5394b80 | 948 | |
b8fa951a | 949 | annotate_thread_changed (); |
ce43223b | 950 | gdb_thread_select (uiout, tidstr, NULL); |
c5394b80 JM |
951 | } |
952 | ||
93815fbf VP |
953 | /* Print notices when new threads are attached and detached. */ |
954 | int print_thread_events = 1; | |
955 | static void | |
956 | show_print_thread_events (struct ui_file *file, int from_tty, | |
957 | struct cmd_list_element *c, const char *value) | |
958 | { | |
959 | fprintf_filtered (file, _("\ | |
960 | Printing of thread events is %s.\n"), | |
961 | value); | |
962 | } | |
963 | ||
c5394b80 | 964 | static int |
6949171e | 965 | do_captured_thread_select (struct ui_out *uiout, void *tidstr) |
c5394b80 JM |
966 | { |
967 | int num; | |
968 | struct thread_info *tp; | |
969 | ||
81490ea1 | 970 | num = value_as_long (parse_and_eval (tidstr)); |
c906108c SS |
971 | |
972 | tp = find_thread_id (num); | |
973 | ||
8b93c638 | 974 | if (!tp) |
8a3fe4f8 | 975 | error (_("Thread ID %d not known."), num); |
c906108c SS |
976 | |
977 | if (!thread_alive (tp)) | |
8a3fe4f8 | 978 | error (_("Thread ID %d has terminated."), num); |
c906108c | 979 | |
39f77062 | 980 | switch_to_thread (tp->ptid); |
c906108c | 981 | |
8b93c638 | 982 | ui_out_text (uiout, "[Switching to thread "); |
39f77062 | 983 | ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid)); |
8b93c638 | 984 | ui_out_text (uiout, " ("); |
39f77062 | 985 | ui_out_text (uiout, target_tid_to_str (inferior_ptid)); |
8b93c638 | 986 | ui_out_text (uiout, ")]"); |
c5394b80 | 987 | |
b04f3ab4 | 988 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); |
c5394b80 JM |
989 | return GDB_RC_OK; |
990 | } | |
991 | ||
992 | enum gdb_rc | |
ce43223b | 993 | gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message) |
c5394b80 | 994 | { |
b0b13bb4 DJ |
995 | if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr, |
996 | error_message, RETURN_MASK_ALL) < 0) | |
997 | return GDB_RC_FAIL; | |
998 | return GDB_RC_OK; | |
c906108c SS |
999 | } |
1000 | ||
1001 | /* Commands with a prefix of `thread'. */ | |
1002 | struct cmd_list_element *thread_cmd_list = NULL; | |
1003 | ||
1004 | void | |
fba45db2 | 1005 | _initialize_thread (void) |
c906108c SS |
1006 | { |
1007 | static struct cmd_list_element *thread_apply_list = NULL; | |
c906108c SS |
1008 | |
1009 | add_info ("threads", info_threads_command, | |
1bedd215 | 1010 | _("IDs of currently known threads.")); |
c906108c | 1011 | |
1bedd215 AC |
1012 | add_prefix_cmd ("thread", class_run, thread_command, _("\ |
1013 | Use this command to switch between threads.\n\ | |
1014 | The new thread ID must be currently known."), | |
1015 | &thread_cmd_list, "thread ", 1, &cmdlist); | |
c906108c SS |
1016 | |
1017 | add_prefix_cmd ("apply", class_run, thread_apply_command, | |
1bedd215 | 1018 | _("Apply a command to a list of threads."), |
ad21ceb0 | 1019 | &thread_apply_list, "thread apply ", 1, &thread_cmd_list); |
c906108c SS |
1020 | |
1021 | add_cmd ("all", class_run, thread_apply_all_command, | |
1a966eab | 1022 | _("Apply a command to all threads."), &thread_apply_list); |
c906108c SS |
1023 | |
1024 | if (!xdb_commands) | |
1025 | add_com_alias ("t", "thread", class_run, 1); | |
93815fbf VP |
1026 | |
1027 | add_setshow_boolean_cmd ("thread-events", no_class, | |
1028 | &print_thread_events, _("\ | |
11c68c47 EZ |
1029 | Set printing of thread events (such as thread start and exit)."), _("\ |
1030 | Show printing of thread events (such as thread start and exit)."), NULL, | |
93815fbf VP |
1031 | NULL, |
1032 | show_print_thread_events, | |
1033 | &setprintlist, &showprintlist); | |
c906108c | 1034 | } |