[Lldb-commits] [lldb] 0c208d1 - [lldb] Fix flakyness in TestGdbRemote_vContThreads

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 18 12:42:12 PDT 2021


Author: Pavel Labath
Date: 2021-03-18T20:41:55+01:00
New Revision: 0c208d1f42be3fcbca37729cafcab5e97ce0a8e2

URL: https://github.com/llvm/llvm-project/commit/0c208d1f42be3fcbca37729cafcab5e97ce0a8e2
DIFF: https://github.com/llvm/llvm-project/commit/0c208d1f42be3fcbca37729cafcab5e97ce0a8e2.diff

LOG: [lldb] Fix flakyness in TestGdbRemote_vContThreads

The cause is the non-async-signal-safety printf function (et al.). If
the test managed to interrupt the process and inject a signal before the
printf("@started") call returned (but after it has actually written the
output), that string could end up being printed twice (presumably,
because the function did not manage the clear the userspace buffer, and
so the print call in the signal handler would print it once again).

This patch fixes the issue by replacing the printf call in the signal
handler with a sprintf+write combo, which should not suffer from that
problem (though I wouldn't go as far as to call it async signal safe).

Added: 
    

Modified: 
    lldb/test/API/tools/lldb-server/main.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/test/API/tools/lldb-server/main.cpp b/lldb/test/API/tools/lldb-server/main.cpp
index 4a877deaafbd..8a14c11075f1 100644
--- a/lldb/test/API/tools/lldb-server/main.cpp
+++ b/lldb/test/API/tools/lldb-server/main.cpp
@@ -70,25 +70,23 @@ static void print_pid() {
 #endif
 }
 
-static void print_thread_id() {
+static uint64_t get_thread_id() {
 // Put in the right magic here for your platform to spit out the thread id (tid)
-// that debugserver/lldb-gdbserver would see as a TID. Otherwise, let the else
-// clause print out the unsupported text so that the unit test knows to skip
-// verifying thread ids.
+// that debugserver/lldb-gdbserver would see as a TID.
 #if defined(__APPLE__)
   __uint64_t tid = 0;
   pthread_threadid_np(pthread_self(), &tid);
-  printf("%" PRIx64, tid);
+  return tid;
 #elif defined(__linux__)
   // This is a call to gettid() via syscall.
-  printf("%" PRIx64, static_cast<uint64_t>(syscall(__NR_gettid)));
+  return syscall(__NR_gettid);
 #elif defined(__NetBSD__)
   // Technically lwpid_t is 32-bit signed integer
-  printf("%" PRIx64, static_cast<uint64_t>(_lwp_self()));
+  return static_cast<uint64_t>(_lwp_self());
 #elif defined(_WIN32)
-  printf("%" PRIx64, static_cast<uint64_t>(::GetCurrentThreadId()));
+  return static_cast<uint64_t>(::GetCurrentThreadId());
 #else
-  printf("{no-tid-support}");
+  return -1;
 #endif
 }
 
@@ -109,15 +107,12 @@ static void signal_handler(int signo) {
   }
 
   // Print notice that we received the signal on a given thread.
-  {
-    std::lock_guard<std::mutex> lock(g_print_mutex);
-    if (signal_name)
-      printf("received %s on thread id: ", signal_name);
-    else
-      printf("received signo %d (%s) on thread id: ", signo, strsignal(signo));
-    print_thread_id();
-    printf("\n");
-  }
+  char buf[100];
+  if (signal_name)
+    snprintf(buf, sizeof(buf), "received %s on thread id: %" PRIx64 "\n", signal_name, get_thread_id());
+  else
+    snprintf(buf, sizeof(buf), "received signo %d (%s) on thread id: %" PRIx64 "\n", signo, strsignal(signo), get_thread_id());
+  write(STDOUT_FILENO, buf, strlen(buf));
 
   // Reset the signal handler if we're one of the expected signal handlers.
   switch (signo) {
@@ -195,9 +190,7 @@ static void *thread_func(void *arg) {
   const int this_thread_index = s_thread_index++;
   if (g_print_thread_ids) {
     std::lock_guard<std::mutex> lock(g_print_mutex);
-    printf("thread %d id: ", this_thread_index);
-    print_thread_id();
-    printf("\n");
+    printf("thread %d id: %" PRIx64 "\n", this_thread_index, get_thread_id());
   }
 
   if (g_threads_do_segfault) {
@@ -229,9 +222,7 @@ static void *thread_func(void *arg) {
 
     {
       std::lock_guard<std::mutex> lock(g_print_mutex);
-      printf("thread ");
-      print_thread_id();
-      printf(": past SIGSEGV\n");
+      printf("thread %" PRIx64 ": past SIGSEGV\n", get_thread_id());
     }
   }
 
@@ -362,9 +353,7 @@ int main(int argc, char **argv) {
         // And announce us.
         {
           std::lock_guard<std::mutex> lock(g_print_mutex);
-          printf("thread 0 id: ");
-          print_thread_id();
-          printf("\n");
+          printf("thread 0 id: %" PRIx64 "\n", get_thread_id());
         }
       } else if (std::strstr(argv[i] + strlen(THREAD_PREFIX),
                              THREAD_COMMAND_SEGFAULT)) {


        


More information about the lldb-commits mailing list