Path: blob/main/sys/contrib/openzfs/lib/libspl/assert.c
48378 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2008 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/25/*26* Copyright (c) 2024, Rob Norris <[email protected]>27*/2829#include <assert.h>30#include <pthread.h>31#include <sys/backtrace.h>3233#if defined(__linux__)34#include <errno.h>35#include <sys/prctl.h>36#ifdef HAVE_GETTID37#define libspl_gettid() gettid()38#else39#include <sys/syscall.h>40#define libspl_gettid() ((pid_t)syscall(__NR_gettid))41#endif42#define libspl_getprogname() (program_invocation_short_name)43#define libspl_getthreadname(buf, len) \44prctl(PR_GET_NAME, (unsigned long)(buf), 0, 0, 0)45#elif defined(__FreeBSD__) || defined(__APPLE__)46#if !defined(__APPLE__)47#include <pthread_np.h>48#define libspl_gettid() pthread_getthreadid_np()49#endif50#define libspl_getprogname() getprogname()51#define libspl_getthreadname(buf, len) \52pthread_getname_np(pthread_self(), buf, len);53#endif5455#if defined(__APPLE__)56static inline uint64_t57libspl_gettid(void)58{59uint64_t tid;6061if (pthread_threadid_np(NULL, &tid) != 0)62tid = 0;6364return (tid);65}66#endif6768static boolean_t libspl_assert_ok = B_FALSE;6970void71libspl_set_assert_ok(boolean_t val)72{73libspl_assert_ok = val;74}7576static pthread_mutex_t assert_lock = PTHREAD_MUTEX_INITIALIZER;7778/* printf version of libspl_assert */79void80libspl_assertf(const char *file, const char *func, int line,81const char *format, ...)82{83pthread_mutex_lock(&assert_lock);8485va_list args;86char tname[64];8788libspl_getthreadname(tname, sizeof (tname));8990fprintf(stderr, "ASSERT at %s:%d:%s()\n", file, line, func);9192va_start(args, format);93vfprintf(stderr, format, args);94va_end(args);9596fprintf(stderr, "\n"97" PID: %-8u COMM: %s\n"98#if defined(__APPLE__)99" TID: %-8" PRIu64 " NAME: %s\n",100#else101" TID: %-8u NAME: %s\n",102#endif103getpid(), libspl_getprogname(),104libspl_gettid(), tname);105106libspl_backtrace(STDERR_FILENO);107108#if !__has_feature(attribute_analyzer_noreturn) && !defined(__COVERITY__)109if (libspl_assert_ok) {110pthread_mutex_unlock(&assert_lock);111return;112}113#endif114abort();115}116117118