Commit | Line | Data |
---|---|---|
722247f1 YQ |
1 | /* Remote notification in GDB protocol |
2 | ||
618f726f | 3 | Copyright (C) 1988-2016 Free Software Foundation, Inc. |
722247f1 YQ |
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 | /* Remote async notification is sent from remote target over RSP. | |
21 | Each type of notification is represented by an object of | |
22 | 'struct notif', which has a field 'pending_reply'. It is not | |
23 | NULL when GDB receives a notification from GDBserver, but hasn't | |
24 | acknowledge yet. Before GDB acknowledges the notification, | |
25 | GDBserver shouldn't send notification again (see the header comments | |
26 | in gdbserver/notif.c). | |
27 | ||
28 | Notifications are processed in an almost-unified approach for both | |
29 | all-stop mode and non-stop mode, except the timing to process them. | |
30 | In non-stop mode, notifications are processed in | |
31 | remote_async_get_pending_events_handler, while in all-stop mode, | |
32 | they are processed in remote_resume. */ | |
33 | ||
34 | #include "defs.h" | |
35 | #include "remote.h" | |
36 | #include "remote-notif.h" | |
37 | #include "observer.h" | |
38 | #include "event-loop.h" | |
39 | #include "target.h" | |
40 | #include "inferior.h" | |
45741a9c | 41 | #include "infrun.h" |
c9b6281a | 42 | #include "gdbcmd.h" |
722247f1 | 43 | |
99f0a309 | 44 | int notif_debug = 0; |
722247f1 YQ |
45 | |
46 | /* Supported clients of notifications. */ | |
47 | ||
48 | static struct notif_client *notifs[] = | |
49 | { | |
50 | ¬if_client_stop, | |
51 | }; | |
52 | ||
f48ff2a7 YQ |
53 | gdb_static_assert (ARRAY_SIZE (notifs) == REMOTE_NOTIF_LAST); |
54 | ||
722247f1 YQ |
55 | static void do_notif_event_xfree (void *arg); |
56 | ||
57 | /* Parse the BUF for the expected notification NC, and send packet to | |
58 | acknowledge. */ | |
59 | ||
60 | void | |
61 | remote_notif_ack (struct notif_client *nc, char *buf) | |
62 | { | |
63 | struct notif_event *event = nc->alloc_event (); | |
64 | struct cleanup *old_chain | |
65 | = make_cleanup (do_notif_event_xfree, event); | |
66 | ||
67 | if (notif_debug) | |
68 | fprintf_unfiltered (gdb_stdlog, "notif: ack '%s'\n", | |
69 | nc->ack_command); | |
70 | ||
71 | nc->parse (nc, buf, event); | |
72 | nc->ack (nc, buf, event); | |
73 | ||
74 | discard_cleanups (old_chain); | |
75 | } | |
76 | ||
77 | /* Parse the BUF for the expected notification NC. */ | |
78 | ||
79 | struct notif_event * | |
80 | remote_notif_parse (struct notif_client *nc, char *buf) | |
81 | { | |
82 | struct notif_event *event = nc->alloc_event (); | |
83 | struct cleanup *old_chain | |
84 | = make_cleanup (do_notif_event_xfree, event); | |
85 | ||
86 | if (notif_debug) | |
87 | fprintf_unfiltered (gdb_stdlog, "notif: parse '%s'\n", nc->name); | |
88 | ||
89 | nc->parse (nc, buf, event); | |
90 | ||
91 | discard_cleanups (old_chain); | |
92 | return event; | |
93 | } | |
94 | ||
722247f1 YQ |
95 | DEFINE_QUEUE_P (notif_client_p); |
96 | ||
5965e028 YQ |
97 | /* Process notifications in STATE's notification queue one by one. |
98 | EXCEPT is not expected in the queue. */ | |
722247f1 YQ |
99 | |
100 | void | |
5965e028 YQ |
101 | remote_notif_process (struct remote_notif_state *state, |
102 | struct notif_client *except) | |
722247f1 | 103 | { |
5965e028 | 104 | while (!QUEUE_is_empty (notif_client_p, state->notif_queue)) |
722247f1 YQ |
105 | { |
106 | struct notif_client *nc = QUEUE_deque (notif_client_p, | |
5965e028 | 107 | state->notif_queue); |
722247f1 YQ |
108 | |
109 | gdb_assert (nc != except); | |
110 | ||
111 | if (nc->can_get_pending_events (nc)) | |
112 | remote_notif_get_pending_events (nc); | |
113 | } | |
114 | } | |
115 | ||
116 | static void | |
117 | remote_async_get_pending_events_handler (gdb_client_data data) | |
118 | { | |
6efcd9a8 | 119 | gdb_assert (target_is_non_stop_p ()); |
19ba03f4 | 120 | remote_notif_process ((struct remote_notif_state *) data, NULL); |
722247f1 YQ |
121 | } |
122 | ||
5965e028 YQ |
123 | /* Remote notification handler. Parse BUF, queue notification and |
124 | update STATE. */ | |
722247f1 YQ |
125 | |
126 | void | |
5965e028 | 127 | handle_notification (struct remote_notif_state *state, char *buf) |
722247f1 | 128 | { |
62972e0b YQ |
129 | struct notif_client *nc; |
130 | size_t i; | |
722247f1 YQ |
131 | |
132 | for (i = 0; i < ARRAY_SIZE (notifs); i++) | |
133 | { | |
62972e0b YQ |
134 | const char *name = notifs[i]->name; |
135 | ||
61012eef | 136 | if (startswith (buf, name) |
62972e0b | 137 | && buf[strlen (name)] == ':') |
722247f1 YQ |
138 | break; |
139 | } | |
140 | ||
141 | /* We ignore notifications we don't recognize, for compatibility | |
142 | with newer stubs. */ | |
62972e0b | 143 | if (i == ARRAY_SIZE (notifs)) |
722247f1 YQ |
144 | return; |
145 | ||
62972e0b YQ |
146 | nc = notifs[i]; |
147 | ||
f48ff2a7 | 148 | if (state->pending_event[nc->id] != NULL) |
722247f1 YQ |
149 | { |
150 | /* We've already parsed the in-flight reply, but the stub for some | |
151 | reason thought we didn't, possibly due to timeout on its side. | |
152 | Just ignore it. */ | |
153 | if (notif_debug) | |
154 | fprintf_unfiltered (gdb_stdlog, | |
155 | "notif: ignoring resent notification\n"); | |
156 | } | |
157 | else | |
158 | { | |
159 | struct notif_event *event | |
160 | = remote_notif_parse (nc, buf + strlen (nc->name) + 1); | |
161 | ||
162 | /* Be careful to only set it after parsing, since an error | |
163 | may be thrown then. */ | |
f48ff2a7 | 164 | state->pending_event[nc->id] = event; |
722247f1 YQ |
165 | |
166 | /* Notify the event loop there's a stop reply to acknowledge | |
167 | and that there may be more events to fetch. */ | |
5965e028 | 168 | QUEUE_enque (notif_client_p, state->notif_queue, nc); |
6efcd9a8 | 169 | if (target_is_non_stop_p ()) |
722247f1 YQ |
170 | { |
171 | /* In non-stop, We mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN | |
172 | in order to go on what we were doing and postpone | |
173 | querying notification events to some point safe to do so. | |
174 | See details in the function comment of | |
175 | remote.c:remote_notif_get_pending_events. | |
176 | ||
177 | In all-stop, GDB may be blocked to wait for the reply, we | |
178 | shouldn't return to event loop until the expected reply | |
179 | arrives. For example: | |
180 | ||
181 | 1.1) --> vCont;c | |
182 | GDB expects getting stop reply 'T05 thread:2'. | |
183 | 1.2) <-- %Notif | |
184 | <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN> | |
185 | ||
186 | After step #1.2, we return to the event loop, which | |
187 | notices there is a new event on the | |
188 | REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and calls the | |
189 | handler, which will send 'vNotif' packet. | |
190 | 1.3) --> vNotif | |
191 | It is not safe to start a new sequence, because target | |
192 | is still running and GDB is expecting the stop reply | |
193 | from stub. | |
194 | ||
195 | To solve this, whenever we parse a notification | |
196 | successfully, we don't mark the | |
197 | REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and let GDB blocked | |
198 | there as before to get the sequence done. | |
199 | ||
200 | 2.1) --> vCont;c | |
201 | GDB expects getting stop reply 'T05 thread:2' | |
202 | 2.2) <-- %Notif | |
203 | <Don't mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN> | |
204 | 2.3) <-- T05 thread:2 | |
205 | ||
206 | These pending notifications can be processed later. */ | |
5965e028 | 207 | mark_async_event_handler (state->get_pending_events_token); |
722247f1 YQ |
208 | } |
209 | ||
210 | if (notif_debug) | |
211 | fprintf_unfiltered (gdb_stdlog, | |
212 | "notif: Notification '%s' captured\n", | |
213 | nc->name); | |
214 | } | |
215 | } | |
216 | ||
f48ff2a7 | 217 | /* Invoke destructor of EVENT and xfree it. */ |
722247f1 | 218 | |
f48ff2a7 YQ |
219 | void |
220 | notif_event_xfree (struct notif_event *event) | |
722247f1 | 221 | { |
f48ff2a7 | 222 | if (event != NULL && event->dtr != NULL) |
722247f1 YQ |
223 | event->dtr (event); |
224 | ||
225 | xfree (event); | |
226 | } | |
227 | ||
f48ff2a7 YQ |
228 | /* Cleanup wrapper. */ |
229 | ||
230 | static void | |
231 | do_notif_event_xfree (void *arg) | |
232 | { | |
19ba03f4 | 233 | notif_event_xfree ((struct notif_event *) arg); |
f48ff2a7 YQ |
234 | } |
235 | ||
5965e028 YQ |
236 | /* Return an allocated remote_notif_state. */ |
237 | ||
238 | struct remote_notif_state * | |
239 | remote_notif_state_allocate (void) | |
240 | { | |
8d749320 | 241 | struct remote_notif_state *notif_state = XCNEW (struct remote_notif_state); |
5965e028 YQ |
242 | |
243 | notif_state->notif_queue = QUEUE_alloc (notif_client_p, NULL); | |
244 | ||
245 | /* Register async_event_handler for notification. */ | |
246 | ||
247 | notif_state->get_pending_events_token | |
248 | = create_async_event_handler (remote_async_get_pending_events_handler, | |
249 | notif_state); | |
250 | ||
251 | return notif_state; | |
252 | } | |
253 | ||
254 | /* Free STATE and its fields. */ | |
255 | ||
256 | void | |
257 | remote_notif_state_xfree (struct remote_notif_state *state) | |
722247f1 | 258 | { |
f48ff2a7 YQ |
259 | int i; |
260 | ||
5965e028 YQ |
261 | QUEUE_free (notif_client_p, state->notif_queue); |
262 | ||
263 | /* Unregister async_event_handler for notification. */ | |
264 | if (state->get_pending_events_token != NULL) | |
265 | delete_async_event_handler (&state->get_pending_events_token); | |
722247f1 | 266 | |
f48ff2a7 YQ |
267 | for (i = 0; i < REMOTE_NOTIF_LAST; i++) |
268 | notif_event_xfree (state->pending_event[i]); | |
269 | ||
5965e028 | 270 | xfree (state); |
722247f1 YQ |
271 | } |
272 | ||
273 | /* -Wmissing-prototypes */ | |
274 | extern initialize_file_ftype _initialize_notif; | |
275 | ||
276 | void | |
277 | _initialize_notif (void) | |
278 | { | |
c9b6281a YQ |
279 | add_setshow_boolean_cmd ("notification", no_class, ¬if_debug, |
280 | _("\ | |
281 | Set debugging of async remote notification."), _("\ | |
282 | Show debugging of async remote notification."), _("\ | |
283 | When non-zero, debugging output about async remote notifications" | |
284 | " is enabled."), | |
285 | NULL, | |
286 | NULL, | |
287 | &setdebuglist, &showdebuglist); | |
722247f1 | 288 | } |