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