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