2011-05-27 Pedro Alves <pedro@codesourcery.com>
[external/binutils.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   continuation_ftype *function;
30   continuation_free_arg_ftype *free_arg;
31   void *arg;
32 };
33
34 /* Add a new continuation to the continuation chain.  Args are
35    FUNCTION to run the continuation up with, and ARG to pass to
36    it.  */
37
38 static void
39 make_continuation (struct continuation **pmy_chain,
40                    continuation_ftype *function,
41                    void *arg,  void (*free_arg) (void *))
42 {
43   struct continuation *new = XNEW (struct continuation);
44
45   new->next = *pmy_chain;
46   new->function = function;
47   new->free_arg = free_arg;
48   new->arg = arg;
49   *pmy_chain = new;
50 }
51
52 static void
53 do_my_continuations_1 (struct continuation **pmy_chain)
54 {
55   struct continuation *ptr;
56
57   while ((ptr = *pmy_chain) != NULL)
58     {
59       *pmy_chain = ptr->next;   /* Do this first in case of recursion.  */
60       (*ptr->function) (ptr->arg);
61       if (ptr->free_arg)
62         (*ptr->free_arg) (ptr->arg);
63       xfree (ptr);
64     }
65 }
66
67 static void
68 do_my_continuations (struct continuation **list)
69 {
70   struct continuation *continuations;
71
72   if (*list == NULL)
73     return;
74
75   /* Copy the list header into another pointer, and set the global
76      list header to null, so that the global list can change as a side
77      effect of invoking the continuations and the processing of the
78      preexisting continuations will not be affected.  */
79
80   continuations = *list;
81   *list = NULL;
82
83   /* Work now on the list we have set aside.  */
84   do_my_continuations_1 (&continuations);
85 }
86
87 static void
88 discard_my_continuations_1 (struct continuation **pmy_chain)
89 {
90   struct continuation *ptr;
91
92   while ((ptr = *pmy_chain) != NULL)
93     {
94       *pmy_chain = ptr->next;
95       if (ptr->free_arg)
96         (*ptr->free_arg) (ptr->arg);
97       xfree (ptr);
98     }
99 }
100
101 static void
102 discard_my_continuations (struct continuation **list)
103 {
104   struct continuation *continuation_ptr = *list;
105
106   discard_my_continuations_1 (list);
107   *list = NULL;
108 }
109
110 /* Add a continuation to the continuation list of INFERIOR.  The new
111    continuation will be added at the front.  */
112
113 void
114 add_inferior_continuation (continuation_ftype *hook, void *args,
115                            continuation_free_arg_ftype *free_arg)
116 {
117   struct inferior *inf = current_inferior ();
118
119   make_continuation (&inf->continuations, hook, args, free_arg);
120 }
121
122 /* Do all continuations of the current inferior.  */
123
124 void
125 do_all_inferior_continuations (void)
126 {
127   struct inferior *inf = current_inferior ();
128   do_my_continuations (&inf->continuations);
129 }
130
131 /* Get rid of all the inferior-wide continuations of INF.  */
132
133 void
134 discard_all_inferior_continuations (struct inferior *inf)
135 {
136   discard_my_continuations (&inf->continuations);
137 }
138
139 /* Add a continuation to the continuation list of THREAD.  The new
140    continuation will be added at the front.  */
141
142 void
143 add_continuation (struct thread_info *thread,
144                   continuation_ftype *hook, void *args,
145                   continuation_free_arg_ftype *free_arg)
146 {
147   make_continuation (&thread->continuations, hook, args, free_arg);
148 }
149
150 static void
151 restore_thread_cleanup (void *arg)
152 {
153   ptid_t *ptid_p = arg;
154
155   switch_to_thread (*ptid_p);
156 }
157
158 /* Walk down the continuation list of PTID, and execute all the
159    continuations.  There is a problem though.  In some cases new
160    continuations may be added while we are in the middle of this loop.
161    If this happens they will be added in the front, and done before we
162    have a chance of exhausting those that were already there.  We need
163    to then save the beginning of the list in a pointer and do the
164    continuations from there on, instead of using the global beginning
165    of list as our iteration pointer.  */
166
167 static void
168 do_all_continuations_ptid (ptid_t ptid,
169                            struct continuation **continuations_p)
170 {
171   struct cleanup *old_chain;
172   ptid_t current_thread;
173
174   if (*continuations_p == NULL)
175     return;
176
177   current_thread = inferior_ptid;
178
179   /* Restore selected thread on exit.  Don't try to restore the frame
180      as well, because:
181
182      - When running continuations, the selected frame is always #0.
183
184      - The continuations may trigger symbol file loads, which may
185      change the frame layout (frame ids change), which would trigger
186      a warning if we used make_cleanup_restore_current_thread.  */
187
188   old_chain = make_cleanup (restore_thread_cleanup, &current_thread);
189
190   /* Let the continuation see this thread as selected.  */
191   switch_to_thread (ptid);
192
193   do_my_continuations (continuations_p);
194
195   do_cleanups (old_chain);
196 }
197
198 /* Callback for iterate over threads.  */
199
200 static int
201 do_all_continuations_thread_callback (struct thread_info *thread, void *data)
202 {
203   do_all_continuations_ptid (thread->ptid, &thread->continuations);
204   return 0;
205 }
206
207 /* Do all continuations of thread THREAD.  */
208
209 void
210 do_all_continuations_thread (struct thread_info *thread)
211 {
212   do_all_continuations_thread_callback (thread, NULL);
213 }
214
215 /* Do all continuations of all threads.  */
216
217 void
218 do_all_continuations (void)
219 {
220   iterate_over_threads (do_all_continuations_thread_callback, NULL);
221 }
222
223 /* Callback for iterate over threads.  */
224
225 static int
226 discard_all_continuations_thread_callback (struct thread_info *thread,
227                                            void *data)
228 {
229   discard_my_continuations (&thread->continuations);
230   return 0;
231 }
232
233 /* Get rid of all the continuations of THREAD.  */
234
235 void
236 discard_all_continuations_thread (struct thread_info *thread)
237 {
238   discard_all_continuations_thread_callback (thread, NULL);
239 }
240
241 /* Get rid of all the continuations of all threads.  */
242
243 void
244 discard_all_continuations (void)
245 {
246   iterate_over_threads (discard_all_continuations_thread_callback, NULL);
247 }
248
249
250 /* Add a continuation to the intermediate continuation list of THREAD.
251    The new continuation will be added at the front.  */
252
253 void
254 add_intermediate_continuation (struct thread_info *thread,
255                                continuation_ftype *hook,
256                                void *args,
257                                continuation_free_arg_ftype *free_arg)
258 {
259   make_continuation (&thread->intermediate_continuations, hook,
260                      args, free_arg);
261 }
262
263 /* Walk down the cmd_continuation list, and execute all the
264    continuations.  There is a problem though.  In some cases new
265    continuations may be added while we are in the middle of this
266    loop.  If this happens they will be added in the front, and done
267    before we have a chance of exhausting those that were already
268    there.  We need to then save the beginning of the list in a pointer
269    and do the continuations from there on, instead of using the
270    global beginning of list as our iteration pointer.  */
271
272 static int
273 do_all_intermediate_continuations_thread_callback (struct thread_info *thread,
274                                                    void *data)
275 {
276   do_all_continuations_ptid (thread->ptid,
277                              &thread->intermediate_continuations);
278   return 0;
279 }
280
281 /* Do all intermediate continuations of thread THREAD.  */
282
283 void
284 do_all_intermediate_continuations_thread (struct thread_info *thread)
285 {
286   do_all_intermediate_continuations_thread_callback (thread, NULL);
287 }
288
289 /* Do all intermediate continuations of all threads.  */
290
291 void
292 do_all_intermediate_continuations (void)
293 {
294   iterate_over_threads (do_all_intermediate_continuations_thread_callback,
295                         NULL);
296 }
297
298 /* Callback for iterate over threads.  */
299
300 static int
301 discard_all_intermediate_continuations_thread_callback (struct thread_info *thread,
302                                                         void *data)
303 {
304   discard_my_continuations (&thread->intermediate_continuations);
305   return 0;
306 }
307
308 /* Get rid of all the intermediate continuations of THREAD.  */
309
310 void
311 discard_all_intermediate_continuations_thread (struct thread_info *thread)
312 {
313   discard_all_intermediate_continuations_thread_callback (thread, NULL);
314 }
315
316 /* Get rid of all the intermediate continuations of all threads.  */
317
318 void
319 discard_all_intermediate_continuations (void)
320 {
321   iterate_over_threads (discard_all_intermediate_continuations_thread_callback,
322                         NULL);
323 }