Skip to content

Commit 8be8f53

Browse files
robnlundman
authored andcommitted
libspl/assert: show process/task details in assert output
Makes it much easier to see what thing complained. Getting thread id, program name and thread name vary wildly between Linux and FreeBSD, so those are set up in macros. pthread_getname_np() did not appear in musl until very recently, but the same info has always been available via prctl(PR_GET_NAME), so we use that instead. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Rob Norris <[email protected]> Sponsored-by: https://despairlabs.com/sponsor/ Closes openzfs#16140
1 parent 97d1ad6 commit 8be8f53

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

config/user.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ AC_DEFUN([ZFS_AC_CONFIG_USER], [
3131
ZFS_AC_CONFIG_USER_MAKEDEV_IN_MKDEV
3232
ZFS_AC_CONFIG_USER_ZFSEXEC
3333
34-
AC_CHECK_FUNCS([execvpe issetugid mlockall strlcat strlcpy])
34+
AC_CHECK_FUNCS([execvpe issetugid mlockall strlcat strlcpy gettid])
3535
3636
AC_SUBST(RM)
3737
])

lib/libspl/assert.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,32 @@
2222
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2323
* Use is subject to license terms.
2424
*/
25+
/*
26+
* Copyright (c) 2024, Rob Norris <[email protected]>
27+
*/
2528

2629
#include <assert.h>
2730

31+
#if defined(__linux__)
32+
#include <errno.h>
33+
#include <sys/prctl.h>
34+
#ifdef HAVE_GETTID
35+
#define libspl_gettid() gettid()
36+
#else
37+
#include <sys/syscall.h>
38+
#define libspl_gettid() ((pid_t)syscall(__NR_gettid))
39+
#endif
40+
#define libspl_getprogname() (program_invocation_short_name)
41+
#define libspl_getthreadname(buf, len) \
42+
prctl(PR_GET_NAME, (unsigned long)(buf), 0, 0, 0)
43+
#elif defined(__FreeBSD__)
44+
#include <pthread_np.h>
45+
#define libspl_gettid() pthread_getthreadid_np()
46+
#define libspl_getprogname() getprogname()
47+
#define libspl_getthreadname(buf, len) \
48+
pthread_getname_np(pthread_self(), buf, len);
49+
#endif
50+
2851
static boolean_t libspl_assert_ok = B_FALSE;
2952

3053
void
@@ -39,13 +62,22 @@ libspl_assertf(const char *file, const char *func, int line,
3962
const char *format, ...)
4063
{
4164
va_list args;
65+
char tname[64];
66+
67+
libspl_getthreadname(tname, sizeof (tname));
68+
69+
fprintf(stderr, "ASSERT at %s:%d:%s()\n", file, line, func);
4270

4371
va_start(args, format);
4472
vfprintf(stderr, format, args);
45-
fprintf(stderr, "\n");
46-
fprintf(stderr, "ASSERT at %s:%d:%s()", file, line, func);
4773
va_end(args);
4874

75+
fprintf(stderr, "\n"
76+
" PID: %-8u COMM: %s\n"
77+
" TID: %-8u NAME: %s\n",
78+
getpid(), libspl_getprogname(),
79+
libspl_gettid(), tname);
80+
4981
#if !__has_feature(attribute_analyzer_noreturn) && !defined(__COVERITY__)
5082
if (libspl_assert_ok) {
5183
return;

0 commit comments

Comments
 (0)