From nobody Thu Nov 10 21:02:22 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4N7Z662Gmrz4XCtP; Thu, 10 Nov 2022 21:02:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4N7Z661lFrz4CWh; Thu, 10 Nov 2022 21:02:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668114142; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1Hh4MBhwsJ6U2p11b6opNNGD/06nVBwFrgnbqC+nSOE=; b=gsCjiznIcIBu9WFQHno+sWndOqfrK0Z5laAtVkCqD31xPDapGcnWAkUfr7pi2afX3d6VIo 48uu1PR5oYIRkPf6jLO5mzzSWyJU/tMAr9NL7nn70MAvcEghZ8fMGiQ8LXOY23zAQz8DM9 xq9mu/2/jMDYUVOPMKK7SALVNBqP7w+xe/WyfUzg4k1I6waRa++YFoiV+7wnbOLwXJKF5m GTloDKDhsclgjoTCf4bu8DipIrZrVfk2knFiZVbnnk7TR8WP3SmDWSkL8imjIRY3nvaCdx zaq9bMm/rJB48cwOopcy8oU7++6S4+LVWNI0+8GIsy6Xf2h79vg2F00lUsSS6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668114142; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1Hh4MBhwsJ6U2p11b6opNNGD/06nVBwFrgnbqC+nSOE=; b=vQv0cdf3FQ1311XoHrfm2tU31nCqRbVtLJvhuJIw+e8oOMQdAVTsi2sbI5KGzJUTcMdKuq psF+Cr2YgQtNolQXhOpr/KDVyGUBnUUDjBxJJlus7bIqLytgTUBtROL19QGNlBrTDRzy1M Y6U7++thtRDbWhKTmVKDoWm2YET85Km94PCeIO9pn348mgqqMJzvGFkZs6y9w6S/wv0yDr ijwiR5+pcpujh0vrehAwJASXBn1ovTaPweCPohQ0Gw/L9ANAr4Diy8xbZk+D1WAiEljDm+ G0A9mbmf94MQEtfmYuctlNfGvS15hvbhPbxyIR41PH/c3XVgsgma+qvRp1gYsA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1668114142; a=rsa-sha256; cv=none; b=VRoaUYzdfRV245ABTJtcbAqFuP2rKW5Xo9DEKgjFNJaTB/4YAR0a0dXlG9t3Lph5ohVCck NWqevxcpuAkEg23kz4ujqq+7XaKWmJN31MvHKqzbNGiDO+Eqsuj/xGNSr8PsiK/uMGahdR I/UeDlDR9/K8eqXP6SUH6iI26F5ZOIRrQeu8MFQQTcvXeBwKZXZ6NlhLq+1/+XT1OABMQr sW+7oqoBJ8YSQgwtQP5jNIEr5PqB7Gm71w449m6elbWqRPLj+xuLNClpZGwWVaP2b7N2gW fESnqqclqz2IIsb8Ex7+glDg6fLUFWd9GTGpPcsOjQwwHOfQBJvlQ1aaB3Ra7g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4N7Z660nn9zGdG; Thu, 10 Nov 2022 21:02:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2AAL2MlV056734; Thu, 10 Nov 2022 21:02:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2AAL2MO6056716; Thu, 10 Nov 2022 21:02:22 GMT (envelope-from git) Date: Thu, 10 Nov 2022 21:02:22 GMT Message-Id: <202211102102.2AAL2MO6056716@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: fd8bed8b21b5 - stable/13 - byhve: add option to specify IP address for gdb List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: fd8bed8b21b5607dc1665de3344f0d371e2cce3b Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=fd8bed8b21b5607dc1665de3344f0d371e2cce3b commit fd8bed8b21b5607dc1665de3344f0d371e2cce3b Author: Mariusz Zaborski AuthorDate: 2021-08-19 17:52:12 +0000 Commit: John Baldwin CommitDate: 2022-11-10 20:54:37 +0000 byhve: add option to specify IP address for gdb Allow user to specify the IP address available for gdb debugger. Reviewed by: jhb, grehan, rgrimes, bcr (man pages) Differential Revision: https://reviews.freebsd.org/D29607 (cherry picked from commit 2cdff9918e79dcd9bfcaa919b8364afa41cd1e02) --- usr.sbin/bhyve/bhyve.8 | 14 +++++++++---- usr.sbin/bhyve/bhyverun.c | 35 ++++++++++++++++++++++++--------- usr.sbin/bhyve/gdb.c | 50 +++++++++++++++++++++++++++++++++++++---------- usr.sbin/bhyve/gdb.h | 2 +- 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/usr.sbin/bhyve/bhyve.8 b/usr.sbin/bhyve/bhyve.8 index 1de8c516b785..6d78ad872f63 100644 --- a/usr.sbin/bhyve/bhyve.8 +++ b/usr.sbin/bhyve/bhyve.8 @@ -137,16 +137,22 @@ Force .Nm to exit when a guest issues an access to an I/O port that is not emulated. This is intended for debug purposes. -.It Fl G Ar port +.It Fl G Xo +.Sm off +.Oo Ar w Oc +.Oo Ar bind_address: Oc +.Ar port +.Sm on +.Xc Start a debug server that uses the GDB protocol to export guest state to a debugger. An IPv4 TCP socket will be bound to the supplied +.Ar bind_address +and .Ar port to listen for debugger connections. Only a single debugger may be attached to the debug server at a time. -If -.Ar port -begins with +If the option begins with .Sq w , .Nm will pause execution at the first instruction waiting for a debugger to attach. diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c index fec4ca29de80..e9fc3c98ddeb 100644 --- a/usr.sbin/bhyve/bhyverun.c +++ b/usr.sbin/bhyve/bhyverun.c @@ -1186,6 +1186,30 @@ parse_simple_config_file(const char *path) fclose(fp); } +static void +parse_gdb_options(char *optarg) +{ + const char *sport; + char *colon; + + if (optarg[0] == 'w') { + set_config_bool("gdb.wait", true); + optarg++; + } + + colon = strrchr(optarg, ':'); + if (colon == NULL) { + sport = optarg; + } else { + *colon = '\0'; + colon++; + sport = colon; + set_config_value("gdb.address", optarg); + } + + set_config_value("gdb.port", sport); +} + static void set_defaults(void) { @@ -1249,11 +1273,7 @@ main(int argc, char *argv[]) set_config_bool("memory.guest_in_core", true); break; case 'G': - if (optarg[0] == 'w') { - set_config_bool("gdb.wait", true); - optarg++; - } - set_config_value("gdb.port", optarg); + parse_gdb_options(optarg); break; case 'k': parse_simple_config_file(optarg); @@ -1440,10 +1460,7 @@ main(int argc, char *argv[]) if (get_config_bool("acpi_tables")) vmgenc_init(ctx); - value = get_config_value("gdb.port"); - if (value != NULL) - init_gdb(ctx, atoi(value), get_config_bool_default("gdb.wait", - false)); + init_gdb(ctx); if (lpc_bootrom()) { if (vm_set_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, 1)) { diff --git a/usr.sbin/bhyve/gdb.c b/usr.sbin/bhyve/gdb.c index dd6f19d476f0..40f226701a1a 100644 --- a/usr.sbin/bhyve/gdb.c +++ b/usr.sbin/bhyve/gdb.c @@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -59,6 +60,7 @@ __FBSDID("$FreeBSD$"); #include #include "bhyverun.h" +#include "config.h" #include "gdb.h" #include "mem.h" #include "mevent.h" @@ -1818,12 +1820,35 @@ limit_gdb_socket(int s) #endif void -init_gdb(struct vmctx *_ctx, int sport, bool wait) +init_gdb(struct vmctx *_ctx) { - struct sockaddr_in sin; int error, flags, optval, s; + struct addrinfo hints; + struct addrinfo *gdbaddr; + const char *saddr, *value; + char *sport; + bool wait; + + value = get_config_value("gdb.port"); + if (value == NULL) + return; + sport = strdup(value); + if (sport == NULL) + errx(4, "Failed to allocate memory"); + + wait = get_config_bool_default("gdb.wait", false); + + saddr = get_config_value("gdb.address"); + if (saddr == NULL) { +#if defined(INET) + saddr = "0.0.0.0"; +#elif defined(INET6) + saddr = "[::]"; +#endif + } - debug("==> starting on %d, %swaiting\n", sport, wait ? "" : "not "); + debug("==> starting on %s:%s, %swaiting\n", + saddr, sport, wait ? "" : "not "); error = pthread_mutex_init(&gdb_lock, NULL); if (error != 0) @@ -1832,20 +1857,23 @@ init_gdb(struct vmctx *_ctx, int sport, bool wait) if (error != 0) errc(1, error, "gdb cv init"); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_PASSIVE; + + if (getaddrinfo(saddr, sport, &hints, &gdbaddr) != 0) + err(1, "gdb address resolve"); + ctx = _ctx; - s = socket(PF_INET, SOCK_STREAM, 0); + s = socket(gdbaddr->ai_family, gdbaddr->ai_socktype, 0); if (s < 0) err(1, "gdb socket create"); optval = 1; (void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); - sin.sin_len = sizeof(sin); - sin.sin_family = AF_INET; - sin.sin_addr.s_addr = htonl(INADDR_ANY); - sin.sin_port = htons(sport); - - if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) + if (bind(s, gdbaddr->ai_addr, gdbaddr->ai_addrlen) < 0) err(1, "gdb socket bind"); if (listen(s, 1) < 0) @@ -1874,4 +1902,6 @@ init_gdb(struct vmctx *_ctx, int sport, bool wait) #endif mevent_add(s, EVF_READ, new_connection, NULL); gdb_active = true; + freeaddrinfo(gdbaddr); + free(sport); } diff --git a/usr.sbin/bhyve/gdb.h b/usr.sbin/bhyve/gdb.h index 93396c1c6705..c5fa522c63e0 100644 --- a/usr.sbin/bhyve/gdb.h +++ b/usr.sbin/bhyve/gdb.h @@ -34,6 +34,6 @@ void gdb_cpu_add(int vcpu); void gdb_cpu_breakpoint(int vcpu, struct vm_exit *vmexit); void gdb_cpu_mtrap(int vcpu); void gdb_cpu_suspend(int vcpu); -void init_gdb(struct vmctx *ctx, int sport, bool wait); +void init_gdb(struct vmctx *ctx); #endif /* !__GDB_H__ */