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