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