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