Truncate thread names if they're too long.

llvm-svn: 296972
This commit is contained in:
Zachary Turner 2017-03-04 16:42:25 +00:00
parent f0236fd490
commit 777de77956
4 changed files with 37 additions and 5 deletions

View File

@ -14,6 +14,8 @@
// Other libraries and framework includes // Other libraries and framework includes
#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/Threading.h"
// Project includes // Project includes
#include "Plugins/Process/Utility/InferiorCallPOSIX.h" #include "Plugins/Process/Utility/InferiorCallPOSIX.h"
#include "lldb/Breakpoint/BreakpointLocation.h" #include "lldb/Breakpoint/BreakpointLocation.h"
@ -3733,8 +3735,8 @@ bool Process::StartPrivateStateThread(bool is_secondary_thread) {
// Create a thread that watches our internal state and controls which // Create a thread that watches our internal state and controls which
// events make it to clients (into the DCProcess event queue). // events make it to clients (into the DCProcess event queue).
char thread_name[1024]; char thread_name[1024];
uint32_t max_len = llvm::get_max_thread_name_length();
if (HostInfo::GetMaxThreadNameLength() <= 30) { if (max_len > 0 && max_len <= 30) {
// On platforms with abbreviated thread name lengths, choose thread names // On platforms with abbreviated thread name lengths, choose thread names
// that fit within the limit. // that fit within the limit.
if (already_running) if (already_running)

View File

@ -137,6 +137,10 @@ void llvm_execute_on_thread(void (*UserFn)(void *), void *UserData,
/// this. /// this.
uint64_t get_threadid(); uint64_t get_threadid();
/// \brief Get the maximum length of a thread name on this platform.
/// A value of 0 means there is no limit.
constexpr 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
/// logging. The level of support for setting a thread's name varies /// logging. The level of support for setting a thread's name varies

View File

@ -109,10 +109,35 @@ uint64_t llvm::get_threadid() {
} }
constexpr uint32_t llvm::get_max_thread_name_length() {
#if defined(__NetBSD__)
return PTHREAD_MAX_NAMELEN_NP;
#elif defined(__APPLE__)
return 64;
#elif defined(__linux__)
#if HAVE_PTHREAD_SETNAME_NP
return 16;
#else
return 0;
#endif
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
return 16;
#else
return 0;
#endif
}
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;
StringRef NameStr = Name.toNullTerminatedStringRef(Storage); StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
// Truncate from the beginning, not the end, if the specified name is too
// long. For one, this ensures that the resulting string is still null
// 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
// threads to share a common prefix.
NameStr = NameStr.take_back(get_max_thread_name_length());
(void)NameStr; (void)NameStr;
#if defined(__linux__) #if defined(__linux__)
#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
@ -170,15 +195,14 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
free(kp); free(kp);
return; return;
#elif defined(__NetBSD__) #elif defined(__NetBSD__)
char buf[PTHREAD_MAX_NAMELEN_NP]; char buf[get_max_thread_name_length()];
::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP); ::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP);
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
constexpr int MAXNAMELEN = 16; char Buffer[get_max_thread_name_length()];
char Buffer[MAXNAMELEN];
if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN)) if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN))
Name.append(Buffer, Buffer + strlen(Buffer)); Name.append(Buffer, Buffer + strlen(Buffer));
#endif #endif

View File

@ -59,6 +59,8 @@ uint64_t llvm::get_threadid() {
return uint64_t(::GetCurrentThreadId()); return uint64_t(::GetCurrentThreadId());
} }
constexpr 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)
// Make sure the input is null terminated. // Make sure the input is null terminated.