xref: /llvm-project/lldb/test/API/tools/lldb-server/main.cpp (revision 09531ede6d5622da68941902072dbca517d31318)
199451b44SJordan Rupprecht #include <atomic>
23266b117SMichał Górny #include <cassert>
399451b44SJordan Rupprecht #include <chrono>
499451b44SJordan Rupprecht #include <cstdlib>
599451b44SJordan Rupprecht #include <cstring>
699451b44SJordan Rupprecht #include <errno.h>
79611282cSPavel Labath #include <future>
899451b44SJordan Rupprecht #include <inttypes.h>
999451b44SJordan Rupprecht #include <memory>
1099451b44SJordan Rupprecht #include <mutex>
1199451b44SJordan Rupprecht #if !defined(_WIN32)
1299451b44SJordan Rupprecht #include <pthread.h>
1399451b44SJordan Rupprecht #include <signal.h>
1499451b44SJordan Rupprecht #include <unistd.h>
1599451b44SJordan Rupprecht #endif
1604b766daSPavel Labath #include "thread.h"
1799451b44SJordan Rupprecht #include <setjmp.h>
1899451b44SJordan Rupprecht #include <stdint.h>
1999451b44SJordan Rupprecht #include <stdio.h>
2099451b44SJordan Rupprecht #include <string.h>
21709f8186SRaphael Isemann #include <string>
2299451b44SJordan Rupprecht #include <thread>
2399451b44SJordan Rupprecht #include <time.h>
2499451b44SJordan Rupprecht #include <vector>
257d850db6SJonas Devlieghere #if defined(__APPLE__)
267d850db6SJonas Devlieghere #include <TargetConditionals.h>
277d850db6SJonas Devlieghere #endif
2899451b44SJordan Rupprecht 
2999451b44SJordan Rupprecht static const char *const PRINT_PID_COMMAND = "print-pid";
3099451b44SJordan Rupprecht 
3199451b44SJordan Rupprecht static bool g_print_thread_ids = false;
3299451b44SJordan Rupprecht static std::mutex g_print_mutex;
3399451b44SJordan Rupprecht static bool g_threads_do_segfault = false;
3499451b44SJordan Rupprecht 
3599451b44SJordan Rupprecht static std::mutex g_jump_buffer_mutex;
3699451b44SJordan Rupprecht static jmp_buf g_jump_buffer;
3799451b44SJordan Rupprecht static bool g_is_segfaulting = false;
3899451b44SJordan Rupprecht 
3999451b44SJordan Rupprecht static char g_message[256];
4099451b44SJordan Rupprecht 
4199451b44SJordan Rupprecht static volatile char g_c1 = '0';
4299451b44SJordan Rupprecht static volatile char g_c2 = '1';
4399451b44SJordan Rupprecht 
print_pid()4499451b44SJordan Rupprecht static void print_pid() {
4599451b44SJordan Rupprecht #if defined(_WIN32)
4699451b44SJordan Rupprecht   fprintf(stderr, "PID: %d\n", ::GetCurrentProcessId());
4799451b44SJordan Rupprecht #else
4899451b44SJordan Rupprecht   fprintf(stderr, "PID: %d\n", getpid());
4999451b44SJordan Rupprecht #endif
5099451b44SJordan Rupprecht }
5199451b44SJordan Rupprecht 
signal_handler(int signo)5299451b44SJordan Rupprecht static void signal_handler(int signo) {
5399451b44SJordan Rupprecht #if defined(_WIN32)
5499451b44SJordan Rupprecht   // No signal support on Windows.
5599451b44SJordan Rupprecht #else
5699451b44SJordan Rupprecht   const char *signal_name = nullptr;
5799451b44SJordan Rupprecht   switch (signo) {
5899451b44SJordan Rupprecht   case SIGUSR1:
5999451b44SJordan Rupprecht     signal_name = "SIGUSR1";
6099451b44SJordan Rupprecht     break;
6199451b44SJordan Rupprecht   case SIGSEGV:
6299451b44SJordan Rupprecht     signal_name = "SIGSEGV";
6399451b44SJordan Rupprecht     break;
6499451b44SJordan Rupprecht   default:
6599451b44SJordan Rupprecht     signal_name = nullptr;
6699451b44SJordan Rupprecht   }
6799451b44SJordan Rupprecht 
6899451b44SJordan Rupprecht   // Print notice that we received the signal on a given thread.
690c208d1fSPavel Labath   char buf[100];
7099451b44SJordan Rupprecht   if (signal_name)
710c208d1fSPavel Labath     snprintf(buf, sizeof(buf), "received %s on thread id: %" PRIx64 "\n", signal_name, get_thread_id());
7299451b44SJordan Rupprecht   else
730c208d1fSPavel Labath     snprintf(buf, sizeof(buf), "received signo %d (%s) on thread id: %" PRIx64 "\n", signo, strsignal(signo), get_thread_id());
740c208d1fSPavel Labath   write(STDOUT_FILENO, buf, strlen(buf));
7599451b44SJordan Rupprecht 
7699451b44SJordan Rupprecht   // Reset the signal handler if we're one of the expected signal handlers.
7799451b44SJordan Rupprecht   switch (signo) {
7899451b44SJordan Rupprecht   case SIGSEGV:
7999451b44SJordan Rupprecht     if (g_is_segfaulting) {
8099451b44SJordan Rupprecht       // Fix up the pointer we're writing to.  This needs to happen if nothing
8199451b44SJordan Rupprecht       // intercepts the SIGSEGV (i.e. if somebody runs this from the command
8299451b44SJordan Rupprecht       // line).
8399451b44SJordan Rupprecht       longjmp(g_jump_buffer, 1);
8499451b44SJordan Rupprecht     }
8599451b44SJordan Rupprecht     break;
8699451b44SJordan Rupprecht   case SIGUSR1:
8799451b44SJordan Rupprecht     if (g_is_segfaulting) {
8899451b44SJordan Rupprecht       // Fix up the pointer we're writing to.  This is used to test gdb remote
8999451b44SJordan Rupprecht       // signal delivery. A SIGSEGV will be raised when the thread is created,
9099451b44SJordan Rupprecht       // switched out for a SIGUSR1, and then this code still needs to fix the
9199451b44SJordan Rupprecht       // seg fault. (i.e. if somebody runs this from the command line).
9299451b44SJordan Rupprecht       longjmp(g_jump_buffer, 1);
9399451b44SJordan Rupprecht     }
9499451b44SJordan Rupprecht     break;
9599451b44SJordan Rupprecht   }
9699451b44SJordan Rupprecht 
9799451b44SJordan Rupprecht   // Reset the signal handler.
9899451b44SJordan Rupprecht   sig_t sig_result = signal(signo, signal_handler);
9999451b44SJordan Rupprecht   if (sig_result == SIG_ERR) {
10099451b44SJordan Rupprecht     fprintf(stderr, "failed to set signal handler: errno=%d\n", errno);
10199451b44SJordan Rupprecht     exit(1);
10299451b44SJordan Rupprecht   }
10399451b44SJordan Rupprecht #endif
10499451b44SJordan Rupprecht }
10599451b44SJordan Rupprecht 
swap_chars()10699451b44SJordan Rupprecht static void swap_chars() {
107aa73ee05SPavel Labath #if defined(__x86_64__) || defined(__i386__)
108aa73ee05SPavel Labath   asm volatile("movb %1, (%2)\n\t"
109aa73ee05SPavel Labath                "movb %0, (%3)\n\t"
110aa73ee05SPavel Labath                "movb %0, (%2)\n\t"
111aa73ee05SPavel Labath                "movb %1, (%3)\n\t"
112aa73ee05SPavel Labath                :
113aa73ee05SPavel Labath                : "i"('0'), "i"('1'), "r"(&g_c1), "r"(&g_c2)
114aa73ee05SPavel Labath                : "memory");
115aa73ee05SPavel Labath #elif defined(__aarch64__)
116aa73ee05SPavel Labath   asm volatile("strb %w1, [%2]\n\t"
117aa73ee05SPavel Labath                "strb %w0, [%3]\n\t"
118aa73ee05SPavel Labath                "strb %w0, [%2]\n\t"
119aa73ee05SPavel Labath                "strb %w1, [%3]\n\t"
120aa73ee05SPavel Labath                :
121aa73ee05SPavel Labath                : "r"('0'), "r"('1'), "r"(&g_c1), "r"(&g_c2)
122aa73ee05SPavel Labath                : "memory");
123aa73ee05SPavel Labath #elif defined(__arm__)
124aa73ee05SPavel Labath   asm volatile("strb %1, [%2]\n\t"
125aa73ee05SPavel Labath                "strb %0, [%3]\n\t"
126aa73ee05SPavel Labath                "strb %0, [%2]\n\t"
127aa73ee05SPavel Labath                "strb %1, [%3]\n\t"
128aa73ee05SPavel Labath                :
129aa73ee05SPavel Labath                : "r"('0'), "r"('1'), "r"(&g_c1), "r"(&g_c2)
130aa73ee05SPavel Labath                : "memory");
131aa73ee05SPavel Labath #else
132aa73ee05SPavel Labath #warning This may generate unpredictible assembly and cause the single-stepping test to fail.
133aa73ee05SPavel Labath #warning Please add appropriate assembly for your target.
13499451b44SJordan Rupprecht   g_c1 = '1';
13599451b44SJordan Rupprecht   g_c2 = '0';
13699451b44SJordan Rupprecht 
13799451b44SJordan Rupprecht   g_c1 = '0';
13899451b44SJordan Rupprecht   g_c2 = '1';
139aa73ee05SPavel Labath #endif
14099451b44SJordan Rupprecht }
14199451b44SJordan Rupprecht 
trap()1425a4fe166SPavel Labath static void trap() {
1435a4fe166SPavel Labath #if defined(__x86_64__) || defined(__i386__)
1445a4fe166SPavel Labath   asm volatile("int3");
1455a4fe166SPavel Labath #elif defined(__aarch64__)
1465a4fe166SPavel Labath   asm volatile("brk #0xf000");
1475a4fe166SPavel Labath #elif defined(__arm__)
1485a4fe166SPavel Labath   asm volatile("udf #254");
1495a4fe166SPavel Labath #elif defined(__powerpc__)
1505a4fe166SPavel Labath   asm volatile("trap");
1515a4fe166SPavel Labath #elif __has_builtin(__builtin_debugtrap())
1525a4fe166SPavel Labath   __builtin_debugtrap();
1535a4fe166SPavel Labath #else
1545a4fe166SPavel Labath #warning Don't know how to generate a trap. Some tests may fail.
1555a4fe166SPavel Labath #endif
1565a4fe166SPavel Labath }
1575a4fe166SPavel Labath 
15899451b44SJordan Rupprecht static void hello() {
15999451b44SJordan Rupprecht   std::lock_guard<std::mutex> lock(g_print_mutex);
16099451b44SJordan Rupprecht   printf("hello, world\n");
16199451b44SJordan Rupprecht }
16299451b44SJordan Rupprecht 
1639611282cSPavel Labath static void *thread_func(std::promise<void> ready) {
1649611282cSPavel Labath   ready.set_value();
16599451b44SJordan Rupprecht   static std::atomic<int> s_thread_index(1);
16699451b44SJordan Rupprecht   const int this_thread_index = s_thread_index++;
16799451b44SJordan Rupprecht   if (g_print_thread_ids) {
16899451b44SJordan Rupprecht     std::lock_guard<std::mutex> lock(g_print_mutex);
1690c208d1fSPavel Labath     printf("thread %d id: %" PRIx64 "\n", this_thread_index, get_thread_id());
17099451b44SJordan Rupprecht   }
17199451b44SJordan Rupprecht 
17299451b44SJordan Rupprecht   if (g_threads_do_segfault) {
17399451b44SJordan Rupprecht     // Sleep for a number of seconds based on the thread index.
17499451b44SJordan Rupprecht     // TODO add ability to send commands to test exe so we can
17599451b44SJordan Rupprecht     // handle timing more precisely.  This is clunky.  All we're
17699451b44SJordan Rupprecht     // trying to do is add predictability as to the timing of
17799451b44SJordan Rupprecht     // signal generation by created threads.
17899451b44SJordan Rupprecht     int sleep_seconds = 2 * (this_thread_index - 1);
17999451b44SJordan Rupprecht     std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds));
18099451b44SJordan Rupprecht 
18199451b44SJordan Rupprecht     // Test creating a SEGV.
18299451b44SJordan Rupprecht     {
18399451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_jump_buffer_mutex);
18499451b44SJordan Rupprecht       g_is_segfaulting = true;
18599451b44SJordan Rupprecht       int *bad_p = nullptr;
18699451b44SJordan Rupprecht       if (setjmp(g_jump_buffer) == 0) {
18799451b44SJordan Rupprecht         // Force a seg fault signal on this thread.
18899451b44SJordan Rupprecht         *bad_p = 0;
18999451b44SJordan Rupprecht       } else {
19099451b44SJordan Rupprecht         // Tell the system we're no longer seg faulting.
19199451b44SJordan Rupprecht         // Used by the SIGUSR1 signal handler that we inject
19299451b44SJordan Rupprecht         // in place of the SIGSEGV so it only tries to
19399451b44SJordan Rupprecht         // recover from the SIGSEGV if this seg fault code
19499451b44SJordan Rupprecht         // was in play.
19599451b44SJordan Rupprecht         g_is_segfaulting = false;
19699451b44SJordan Rupprecht       }
19799451b44SJordan Rupprecht     }
19899451b44SJordan Rupprecht 
19999451b44SJordan Rupprecht     {
20099451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_print_mutex);
2010c208d1fSPavel Labath       printf("thread %" PRIx64 ": past SIGSEGV\n", get_thread_id());
20299451b44SJordan Rupprecht     }
20399451b44SJordan Rupprecht   }
20499451b44SJordan Rupprecht 
20599451b44SJordan Rupprecht   int sleep_seconds_remaining = 60;
20699451b44SJordan Rupprecht   std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_remaining));
20799451b44SJordan Rupprecht 
20899451b44SJordan Rupprecht   return nullptr;
20999451b44SJordan Rupprecht }
21099451b44SJordan Rupprecht 
consume_front(std::string & str,const std::string & front)211953f580bSPavel Labath static bool consume_front(std::string &str, const std::string &front) {
212953f580bSPavel Labath   if (str.find(front) != 0)
213953f580bSPavel Labath     return false;
214953f580bSPavel Labath 
215953f580bSPavel Labath   str = str.substr(front.size());
216953f580bSPavel Labath   return true;
217953f580bSPavel Labath }
218953f580bSPavel Labath 
main(int argc,char ** argv)21999451b44SJordan Rupprecht int main(int argc, char **argv) {
22099451b44SJordan Rupprecht   lldb_enable_attach();
22199451b44SJordan Rupprecht 
22299451b44SJordan Rupprecht   std::vector<std::thread> threads;
22399451b44SJordan Rupprecht   std::unique_ptr<uint8_t[]> heap_array_up;
22499451b44SJordan Rupprecht   int return_value = 0;
22599451b44SJordan Rupprecht 
22699451b44SJordan Rupprecht #if !defined(_WIN32)
227*09531edeSMichał Górny   bool is_child = false;
228*09531edeSMichał Górny 
22999451b44SJordan Rupprecht   // Set the signal handler.
23099451b44SJordan Rupprecht   sig_t sig_result = signal(SIGALRM, signal_handler);
23199451b44SJordan Rupprecht   if (sig_result == SIG_ERR) {
23299451b44SJordan Rupprecht     fprintf(stderr, "failed to set SIGALRM signal handler: errno=%d\n", errno);
23399451b44SJordan Rupprecht     exit(1);
23499451b44SJordan Rupprecht   }
23599451b44SJordan Rupprecht 
23699451b44SJordan Rupprecht   sig_result = signal(SIGUSR1, signal_handler);
23799451b44SJordan Rupprecht   if (sig_result == SIG_ERR) {
23899451b44SJordan Rupprecht     fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
23999451b44SJordan Rupprecht     exit(1);
24099451b44SJordan Rupprecht   }
24199451b44SJordan Rupprecht 
24299451b44SJordan Rupprecht   sig_result = signal(SIGSEGV, signal_handler);
24399451b44SJordan Rupprecht   if (sig_result == SIG_ERR) {
244bbae0c1fSMichał Górny     fprintf(stderr, "failed to set SIGSEGV handler: errno=%d\n", errno);
245bbae0c1fSMichał Górny     exit(1);
246bbae0c1fSMichał Górny   }
247bbae0c1fSMichał Górny 
248bbae0c1fSMichał Górny   sig_result = signal(SIGCHLD, SIG_IGN);
249bbae0c1fSMichał Górny   if (sig_result == SIG_ERR) {
250bbae0c1fSMichał Górny     fprintf(stderr, "failed to set SIGCHLD handler: errno=%d\n", errno);
25199451b44SJordan Rupprecht     exit(1);
25299451b44SJordan Rupprecht   }
25399451b44SJordan Rupprecht #endif
25499451b44SJordan Rupprecht 
25599451b44SJordan Rupprecht   // Process command line args.
25699451b44SJordan Rupprecht   for (int i = 1; i < argc; ++i) {
257953f580bSPavel Labath     std::string arg = argv[i];
258953f580bSPavel Labath     if (consume_front(arg, "stderr:")) {
25999451b44SJordan Rupprecht       // Treat remainder as text to go to stderr.
260953f580bSPavel Labath       fprintf(stderr, "%s\n", arg.c_str());
261953f580bSPavel Labath     } else if (consume_front(arg, "retval:")) {
26299451b44SJordan Rupprecht       // Treat as the return value for the program.
263953f580bSPavel Labath       return_value = std::atoi(arg.c_str());
264953f580bSPavel Labath     } else if (consume_front(arg, "sleep:")) {
26599451b44SJordan Rupprecht       // Treat as the amount of time to have this process sleep (in seconds).
266953f580bSPavel Labath       int sleep_seconds_remaining = std::atoi(arg.c_str());
26799451b44SJordan Rupprecht 
26899451b44SJordan Rupprecht       // Loop around, sleeping until all sleep time is used up.  Note that
26999451b44SJordan Rupprecht       // signals will cause sleep to end early with the number of seconds
27099451b44SJordan Rupprecht       // remaining.
27199451b44SJordan Rupprecht       std::this_thread::sleep_for(
27299451b44SJordan Rupprecht           std::chrono::seconds(sleep_seconds_remaining));
27399451b44SJordan Rupprecht 
274953f580bSPavel Labath     } else if (consume_front(arg, "set-message:")) {
27599451b44SJordan Rupprecht       // Copy the contents after "set-message:" to the g_message buffer.
27699451b44SJordan Rupprecht       // Used for reading inferior memory and verifying contents match
27799451b44SJordan Rupprecht       // expectations.
278953f580bSPavel Labath       strncpy(g_message, arg.c_str(), sizeof(g_message));
27999451b44SJordan Rupprecht 
28099451b44SJordan Rupprecht       // Ensure we're null terminated.
28199451b44SJordan Rupprecht       g_message[sizeof(g_message) - 1] = '\0';
28299451b44SJordan Rupprecht 
283953f580bSPavel Labath     } else if (consume_front(arg, "print-message:")) {
28499451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_print_mutex);
28599451b44SJordan Rupprecht       printf("message: %s\n", g_message);
286953f580bSPavel Labath     } else if (consume_front(arg, "get-data-address-hex:")) {
28799451b44SJordan Rupprecht       volatile void *data_p = nullptr;
28899451b44SJordan Rupprecht 
289953f580bSPavel Labath       if (arg == "g_message")
29099451b44SJordan Rupprecht         data_p = &g_message[0];
291953f580bSPavel Labath       else if (arg == "g_c1")
29299451b44SJordan Rupprecht         data_p = &g_c1;
293953f580bSPavel Labath       else if (arg == "g_c2")
29499451b44SJordan Rupprecht         data_p = &g_c2;
29599451b44SJordan Rupprecht 
29699451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_print_mutex);
29799451b44SJordan Rupprecht       printf("data address: %p\n", data_p);
298953f580bSPavel Labath     } else if (consume_front(arg, "get-heap-address-hex:")) {
29999451b44SJordan Rupprecht       // Create a byte array if not already present.
30099451b44SJordan Rupprecht       if (!heap_array_up)
30199451b44SJordan Rupprecht         heap_array_up.reset(new uint8_t[32]);
30299451b44SJordan Rupprecht 
30399451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_print_mutex);
30499451b44SJordan Rupprecht       printf("heap address: %p\n", heap_array_up.get());
30599451b44SJordan Rupprecht 
306953f580bSPavel Labath     } else if (consume_front(arg, "get-stack-address-hex:")) {
30799451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_print_mutex);
30899451b44SJordan Rupprecht       printf("stack address: %p\n", &return_value);
309953f580bSPavel Labath     } else if (consume_front(arg, "get-code-address-hex:")) {
31099451b44SJordan Rupprecht       void (*func_p)() = nullptr;
31199451b44SJordan Rupprecht 
312953f580bSPavel Labath       if (arg == "hello")
31399451b44SJordan Rupprecht         func_p = hello;
314953f580bSPavel Labath       else if (arg == "swap_chars")
31599451b44SJordan Rupprecht         func_p = swap_chars;
31699451b44SJordan Rupprecht 
31799451b44SJordan Rupprecht       std::lock_guard<std::mutex> lock(g_print_mutex);
31899451b44SJordan Rupprecht       printf("code address: %p\n", func_p);
319953f580bSPavel Labath     } else if (consume_front(arg, "call-function:")) {
32099451b44SJordan Rupprecht       void (*func_p)() = nullptr;
32199451b44SJordan Rupprecht 
322953f580bSPavel Labath       if (arg == "hello")
32399451b44SJordan Rupprecht         func_p = hello;
324953f580bSPavel Labath       else if (arg == "swap_chars")
32599451b44SJordan Rupprecht         func_p = swap_chars;
32699451b44SJordan Rupprecht       func_p();
3277d850db6SJonas Devlieghere #if !defined(_WIN32) && !defined(TARGET_OS_WATCH) && !defined(TARGET_OS_TV)
328bbae0c1fSMichał Górny     } else if (arg == "fork") {
329*09531edeSMichał Górny       pid_t fork_pid = fork();
330*09531edeSMichał Górny       assert(fork_pid != -1);
331*09531edeSMichał Górny       is_child = fork_pid == 0;
332bbae0c1fSMichał Górny     } else if (arg == "vfork") {
333bbae0c1fSMichał Górny       if (vfork() == 0)
334bbae0c1fSMichał Górny         _exit(0);
335*09531edeSMichał Górny     } else if (consume_front(arg, "process:sync:")) {
336*09531edeSMichał Górny       // this is only valid after fork
337*09531edeSMichał Górny       const char *filenames[] = {"parent", "child"};
338*09531edeSMichał Górny       std::string my_file = arg + "." + filenames[is_child];
339*09531edeSMichał Górny       std::string other_file = arg + "." + filenames[!is_child];
340*09531edeSMichał Górny 
341*09531edeSMichał Górny       // indicate that we're ready
342*09531edeSMichał Górny       FILE *f = fopen(my_file.c_str(), "w");
343*09531edeSMichał Górny       assert(f);
344*09531edeSMichał Górny       fclose(f);
345*09531edeSMichał Górny 
346*09531edeSMichał Górny       // wait for the other process to be ready
347*09531edeSMichał Górny       for (int i = 0; i < 5; ++i) {
348*09531edeSMichał Górny         f = fopen(other_file.c_str(), "r");
349*09531edeSMichał Górny         if (f)
350*09531edeSMichał Górny           break;
351*09531edeSMichał Górny         std::this_thread::sleep_for(std::chrono::milliseconds(125 * (1<<i)));
352*09531edeSMichał Górny       }
353*09531edeSMichał Górny       assert(f);
354*09531edeSMichał Górny       fclose(f);
355bbae0c1fSMichał Górny #endif
356953f580bSPavel Labath     } else if (consume_front(arg, "thread:new")) {
3579611282cSPavel Labath       std::promise<void> promise;
3589611282cSPavel Labath       std::future<void> ready = promise.get_future();
3599611282cSPavel Labath       threads.push_back(std::thread(thread_func, std::move(promise)));
3609611282cSPavel Labath       ready.wait();
361953f580bSPavel Labath     } else if (consume_front(arg, "thread:print-ids")) {
36299451b44SJordan Rupprecht       // Turn on thread id announcing.
36399451b44SJordan Rupprecht       g_print_thread_ids = true;
36499451b44SJordan Rupprecht 
36599451b44SJordan Rupprecht       // And announce us.
36699451b44SJordan Rupprecht       {
36799451b44SJordan Rupprecht         std::lock_guard<std::mutex> lock(g_print_mutex);
3680c208d1fSPavel Labath         printf("thread 0 id: %" PRIx64 "\n", get_thread_id());
36999451b44SJordan Rupprecht       }
370953f580bSPavel Labath     } else if (consume_front(arg, "thread:segfault")) {
37199451b44SJordan Rupprecht       g_threads_do_segfault = true;
372953f580bSPavel Labath     } else if (consume_front(arg, "print-pid")) {
37399451b44SJordan Rupprecht       print_pid();
3743fade954SMichał Górny     } else if (consume_front(arg, "print-env:")) {
3753fade954SMichał Górny       // Print the value of specified envvar to stdout.
3763fade954SMichał Górny       const char *value = getenv(arg.c_str());
3773fade954SMichał Górny       printf("%s\n", value ? value : "__unset__");
3785a4fe166SPavel Labath     } else if (consume_front(arg, "trap")) {
3795a4fe166SPavel Labath       trap();
380251165b2SMichał Górny #if !defined(_WIN32)
381251165b2SMichał Górny     } else if (arg == "stop") {
382fea52ac5SPavel Labath       raise(SIGINT);
383251165b2SMichał Górny #endif
38499451b44SJordan Rupprecht     } else {
38599451b44SJordan Rupprecht       // Treat the argument as text for stdout.
38699451b44SJordan Rupprecht       printf("%s\n", argv[i]);
38799451b44SJordan Rupprecht     }
38899451b44SJordan Rupprecht   }
38999451b44SJordan Rupprecht 
39099451b44SJordan Rupprecht   // If we launched any threads, join them
39199451b44SJordan Rupprecht   for (std::vector<std::thread>::iterator it = threads.begin();
39299451b44SJordan Rupprecht        it != threads.end(); ++it)
39399451b44SJordan Rupprecht     it->join();
39499451b44SJordan Rupprecht 
39599451b44SJordan Rupprecht   return return_value;
39699451b44SJordan Rupprecht }
397