2011-05-27 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / continuations.c
1 /* Continuations for GDB, the GNU debugger.
2
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.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "defs.h"
23 #include "gdbthread.h"
24 #include "inferior.h"
25
26 struct continuation
27 {
28 struct continuation *next;
29 void (*function) (void *);
30 void (*free_arg) (void *);
31 void *arg;
32 };
33
34 typedef void (make_continuation_ftype) (void *);
35
36 /* Add a new continuation to the continuation chain. Args are
37 FUNCTION to run the continuation up with, and ARG to pass to
38 it. */
39
40 static void
41 make_continuation (struct continuation **pmy_chain,
42 make_continuation_ftype *function,
43 void *arg, void (*free_arg) (void *))
44 {
45 struct continuation *new = XNEW (struct continuation);
46
47 new->next = *pmy_chain;
48 new->function = function;
49 new->free_arg = free_arg;
50 new->arg = arg;
51 *pmy_chain = new;
52 }
53
54 static void
55 do_my_continuations_1 (struct continuation **pmy_chain)
56 {
57 struct continuation *ptr;
58
59 while ((ptr = *pmy_chain) != NULL)
60 {
61 *pmy_chain = ptr->next; /* Do this first in case of recursion. */
62 (*ptr->function) (ptr->arg);
63 if (ptr->free_arg)
64 (*ptr->free_arg) (ptr->arg);
65 xfree (ptr);
66 }
67 }
68
69 static void
70 do_my_continuations (struct continuation **list)
71 {
72 struct continuation *continuations;
73
74 if (*list == NULL)
75 return;
76
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. */
81
82 continuations = *list;
83 *list = NULL;
84
85 /* Work now on the list we have set aside. */
86 do_my_continuations_1 (&continuations);
87 }
88
89 static void
90 discard_my_continuations_1 (struct continuation **pmy_chain)
91 {
92 struct continuation *ptr;
93
94 while ((ptr = *pmy_chain) != NULL)
95 {
96 *pmy_chain = ptr->next;
97 if (ptr->free_arg)
98 (*ptr->free_arg) (ptr->arg);
99 xfree (ptr);
100 }
101 }
102
103 static void
104 discard_my_continuations (struct continuation **list)
105 {
106 struct continuation *continuation_ptr = *list;
107
108 discard_my_continuations_1 (list);
109 *list = NULL;
110 }
111
112 /* Add a continuation to the continuation list of INFERIOR. The new
113 continuation will be added at the front. */
114
115 void
116 add_inferior_continuation (void (*continuation_hook) (void *), void *args,
117 void (*continuation_free_args) (void *))
118 {
119 struct inferior *inf = current_inferior ();
120
121 make_continuation (&inf->continuations, continuation_hook,
122 args, continuation_free_args);
123 }
124
125 /* Do all continuations of the current inferior. */
126
127 void
128 do_all_inferior_continuations (void)
129 {
130 struct inferior *inf = current_inferior ();
131 do_my_continuations (&inf->continuations);
132 }
133
134 /* Get rid of all the inferior-wide continuations of INF. */
135
136 void
137 discard_all_inferior_continuations (struct inferior *inf)
138 {
139 discard_my_continuations (&inf->continuations);
140 }
141
142 /* Add a continuation to the continuation list of THREAD. The new
143 continuation will be added at the front. */
144
145 void
146 add_continuation (struct thread_info *thread,
147 void (*continuation_hook) (void *), void *args,
148 void (*continuation_free_args) (void *))
149 {
150 make_continuation (&thread->continuations, continuation_hook,
151 args, continuation_free_args);
152 }
153
154 static void
155 restore_thread_cleanup (void *arg)
156 {
157 ptid_t *ptid_p = arg;
158
159 switch_to_thread (*ptid_p);
160 }
161
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. */
170
171 static void
172 do_all_continuations_ptid (ptid_t ptid,
173 struct continuation **continuations_p)
174 {
175 struct cleanup *old_chain;
176 ptid_t current_thread;
177
178 if (*continuations_p == NULL)
179 return;
180
181 current_thread = inferior_ptid;
182
183 /* Restore selected thread on exit. Don't try to restore the frame
184 as well, because:
185
186 - When running continuations, the selected frame is always #0.
187
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. */
191
192 old_chain = make_cleanup (restore_thread_cleanup, &current_thread);
193
194 /* Let the continuation see this thread as selected. */
195 switch_to_thread (ptid);
196
197 do_my_continuations (continuations_p);
198
199 do_cleanups (old_chain);
200 }
201
202 /* Callback for iterate over threads. */
203
204 static int
205 do_all_continuations_thread_callback (struct thread_info *thread, void *data)
206 {
207 do_all_continuations_ptid (thread->ptid, &thread->continuations);
208 return 0;
209 }
210
211 /* Do all continuations of thread THREAD. */
212
213 void
214 do_all_continuations_thread (struct thread_info *thread)
215 {
216 do_all_continuations_thread_callback (thread, NULL);
217 }
218
219 /* Do all continuations of all threads. */
220
221 void
222 do_all_continuations (void)
223 {
224 iterate_over_threads (do_all_continuations_thread_callback, NULL);
225 }
226
227 /* Callback for iterate over threads. */
228
229 static int
230 discard_all_continuations_thread_callback (struct thread_info *thread,
231 void *data)
232 {
233 discard_my_continuations (&thread->continuations);
234 return 0;
235 }
236
237 /* Get rid of all the continuations of THREAD. */
238
239 void
240 discard_all_continuations_thread (struct thread_info *thread)
241 {
242 discard_all_continuations_thread_callback (thread, NULL);
243 }
244
245 /* Get rid of all the continuations of all threads. */
246
247 void
248 discard_all_continuations (void)
249 {
250 iterate_over_threads (discard_all_continuations_thread_callback, NULL);
251 }
252
253
254 /* Add a continuation to the intermediate continuation list of THREAD.
255 The new continuation will be added at the front. */
256
257 void
258 add_intermediate_continuation (struct thread_info *thread,
259 void (*continuation_hook)
260 (void *), void *args,
261 void (*continuation_free_args) (void *))
262 {
263 make_continuation (&thread->intermediate_continuations, continuation_hook,
264 args, continuation_free_args);
265 }
266
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. */
275
276 static int
277 do_all_intermediate_continuations_thread_callback (struct thread_info *thread,
278 void *data)
279 {
280 do_all_continuations_ptid (thread->ptid,
281 &thread->intermediate_continuations);
282 return 0;
283 }
284
285 /* Do all intermediate continuations of thread THREAD. */
286
287 void
288 do_all_intermediate_continuations_thread (struct thread_info *thread)
289 {
290 do_all_intermediate_continuations_thread_callback (thread, NULL);
291 }
292
293 /* Do all intermediate continuations of all threads. */
294
295 void
296 do_all_intermediate_continuations (void)
297 {
298 iterate_over_threads (do_all_intermediate_continuations_thread_callback,
299 NULL);
300 }
301
302 /* Callback for iterate over threads. */
303
304 static int
305 discard_all_intermediate_continuations_thread_callback (struct thread_info *thread,
306 void *data)
307 {
308 discard_my_continuations (&thread->intermediate_continuations);
309 return 0;
310 }
311
312 /* Get rid of all the intermediate continuations of THREAD. */
313
314 void
315 discard_all_intermediate_continuations_thread (struct thread_info *thread)
316 {
317 discard_all_intermediate_continuations_thread_callback (thread, NULL);
318 }
319
320 /* Get rid of all the intermediate continuations of all threads. */
321
322 void
323 discard_all_intermediate_continuations (void)
324 {
325 iterate_over_threads (discard_all_intermediate_continuations_thread_callback,
326 NULL);
327 }
This page took 0.036514 seconds and 5 git commands to generate.