@@ -160,7 +160,6 @@ address os::Linux::_initial_thread_stack_bottom = nullptr;
160160uintptr_t os::Linux::_initial_thread_stack_size = 0 ;
161161
162162int (*os::Linux::_pthread_getcpuclockid)(pthread_t , clockid_t *) = nullptr ;
163- int (*os::Linux::_pthread_setname_np)(pthread_t , const char *) = nullptr ;
164163pthread_t os::Linux::_main_thread;
165164bool os::Linux::_supports_fast_thread_cpu_time = false ;
166165const 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
48534848void 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// //////////////////////////////////////////////////////////////////////////////
0 commit comments