git: b9c8a07d4dd9 - main - C runtime: enable extended error reporting from kernel

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 31 May 2025 19:52:54 UTC
The branch main has been updated by kib:

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

commit b9c8a07d4dd932aaa43a4ed4c2fe07ac3d901544
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-05-23 05:02:41 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-05-31 19:52:42 +0000

    C runtime: enable extended error reporting from kernel
    
    Reviewed by:    brooks
    Sponsored by:   The FreeBSD Foundation
    MFC after:      2 weeks
    Differential revision:  https://reviews.freebsd.org/D50483
---
 lib/libc/gen/Makefile.inc       |  1 +
 lib/libc/gen/uexterr_gettext.c  | 15 +++++++++++++++
 lib/libthr/thread/thr_create.c  |  4 ++++
 lib/libthr/thread/thr_init.c    |  3 +++
 lib/libthr/thread/thr_private.h |  3 +++
 5 files changed, 26 insertions(+)

diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
index 9a223f2f69e8..6416effeb568 100644
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -159,6 +159,7 @@ SRCS+= \
 	ttyname.c \
 	ttyslot.c \
 	ualarm.c \
+	uexterr_gettext.c \
 	ulimit.c \
 	uname.c \
 	unvis-compat.c \
diff --git a/lib/libc/gen/uexterr_gettext.c b/lib/libc/gen/uexterr_gettext.c
new file mode 100644
index 000000000000..1ee295556a4e
--- /dev/null
+++ b/lib/libc/gen/uexterr_gettext.c
@@ -0,0 +1,15 @@
+#include <sys/types.h>
+#include <sys/exterrvar.h>
+#include <exterr.h>
+#include <string.h>
+
+static struct uexterror uexterr = {
+	.ver = UEXTERROR_VER,
+};
+
+static void uexterr_ctr(void) __attribute__((constructor));
+static void
+uexterr_ctr(void)
+{
+	exterrctl(EXTERRCTL_ENABLE, 0, &uexterr);
+}
diff --git a/lib/libthr/thread/thr_create.c b/lib/libthr/thread/thr_create.c
index 84bbd36ed28d..ba2575d461e5 100644
--- a/lib/libthr/thread/thr_create.c
+++ b/lib/libthr/thread/thr_create.c
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <sys/rtprio.h>
 #include <sys/signalvar.h>
+#include <sys/exterrvar.h>
 #include <errno.h>
 #include <link.h>
 #include <stdlib.h>
@@ -285,6 +286,9 @@ thread_start(struct pthread *curthread)
 		curthread->attr.stacksize_attr;
 #endif
 
+	curthread->uexterr.ver = UEXTERROR_VER;
+	exterrctl(EXTERRCTL_ENABLE, 0, &curthread->uexterr);
+
 	/* Run the current thread's start routine with argument: */
 	_pthread_exit(curthread->start_routine(curthread->arg));
 
diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c
index 8855491b91cb..aef2281c5f22 100644
--- a/lib/libthr/thread/thr_init.c
+++ b/lib/libthr/thread/thr_init.c
@@ -433,6 +433,9 @@ init_main_thread(struct pthread *thread)
 	thread->unwind_stackend = _usrstack;
 #endif
 
+	thread->uexterr.ver = UEXTERROR_VER;
+	exterrctl(EXTERRCTL_ENABLE, EXTERRCTLF_FORCE, &thread->uexterr);
+
 	/* Others cleared to zero by thr_alloc() */
 }
 
diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h
index dc5be08a0760..11afd74eea16 100644
--- a/lib/libthr/thread/thr_private.h
+++ b/lib/libthr/thread/thr_private.h
@@ -40,6 +40,7 @@
 #include <sys/queue.h>
 #include <sys/param.h>
 #include <sys/cpuset.h>
+#include <sys/exterrvar.h>
 #include <machine/atomic.h>
 #include <errno.h>
 #include <limits.h>
@@ -576,6 +577,8 @@ struct pthread {
 
 	/* pthread_set/get_name_np */
 	char			*name;
+
+	struct uexterror	uexterr;
 };
 
 #define THR_SHOULD_GC(thrd) 						\