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