Reformat inferior's main.cpp in lldb-server test

Summary: main.cpp is complete mess of tabs and spaces. This change brings it to compliance with LLVM coding style.

Reviewers: jmajors, labath

Reviewed By: jmajors, labath

Subscribers: krytarowski, jingham, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D30234
Author: Eugene Zemtsov <ezemtsov@google.com>

llvm-svn: 295950
This commit is contained in:
Pavel Labath 2017-02-23 09:46:33 +00:00
parent 68f2402c61
commit de4c1c0ee7
2 changed files with 309 additions and 340 deletions

View File

@ -0,0 +1 @@
BasedOnStyle: LLVM

View File

@ -1,3 +1,12 @@
//===-- main.cpp ------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <errno.h> #include <errno.h>
@ -50,11 +59,11 @@ static char g_message[256];
static volatile char g_c1 = '0'; static volatile char g_c1 = '0';
static volatile char g_c2 = '1'; static volatile char g_c2 = '1';
static void static void print_thread_id() {
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
// 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. // clause print out the unsupported text so that the unit test knows to skip
// Otherwise, let the else clause print out the unsupported text so that the unit test knows to skip verifying thread ids. // verifying thread ids.
#if defined(__APPLE__) #if defined(__APPLE__)
__uint64_t tid = 0; __uint64_t tid = 0;
pthread_threadid_np(pthread_self(), &tid); pthread_threadid_np(pthread_self(), &tid);
@ -67,15 +76,17 @@ print_thread_id ()
#endif #endif
} }
static void static void signal_handler(int signo) {
signal_handler (int signo)
{
const char *signal_name = nullptr; const char *signal_name = nullptr;
switch (signo) switch (signo) {
{ case SIGUSR1:
case SIGUSR1: signal_name = "SIGUSR1"; break; signal_name = "SIGUSR1";
case SIGSEGV: signal_name = "SIGSEGV"; break; break;
default: signal_name = nullptr; case SIGSEGV:
signal_name = "SIGSEGV";
break;
default:
signal_name = nullptr;
} }
// Print notice that we received the signal on a given thread. // Print notice that we received the signal on a given thread.
@ -89,23 +100,21 @@ signal_handler (int signo)
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
// Reset the signal handler if we're one of the expected signal handlers. // Reset the signal handler if we're one of the expected signal handlers.
switch (signo) switch (signo) {
{
case SIGSEGV: case SIGSEGV:
if (g_is_segfaulting) if (g_is_segfaulting) {
{ // Fix up the pointer we're writing to. This needs to happen if nothing
// Fix up the pointer we're writing to. This needs to happen if nothing intercepts the SIGSEGV // intercepts the SIGSEGV (i.e. if somebody runs this from the command
// (i.e. if somebody runs this from the command line). // line).
longjmp(g_jump_buffer, 1); longjmp(g_jump_buffer, 1);
} }
break; break;
case SIGUSR1: case SIGUSR1:
if (g_is_segfaulting) if (g_is_segfaulting) {
{ // Fix up the pointer we're writing to. This is used to test gdb remote
// Fix up the pointer we're writing to. This is used to test gdb remote signal delivery. // signal delivery. A SIGSEGV will be raised when the thread is created,
// A SIGSEGV will be raised when the thread is created, switched out for a SIGUSR1, and // switched out for a SIGUSR1, and then this code still needs to fix the
// then this code still needs to fix the seg fault. // seg fault. (i.e. if somebody runs this from the command line).
// (i.e. if somebody runs this from the command line).
longjmp(g_jump_buffer, 1); longjmp(g_jump_buffer, 1);
} }
break; break;
@ -113,16 +122,13 @@ signal_handler (int signo)
// Reset the signal handler. // Reset the signal handler.
sig_t sig_result = signal(signo, signal_handler); sig_t sig_result = signal(signo, signal_handler);
if (sig_result == SIG_ERR) if (sig_result == SIG_ERR) {
{
fprintf(stderr, "failed to set signal handler: errno=%d\n", errno); fprintf(stderr, "failed to set signal handler: errno=%d\n", errno);
exit(1); exit(1);
} }
} }
static void static void swap_chars() {
swap_chars ()
{
g_c1 = '1'; g_c1 = '1';
g_c2 = '0'; g_c2 = '0';
@ -130,17 +136,13 @@ swap_chars ()
g_c2 = '1'; g_c2 = '1';
} }
static void static void hello() {
hello ()
{
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("hello, world\n"); printf("hello, world\n");
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} }
static void* static void *thread_func(void *arg) {
thread_func (void *arg)
{
static pthread_mutex_t s_thread_index_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t s_thread_index_mutex = PTHREAD_MUTEX_INITIALIZER;
static int s_thread_index = 1; static int s_thread_index = 1;
@ -148,8 +150,7 @@ thread_func (void *arg)
const int this_thread_index = s_thread_index++; const int this_thread_index = s_thread_index++;
pthread_mutex_unlock(&s_thread_index_mutex); pthread_mutex_unlock(&s_thread_index_mutex);
if (g_print_thread_ids) if (g_print_thread_ids) {
{
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("thread %d id: ", this_thread_index); printf("thread %d id: ", this_thread_index);
print_thread_id(); print_thread_id();
@ -157,8 +158,7 @@ thread_func (void *arg)
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} }
if (g_threads_do_segfault) if (g_threads_do_segfault) {
{
// Sleep for a number of seconds based on the thread index. // Sleep for a number of seconds based on the thread index.
// TODO add ability to send commands to test exe so we can // TODO add ability to send commands to test exe so we can
// handle timing more precisely. This is clunky. All we're // handle timing more precisely. This is clunky. All we're
@ -172,13 +172,10 @@ thread_func (void *arg)
pthread_mutex_lock(&g_jump_buffer_mutex); pthread_mutex_lock(&g_jump_buffer_mutex);
g_is_segfaulting = true; g_is_segfaulting = true;
int *bad_p = nullptr; int *bad_p = nullptr;
if (setjmp(g_jump_buffer) == 0) if (setjmp(g_jump_buffer) == 0) {
{
// Force a seg fault signal on this thread. // Force a seg fault signal on this thread.
*bad_p = 0; *bad_p = 0;
} } else {
else
{
// Tell the system we're no longer seg faulting. // Tell the system we're no longer seg faulting.
// Used by the SIGUSR1 signal handler that we inject // Used by the SIGUSR1 signal handler that we inject
// in place of the SIGSEGV so it only tries to // in place of the SIGSEGV so it only tries to
@ -196,16 +193,14 @@ thread_func (void *arg)
} }
int sleep_seconds_remaining = 60; int sleep_seconds_remaining = 60;
while (sleep_seconds_remaining > 0) while (sleep_seconds_remaining > 0) {
{
sleep_seconds_remaining = sleep(sleep_seconds_remaining); sleep_seconds_remaining = sleep(sleep_seconds_remaining);
} }
return nullptr; return nullptr;
} }
int main (int argc, char **argv) int main(int argc, char **argv) {
{
lldb_enable_attach(); lldb_enable_attach();
std::vector<pthread_t> threads; std::vector<pthread_t> threads;
@ -214,70 +209,58 @@ int main (int argc, char **argv)
// Set the signal handler. // Set the signal handler.
sig_t sig_result = signal(SIGALRM, signal_handler); sig_t sig_result = signal(SIGALRM, signal_handler);
if (sig_result == SIG_ERR) if (sig_result == SIG_ERR) {
{
fprintf(stderr, "failed to set SIGALRM signal handler: errno=%d\n", errno); fprintf(stderr, "failed to set SIGALRM signal handler: errno=%d\n", errno);
exit(1); exit(1);
} }
sig_result = signal(SIGUSR1, signal_handler); sig_result = signal(SIGUSR1, signal_handler);
if (sig_result == SIG_ERR) if (sig_result == SIG_ERR) {
{
fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno); fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
exit(1); exit(1);
} }
sig_result = signal(SIGSEGV, signal_handler); sig_result = signal(SIGSEGV, signal_handler);
if (sig_result == SIG_ERR) if (sig_result == SIG_ERR) {
{
fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno); fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
exit(1); exit(1);
} }
// Process command line args. // Process command line args.
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i) {
{ if (std::strstr(argv[i], STDERR_PREFIX)) {
if (std::strstr (argv[i], STDERR_PREFIX))
{
// Treat remainder as text to go to stderr. // Treat remainder as text to go to stderr.
fprintf(stderr, "%s\n", (argv[i] + strlen(STDERR_PREFIX))); fprintf(stderr, "%s\n", (argv[i] + strlen(STDERR_PREFIX)));
} } else if (std::strstr(argv[i], RETVAL_PREFIX)) {
else if (std::strstr (argv[i], RETVAL_PREFIX))
{
// Treat as the return value for the program. // Treat as the return value for the program.
return_value = std::atoi(argv[i] + strlen(RETVAL_PREFIX)); return_value = std::atoi(argv[i] + strlen(RETVAL_PREFIX));
} } else if (std::strstr(argv[i], SLEEP_PREFIX)) {
else if (std::strstr (argv[i], SLEEP_PREFIX))
{
// Treat as the amount of time to have this process sleep (in seconds). // Treat as the amount of time to have this process sleep (in seconds).
int sleep_seconds_remaining = std::atoi(argv[i] + strlen(SLEEP_PREFIX)); int sleep_seconds_remaining = std::atoi(argv[i] + strlen(SLEEP_PREFIX));
// Loop around, sleeping until all sleep time is used up. Note that // 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. // signals will cause sleep to end early with the number of seconds
for (int i = 0; sleep_seconds_remaining > 0; ++i) // remaining.
{ for (int i = 0; sleep_seconds_remaining > 0; ++i) {
sleep_seconds_remaining = sleep(sleep_seconds_remaining); sleep_seconds_remaining = sleep(sleep_seconds_remaining);
// std::cout << "sleep result (call " << i << "): " << sleep_seconds_remaining << std::endl; // std::cout << "sleep result (call " << i << "): " <<
// sleep_seconds_remaining << std::endl;
} }
} } else if (std::strstr(argv[i], SET_MESSAGE_PREFIX)) {
else if (std::strstr (argv[i], SET_MESSAGE_PREFIX))
{
// Copy the contents after "set-message:" to the g_message buffer. // Copy the contents after "set-message:" to the g_message buffer.
// Used for reading inferior memory and verifying contents match expectations. // Used for reading inferior memory and verifying contents match
strncpy (g_message, argv[i] + strlen (SET_MESSAGE_PREFIX), sizeof (g_message)); // expectations.
strncpy(g_message, argv[i] + strlen(SET_MESSAGE_PREFIX),
sizeof(g_message));
// Ensure we're null terminated. // Ensure we're null terminated.
g_message[sizeof(g_message) - 1] = '\0'; g_message[sizeof(g_message) - 1] = '\0';
} } else if (std::strstr(argv[i], PRINT_MESSAGE_COMMAND)) {
else if (std::strstr (argv[i], PRINT_MESSAGE_COMMAND))
{
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("message: %s\n", g_message); printf("message: %s\n", g_message);
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} } else if (std::strstr(argv[i], GET_DATA_ADDRESS_PREFIX)) {
else if (std::strstr (argv[i], GET_DATA_ADDRESS_PREFIX))
{
volatile void *data_p = nullptr; volatile void *data_p = nullptr;
if (std::strstr(argv[i] + strlen(GET_DATA_ADDRESS_PREFIX), "g_message")) if (std::strstr(argv[i] + strlen(GET_DATA_ADDRESS_PREFIX), "g_message"))
@ -290,9 +273,7 @@ int main (int argc, char **argv)
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("data address: %p\n", data_p); printf("data address: %p\n", data_p);
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} } else if (std::strstr(argv[i], GET_HEAP_ADDRESS_COMMAND)) {
else if (std::strstr (argv[i], GET_HEAP_ADDRESS_COMMAND))
{
// Create a byte array if not already present. // Create a byte array if not already present.
if (!heap_array_up) if (!heap_array_up)
heap_array_up.reset(new uint8_t[32]); heap_array_up.reset(new uint8_t[32]);
@ -300,57 +281,49 @@ int main (int argc, char **argv)
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("heap address: %p\n", heap_array_up.get()); printf("heap address: %p\n", heap_array_up.get());
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} } else if (std::strstr(argv[i], GET_STACK_ADDRESS_COMMAND)) {
else if (std::strstr (argv[i], GET_STACK_ADDRESS_COMMAND))
{
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("stack address: %p\n", &return_value); printf("stack address: %p\n", &return_value);
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} } else if (std::strstr(argv[i], GET_CODE_ADDRESS_PREFIX)) {
else if (std::strstr (argv[i], GET_CODE_ADDRESS_PREFIX))
{
void (*func_p)() = nullptr; void (*func_p)() = nullptr;
if (std::strstr(argv[i] + strlen(GET_CODE_ADDRESS_PREFIX), "hello")) if (std::strstr(argv[i] + strlen(GET_CODE_ADDRESS_PREFIX), "hello"))
func_p = hello; func_p = hello;
else if (std::strstr (argv[i] + strlen (GET_CODE_ADDRESS_PREFIX), "swap_chars")) else if (std::strstr(argv[i] + strlen(GET_CODE_ADDRESS_PREFIX),
"swap_chars"))
func_p = swap_chars; func_p = swap_chars;
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf("code address: %p\n", func_p); printf("code address: %p\n", func_p);
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} } else if (std::strstr(argv[i], CALL_FUNCTION_PREFIX)) {
else if (std::strstr (argv[i], CALL_FUNCTION_PREFIX))
{
// Defaut to providing the address of main. // Defaut to providing the address of main.
if (std::strcmp(argv[i] + strlen(CALL_FUNCTION_PREFIX), "hello") == 0) if (std::strcmp(argv[i] + strlen(CALL_FUNCTION_PREFIX), "hello") == 0)
hello(); hello();
else if (std::strcmp (argv[i] + strlen (CALL_FUNCTION_PREFIX), "swap_chars") == 0) else if (std::strcmp(argv[i] + strlen(CALL_FUNCTION_PREFIX),
"swap_chars") == 0)
swap_chars(); swap_chars();
else else {
{
pthread_mutex_lock(&g_print_mutex); pthread_mutex_lock(&g_print_mutex);
printf ("unknown function: %s\n", argv[i] + strlen (CALL_FUNCTION_PREFIX)); printf("unknown function: %s\n",
argv[i] + strlen(CALL_FUNCTION_PREFIX));
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} }
} } else if (std::strstr(argv[i], THREAD_PREFIX)) {
else if (std::strstr (argv[i], THREAD_PREFIX))
{
// Check if we're creating a new thread. // Check if we're creating a new thread.
if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_NEW)) if (std::strstr(argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_NEW)) {
{
// Create a new thread. // Create a new thread.
pthread_t new_thread; pthread_t new_thread;
const int err = ::pthread_create (&new_thread, nullptr, thread_func, nullptr); const int err =
if (err) ::pthread_create(&new_thread, nullptr, thread_func, nullptr);
{ if (err) {
fprintf(stderr, "pthread_create() failed with error code %d\n", err); fprintf(stderr, "pthread_create() failed with error code %d\n", err);
exit(err); exit(err);
} }
threads.push_back(new_thread); threads.push_back(new_thread);
} } else if (std::strstr(argv[i] + strlen(THREAD_PREFIX),
else if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_PRINT_IDS)) THREAD_COMMAND_PRINT_IDS)) {
{
// Turn on thread id announcing. // Turn on thread id announcing.
g_print_thread_ids = true; g_print_thread_ids = true;
@ -360,27 +333,22 @@ int main (int argc, char **argv)
print_thread_id(); print_thread_id();
printf("\n"); printf("\n");
pthread_mutex_unlock(&g_print_mutex); pthread_mutex_unlock(&g_print_mutex);
} } else if (std::strstr(argv[i] + strlen(THREAD_PREFIX),
else if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_SEGFAULT)) THREAD_COMMAND_SEGFAULT)) {
{
g_threads_do_segfault = true; g_threads_do_segfault = true;
} } else {
else
{
// At this point we don't do anything else with threads. // At this point we don't do anything else with threads.
// Later use thread index and send command to thread. // Later use thread index and send command to thread.
} }
} } else {
else
{
// Treat the argument as text for stdout. // Treat the argument as text for stdout.
printf("%s\n", argv[i]); printf("%s\n", argv[i]);
} }
} }
// If we launched any threads, join them // If we launched any threads, join them
for (std::vector<pthread_t>::iterator it = threads.begin (); it != threads.end (); ++it) for (std::vector<pthread_t>::iterator it = threads.begin();
{ it != threads.end(); ++it) {
void *thread_retval = nullptr; void *thread_retval = nullptr;
const int err = ::pthread_join(*it, &thread_retval); const int err = ::pthread_join(*it, &thread_retval);
if (err != 0) if (err != 0)