Fix dw2-ifort-parameter.exp on PPC64
[binutils-gdb.git] / gdb / remote-notif.c
blobd3e1b26394b7b779008aae49a078fd18466ff9e1
1 /* Remote notification in GDB protocol
3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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).
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. */
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"
41 #include "gdbcmd.h"
43 #include <string.h>
45 int notif_debug = 0;
47 /* Supported clients of notifications. */
49 static struct notif_client *notifs[] =
51 &notif_client_stop,
54 gdb_static_assert (ARRAY_SIZE (notifs) == REMOTE_NOTIF_LAST);
56 static void do_notif_event_xfree (void *arg);
58 /* Parse the BUF for the expected notification NC, and send packet to
59 acknowledge. */
61 void
62 remote_notif_ack (struct notif_client *nc, char *buf)
64 struct notif_event *event = nc->alloc_event ();
65 struct cleanup *old_chain
66 = make_cleanup (do_notif_event_xfree, event);
68 if (notif_debug)
69 fprintf_unfiltered (gdb_stdlog, "notif: ack '%s'\n",
70 nc->ack_command);
72 nc->parse (nc, buf, event);
73 nc->ack (nc, buf, event);
75 discard_cleanups (old_chain);
78 /* Parse the BUF for the expected notification NC. */
80 struct notif_event *
81 remote_notif_parse (struct notif_client *nc, char *buf)
83 struct notif_event *event = nc->alloc_event ();
84 struct cleanup *old_chain
85 = make_cleanup (do_notif_event_xfree, event);
87 if (notif_debug)
88 fprintf_unfiltered (gdb_stdlog, "notif: parse '%s'\n", nc->name);
90 nc->parse (nc, buf, event);
92 discard_cleanups (old_chain);
93 return event;
96 DEFINE_QUEUE_P (notif_client_p);
98 /* Process notifications in STATE's notification queue one by one.
99 EXCEPT is not expected in the queue. */
101 void
102 remote_notif_process (struct remote_notif_state *state,
103 struct notif_client *except)
105 while (!QUEUE_is_empty (notif_client_p, state->notif_queue))
107 struct notif_client *nc = QUEUE_deque (notif_client_p,
108 state->notif_queue);
110 gdb_assert (nc != except);
112 if (nc->can_get_pending_events (nc))
113 remote_notif_get_pending_events (nc);
117 static void
118 remote_async_get_pending_events_handler (gdb_client_data data)
120 gdb_assert (non_stop);
121 remote_notif_process (data, NULL);
124 /* Remote notification handler. Parse BUF, queue notification and
125 update STATE. */
127 void
128 handle_notification (struct remote_notif_state *state, char *buf)
130 struct notif_client *nc;
131 size_t i;
133 for (i = 0; i < ARRAY_SIZE (notifs); i++)
135 const char *name = notifs[i]->name;
137 if (strncmp (buf, name, strlen (name)) == 0
138 && buf[strlen (name)] == ':')
139 break;
142 /* We ignore notifications we don't recognize, for compatibility
143 with newer stubs. */
144 if (i == ARRAY_SIZE (notifs))
145 return;
147 nc = notifs[i];
149 if (state->pending_event[nc->id] != NULL)
151 /* We've already parsed the in-flight reply, but the stub for some
152 reason thought we didn't, possibly due to timeout on its side.
153 Just ignore it. */
154 if (notif_debug)
155 fprintf_unfiltered (gdb_stdlog,
156 "notif: ignoring resent notification\n");
158 else
160 struct notif_event *event
161 = remote_notif_parse (nc, buf + strlen (nc->name) + 1);
163 /* Be careful to only set it after parsing, since an error
164 may be thrown then. */
165 state->pending_event[nc->id] = event;
167 /* Notify the event loop there's a stop reply to acknowledge
168 and that there may be more events to fetch. */
169 QUEUE_enque (notif_client_p, state->notif_queue, nc);
170 if (non_stop)
172 /* In non-stop, We mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
173 in order to go on what we were doing and postpone
174 querying notification events to some point safe to do so.
175 See details in the function comment of
176 remote.c:remote_notif_get_pending_events.
178 In all-stop, GDB may be blocked to wait for the reply, we
179 shouldn't return to event loop until the expected reply
180 arrives. For example:
182 1.1) --> vCont;c
183 GDB expects getting stop reply 'T05 thread:2'.
184 1.2) <-- %Notif
185 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
187 After step #1.2, we return to the event loop, which
188 notices there is a new event on the
189 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and calls the
190 handler, which will send 'vNotif' packet.
191 1.3) --> vNotif
192 It is not safe to start a new sequence, because target
193 is still running and GDB is expecting the stop reply
194 from stub.
196 To solve this, whenever we parse a notification
197 successfully, we don't mark the
198 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and let GDB blocked
199 there as before to get the sequence done.
201 2.1) --> vCont;c
202 GDB expects getting stop reply 'T05 thread:2'
203 2.2) <-- %Notif
204 <Don't mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
205 2.3) <-- T05 thread:2
207 These pending notifications can be processed later. */
208 mark_async_event_handler (state->get_pending_events_token);
211 if (notif_debug)
212 fprintf_unfiltered (gdb_stdlog,
213 "notif: Notification '%s' captured\n",
214 nc->name);
218 /* Invoke destructor of EVENT and xfree it. */
220 void
221 notif_event_xfree (struct notif_event *event)
223 if (event != NULL && event->dtr != NULL)
224 event->dtr (event);
226 xfree (event);
229 /* Cleanup wrapper. */
231 static void
232 do_notif_event_xfree (void *arg)
234 notif_event_xfree (arg);
237 /* Return an allocated remote_notif_state. */
239 struct remote_notif_state *
240 remote_notif_state_allocate (void)
242 struct remote_notif_state *notif_state = xzalloc (sizeof (*notif_state));
244 notif_state->notif_queue = QUEUE_alloc (notif_client_p, NULL);
246 /* Register async_event_handler for notification. */
248 notif_state->get_pending_events_token
249 = create_async_event_handler (remote_async_get_pending_events_handler,
250 notif_state);
252 return notif_state;
255 /* Free STATE and its fields. */
257 void
258 remote_notif_state_xfree (struct remote_notif_state *state)
260 int i;
262 QUEUE_free (notif_client_p, state->notif_queue);
264 /* Unregister async_event_handler for notification. */
265 if (state->get_pending_events_token != NULL)
266 delete_async_event_handler (&state->get_pending_events_token);
268 for (i = 0; i < REMOTE_NOTIF_LAST; i++)
269 notif_event_xfree (state->pending_event[i]);
271 xfree (state);
274 /* -Wmissing-prototypes */
275 extern initialize_file_ftype _initialize_notif;
277 void
278 _initialize_notif (void)
280 add_setshow_boolean_cmd ("notification", no_class, &notif_debug,
281 _("\
282 Set debugging of async remote notification."), _("\
283 Show debugging of async remote notification."), _("\
284 When non-zero, debugging output about async remote notifications"
285 " is enabled."),
286 NULL,
287 NULL,
288 &setdebuglist, &showdebuglist);