From owner-dev-commits-src-branches@freebsd.org Mon May 10 01:14:59 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 1A6FF628329; Mon, 10 May 2021 01:14:59 +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 4FdjlQ5B4Tz4Zd7; Mon, 10 May 2021 01:14:58 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 347A81B97C; Mon, 10 May 2021 01:14:58 +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 14A1Ewat028802; Mon, 10 May 2021 01:14:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 14A1EwEm028801; Mon, 10 May 2021 01:14:58 GMT (envelope-from git) Date: Mon, 10 May 2021 01:14:58 GMT Message-Id: <202105100114.14A1EwEm028801@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 32bfffcb9adf - stable/13 - gcore: add option to dump core using kernel facility MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 32bfffcb9adfe146f0e852d997717fd3982eda36 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 May 2021 01:14:59 -0000 The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=32bfffcb9adfe146f0e852d997717fd3982eda36 commit 32bfffcb9adfe146f0e852d997717fd3982eda36 Author: Konstantin Belousov AuthorDate: 2021-04-24 11:20:24 +0000 Commit: Konstantin Belousov CommitDate: 2021-05-10 01:03:19 +0000 gcore: add option to dump core using kernel facility (cherry picked from commit 73e8f06ac523ee4b530e17d50cc580dc366f7ad8) --- usr.bin/gcore/gcore.1 | 16 +++++++++-- usr.bin/gcore/gcore.c | 75 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/usr.bin/gcore/gcore.1 b/usr.bin/gcore/gcore.1 index 55db2aed3e3a..aa93a5ef1fe0 100644 --- a/usr.bin/gcore/gcore.1 +++ b/usr.bin/gcore/gcore.1 @@ -28,7 +28,7 @@ .\" @(#)gcore.1 8.2 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" -.Dd July 13, 2016 +.Dd April 24, 2021 .Dt GCORE 1 .Os .Sh NAME @@ -37,6 +37,7 @@ .Sh SYNOPSIS .Nm .Op Fl f +.Op Fl k .Op Fl c Ar core .Op Ar executable .Ar pid @@ -58,13 +59,24 @@ The following options are available: Write the core file to the specified file instead of .Dq Pa core. . .It Fl f -Dumps all available segments, excluding only malformed and undumpable segments. +Dumps all available segments, excluding only malformed and undumpable +segments. Unlike the default invocation, this flag dumps mappings of devices which may invalidate the state of device transactions or trigger other unexpected behavior. As a result, this flag should only be used when the behavior of the application and any devices it has mapped is fully understood and any side effects can be controlled or tolerated. +.It Fl k +Use the +.Xr ptrace 2 +.Dv PT_COREDUMP +kernel facility to write the core dump, instead of reading the process' +memory and constructing the dump file in +.Nm +itself. +This is faster, and the dump is written by the +same kernel code that writes core dumps upon fatal signals. .El .Sh FILES .Bl -tag -width /var/log/messages -compact diff --git a/usr.bin/gcore/gcore.c b/usr.bin/gcore/gcore.c index cbbfea82085b..8055193625f9 100644 --- a/usr.bin/gcore/gcore.c +++ b/usr.bin/gcore/gcore.c @@ -56,13 +56,16 @@ __FBSDID("$FreeBSD$"); */ #include +#include #include #include #include #include +#include #include #include +#include #include #include #include @@ -75,6 +78,7 @@ static void killed(int); static void usage(void) __dead2; static pid_t pid; +static bool kflag = false; SET_DECLARE(dumpset, struct dumpers); @@ -82,6 +86,7 @@ static int open_corefile(char *corefile) { char fname[MAXPATHLEN]; + int fd; if (corefile == NULL) { (void)snprintf(fname, sizeof(fname), "core.%d", pid); @@ -93,6 +98,45 @@ open_corefile(char *corefile) return (fd); } +static void +kcoredump(int fd, pid_t pid) +{ + struct ptrace_coredump pc; + int error, res, ret, waited; + + error = ptrace(PT_ATTACH, pid, NULL, 0); + if (error != 0) + err(1, "attach"); + + waited = waitpid(pid, &res, 0); + if (waited == -1) + err(1, "wait for STOP"); + + ret = 0; + memset(&pc, 0, sizeof(pc)); + pc.pc_fd = fd; + pc.pc_flags = (pflags & PFLAGS_FULL) != 0 ? PC_ALL : 0; + error = ptrace(PT_COREDUMP, pid, (void *)&pc, sizeof(pc)); + if (error == -1) { + warn("coredump"); + ret = 1; + } + + waited = waitpid(pid, &res, WNOHANG); + if (waited == -1) { + warn("wait after coredump"); + ret = 1; + } + + error = ptrace(PT_DETACH, pid, NULL, 0); + if (error == -1) { + warn("detach failed, check process status"); + ret = 1; + } + + exit(ret); +} + int main(int argc, char *argv[]) { @@ -104,7 +148,7 @@ main(int argc, char *argv[]) pflags = 0; corefile = NULL; - while ((ch = getopt(argc, argv, "c:f")) != -1) { + while ((ch = getopt(argc, argv, "c:fk")) != -1) { switch (ch) { case 'c': corefile = optarg; @@ -112,6 +156,9 @@ main(int argc, char *argv[]) case 'f': pflags |= PFLAGS_FULL; break; + case 'k': + kflag = true; + break; default: usage(); break; @@ -119,10 +166,26 @@ main(int argc, char *argv[]) } argv += optind; argc -= optind; + /* XXX we should check that the pid argument is really a number */ switch (argc) { case 1: pid = atoi(argv[0]); + break; + case 2: + binfile = argv[0]; + pid = atoi(argv[1]); + break; + default: + usage(); + } + + if (kflag) { + fd = open_corefile(corefile); + kcoredump(fd, pid); + } + + if (argc == 1) { name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_PATHNAME; @@ -131,13 +194,6 @@ main(int argc, char *argv[]) if (sysctl(name, 4, passpath, &len, NULL, 0) == -1) errx(1, "kern.proc.pathname failure"); binfile = passpath; - break; - case 2: - pid = atoi(argv[1]); - binfile = argv[0]; - break; - default: - usage(); } efd = open(binfile, O_RDONLY, 0); if (efd < 0) @@ -165,6 +221,7 @@ void usage(void) { - (void)fprintf(stderr, "usage: gcore [-c core] [executable] pid\n"); + (void)fprintf(stderr, + "usage: gcore [-kf] [-c core] [executable] pid\n"); exit(1); }