Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / test / API / api / multiple-debuggers / multi-process-driver.cpp
blob5cf5ff356203009c5ff2a9ec9ea2243b6d1673ee
2 // This program creates NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS of pthreads,
3 // creates an lldb Debugger on each thread, creates targets, inserts two
4 // breakpoints, runs to the first breakpoint, backtraces, runs to the second
5 // breakpoint, backtraces, kills the inferior process, closes down the
6 // debugger.
8 // The main thread keeps track of which pthreads have completed and which
9 // pthreads have completed successfully, and exits when all pthreads have
10 // completed successfully, or our time limit has been exceeded.
12 // This test file helps to uncover race conditions and locking mistakes
13 // that are hit when lldb is being used to debug multiple processes
14 // simultaneously.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "lldb/API/LLDB.h"
21 #include "lldb/API/SBCommandInterpreter.h"
22 #include "lldb/API/SBCommandReturnObject.h"
23 #include "lldb/API/SBDebugger.h"
25 #include <chrono>
26 #include <csignal>
27 #include <thread>
29 #define NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS 10
31 #define DEBUG 0
33 using namespace lldb;
35 bool *completed_threads_array = 0;
36 bool *successful_threads_array = 0;
38 const char *inferior_process_name = "testprog";
40 bool
41 wait_for_stop_event (SBProcess process, SBListener listener)
43 bool stopped = false;
44 while (!stopped)
46 SBEvent event;
47 bool waitfor_ret = listener.WaitForEvent (2, event);
48 if (event.GetType() == SBProcess::eBroadcastBitStateChanged)
50 if (process.GetState() == StateType::eStateStopped
51 || process.GetState() == StateType::eStateCrashed
52 || process.GetState() == StateType::eStateDetached
53 || process.GetState() == StateType::eStateExited)
55 stopped = true;
59 return stopped;
62 bool
63 walk_stack_to_main (SBThread thread)
65 if (thread.IsValid() == 0)
67 return false;
70 bool found_main = false;
71 uint32_t curr_frame = 0;
72 const uint32_t framecount = thread.GetNumFrames();
73 while (!found_main && curr_frame < framecount)
75 SBFrame frame = thread.GetFrameAtIndex (curr_frame);
76 if (strcmp (frame.GetFunctionName(), "main") == 0)
78 found_main = true;
79 break;
81 curr_frame += 1;
83 return found_main;
86 void *do_one_debugger (void *in)
88 uint64_t threadnum = (uint64_t) in;
90 #if defined (__APPLE__)
91 char *threadname;
92 asprintf (&threadname, "thread #%lld", threadnum);
93 pthread_setname_np (threadname);
94 free (threadname);
95 #endif
97 #if DEBUG == 1
98 printf ("#%lld: Starting debug session\n", threadnum);
99 #endif
101 SBDebugger debugger = lldb::SBDebugger::Create (false);
102 if (debugger.IsValid ())
104 debugger.SetAsync (true);
105 SBTarget target = debugger.CreateTargetWithFileAndArch(inferior_process_name, "x86_64");
106 SBCommandInterpreter command_interp = debugger.GetCommandInterpreter();
107 if (target.IsValid())
109 SBBreakpoint bar_br = target.BreakpointCreateByName ("bar", "testprog");
110 if (!bar_br.IsValid())
112 printf ("#%lld: failed to set breakpoint on bar, exiting.\n", threadnum);
113 exit (1);
115 SBBreakpoint foo_br = target.BreakpointCreateByName ("foo", "testprog");
116 if (!foo_br.IsValid())
118 printf ("#%lld: Failed to set breakpoint on foo()\n", threadnum);
121 SBLaunchInfo launch_info (NULL);
122 SBError error;
123 SBProcess process = target.Launch (launch_info, error);
124 if (process.IsValid())
126 SBListener listener = debugger.GetListener();
127 SBBroadcaster broadcaster = process.GetBroadcaster();
128 uint32_t rc = broadcaster.AddListener (listener, SBProcess::eBroadcastBitStateChanged);
129 if (rc == 0)
131 printf ("adding listener failed\n");
132 exit (1);
135 wait_for_stop_event (process, listener);
137 if (!walk_stack_to_main (process.GetThreadAtIndex(0)))
139 printf ("#%lld: backtrace while @ foo() failed\n", threadnum);
140 completed_threads_array[threadnum] = true;
141 return (void *) 1;
144 if (strcmp (process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetFunctionName(), "foo") != 0)
146 #if DEBUG == 1
147 printf ("#%lld: First breakpoint did not stop at foo(), instead stopped at '%s'\n", threadnum, process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetFunctionName());
148 #endif
149 completed_threads_array[threadnum] = true;
150 return (void*) 1;
153 process.Continue();
155 wait_for_stop_event (process, listener);
157 if (process.GetState() == StateType::eStateExited)
159 printf ("#%lld: Process exited\n", threadnum);
160 completed_threads_array[threadnum] = true;
161 return (void *) 1;
165 if (!walk_stack_to_main (process.GetThreadAtIndex(0)))
167 printf ("#%lld: backtrace while @ bar() failed\n", threadnum);
168 completed_threads_array[threadnum] = true;
169 return (void *) 1;
172 if (strcmp (process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetFunctionName(), "bar") != 0)
174 printf ("#%lld: First breakpoint did not stop at bar()\n", threadnum);
175 completed_threads_array[threadnum] = true;
176 return (void*) 1;
179 process.Kill();
181 wait_for_stop_event (process, listener);
183 SBDebugger::Destroy(debugger);
185 #if DEBUG == 1
186 printf ("#%lld: All good!\n", threadnum);
187 #endif
188 successful_threads_array[threadnum] = true;
189 completed_threads_array[threadnum] = true;
190 return (void*) 0;
192 else
194 printf("#%lld: process failed to launch\n", threadnum);
195 successful_threads_array[threadnum] = false;
196 completed_threads_array[threadnum] = true;
197 return (void*) 0;
200 else
202 printf ("#%lld: did not get valid target\n", threadnum);
203 successful_threads_array[threadnum] = false;
204 completed_threads_array[threadnum] = true;
205 return (void*) 0;
208 else
210 printf ("#%lld: did not get debugger\n", threadnum);
211 successful_threads_array[threadnum] = false;
212 completed_threads_array[threadnum] = true;
213 return (void*) 0;
215 completed_threads_array[threadnum] = true;
216 return (void*) 1;
219 int main (int argc, char **argv)
221 #if !defined(_MSC_VER)
222 signal(SIGPIPE, SIG_IGN);
223 #endif
225 SBDebugger::Initialize();
227 completed_threads_array = (bool *) malloc (sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
228 memset (completed_threads_array, 0, sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
229 successful_threads_array = (bool *) malloc (sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
230 memset (successful_threads_array, 0, sizeof (bool) * NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
232 if (argc > 1 && argv[1] != NULL)
234 inferior_process_name = argv[1];
237 std::thread threads[NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS];
238 for (uint64_t i = 0; i< NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS; i++)
240 threads[i] = std::move(std::thread(do_one_debugger, (void*)i));
244 int max_time_to_wait = 20; // 20 iterations, or 60 seconds
245 int iter = 0;
246 while (1)
248 std::this_thread::sleep_for(std::chrono::seconds(3));
249 bool all_done = true;
250 int successful_threads = 0;
251 int total_completed_threads = 0;
252 for (uint64_t i = 0; i < NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS; i++)
254 if (successful_threads_array[i] == true)
255 successful_threads++;
256 if (completed_threads_array[i] == true)
257 total_completed_threads++;
258 if (completed_threads_array[i] == false)
260 all_done = false;
263 if (all_done)
265 #if DEBUG == 1
266 printf ("All threads completed.\n");
267 printf ("%d threads completed successfully out of %d\n", successful_threads, NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
268 #endif
269 SBDebugger::Terminate();
270 exit(0);
272 else
274 #if DEBUG == 1
275 printf ("%d threads completed so far (%d successfully), out of %d\n", total_completed_threads, successful_threads, NUMBER_OF_SIMULTANEOUS_DEBUG_SESSIONS);
276 #endif
278 if (iter++ == max_time_to_wait)
280 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);
281 break;
286 SBDebugger::Terminate();
287 exit (1);