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