From nobody Thu Mar 21 15:25:28 2024 X-Original-To: dev-commits-src-all@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 4V0q604pn3z5Ds97; Thu, 21 Mar 2024 15:25:28 +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 4V0q603hj0z4k4R; Thu, 21 Mar 2024 15:25:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711034728; 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=EG9Hhd34uixVhmd2meRykRVbsbzTcTxfjAqPBGSEndw=; b=WKe6kpdGs97hcQsY7o/4Ebw8n/Zt8YejTjO9qY/JLdAm/qRDR4SavbkVOq6nzlQfBeqoOW 97AFIo8XDJR15t0qWTIcfXAIqAxsFRMTwG6CX98XLZ7+I6SNAB14zHkJMtdtRbYQan7yWu EC/X7GsiX2iLgsKRsVo6BYUtWLaO3tcEg3Haw6HErpGNpN+Qo5/457j/9NJ47vFJ5BV6jF Zih0TsFwFsTj96s0P3ridy9PvswQ+4X4TTtMIBU1Az8uymAugVXtzAZGVYwYiMUpkhXXFR KMF7SpdKIY7AYkrDS6G306y2GLFN0zS7KdXRrHVNzDJDYybJ5HU9F1+UQ0feEQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711034728; a=rsa-sha256; cv=none; b=CveaApPThOd8OsUGo4ilq74eoRWxd08AK9Vu5f/amhwaEsx7yVEGEFJlTRa7vz2dk6pamW Pi1y6S9sdC+s1TcbMy62pX9Htx+1mo/x73o+fHFY2DzMd0L0a4dgGwoC5Fup3GDCJX3kva 1BEa5D14A+mhCv0Lm6ujbwy2TFTJ3v+iaGnCj9G3dqD3eyBve47wXFsSlUxx1MwyVLutFf 6fsPtmFbWJ7ZS6XAfWc4qkmJ/TaV8qCGFR3TC3vKx+CuZ4p6gqwgzRXB522OzSTHT9cm3Y fmhz2D9pVHxBDzEJx/7u/U96ywJ9gflo4M6BW8nUvYrrY5VDo83CMv9ydZDMKw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711034728; 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=EG9Hhd34uixVhmd2meRykRVbsbzTcTxfjAqPBGSEndw=; b=ETBGus2F+11iJNIBf1JIlLBQYsqkVVI0yPxnyybamrIcTyX4boqCJ/Ha5pJVRoEj6Wgah5 /Q4oTGWMQD/WxA4hODP+KUiZfDgYV/RscdiqbuCgSGe3UCe1wOX7AIcEf2cs5xDAWNMIQJ 8+ohaIV4TyHuI2N2ap59myPMMsRSijTMsXxm++L13HmpO4BrqOXLDS17KWOlayNUHD1caT TtkqLD/Xbkz3jJJqdOJtXWO/bk7PK8e1V0RSdmXCXytw86UdB9vSt1jeLWSFR2tbSkLIIe pHlTgB3ItfPlSe9LYQTbFHNs8nWMyPNAFuJ4dCvCvWUzZSlUkWDZuaPD52p4rw== 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 4V0q6030Zxzh2B; Thu, 21 Mar 2024 15:25:28 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 42LFPS9t055989; Thu, 21 Mar 2024 15:25:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42LFPS1D055986; Thu, 21 Mar 2024 15:25:28 GMT (envelope-from git) Date: Thu, 21 Mar 2024 15:25:28 GMT Message-Id: <202403211525.42LFPS1D055986@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: 83a426d13a6a - main - KASSERT(9): add assertion message guidelines List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 83a426d13a6a384e63e75d8252c03dd40af3817e Auto-Submitted: auto-generated The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=83a426d13a6a384e63e75d8252c03dd40af3817e commit 83a426d13a6a384e63e75d8252c03dd40af3817e Author: Mitchell Horne AuthorDate: 2024-03-21 14:54:49 +0000 Commit: Mitchell Horne CommitDate: 2024-03-21 15:24:16 +0000 KASSERT(9): add assertion message guidelines Add some text describing how to create useful assertion messages. Improve and add to the EXAMPLES. See the discussion prompting this on -hackers: https://mail-archive.freebsd.org/cgi/mid.cgi?57o4rnnq-013s-3nsn-59n5-4ssn1pq81s94 Reviewed by: emaste Discussed with: imp, bz MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D44434 --- share/man/man9/KASSERT.9 | 72 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/share/man/man9/KASSERT.9 b/share/man/man9/KASSERT.9 index 1d28ce80c9fb..8d9dd98014a8 100644 --- a/share/man/man9/KASSERT.9 +++ b/share/man/man9/KASSERT.9 @@ -2,7 +2,7 @@ .\" .\" Copyright (c) 2000 Jonathan M. Bresler .\" All rights reserved. -.\" Copyright (c) 2023 The FreeBSD Foundation +.\" Copyright (c) 2023-2024 The FreeBSD Foundation .\" .\" Portions of this documentation were written by Mitchell Horne .\" under sponsorship from the FreeBSD Foundation. @@ -29,7 +29,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd March 16, 2023 +.Dd March 19, 2024 .Dt KASSERT 9 .Os .Sh NAME @@ -93,8 +93,37 @@ The macro (read as: "must-pass") is a convenience wrapper around .Fn KASSERT -that automatically generates a sensible assertion message including file and -line information. +that automatically generates a simple assertion message including file and line +information. +.Ss Assertion Guidelines +When adding new assertions, keep in mind their primary purpose: to aid in +identifying and debugging of complex error conditions. +.Pp +The panic messages resulting from assertion failures should be useful without +the resulting kernel dump; the message may be included in a bug report, and +should contain the relevant information needed to discern how the assertion was +violated. +This is especially important when the error condition is difficult or +impossible for the developer to reproduce locally. +.Pp +Therefore, assertions should adhere to the following guidelines: +.Bl -enum +.It +Whenever possible, the value of a runtime variable checked by an assertion +condition should appear in its message. +.It +Unrelated conditions must appear in separate assertions. +.It +Multiple related conditions should be distinguishable (e.g. by value), or split +into separate assertions. +.It +When in doubt, print more information, not less. +.El +.Pp +Combined, this gives greater clarity into the exact cause of an assertion +panic; see +.Sx EXAMPLES +below. .Sh EXAMPLES A hypothetical .Vt struct foo @@ -106,11 +135,19 @@ foo_dealloc(struct foo *fp) { KASSERT((fp->foo_flags & FOO_ACTIVE) == 0, - ("%s: fp %p is still active", __func__, fp)); + ("%s: fp %p is still active, flags=%x", __func__, fp, + fp->foo_flags)); ... } .Ed .Pp +This assertion provides the full flag set for the object, as well as the memory +pointer, which may be used by a debugger to examine the object in detail +.Po +for example with a 'show foo' command in +.Xr ddb 4 +.Pc . +.Pp The assertion .Bd -literal -offset indent MPASS(td == curthread); @@ -121,6 +158,31 @@ message: .Bd -literal -offset indent panic: Assertion td == curthread failed at foo.c:87 .Ed +.Pp +This is a simple condition, and the message provides enough information to +investigate the failure. +.Pp +The assertion +.Bd -literal -offset indent +MPASS(td == curthread && (sz >= SIZE_MIN && sz <= SIZE_MAX)); +.Ed +.Pp +is +.Em NOT +useful enough. +The message doesn't indicate which part of the assertion was violated, nor +does it report the value of +.Dv sz , +which may be critical to understanding +.Em why +the assertion failed. +.Pp +According to the guidelines above, this would be correctly expressed as: +.Bd -literal -offset indent +MPASS(td == curthread); +KASSERT(sz >= SIZE_MIN && sz <= SIZE_MAX, + ("invalid size argument: %u", sz)); +.Ed .Sh SEE ALSO .Xr panic 9 .Sh AUTHORS