Index: packages/Python/lldbsuite/test/tools/lldb-server/main.cpp =================================================================== --- packages/Python/lldbsuite/test/tools/lldb-server/main.cpp +++ packages/Python/lldbsuite/test/tools/lldb-server/main.cpp @@ -6,19 +6,25 @@ // //===----------------------------------------------------------------------===// +#include +#include #include #include #include #include #include +#include +#if !defined(_WIN32) #include -#include #include +#include +#endif +#include #include #include #include +#include #include -#include #include #if defined(__APPLE__) @@ -28,6 +34,8 @@ #include #elif defined(__NetBSD__) #include +#elif defined(_WIN32) +#include #endif static const char *const RETVAL_PREFIX = "retval:"; @@ -50,10 +58,10 @@ static const char *const PRINT_PID_COMMAND = "print-pid"; static bool g_print_thread_ids = false; -static pthread_mutex_t g_print_mutex = PTHREAD_MUTEX_INITIALIZER; +static std::mutex g_print_mutex; static bool g_threads_do_segfault = false; -static pthread_mutex_t g_jump_buffer_mutex = PTHREAD_MUTEX_INITIALIZER; +static std::mutex g_jump_buffer_mutex; static jmp_buf g_jump_buffer; static bool g_is_segfaulting = false; @@ -63,7 +71,11 @@ static volatile char g_c2 = '1'; static void print_pid() { +#if defined(_WIN32) + fprintf(stderr, "PID: %d\n", ::GetCurrentProcessId()); +#else fprintf(stderr, "PID: %d\n", getpid()); +#endif } static void print_thread_id() { @@ -81,12 +93,17 @@ #elif defined(__NetBSD__) // Technically lwpid_t is 32-bit signed integer printf("%" PRIx64, static_cast(_lwp_self())); +#elif defined(_WIN32) + printf("%" PRIx64, static_cast(::GetCurrentThreadId())); #else printf("{no-tid-support}"); #endif } static void signal_handler(int signo) { +#if defined(_WIN32) + // No signal support on Windows. +#else const char *signal_name = nullptr; switch (signo) { case SIGUSR1: @@ -100,14 +117,15 @@ } // Print notice that we received the signal on a given thread. - pthread_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"); - pthread_mutex_unlock(&g_print_mutex); + { + std::lock_guard 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"); + } // Reset the signal handler if we're one of the expected signal handlers. switch (signo) { @@ -136,6 +154,7 @@ fprintf(stderr, "failed to set signal handler: errno=%d\n", errno); exit(1); } +#endif } static void swap_chars() { @@ -147,25 +166,18 @@ } static void hello() { - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("hello, world\n"); - pthread_mutex_unlock(&g_print_mutex); } static void *thread_func(void *arg) { - static pthread_mutex_t s_thread_index_mutex = PTHREAD_MUTEX_INITIALIZER; - static int s_thread_index = 1; - - pthread_mutex_lock(&s_thread_index_mutex); + static std::atomic s_thread_index(1); const int this_thread_index = s_thread_index++; - pthread_mutex_unlock(&s_thread_index_mutex); - if (g_print_thread_ids) { - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("thread %d id: ", this_thread_index); print_thread_id(); printf("\n"); - pthread_mutex_unlock(&g_print_mutex); } if (g_threads_do_segfault) { @@ -175,37 +187,36 @@ // trying to do is add predictability as to the timing of // signal generation by created threads. int sleep_seconds = 2 * (this_thread_index - 1); - while (sleep_seconds > 0) - sleep_seconds = sleep(sleep_seconds); + std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds)); // Test creating a SEGV. - pthread_mutex_lock(&g_jump_buffer_mutex); - g_is_segfaulting = true; - int *bad_p = nullptr; - if (setjmp(g_jump_buffer) == 0) { - // Force a seg fault signal on this thread. - *bad_p = 0; - } else { - // Tell the system we're no longer seg faulting. - // Used by the SIGUSR1 signal handler that we inject - // in place of the SIGSEGV so it only tries to - // recover from the SIGSEGV if this seg fault code - // was in play. - g_is_segfaulting = false; + { + std::lock_guard lock(g_jump_buffer_mutex); + g_is_segfaulting = true; + int *bad_p = nullptr; + if (setjmp(g_jump_buffer) == 0) { + // Force a seg fault signal on this thread. + *bad_p = 0; + } else { + // Tell the system we're no longer seg faulting. + // Used by the SIGUSR1 signal handler that we inject + // in place of the SIGSEGV so it only tries to + // recover from the SIGSEGV if this seg fault code + // was in play. + g_is_segfaulting = false; + } } - pthread_mutex_unlock(&g_jump_buffer_mutex); - pthread_mutex_lock(&g_print_mutex); - printf("thread "); - print_thread_id(); - printf(": past SIGSEGV\n"); - pthread_mutex_unlock(&g_print_mutex); + { + std::lock_guard lock(g_print_mutex); + printf("thread "); + print_thread_id(); + printf(": past SIGSEGV\n"); + } } int sleep_seconds_remaining = 60; - while (sleep_seconds_remaining > 0) { - sleep_seconds_remaining = sleep(sleep_seconds_remaining); - } + std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_remaining)); return nullptr; } @@ -213,10 +224,11 @@ int main(int argc, char **argv) { lldb_enable_attach(); - std::vector threads; + std::vector threads; std::unique_ptr heap_array_up; int return_value = 0; +#if !defined(_WIN32) // Set the signal handler. sig_t sig_result = signal(SIGALRM, signal_handler); if (sig_result == SIG_ERR) { @@ -235,6 +247,7 @@ fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno); exit(1); } +#endif // Process command line args. for (int i = 1; i < argc; ++i) { @@ -251,11 +264,9 @@ // Loop around, sleeping until all sleep time is used up. Note that // signals will cause sleep to end early with the number of seconds // remaining. - for (int i = 0; sleep_seconds_remaining > 0; ++i) { - sleep_seconds_remaining = sleep(sleep_seconds_remaining); - // std::cout << "sleep result (call " << i << "): " << - // sleep_seconds_remaining << std::endl; - } + std::this_thread::sleep_for( + std::chrono::seconds(sleep_seconds_remaining)); + } else if (std::strstr(argv[i], SET_MESSAGE_PREFIX)) { // Copy the contents after "set-message:" to the g_message buffer. // Used for reading inferior memory and verifying contents match @@ -267,9 +278,8 @@ g_message[sizeof(g_message) - 1] = '\0'; } else if (std::strstr(argv[i], PRINT_MESSAGE_COMMAND)) { - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("message: %s\n", g_message); - pthread_mutex_unlock(&g_print_mutex); } else if (std::strstr(argv[i], GET_DATA_ADDRESS_PREFIX)) { volatile void *data_p = nullptr; @@ -280,21 +290,19 @@ else if (std::strstr(argv[i] + strlen(GET_DATA_ADDRESS_PREFIX), "g_c2")) data_p = &g_c2; - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("data address: %p\n", data_p); - pthread_mutex_unlock(&g_print_mutex); } else if (std::strstr(argv[i], GET_HEAP_ADDRESS_COMMAND)) { // Create a byte array if not already present. if (!heap_array_up) heap_array_up.reset(new uint8_t[32]); - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("heap address: %p\n", heap_array_up.get()); - pthread_mutex_unlock(&g_print_mutex); + } else if (std::strstr(argv[i], GET_STACK_ADDRESS_COMMAND)) { - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("stack address: %p\n", &return_value); - pthread_mutex_unlock(&g_print_mutex); } else if (std::strstr(argv[i], GET_CODE_ADDRESS_PREFIX)) { void (*func_p)() = nullptr; @@ -304,9 +312,8 @@ "swap_chars")) func_p = swap_chars; - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("code address: %p\n", func_p); - pthread_mutex_unlock(&g_print_mutex); } else if (std::strstr(argv[i], CALL_FUNCTION_PREFIX)) { void (*func_p)() = nullptr; @@ -317,36 +324,28 @@ "swap_chars") == 0) func_p = swap_chars; else { - pthread_mutex_lock(&g_print_mutex); + std::lock_guard lock(g_print_mutex); printf("unknown function: %s\n", argv[i] + strlen(CALL_FUNCTION_PREFIX)); - pthread_mutex_unlock(&g_print_mutex); } if (func_p) func_p(); } else if (std::strstr(argv[i], THREAD_PREFIX)) { // Check if we're creating a new thread. if (std::strstr(argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_NEW)) { - // Create a new thread. - pthread_t new_thread; - const int err = - ::pthread_create(&new_thread, nullptr, thread_func, nullptr); - if (err) { - fprintf(stderr, "pthread_create() failed with error code %d\n", err); - exit(err); - } - threads.push_back(new_thread); + threads.push_back(std::thread(thread_func, nullptr)); } else if (std::strstr(argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_PRINT_IDS)) { // Turn on thread id announcing. g_print_thread_ids = true; // And announce us. - pthread_mutex_lock(&g_print_mutex); - printf("thread 0 id: "); - print_thread_id(); - printf("\n"); - pthread_mutex_unlock(&g_print_mutex); + { + std::lock_guard lock(g_print_mutex); + printf("thread 0 id: "); + print_thread_id(); + printf("\n"); + } } else if (std::strstr(argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_SEGFAULT)) { g_threads_do_segfault = true; @@ -355,7 +354,7 @@ // Later use thread index and send command to thread. } } else if (std::strstr(argv[i], PRINT_PID_COMMAND)) { - print_pid(); + print_pid(); } else { // Treat the argument as text for stdout. printf("%s\n", argv[i]); @@ -363,13 +362,9 @@ } // If we launched any threads, join them - for (std::vector::iterator it = threads.begin(); - it != threads.end(); ++it) { - void *thread_retval = nullptr; - const int err = ::pthread_join(*it, &thread_retval); - if (err != 0) - fprintf(stderr, "pthread_join() failed with error code %d\n", err); - } + for (std::vector::iterator it = threads.begin(); + it != threads.end(); ++it) + it->join(); return return_value; }