[sanitizer] implement SanitizerSetThreadName/SanitizerGetThreadName. Just for linux so far (using prctl(PR_GET_NAME))

llvm-svn: 169598
This commit is contained in:
Kostya Serebryany 2012-12-07 11:27:24 +00:00
parent 5d2d761ad6
commit 35f9e5e87b
3 changed files with 40 additions and 0 deletions

View File

@ -137,6 +137,13 @@ void NORETURN Die();
void NORETURN SANITIZER_INTERFACE_ATTRIBUTE void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2); CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
// Set the name of the current thread to 'name', return true on succees.
// The name may be truncated to a system-dependent limit.
bool SanitizerSetThreadName(const char *name);
// Get the name of the current thread (no more than max_len bytes),
// return true on succees. name should have space for at least max_len+1 bytes.
bool SanitizerGetThreadName(char *name, int max_len);
// Specific tools may override behavior of "Die" and "CheckFailed" functions // Specific tools may override behavior of "Die" and "CheckFailed" functions
// to do tool-specific job. // to do tool-specific job.
void SetDieCallback(void (*callback)(void)); void SetDieCallback(void (*callback)(void));

View File

@ -31,6 +31,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <sys/prctl.h>
// Are we using 32-bit or 64-bit syscalls? // Are we using 32-bit or 64-bit syscalls?
// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
@ -356,6 +357,19 @@ bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
return IterateForObjectNameAndOffset(addr, offset, filename, filename_size); return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
} }
bool SanitizerSetThreadName(const char *name) {
return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
}
bool SanitizerGetThreadName(char *name, int max_len) {
char buff[17];
if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0))
return false;
internal_strncpy(name, buff, max_len);
name[max_len] = 0;
return true;
}
} // namespace __sanitizer } // namespace __sanitizer
#endif // __linux__ #endif // __linux__

View File

@ -11,6 +11,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace __sanitizer { namespace __sanitizer {
@ -78,4 +79,22 @@ TEST(SanitizerCommon, MmapAlignedOrDie) {
} }
} }
#ifdef __linux__
TEST(SanitizerCommon, SanitizerSetThreadName) {
const char *names[] = {
"0123456789012",
"01234567890123",
"012345678901234", // Larger names will be truncated on linux.
};
for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
EXPECT_TRUE(SanitizerSetThreadName(names[i]));
char buff[100];
EXPECT_TRUE(SanitizerGetThreadName(buff, sizeof(buff) - 1));
Printf("buff: %s\n", buff);
EXPECT_EQ(0, internal_strcmp(buff, names[i]));
}
}
#endif
} // namespace sanitizer } // namespace sanitizer