Commit | Line | Data |
---|---|---|
50c0c017 PA |
1 | /* Continuations for GDB, the GNU debugger. |
2 | ||
0b302171 | 3 | Copyright (C) 1986, 1988-2012 Free Software Foundation, Inc. |
50c0c017 PA |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "gdbthread.h" | |
22 | #include "inferior.h" | |
be34f849 | 23 | #include "continuations.h" |
50c0c017 PA |
24 | |
25 | struct continuation | |
26 | { | |
27 | struct continuation *next; | |
b0f260d6 PA |
28 | continuation_ftype *function; |
29 | continuation_free_arg_ftype *free_arg; | |
50c0c017 PA |
30 | void *arg; |
31 | }; | |
32 | ||
af1e9a32 PA |
33 | /* Add a new continuation to the continuation chain. Args are |
34 | FUNCTION to run the continuation up with, and ARG to pass to | |
35 | it. */ | |
50c0c017 | 36 | |
af1e9a32 | 37 | static void |
50c0c017 | 38 | make_continuation (struct continuation **pmy_chain, |
b0f260d6 | 39 | continuation_ftype *function, |
50c0c017 PA |
40 | void *arg, void (*free_arg) (void *)) |
41 | { | |
42 | struct continuation *new = XNEW (struct continuation); | |
50c0c017 PA |
43 | |
44 | new->next = *pmy_chain; | |
45 | new->function = function; | |
46 | new->free_arg = free_arg; | |
47 | new->arg = arg; | |
48 | *pmy_chain = new; | |
50c0c017 PA |
49 | } |
50 | ||
51 | static void | |
fa4cd53f | 52 | do_my_continuations_1 (struct continuation **pmy_chain, int err) |
50c0c017 PA |
53 | { |
54 | struct continuation *ptr; | |
55 | ||
af1e9a32 | 56 | while ((ptr = *pmy_chain) != NULL) |
50c0c017 PA |
57 | { |
58 | *pmy_chain = ptr->next; /* Do this first in case of recursion. */ | |
fa4cd53f | 59 | (*ptr->function) (ptr->arg, err); |
50c0c017 PA |
60 | if (ptr->free_arg) |
61 | (*ptr->free_arg) (ptr->arg); | |
62 | xfree (ptr); | |
63 | } | |
64 | } | |
65 | ||
af1e9a32 | 66 | static void |
fa4cd53f | 67 | do_my_continuations (struct continuation **list, int err) |
af1e9a32 PA |
68 | { |
69 | struct continuation *continuations; | |
70 | ||
71 | if (*list == NULL) | |
72 | return; | |
73 | ||
74 | /* Copy the list header into another pointer, and set the global | |
75 | list header to null, so that the global list can change as a side | |
76 | effect of invoking the continuations and the processing of the | |
77 | preexisting continuations will not be affected. */ | |
78 | ||
79 | continuations = *list; | |
80 | *list = NULL; | |
81 | ||
82 | /* Work now on the list we have set aside. */ | |
fa4cd53f | 83 | do_my_continuations_1 (&continuations, err); |
af1e9a32 PA |
84 | } |
85 | ||
86 | static void | |
87 | discard_my_continuations_1 (struct continuation **pmy_chain) | |
50c0c017 PA |
88 | { |
89 | struct continuation *ptr; | |
90 | ||
af1e9a32 | 91 | while ((ptr = *pmy_chain) != NULL) |
50c0c017 PA |
92 | { |
93 | *pmy_chain = ptr->next; | |
94 | if (ptr->free_arg) | |
95 | (*ptr->free_arg) (ptr->arg); | |
96 | xfree (ptr); | |
97 | } | |
98 | } | |
99 | ||
af1e9a32 PA |
100 | static void |
101 | discard_my_continuations (struct continuation **list) | |
50c0c017 | 102 | { |
af1e9a32 | 103 | struct continuation *continuation_ptr = *list; |
50c0c017 | 104 | |
af1e9a32 PA |
105 | discard_my_continuations_1 (list); |
106 | *list = NULL; | |
50c0c017 PA |
107 | } |
108 | ||
109 | /* Add a continuation to the continuation list of INFERIOR. The new | |
110 | continuation will be added at the front. */ | |
111 | ||
112 | void | |
b0f260d6 PA |
113 | add_inferior_continuation (continuation_ftype *hook, void *args, |
114 | continuation_free_arg_ftype *free_arg) | |
50c0c017 PA |
115 | { |
116 | struct inferior *inf = current_inferior (); | |
50c0c017 | 117 | |
b0f260d6 | 118 | make_continuation (&inf->continuations, hook, args, free_arg); |
50c0c017 PA |
119 | } |
120 | ||
121 | /* Do all continuations of the current inferior. */ | |
122 | ||
123 | void | |
fa4cd53f | 124 | do_all_inferior_continuations (int err) |
50c0c017 | 125 | { |
50c0c017 | 126 | struct inferior *inf = current_inferior (); |
fa4cd53f | 127 | do_my_continuations (&inf->continuations, err); |
50c0c017 PA |
128 | } |
129 | ||
130 | /* Get rid of all the inferior-wide continuations of INF. */ | |
131 | ||
132 | void | |
133 | discard_all_inferior_continuations (struct inferior *inf) | |
134 | { | |
af1e9a32 PA |
135 | discard_my_continuations (&inf->continuations); |
136 | } | |
50c0c017 | 137 | |
af1e9a32 PA |
138 | /* Add a continuation to the continuation list of THREAD. The new |
139 | continuation will be added at the front. */ | |
140 | ||
141 | void | |
142 | add_continuation (struct thread_info *thread, | |
b0f260d6 PA |
143 | continuation_ftype *hook, void *args, |
144 | continuation_free_arg_ftype *free_arg) | |
af1e9a32 | 145 | { |
b0f260d6 | 146 | make_continuation (&thread->continuations, hook, args, free_arg); |
50c0c017 PA |
147 | } |
148 | ||
149 | static void | |
150 | restore_thread_cleanup (void *arg) | |
151 | { | |
152 | ptid_t *ptid_p = arg; | |
153 | ||
154 | switch_to_thread (*ptid_p); | |
155 | } | |
156 | ||
157 | /* Walk down the continuation list of PTID, and execute all the | |
158 | continuations. There is a problem though. In some cases new | |
159 | continuations may be added while we are in the middle of this loop. | |
160 | If this happens they will be added in the front, and done before we | |
161 | have a chance of exhausting those that were already there. We need | |
162 | to then save the beginning of the list in a pointer and do the | |
163 | continuations from there on, instead of using the global beginning | |
164 | of list as our iteration pointer. */ | |
165 | ||
166 | static void | |
167 | do_all_continuations_ptid (ptid_t ptid, | |
fa4cd53f PA |
168 | struct continuation **continuations_p, |
169 | int err) | |
50c0c017 PA |
170 | { |
171 | struct cleanup *old_chain; | |
50c0c017 PA |
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, ¤t_thread); | |
189 | ||
190 | /* Let the continuation see this thread as selected. */ | |
191 | switch_to_thread (ptid); | |
192 | ||
fa4cd53f | 193 | do_my_continuations (continuations_p, err); |
50c0c017 PA |
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 | { | |
fa4cd53f PA |
203 | int err = * (int *) data; |
204 | do_all_continuations_ptid (thread->ptid, &thread->continuations, err); | |
50c0c017 PA |
205 | return 0; |
206 | } | |
207 | ||
208 | /* Do all continuations of thread THREAD. */ | |
209 | ||
210 | void | |
fa4cd53f | 211 | do_all_continuations_thread (struct thread_info *thread, int err) |
50c0c017 | 212 | { |
fa4cd53f | 213 | do_all_continuations_thread_callback (thread, &err); |
50c0c017 PA |
214 | } |
215 | ||
216 | /* Do all continuations of all threads. */ | |
217 | ||
218 | void | |
fa4cd53f | 219 | do_all_continuations (int err) |
50c0c017 | 220 | { |
fa4cd53f | 221 | iterate_over_threads (do_all_continuations_thread_callback, &err); |
50c0c017 PA |
222 | } |
223 | ||
224 | /* Callback for iterate over threads. */ | |
225 | ||
226 | static int | |
227 | discard_all_continuations_thread_callback (struct thread_info *thread, | |
228 | void *data) | |
229 | { | |
af1e9a32 | 230 | discard_my_continuations (&thread->continuations); |
50c0c017 PA |
231 | return 0; |
232 | } | |
233 | ||
234 | /* Get rid of all the continuations of THREAD. */ | |
235 | ||
236 | void | |
237 | discard_all_continuations_thread (struct thread_info *thread) | |
238 | { | |
239 | discard_all_continuations_thread_callback (thread, NULL); | |
240 | } | |
241 | ||
242 | /* Get rid of all the continuations of all threads. */ | |
243 | ||
244 | void | |
245 | discard_all_continuations (void) | |
246 | { | |
247 | iterate_over_threads (discard_all_continuations_thread_callback, NULL); | |
248 | } | |
249 | ||
250 | ||
251 | /* Add a continuation to the intermediate continuation list of THREAD. | |
252 | The new continuation will be added at the front. */ | |
253 | ||
254 | void | |
255 | add_intermediate_continuation (struct thread_info *thread, | |
b0f260d6 PA |
256 | continuation_ftype *hook, |
257 | void *args, | |
258 | continuation_free_arg_ftype *free_arg) | |
50c0c017 | 259 | { |
b0f260d6 PA |
260 | make_continuation (&thread->intermediate_continuations, hook, |
261 | args, free_arg); | |
50c0c017 PA |
262 | } |
263 | ||
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. */ | |
272 | ||
273 | static int | |
274 | do_all_intermediate_continuations_thread_callback (struct thread_info *thread, | |
275 | void *data) | |
276 | { | |
fa4cd53f PA |
277 | int err = * (int *) data; |
278 | ||
50c0c017 | 279 | do_all_continuations_ptid (thread->ptid, |
fa4cd53f | 280 | &thread->intermediate_continuations, err); |
50c0c017 PA |
281 | return 0; |
282 | } | |
283 | ||
284 | /* Do all intermediate continuations of thread THREAD. */ | |
285 | ||
286 | void | |
fa4cd53f | 287 | do_all_intermediate_continuations_thread (struct thread_info *thread, int err) |
50c0c017 | 288 | { |
fa4cd53f | 289 | do_all_intermediate_continuations_thread_callback (thread, &err); |
50c0c017 PA |
290 | } |
291 | ||
292 | /* Do all intermediate continuations of all threads. */ | |
293 | ||
294 | void | |
fa4cd53f | 295 | do_all_intermediate_continuations (int err) |
50c0c017 PA |
296 | { |
297 | iterate_over_threads (do_all_intermediate_continuations_thread_callback, | |
fa4cd53f | 298 | &err); |
50c0c017 PA |
299 | } |
300 | ||
301 | /* Callback for iterate over threads. */ | |
302 | ||
303 | static int | |
304 | discard_all_intermediate_continuations_thread_callback (struct thread_info *thread, | |
305 | void *data) | |
306 | { | |
af1e9a32 | 307 | discard_my_continuations (&thread->intermediate_continuations); |
50c0c017 PA |
308 | return 0; |
309 | } | |
310 | ||
311 | /* Get rid of all the intermediate continuations of THREAD. */ | |
312 | ||
313 | void | |
314 | discard_all_intermediate_continuations_thread (struct thread_info *thread) | |
315 | { | |
316 | discard_all_intermediate_continuations_thread_callback (thread, NULL); | |
317 | } | |
318 | ||
319 | /* Get rid of all the intermediate continuations of all threads. */ | |
320 | ||
321 | void | |
322 | discard_all_intermediate_continuations (void) | |
323 | { | |
324 | iterate_over_threads (discard_all_intermediate_continuations_thread_callback, | |
325 | NULL); | |
326 | } |