Skip to content

Commit da90652

Browse files
casaroliacassis
authored andcommitted
system/gdbstub: Serve the next client when a TCP client closes.
With "gdbstub -p <port>", a client that closes the connection stopped the server: - read() returns 0 when the peer closed, and gdb_recv() called it again forever without blocking. gdbstub kept the CPU busy and never accepted another client. This happens on the gdb command "disconnect" or when gdb is killed (gdb sends "k" first only when it exits normally). - An error other than ECONNRESET from gdb_process() ended gdbstub. - A write to the closed socket can raise SIGPIPE, which ends gdbstub when the default action of SIGPIPE terminates the task. - Each new client allocated a new gdb state; the old one leaked. gdb_recv() now returns -ECONNRESET at end of file, as the stub does on a "k" packet. In TCP mode, any error from gdb_process() ends the session: gdbstub frees the state, closes the connection and accepts the next client. The TCP mode sends with send(MSG_NOSIGNAL). The tty mode does not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 09a59b9 commit da90652

1 file changed

Lines changed: 45 additions & 11 deletions

File tree

‎system/gdbstub/gdbstub.c‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static int gdb_monitor(FAR struct gdb_state_s *state, FAR const char *cmd)
7272
#endif
7373
}
7474

75-
static ssize_t gdb_send(FAR void *priv, FAR const char *buf, size_t len)
75+
static ssize_t gdb_write(FAR void *priv, FAR const char *buf, size_t len)
7676
{
7777
int fd = *(FAR int *)priv;
7878
size_t i = 0;
@@ -92,6 +92,28 @@ static ssize_t gdb_send(FAR void *priv, FAR const char *buf, size_t len)
9292
return len;
9393
}
9494

95+
#ifdef CONFIG_NET
96+
static ssize_t gdb_send(FAR void *priv, FAR const char *buf, size_t len)
97+
{
98+
int fd = *(FAR int *)priv;
99+
size_t i = 0;
100+
101+
while (i < len)
102+
{
103+
ssize_t ret = send(fd, buf + i, len - i, MSG_NOSIGNAL);
104+
105+
if (ret < 0)
106+
{
107+
return -errno;
108+
}
109+
110+
i += ret;
111+
}
112+
113+
return len;
114+
}
115+
#endif
116+
95117
static ssize_t gdb_recv(FAR void *priv, FAR void *buf,
96118
size_t len)
97119
{
@@ -106,6 +128,10 @@ static ssize_t gdb_recv(FAR void *priv, FAR void *buf,
106128
{
107129
return -errno;
108130
}
131+
else if (ret == 0)
132+
{
133+
return -ECONNRESET; /* The peer closed the connection */
134+
}
109135

110136
i += ret;
111137
}
@@ -215,7 +241,17 @@ int main(int argc, FAR char *argv[])
215241
usage(argv[0]);
216242
}
217243

218-
state = gdb_state_init(gdb_send, gdb_recv, gdb_monitor, &fd);
244+
#ifdef CONFIG_NET
245+
if (port)
246+
{
247+
state = gdb_state_init(gdb_send, gdb_recv, gdb_monitor, &fd);
248+
}
249+
else
250+
#endif
251+
{
252+
state = gdb_state_init(gdb_write, gdb_recv, gdb_monitor, &fd);
253+
}
254+
219255
if (state == NULL)
220256
{
221257
ret = -ENOMEM;
@@ -225,18 +261,16 @@ int main(int argc, FAR char *argv[])
225261
do
226262
{
227263
ret = gdb_process(state, 0, NULL);
228-
if (ret == -ECONNRESET)
229-
{
230264
#ifdef CONFIG_NET
231-
if (port)
232-
{
233-
close(fd);
234-
goto reconnect;
235-
}
265+
if (ret < 0 && port)
266+
{
267+
/* The session ended: wait for the next client */
236268

237-
#endif
238-
continue;
269+
gdb_state_uninit(state);
270+
close(fd);
271+
goto reconnect;
239272
}
273+
#endif
240274
}
241275
while (ret >= 0);
242276

0 commit comments

Comments
 (0)