2014-04-29 18:21:07 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2014-05-29 20:44:45 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2014-06-04 16:42:12 +00:00
|
|
|
#include <memory>
|
2014-05-23 22:25:29 +00:00
|
|
|
#include <pthread.h>
|
2014-05-30 17:59:47 +00:00
|
|
|
#include <setjmp.h>
|
2014-05-29 20:44:45 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2014-06-03 18:09:56 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2014-05-14 14:51:27 +00:00
|
|
|
#include <unistd.h>
|
2014-05-23 22:25:29 +00:00
|
|
|
#include <vector>
|
2014-04-25 23:08:24 +00:00
|
|
|
|
2014-05-30 17:59:47 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
|
|
|
|
int pthread_threadid_np(pthread_t,__uint64_t*);
|
|
|
|
#elif defined(__linux__)
|
2014-05-29 20:44:45 +00:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
2014-10-20 03:56:46 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-05 16:34:13 +00:00
|
|
|
static const char *const RETVAL_PREFIX = "retval:";
|
|
|
|
static const char *const SLEEP_PREFIX = "sleep:";
|
|
|
|
static const char *const STDERR_PREFIX = "stderr:";
|
|
|
|
static const char *const SET_MESSAGE_PREFIX = "set-message:";
|
2014-06-10 22:15:56 +00:00
|
|
|
static const char *const PRINT_MESSAGE_COMMAND = "print-message:";
|
2014-06-09 17:46:37 +00:00
|
|
|
static const char *const GET_DATA_ADDRESS_PREFIX = "get-data-address-hex:";
|
2014-06-05 16:34:13 +00:00
|
|
|
static const char *const GET_STACK_ADDRESS_COMMAND = "get-stack-address-hex:";
|
|
|
|
static const char *const GET_HEAP_ADDRESS_COMMAND = "get-heap-address-hex:";
|
|
|
|
|
|
|
|
static const char *const GET_CODE_ADDRESS_PREFIX = "get-code-address-hex:";
|
|
|
|
static const char *const CALL_FUNCTION_PREFIX = "call-function:";
|
2014-04-29 18:21:07 +00:00
|
|
|
|
2014-05-23 22:25:29 +00:00
|
|
|
static const char *const THREAD_PREFIX = "thread:";
|
2014-06-04 16:42:12 +00:00
|
|
|
static const char *const THREAD_COMMAND_NEW = "new";
|
|
|
|
static const char *const THREAD_COMMAND_PRINT_IDS = "print-ids";
|
|
|
|
static const char *const THREAD_COMMAND_SEGFAULT = "segfault";
|
2014-05-29 20:44:45 +00:00
|
|
|
|
|
|
|
static bool g_print_thread_ids = false;
|
|
|
|
static pthread_mutex_t g_print_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2014-05-30 17:59:47 +00:00
|
|
|
static bool g_threads_do_segfault = false;
|
|
|
|
|
|
|
|
static pthread_mutex_t g_jump_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static jmp_buf g_jump_buffer;
|
|
|
|
static bool g_is_segfaulting = false;
|
2014-05-29 20:44:45 +00:00
|
|
|
|
2014-06-03 18:09:56 +00:00
|
|
|
static char g_message[256];
|
|
|
|
|
2014-06-09 17:46:37 +00:00
|
|
|
static volatile char g_c1 = '0';
|
|
|
|
static volatile char g_c2 = '1';
|
|
|
|
|
2014-05-29 20:44:45 +00:00
|
|
|
static void
|
|
|
|
print_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.
|
|
|
|
#if defined(__APPLE__)
|
2014-05-30 17:59:47 +00:00
|
|
|
__uint64_t tid = 0;
|
|
|
|
pthread_threadid_np(pthread_self(), &tid);
|
|
|
|
printf ("%" PRIx64, tid);
|
2014-05-29 20:44:45 +00:00
|
|
|
#elif defined (__linux__)
|
|
|
|
// This is a call to gettid() via syscall.
|
|
|
|
printf ("%" PRIx64, static_cast<uint64_t> (syscall (__NR_gettid)));
|
|
|
|
#else
|
|
|
|
printf("{no-tid-support}");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
signal_handler (int signo)
|
|
|
|
{
|
2014-06-05 16:34:13 +00:00
|
|
|
const char *signal_name = nullptr;
|
2014-05-30 17:59:47 +00:00
|
|
|
switch (signo)
|
|
|
|
{
|
|
|
|
case SIGUSR1: signal_name = "SIGUSR1"; break;
|
|
|
|
case SIGSEGV: signal_name = "SIGSEGV"; break;
|
2014-06-05 16:34:13 +00:00
|
|
|
default: signal_name = nullptr;
|
2014-05-30 17:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Reset the signal handler if we're one of the expected signal handlers.
|
2014-05-29 20:44:45 +00:00
|
|
|
switch (signo)
|
|
|
|
{
|
2014-06-04 16:42:12 +00:00
|
|
|
case SIGSEGV:
|
2014-06-30 21:05:18 +00:00
|
|
|
if (g_is_segfaulting)
|
|
|
|
{
|
|
|
|
// Fix up the pointer we're writing to. This needs to happen if nothing intercepts the SIGSEGV
|
|
|
|
// (i.e. if somebody runs this from the command line).
|
|
|
|
longjmp(g_jump_buffer, 1);
|
|
|
|
}
|
2014-06-04 16:42:12 +00:00
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
if (g_is_segfaulting)
|
|
|
|
{
|
|
|
|
// Fix up the pointer we're writing to. This is used to test gdb remote signal delivery.
|
|
|
|
// A SIGSEGV will be raised when the thread is created, switched out for a SIGUSR1, and
|
|
|
|
// then this code still needs to fix the seg fault.
|
|
|
|
// (i.e. if somebody runs this from the command line).
|
|
|
|
longjmp(g_jump_buffer, 1);
|
|
|
|
}
|
|
|
|
break;
|
2014-05-29 20:44:45 +00:00
|
|
|
}
|
2014-05-30 17:59:47 +00:00
|
|
|
|
|
|
|
// Reset the signal handler.
|
|
|
|
sig_t sig_result = signal (signo, signal_handler);
|
|
|
|
if (sig_result == SIG_ERR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to set signal handler: errno=%d\n", errno);
|
|
|
|
exit (1);
|
|
|
|
}
|
2014-05-29 20:44:45 +00:00
|
|
|
}
|
2014-05-23 22:25:29 +00:00
|
|
|
|
2014-06-09 17:46:37 +00:00
|
|
|
static void
|
|
|
|
swap_chars ()
|
|
|
|
{
|
|
|
|
g_c1 = '1';
|
|
|
|
g_c2 = '0';
|
|
|
|
|
|
|
|
g_c1 = '0';
|
|
|
|
g_c2 = '1';
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:34:13 +00:00
|
|
|
static void
|
|
|
|
hello ()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
|
|
|
printf ("hello, world\n");
|
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
|
|
|
}
|
|
|
|
|
2014-05-23 22:25:29 +00:00
|
|
|
static void*
|
|
|
|
thread_func (void *arg)
|
|
|
|
{
|
2014-05-30 17:59:47 +00:00
|
|
|
static pthread_mutex_t s_thread_index_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2014-05-29 20:44:45 +00:00
|
|
|
static int s_thread_index = 1;
|
2014-05-23 22:25:29 +00:00
|
|
|
|
2014-05-30 17:59:47 +00:00
|
|
|
pthread_mutex_lock (&s_thread_index_mutex);
|
2014-05-29 20:44:45 +00:00
|
|
|
const int this_thread_index = s_thread_index++;
|
2014-05-30 17:59:47 +00:00
|
|
|
pthread_mutex_unlock (&s_thread_index_mutex);
|
2014-05-29 20:44:45 +00:00
|
|
|
|
|
|
|
if (g_print_thread_ids)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
|
|
|
printf ("thread %d id: ", this_thread_index);
|
|
|
|
print_thread_id ();
|
|
|
|
printf ("\n");
|
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
|
|
|
}
|
|
|
|
|
2014-05-30 17:59:47 +00:00
|
|
|
if (g_threads_do_segfault)
|
|
|
|
{
|
|
|
|
// Sleep for a number of seconds based on the thread index.
|
|
|
|
// TODO add ability to send commands to test exe so we can
|
|
|
|
// handle timing more precisely. This is clunky. All we're
|
|
|
|
// 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);
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-05-30 17:59:47 +00:00
|
|
|
// Test creating a SEGV.
|
|
|
|
pthread_mutex_lock (&g_jump_buffer_mutex);
|
|
|
|
g_is_segfaulting = true;
|
2014-06-05 16:34:13 +00:00
|
|
|
int *bad_p = nullptr;
|
2014-05-30 17:59:47 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2015-05-22 00:52:41 +00:00
|
|
|
int sleep_seconds_remaining = 60;
|
2014-05-23 22:25:29 +00:00
|
|
|
while (sleep_seconds_remaining > 0)
|
|
|
|
{
|
|
|
|
sleep_seconds_remaining = sleep (sleep_seconds_remaining);
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:34:13 +00:00
|
|
|
return nullptr;
|
2014-05-23 22:25:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 18:21:07 +00:00
|
|
|
int main (int argc, char **argv)
|
2014-04-25 23:08:24 +00:00
|
|
|
{
|
2014-10-20 03:56:46 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
// Immediately enable any ptracer so that we can allow the stub attach
|
|
|
|
// operation to succeed. Some Linux kernels are locked down so that
|
|
|
|
// only an ancestor can be a ptracer of a process. This disables that
|
|
|
|
// restriction. Without it, attach-related stub tests will fail.
|
|
|
|
#if defined(PR_SET_PTRACER) && defined(PR_SET_PTRACER_ANY)
|
|
|
|
const int prctl_result = prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
|
|
|
|
static_cast<void> (prctl_result);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-05-23 22:25:29 +00:00
|
|
|
std::vector<pthread_t> threads;
|
2014-06-04 15:44:33 +00:00
|
|
|
std::unique_ptr<uint8_t[]> heap_array_up;
|
2014-04-29 18:21:07 +00:00
|
|
|
int return_value = 0;
|
|
|
|
|
2014-05-29 20:44:45 +00:00
|
|
|
// Set the signal handler.
|
2014-05-30 17:59:47 +00:00
|
|
|
sig_t sig_result = signal (SIGALRM, signal_handler);
|
2014-05-29 20:44:45 +00:00
|
|
|
if (sig_result == SIG_ERR)
|
|
|
|
{
|
2014-05-30 17:59:47 +00:00
|
|
|
fprintf(stderr, "failed to set SIGALRM signal handler: errno=%d\n", errno);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sig_result = signal (SIGUSR1, signal_handler);
|
|
|
|
if (sig_result == SIG_ERR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sig_result = signal (SIGSEGV, signal_handler);
|
|
|
|
if (sig_result == SIG_ERR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
|
2014-05-29 20:44:45 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-05-29 20:44:45 +00:00
|
|
|
// Process command line args.
|
2014-04-29 18:21:07 +00:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
if (std::strstr (argv[i], STDERR_PREFIX))
|
|
|
|
{
|
|
|
|
// Treat remainder as text to go to stderr.
|
2014-05-29 20:44:45 +00:00
|
|
|
fprintf (stderr, "%s\n", (argv[i] + strlen (STDERR_PREFIX)));
|
2014-04-29 18:21:07 +00:00
|
|
|
}
|
|
|
|
else if (std::strstr (argv[i], RETVAL_PREFIX))
|
|
|
|
{
|
|
|
|
// Treat as the return value for the program.
|
|
|
|
return_value = std::atoi (argv[i] + strlen (RETVAL_PREFIX));
|
|
|
|
}
|
2014-05-14 14:51:27 +00:00
|
|
|
else if (std::strstr (argv[i], SLEEP_PREFIX))
|
|
|
|
{
|
|
|
|
// Treat as the amount of time to have this process sleep (in seconds).
|
2014-05-14 19:34:06 +00:00
|
|
|
int sleep_seconds_remaining = std::atoi (argv[i] + strlen (SLEEP_PREFIX));
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-05-14 19:34:06 +00:00
|
|
|
// 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);
|
Added gdb remote protocol tests across all qRegisterInfo responses.
Added support for gdb remote protocol capture/playback where there is a query/multiple-response
pattern. The new playback entry supports:
- a general query command (key: next_query or query)
- an optional first-query command if that differs from the subsequent queries (key: first_query)
- an end regex for matching anything that would signify that the query/multi-response
iteration has come to an end. An assumption is that the end regex is not a content
package we care about aside from ending the iteration. (key: end_regex)
- an optional 0-based index appended to the end of the query command
(key: append_iteration_suffix), default: False.
- a key used to collect responses from the query. Any response from the gdb remote
that doesn't match the end-of-iteration regex is captured in the playback context
dictionary using the key specified. That key will be an array, where each array
entry is one of the responses from the query/multi-response iteration. (key: save_key).
- a runaway response value, defaulting to 10k, where if this many responses is captured,
assume the ending condition regex is invalid, or the debug monitor is doing something
goofy that is going to blow out memory or time. (key: runaway_response_count, default: 10000)
See the lldbgdbserverutils.MultiResponseGdbRemoteEntry class for details.
A MultiResponseGdbRemoteEntry is added by adding an element to the GdbRemoteTestSequence
(via GdbRemoteTestSequence.add_log_lines), using a dictionary, where the "type" key
is set to "multi_response", and the rest of the keys in the dictionary entry are
set to the keys documented for MultiResponseGdbRemoteEntry.
Added helper functions to add the required entry to grab all qRegisterInfo responses.
Added another helper to parse the qRegisterInfo response packets into an array of
dictionaries, where each key:value in the dictionary comes from the register info
response packet.
Added a test to verify that a generic register exists for the program counter,
frame pointer, stack pointer and cpu flags across all register info responses.
Added a test to verify that at least one register set exists across all register
info responses.
llvm-svn: 209170
2014-05-19 22:35:24 +00:00
|
|
|
// std::cout << "sleep result (call " << i << "): " << sleep_seconds_remaining << std::endl;
|
2014-05-14 19:34:06 +00:00
|
|
|
}
|
2014-06-03 18:09:56 +00:00
|
|
|
}
|
|
|
|
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 expectations.
|
|
|
|
strncpy (g_message, argv[i] + strlen (SET_MESSAGE_PREFIX), sizeof (g_message));
|
|
|
|
|
|
|
|
// Ensure we're null terminated.
|
|
|
|
g_message[sizeof (g_message) - 1] = '\0';
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-06-03 18:09:56 +00:00
|
|
|
}
|
2014-06-10 22:15:56 +00:00
|
|
|
else if (std::strstr (argv[i], PRINT_MESSAGE_COMMAND))
|
|
|
|
{
|
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
|
|
|
printf ("message: %s\n", g_message);
|
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
|
|
|
}
|
2014-06-09 17:46:37 +00:00
|
|
|
else if (std::strstr (argv[i], GET_DATA_ADDRESS_PREFIX))
|
2014-06-03 18:09:56 +00:00
|
|
|
{
|
2014-06-09 17:46:37 +00:00
|
|
|
volatile void *data_p = nullptr;
|
|
|
|
|
|
|
|
if (std::strstr (argv[i] + strlen (GET_DATA_ADDRESS_PREFIX), "g_message"))
|
|
|
|
data_p = &g_message[0];
|
|
|
|
else if (std::strstr (argv[i] + strlen (GET_DATA_ADDRESS_PREFIX), "g_c1"))
|
|
|
|
data_p = &g_c1;
|
|
|
|
else if (std::strstr (argv[i] + strlen (GET_DATA_ADDRESS_PREFIX), "g_c2"))
|
|
|
|
data_p = &g_c2;
|
|
|
|
|
2014-06-03 18:09:56 +00:00
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
2014-06-09 17:46:37 +00:00
|
|
|
printf ("data address: %p\n", data_p);
|
2014-06-03 18:09:56 +00:00
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
2014-06-04 15:44:33 +00:00
|
|
|
}
|
|
|
|
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]);
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-06-04 15:44:33 +00:00
|
|
|
pthread_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);
|
|
|
|
printf ("stack address: %p\n", &return_value);
|
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
|
|
|
}
|
2014-06-05 16:34:13 +00:00
|
|
|
else if (std::strstr (argv[i], GET_CODE_ADDRESS_PREFIX))
|
2014-06-04 15:44:33 +00:00
|
|
|
{
|
2014-06-05 16:34:13 +00:00
|
|
|
void (*func_p)() = nullptr;
|
|
|
|
|
|
|
|
if (std::strstr (argv[i] + strlen (GET_CODE_ADDRESS_PREFIX), "hello"))
|
|
|
|
func_p = hello;
|
2014-06-09 17:46:37 +00:00
|
|
|
else if (std::strstr (argv[i] + strlen (GET_CODE_ADDRESS_PREFIX), "swap_chars"))
|
|
|
|
func_p = swap_chars;
|
2014-06-05 16:34:13 +00:00
|
|
|
|
2014-06-04 15:44:33 +00:00
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
2014-06-05 16:34:13 +00:00
|
|
|
printf ("code address: %p\n", func_p);
|
2014-06-04 15:44:33 +00:00
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
2014-06-05 16:34:13 +00:00
|
|
|
}
|
|
|
|
else if (std::strstr (argv[i], CALL_FUNCTION_PREFIX))
|
|
|
|
{
|
|
|
|
// Defaut to providing the address of main.
|
|
|
|
if (std::strcmp (argv[i] + strlen (CALL_FUNCTION_PREFIX), "hello") == 0)
|
|
|
|
hello();
|
2014-06-09 17:46:37 +00:00
|
|
|
else if (std::strcmp (argv[i] + strlen (CALL_FUNCTION_PREFIX), "swap_chars") == 0)
|
|
|
|
swap_chars();
|
2014-06-05 16:34:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
|
|
|
printf ("unknown function: %s\n", argv[i] + strlen (CALL_FUNCTION_PREFIX));
|
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
|
|
|
}
|
2014-05-14 14:51:27 +00:00
|
|
|
}
|
2014-05-23 22:25:29 +00:00
|
|
|
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;
|
2014-06-05 16:34:13 +00:00
|
|
|
const int err = ::pthread_create (&new_thread, nullptr, thread_func, nullptr);
|
2014-05-23 22:25:29 +00:00
|
|
|
if (err)
|
|
|
|
{
|
2014-05-29 20:44:45 +00:00
|
|
|
fprintf (stderr, "pthread_create() failed with error code %d\n", err);
|
2014-05-23 22:25:29 +00:00
|
|
|
exit (err);
|
|
|
|
}
|
|
|
|
threads.push_back (new_thread);
|
|
|
|
}
|
2014-05-29 20:44:45 +00:00
|
|
|
else if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_PRINT_IDS))
|
|
|
|
{
|
|
|
|
// Turn on thread id announcing.
|
|
|
|
g_print_thread_ids = true;
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-05-29 20:44:45 +00:00
|
|
|
// And announce us.
|
|
|
|
pthread_mutex_lock (&g_print_mutex);
|
|
|
|
printf ("thread 0 id: ");
|
|
|
|
print_thread_id ();
|
|
|
|
printf ("\n");
|
|
|
|
pthread_mutex_unlock (&g_print_mutex);
|
|
|
|
}
|
2014-05-30 17:59:47 +00:00
|
|
|
else if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_SEGFAULT))
|
|
|
|
{
|
|
|
|
g_threads_do_segfault = true;
|
|
|
|
}
|
2014-05-23 22:25:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// At this point we don't do anything else with threads.
|
|
|
|
// Later use thread index and send command to thread.
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 18:21:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Treat the argument as text for stdout.
|
2014-05-29 20:44:45 +00:00
|
|
|
printf("%s\n", argv[i]);
|
2014-04-29 18:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-23 22:25:29 +00:00
|
|
|
|
|
|
|
// If we launched any threads, join them
|
|
|
|
for (std::vector<pthread_t>::iterator it = threads.begin (); it != threads.end (); ++it)
|
|
|
|
{
|
2014-06-05 16:34:13 +00:00
|
|
|
void *thread_retval = nullptr;
|
2014-05-23 22:25:29 +00:00
|
|
|
const int err = ::pthread_join (*it, &thread_retval);
|
|
|
|
if (err != 0)
|
2014-05-29 20:44:45 +00:00
|
|
|
fprintf (stderr, "pthread_join() failed with error code %d\n", err);
|
2014-05-23 22:25:29 +00:00
|
|
|
}
|
2014-06-04 16:42:12 +00:00
|
|
|
|
2014-04-29 18:21:07 +00:00
|
|
|
return return_value;
|
2014-04-25 23:08:24 +00:00
|
|
|
}
|