The branch main has been updated by mckusick:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=6fd98877de633f5ec6f028e78d5a2d94527d63d0

commit 6fd98877de633f5ec6f028e78d5a2d94527d63d0
Author:     Kirk McKusick <[email protected]>
AuthorDate: 2026-02-26 06:20:12 +0000
Commit:     Kirk McKusick <[email protected]>
CommitDate: 2026-02-26 06:20:12 +0000

    Refinements to the output when the EXTERROR_VERBOSE environment is set
    
    When kernel external errors are available they are included in the
    err(3) library function messages. In addition to the extended error
    itself, the kernel also tracks the kernel file and line number at
    which the error was generated. This additional information is not
    included in the err(3) messages unless the EXTERROR_VERBOSE environment
    variable is present. Currently, when EXTERROR_VERBOSE is present,
    all the internal extended error information associated with the
    error is printed most of which is redundant with the formatted error
    message printed by err(3). This change will add only the kernel
    file and line number to the err(3) message when EXTERROR_VERBOSE
    is present and set to "brief".
    
    Sample output with bad protection bits to mmap:
    
    guest_16 % ./Example bigfile
    Example: mmap bigfile: Invalid argument (unknown PROT bits 0x8)
    
    guest_16 % setenv EXTERROR_VERBOSE
    guest_16 % ./Example bigfile
    Example: mmap bigfile: Invalid argument (unknown PROT bits 0x8 errno 22 
category 1 (src sys/vm/vm_mmap.c:200) p1 0x8 p2 0)
    
    guest_16 % setenv EXTERROR_VERBOSE brief
    guest_16 % ./Example bigfile
    Example: mmap bigfile: Invalid argument (unknown PROT bits 0x8 (src 
sys/vm/vm_mmap.c:200))
    
    Reviewed by: kib
    Differential Revision: https://reviews.freebsd.org/D55494
    MFC-after:    1 week
    Sponsored-by: Netflix
---
 lib/libc/gen/err.3            |  8 +++++++-
 lib/libc/gen/uexterr_format.c | 28 ++++++++++++++++++++--------
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/lib/libc/gen/err.3 b/lib/libc/gen/err.3
index 70a214152a19..2df03c2dcdde 100644
--- a/lib/libc/gen/err.3
+++ b/lib/libc/gen/err.3
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd March 29, 2012
+.Dd February 23, 2026
 .Dt ERR 3
 .Os
 .Sh NAME
@@ -128,6 +128,12 @@ environment variable is present, an additional report is 
printed.
 The report includes at least the category of the error, the name of
 the source file (if known by the used version of libc),
 the source line number, and parameters.
+If the
+.Ev EXTERROR_VERBOSE
+environment variable is present and set to "brief",
+the report adds only the name of
+the source file (if known by the used version of libc)
+and the source line number.
 The format of the printed string is not contractual and might be changed.
 .Pp
 In the case of the
diff --git a/lib/libc/gen/uexterr_format.c b/lib/libc/gen/uexterr_format.c
index c1974f3c361a..308220adea1f 100644
--- a/lib/libc/gen/uexterr_format.c
+++ b/lib/libc/gen/uexterr_format.c
@@ -35,13 +35,16 @@ static const char exterror_verbose_name[] = 
"EXTERROR_VERBOSE";
 enum exterr_verbose_state {
        EXTERR_VERBOSE_UNKNOWN = 100,
        EXTERR_VERBOSE_DEFAULT,
-       EXTERR_VERBOSE_ALLOW,
+       EXTERR_VERBOSE_ALLOW_BRIEF,
+       EXTERR_VERBOSE_ALLOW_FULL,
 };
 static enum exterr_verbose_state exterror_verbose = EXTERR_VERBOSE_UNKNOWN;
 
 static void
 exterr_verbose_init(void)
 {
+       const char *v;
+
        /*
         * No need to care about thread-safety, the result is
         * idempotent.
@@ -50,8 +53,9 @@ exterr_verbose_init(void)
                return;
        if (issetugid()) {
                exterror_verbose = EXTERR_VERBOSE_DEFAULT;
-       } else if (getenv(exterror_verbose_name) != NULL) {
-               exterror_verbose = EXTERR_VERBOSE_ALLOW;
+       } else if ((v = getenv(exterror_verbose_name)) != NULL) {
+               exterror_verbose = strcmp(v, "brief") == 0 ?
+                   EXTERR_VERBOSE_ALLOW_BRIEF : EXTERR_VERBOSE_ALLOW_FULL;
        } else {
                exterror_verbose = EXTERR_VERBOSE_DEFAULT;
        }
@@ -78,13 +82,21 @@ __uexterr_format(const struct uexterror *ue, char *buf, 
size_t bufsz)
                strlcpy(buf, "", bufsz);
        }
 
-       if (exterror_verbose == EXTERR_VERBOSE_ALLOW || !has_msg) {
+       if (exterror_verbose > EXTERR_VERBOSE_DEFAULT || !has_msg) {
                char lbuf[128];
 
-               snprintf(lbuf, sizeof(lbuf),
-                   "errno %d category %u (src sys/%s:%u) p1 %#jx p2 %#jx",
-                   ue->error, ue->cat, cat_to_filename(ue->cat),
-                   ue->src_line, (uintmax_t)ue->p1, (uintmax_t)ue->p2);
+#define        SRC_FMT "(src sys/%s:%u)"
+               if (exterror_verbose == EXTERR_VERBOSE_ALLOW_BRIEF) {
+                       snprintf(lbuf, sizeof(lbuf), SRC_FMT,
+                            cat_to_filename(ue->cat), ue->src_line);
+               } else if (!has_msg ||
+                   exterror_verbose == EXTERR_VERBOSE_ALLOW_FULL) {
+                       snprintf(lbuf, sizeof(lbuf),
+                           "errno %d category %u " SRC_FMT " p1 %#jx p2 %#jx",
+                           ue->error, ue->cat, cat_to_filename(ue->cat),
+                           ue->src_line, (uintmax_t)ue->p1, (uintmax_t)ue->p2);
+               }
+#undef SRC_FMT
                if (has_msg)
                        strlcat(buf, " ", bufsz);
                strlcat(buf, lbuf, bufsz);

Reply via email to