Skip to content

Commit

Permalink
[lldb-server] Update tests to use std::thread/mutex for all platforms
Browse files Browse the repository at this point in the history
Summary:
Some cleanup suggested when bringing up lldb-server on Windows. 
Thanks to Hui Huang for the patch.

Reviewers: zturner, labath, jfb, Hui

Reviewed By: labath

Subscribers: clayborg, dexonsmith, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D60496

llvm-svn: 358265
  • Loading branch information
aaronsm committed Apr 12, 2019
1 parent b414487 commit 4b0931b
Showing 1 changed file with 81 additions and 86 deletions.
167 changes: 81 additions & 86 deletions lldb/packages/Python/lldbsuite/test/tools/lldb-server/main.cpp
Expand Up @@ -6,19 +6,25 @@
//
//===----------------------------------------------------------------------===//

#include <atomic>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <inttypes.h>
#include <memory>
#include <mutex>
#if !defined(_WIN32)
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#endif
#include <setjmp.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <thread>
#include <time.h>
#include <unistd.h>
#include <vector>

#if defined(__APPLE__)
Expand All @@ -28,6 +34,8 @@ int pthread_threadid_np(pthread_t, __uint64_t *);
#include <sys/syscall.h>
#elif defined(__NetBSD__)
#include <lwp.h>
#elif defined(_WIN32)
#include <windows.h>
#endif

static const char *const RETVAL_PREFIX = "retval:";
Expand All @@ -50,10 +58,10 @@ static const char *const THREAD_COMMAND_SEGFAULT = "segfault";
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;

Expand All @@ -63,7 +71,11 @@ static volatile char g_c1 = '0';
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() {
Expand All @@ -81,12 +93,17 @@ static void print_thread_id() {
#elif defined(__NetBSD__)
// Technically lwpid_t is 32-bit signed integer
printf("%" PRIx64, static_cast<uint64_t>(_lwp_self()));
#elif defined(_WIN32)
printf("%" PRIx64, static_cast<uint64_t>(::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:
Expand All @@ -100,14 +117,15 @@ static void signal_handler(int signo) {
}

// 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<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");
}

// Reset the signal handler if we're one of the expected signal handlers.
switch (signo) {
Expand Down Expand Up @@ -136,6 +154,7 @@ static void signal_handler(int signo) {
fprintf(stderr, "failed to set signal handler: errno=%d\n", errno);
exit(1);
}
#endif
}

static void swap_chars() {
Expand All @@ -147,25 +166,18 @@ static void swap_chars() {
}

static void hello() {
pthread_mutex_lock(&g_print_mutex);
std::lock_guard<std::mutex> 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<int> 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<std::mutex> 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) {
Expand All @@ -175,48 +187,48 @@ static void *thread_func(void *arg) {
// 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<std::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;
}
}
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<std::mutex> 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;
}

int main(int argc, char **argv) {
lldb_enable_attach();

std::vector<pthread_t> threads;
std::vector<std::thread> threads;
std::unique_ptr<uint8_t[]> 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) {
Expand All @@ -235,6 +247,7 @@ int main(int argc, char **argv) {
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) {
Expand All @@ -251,11 +264,9 @@ int main(int argc, char **argv) {
// 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
Expand All @@ -267,9 +278,8 @@ int main(int argc, char **argv) {
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<std::mutex> 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;

Expand All @@ -280,21 +290,19 @@ int main(int argc, char **argv) {
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<std::mutex> 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<std::mutex> 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<std::mutex> 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;

Expand All @@ -304,9 +312,8 @@ int main(int argc, char **argv) {
"swap_chars"))
func_p = swap_chars;

pthread_mutex_lock(&g_print_mutex);
std::lock_guard<std::mutex> 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;

Expand All @@ -317,36 +324,28 @@ int main(int argc, char **argv) {
"swap_chars") == 0)
func_p = swap_chars;
else {
pthread_mutex_lock(&g_print_mutex);
std::lock_guard<std::mutex> 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<std::mutex> 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;
Expand All @@ -355,21 +354,17 @@ int main(int argc, char **argv) {
// 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]);
}
}

// If we launched any threads, join them
for (std::vector<pthread_t>::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<std::thread>::iterator it = threads.begin();
it != threads.end(); ++it)
it->join();

return return_value;
}

0 comments on commit 4b0931b

Please sign in to comment.