xref: /freebsd/lib/libc/gen/uexterror.3 (revision 95e33099c74d9fc7963968f052b7546ad3040317)
1*95e33099SBrooks Davis.\" SPDX-License-Identifier: BSD-2-Clause
2*95e33099SBrooks Davis.\"
3*95e33099SBrooks Davis.\" Copyright (c) 2025 The FreeBSD Foundation
4*95e33099SBrooks Davis.\" Copyright (c) 2026 Capabilities Limited
5*95e33099SBrooks Davis.\"
6*95e33099SBrooks Davis.\" This documentation was written by
7*95e33099SBrooks Davis.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship
8*95e33099SBrooks Davis.\" from the FreeBSD Foundation.
9*95e33099SBrooks Davis.\"
10*95e33099SBrooks Davis.\" This software was developed by Capabilities Limited with funding from
11*95e33099SBrooks Davis.\" Innovate UK and the Department for Science, Innovation and Technology
12*95e33099SBrooks Davis.\" for the adoption and diffusion of CHERI technology under project
13*95e33099SBrooks Davis.\" 10168042 (“CheriBSD feature extraction, maturity, and testing”).
14*95e33099SBrooks Davis.\"
15*95e33099SBrooks Davis.Dd August 20, 2026
16*95e33099SBrooks Davis.Dt UEXTERROR 3
17*95e33099SBrooks Davis.Os
18*95e33099SBrooks Davis.Sh NAME
19*95e33099SBrooks Davis.Nm uexterror
20*95e33099SBrooks Davis.Nd provide extended error information from user-space code
21*95e33099SBrooks Davis.Sh SYNOPSIS
22*95e33099SBrooks Davis.Bd -literal -offset left -compact
23*95e33099SBrooks Davis#define UEXTERR_CATEGORY "path/to/this/file.c"
24*95e33099SBrooks Davis.Ed
25*95e33099SBrooks Davis.In uexterror.h
26*95e33099SBrooks Davis.Ft void
27*95e33099SBrooks Davis.Fn UEXTERROR "int error" "const char *msg" ...
28*95e33099SBrooks Davis.Sh DESCRIPTION
29*95e33099SBrooks DavisThe
30*95e33099SBrooks Davis.Nm
31*95e33099SBrooks Davisframework allows user-space code to report errors using the
32*95e33099SBrooks Davissame
33*95e33099SBrooks Davis.Xr exterror 9
34*95e33099SBrooks Davisinfrastructure the kernel uses to return additional information about an
35*95e33099SBrooks Daviserror along with the standard
36*95e33099SBrooks Davis.Xr errno 3
37*95e33099SBrooks Daviserror code, which is terse and often lacking context.
38*95e33099SBrooks Davis.Pp
39*95e33099SBrooks DavisThe terseness is especially visible with commonly overloaded error codes like
40*95e33099SBrooks Davis.Er EINVAL
41*95e33099SBrooks Davisor
42*95e33099SBrooks Davis.Er EIO ,
43*95e33099SBrooks Daviswhich occur at many places for a given syscall, or even
44*95e33099SBrooks Davisoutside the context of the current call.
45*95e33099SBrooks DavisIdentifying the specific cause for the returned error using only the
46*95e33099SBrooks Davis.Va errno
47*95e33099SBrooks Davisvalue requires searching for all instances that the error is returned
48*95e33099SBrooks Davisin user-space and the kernel and trying to guess which is the most
49*95e33099SBrooks Davislikely code path to have returned the error.
50*95e33099SBrooks Davis.Nm
51*95e33099SBrooks Davisattaches additional data to the error itself
52*95e33099SBrooks Davisand records the error category and
53*95e33099SBrooks Davisthe source code file line number.
54*95e33099SBrooks DavisThe intent of
55*95e33099SBrooks Davis.Nm
56*95e33099SBrooks Davisis to make it easier for a user to identify the cause of the error.
57*95e33099SBrooks Davis.Sh USAGE
58*95e33099SBrooks DavisBefore
59*95e33099SBrooks Davis.Nm
60*95e33099SBrooks Daviscan be used in the given source .c file, the category of extended errors
61*95e33099SBrooks Davismust be defined.
62*95e33099SBrooks DavisThis can be done by setting the
63*95e33099SBrooks Davis.Va UEXTERR_CATEGORY
64*95e33099SBrooks Davismacro to a string containing the name of the file relative to
65*95e33099SBrooks Davis.Pa src/sys
66*95e33099SBrooks Davisdirectory.
67*95e33099SBrooks Davis.Pp
68*95e33099SBrooks DavisA typical code fragment to report an error is to set errno
69*95e33099SBrooks Davis.D1 errno = EINVAL;
70*95e33099SBrooks DavisAn extended error can augment the error code with additional information:
71*95e33099SBrooks Davis.D1 UEXTERROR(EINVAL, \[dq]Invalid length\[dq]));
72*95e33099SBrooks DavisThe error data and metadata is saved in the current thread storage.
73*95e33099SBrooks DavisThe metadata includes the category and the source file line number.
74*95e33099SBrooks Davis.Pp
75*95e33099SBrooks DavisArguments to the
76*95e33099SBrooks Davis.Fn UEXTERROR
77*95e33099SBrooks Davismacro:
78*95e33099SBrooks Davis.Bl -dash
79*95e33099SBrooks Davis.It
80*95e33099SBrooks DavisThe first argument to
81*95e33099SBrooks Davis.Fn UEXTERROR
82*95e33099SBrooks Davisis the errno error code.
83*95e33099SBrooks Davis.It
84*95e33099SBrooks DavisThe second argument is a constant string with the unbound lifetime,
85*95e33099SBrooks Daviswhich should tersely provide enough human-readable details about
86*95e33099SBrooks Davisthe error.
87*95e33099SBrooks Davis.It
88*95e33099SBrooks DavisThe
89*95e33099SBrooks Davis.Fn UEXTERROR
90*95e33099SBrooks Davismacro can take two optional uintptr_t or 64-bit integer arguments,
91*95e33099SBrooks Daviswhose meaning is specific to the subsystem.
92*95e33099SBrooks DavisThe format string may include up to two printf-like format
93*95e33099SBrooks Davisspecifiers to insert the optional argument values in the
94*95e33099SBrooks Davisuser output, which is done in userspace.
95*95e33099SBrooks Davis.Pp
96*95e33099SBrooks DavisThe format specifier must be for an character, integer, or pointer type.
97*95e33099SBrooks DavisNote that userspace printing assumes all
98*95e33099SBrooks Davis.Dt long Ns -derived
99*95e33099SBrooks Davistypes such as
100*95e33099SBrooks Davis.Dt size_t
101*95e33099SBrooks Davisare 64-bit and prints them accordingly.
102*95e33099SBrooks DavisSigned integer types should thus be cast to
103*95e33099SBrooks Davis.Dt int64_t
104*95e33099SBrooks Davisor similar to insure proper sign extension.
105*95e33099SBrooks Davis.El
106*95e33099SBrooks Davis.Pp
107*95e33099SBrooks DavisNote that unlike its kernel counterpart
108*95e33099SBrooks Davis.Xr EXTERROR 9
109*95e33099SBrooks Davisthe
110*95e33099SBrooks Davis.Nm
111*95e33099SBrooks Davismacro does not return a value and sets
112*95e33099SBrooks Davis.Xr errno 3
113*95e33099SBrooks Davisdirectly.
114*95e33099SBrooks Davis.Pp
115*95e33099SBrooks DavisThe name of the file and strings passed as the second argument are
116*95e33099SBrooks Davisretained in the program or library text as long as the translation unit
117*95e33099SBrooks Daviswas not compiled with the
118*95e33099SBrooks Davis.Cd NO_UEXTERR_STRINGS
119*95e33099SBrooks Davismacro defined.
120*95e33099SBrooks Davis.Sh SEE ALSO
121*95e33099SBrooks Davis.Xr errno 3 ,
122*95e33099SBrooks Davis.Xr err 3 ,
123*95e33099SBrooks Davis.Xr uexterr_gettext 3 ,
124*95e33099SBrooks Davis.Xr exterror 9
125*95e33099SBrooks Davis.Sh HISTORY
126*95e33099SBrooks DavisThe
127*95e33099SBrooks Davis.Nm
128*95e33099SBrooks Davisfacility was introduced in
129*95e33099SBrooks Davis.Fx 16.0 .
130*95e33099SBrooks Davis.Sh AUTHORS
131*95e33099SBrooks DavisThis software and this manual page were developed by Capabilities
132*95e33099SBrooks DavisLimited with funding from Innovate UK and the Department for Science,
133*95e33099SBrooks DavisInnovation and Technology for the adoption and diffusion of CHERI
134*95e33099SBrooks Davistechnology under project 10168042
135*95e33099SBrooks Davis.Pq Do CheriBSD feature extraction, maturity, and testing Dc .
136