From patchwork Tue Jan 12 21:04:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Packard X-Patchwork-Id: 12014819 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6829AC433DB for ; Tue, 12 Jan 2021 21:08:09 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D240223123 for ; Tue, 12 Jan 2021 21:08:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D240223123 Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=nongnu.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:43834 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kzQtb-0002WY-OT for qemu-devel@archiver.kernel.org; Tue, 12 Jan 2021 16:08:07 -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) 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 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) 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== Received: by keithp.com (Postfix, from userid 1000) id 2DAA91582498; Tue, 12 Jan 2021 13:04:27 -0800 (PST) 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 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_Mathieu-Daud=C3=A9?= , Keith Packard , =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Reply-to: Keith Packard X-Patchwork-Original-From: Keith Packard via From: Keith Packard 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) bool gdb_has_xml; +/* 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 != NULL; +} + #ifdef CONFIG_USER_ONLY +/* Close GDB connection */ +static void gdb_disconnect(void) +{ + if (gdb_is_connected()) { + close(gdbserver_state.fd); + gdbserver_state.fd = -1; + gdbserver_state.c_cpu = NULL; + } +} + static int get_char(void) { uint8_t ch; @@ -424,12 +444,11 @@ static int get_char(void) ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0); if (ret < 0) { if (errno == ECONNRESET) - gdbserver_state.fd = -1; + gdb_disconnect(); if (errno != EINTR) return -1; } else if (ret == 0) { - close(gdbserver_state.fd); - gdbserver_state.fd = -1; + gdb_disconnect(); return -1; } else { break; @@ -2796,7 +2815,7 @@ void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va) target_ulong addr; uint64_t i64; - if (!gdbserver_state.init) { + if (!gdb_is_connected()) { return; } @@ -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 trace_gdbstub_op_exiting((uint8_t)code); @@ -3072,7 +3091,7 @@ gdb_handlesig(CPUState *cpu, int sig) char buf[256]; int n; - if (!gdbserver_state.init || gdbserver_state.fd < 0) { + if (!gdb_is_connected()) { return sig; } @@ -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; } sig = 0; gdbserver_state.state = RS_IDLE; gdbserver_state.running_state = 0; - while (gdbserver_state.running_state == 0) { + while (gdbserver_state.running_state == 0 && gdb_is_connected()) { n = 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 == 0) { - close(gdbserver_state.fd); - } - gdbserver_state.fd = -1; + gdb_disconnect(); return sig; } } @@ -3121,7 +3137,7 @@ void gdb_signalled(CPUArchState *env, int sig) { char buf[4]; - if (!gdbserver_state.init || gdbserver_state.fd < 0) { + if (!gdb_is_connected()) { return; } @@ -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 = -1; + gdb_disconnect(); cpu_breakpoint_remove_all(cpu, BP_GDB); cpu_watchpoint_remove_all(cpu, BP_GDB); }