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