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