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