1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
// This program creates NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS of pthreads,
// creates an lldb Debugger on each thread, creates targets, inserts two
// breakpoints, runs to the first breakpoint, backtraces, runs to the second
// breakpoint, backtraces, kills the inferior process, closes down the
// debugger.
// The main thread keeps track of which pthreads have completed and which
// pthreads have completed successfully, and exits when all pthreads have
// completed successfully, or our time limit has been exceeded.
// This test file helps to uncover race conditions and locking mistakes
// that are hit when lldb is being used to debug multiple processes
// simultaneously.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lldb/API/LLDB.h"
#include "lldb/API/SBCommandInterpreter.h"
#include "lldb/API/SBCommandReturnObject.h"
#include "lldb/API/SBDebugger.h"
#include <chrono>
#include <csignal>
#include <thread>
#define NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS 10
#define DEBUG 0
using namespace lldb;
bool *completed_threads_array = 0;
bool *successful_threads_array = 0;
const char *inferior_process_name = "testprog";
bool
wait_for_stop_event (SBProcess process, SBListener listener)
{
bool stopped = false;
while (!stopped)
{
SBEvent event;
bool waitfor_ret = listener.WaitForEvent (2, event);
if (event.GetType() == SBProcess::eBroadcastBitStateChanged)
{
if (process.GetState() == StateType::eStateStopped
|| process.GetState() == StateType::eStateCrashed
|| process.GetState() == StateType::eStateDetached
|| process.GetState() == StateType::eStateExited)
{
stopped = true;
}
}
}
return stopped;
}
bool
walk_stack_to_main (SBThread thread)
{
if (thread.IsValid() == 0)
{
return false;
}
bool found_main = false;
uint32_t curr_frame = 0;
const uint32_t framecount = thread.GetNumFrames();
while (!found_main && curr_frame < framecount)
{
SBFrame frame = thread.GetFrameAtIndex (curr_frame);
if (strcmp (frame.GetFunctionName(), "main") == 0)
{
found_main = true;
break;
}
curr_frame += 1;
}
return found_main;
}
void *do_one_debugger (void *in)
{
uint64_t threadnum = (uint64_t) in;
#if defined (__APPLE__)
char *threadname;
asprintf (&threadname, "thread #%lld", threadnum);
pthread_setname_np (threadname);
free (threadname);
#endif
#if DEBUG == 1
printf ("#%lld: Starting debug session\n", threadnum);
#endif
SBDebugger debugger = lldb::SBDebugger::Create (false);
if (debugger.IsValid ())
{
debugger.SetAsync (true);
SBTarget target = debugger.CreateTargetWithFileAndArch(inferior_process_name, "x86_64");
SBCommandInterpreter command_interp = debugger.GetCommandInterpreter();
if (target.IsValid())
{
SBBreakpoint bar_br = target.BreakpointCreateByName ("bar", "testprog");
if (!bar_br.IsValid())
{
printf ("#%lld: failed to set breakpoint on bar, exiting.\n", threadnum);
exit (1);
}
SBBreakpoint foo_br = target.BreakpointCreateByName ("foo", "testprog");
if (!foo_br.IsValid())
{
printf ("#%lld: Failed to set breakpoint on foo()\n", threadnum);
}
SBLaunchInfo launch_info (NULL);
SBError error;
SBProcess process = target.Launch (launch_info, error);
if (process.IsValid())
{
SBListener listener = debugger.GetListener();
SBBroadcaster broadcaster = process.GetBroadcaster();
uint32_t rc = broadcaster.AddListener (listener, SBProcess::eBroadcastBitStateChanged);
if (rc == 0)
{
printf ("adding listener failed\n");
exit (1);
}
wait_for_stop_event (process, listener);
if (!walk_stack_to_main (process.GetThreadAtIndex(0)))
{
printf ("#%lld: backtrace while @ foo() failed\n", threadnum);
completed_threads_array[threadnum] = true;
return (void *) 1;
}
if (strcmp (process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetFunctionName(), "foo") != 0)
{
#if DEBUG == 1
printf ("#%lld: First breakpoint did not stop at foo(), instead stopped at '%s'\n", threadnum, process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetFunctionName());
#endif
completed_threads_array[threadnum] = true;
return (void*) 1;
}
process.Continue();
wait_for_stop_event (process, listener);
if (process.GetState() == StateType::eStateExited)
{
printf ("#%lld: Process exited\n", threadnum);
completed_threads_array[threadnum] = true;
return (void *) 1;
}
if (!walk_stack_to_main (process.GetThreadAtIndex(0)))
{
printf ("#%lld: backtrace while @ bar() failed\n", threadnum);
completed_threads_array[threadnum] = true;
return (void *) 1;
}
if (strcmp (process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetFunctionName(), "bar") != 0)
{
printf ("#%lld: First breakpoint did not stop at bar()\n", threadnum);
completed_threads_array[threadnum] = true;
return (void*) 1;
}
process.Kill();
wait_for_stop_event (process, listener);
SBDebugger::Destroy(debugger);
#if DEBUG == 1
printf ("#%lld: All good!\n", threadnum);
#endif
successful_threads_array[threadnum] = true;
completed_threads_array[threadnum] = true;
return (void*) 0;
}
else
{
printf("#%lld: process failed to launch\n", threadnum);
successful_threads_array[threadnum] = false;
completed_threads_array[threadnum] = true;
return (void*) 0;
}
}
else
{
printf ("#%lld: did not get valid target\n", threadnum);
successful_threads_array[threadnum] = false;
completed_threads_array[threadnum] = true;
return (void*) 0;
}
}
else
{
printf ("#%lld: did not get debugger\n", threadnum);
successful_threads_array[threadnum] = false;
completed_threads_array[threadnum] = true;
return (void*) 0;
}
completed_threads_array[threadnum] = true;
return (void*) 1;
}
int count_completed_threads(int num_threads) {
int num_completed_threads = 0;
for (int i = 0; i < num_threads; i++)
if (completed_threads_array[i])
num_completed_threads++;
return num_completed_threads;
}
int count_successful_threads(int num_threads) {
int num_successful_threads = 0;
for (int i = 0; i < num_threads; i++)
if (successful_threads_array[i])
num_successful_threads++;
return num_successful_threads;
}
int main (int argc, char **argv)
{
#if !defined(_MSC_VER)
signal(SIGPIPE, SIG_IGN);
#endif
SBDebugger::Initialize();
completed_threads_array = (bool *) malloc (sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
memset (completed_threads_array, 0, sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
successful_threads_array = (bool *) malloc (sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
memset (successful_threads_array, 0, sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
if (argc > 1 && argv[1] != NULL)
{
inferior_process_name = argv[1];
}
std::thread threads[NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS];
for (uint64_t i = 0; i< NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS; i++)
{
threads[i] = std::move(std::thread(do_one_debugger, (void*)i));
}
int max_time_to_wait = 40; // 40 iterations, or 120 seconds
if (getenv("ASAN_OPTIONS"))
max_time_to_wait *= 4;
for (int iter = 0; iter < max_time_to_wait; iter++) {
std::this_thread::sleep_for(std::chrono::seconds(3));
int successful_threads = count_successful_threads(NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
int total_completed_threads = count_completed_threads(NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
if (total_completed_threads == NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS)
{
#if DEBUG == 1
printf ("All threads completed.\n");
printf ("%d threads completed successfully out of %d\n", successful_threads, NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
#endif
SBDebugger::Terminate();
exit(0);
}
else
{
#if DEBUG == 1
printf ("%d threads completed so far (%d successfully), out of %d\n", total_completed_threads, successful_threads, NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
#endif
}
if (iter == max_time_to_wait)
printf("reached maximum timeout but only %d threads have completed "
"so far "
"(%d successfully), out of %d. Exiting.\n",
total_completed_threads, successful_threads,
NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
}
SBDebugger::Terminate();
exit (1);
}
|