1 /* Continuations for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010, 2011 Free Software Foundation, Inc.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbthread.h"
28 struct continuation *next;
29 void (*function) (void *);
30 void (*free_arg) (void *);
34 typedef void (make_continuation_ftype) (void *);
36 /* Add a new continuation to the continuation chain. Args are
37 FUNCTION to run the continuation up with, and ARG to pass to
41 make_continuation (struct continuation **pmy_chain,
42 make_continuation_ftype *function,
43 void *arg, void (*free_arg) (void *))
45 struct continuation *new = XNEW (struct continuation);
47 new->next = *pmy_chain;
48 new->function = function;
49 new->free_arg = free_arg;
55 do_my_continuations_1 (struct continuation **pmy_chain)
57 struct continuation *ptr;
59 while ((ptr = *pmy_chain) != NULL)
61 *pmy_chain = ptr->next; /* Do this first in case of recursion. */
62 (*ptr->function) (ptr->arg);
64 (*ptr->free_arg) (ptr->arg);
70 do_my_continuations (struct continuation **list)
72 struct continuation *continuations;
77 /* Copy the list header into another pointer, and set the global
78 list header to null, so that the global list can change as a side
79 effect of invoking the continuations and the processing of the
80 preexisting continuations will not be affected. */
82 continuations = *list;
85 /* Work now on the list we have set aside. */
86 do_my_continuations_1 (&continuations);
90 discard_my_continuations_1 (struct continuation **pmy_chain)
92 struct continuation *ptr;
94 while ((ptr = *pmy_chain) != NULL)
96 *pmy_chain = ptr->next;
98 (*ptr->free_arg) (ptr->arg);
104 discard_my_continuations (struct continuation **list)
106 struct continuation *continuation_ptr = *list;
108 discard_my_continuations_1 (list);
112 /* Add a continuation to the continuation list of INFERIOR. The new
113 continuation will be added at the front. */
116 add_inferior_continuation (void (*continuation_hook) (void *), void *args,
117 void (*continuation_free_args) (void *))
119 struct inferior *inf = current_inferior ();
121 make_continuation (&inf->continuations, continuation_hook,
122 args, continuation_free_args);
125 /* Do all continuations of the current inferior. */
128 do_all_inferior_continuations (void)
130 struct inferior *inf = current_inferior ();
131 do_my_continuations (&inf->continuations);
134 /* Get rid of all the inferior-wide continuations of INF. */
137 discard_all_inferior_continuations (struct inferior *inf)
139 discard_my_continuations (&inf->continuations);
142 /* Add a continuation to the continuation list of THREAD. The new
143 continuation will be added at the front. */
146 add_continuation (struct thread_info *thread,
147 void (*continuation_hook) (void *), void *args,
148 void (*continuation_free_args) (void *))
150 make_continuation (&thread->continuations, continuation_hook,
151 args, continuation_free_args);
155 restore_thread_cleanup (void *arg)
157 ptid_t *ptid_p = arg;
159 switch_to_thread (*ptid_p);
162 /* Walk down the continuation list of PTID, and execute all the
163 continuations. There is a problem though. In some cases new
164 continuations may be added while we are in the middle of this loop.
165 If this happens they will be added in the front, and done before we
166 have a chance of exhausting those that were already there. We need
167 to then save the beginning of the list in a pointer and do the
168 continuations from there on, instead of using the global beginning
169 of list as our iteration pointer. */
172 do_all_continuations_ptid (ptid_t ptid,
173 struct continuation **continuations_p)
175 struct cleanup *old_chain;
176 ptid_t current_thread;
178 if (*continuations_p == NULL)
181 current_thread = inferior_ptid;
183 /* Restore selected thread on exit. Don't try to restore the frame
186 - When running continuations, the selected frame is always #0.
188 - The continuations may trigger symbol file loads, which may
189 change the frame layout (frame ids change), which would trigger
190 a warning if we used make_cleanup_restore_current_thread. */
192 old_chain = make_cleanup (restore_thread_cleanup, ¤t_thread);
194 /* Let the continuation see this thread as selected. */
195 switch_to_thread (ptid);
197 do_my_continuations (continuations_p);
199 do_cleanups (old_chain);
202 /* Callback for iterate over threads. */
205 do_all_continuations_thread_callback (struct thread_info *thread, void *data)
207 do_all_continuations_ptid (thread->ptid, &thread->continuations);
211 /* Do all continuations of thread THREAD. */
214 do_all_continuations_thread (struct thread_info *thread)
216 do_all_continuations_thread_callback (thread, NULL);
219 /* Do all continuations of all threads. */
222 do_all_continuations (void)
224 iterate_over_threads (do_all_continuations_thread_callback, NULL);
227 /* Callback for iterate over threads. */
230 discard_all_continuations_thread_callback (struct thread_info *thread,
233 discard_my_continuations (&thread->continuations);
237 /* Get rid of all the continuations of THREAD. */
240 discard_all_continuations_thread (struct thread_info *thread)
242 discard_all_continuations_thread_callback (thread, NULL);
245 /* Get rid of all the continuations of all threads. */
248 discard_all_continuations (void)
250 iterate_over_threads (discard_all_continuations_thread_callback, NULL);
254 /* Add a continuation to the intermediate continuation list of THREAD.
255 The new continuation will be added at the front. */
258 add_intermediate_continuation (struct thread_info *thread,
259 void (*continuation_hook)
260 (void *), void *args,
261 void (*continuation_free_args) (void *))
263 make_continuation (&thread->intermediate_continuations, continuation_hook,
264 args, continuation_free_args);
267 /* Walk down the cmd_continuation list, and execute all the
268 continuations. There is a problem though. In some cases new
269 continuations may be added while we are in the middle of this
270 loop. If this happens they will be added in the front, and done
271 before we have a chance of exhausting those that were already
272 there. We need to then save the beginning of the list in a pointer
273 and do the continuations from there on, instead of using the
274 global beginning of list as our iteration pointer. */
277 do_all_intermediate_continuations_thread_callback (struct thread_info *thread,
280 do_all_continuations_ptid (thread->ptid,
281 &thread->intermediate_continuations);
285 /* Do all intermediate continuations of thread THREAD. */
288 do_all_intermediate_continuations_thread (struct thread_info *thread)
290 do_all_intermediate_continuations_thread_callback (thread, NULL);
293 /* Do all intermediate continuations of all threads. */
296 do_all_intermediate_continuations (void)
298 iterate_over_threads (do_all_intermediate_continuations_thread_callback,
302 /* Callback for iterate over threads. */
305 discard_all_intermediate_continuations_thread_callback (struct thread_info *thread,
308 discard_my_continuations (&thread->intermediate_continuations);
312 /* Get rid of all the intermediate continuations of THREAD. */
315 discard_all_intermediate_continuations_thread (struct thread_info *thread)
317 discard_all_intermediate_continuations_thread_callback (thread, NULL);
320 /* Get rid of all the intermediate continuations of all threads. */
323 discard_all_intermediate_continuations (void)
325 iterate_over_threads (discard_all_intermediate_continuations_thread_callback,