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, and return the
37 previous chain pointer to be passed later to do_continuations or
38 discard_continuations. Args are FUNCTION to run the continuation
39 up with, and ARG to pass to it. */
41 static struct continuation
*
42 make_continuation (struct continuation
**pmy_chain
,
43 make_continuation_ftype
*function
,
44 void *arg
, void (*free_arg
) (void *))
46 struct continuation
*new = XNEW (struct continuation
);
47 struct continuation
*old_chain
= *pmy_chain
;
49 new->next
= *pmy_chain
;
50 new->function
= function
;
51 new->free_arg
= free_arg
;
59 do_my_continuations (struct continuation
**pmy_chain
,
60 struct continuation
*old_chain
)
62 struct continuation
*ptr
;
64 while ((ptr
= *pmy_chain
) != old_chain
)
66 *pmy_chain
= ptr
->next
; /* Do this first in case of recursion. */
67 (*ptr
->function
) (ptr
->arg
);
69 (*ptr
->free_arg
) (ptr
->arg
);
75 discard_my_continuations (struct continuation
**pmy_chain
,
76 struct continuation
*old_chain
)
78 struct continuation
*ptr
;
80 while ((ptr
= *pmy_chain
) != old_chain
)
82 *pmy_chain
= ptr
->next
;
84 (*ptr
->free_arg
) (ptr
->arg
);
89 /* Add a continuation to the continuation list of THREAD. The new
90 continuation will be added at the front. */
93 add_continuation (struct thread_info
*thread
,
94 void (*continuation_hook
) (void *), void *args
,
95 void (*continuation_free_args
) (void *))
97 struct continuation
*continuations
= thread
->continuations
;
98 make_cleanup_ftype
*continuation_hook_fn
= continuation_hook
;
100 make_continuation (&continuations
,
101 continuation_hook_fn
,
103 continuation_free_args
);
105 thread
->continuations
= continuations
;
108 /* Add a continuation to the continuation list of INFERIOR. The new
109 continuation will be added at the front. */
112 add_inferior_continuation (void (*continuation_hook
) (void *), void *args
,
113 void (*continuation_free_args
) (void *))
115 struct inferior
*inf
= current_inferior ();
116 struct continuation
*continuations
= inf
->continuations
;
117 make_cleanup_ftype
*continuation_hook_fn
= continuation_hook
;
119 make_continuation (&continuations
,
120 continuation_hook_fn
,
122 continuation_free_args
);
124 inf
->continuations
= continuations
;
127 /* Do all continuations of the current inferior. */
130 do_all_inferior_continuations (void)
132 struct continuation
*continuations
;
133 struct inferior
*inf
= current_inferior ();
135 if (inf
->continuations
== NULL
)
138 /* Copy the list header into another pointer, and set the global
139 list header to null, so that the global list can change as a side
140 effect of invoking the continuations and the processing of the
141 preexisting continuations will not be affected. */
143 continuations
= inf
->continuations
;
144 inf
->continuations
= NULL
;
146 /* Work now on the list we have set aside. */
147 do_my_continuations (&continuations
, NULL
);
150 /* Get rid of all the inferior-wide continuations of INF. */
153 discard_all_inferior_continuations (struct inferior
*inf
)
155 struct continuation
*continuation_ptr
= inf
->continuations
;
157 discard_my_continuations (&continuation_ptr
, NULL
);
158 inf
->continuations
= NULL
;
162 restore_thread_cleanup (void *arg
)
164 ptid_t
*ptid_p
= arg
;
166 switch_to_thread (*ptid_p
);
169 /* Walk down the continuation list of PTID, and execute all the
170 continuations. There is a problem though. In some cases new
171 continuations may be added while we are in the middle of this loop.
172 If this happens they will be added in the front, and done before we
173 have a chance of exhausting those that were already there. We need
174 to then save the beginning of the list in a pointer and do the
175 continuations from there on, instead of using the global beginning
176 of list as our iteration pointer. */
179 do_all_continuations_ptid (ptid_t ptid
,
180 struct continuation
**continuations_p
)
182 struct cleanup
*old_chain
;
183 struct continuation
*continuations
;
184 ptid_t current_thread
;
186 if (*continuations_p
== NULL
)
189 current_thread
= inferior_ptid
;
191 /* Restore selected thread on exit. Don't try to restore the frame
194 - When running continuations, the selected frame is always #0.
196 - The continuations may trigger symbol file loads, which may
197 change the frame layout (frame ids change), which would trigger
198 a warning if we used make_cleanup_restore_current_thread. */
200 old_chain
= make_cleanup (restore_thread_cleanup
, ¤t_thread
);
202 /* Let the continuation see this thread as selected. */
203 switch_to_thread (ptid
);
205 /* Copy the list header into another pointer, and set the global
206 list header to null, so that the global list can change as a side
207 effect of invoking the continuations and the processing of the
208 preexisting continuations will not be affected. */
210 continuations
= *continuations_p
;
211 *continuations_p
= NULL
;
213 /* Work now on the list we have set aside. */
214 do_my_continuations (&continuations
, NULL
);
216 do_cleanups (old_chain
);
219 /* Callback for iterate over threads. */
222 do_all_continuations_thread_callback (struct thread_info
*thread
, void *data
)
224 do_all_continuations_ptid (thread
->ptid
, &thread
->continuations
);
228 /* Do all continuations of thread THREAD. */
231 do_all_continuations_thread (struct thread_info
*thread
)
233 do_all_continuations_thread_callback (thread
, NULL
);
236 /* Do all continuations of all threads. */
239 do_all_continuations (void)
241 iterate_over_threads (do_all_continuations_thread_callback
, NULL
);
244 /* Callback for iterate over threads. */
247 discard_all_continuations_thread_callback (struct thread_info
*thread
,
250 struct continuation
*continuation_ptr
= thread
->continuations
;
252 discard_my_continuations (&continuation_ptr
, NULL
);
253 thread
->continuations
= NULL
;
257 /* Get rid of all the continuations of THREAD. */
260 discard_all_continuations_thread (struct thread_info
*thread
)
262 discard_all_continuations_thread_callback (thread
, NULL
);
265 /* Get rid of all the continuations of all threads. */
268 discard_all_continuations (void)
270 iterate_over_threads (discard_all_continuations_thread_callback
, NULL
);
274 /* Add a continuation to the intermediate continuation list of THREAD.
275 The new continuation will be added at the front. */
278 add_intermediate_continuation (struct thread_info
*thread
,
279 void (*continuation_hook
)
280 (void *), void *args
,
281 void (*continuation_free_args
) (void *))
283 struct continuation
*continuations
= thread
->intermediate_continuations
;
284 make_cleanup_ftype
*continuation_hook_fn
= continuation_hook
;
286 make_continuation (&continuations
,
287 continuation_hook_fn
,
289 continuation_free_args
);
291 thread
->intermediate_continuations
= continuations
;
294 /* Walk down the cmd_continuation list, and execute all the
295 continuations. There is a problem though. In some cases new
296 continuations may be added while we are in the middle of this
297 loop. If this happens they will be added in the front, and done
298 before we have a chance of exhausting those that were already
299 there. We need to then save the beginning of the list in a pointer
300 and do the continuations from there on, instead of using the
301 global beginning of list as our iteration pointer. */
304 do_all_intermediate_continuations_thread_callback (struct thread_info
*thread
,
307 do_all_continuations_ptid (thread
->ptid
,
308 &thread
->intermediate_continuations
);
312 /* Do all intermediate continuations of thread THREAD. */
315 do_all_intermediate_continuations_thread (struct thread_info
*thread
)
317 do_all_intermediate_continuations_thread_callback (thread
, NULL
);
320 /* Do all intermediate continuations of all threads. */
323 do_all_intermediate_continuations (void)
325 iterate_over_threads (do_all_intermediate_continuations_thread_callback
,
329 /* Callback for iterate over threads. */
332 discard_all_intermediate_continuations_thread_callback (struct thread_info
*thread
,
335 struct continuation
*continuation_ptr
= thread
->intermediate_continuations
;
337 discard_my_continuations (&continuation_ptr
, NULL
);
338 thread
->intermediate_continuations
= NULL
;
342 /* Get rid of all the intermediate continuations of THREAD. */
345 discard_all_intermediate_continuations_thread (struct thread_info
*thread
)
347 discard_all_intermediate_continuations_thread_callback (thread
, NULL
);
350 /* Get rid of all the intermediate continuations of all threads. */
353 discard_all_intermediate_continuations (void)
355 iterate_over_threads (discard_all_intermediate_continuations_thread_callback
,