1 //===-- TestCase.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 using namespace lldb_perf;
16 TestCase::TestCase () :
25 SBDebugger::Initialize();
26 SBHostOS::ThreadCreated ("<lldb-tester.app.main>");
27 m_debugger = SBDebugger::Create(false);
28 m_listener = m_debugger.GetListener();
29 m_listener.StartListeningForEventClass (m_debugger, SBProcess::GetBroadcasterClass(), SBProcess::eBroadcastBitStateChanged | SBProcess::eBroadcastBitInterrupt);
33 GetShortOptionString (struct option *long_options)
35 std::string option_string;
36 for (int i = 0; long_options[i].name != NULL; ++i)
38 if (long_options[i].flag == NULL)
40 option_string.push_back ((char) long_options[i].val);
41 switch (long_options[i].has_arg)
46 case required_argument:
47 option_string.push_back (':');
49 case optional_argument:
50 option_string.append (2, ':');
59 TestCase::Setup (int& argc, const char**& argv)
63 struct option* long_options = GetLongOptions();
67 std::string short_option_string (GetShortOptionString(long_options));
77 int long_options_index = -1;
78 const int short_option = ::getopt_long_only (argc,
79 const_cast<char **>(argv),
80 short_option_string.c_str(),
95 done = !ParseOption(short_option, optarg);
107 TestCase::Launch (lldb::SBLaunchInfo &launch_info)
110 m_process = m_target.Launch (launch_info, error);
111 if (!error.Success())
112 fprintf (stderr, "error: %s\n", error.GetCString());
113 if (m_process.IsValid())
119 TestCase::Launch (std::initializer_list<const char*> args)
121 std::vector<const char*> args_vect(args);
122 args_vect.push_back(NULL);
123 lldb::SBLaunchInfo launch_info((const char**)&args_vect[0]);
124 return Launch(launch_info);
128 TestCase::SetVerbose (bool b)
134 TestCase::GetVerbose ()
144 bool call_test_step = false;
145 if (m_process.IsValid())
148 m_listener.WaitForEvent (UINT32_MAX, evt);
149 StateType state = SBProcess::GetStateFromEvent (evt);
151 printf("event = %s\n",SBDebugger::StateAsCString(state));
152 if (SBProcess::GetRestartedFromEvent(evt))
156 const uint32_t num_threads = m_process.GetNumThreads();
157 for (auto thread_index = 0; thread_index < num_threads; thread_index++)
159 SBThread thread(m_process.GetThreadAtIndex(thread_index));
160 SBFrame frame(thread.GetFrameAtIndex(0));
162 strm.RedirectToFileHandle(stdout, false);
163 frame.GetDescription(strm);
167 call_test_step = false;
180 case eStateConnected:
181 case eStateAttaching:
182 case eStateLaunching:
185 call_test_step = false;
189 case eStateSuspended:
191 call_test_step = true;
193 bool selected_thread = false;
194 const uint32_t num_threads = m_process.GetNumThreads();
195 for (auto thread_index = 0; thread_index < num_threads; thread_index++)
197 SBThread thread(m_process.GetThreadAtIndex(thread_index));
198 SBFrame frame(thread.GetFrameAtIndex(0));
200 strm.RedirectToFileHandle(stdout, false);
201 frame.GetDescription(strm);
202 bool select_thread = false;
203 StopReason stop_reason = thread.GetStopReason();
204 if (m_verbose) printf("tid = 0x%llx pc = 0x%llx ",thread.GetThreadID(),frame.GetPC());
207 case eStopReasonNone:
212 case eStopReasonTrace:
213 select_thread = true;
218 case eStopReasonPlanComplete:
219 select_thread = true;
221 printf("plan complete\n");
223 case eStopReasonThreadExiting:
225 printf("thread exiting\n");
227 case eStopReasonExec:
231 case eStopReasonInvalid:
235 case eStopReasonException:
236 select_thread = true;
238 printf("exception\n");
241 case eStopReasonBreakpoint:
242 select_thread = true;
244 printf("breakpoint id = %lld.%lld\n",thread.GetStopReasonDataAtIndex(0),thread.GetStopReasonDataAtIndex(1));
246 case eStopReasonWatchpoint:
247 select_thread = true;
249 printf("watchpoint id = %lld\n",thread.GetStopReasonDataAtIndex(0));
251 case eStopReasonSignal:
252 select_thread = true;
254 printf("signal %d\n",(int)thread.GetStopReasonDataAtIndex(0));
257 if (select_thread && !selected_thread)
260 selected_thread = m_process.SetSelectedThread(thread);
265 if (m_verbose) Xcode::RunCommand(m_debugger,"bt all",true);
275 call_test_step = true;
282 printf("RUNNING STEP %d\n",m_step);
284 TestStep(m_step, action);
289 case ActionWanted::Type::eNone:
290 // Just exit and wait for the next event
292 case ActionWanted::Type::eContinue:
293 err = m_process.Continue();
295 case ActionWanted::Type::eStepOut:
296 if (action.thread.IsValid() == false)
300 Xcode::RunCommand(m_debugger,"bt all",true);
301 printf("error: invalid thread for step out on step %d\n", m_step);
305 m_process.SetSelectedThread(action.thread);
306 action.thread.StepOut();
308 case ActionWanted::Type::eStepOver:
309 if (action.thread.IsValid() == false)
313 Xcode::RunCommand(m_debugger,"bt all",true);
314 printf("error: invalid thread for step over %d\n",m_step);
318 m_process.SetSelectedThread(action.thread);
319 action.thread.StepOver();
321 case ActionWanted::Type::eRelaunch:
322 if (m_process.IsValid())
327 Launch(action.launch_info);
329 case ActionWanted::Type::eKill:
334 case ActionWanted::Type::eCallNext:
342 if (GetVerbose()) printf("I am gonna die at step %d\n",m_step);
346 TestCase::Run (TestCase& test, int argc, const char** argv)
348 if (test.Setup(argc, argv))
352 test.WriteResults(results);
356 return RUN_SETUP_ERROR;