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