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