From nobody Thu Apr 25 14:56:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1610485692; cv=none; d=zohomail.com; s=zohoarc; b=boio+B7Myh5h8TMIFcv6vRy9TvXA8tDRSiogXuHDknMHNtz+a3IJI0Vqjg04s1QTi9IotNxiylkY6t93PLO0LA47i4hq9wQGPTwONiISgaDXw6GO8jqaulfLaSk5xIo2EQhcy4bUxZUhY3IQDt4D1rtDi40cqv/vnPoF9a29CFc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610485692; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=+W0bZAZ/dI684O4HQ6kR6qN2OdvHX/iT3DI7YkQw4i4=; b=Fw673XUO+XI2Xp3pEVV7O1vtIs7JiJCLYHXtaTLXnJZaEyQTKssjApCgYTOgonbdgqpsiReX6CJdalpg2wxQIXA28sflkVHOZV3Uexy1Yv3v8s0Mbhos36m40VfZyDMfOGQEIh/0DwPdcAAMQ+EDeWRlSDG8k2mhciRP2PxuZX8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1610485692427207.5752608549701; Tue, 12 Jan 2021 13:08:12 -0800 (PST) Received: from localhost ([::1]:43834 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kzQtf-0002WY-9l for importer@patchew.org; Tue, 12 Jan 2021 16:08:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36236) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kzQq9-0007N5-JH for qemu-devel@nongnu.org; Tue, 12 Jan 2021 16:04:33 -0500 Received: from home.keithp.com ([63.227.221.253]:41552 helo=elaine.keithp.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kzQq6-0000AI-WF for qemu-devel@nongnu.org; Tue, 12 Jan 2021 16:04:33 -0500 Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id CBB213F2E77B; Tue, 12 Jan 2021 13:04:27 -0800 (PST) Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id BUR8CZLgFRWy; Tue, 12 Jan 2021 13:04:27 -0800 (PST) Received: from keithp.com (koto.keithp.com [10.0.0.2]) by elaine.keithp.com (Postfix) with ESMTPSA id 518E13F2E776; Tue, 12 Jan 2021 13:04:27 -0800 (PST) Received: by keithp.com (Postfix, from userid 1000) id 2DAA91582498; Tue, 12 Jan 2021 13:04:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=keithp.com; s=mail; t=1610485467; bh=ld1gKjzLv+O7RWaahWAnmnHTLDHyIytV9VjtlF0mN2U=; h=From:To:Cc:Subject:Date:From; b=QoI6jnoRpbtq1RhWGDdijkNwHnfN8yW6mm1Yzu3lideyDZ49r0xlf4bAgNHqecO/J A+LtyYfDvgWCbLuh2W9fT0pTPkjUXUUu6UZxMJ6pmDYU0bAarVw6sG5PatUbmX/gkf 0vUFwykwCTT6yeyi9lCDkvQzcrpv7h2urXuTG+rXAUnKpdVLQ7oI+Ju/N+b5U4rmDl Srv/iMcPZudT+o3Bs2y5ERipakvzcBerFfhdOCfGfZO6VP6QBHgaB6lMUoNsblykLa +9D0CPx7M0tJzYUNNA+Qjmt9AfA5VBwy4BbvPLeb55IvuAPoFla0Y2KELQuL7jPjGZ x3GwHw5dGG4Bw== X-Virus-Scanned: Debian amavisd-new at keithp.com DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=keithp.com; s=mail; t=1610485467; bh=ld1gKjzLv+O7RWaahWAnmnHTLDHyIytV9VjtlF0mN2U=; h=From:To:Cc:Subject:Date:From; b=QoI6jnoRpbtq1RhWGDdijkNwHnfN8yW6mm1Yzu3lideyDZ49r0xlf4bAgNHqecO/J A+LtyYfDvgWCbLuh2W9fT0pTPkjUXUUu6UZxMJ6pmDYU0bAarVw6sG5PatUbmX/gkf 0vUFwykwCTT6yeyi9lCDkvQzcrpv7h2urXuTG+rXAUnKpdVLQ7oI+Ju/N+b5U4rmDl Srv/iMcPZudT+o3Bs2y5ERipakvzcBerFfhdOCfGfZO6VP6QBHgaB6lMUoNsblykLa +9D0CPx7M0tJzYUNNA+Qjmt9AfA5VBwy4BbvPLeb55IvuAPoFla0Y2KELQuL7jPjGZ x3GwHw5dGG4Bw== To: qemu-devel@nongnu.org Subject: [PATCH] Create API for checking and clearing GDB connection status Date: Tue, 12 Jan 2021 13:04:18 -0800 Message-Id: <20210112210418.1471412-1-keithp@keithp.com> X-Mailer: git-send-email 2.30.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.227.221.253; envelope-from=keithp@keithp.com; helo=elaine.keithp.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Keith Packard , =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Keith Packard From: Keith Packard via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When checking whether there is a live gdb connection, code shouldn't use 'gdbserver_state.init' as that value is set when the gdbserver_state structure is initialized in init_gdbserver_state, not when the gdb socket has a valid connection. I've created two new functions to manage the gdb connection status: /* Check whether GDB is currently connected */ static int gdb_is_connected(void) #ifdef CONFIG_USER_ONLY /* Close GDB connection */ static void gdb_disconnect(void) #endif The first checks whether there is an active GDB connection, the second closes that connection and resets the connection status indication. The 'handle_detach' function used 'gdbserver_state.c_cpu' as an indication of whether there is a connection, so I've used the same in gdb_is_connected as that is independent of CONFIG_USER_ONLY. This avoids a segfault when qemu is run with the '-s' flag (create a gdb protocol socket), but without the '-S' flag (delay until 'c' command is received). Signed-off-by: Keith Packard --- gdbstub.c | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/gdbstub.c b/gdbstub.c index d99bc0bf2e..8ee7e442d5 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -413,8 +413,28 @@ static void reset_gdbserver_state(void) =20 bool gdb_has_xml; =20 +/* Check whether GDB is currently connected */ +static int gdb_is_connected(void) +{ + /* + * XXX c_cpu is NULL until gdb_accept_init has been called, so use + * this as a proxy for whether the gdb connection is active + */ + return gdbserver_state.c_cpu !=3D NULL; +} + #ifdef CONFIG_USER_ONLY =20 +/* Close GDB connection */ +static void gdb_disconnect(void) +{ + if (gdb_is_connected()) { + close(gdbserver_state.fd); + gdbserver_state.fd =3D -1; + gdbserver_state.c_cpu =3D NULL; + } +} + static int get_char(void) { uint8_t ch; @@ -424,12 +444,11 @@ static int get_char(void) ret =3D qemu_recv(gdbserver_state.fd, &ch, 1, 0); if (ret < 0) { if (errno =3D=3D ECONNRESET) - gdbserver_state.fd =3D -1; + gdb_disconnect(); if (errno !=3D EINTR) return -1; } else if (ret =3D=3D 0) { - close(gdbserver_state.fd); - gdbserver_state.fd =3D -1; + gdb_disconnect(); return -1; } else { break; @@ -2796,7 +2815,7 @@ void gdb_do_syscallv(gdb_syscall_complete_cb cb, cons= t char *fmt, va_list va) target_ulong addr; uint64_t i64; =20 - if (!gdbserver_state.init) { + if (!gdb_is_connected()) { return; } =20 @@ -3025,9 +3044,9 @@ void gdb_exit(CPUArchState *env, int code) if (gdbserver_state.socket_path) { unlink(gdbserver_state.socket_path); } - if (gdbserver_state.fd < 0) { - return; - } + if (!gdb_is_connected()) { + return; + } #endif =20 trace_gdbstub_op_exiting((uint8_t)code); @@ -3072,7 +3091,7 @@ gdb_handlesig(CPUState *cpu, int sig) char buf[256]; int n; =20 - if (!gdbserver_state.init || gdbserver_state.fd < 0) { + if (!gdb_is_connected()) { return sig; } =20 @@ -3086,14 +3105,14 @@ gdb_handlesig(CPUState *cpu, int sig) } /* put_packet() might have detected that the peer terminated the connection. */ - if (gdbserver_state.fd < 0) { + if (!gdb_is_connected()) { return sig; } =20 sig =3D 0; gdbserver_state.state =3D RS_IDLE; gdbserver_state.running_state =3D 0; - while (gdbserver_state.running_state =3D=3D 0) { + while (gdbserver_state.running_state =3D=3D 0 && gdb_is_connected()) { n =3D read(gdbserver_state.fd, buf, 256); if (n > 0) { int i; @@ -3104,10 +3123,7 @@ gdb_handlesig(CPUState *cpu, int sig) } else { /* XXX: Connection closed. Should probably wait for another connection before continuing. */ - if (n =3D=3D 0) { - close(gdbserver_state.fd); - } - gdbserver_state.fd =3D -1; + gdb_disconnect(); return sig; } } @@ -3121,7 +3137,7 @@ void gdb_signalled(CPUArchState *env, int sig) { char buf[4]; =20 - if (!gdbserver_state.init || gdbserver_state.fd < 0) { + if (!gdb_is_connected()) { return; } =20 @@ -3280,11 +3296,10 @@ int gdbserver_start(const char *port_or_path) /* Disable gdb stub for child processes. */ void gdbserver_fork(CPUState *cpu) { - if (!gdbserver_state.init || gdbserver_state.fd < 0) { + if (!gdb_is_connected()) { return; } - close(gdbserver_state.fd); - gdbserver_state.fd =3D -1; + gdb_disconnect(); cpu_breakpoint_remove_all(cpu, BP_GDB); cpu_watchpoint_remove_all(cpu, BP_GDB); } --=20 2.30.0