asmith created this revision.
asmith added reviewers: zturner, labath, jfb, Hui.
Herald added subscribers: lldb-commits, dexonsmith.
Herald added a project: LLDB.
Some cleanup suggested when bringing up lldb-server on Windows.
Thanks to Hui Huang for the patch.
Repository:
rLLDB LLDB
https://reviews.llvm.org/D60496
Files:
packages/Python/lldbsuite/test/tools/lldb-server/main.cpp
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
@@ -11,14 +11,18 @@
#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__)
@@ -30,6 +34,16 @@
#include <lwp.h>
#endif
+#if defined(_WIN32)
+#include <windows.h>
+
+static unsigned int sleep(unsigned int seconds) {
+ ::Sleep(seconds * 1000);
+ return 0;
+}
+
+#endif
+
static const char *const RETVAL_PREFIX = "retval:";
static const char *const SLEEP_PREFIX = "sleep:";
static const char *const STDERR_PREFIX = "stderr:";
@@ -50,10 +64,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 +77,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 +99,17 @@
#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:
@@ -100,14 +123,14 @@
}
// Print notice that we received the signal on a given thread.
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
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);
+ g_print_mutex.unlock();
// Reset the signal handler if we're one of the expected signal handlers.
switch (signo) {
@@ -136,6 +159,7 @@
fprintf(stderr, "failed to set signal handler: errno=%d\n", errno);
exit(1);
}
+#endif
}
static void swap_chars() {
@@ -147,25 +171,25 @@
}
static void hello() {
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("hello, world\n");
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
}
static void *thread_func(void *arg) {
- static pthread_mutex_t s_thread_index_mutex = PTHREAD_MUTEX_INITIALIZER;
+ static std::mutex s_thread_index_mutex;
static int s_thread_index = 1;
- pthread_mutex_lock(&s_thread_index_mutex);
+ s_thread_index_mutex.lock();
const int this_thread_index = s_thread_index++;
- pthread_mutex_unlock(&s_thread_index_mutex);
+ s_thread_index_mutex.unlock();
if (g_print_thread_ids) {
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("thread %d id: ", this_thread_index);
print_thread_id();
printf("\n");
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
}
if (g_threads_do_segfault) {
@@ -179,7 +203,7 @@
sleep_seconds = sleep(sleep_seconds);
// Test creating a SEGV.
- pthread_mutex_lock(&g_jump_buffer_mutex);
+ g_jump_buffer_mutex.lock();
g_is_segfaulting = true;
int *bad_p = nullptr;
if (setjmp(g_jump_buffer) == 0) {
@@ -193,13 +217,13 @@
// was in play.
g_is_segfaulting = false;
}
- pthread_mutex_unlock(&g_jump_buffer_mutex);
+ g_jump_buffer_mutex.unlock();
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("thread ");
print_thread_id();
printf(": past SIGSEGV\n");
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
}
int sleep_seconds_remaining = 60;
@@ -213,10 +237,11 @@
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) {
@@ -235,6 +260,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) {
@@ -267,9 +293,9 @@
g_message[sizeof(g_message) - 1] = '\0';
} else if (std::strstr(argv[i], PRINT_MESSAGE_COMMAND)) {
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("message: %s\n", g_message);
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
} else if (std::strstr(argv[i], GET_DATA_ADDRESS_PREFIX)) {
volatile void *data_p = nullptr;
@@ -280,21 +306,21 @@
else if (std::strstr(argv[i] + strlen(GET_DATA_ADDRESS_PREFIX), "g_c2"))
data_p = &g_c2;
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("data address: %p\n", data_p);
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
} 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);
+ g_print_mutex.lock();
printf("heap address: %p\n", heap_array_up.get());
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
} else if (std::strstr(argv[i], GET_STACK_ADDRESS_COMMAND)) {
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("stack address: %p\n", &return_value);
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
} else if (std::strstr(argv[i], GET_CODE_ADDRESS_PREFIX)) {
void (*func_p)() = nullptr;
@@ -304,9 +330,9 @@
"swap_chars"))
func_p = swap_chars;
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("code address: %p\n", func_p);
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
} else if (std::strstr(argv[i], CALL_FUNCTION_PREFIX)) {
void (*func_p)() = nullptr;
@@ -317,36 +343,28 @@
"swap_chars") == 0)
func_p = swap_chars;
else {
- pthread_mutex_lock(&g_print_mutex);
+ g_print_mutex.lock();
printf("unknown function: %s\n",
argv[i] + strlen(CALL_FUNCTION_PREFIX));
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
}
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);
+ g_print_mutex.lock();
printf("thread 0 id: ");
print_thread_id();
printf("\n");
- pthread_mutex_unlock(&g_print_mutex);
+ g_print_mutex.unlock();
} else if (std::strstr(argv[i] + strlen(THREAD_PREFIX),
THREAD_COMMAND_SEGFAULT)) {
g_threads_do_segfault = true;
@@ -355,7 +373,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 +381,9 @@
}
// 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;
}
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits