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