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