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