From patchwork Wed May 6 12:05:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 186259 Delivered-To: patch@linaro.org Received: by 2002:a92:8d81:0:0:0:0:0 with SMTP id w1csp2056541ill; Wed, 6 May 2020 05:14:18 -0700 (PDT) X-Google-Smtp-Source: APiQypLnY9/vDqZtWe3au2kB6tmr4x90+cwy8bjpjAm5RDvZIY0KzYBWDKJq6KXV5RXsodzfX47x X-Received: by 2002:a0c:fc0c:: with SMTP id z12mr7216032qvo.41.1588767258412; Wed, 06 May 2020 05:14:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588767258; cv=none; d=google.com; s=arc-20160816; b=ITHkI/TBtZKT8WtCcmdzLcZ/19rlJOXZjsyguYwxXBYbpbDlHwJz/n1nHFhgfSrqKg YwDyaap0c7Yu+/oXEn+vzcL0+fax8lOTE+lHWGXMu2EA6etJMvWIeBvIGIlpslfxtFrh q43Kye0PP3vqQJ4EPiGWgitvU936IH+IX/IfomCi0tnb2yCQEW+yHT285gomCFDilgHG UX2RBk47UA5QDYy9L7heyaPhnNtbYLl5itIbpKGPkjwSvIfJpjmhLQUr72Op8wQczNzf 285gtFNaPqCc+EqVq9HmoVGGAzmnWTkqO6AmXWArctx3tulC3EQ/BJpmPNgEKwi96aie NjMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=usxqN0chs/+svG7Q89+uOboA+O3BScfxkits4ZbaY44=; b=RXIQlm026rPf5JJUXHkscuSijY9CwPOp+hBhFHqwjDcOVBUtLyplF6dQrTMl9HKtls XOL4GfQZQ6CcRnz317VdpKcMOhq1lXBm+MUHtWlrIr0FEPJcdw3BrVeGozsuPUkng0wx xapH9qsbY/j3nGrL4yb96HEx8TsKwHG5EOdYLTuJyjsp277RYkXrLZbfWtSyhVzzwPZE HMVUJrAgDHOs1eDgYrkVmu6oZKHiwhwouAtoK+stYueH7SDkM9YdpkhFY8qq1DouQ3Bo ZKAtTaV5+BU+mQe8qclmgsVaVt0S58YjOuo38fcf6G0NNCp1m0kBSx97un9A2Wn5/gyY yazQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=POJtNPv3; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:470:142::17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:470:142::17]) by mx.google.com with ESMTPS id n202si665706qka.151.2020.05.06.05.14.18 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 06 May 2020 05:14:18 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:470:142::17 as permitted sender) client-ip=2001:470:142::17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=POJtNPv3; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:470:142::17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:60360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jWIwL-0004Qr-VX for patch@linaro.org; Wed, 06 May 2020 08:14:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56534) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jWIo9-0006Lf-Ah for qemu-devel@nongnu.org; Wed, 06 May 2020 08:05:49 -0400 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]:39606) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jWIo7-00012z-6z for qemu-devel@nongnu.org; Wed, 06 May 2020 08:05:48 -0400 Received: by mail-wm1-x32d.google.com with SMTP id y24so2344096wma.4 for ; Wed, 06 May 2020 05:05:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=usxqN0chs/+svG7Q89+uOboA+O3BScfxkits4ZbaY44=; b=POJtNPv3efthz7dl5Ok8qN9NoLDOYAH5sEvyJa1iHs+ZEaywIM42HrYrK+6dNHrf4R xGZghz6KEq33gyAp3xmwjCCfK/aDSj15xcTcJ0fEjvW1MdLT33P3LvQwuZnZA8AUpuFf /1iu0o7Z0CPnyEsYqa9UiYEO/++x8ckI1uxf65onWQGih/LSz4fKdqe4giP/mcMcgJM1 q3EHslFqG17hve4e/cv+4bSp2NsdxMzGK9LstrA174r/gQbGfYiJDdANURKt893/X0lI okPmN4bqEZXpMKb2XbnCizS2gu1CxGYRs3SAN/w9K9bsKgxVnViSf+5bcc8c5jsBOZOh 8lMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=usxqN0chs/+svG7Q89+uOboA+O3BScfxkits4ZbaY44=; b=VsBzc4vKXE9GC5jvsC2pyelwGSKH6LSgf5Lilky0qHeuEGIDF50Gu92uLXiaMoi5LE dic8JCm3f6h7fxGWomW+TniUdHCq0V61+zofhyRXmb/a4yd7XioD6RUskz9s4Pc2IYrh LL0G92Hr8Ubs+M7j6AgbwcLQFZsaPoV2xaCFgq3VXOefT11T28XADhYYPdGU61Y6/civ 1j2BwwUBXgyoC/oqyv2ujZZXQKSEn3mCh0ZQlkU7fDU4fOvlHebKAo5NGoZmMhVJTbiL tvQHP/ExYUYfU8xzbcSB6vz2OmCCOAry/lQBn6qOfcHMMwDusrCL4mPdU2+VldXkFTLI 0yBg== X-Gm-Message-State: AGi0PuZaXdCT7gjVvE1EV3E39BVp2VJlhLAHb175i5RJexIfSxgC1q9D T1oR2/ySOl8GsJy6ROpg9AZjLg== X-Received: by 2002:a1c:bc08:: with SMTP id m8mr3946648wmf.119.1588766745661; Wed, 06 May 2020 05:05:45 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id k17sm2683515wmi.10.2020.05.06.05.05.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2020 05:05:37 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id ADFEB1FF9A; Wed, 6 May 2020 13:05:30 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 11/14] gdbstub/linux-user: support debugging over a unix socket Date: Wed, 6 May 2020 13:05:26 +0100 Message-Id: <20200506120529.18974-12-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200506120529.18974-1-alex.bennee@linaro.org> References: <20200506120529.18974-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32d; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x32d.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN 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?= , Riku Voipio , Richard Henderson , Laurent Vivier , qemu-devel@nongnu.org, =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" While debugging over TCP is fairly straightforward now we have test cases that want to orchestrate via make and currently a parallel build fails as two processes can't use the same listening port. While system emulation offers a wide cornucopia of connection methods thanks to the chardev abstraction we are a little more limited for linux user. Thankfully the programming API for a TCP socket and a local UNIX socket is pretty much the same once it's set up. Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson Message-Id: <20200430190122.4592-7-alex.bennee@linaro.org> -- 2.20.1 diff --git a/include/exec/gdbstub.h b/include/exec/gdbstub.h index 4a2b8e30893..94d8f83e92d 100644 --- a/include/exec/gdbstub.h +++ b/include/exec/gdbstub.h @@ -177,11 +177,15 @@ static inline uint8_t * gdb_get_reg_ptr(GByteArray *buf, int len) #endif -#ifdef CONFIG_USER_ONLY -int gdbserver_start(int); -#else -int gdbserver_start(const char *port); -#endif +/** + * gdbserver_start: start the gdb server + * @port_or_device: connection spec for gdb + * + * For CONFIG_USER this is either a tcp port or a path to a fifo. For + * system emulation you can use a full chardev spec for your gdbserver + * port. + */ +int gdbserver_start(const char *port_or_device); void gdbserver_cleanup(void); diff --git a/bsd-user/main.c b/bsd-user/main.c index 770c2b267ad..28f122b80e6 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -738,7 +738,7 @@ int main(int argc, char **argv) CPUState *cpu; int optind; const char *r; - int gdbstub_port = 0; + const char *gdbstub = NULL; char **target_environ, **wrk; envlist_t *envlist = NULL; char *trace_file = NULL; @@ -814,7 +814,7 @@ int main(int argc, char **argv) exit(1); } } else if (!strcmp(r, "g")) { - gdbstub_port = atoi(argv[optind++]); + gdbstub = g_strdup(argv[optind++]); } else if (!strcmp(r, "r")) { qemu_uname_release = argv[optind++]; } else if (!strcmp(r, "cpu")) { @@ -1124,8 +1124,8 @@ int main(int argc, char **argv) #error unsupported target CPU #endif - if (gdbstub_port) { - gdbserver_start (gdbstub_port); + if (gdbstub) { + gdbserver_start(gdbstub); gdb_handlesig(cpu, 0); } cpu_loop(env); diff --git a/gdbstub.c b/gdbstub.c index b5381aa5204..6950fd243f3 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -355,6 +355,7 @@ typedef struct GDBState { int signal; #ifdef CONFIG_USER_ONLY int fd; + char *socket_path; int running_state; #else CharBackend chr; @@ -2962,6 +2963,9 @@ void gdb_exit(CPUArchState *env, int code) return; } #ifdef CONFIG_USER_ONLY + if (gdbserver_state.socket_path) { + unlink(gdbserver_state.socket_path); + } if (gdbserver_state.fd < 0) { return; } @@ -3066,7 +3070,66 @@ void gdb_signalled(CPUArchState *env, int sig) put_packet(buf); } -static bool gdb_accept(int gdb_fd) +static void gdb_accept_init(int fd) +{ + init_gdbserver_state(); + create_default_process(&gdbserver_state); + gdbserver_state.processes[0].attached = true; + gdbserver_state.c_cpu = gdb_first_attached_cpu(); + gdbserver_state.g_cpu = gdbserver_state.c_cpu; + gdbserver_state.fd = fd; + gdb_has_xml = false; +} + +static bool gdb_accept_socket(int gdb_fd) +{ + int fd; + + for(;;) { + fd = accept(gdb_fd, NULL, NULL); + if (fd < 0 && errno != EINTR) { + perror("accept socket"); + return false; + } else if (fd >= 0) { + qemu_set_cloexec(fd); + break; + } + } + + gdb_accept_init(fd); + return true; +} + +static int gdbserver_open_socket(const char *path) +{ + struct sockaddr_un sockaddr; + int fd, ret; + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { + perror("create socket"); + return -1; + } + + sockaddr.sun_family = AF_UNIX; + pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path); + ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); + if (ret < 0) { + perror("bind socket"); + close(fd); + return -1; + } + ret = listen(fd, 1); + if (ret < 0) { + perror("listen socket"); + close(fd); + return -1; + } + + return fd; +} + +static bool gdb_accept_tcp(int gdb_fd) { struct sockaddr_in sockaddr; socklen_t len; @@ -3091,17 +3154,11 @@ static bool gdb_accept(int gdb_fd) return false; } - init_gdbserver_state(); - create_default_process(&gdbserver_state); - gdbserver_state.processes[0].attached = true; - gdbserver_state.c_cpu = gdb_first_attached_cpu(); - gdbserver_state.g_cpu = gdbserver_state.c_cpu; - gdbserver_state.fd = fd; - gdb_has_xml = false; + gdb_accept_init(fd); return true; } -static int gdbserver_open(int port) +static int gdbserver_open_port(int port) { struct sockaddr_in sockaddr; int fd, ret; @@ -3130,21 +3187,35 @@ static int gdbserver_open(int port) close(fd); return -1; } + return fd; } -int gdbserver_start(int port) +int gdbserver_start(const char *port_or_path) { - int gdb_fd = gdbserver_open(port); + int port = g_ascii_strtoull(port_or_path, NULL, 10); + int gdb_fd; + + if (port > 0) { + gdb_fd = gdbserver_open_port(port); + } else { + gdb_fd = gdbserver_open_socket(port_or_path); + } + if (gdb_fd < 0) { return -1; } - /* accept connections */ - if (!gdb_accept(gdb_fd)) { - close(gdb_fd); - return -1; + + if (port > 0 && gdb_accept_tcp(gdb_fd)) { + return 0; + } else if (gdb_accept_socket(gdb_fd)) { + gdbserver_state.socket_path = g_strdup(port_or_path); + return 0; } - return 0; + + /* gone wrong */ + close(gdb_fd); + return -1; } /* Disable gdb stub for child processes. */ diff --git a/linux-user/main.c b/linux-user/main.c index 22578b16336..2cd443237d8 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -51,7 +51,7 @@ char *exec_path; int singlestep; static const char *argv0; -static int gdbstub_port; +static const char *gdbstub; static envlist_t *envlist; static const char *cpu_model; static const char *cpu_type; @@ -310,7 +310,7 @@ static void handle_arg_seed(const char *arg) static void handle_arg_gdb(const char *arg) { - gdbstub_port = atoi(arg); + gdbstub = g_strdup(arg); } static void handle_arg_uname(const char *arg) @@ -861,10 +861,10 @@ int main(int argc, char **argv, char **envp) target_cpu_copy_regs(env, regs); - if (gdbstub_port) { - if (gdbserver_start(gdbstub_port) < 0) { - fprintf(stderr, "qemu: could not open gdbserver on port %d\n", - gdbstub_port); + if (gdbstub) { + if (gdbserver_start(gdbstub) < 0) { + fprintf(stderr, "qemu: could not open gdbserver on %s\n", + gdbstub); exit(EXIT_FAILURE); } gdb_handlesig(cpu, 0);