Skip to content

Commit a663812

Browse files
committed
8368124: Show useful thread names in ASAN reports
Reviewed-by: dholmes, mbaesken
1 parent ca03080 commit a663812

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

src/hotspot/os/linux/os_linux.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ address os::Linux::_initial_thread_stack_bottom = nullptr;
160160
uintptr_t os::Linux::_initial_thread_stack_size = 0;
161161

162162
int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = nullptr;
163-
int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = nullptr;
164163
pthread_t os::Linux::_main_thread;
165164
bool os::Linux::_supports_fast_thread_cpu_time = false;
166165
const char * os::Linux::_libc_version = nullptr;
@@ -4371,10 +4370,6 @@ void os::init(void) {
43714370
// _main_thread points to the thread that created/loaded the JVM.
43724371
Linux::_main_thread = pthread_self();
43734372

4374-
// retrieve entry point for pthread_setname_np
4375-
Linux::_pthread_setname_np =
4376-
(int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");
4377-
43784373
check_pax();
43794374

43804375
// Check the availability of MADV_POPULATE_WRITE.
@@ -4851,14 +4846,24 @@ uint os::processor_id() {
48514846
}
48524847

48534848
void os::set_native_thread_name(const char *name) {
4854-
if (Linux::_pthread_setname_np) {
4855-
char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
4856-
(void) os::snprintf(buf, sizeof(buf), "%s", name);
4857-
buf[sizeof(buf) - 1] = '\0';
4858-
const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
4859-
// ERANGE should not happen; all other errors should just be ignored.
4860-
assert(rc != ERANGE, "pthread_setname_np failed");
4861-
}
4849+
char buf[16]; // according to glibc manpage, 16 chars incl. '/0'
4850+
// We may need to truncate the thread name. Since a common pattern
4851+
// for thread names is to be both longer than 15 chars and have a
4852+
// trailing number ("DispatcherWorkerThread21", "C2 CompilerThread#54" etc),
4853+
// we preserve the end of the thread name by truncating the middle
4854+
// (e.g. "Dispatc..read21").
4855+
const size_t len = strlen(name);
4856+
if (len < sizeof(buf)) {
4857+
strcpy(buf, name);
4858+
} else {
4859+
(void) os::snprintf(buf, sizeof(buf), "%.7s..%.6s", name, name + len - 6);
4860+
}
4861+
// Note: we use the system call here instead of calling pthread_setname_np
4862+
// since this is the only way to make ASAN aware of our thread names. Even
4863+
// though ASAN intercepts both prctl and pthread_setname_np, it only processes
4864+
// the thread name given to the former.
4865+
int rc = prctl(PR_SET_NAME, buf);
4866+
assert(rc == 0, "prctl(PR_SET_NAME) failed");
48624867
}
48634868

48644869
////////////////////////////////////////////////////////////////////////////////

src/hotspot/os/linux/os_linux.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class os::Linux {
3333
friend class os;
3434

3535
static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
36-
static int (*_pthread_setname_np)(pthread_t, const char*);
3736

3837
static address _initial_thread_stack_bottom;
3938
static uintptr_t _initial_thread_stack_size;

src/hotspot/share/utilities/stringUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "jvm_io.h"
2626
#include "memory/allocation.hpp"
27+
#include "runtime/os.hpp"
2728
#include "utilities/debug.hpp"
2829
#include "utilities/stringUtils.hpp"
2930

src/hotspot/share/utilities/stringUtils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it

test/hotspot/gtest/runtime/test_os_linux.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "unittest.hpp"
3636

3737
#include <sys/mman.h>
38+
#include <sys/prctl.h>
3839

3940
static bool using_explicit_hugepages() { return UseLargePages && !UseTransparentHugePages; }
4041

@@ -468,4 +469,27 @@ TEST_VM(os_linux, glibc_mallinfo_wrapper) {
468469
#endif // ADDRESS_SANITIZER
469470
#endif // __GLIBC__
470471

472+
static void test_set_thread_name(const char* name, const char* expected) {
473+
os::set_native_thread_name(name);
474+
char buf[16];
475+
int rc = prctl(PR_GET_NAME, buf);
476+
ASSERT_EQ(0, rc);
477+
ASSERT_STREQ(buf, expected);
478+
}
479+
480+
TEST_VM(os_linux, set_thread_name) {
481+
char buf[16];
482+
// retrieve current name
483+
int rc = prctl(PR_GET_NAME, buf);
484+
ASSERT_EQ(0, rc);
485+
486+
test_set_thread_name("shortname", "shortname");
487+
test_set_thread_name("012345678901234", "012345678901234");
488+
test_set_thread_name("0123456789012345", "0123456..012345");
489+
test_set_thread_name("MyAllocationWorkerThread22", "MyAlloc..read22");
490+
491+
// restore current name
492+
test_set_thread_name(buf, buf);
493+
}
494+
471495
#endif // LINUX

0 commit comments

Comments
 (0)