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