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