Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Multi-process/thread control for GDB, the GNU debugger. |
8926118c | 2 | |
ecd75fc8 | 3 | Copyright (C) 1986-2014 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" | |
60250e8b | 30 | #include "exceptions.h" |
c906108c SS |
31 | #include "command.h" |
32 | #include "gdbcmd.h" | |
4e052eda | 33 | #include "regcache.h" |
5b7f31a4 | 34 | #include "gdb.h" |
02d27625 | 35 | #include "btrace.h" |
c906108c SS |
36 | |
37 | #include <ctype.h> | |
38 | #include <sys/types.h> | |
39 | #include <signal.h> | |
8b93c638 | 40 | #include "ui-out.h" |
683f2885 | 41 | #include "observer.h" |
d4fc5b1e | 42 | #include "annotate.h" |
94cc34af | 43 | #include "cli/cli-decode.h" |
60f98dde | 44 | #include "gdb_regex.h" |
aea5b279 | 45 | #include "cli/cli-utils.h" |
be34f849 | 46 | #include "continuations.h" |
94cc34af | 47 | |
c378eb4e | 48 | /* Definition of struct thread_info exported to gdbthread.h. */ |
c906108c | 49 | |
c378eb4e | 50 | /* Prototypes for exported functions. */ |
c906108c | 51 | |
a14ed312 | 52 | void _initialize_thread (void); |
c906108c | 53 | |
c378eb4e | 54 | /* Prototypes for local functions. */ |
c906108c | 55 | |
e5ef252a | 56 | struct thread_info *thread_list = NULL; |
c906108c SS |
57 | static int highest_thread_num; |
58 | ||
a14ed312 KB |
59 | static void thread_command (char *tidstr, int from_tty); |
60 | static void thread_apply_all_command (char *, int); | |
61 | static int thread_alive (struct thread_info *); | |
62 | static void info_threads_command (char *, int); | |
63 | static void thread_apply_command (char *, int); | |
39f77062 | 64 | static void restore_current_thread (ptid_t); |
a14ed312 | 65 | static void prune_threads (void); |
c906108c | 66 | |
054e8d9e AA |
67 | /* Data to cleanup thread array. */ |
68 | ||
69 | struct thread_array_cleanup | |
70 | { | |
71 | /* Array of thread pointers used to set | |
72 | reference count. */ | |
73 | struct thread_info **tp_array; | |
74 | ||
75 | /* Thread count in the array. */ | |
76 | int count; | |
77 | }; | |
78 | ||
79 | ||
a5321aa4 | 80 | struct thread_info* |
4e1c45ea | 81 | inferior_thread (void) |
8601f500 | 82 | { |
e09875d4 | 83 | struct thread_info *tp = find_thread_ptid (inferior_ptid); |
4e1c45ea PA |
84 | gdb_assert (tp); |
85 | return tp; | |
86 | } | |
8601f500 | 87 | |
4e1c45ea PA |
88 | void |
89 | delete_step_resume_breakpoint (struct thread_info *tp) | |
90 | { | |
8358c15c | 91 | if (tp && tp->control.step_resume_breakpoint) |
8601f500 | 92 | { |
8358c15c JK |
93 | delete_breakpoint (tp->control.step_resume_breakpoint); |
94 | tp->control.step_resume_breakpoint = NULL; | |
8601f500 MS |
95 | } |
96 | } | |
97 | ||
186c406b TT |
98 | void |
99 | delete_exception_resume_breakpoint (struct thread_info *tp) | |
100 | { | |
101 | if (tp && tp->control.exception_resume_breakpoint) | |
102 | { | |
103 | delete_breakpoint (tp->control.exception_resume_breakpoint); | |
104 | tp->control.exception_resume_breakpoint = NULL; | |
105 | } | |
106 | } | |
107 | ||
7c952b6d | 108 | static void |
4f8d22e3 | 109 | clear_thread_inferior_resources (struct thread_info *tp) |
7c952b6d ND |
110 | { |
111 | /* NOTE: this will take care of any left-over step_resume breakpoints, | |
4d8453a5 DJ |
112 | but not any user-specified thread-specific breakpoints. We can not |
113 | delete the breakpoint straight-off, because the inferior might not | |
114 | be stopped at the moment. */ | |
8358c15c | 115 | if (tp->control.step_resume_breakpoint) |
4f8d22e3 | 116 | { |
8358c15c JK |
117 | tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop; |
118 | tp->control.step_resume_breakpoint = NULL; | |
4f8d22e3 | 119 | } |
7c952b6d | 120 | |
186c406b TT |
121 | if (tp->control.exception_resume_breakpoint) |
122 | { | |
123 | tp->control.exception_resume_breakpoint->disposition | |
124 | = disp_del_at_next_stop; | |
125 | tp->control.exception_resume_breakpoint = NULL; | |
126 | } | |
127 | ||
f59f708a PA |
128 | delete_longjmp_breakpoint_at_next_stop (tp->num); |
129 | ||
16c381f0 | 130 | bpstat_clear (&tp->control.stop_bpstat); |
95e54da7 | 131 | |
02d27625 MM |
132 | btrace_teardown (tp); |
133 | ||
fa4cd53f PA |
134 | do_all_intermediate_continuations_thread (tp, 1); |
135 | do_all_continuations_thread (tp, 1); | |
4f8d22e3 PA |
136 | } |
137 | ||
138 | static void | |
139 | free_thread (struct thread_info *tp) | |
140 | { | |
7c952b6d | 141 | if (tp->private) |
dc146f7c VP |
142 | { |
143 | if (tp->private_dtor) | |
144 | tp->private_dtor (tp->private); | |
145 | else | |
146 | xfree (tp->private); | |
147 | } | |
7c952b6d | 148 | |
4694da01 | 149 | xfree (tp->name); |
b8c9b27d | 150 | xfree (tp); |
7c952b6d ND |
151 | } |
152 | ||
c906108c | 153 | void |
fba45db2 | 154 | init_thread_list (void) |
c906108c SS |
155 | { |
156 | struct thread_info *tp, *tpnext; | |
157 | ||
7c952b6d | 158 | highest_thread_num = 0; |
8ea051c5 | 159 | |
c906108c SS |
160 | if (!thread_list) |
161 | return; | |
162 | ||
163 | for (tp = thread_list; tp; tp = tpnext) | |
164 | { | |
165 | tpnext = tp->next; | |
7c952b6d | 166 | free_thread (tp); |
c906108c SS |
167 | } |
168 | ||
169 | thread_list = NULL; | |
c906108c SS |
170 | } |
171 | ||
e58b0e63 PA |
172 | /* Allocate a new thread with target id PTID and add it to the thread |
173 | list. */ | |
174 | ||
175 | static struct thread_info * | |
176 | new_thread (ptid_t ptid) | |
177 | { | |
178 | struct thread_info *tp; | |
179 | ||
180 | tp = xcalloc (1, sizeof (*tp)); | |
181 | ||
182 | tp->ptid = ptid; | |
183 | tp->num = ++highest_thread_num; | |
184 | tp->next = thread_list; | |
185 | thread_list = tp; | |
186 | ||
187 | /* Nothing to follow yet. */ | |
188 | tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS; | |
30596231 | 189 | tp->state = THREAD_STOPPED; |
e58b0e63 PA |
190 | |
191 | return tp; | |
192 | } | |
193 | ||
0d06e24b | 194 | struct thread_info * |
93815fbf | 195 | add_thread_silent (ptid_t ptid) |
c906108c SS |
196 | { |
197 | struct thread_info *tp; | |
198 | ||
e09875d4 | 199 | tp = find_thread_ptid (ptid); |
4f8d22e3 PA |
200 | if (tp) |
201 | /* Found an old thread with the same id. It has to be dead, | |
202 | otherwise we wouldn't be adding a new thread with the same id. | |
203 | The OS is reusing this id --- delete it, and recreate a new | |
204 | one. */ | |
205 | { | |
206 | /* In addition to deleting the thread, if this is the current | |
dcf4fbde PA |
207 | thread, then we need to take care that delete_thread doesn't |
208 | really delete the thread if it is inferior_ptid. Create a | |
209 | new template thread in the list with an invalid ptid, switch | |
210 | to it, delete the original thread, reset the new thread's | |
211 | ptid, and switch to it. */ | |
4f8d22e3 PA |
212 | |
213 | if (ptid_equal (inferior_ptid, ptid)) | |
214 | { | |
c820c52a | 215 | tp = new_thread (null_ptid); |
dcf4fbde PA |
216 | |
217 | /* Make switch_to_thread not read from the thread. */ | |
30596231 | 218 | tp->state = THREAD_EXITED; |
c820c52a | 219 | switch_to_thread (null_ptid); |
4f8d22e3 PA |
220 | |
221 | /* Now we can delete it. */ | |
222 | delete_thread (ptid); | |
223 | ||
dcf4fbde | 224 | /* Now reset its ptid, and reswitch inferior_ptid to it. */ |
4f8d22e3 | 225 | tp->ptid = ptid; |
30596231 | 226 | tp->state = THREAD_STOPPED; |
4f8d22e3 PA |
227 | switch_to_thread (ptid); |
228 | ||
229 | observer_notify_new_thread (tp); | |
230 | ||
231 | /* All done. */ | |
232 | return tp; | |
233 | } | |
234 | else | |
235 | /* Just go ahead and delete it. */ | |
236 | delete_thread (ptid); | |
237 | } | |
238 | ||
e58b0e63 | 239 | tp = new_thread (ptid); |
cfc01461 VP |
240 | observer_notify_new_thread (tp); |
241 | ||
0d06e24b | 242 | return tp; |
c906108c SS |
243 | } |
244 | ||
93815fbf | 245 | struct thread_info * |
17faa917 | 246 | add_thread_with_info (ptid_t ptid, struct private_thread_info *private) |
93815fbf VP |
247 | { |
248 | struct thread_info *result = add_thread_silent (ptid); | |
249 | ||
17faa917 DJ |
250 | result->private = private; |
251 | ||
93815fbf | 252 | if (print_thread_events) |
fd532e2e | 253 | printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid)); |
d4fc5b1e NR |
254 | |
255 | annotate_new_thread (); | |
93815fbf VP |
256 | return result; |
257 | } | |
258 | ||
17faa917 DJ |
259 | struct thread_info * |
260 | add_thread (ptid_t ptid) | |
261 | { | |
262 | return add_thread_with_info (ptid, NULL); | |
263 | } | |
264 | ||
5e0b29c1 PA |
265 | /* Delete thread PTID. If SILENT, don't notify the observer of this |
266 | exit. */ | |
267 | static void | |
268 | delete_thread_1 (ptid_t ptid, int silent) | |
c906108c SS |
269 | { |
270 | struct thread_info *tp, *tpprev; | |
271 | ||
272 | tpprev = NULL; | |
273 | ||
274 | for (tp = thread_list; tp; tpprev = tp, tp = tp->next) | |
39f77062 | 275 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
276 | break; |
277 | ||
278 | if (!tp) | |
279 | return; | |
280 | ||
4f8d22e3 PA |
281 | /* If this is the current thread, or there's code out there that |
282 | relies on it existing (refcount > 0) we can't delete yet. Mark | |
283 | it as exited, and notify it. */ | |
284 | if (tp->refcount > 0 | |
285 | || ptid_equal (tp->ptid, inferior_ptid)) | |
286 | { | |
30596231 | 287 | if (tp->state != THREAD_EXITED) |
4f8d22e3 | 288 | { |
a07daef3 | 289 | observer_notify_thread_exit (tp, silent); |
4f8d22e3 PA |
290 | |
291 | /* Tag it as exited. */ | |
30596231 | 292 | tp->state = THREAD_EXITED; |
4f8d22e3 PA |
293 | |
294 | /* Clear breakpoints, etc. associated with this thread. */ | |
295 | clear_thread_inferior_resources (tp); | |
296 | } | |
297 | ||
298 | /* Will be really deleted some other time. */ | |
299 | return; | |
300 | } | |
301 | ||
fa4cd53f | 302 | /* Notify thread exit, but only if we haven't already. */ |
30596231 | 303 | if (tp->state != THREAD_EXITED) |
fa4cd53f PA |
304 | observer_notify_thread_exit (tp, silent); |
305 | ||
306 | /* Tag it as exited. */ | |
30596231 | 307 | tp->state = THREAD_EXITED; |
fa4cd53f PA |
308 | clear_thread_inferior_resources (tp); |
309 | ||
c906108c SS |
310 | if (tpprev) |
311 | tpprev->next = tp->next; | |
312 | else | |
313 | thread_list = tp->next; | |
314 | ||
7c952b6d | 315 | free_thread (tp); |
c906108c SS |
316 | } |
317 | ||
4f8d22e3 PA |
318 | /* Delete thread PTID and notify of thread exit. If this is |
319 | inferior_ptid, don't actually delete it, but tag it as exited and | |
320 | do the notification. If PTID is the user selected thread, clear | |
321 | it. */ | |
5e0b29c1 PA |
322 | void |
323 | delete_thread (ptid_t ptid) | |
324 | { | |
325 | delete_thread_1 (ptid, 0 /* not silent */); | |
326 | } | |
327 | ||
328 | void | |
329 | delete_thread_silent (ptid_t ptid) | |
330 | { | |
331 | delete_thread_1 (ptid, 1 /* silent */); | |
332 | } | |
333 | ||
1e92afda | 334 | struct thread_info * |
fba45db2 | 335 | find_thread_id (int num) |
c906108c SS |
336 | { |
337 | struct thread_info *tp; | |
338 | ||
339 | for (tp = thread_list; tp; tp = tp->next) | |
340 | if (tp->num == num) | |
341 | return tp; | |
342 | ||
343 | return NULL; | |
344 | } | |
345 | ||
39f77062 | 346 | /* Find a thread_info by matching PTID. */ |
0d06e24b | 347 | struct thread_info * |
e09875d4 | 348 | find_thread_ptid (ptid_t ptid) |
0d06e24b JM |
349 | { |
350 | struct thread_info *tp; | |
351 | ||
352 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 353 | if (ptid_equal (tp->ptid, ptid)) |
0d06e24b JM |
354 | return tp; |
355 | ||
356 | return NULL; | |
357 | } | |
358 | ||
359 | /* | |
360 | * Thread iterator function. | |
361 | * | |
362 | * Calls a callback function once for each thread, so long as | |
363 | * the callback function returns false. If the callback function | |
364 | * returns true, the iteration will end and the current thread | |
365 | * will be returned. This can be useful for implementing a | |
366 | * search for a thread with arbitrary attributes, or for applying | |
367 | * some operation to every thread. | |
368 | * | |
369 | * FIXME: some of the existing functionality, such as | |
370 | * "Thread apply all", might be rewritten using this functionality. | |
371 | */ | |
372 | ||
373 | struct thread_info * | |
fd118b61 KB |
374 | iterate_over_threads (int (*callback) (struct thread_info *, void *), |
375 | void *data) | |
0d06e24b | 376 | { |
4f8d22e3 | 377 | struct thread_info *tp, *next; |
0d06e24b | 378 | |
4f8d22e3 PA |
379 | for (tp = thread_list; tp; tp = next) |
380 | { | |
381 | next = tp->next; | |
382 | if ((*callback) (tp, data)) | |
383 | return tp; | |
384 | } | |
0d06e24b JM |
385 | |
386 | return NULL; | |
387 | } | |
388 | ||
20874c92 VP |
389 | int |
390 | thread_count (void) | |
391 | { | |
392 | int result = 0; | |
393 | struct thread_info *tp; | |
394 | ||
395 | for (tp = thread_list; tp; tp = tp->next) | |
396 | ++result; | |
397 | ||
398 | return result; | |
399 | } | |
400 | ||
c906108c | 401 | int |
fba45db2 | 402 | valid_thread_id (int num) |
c906108c SS |
403 | { |
404 | struct thread_info *tp; | |
405 | ||
406 | for (tp = thread_list; tp; tp = tp->next) | |
407 | if (tp->num == num) | |
408 | return 1; | |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
413 | int | |
39f77062 | 414 | pid_to_thread_id (ptid_t ptid) |
c906108c SS |
415 | { |
416 | struct thread_info *tp; | |
417 | ||
418 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 419 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
420 | return tp->num; |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
39f77062 | 425 | ptid_t |
fba45db2 | 426 | thread_id_to_pid (int num) |
c906108c SS |
427 | { |
428 | struct thread_info *thread = find_thread_id (num); | |
5d502164 | 429 | |
c906108c | 430 | if (thread) |
39f77062 | 431 | return thread->ptid; |
c906108c | 432 | else |
39f77062 | 433 | return pid_to_ptid (-1); |
c906108c SS |
434 | } |
435 | ||
436 | int | |
39f77062 | 437 | in_thread_list (ptid_t ptid) |
c906108c SS |
438 | { |
439 | struct thread_info *tp; | |
440 | ||
441 | for (tp = thread_list; tp; tp = tp->next) | |
39f77062 | 442 | if (ptid_equal (tp->ptid, ptid)) |
c906108c SS |
443 | return 1; |
444 | ||
c378eb4e | 445 | return 0; /* Never heard of 'im. */ |
c906108c | 446 | } |
8926118c | 447 | |
bad34192 PA |
448 | /* Finds the first thread of the inferior given by PID. If PID is -1, |
449 | return the first thread in the list. */ | |
450 | ||
451 | struct thread_info * | |
452 | first_thread_of_process (int pid) | |
453 | { | |
454 | struct thread_info *tp, *ret = NULL; | |
455 | ||
456 | for (tp = thread_list; tp; tp = tp->next) | |
457 | if (pid == -1 || ptid_get_pid (tp->ptid) == pid) | |
458 | if (ret == NULL || tp->num < ret->num) | |
459 | ret = tp; | |
460 | ||
461 | return ret; | |
462 | } | |
463 | ||
2277426b PA |
464 | struct thread_info * |
465 | any_thread_of_process (int pid) | |
466 | { | |
467 | struct thread_info *tp; | |
468 | ||
469 | for (tp = thread_list; tp; tp = tp->next) | |
470 | if (ptid_get_pid (tp->ptid) == pid) | |
471 | return tp; | |
472 | ||
473 | return NULL; | |
474 | } | |
475 | ||
6c95b8df PA |
476 | struct thread_info * |
477 | any_live_thread_of_process (int pid) | |
478 | { | |
479 | struct thread_info *tp; | |
9941e0c5 | 480 | struct thread_info *tp_executing = NULL; |
6c95b8df PA |
481 | |
482 | for (tp = thread_list; tp; tp = tp->next) | |
30596231 | 483 | if (tp->state != THREAD_EXITED && ptid_get_pid (tp->ptid) == pid) |
6c95b8df | 484 | { |
30596231 | 485 | if (tp->executing) |
9941e0c5 MK |
486 | tp_executing = tp; |
487 | else | |
6c95b8df | 488 | return tp; |
6c95b8df PA |
489 | } |
490 | ||
9941e0c5 | 491 | return tp_executing; |
6c95b8df PA |
492 | } |
493 | ||
8b93c638 | 494 | /* Print a list of thread ids currently known, and the total number of |
c378eb4e | 495 | threads. To be used from within catch_errors. */ |
6949171e JJ |
496 | static int |
497 | do_captured_list_thread_ids (struct ui_out *uiout, void *arg) | |
8b93c638 JM |
498 | { |
499 | struct thread_info *tp; | |
500 | int num = 0; | |
3b31d625 | 501 | struct cleanup *cleanup_chain; |
592375cd | 502 | int current_thread = -1; |
8b93c638 | 503 | |
dc146f7c | 504 | update_thread_list (); |
7990a578 | 505 | |
3b31d625 | 506 | cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids"); |
8b93c638 JM |
507 | |
508 | for (tp = thread_list; tp; tp = tp->next) | |
509 | { | |
30596231 | 510 | if (tp->state == THREAD_EXITED) |
4f8d22e3 | 511 | continue; |
592375cd VP |
512 | |
513 | if (ptid_equal (tp->ptid, inferior_ptid)) | |
514 | current_thread = tp->num; | |
515 | ||
8b93c638 JM |
516 | num++; |
517 | ui_out_field_int (uiout, "thread-id", tp->num); | |
518 | } | |
519 | ||
3b31d625 | 520 | do_cleanups (cleanup_chain); |
592375cd VP |
521 | |
522 | if (current_thread != -1) | |
523 | ui_out_field_int (uiout, "current-thread-id", current_thread); | |
8b93c638 JM |
524 | ui_out_field_int (uiout, "number-of-threads", num); |
525 | return GDB_RC_OK; | |
526 | } | |
527 | ||
528 | /* Official gdblib interface function to get a list of thread ids and | |
c378eb4e | 529 | the total number. */ |
8b93c638 | 530 | enum gdb_rc |
ce43223b | 531 | gdb_list_thread_ids (struct ui_out *uiout, char **error_message) |
8b93c638 | 532 | { |
b0b13bb4 DJ |
533 | if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL, |
534 | error_message, RETURN_MASK_ALL) < 0) | |
535 | return GDB_RC_FAIL; | |
536 | return GDB_RC_OK; | |
8b93c638 | 537 | } |
c906108c | 538 | |
c378eb4e | 539 | /* Return true if TP is an active thread. */ |
c906108c | 540 | static int |
fba45db2 | 541 | thread_alive (struct thread_info *tp) |
c906108c | 542 | { |
30596231 | 543 | if (tp->state == THREAD_EXITED) |
c906108c | 544 | return 0; |
39f77062 | 545 | if (!target_thread_alive (tp->ptid)) |
4f8d22e3 | 546 | return 0; |
c906108c SS |
547 | return 1; |
548 | } | |
549 | ||
550 | static void | |
fba45db2 | 551 | prune_threads (void) |
c906108c | 552 | { |
d4f3574e | 553 | struct thread_info *tp, *next; |
c906108c | 554 | |
c906108c SS |
555 | for (tp = thread_list; tp; tp = next) |
556 | { | |
557 | next = tp->next; | |
558 | if (!thread_alive (tp)) | |
39f77062 | 559 | delete_thread (tp->ptid); |
c906108c SS |
560 | } |
561 | } | |
562 | ||
5231c1fd PA |
563 | void |
564 | thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid) | |
565 | { | |
82f73884 PA |
566 | struct inferior *inf; |
567 | struct thread_info *tp; | |
568 | ||
569 | /* It can happen that what we knew as the target inferior id | |
570 | changes. E.g, target remote may only discover the remote process | |
571 | pid after adding the inferior to GDB's list. */ | |
572 | inf = find_inferior_pid (ptid_get_pid (old_ptid)); | |
573 | inf->pid = ptid_get_pid (new_ptid); | |
574 | ||
e09875d4 | 575 | tp = find_thread_ptid (old_ptid); |
5231c1fd PA |
576 | tp->ptid = new_ptid; |
577 | ||
578 | observer_notify_thread_ptid_changed (old_ptid, new_ptid); | |
579 | } | |
580 | ||
e1ac3328 VP |
581 | void |
582 | set_running (ptid_t ptid, int running) | |
583 | { | |
584 | struct thread_info *tp; | |
d90e17a7 | 585 | int all = ptid_equal (ptid, minus_one_ptid); |
e1ac3328 | 586 | |
e1ac3328 VP |
587 | /* We try not to notify the observer if no thread has actually changed |
588 | the running state -- merely to reduce the number of messages to | |
589 | frontend. Frontend is supposed to handle multiple *running just fine. */ | |
d90e17a7 | 590 | if (all || ptid_is_pid (ptid)) |
e1ac3328 VP |
591 | { |
592 | int any_started = 0; | |
5d502164 | 593 | |
e1ac3328 | 594 | for (tp = thread_list; tp; tp = tp->next) |
d90e17a7 PA |
595 | if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid)) |
596 | { | |
30596231 | 597 | if (tp->state == THREAD_EXITED) |
d90e17a7 | 598 | continue; |
30596231 | 599 | if (running && tp->state == THREAD_STOPPED) |
d90e17a7 | 600 | any_started = 1; |
30596231 | 601 | tp->state = running ? THREAD_RUNNING : THREAD_STOPPED; |
d90e17a7 | 602 | } |
c5a4d20b PA |
603 | if (any_started) |
604 | observer_notify_target_resumed (ptid); | |
e1ac3328 VP |
605 | } |
606 | else | |
607 | { | |
4f8d22e3 | 608 | int started = 0; |
5d502164 | 609 | |
e09875d4 | 610 | tp = find_thread_ptid (ptid); |
e1ac3328 | 611 | gdb_assert (tp); |
30596231 PA |
612 | gdb_assert (tp->state != THREAD_EXITED); |
613 | if (running && tp->state == THREAD_STOPPED) | |
4f8d22e3 | 614 | started = 1; |
30596231 | 615 | tp->state = running ? THREAD_RUNNING : THREAD_STOPPED; |
c5a4d20b | 616 | if (started) |
4f8d22e3 PA |
617 | observer_notify_target_resumed (ptid); |
618 | } | |
e1ac3328 VP |
619 | } |
620 | ||
4f8d22e3 PA |
621 | static int |
622 | is_thread_state (ptid_t ptid, enum thread_state state) | |
e1ac3328 VP |
623 | { |
624 | struct thread_info *tp; | |
625 | ||
e09875d4 | 626 | tp = find_thread_ptid (ptid); |
e1ac3328 | 627 | gdb_assert (tp); |
30596231 | 628 | return tp->state == state; |
4f8d22e3 PA |
629 | } |
630 | ||
631 | int | |
632 | is_stopped (ptid_t ptid) | |
633 | { | |
4f8d22e3 PA |
634 | return is_thread_state (ptid, THREAD_STOPPED); |
635 | } | |
636 | ||
637 | int | |
638 | is_exited (ptid_t ptid) | |
639 | { | |
4f8d22e3 PA |
640 | return is_thread_state (ptid, THREAD_EXITED); |
641 | } | |
642 | ||
643 | int | |
644 | is_running (ptid_t ptid) | |
645 | { | |
4f8d22e3 | 646 | return is_thread_state (ptid, THREAD_RUNNING); |
e1ac3328 VP |
647 | } |
648 | ||
8ea051c5 PA |
649 | int |
650 | is_executing (ptid_t ptid) | |
651 | { | |
652 | struct thread_info *tp; | |
653 | ||
e09875d4 | 654 | tp = find_thread_ptid (ptid); |
8ea051c5 | 655 | gdb_assert (tp); |
30596231 | 656 | return tp->executing; |
8ea051c5 PA |
657 | } |
658 | ||
659 | void | |
660 | set_executing (ptid_t ptid, int executing) | |
661 | { | |
662 | struct thread_info *tp; | |
d90e17a7 | 663 | int all = ptid_equal (ptid, minus_one_ptid); |
8ea051c5 | 664 | |
d90e17a7 | 665 | if (all || ptid_is_pid (ptid)) |
8ea051c5 PA |
666 | { |
667 | for (tp = thread_list; tp; tp = tp->next) | |
d90e17a7 | 668 | if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid)) |
30596231 | 669 | tp->executing = executing; |
8ea051c5 PA |
670 | } |
671 | else | |
672 | { | |
e09875d4 | 673 | tp = find_thread_ptid (ptid); |
8ea051c5 | 674 | gdb_assert (tp); |
30596231 | 675 | tp->executing = executing; |
8ea051c5 PA |
676 | } |
677 | } | |
678 | ||
252fbfc8 PA |
679 | void |
680 | set_stop_requested (ptid_t ptid, int stop) | |
681 | { | |
682 | struct thread_info *tp; | |
683 | int all = ptid_equal (ptid, minus_one_ptid); | |
684 | ||
685 | if (all || ptid_is_pid (ptid)) | |
686 | { | |
687 | for (tp = thread_list; tp; tp = tp->next) | |
688 | if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid)) | |
689 | tp->stop_requested = stop; | |
690 | } | |
691 | else | |
692 | { | |
e09875d4 | 693 | tp = find_thread_ptid (ptid); |
252fbfc8 PA |
694 | gdb_assert (tp); |
695 | tp->stop_requested = stop; | |
696 | } | |
697 | ||
698 | /* Call the stop requested observer so other components of GDB can | |
699 | react to this request. */ | |
700 | if (stop) | |
701 | observer_notify_thread_stop_requested (ptid); | |
702 | } | |
703 | ||
29f49a6a PA |
704 | void |
705 | finish_thread_state (ptid_t ptid) | |
706 | { | |
707 | struct thread_info *tp; | |
708 | int all; | |
709 | int any_started = 0; | |
710 | ||
711 | all = ptid_equal (ptid, minus_one_ptid); | |
712 | ||
713 | if (all || ptid_is_pid (ptid)) | |
714 | { | |
715 | for (tp = thread_list; tp; tp = tp->next) | |
716 | { | |
30596231 | 717 | if (tp->state == THREAD_EXITED) |
29f49a6a PA |
718 | continue; |
719 | if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid)) | |
720 | { | |
30596231 | 721 | if (tp->executing && tp->state == THREAD_STOPPED) |
29f49a6a | 722 | any_started = 1; |
30596231 | 723 | tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED; |
29f49a6a PA |
724 | } |
725 | } | |
726 | } | |
727 | else | |
728 | { | |
e09875d4 | 729 | tp = find_thread_ptid (ptid); |
29f49a6a | 730 | gdb_assert (tp); |
30596231 | 731 | if (tp->state != THREAD_EXITED) |
29f49a6a | 732 | { |
30596231 | 733 | if (tp->executing && tp->state == THREAD_STOPPED) |
29f49a6a | 734 | any_started = 1; |
30596231 | 735 | tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED; |
29f49a6a PA |
736 | } |
737 | } | |
738 | ||
739 | if (any_started) | |
740 | observer_notify_target_resumed (ptid); | |
741 | } | |
742 | ||
743 | void | |
744 | finish_thread_state_cleanup (void *arg) | |
745 | { | |
746 | ptid_t *ptid_p = arg; | |
747 | ||
748 | gdb_assert (arg); | |
749 | ||
750 | finish_thread_state (*ptid_p); | |
751 | } | |
752 | ||
ce4c476a PA |
753 | int |
754 | pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread) | |
755 | { | |
756 | return (pc >= thread->control.step_range_start | |
757 | && pc < thread->control.step_range_end); | |
758 | } | |
759 | ||
8e8901c5 | 760 | /* Prints the list of threads and their details on UIOUT. |
aea5b279 | 761 | This is a version of 'info_threads_command' suitable for |
c378eb4e | 762 | use from MI. |
4f8d22e3 | 763 | If REQUESTED_THREAD is not -1, it's the GDB id of the thread |
8e8901c5 | 764 | that should be printed. Otherwise, all threads are |
c378eb4e | 765 | printed. |
3ee1c036 | 766 | If PID is not -1, only print threads from the process PID. |
c378eb4e | 767 | Otherwise, threads from all attached PIDs are printed. |
3ee1c036 VP |
768 | If both REQUESTED_THREAD and PID are not -1, then the thread |
769 | is printed if it belongs to the specified process. Otherwise, | |
770 | an error is raised. */ | |
8e8901c5 | 771 | void |
aea5b279 | 772 | print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) |
c906108c SS |
773 | { |
774 | struct thread_info *tp; | |
39f77062 | 775 | ptid_t current_ptid; |
99b3d574 | 776 | struct cleanup *old_chain; |
4694da01 | 777 | char *extra_info, *name, *target_id; |
8e8901c5 | 778 | int current_thread = -1; |
c906108c | 779 | |
dc146f7c | 780 | update_thread_list (); |
39f77062 | 781 | current_ptid = inferior_ptid; |
4f8d22e3 PA |
782 | |
783 | /* We'll be switching threads temporarily. */ | |
784 | old_chain = make_cleanup_restore_current_thread (); | |
785 | ||
a7658b96 TT |
786 | /* For backward compatibility, we make a list for MI. A table is |
787 | preferable for the CLI, though, because it shows table | |
788 | headers. */ | |
789 | if (ui_out_is_mi_like_p (uiout)) | |
790 | make_cleanup_ui_out_list_begin_end (uiout, "threads"); | |
791 | else | |
792 | { | |
793 | int n_threads = 0; | |
794 | ||
795 | for (tp = thread_list; tp; tp = tp->next) | |
796 | { | |
aea5b279 | 797 | if (!number_is_in_list (requested_threads, tp->num)) |
a7658b96 TT |
798 | continue; |
799 | ||
dfd4cc63 | 800 | if (pid != -1 && ptid_get_pid (tp->ptid) != pid) |
a7658b96 TT |
801 | continue; |
802 | ||
30596231 | 803 | if (tp->state == THREAD_EXITED) |
a7658b96 TT |
804 | continue; |
805 | ||
806 | ++n_threads; | |
807 | } | |
808 | ||
809 | if (n_threads == 0) | |
810 | { | |
aea5b279 | 811 | if (requested_threads == NULL || *requested_threads == '\0') |
a7658b96 TT |
812 | ui_out_message (uiout, 0, _("No threads.\n")); |
813 | else | |
aea5b279 MS |
814 | ui_out_message (uiout, 0, _("No threads match '%s'.\n"), |
815 | requested_threads); | |
a7658b96 TT |
816 | do_cleanups (old_chain); |
817 | return; | |
818 | } | |
819 | ||
4694da01 | 820 | make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads"); |
a7658b96 TT |
821 | |
822 | ui_out_table_header (uiout, 1, ui_left, "current", ""); | |
823 | ui_out_table_header (uiout, 4, ui_left, "id", "Id"); | |
824 | ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id"); | |
a7658b96 TT |
825 | ui_out_table_header (uiout, 1, ui_left, "frame", "Frame"); |
826 | ui_out_table_body (uiout); | |
827 | } | |
828 | ||
c906108c SS |
829 | for (tp = thread_list; tp; tp = tp->next) |
830 | { | |
8e8901c5 | 831 | struct cleanup *chain2; |
dc146f7c | 832 | int core; |
8e8901c5 | 833 | |
aea5b279 | 834 | if (!number_is_in_list (requested_threads, tp->num)) |
8e8901c5 VP |
835 | continue; |
836 | ||
dfd4cc63 | 837 | if (pid != -1 && ptid_get_pid (tp->ptid) != pid) |
3ee1c036 | 838 | { |
aea5b279 | 839 | if (requested_threads != NULL && *requested_threads != '\0') |
3ee1c036 VP |
840 | error (_("Requested thread not found in requested process")); |
841 | continue; | |
842 | } | |
843 | ||
4f8d22e3 PA |
844 | if (ptid_equal (tp->ptid, current_ptid)) |
845 | current_thread = tp->num; | |
846 | ||
30596231 | 847 | if (tp->state == THREAD_EXITED) |
4f8d22e3 PA |
848 | continue; |
849 | ||
8e8901c5 VP |
850 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
851 | ||
a7658b96 TT |
852 | if (ui_out_is_mi_like_p (uiout)) |
853 | { | |
854 | /* Compatibility. */ | |
855 | if (ptid_equal (tp->ptid, current_ptid)) | |
856 | ui_out_text (uiout, "* "); | |
857 | else | |
858 | ui_out_text (uiout, " "); | |
859 | } | |
c906108c | 860 | else |
a7658b96 TT |
861 | { |
862 | if (ptid_equal (tp->ptid, current_ptid)) | |
863 | ui_out_field_string (uiout, "current", "*"); | |
864 | else | |
865 | ui_out_field_skip (uiout, "current"); | |
866 | } | |
c906108c | 867 | |
8e8901c5 | 868 | ui_out_field_int (uiout, "id", tp->num); |
0d06e24b | 869 | |
4694da01 TT |
870 | /* For the CLI, we stuff everything into the target-id field. |
871 | This is a gross hack to make the output come out looking | |
872 | correct. The underlying problem here is that ui-out has no | |
873 | way to specify that a field's space allocation should be | |
874 | shared by several fields. For MI, we do the right thing | |
875 | instead. */ | |
876 | ||
877 | target_id = target_pid_to_str (tp->ptid); | |
ed406532 | 878 | extra_info = target_extra_thread_info (tp); |
4694da01 TT |
879 | name = tp->name ? tp->name : target_thread_name (tp); |
880 | ||
881 | if (ui_out_is_mi_like_p (uiout)) | |
8e8901c5 | 882 | { |
4694da01 TT |
883 | ui_out_field_string (uiout, "target-id", target_id); |
884 | if (extra_info) | |
885 | ui_out_field_string (uiout, "details", extra_info); | |
886 | if (name) | |
887 | ui_out_field_string (uiout, "name", name); | |
888 | } | |
889 | else | |
890 | { | |
891 | struct cleanup *str_cleanup; | |
892 | char *contents; | |
893 | ||
894 | if (extra_info && name) | |
895 | contents = xstrprintf ("%s \"%s\" (%s)", target_id, | |
896 | name, extra_info); | |
897 | else if (extra_info) | |
898 | contents = xstrprintf ("%s (%s)", target_id, extra_info); | |
899 | else if (name) | |
900 | contents = xstrprintf ("%s \"%s\"", target_id, name); | |
901 | else | |
902 | contents = xstrdup (target_id); | |
903 | str_cleanup = make_cleanup (xfree, contents); | |
904 | ||
905 | ui_out_field_string (uiout, "target-id", contents); | |
906 | do_cleanups (str_cleanup); | |
8e8901c5 | 907 | } |
4f8d22e3 | 908 | |
30596231 | 909 | if (tp->state == THREAD_RUNNING) |
94cc34af PA |
910 | ui_out_text (uiout, "(running)\n"); |
911 | else | |
912 | { | |
913 | /* The switch below puts us at the top of the stack (leaf | |
914 | frame). */ | |
915 | switch_to_thread (tp->ptid); | |
916 | print_stack_frame (get_selected_frame (NULL), | |
917 | /* For MI output, print frame level. */ | |
918 | ui_out_is_mi_like_p (uiout), | |
08d72866 | 919 | LOCATION, 0); |
94cc34af | 920 | } |
8e8901c5 | 921 | |
90139f7d VP |
922 | if (ui_out_is_mi_like_p (uiout)) |
923 | { | |
924 | char *state = "stopped"; | |
5d502164 | 925 | |
30596231 | 926 | if (tp->state == THREAD_RUNNING) |
90139f7d VP |
927 | state = "running"; |
928 | ui_out_field_string (uiout, "state", state); | |
929 | } | |
930 | ||
dc146f7c VP |
931 | core = target_core_of_thread (tp->ptid); |
932 | if (ui_out_is_mi_like_p (uiout) && core != -1) | |
933 | ui_out_field_int (uiout, "core", core); | |
934 | ||
8e8901c5 | 935 | do_cleanups (chain2); |
c906108c SS |
936 | } |
937 | ||
99b3d574 DP |
938 | /* Restores the current thread and the frame selected before |
939 | the "info threads" command. */ | |
940 | do_cleanups (old_chain); | |
c906108c | 941 | |
aea5b279 | 942 | if (pid == -1 && requested_threads == NULL) |
8e8901c5 | 943 | { |
4f8d22e3 | 944 | gdb_assert (current_thread != -1 |
d729566a PA |
945 | || !thread_list |
946 | || ptid_equal (inferior_ptid, null_ptid)); | |
0bcd3e20 | 947 | if (current_thread != -1 && ui_out_is_mi_like_p (uiout)) |
8e8901c5 | 948 | ui_out_field_int (uiout, "current-thread-id", current_thread); |
94cc34af | 949 | |
4f8d22e3 PA |
950 | if (current_thread != -1 && is_exited (current_ptid)) |
951 | ui_out_message (uiout, 0, "\n\ | |
952 | The current thread <Thread ID %d> has terminated. See `help thread'.\n", | |
953 | current_thread); | |
d729566a PA |
954 | else if (thread_list |
955 | && current_thread == -1 | |
956 | && ptid_equal (current_ptid, null_ptid)) | |
957 | ui_out_message (uiout, 0, "\n\ | |
958 | No selected thread. See `help thread'.\n"); | |
c906108c | 959 | } |
c906108c SS |
960 | } |
961 | ||
8e8901c5 VP |
962 | /* Print information about currently known threads |
963 | ||
60f98dde MS |
964 | Optional ARG is a thread id, or list of thread ids. |
965 | ||
966 | Note: this has the drawback that it _really_ switches | |
967 | threads, which frees the frame cache. A no-side | |
968 | effects info-threads command would be nicer. */ | |
8e8901c5 VP |
969 | |
970 | static void | |
971 | info_threads_command (char *arg, int from_tty) | |
972 | { | |
79a45e25 | 973 | print_thread_info (current_uiout, arg, -1); |
8e8901c5 VP |
974 | } |
975 | ||
c378eb4e | 976 | /* Switch from one thread to another. */ |
c906108c | 977 | |
6a6b96b9 | 978 | void |
39f77062 | 979 | switch_to_thread (ptid_t ptid) |
c906108c | 980 | { |
6c95b8df PA |
981 | /* Switch the program space as well, if we can infer it from the now |
982 | current thread. Otherwise, it's up to the caller to select the | |
983 | space it wants. */ | |
984 | if (!ptid_equal (ptid, null_ptid)) | |
985 | { | |
986 | struct inferior *inf; | |
987 | ||
988 | inf = find_inferior_pid (ptid_get_pid (ptid)); | |
989 | gdb_assert (inf != NULL); | |
990 | set_current_program_space (inf->pspace); | |
991 | set_current_inferior (inf); | |
992 | } | |
993 | ||
39f77062 | 994 | if (ptid_equal (ptid, inferior_ptid)) |
c906108c SS |
995 | return; |
996 | ||
39f77062 | 997 | inferior_ptid = ptid; |
35f196d9 | 998 | reinit_frame_cache (); |
94cc34af | 999 | |
4f8d22e3 PA |
1000 | /* We don't check for is_stopped, because we're called at times |
1001 | while in the TARGET_RUNNING state, e.g., while handling an | |
1002 | internal event. */ | |
d729566a PA |
1003 | if (!ptid_equal (inferior_ptid, null_ptid) |
1004 | && !is_exited (ptid) | |
1005 | && !is_executing (ptid)) | |
fb14de7b | 1006 | stop_pc = regcache_read_pc (get_thread_regcache (ptid)); |
94cc34af PA |
1007 | else |
1008 | stop_pc = ~(CORE_ADDR) 0; | |
c906108c SS |
1009 | } |
1010 | ||
1011 | static void | |
39f77062 | 1012 | restore_current_thread (ptid_t ptid) |
c906108c | 1013 | { |
dcf4fbde | 1014 | switch_to_thread (ptid); |
99b3d574 DP |
1015 | } |
1016 | ||
1017 | static void | |
4f8d22e3 | 1018 | restore_selected_frame (struct frame_id a_frame_id, int frame_level) |
99b3d574 | 1019 | { |
4f8d22e3 PA |
1020 | struct frame_info *frame = NULL; |
1021 | int count; | |
1022 | ||
eb8c0621 TT |
1023 | /* This means there was no selected frame. */ |
1024 | if (frame_level == -1) | |
1025 | { | |
1026 | select_frame (NULL); | |
1027 | return; | |
1028 | } | |
1029 | ||
4f8d22e3 PA |
1030 | gdb_assert (frame_level >= 0); |
1031 | ||
1032 | /* Restore by level first, check if the frame id is the same as | |
1033 | expected. If that fails, try restoring by frame id. If that | |
1034 | fails, nothing to do, just warn the user. */ | |
1035 | ||
1036 | count = frame_level; | |
1037 | frame = find_relative_frame (get_current_frame (), &count); | |
1038 | if (count == 0 | |
1039 | && frame != NULL | |
005ca36a JB |
1040 | /* The frame ids must match - either both valid or both outer_frame_id. |
1041 | The latter case is not failsafe, but since it's highly unlikely | |
4f8d22e3 PA |
1042 | the search by level finds the wrong frame, it's 99.9(9)% of |
1043 | the time (for all practical purposes) safe. */ | |
005ca36a | 1044 | && frame_id_eq (get_frame_id (frame), a_frame_id)) |
4f8d22e3 PA |
1045 | { |
1046 | /* Cool, all is fine. */ | |
1047 | select_frame (frame); | |
1048 | return; | |
1049 | } | |
99b3d574 | 1050 | |
4f8d22e3 PA |
1051 | frame = frame_find_by_id (a_frame_id); |
1052 | if (frame != NULL) | |
1053 | { | |
1054 | /* Cool, refound it. */ | |
1055 | select_frame (frame); | |
1056 | return; | |
1057 | } | |
99b3d574 | 1058 | |
0c501536 PA |
1059 | /* Nothing else to do, the frame layout really changed. Select the |
1060 | innermost stack frame. */ | |
1061 | select_frame (get_current_frame ()); | |
1062 | ||
1063 | /* Warn the user. */ | |
79a45e25 | 1064 | if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout)) |
99b3d574 | 1065 | { |
3e43a32a | 1066 | warning (_("Couldn't restore frame #%d in " |
e0162910 | 1067 | "current thread. Bottom (innermost) frame selected:"), |
4f8d22e3 PA |
1068 | frame_level); |
1069 | /* For MI, we should probably have a notification about | |
1070 | current frame change. But this error is not very | |
1071 | likely, so don't bother for now. */ | |
08d72866 | 1072 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
c906108c SS |
1073 | } |
1074 | } | |
1075 | ||
6ecce94d AC |
1076 | struct current_thread_cleanup |
1077 | { | |
39f77062 | 1078 | ptid_t inferior_ptid; |
99b3d574 | 1079 | struct frame_id selected_frame_id; |
4f8d22e3 PA |
1080 | int selected_frame_level; |
1081 | int was_stopped; | |
6c95b8df | 1082 | int inf_id; |
9addecb9 | 1083 | int was_removable; |
6ecce94d AC |
1084 | }; |
1085 | ||
1086 | static void | |
1087 | do_restore_current_thread_cleanup (void *arg) | |
1088 | { | |
4f8d22e3 | 1089 | struct thread_info *tp; |
6ecce94d | 1090 | struct current_thread_cleanup *old = arg; |
d729566a | 1091 | |
e09875d4 | 1092 | tp = find_thread_ptid (old->inferior_ptid); |
d729566a PA |
1093 | |
1094 | /* If the previously selected thread belonged to a process that has | |
1095 | in the mean time been deleted (due to normal exit, detach, etc.), | |
1096 | then don't revert back to it, but instead simply drop back to no | |
1097 | thread selected. */ | |
1098 | if (tp | |
88fc996f | 1099 | && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL) |
d729566a | 1100 | restore_current_thread (old->inferior_ptid); |
88fc996f | 1101 | else |
6c95b8df PA |
1102 | { |
1103 | restore_current_thread (null_ptid); | |
1104 | set_current_inferior (find_inferior_id (old->inf_id)); | |
1105 | } | |
94cc34af | 1106 | |
4f8d22e3 PA |
1107 | /* The running state of the originally selected thread may have |
1108 | changed, so we have to recheck it here. */ | |
d729566a PA |
1109 | if (!ptid_equal (inferior_ptid, null_ptid) |
1110 | && old->was_stopped | |
4f8d22e3 PA |
1111 | && is_stopped (inferior_ptid) |
1112 | && target_has_registers | |
1113 | && target_has_stack | |
1114 | && target_has_memory) | |
1115 | restore_selected_frame (old->selected_frame_id, | |
1116 | old->selected_frame_level); | |
1117 | } | |
1118 | ||
1119 | static void | |
1120 | restore_current_thread_cleanup_dtor (void *arg) | |
1121 | { | |
1122 | struct current_thread_cleanup *old = arg; | |
1123 | struct thread_info *tp; | |
9addecb9 | 1124 | struct inferior *inf; |
5d502164 | 1125 | |
e09875d4 | 1126 | tp = find_thread_ptid (old->inferior_ptid); |
4f8d22e3 PA |
1127 | if (tp) |
1128 | tp->refcount--; | |
9addecb9 TT |
1129 | inf = find_inferior_id (old->inf_id); |
1130 | if (inf != NULL) | |
1131 | inf->removable = old->was_removable; | |
b8c9b27d | 1132 | xfree (old); |
6ecce94d AC |
1133 | } |
1134 | ||
054e8d9e AA |
1135 | /* Set the thread reference count. */ |
1136 | ||
1137 | static void | |
1138 | set_thread_refcount (void *data) | |
1139 | { | |
1140 | int k; | |
1141 | struct thread_array_cleanup *ta_cleanup = data; | |
1142 | ||
1143 | for (k = 0; k != ta_cleanup->count; k++) | |
1144 | ta_cleanup->tp_array[k]->refcount--; | |
1145 | } | |
1146 | ||
6208b47d | 1147 | struct cleanup * |
4f8d22e3 | 1148 | make_cleanup_restore_current_thread (void) |
6ecce94d | 1149 | { |
4f8d22e3 PA |
1150 | struct thread_info *tp; |
1151 | struct frame_info *frame; | |
1152 | struct current_thread_cleanup *old; | |
1153 | ||
1154 | old = xmalloc (sizeof (struct current_thread_cleanup)); | |
39f77062 | 1155 | old->inferior_ptid = inferior_ptid; |
6c95b8df | 1156 | old->inf_id = current_inferior ()->num; |
9addecb9 | 1157 | old->was_removable = current_inferior ()->removable; |
4f8d22e3 | 1158 | |
d729566a PA |
1159 | if (!ptid_equal (inferior_ptid, null_ptid)) |
1160 | { | |
1161 | old->was_stopped = is_stopped (inferior_ptid); | |
1162 | if (old->was_stopped | |
1163 | && target_has_registers | |
1164 | && target_has_stack | |
1165 | && target_has_memory) | |
eb8c0621 TT |
1166 | { |
1167 | /* When processing internal events, there might not be a | |
1168 | selected frame. If we naively call get_selected_frame | |
1169 | here, then we can end up reading debuginfo for the | |
1170 | current frame, but we don't generally need the debuginfo | |
1171 | at this point. */ | |
1172 | frame = get_selected_frame_if_set (); | |
1173 | } | |
d729566a PA |
1174 | else |
1175 | frame = NULL; | |
4f8d22e3 | 1176 | |
d729566a PA |
1177 | old->selected_frame_id = get_frame_id (frame); |
1178 | old->selected_frame_level = frame_relative_level (frame); | |
1179 | ||
e09875d4 | 1180 | tp = find_thread_ptid (inferior_ptid); |
d729566a PA |
1181 | if (tp) |
1182 | tp->refcount++; | |
1183 | } | |
4f8d22e3 | 1184 | |
9addecb9 TT |
1185 | current_inferior ()->removable = 0; |
1186 | ||
4f8d22e3 PA |
1187 | return make_cleanup_dtor (do_restore_current_thread_cleanup, old, |
1188 | restore_current_thread_cleanup_dtor); | |
6ecce94d AC |
1189 | } |
1190 | ||
c906108c SS |
1191 | /* Apply a GDB command to a list of threads. List syntax is a whitespace |
1192 | seperated list of numbers, or ranges, or the keyword `all'. Ranges consist | |
1193 | of two numbers seperated by a hyphen. Examples: | |
1194 | ||
c5aa993b JM |
1195 | thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4 |
1196 | thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9 | |
c378eb4e | 1197 | thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */ |
c906108c SS |
1198 | |
1199 | static void | |
fba45db2 | 1200 | thread_apply_all_command (char *cmd, int from_tty) |
c906108c | 1201 | { |
4f8d22e3 | 1202 | struct cleanup *old_chain; |
e35ce267 | 1203 | char *saved_cmd; |
054e8d9e AA |
1204 | int tc; |
1205 | struct thread_array_cleanup ta_cleanup; | |
c906108c SS |
1206 | |
1207 | if (cmd == NULL || *cmd == '\000') | |
8a3fe4f8 | 1208 | error (_("Please specify a command following the thread ID list")); |
94cc34af | 1209 | |
dc146f7c | 1210 | update_thread_list (); |
e9d196c5 | 1211 | |
4f8d22e3 PA |
1212 | old_chain = make_cleanup_restore_current_thread (); |
1213 | ||
e35ce267 | 1214 | /* Save a copy of the command in case it is clobbered by |
c378eb4e | 1215 | execute_command. */ |
5b616ba1 | 1216 | saved_cmd = xstrdup (cmd); |
94cc34af | 1217 | make_cleanup (xfree, saved_cmd); |
054e8d9e | 1218 | tc = thread_count (); |
94cc34af | 1219 | |
054e8d9e AA |
1220 | if (tc) |
1221 | { | |
1222 | struct thread_info **tp_array; | |
1223 | struct thread_info *tp; | |
1224 | int i = 0, k; | |
1225 | ||
1226 | /* Save a copy of the thread_list in case we execute detach | |
1227 | command. */ | |
1228 | tp_array = xmalloc (sizeof (struct thread_info *) * tc); | |
1229 | make_cleanup (xfree, tp_array); | |
1230 | ta_cleanup.tp_array = tp_array; | |
1231 | ta_cleanup.count = tc; | |
1232 | ||
034f788c | 1233 | ALL_NON_EXITED_THREADS (tp) |
054e8d9e AA |
1234 | { |
1235 | tp_array[i] = tp; | |
1236 | tp->refcount++; | |
1237 | i++; | |
1238 | } | |
1239 | ||
1240 | make_cleanup (set_thread_refcount, &ta_cleanup); | |
1241 | ||
1242 | for (k = 0; k != i; k++) | |
1243 | if (thread_alive (tp_array[k])) | |
1244 | { | |
1245 | switch_to_thread (tp_array[k]->ptid); | |
1246 | printf_filtered (_("\nThread %d (%s):\n"), | |
1247 | tp_array[k]->num, | |
1248 | target_pid_to_str (inferior_ptid)); | |
1249 | execute_command (cmd, from_tty); | |
1250 | ||
1251 | /* Restore exact command used previously. */ | |
1252 | strcpy (cmd, saved_cmd); | |
1253 | } | |
1254 | } | |
6ecce94d AC |
1255 | |
1256 | do_cleanups (old_chain); | |
c906108c SS |
1257 | } |
1258 | ||
1259 | static void | |
fba45db2 | 1260 | thread_apply_command (char *tidlist, int from_tty) |
c906108c SS |
1261 | { |
1262 | char *cmd; | |
c906108c | 1263 | struct cleanup *old_chain; |
e35ce267 | 1264 | char *saved_cmd; |
197f0a60 | 1265 | struct get_number_or_range_state state; |
c906108c SS |
1266 | |
1267 | if (tidlist == NULL || *tidlist == '\000') | |
8a3fe4f8 | 1268 | error (_("Please specify a thread ID list")); |
c906108c | 1269 | |
c5aa993b | 1270 | for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++); |
c906108c SS |
1271 | |
1272 | if (*cmd == '\000') | |
8a3fe4f8 | 1273 | error (_("Please specify a command following the thread ID list")); |
c906108c | 1274 | |
e35ce267 | 1275 | /* Save a copy of the command in case it is clobbered by |
c378eb4e | 1276 | execute_command. */ |
5b616ba1 | 1277 | saved_cmd = xstrdup (cmd); |
4f8d22e3 | 1278 | old_chain = make_cleanup (xfree, saved_cmd); |
197f0a60 TT |
1279 | |
1280 | init_number_or_range (&state, tidlist); | |
1281 | while (!state.finished && state.string < cmd) | |
c906108c SS |
1282 | { |
1283 | struct thread_info *tp; | |
65ebfb52 | 1284 | int start; |
c906108c | 1285 | |
197f0a60 | 1286 | start = get_number_or_range (&state); |
c906108c | 1287 | |
65fc9b77 PA |
1288 | make_cleanup_restore_current_thread (); |
1289 | ||
65ebfb52 | 1290 | tp = find_thread_id (start); |
c906108c | 1291 | |
65ebfb52 MS |
1292 | if (!tp) |
1293 | warning (_("Unknown thread %d."), start); | |
1294 | else if (!thread_alive (tp)) | |
1295 | warning (_("Thread %d has terminated."), start); | |
1296 | else | |
1297 | { | |
1298 | switch_to_thread (tp->ptid); | |
4f8d22e3 | 1299 | |
65ebfb52 MS |
1300 | printf_filtered (_("\nThread %d (%s):\n"), tp->num, |
1301 | target_pid_to_str (inferior_ptid)); | |
1302 | execute_command (cmd, from_tty); | |
4f8d22e3 | 1303 | |
65ebfb52 MS |
1304 | /* Restore exact command used previously. */ |
1305 | strcpy (cmd, saved_cmd); | |
c906108c SS |
1306 | } |
1307 | } | |
6ecce94d AC |
1308 | |
1309 | do_cleanups (old_chain); | |
c906108c SS |
1310 | } |
1311 | ||
1312 | /* Switch to the specified thread. Will dispatch off to thread_apply_command | |
1313 | if prefix of arg is `apply'. */ | |
1314 | ||
1315 | static void | |
fba45db2 | 1316 | thread_command (char *tidstr, int from_tty) |
c906108c | 1317 | { |
c906108c SS |
1318 | if (!tidstr) |
1319 | { | |
d729566a PA |
1320 | if (ptid_equal (inferior_ptid, null_ptid)) |
1321 | error (_("No thread selected")); | |
1322 | ||
c906108c | 1323 | if (target_has_stack) |
4f8d22e3 PA |
1324 | { |
1325 | if (is_exited (inferior_ptid)) | |
1326 | printf_filtered (_("[Current thread is %d (%s) (exited)]\n"), | |
1327 | pid_to_thread_id (inferior_ptid), | |
54ba13f7 | 1328 | target_pid_to_str (inferior_ptid)); |
4f8d22e3 PA |
1329 | else |
1330 | printf_filtered (_("[Current thread is %d (%s)]\n"), | |
1331 | pid_to_thread_id (inferior_ptid), | |
54ba13f7 | 1332 | target_pid_to_str (inferior_ptid)); |
4f8d22e3 | 1333 | } |
c906108c | 1334 | else |
8a3fe4f8 | 1335 | error (_("No stack.")); |
c906108c SS |
1336 | return; |
1337 | } | |
c5394b80 | 1338 | |
79a45e25 | 1339 | gdb_thread_select (current_uiout, tidstr, NULL); |
c5394b80 JM |
1340 | } |
1341 | ||
4694da01 TT |
1342 | /* Implementation of `thread name'. */ |
1343 | ||
1344 | static void | |
1345 | thread_name_command (char *arg, int from_tty) | |
1346 | { | |
1347 | struct thread_info *info; | |
1348 | ||
1349 | if (ptid_equal (inferior_ptid, null_ptid)) | |
1350 | error (_("No thread selected")); | |
1351 | ||
529480d0 | 1352 | arg = skip_spaces (arg); |
4694da01 TT |
1353 | |
1354 | info = inferior_thread (); | |
1355 | xfree (info->name); | |
1356 | info->name = arg ? xstrdup (arg) : NULL; | |
1357 | } | |
1358 | ||
60f98dde MS |
1359 | /* Find thread ids with a name, target pid, or extra info matching ARG. */ |
1360 | ||
1361 | static void | |
1362 | thread_find_command (char *arg, int from_tty) | |
1363 | { | |
1364 | struct thread_info *tp; | |
1365 | char *tmp; | |
1366 | unsigned long match = 0; | |
1367 | ||
1368 | if (arg == NULL || *arg == '\0') | |
1369 | error (_("Command requires an argument.")); | |
1370 | ||
1371 | tmp = re_comp (arg); | |
1372 | if (tmp != 0) | |
1373 | error (_("Invalid regexp (%s): %s"), tmp, arg); | |
1374 | ||
1375 | update_thread_list (); | |
1376 | for (tp = thread_list; tp; tp = tp->next) | |
1377 | { | |
1378 | if (tp->name != NULL && re_exec (tp->name)) | |
1379 | { | |
1380 | printf_filtered (_("Thread %d has name '%s'\n"), | |
1381 | tp->num, tp->name); | |
1382 | match++; | |
1383 | } | |
1384 | ||
1385 | tmp = target_thread_name (tp); | |
1386 | if (tmp != NULL && re_exec (tmp)) | |
1387 | { | |
1388 | printf_filtered (_("Thread %d has target name '%s'\n"), | |
1389 | tp->num, tmp); | |
1390 | match++; | |
1391 | } | |
1392 | ||
1393 | tmp = target_pid_to_str (tp->ptid); | |
1394 | if (tmp != NULL && re_exec (tmp)) | |
1395 | { | |
1396 | printf_filtered (_("Thread %d has target id '%s'\n"), | |
1397 | tp->num, tmp); | |
1398 | match++; | |
1399 | } | |
1400 | ||
1401 | tmp = target_extra_thread_info (tp); | |
1402 | if (tmp != NULL && re_exec (tmp)) | |
1403 | { | |
1404 | printf_filtered (_("Thread %d has extra info '%s'\n"), | |
1405 | tp->num, tmp); | |
1406 | match++; | |
1407 | } | |
1408 | } | |
1409 | if (!match) | |
1410 | printf_filtered (_("No threads match '%s'\n"), arg); | |
1411 | } | |
1412 | ||
93815fbf VP |
1413 | /* Print notices when new threads are attached and detached. */ |
1414 | int print_thread_events = 1; | |
1415 | static void | |
1416 | show_print_thread_events (struct ui_file *file, int from_tty, | |
1417 | struct cmd_list_element *c, const char *value) | |
1418 | { | |
3e43a32a MS |
1419 | fprintf_filtered (file, |
1420 | _("Printing of thread events is %s.\n"), | |
93815fbf VP |
1421 | value); |
1422 | } | |
1423 | ||
c5394b80 | 1424 | static int |
6949171e | 1425 | do_captured_thread_select (struct ui_out *uiout, void *tidstr) |
c5394b80 JM |
1426 | { |
1427 | int num; | |
1428 | struct thread_info *tp; | |
1429 | ||
81490ea1 | 1430 | num = value_as_long (parse_and_eval (tidstr)); |
c906108c SS |
1431 | |
1432 | tp = find_thread_id (num); | |
1433 | ||
8b93c638 | 1434 | if (!tp) |
8a3fe4f8 | 1435 | error (_("Thread ID %d not known."), num); |
c906108c SS |
1436 | |
1437 | if (!thread_alive (tp)) | |
8a3fe4f8 | 1438 | error (_("Thread ID %d has terminated."), num); |
c906108c | 1439 | |
dcf4fbde | 1440 | switch_to_thread (tp->ptid); |
c906108c | 1441 | |
db5a7484 NR |
1442 | annotate_thread_changed (); |
1443 | ||
8b93c638 | 1444 | ui_out_text (uiout, "[Switching to thread "); |
39f77062 | 1445 | ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid)); |
8b93c638 | 1446 | ui_out_text (uiout, " ("); |
54ba13f7 | 1447 | ui_out_text (uiout, target_pid_to_str (inferior_ptid)); |
8b93c638 | 1448 | ui_out_text (uiout, ")]"); |
c5394b80 | 1449 | |
4f8d22e3 PA |
1450 | /* Note that we can't reach this with an exited thread, due to the |
1451 | thread_alive check above. */ | |
30596231 | 1452 | if (tp->state == THREAD_RUNNING) |
94cc34af | 1453 | ui_out_text (uiout, "(running)\n"); |
4f8d22e3 | 1454 | else |
98871305 TT |
1455 | { |
1456 | ui_out_text (uiout, "\n"); | |
08d72866 | 1457 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
98871305 | 1458 | } |
4f8d22e3 PA |
1459 | |
1460 | /* Since the current thread may have changed, see if there is any | |
1461 | exited thread we can now delete. */ | |
1462 | prune_threads (); | |
94cc34af | 1463 | |
c5394b80 JM |
1464 | return GDB_RC_OK; |
1465 | } | |
1466 | ||
1467 | enum gdb_rc | |
ce43223b | 1468 | gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message) |
c5394b80 | 1469 | { |
b0b13bb4 DJ |
1470 | if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr, |
1471 | error_message, RETURN_MASK_ALL) < 0) | |
1472 | return GDB_RC_FAIL; | |
1473 | return GDB_RC_OK; | |
c906108c SS |
1474 | } |
1475 | ||
dc146f7c VP |
1476 | void |
1477 | update_thread_list (void) | |
1478 | { | |
1479 | prune_threads (); | |
1480 | target_find_new_threads (); | |
1481 | } | |
1482 | ||
6aed2dbc SS |
1483 | /* Return a new value for the selected thread's id. Return a value of 0 if |
1484 | no thread is selected, or no threads exist. */ | |
1485 | ||
1486 | static struct value * | |
22d2b532 SDJ |
1487 | thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, |
1488 | void *ignore) | |
6aed2dbc SS |
1489 | { |
1490 | struct thread_info *tp = find_thread_ptid (inferior_ptid); | |
1491 | ||
1492 | return value_from_longest (builtin_type (gdbarch)->builtin_int, | |
1493 | (tp ? tp->num : 0)); | |
1494 | } | |
1495 | ||
c906108c SS |
1496 | /* Commands with a prefix of `thread'. */ |
1497 | struct cmd_list_element *thread_cmd_list = NULL; | |
1498 | ||
22d2b532 SDJ |
1499 | /* Implementation of `thread' variable. */ |
1500 | ||
1501 | static const struct internalvar_funcs thread_funcs = | |
1502 | { | |
1503 | thread_id_make_value, | |
1504 | NULL, | |
1505 | NULL | |
1506 | }; | |
1507 | ||
c906108c | 1508 | void |
fba45db2 | 1509 | _initialize_thread (void) |
c906108c SS |
1510 | { |
1511 | static struct cmd_list_element *thread_apply_list = NULL; | |
c906108c | 1512 | |
60f98dde MS |
1513 | add_info ("threads", info_threads_command, |
1514 | _("Display currently known threads.\n\ | |
1515 | Usage: info threads [ID]...\n\ | |
1516 | Optional arguments are thread IDs with spaces between.\n\ | |
1517 | If no arguments, all threads are displayed.")); | |
c906108c | 1518 | |
d729566a | 1519 | add_prefix_cmd ("thread", class_run, thread_command, _("\ |
1bedd215 AC |
1520 | Use this command to switch between threads.\n\ |
1521 | The new thread ID must be currently known."), | |
d729566a | 1522 | &thread_cmd_list, "thread ", 1, &cmdlist); |
c906108c | 1523 | |
d729566a PA |
1524 | add_prefix_cmd ("apply", class_run, thread_apply_command, |
1525 | _("Apply a command to a list of threads."), | |
1526 | &thread_apply_list, "thread apply ", 1, &thread_cmd_list); | |
c906108c | 1527 | |
d729566a PA |
1528 | add_cmd ("all", class_run, thread_apply_all_command, |
1529 | _("Apply a command to all threads."), &thread_apply_list); | |
c906108c | 1530 | |
4694da01 TT |
1531 | add_cmd ("name", class_run, thread_name_command, |
1532 | _("Set the current thread's name.\n\ | |
1533 | Usage: thread name [NAME]\n\ | |
1534 | If NAME is not given, then any existing name is removed."), &thread_cmd_list); | |
1535 | ||
60f98dde MS |
1536 | add_cmd ("find", class_run, thread_find_command, _("\ |
1537 | Find threads that match a regular expression.\n\ | |
1538 | Usage: thread find REGEXP\n\ | |
1539 | Will display thread ids whose name, target ID, or extra info matches REGEXP."), | |
1540 | &thread_cmd_list); | |
1541 | ||
c906108c SS |
1542 | if (!xdb_commands) |
1543 | add_com_alias ("t", "thread", class_run, 1); | |
93815fbf VP |
1544 | |
1545 | add_setshow_boolean_cmd ("thread-events", no_class, | |
1546 | &print_thread_events, _("\ | |
11c68c47 EZ |
1547 | Set printing of thread events (such as thread start and exit)."), _("\ |
1548 | Show printing of thread events (such as thread start and exit)."), NULL, | |
93815fbf VP |
1549 | NULL, |
1550 | show_print_thread_events, | |
1551 | &setprintlist, &showprintlist); | |
6aed2dbc | 1552 | |
22d2b532 | 1553 | create_internalvar_type_lazy ("_thread", &thread_funcs, NULL); |
c906108c | 1554 | } |