1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23eda14cbcSMatt Macy * Use is subject to license terms.
24eda14cbcSMatt Macy */
25b985c9caSMartin Matuska /*
26b985c9caSMartin Matuska * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
27b985c9caSMartin Matuska */
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy #include <assert.h>
30b985c9caSMartin Matuska #include <pthread.h>
31*aca928a5SMartin Matuska #include <sys/backtrace.h>
32b985c9caSMartin Matuska
33b985c9caSMartin Matuska #if defined(__linux__)
34b985c9caSMartin Matuska #include <errno.h>
35b985c9caSMartin Matuska #include <sys/prctl.h>
36b985c9caSMartin Matuska #ifdef HAVE_GETTID
37b985c9caSMartin Matuska #define libspl_gettid() gettid()
38b985c9caSMartin Matuska #else
39b985c9caSMartin Matuska #include <sys/syscall.h>
40b985c9caSMartin Matuska #define libspl_gettid() ((pid_t)syscall(__NR_gettid))
41b985c9caSMartin Matuska #endif
42b985c9caSMartin Matuska #define libspl_getprogname() (program_invocation_short_name)
43b985c9caSMartin Matuska #define libspl_getthreadname(buf, len) \
44b985c9caSMartin Matuska prctl(PR_GET_NAME, (unsigned long)(buf), 0, 0, 0)
453c1be0b2SMartin Matuska #elif defined(__FreeBSD__) || defined(__APPLE__)
463c1be0b2SMartin Matuska #if !defined(__APPLE__)
47b985c9caSMartin Matuska #include <pthread_np.h>
48b985c9caSMartin Matuska #define libspl_gettid() pthread_getthreadid_np()
493c1be0b2SMartin Matuska #endif
50b985c9caSMartin Matuska #define libspl_getprogname() getprogname()
51b985c9caSMartin Matuska #define libspl_getthreadname(buf, len) \
52b985c9caSMartin Matuska pthread_getname_np(pthread_self(), buf, len);
53b985c9caSMartin Matuska #endif
54b985c9caSMartin Matuska
55*aca928a5SMartin Matuska #if defined(__APPLE__)
56*aca928a5SMartin Matuska static inline uint64_t
libspl_gettid(void)57*aca928a5SMartin Matuska libspl_gettid(void)
58b985c9caSMartin Matuska {
59*aca928a5SMartin Matuska uint64_t tid;
60b985c9caSMartin Matuska
61*aca928a5SMartin Matuska if (pthread_threadid_np(NULL, &tid) != 0)
62*aca928a5SMartin Matuska tid = 0;
63b985c9caSMartin Matuska
64*aca928a5SMartin Matuska return (tid);
65b985c9caSMartin Matuska }
66b985c9caSMartin Matuska #endif
67eda14cbcSMatt Macy
68c03c5b1cSMartin Matuska static boolean_t libspl_assert_ok = B_FALSE;
69c03c5b1cSMartin Matuska
70c03c5b1cSMartin Matuska void
libspl_set_assert_ok(boolean_t val)71c03c5b1cSMartin Matuska libspl_set_assert_ok(boolean_t val)
72c03c5b1cSMartin Matuska {
73c03c5b1cSMartin Matuska libspl_assert_ok = val;
74c03c5b1cSMartin Matuska }
75eda14cbcSMatt Macy
76b985c9caSMartin Matuska static pthread_mutex_t assert_lock = PTHREAD_MUTEX_INITIALIZER;
77b985c9caSMartin Matuska
78eda14cbcSMatt Macy /* printf version of libspl_assert */
79eda14cbcSMatt Macy void
libspl_assertf(const char * file,const char * func,int line,const char * format,...)80eda14cbcSMatt Macy libspl_assertf(const char *file, const char *func, int line,
81eda14cbcSMatt Macy const char *format, ...)
82eda14cbcSMatt Macy {
83b985c9caSMartin Matuska pthread_mutex_lock(&assert_lock);
84b985c9caSMartin Matuska
85eda14cbcSMatt Macy va_list args;
86b985c9caSMartin Matuska char tname[64];
87b985c9caSMartin Matuska
88b985c9caSMartin Matuska libspl_getthreadname(tname, sizeof (tname));
89b985c9caSMartin Matuska
90b985c9caSMartin Matuska fprintf(stderr, "ASSERT at %s:%d:%s()\n", file, line, func);
91eda14cbcSMatt Macy
92eda14cbcSMatt Macy va_start(args, format);
93eda14cbcSMatt Macy vfprintf(stderr, format, args);
94eda14cbcSMatt Macy va_end(args);
95be181ee2SMartin Matuska
96b985c9caSMartin Matuska fprintf(stderr, "\n"
97b985c9caSMartin Matuska " PID: %-8u COMM: %s\n"
983c1be0b2SMartin Matuska #if defined(__APPLE__)
993c1be0b2SMartin Matuska " TID: %-8" PRIu64 " NAME: %s\n",
1003c1be0b2SMartin Matuska #else
101b985c9caSMartin Matuska " TID: %-8u NAME: %s\n",
1023c1be0b2SMartin Matuska #endif
103b985c9caSMartin Matuska getpid(), libspl_getprogname(),
104b985c9caSMartin Matuska libspl_gettid(), tname);
105b985c9caSMartin Matuska
106*aca928a5SMartin Matuska libspl_backtrace(STDERR_FILENO);
107b985c9caSMartin Matuska
108be181ee2SMartin Matuska #if !__has_feature(attribute_analyzer_noreturn) && !defined(__COVERITY__)
10916038816SMartin Matuska if (libspl_assert_ok) {
110b985c9caSMartin Matuska pthread_mutex_unlock(&assert_lock);
111eda14cbcSMatt Macy return;
112eda14cbcSMatt Macy }
113be181ee2SMartin Matuska #endif
114eda14cbcSMatt Macy abort();
115eda14cbcSMatt Macy }
116