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"
25 #include "continuations.h"
29 struct continuation *next;
30 continuation_ftype *function;
31 continuation_free_arg_ftype *free_arg;
35 /* Add a new continuation to the continuation chain. Args are
36 FUNCTION to run the continuation up with, and ARG to pass to
40 make_continuation (struct continuation **pmy_chain,
41 continuation_ftype *function,
42 void *arg, void (*free_arg) (void *))
44 struct continuation *new = XNEW (struct continuation);
46 new->next = *pmy_chain;
47 new->function = function;
48 new->free_arg = free_arg;
54 do_my_continuations_1 (struct continuation **pmy_chain)
56 struct continuation *ptr;
58 while ((ptr = *pmy_chain) != NULL)
60 *pmy_chain = ptr->next; /* Do this first in case of recursion. */
61 (*ptr->function) (ptr->arg);
63 (*ptr->free_arg) (ptr->arg);
69 do_my_continuations (struct continuation **list)
71 struct continuation *continuations;
76 /* Copy the list header into another pointer, and set the global
77 list header to null, so that the global list can change as a side
78 effect of invoking the continuations and the processing of the
79 preexisting continuations will not be affected. */
81 continuations = *list;
84 /* Work now on the list we have set aside. */
85 do_my_continuations_1 (&continuations);
89 discard_my_continuations_1 (struct continuation **pmy_chain)
91 struct continuation *ptr;
93 while ((ptr = *pmy_chain) != NULL)
95 *pmy_chain = ptr->next;
97 (*ptr->free_arg) (ptr->arg);
103 discard_my_continuations (struct continuation **list)
105 struct continuation *continuation_ptr = *list;
107 discard_my_continuations_1 (list);
111 /* Add a continuation to the continuation list of INFERIOR. The new
112 continuation will be added at the front. */
115 add_inferior_continuation (continuation_ftype *hook, void *args,
116 continuation_free_arg_ftype *free_arg)
118 struct inferior *inf = current_inferior ();
120 make_continuation (&inf->continuations, hook, args, free_arg);
123 /* Do all continuations of the current inferior. */
126 do_all_inferior_continuations (void)
128 struct inferior *inf = current_inferior ();
129 do_my_continuations (&inf->continuations);
132 /* Get rid of all the inferior-wide continuations of INF. */
135 discard_all_inferior_continuations (struct inferior *inf)
137 discard_my_continuations (&inf->continuations);
140 /* Add a continuation to the continuation list of THREAD. The new
141 continuation will be added at the front. */
144 add_continuation (struct thread_info *thread,
145 continuation_ftype *hook, void *args,
146 continuation_free_arg_ftype *free_arg)
148 make_continuation (&thread->continuations, hook, args, free_arg);
152 restore_thread_cleanup (void *arg)
154 ptid_t *ptid_p = arg;
156 switch_to_thread (*ptid_p);
159 /* Walk down the continuation list of PTID, and execute all the
160 continuations. There is a problem though. In some cases new
161 continuations may be added while we are in the middle of this loop.
162 If this happens they will be added in the front, and done before we
163 have a chance of exhausting those that were already there. We need
164 to then save the beginning of the list in a pointer and do the
165 continuations from there on, instead of using the global beginning
166 of list as our iteration pointer. */
169 do_all_continuations_ptid (ptid_t ptid,
170 struct continuation **continuations_p)
172 struct cleanup *old_chain;
173 ptid_t current_thread;
175 if (*continuations_p == NULL)
178 current_thread = inferior_ptid;
180 /* Restore selected thread on exit. Don't try to restore the frame
183 - When running continuations, the selected frame is always #0.
185 - The continuations may trigger symbol file loads, which may
186 change the frame layout (frame ids change), which would trigger
187 a warning if we used make_cleanup_restore_current_thread. */
189 old_chain = make_cleanup (restore_thread_cleanup, ¤t_thread);
191 /* Let the continuation see this thread as selected. */
192 switch_to_thread (ptid);
194 do_my_continuations (continuations_p);
196 do_cleanups (old_chain);
199 /* Callback for iterate over threads. */
202 do_all_continuations_thread_callback (struct thread_info *thread, void *data)
204 do_all_continuations_ptid (thread->ptid, &thread->continuations);
208 /* Do all continuations of thread THREAD. */
211 do_all_continuations_thread (struct thread_info *thread)
213 do_all_continuations_thread_callback (thread, NULL);
216 /* Do all continuations of all threads. */
219 do_all_continuations (void)
221 iterate_over_threads (do_all_continuations_thread_callback, NULL);
224 /* Callback for iterate over threads. */
227 discard_all_continuations_thread_callback (struct thread_info *thread,
230 discard_my_continuations (&thread->continuations);
234 /* Get rid of all the continuations of THREAD. */
237 discard_all_continuations_thread (struct thread_info *thread)
239 discard_all_continuations_thread_callback (thread, NULL);
242 /* Get rid of all the continuations of all threads. */
245 discard_all_continuations (void)
247 iterate_over_threads (discard_all_continuations_thread_callback, NULL);
251 /* Add a continuation to the intermediate continuation list of THREAD.
252 The new continuation will be added at the front. */
255 add_intermediate_continuation (struct thread_info *thread,
256 continuation_ftype *hook,
258 continuation_free_arg_ftype *free_arg)
260 make_continuation (&thread->intermediate_continuations, hook,
264 /* Walk down the cmd_continuation list, and execute all the
265 continuations. There is a problem though. In some cases new
266 continuations may be added while we are in the middle of this
267 loop. If this happens they will be added in the front, and done
268 before we have a chance of exhausting those that were already
269 there. We need to then save the beginning of the list in a pointer
270 and do the continuations from there on, instead of using the
271 global beginning of list as our iteration pointer. */
274 do_all_intermediate_continuations_thread_callback (struct thread_info *thread,
277 do_all_continuations_ptid (thread->ptid,
278 &thread->intermediate_continuations);
282 /* Do all intermediate continuations of thread THREAD. */
285 do_all_intermediate_continuations_thread (struct thread_info *thread)
287 do_all_intermediate_continuations_thread_callback (thread, NULL);
290 /* Do all intermediate continuations of all threads. */
293 do_all_intermediate_continuations (void)
295 iterate_over_threads (do_all_intermediate_continuations_thread_callback,
299 /* Callback for iterate over threads. */
302 discard_all_intermediate_continuations_thread_callback (struct thread_info *thread,
305 discard_my_continuations (&thread->intermediate_continuations);
309 /* Get rid of all the intermediate continuations of THREAD. */
312 discard_all_intermediate_continuations_thread (struct thread_info *thread)
314 discard_all_intermediate_continuations_thread_callback (thread, NULL);
317 /* Get rid of all the intermediate continuations of all threads. */
320 discard_all_intermediate_continuations (void)
322 iterate_over_threads (discard_all_intermediate_continuations_thread_callback,