Try to fix thread name truncation on non-Windows.

llvm-svn: 296976
This commit is contained in:
Zachary Turner 2017-03-04 18:53:09 +00:00
parent 898c241b26
commit 1f004c43d2
4 changed files with 17 additions and 8 deletions

View File

@ -139,7 +139,7 @@ void llvm_execute_on_thread(void (*UserFn)(void *), void *UserData,
/// \brief Get the maximum length of a thread name on this platform. /// \brief Get the maximum length of a thread name on this platform.
/// A value of 0 means there is no limit. /// A value of 0 means there is no limit.
constexpr uint32_t get_max_thread_name_length(); uint32_t get_max_thread_name_length();
/// \brief Set the name of the current thread. Setting a thread's name can /// \brief Set the name of the current thread. Setting a thread's name can
/// be helpful for enabling useful diagnostics under a debugger or when /// be helpful for enabling useful diagnostics under a debugger or when

View File

@ -49,6 +49,8 @@ unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
uint64_t llvm::get_threadid() { return 0; } uint64_t llvm::get_threadid() { return 0; }
uint32_t llvm::get_max_thread_name_length() { return 0; }
void llvm::set_thread_name(const Twine &Name) {} void llvm::set_thread_name(const Twine &Name) {}
void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); } void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }

View File

@ -106,7 +106,7 @@ uint64_t llvm::get_threadid() {
} }
constexpr uint32_t llvm::get_max_thread_name_length() { static constexpr uint32_t get_max_thread_name_length_impl() {
#if defined(__NetBSD__) #if defined(__NetBSD__)
return PTHREAD_MAX_NAMELEN_NP; return PTHREAD_MAX_NAMELEN_NP;
#elif defined(__APPLE__) #elif defined(__APPLE__)
@ -124,6 +124,10 @@ constexpr uint32_t llvm::get_max_thread_name_length() {
#endif #endif
} }
uint32_t llvm::get_max_thread_name_length() {
return get_max_thread_name_length_impl();
}
void llvm::set_thread_name(const Twine &Name) { void llvm::set_thread_name(const Twine &Name) {
// Make sure the input is null terminated. // Make sure the input is null terminated.
SmallString<64> Storage; SmallString<64> Storage;
@ -134,6 +138,7 @@ void llvm::set_thread_name(const Twine &Name) {
// terminated, but additionally the end of a long thread name will usually // terminated, but additionally the end of a long thread name will usually
// be more unique than the beginning, since a common pattern is for similar // be more unique than the beginning, since a common pattern is for similar
// threads to share a common prefix. // threads to share a common prefix.
if (get_max_thread_name_length() > 0)
NameStr = NameStr.take_back(get_max_thread_name_length()); NameStr = NameStr.take_back(get_max_thread_name_length());
(void)NameStr; (void)NameStr;
#if defined(__linux__) #if defined(__linux__)
@ -192,15 +197,17 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
free(kp); free(kp);
return; return;
#elif defined(__NetBSD__) #elif defined(__NetBSD__)
char buf[get_max_thread_name_length()]; constexpr uint32_t len = get_max_thread_name_length_impl();
::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP); char buf[len];
::pthread_getname_np(::pthread_self(), buf, len);
Name.append(buf, buf + strlen(buf)); Name.append(buf, buf + strlen(buf));
#elif defined(__linux__) #elif defined(__linux__)
#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
#if HAVE_PTHREAD_GETNAME_NP #if HAVE_PTHREAD_GETNAME_NP
char Buffer[get_max_thread_name_length()]; constexpr uint32_t len = get_max_thread_name_length_impl();
if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN)) char Buffer[len];
if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
Name.append(Buffer, Buffer + strlen(Buffer)); Name.append(Buffer, Buffer + strlen(Buffer));
#endif #endif
#endif #endif

View File

@ -59,7 +59,7 @@ uint64_t llvm::get_threadid() {
return uint64_t(::GetCurrentThreadId()); return uint64_t(::GetCurrentThreadId());
} }
constexpr uint32_t llvm::get_max_thread_name_length() { return 0; } uint32_t llvm::get_max_thread_name_length() { return 0; }
void llvm::set_thread_name(const Twine &Name) { void llvm::set_thread_name(const Twine &Name) {
#if defined(_MSC_VER) #if defined(_MSC_VER)