[llvm-commits] [compiler-rt] r169598 - in /compiler-rt/trunk/lib/sanitizer_common: sanitizer_common.h sanitizer_linux.cc tests/sanitizer_common_test.cc

Kostya Serebryany kcc at google.com
Fri Dec 7 03:27:25 PST 2012


Author: kcc
Date: Fri Dec  7 05:27:24 2012
New Revision: 169598

URL: http://llvm.org/viewvc/llvm-project?rev=169598&view=rev
Log:
[sanitizer] implement SanitizerSetThreadName/SanitizerGetThreadName. Just for linux so far (using prctl(PR_GET_NAME))

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=169598&r1=169597&r2=169598&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Fri Dec  7 05:27:24 2012
@@ -137,6 +137,13 @@
 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
 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
 // to do tool-specific job.
 void SetDieCallback(void (*callback)(void));

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc?rev=169598&r1=169597&r2=169598&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc Fri Dec  7 05:27:24 2012
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/prctl.h>
 
 // Are we using 32-bit or 64-bit syscalls?
 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
@@ -356,6 +357,19 @@
   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
 
 #endif  // __linux__

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc?rev=169598&r1=169597&r2=169598&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_common_test.cc Fri Dec  7 05:27:24 2012
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 #include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_libc.h"
 #include "gtest/gtest.h"
 
 namespace __sanitizer {
@@ -78,4 +79,22 @@
   }
 }
 
+#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





More information about the llvm-commits mailing list