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