blob: 6a17bb4cc69a24c3f1215d2d1dcb4d591c3ef322 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman7a62c8b2010-06-09 07:44:37 +000010#include "lldb/API/SBTarget.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
Greg Claytonb3448432011-03-24 21:19:54 +000012#include "lldb/lldb-public.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013
Greg Clayton917c0002011-06-29 22:09:02 +000014#include "lldb/API/SBDebugger.h"
15#include "lldb/API/SBBreakpoint.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/API/SBFileSpec.h"
Greg Clayton917c0002011-06-29 22:09:02 +000017#include "lldb/API/SBListener.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/API/SBModule.h"
Greg Clayton917c0002011-06-29 22:09:02 +000019#include "lldb/API/SBProcess.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000020#include "lldb/API/SBStream.h"
Greg Clayton4ed315f2011-06-21 01:34:41 +000021#include "lldb/API/SBSymbolContextList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointID.h"
23#include "lldb/Breakpoint/BreakpointIDList.h"
24#include "lldb/Breakpoint/BreakpointList.h"
25#include "lldb/Breakpoint/BreakpointLocation.h"
26#include "lldb/Core/Address.h"
27#include "lldb/Core/AddressResolver.h"
28#include "lldb/Core/AddressResolverName.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Core/ArchSpec.h"
30#include "lldb/Core/Debugger.h"
31#include "lldb/Core/Disassembler.h"
Caroline Tice7826c882010-10-26 03:11:13 +000032#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Core/RegularExpression.h"
34#include "lldb/Core/SearchFilter.h"
35#include "lldb/Core/STLUtils.h"
Greg Clayton917c0002011-06-29 22:09:02 +000036#include "lldb/Core/ValueObjectList.h"
37#include "lldb/Core/ValueObjectVariable.h"
38#include "lldb/Host/FileSpec.h"
Greg Claytoncd548032011-02-01 01:31:41 +000039#include "lldb/Host/Host.h"
Greg Clayton917c0002011-06-29 22:09:02 +000040#include "lldb/Interpreter/Args.h"
41#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000042#include "lldb/Target/Process.h"
43#include "lldb/Target/Target.h"
44#include "lldb/Target/TargetList.h"
45
46#include "lldb/Interpreter/CommandReturnObject.h"
47#include "../source/Commands/CommandObjectBreakpoint.h"
48
Chris Lattner24943d22010-06-08 16:52:24 +000049
50using namespace lldb;
51using namespace lldb_private;
52
53#define DEFAULT_DISASM_BYTE_SIZE 32
54
55//----------------------------------------------------------------------
56// SBTarget constructor
57//----------------------------------------------------------------------
Greg Claytonc3b61d22010-12-15 05:08:08 +000058SBTarget::SBTarget () :
59 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000060{
61}
62
63SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000064 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000065{
66}
67
68SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000069 m_opaque_sp (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000070{
71}
72
Greg Clayton538eb822010-11-05 23:17:00 +000073const SBTarget&
74SBTarget::operator = (const SBTarget& rhs)
75{
76 if (this != &rhs)
77 m_opaque_sp = rhs.m_opaque_sp;
78 return *this;
79}
80
Chris Lattner24943d22010-06-08 16:52:24 +000081//----------------------------------------------------------------------
82// Destructor
83//----------------------------------------------------------------------
84SBTarget::~SBTarget()
85{
86}
87
88bool
89SBTarget::IsValid () const
90{
Greg Clayton63094e02010-06-23 01:19:29 +000091 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000092}
93
94SBProcess
95SBTarget::GetProcess ()
96{
Caroline Tice7826c882010-10-26 03:11:13 +000097
Chris Lattner24943d22010-06-08 16:52:24 +000098 SBProcess sb_process;
Greg Clayton63094e02010-06-23 01:19:29 +000099 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000100 {
101 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000102 sb_process.SetProcess (m_opaque_sp->GetProcessSP());
Greg Claytonbdcda462010-12-20 20:49:23 +0000103 }
Caroline Tice7826c882010-10-26 03:11:13 +0000104
Greg Claytone005f2c2010-11-06 01:53:30 +0000105 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000106 if (log)
107 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000108 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
109 m_opaque_sp.get(), sb_process.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000110 }
111
Chris Lattner24943d22010-06-08 16:52:24 +0000112 return sb_process;
113}
114
Greg Clayton63094e02010-06-23 01:19:29 +0000115SBDebugger
116SBTarget::GetDebugger () const
117{
118 SBDebugger debugger;
119 if (m_opaque_sp)
120 debugger.reset (m_opaque_sp->GetDebugger().GetSP());
121 return debugger;
122}
123
Jim Inghamb5871fe2011-03-31 00:01:24 +0000124SBProcess
125SBTarget::LaunchSimple
126(
127 char const **argv,
128 char const **envp,
129 const char *working_directory
130)
131{
132 char *stdin_path = NULL;
133 char *stdout_path = NULL;
134 char *stderr_path = NULL;
135 uint32_t launch_flags = 0;
136 bool stop_at_entry = false;
137 SBError error;
138 SBListener listener = GetDebugger().GetListener();
139 return Launch (listener,
140 argv,
141 envp,
142 stdin_path,
143 stdout_path,
144 stderr_path,
145 working_directory,
146 launch_flags,
147 stop_at_entry,
148 error);
149}
Greg Claytonde915be2011-01-23 05:56:20 +0000150
151SBProcess
152SBTarget::Launch
153(
Greg Clayton271a5db2011-02-03 21:28:34 +0000154 SBListener &listener,
Greg Claytonde915be2011-01-23 05:56:20 +0000155 char const **argv,
156 char const **envp,
157 const char *stdin_path,
158 const char *stdout_path,
159 const char *stderr_path,
160 const char *working_directory,
161 uint32_t launch_flags, // See LaunchFlags
162 bool stop_at_entry,
163 lldb::SBError& error
164)
165{
Greg Claytone005f2c2010-11-06 01:53:30 +0000166 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000167
168 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000169 {
Greg Claytonde915be2011-01-23 05:56:20 +0000170 log->Printf ("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...",
171 m_opaque_sp.get(),
172 argv,
173 envp,
174 stdin_path ? stdin_path : "NULL",
175 stdout_path ? stdout_path : "NULL",
176 stderr_path ? stderr_path : "NULL",
177 working_directory ? working_directory : "NULL",
178 launch_flags,
179 stop_at_entry,
180 error.get());
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000181 }
Greg Clayton1a3083a2010-10-06 03:53:16 +0000182 SBProcess sb_process;
183 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000184 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000185 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000186
Greg Clayton7c330d62011-01-27 01:01:10 +0000187 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
188 launch_flags |= eLaunchFlagDisableASLR;
189
Greg Clayton180546b2011-04-30 01:09:13 +0000190 StateType state = eStateInvalid;
191 sb_process.SetProcess (m_opaque_sp->GetProcessSP());
192 if (sb_process.IsValid())
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000193 {
Greg Clayton180546b2011-04-30 01:09:13 +0000194 state = sb_process->GetState();
195
196 if (sb_process->IsAlive() && state != eStateConnected)
197 {
198 if (state == eStateAttaching)
199 error.SetErrorString ("process attach is in progress");
200 else
201 error.SetErrorString ("a process is already being debugged");
202 sb_process.Clear();
203 return sb_process;
204 }
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000205 }
206
Greg Clayton180546b2011-04-30 01:09:13 +0000207 if (state == eStateConnected)
208 {
209 // If we are already connected, then we have already specified the
210 // listener, so if a valid listener is supplied, we need to error out
211 // to let the client know.
212 if (listener.IsValid())
213 {
214 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
215 sb_process.Clear();
216 return sb_process;
217 }
218 }
219 else
Greg Claytonc5f728c2010-10-06 22:10:17 +0000220 {
Greg Clayton271a5db2011-02-03 21:28:34 +0000221 if (listener.IsValid())
222 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
223 else
224 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
Greg Clayton180546b2011-04-30 01:09:13 +0000225 }
Greg Clayton7c330d62011-01-27 01:01:10 +0000226
Greg Clayton180546b2011-04-30 01:09:13 +0000227 if (sb_process.IsValid())
228 {
229 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
230 launch_flags |= eLaunchFlagDisableSTDIO;
231
232 error.SetError (sb_process->Launch (argv, envp, launch_flags, stdin_path, stdout_path, stderr_path, working_directory));
233 if (error.Success())
Greg Clayton7c330d62011-01-27 01:01:10 +0000234 {
Greg Clayton180546b2011-04-30 01:09:13 +0000235 // We we are stopping at the entry point, we can return now!
236 if (stop_at_entry)
237 return sb_process;
Greg Clayton7c330d62011-01-27 01:01:10 +0000238
Greg Clayton180546b2011-04-30 01:09:13 +0000239 // Make sure we are stopped at the entry
240 StateType state = sb_process->WaitForProcessToStop (NULL);
241 if (state == eStateStopped)
Greg Clayton7c330d62011-01-27 01:01:10 +0000242 {
Greg Clayton180546b2011-04-30 01:09:13 +0000243 // resume the process to skip the entry point
244 error.SetError (sb_process->Resume());
245 if (error.Success())
Greg Clayton7c330d62011-01-27 01:01:10 +0000246 {
Greg Clayton180546b2011-04-30 01:09:13 +0000247 // If we are doing synchronous mode, then wait for the
248 // process to stop yet again!
249 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
250 sb_process->WaitForProcessToStop (NULL);
Greg Clayton7c330d62011-01-27 01:01:10 +0000251 }
252 }
253 }
Greg Clayton180546b2011-04-30 01:09:13 +0000254 }
255 else
256 {
257 error.SetErrorString ("unable to create lldb_private::Process");
Greg Claytonc5f728c2010-10-06 22:10:17 +0000258 }
259 }
260 else
261 {
262 error.SetErrorString ("SBTarget is invalid");
Chris Lattner24943d22010-06-08 16:52:24 +0000263 }
Caroline Tice7826c882010-10-26 03:11:13 +0000264
Caroline Tice926060e2010-10-29 21:48:37 +0000265 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000266 if (log)
267 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000268 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
269 m_opaque_sp.get(), sb_process.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000270 }
271
Greg Clayton1a3083a2010-10-06 03:53:16 +0000272 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000273}
274
Greg Claytonc5f728c2010-10-06 22:10:17 +0000275
276lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000277SBTarget::AttachToProcessWithID
Greg Claytonc5f728c2010-10-06 22:10:17 +0000278(
Greg Clayton271a5db2011-02-03 21:28:34 +0000279 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000280 lldb::pid_t pid,// The process ID to attach to
281 SBError& error // An error explaining what went wrong if attach fails
282)
283{
284 SBProcess sb_process;
285 if (m_opaque_sp)
286 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000287 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Claytonde1dd812011-06-24 03:21:43 +0000288
289 StateType state = eStateInvalid;
290 sb_process.SetProcess (m_opaque_sp->GetProcessSP());
291 if (sb_process.IsValid())
292 {
293 state = sb_process->GetState();
294
295 if (sb_process->IsAlive() && state != eStateConnected)
296 {
297 if (state == eStateAttaching)
298 error.SetErrorString ("process attach is in progress");
299 else
300 error.SetErrorString ("a process is already being debugged");
301 sb_process.Clear();
302 return sb_process;
303 }
304 }
305
306 if (state == eStateConnected)
307 {
308 // If we are already connected, then we have already specified the
309 // listener, so if a valid listener is supplied, we need to error out
310 // to let the client know.
311 if (listener.IsValid())
312 {
313 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
314 sb_process.Clear();
315 return sb_process;
316 }
317 }
Greg Clayton271a5db2011-02-03 21:28:34 +0000318 else
Greg Claytonde1dd812011-06-24 03:21:43 +0000319 {
320 if (listener.IsValid())
321 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
322 else
323 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
324 }
Greg Claytonc5f728c2010-10-06 22:10:17 +0000325
326 if (sb_process.IsValid())
327 {
328 error.SetError (sb_process->Attach (pid));
Johnny Chen535960e2011-06-17 00:51:15 +0000329 // If we are doing synchronous mode, then wait for the
330 // process to stop!
331 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
332 sb_process->WaitForProcessToStop (NULL);
Greg Claytonc5f728c2010-10-06 22:10:17 +0000333 }
334 else
335 {
336 error.SetErrorString ("unable to create lldb_private::Process");
337 }
338 }
339 else
340 {
341 error.SetErrorString ("SBTarget is invalid");
342 }
343 return sb_process;
344
345}
346
347lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000348SBTarget::AttachToProcessWithName
Greg Claytonc5f728c2010-10-06 22:10:17 +0000349(
Greg Clayton271a5db2011-02-03 21:28:34 +0000350 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000351 const char *name, // basename of process to attach to
352 bool wait_for, // if true wait for a new instance of "name" to be launched
353 SBError& error // An error explaining what went wrong if attach fails
354)
355{
356 SBProcess sb_process;
357 if (m_opaque_sp)
358 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000359 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
360
Greg Claytonde1dd812011-06-24 03:21:43 +0000361 StateType state = eStateInvalid;
362 sb_process.SetProcess (m_opaque_sp->GetProcessSP());
363 if (sb_process.IsValid())
364 {
365 state = sb_process->GetState();
366
367 if (sb_process->IsAlive() && state != eStateConnected)
368 {
369 if (state == eStateAttaching)
370 error.SetErrorString ("process attach is in progress");
371 else
372 error.SetErrorString ("a process is already being debugged");
373 sb_process.Clear();
374 return sb_process;
375 }
376 }
377
378 if (state == eStateConnected)
379 {
380 // If we are already connected, then we have already specified the
381 // listener, so if a valid listener is supplied, we need to error out
382 // to let the client know.
383 if (listener.IsValid())
384 {
385 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
386 sb_process.Clear();
387 return sb_process;
388 }
389 }
Greg Clayton271a5db2011-02-03 21:28:34 +0000390 else
Greg Claytonde1dd812011-06-24 03:21:43 +0000391 {
392 if (listener.IsValid())
393 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
394 else
395 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
396 }
Greg Claytonc5f728c2010-10-06 22:10:17 +0000397
398 if (sb_process.IsValid())
399 {
400 error.SetError (sb_process->Attach (name, wait_for));
Johnny Chen58d02ff2011-06-17 19:21:30 +0000401 // If we are doing synchronous mode, then wait for the
402 // process to stop!
403 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
404 sb_process->WaitForProcessToStop (NULL);
Greg Claytonc5f728c2010-10-06 22:10:17 +0000405 }
406 else
407 {
408 error.SetErrorString ("unable to create lldb_private::Process");
409 }
410 }
411 else
412 {
413 error.SetErrorString ("SBTarget is invalid");
414 }
415 return sb_process;
416
417}
418
James McIlree38093402011-03-04 00:31:13 +0000419lldb::SBProcess
420SBTarget::ConnectRemote
421(
422 SBListener &listener,
423 const char *url,
424 const char *plugin_name,
425 SBError& error
426)
427{
428 SBProcess sb_process;
429 if (m_opaque_sp)
430 {
431 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
432 if (listener.IsValid())
433 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
434 else
435 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
436
437
438 if (sb_process.IsValid())
439 {
440 error.SetError (sb_process->ConnectRemote (url));
441 }
442 else
443 {
444 error.SetErrorString ("unable to create lldb_private::Process");
445 }
446 }
447 else
448 {
449 error.SetErrorString ("SBTarget is invalid");
450 }
451 return sb_process;
452}
453
Chris Lattner24943d22010-06-08 16:52:24 +0000454SBFileSpec
455SBTarget::GetExecutable ()
456{
Caroline Tice7826c882010-10-26 03:11:13 +0000457
Chris Lattner24943d22010-06-08 16:52:24 +0000458 SBFileSpec exe_file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +0000459 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000460 {
Greg Clayton63094e02010-06-23 01:19:29 +0000461 ModuleSP exe_module_sp (m_opaque_sp->GetExecutableModule ());
Chris Lattner24943d22010-06-08 16:52:24 +0000462 if (exe_module_sp)
463 exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec());
464 }
Caroline Tice7826c882010-10-26 03:11:13 +0000465
Greg Claytone005f2c2010-11-06 01:53:30 +0000466 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000467 if (log)
468 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000469 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
470 m_opaque_sp.get(), exe_file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000471 }
472
Chris Lattner24943d22010-06-08 16:52:24 +0000473 return exe_file_spec;
474}
475
Chris Lattner24943d22010-06-08 16:52:24 +0000476bool
Chris Lattner24943d22010-06-08 16:52:24 +0000477SBTarget::operator == (const SBTarget &rhs) const
478{
Greg Clayton63094e02010-06-23 01:19:29 +0000479 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000480}
481
482bool
483SBTarget::operator != (const SBTarget &rhs) const
484{
Greg Clayton63094e02010-06-23 01:19:29 +0000485 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000486}
487
488lldb_private::Target *
Greg Clayton63094e02010-06-23 01:19:29 +0000489SBTarget::operator ->() const
Chris Lattner24943d22010-06-08 16:52:24 +0000490{
Greg Clayton63094e02010-06-23 01:19:29 +0000491 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000492}
Greg Clayton63094e02010-06-23 01:19:29 +0000493
494lldb_private::Target *
495SBTarget::get() const
Chris Lattner24943d22010-06-08 16:52:24 +0000496{
Greg Clayton63094e02010-06-23 01:19:29 +0000497 return m_opaque_sp.get();
498}
499
500void
501SBTarget::reset (const lldb::TargetSP& target_sp)
502{
503 m_opaque_sp = target_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000504}
505
Greg Claytonea49cc72010-12-12 19:25:26 +0000506bool
507SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr,
508 lldb::SBAddress& addr)
509{
Johnny Chene657fbc2011-06-29 00:05:40 +0000510 if (m_opaque_sp && addr.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +0000511 {
512 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Claytonea49cc72010-12-12 19:25:26 +0000513 return m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, *addr);
Greg Claytonbdcda462010-12-20 20:49:23 +0000514 }
Greg Claytonea49cc72010-12-12 19:25:26 +0000515
Johnny Chene657fbc2011-06-29 00:05:40 +0000516 if (addr.IsValid())
517 addr->Clear();
Greg Claytonea49cc72010-12-12 19:25:26 +0000518 return false;
519}
520
Greg Claytonafb81862011-03-02 21:34:46 +0000521SBSymbolContext
522SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
523{
524 SBSymbolContext sc;
Johnny Chene657fbc2011-06-29 00:05:40 +0000525 if (m_opaque_sp && addr.IsValid())
Greg Claytonafb81862011-03-02 21:34:46 +0000526 m_opaque_sp->GetImages().ResolveSymbolContextForAddress (*addr, resolve_scope, sc.ref());
527 return sc;
528}
529
530
Chris Lattner24943d22010-06-08 16:52:24 +0000531SBBreakpoint
532SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
533{
Greg Claytond6d806c2010-11-08 00:28:40 +0000534 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner24943d22010-06-08 16:52:24 +0000535}
536
537SBBreakpoint
538SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
539{
Greg Claytone005f2c2010-11-06 01:53:30 +0000540 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000541
Chris Lattner24943d22010-06-08 16:52:24 +0000542 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000543 if (m_opaque_sp.get() && line != 0)
Greg Claytonbdcda462010-12-20 20:49:23 +0000544 {
545 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000546 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000547 }
Caroline Tice7826c882010-10-26 03:11:13 +0000548
549 if (log)
550 {
551 SBStream sstr;
552 sb_bp.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +0000553 char path[PATH_MAX];
554 sb_file_spec->GetPath (path, sizeof(path));
555 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytona66ba462010-10-30 04:51:46 +0000556 m_opaque_sp.get(),
Greg Clayton49ce6822010-10-31 03:01:06 +0000557 path,
Greg Claytona66ba462010-10-30 04:51:46 +0000558 line,
Greg Clayton49ce6822010-10-31 03:01:06 +0000559 sb_bp.get(),
Caroline Tice7826c882010-10-26 03:11:13 +0000560 sstr.GetData());
561 }
562
Chris Lattner24943d22010-06-08 16:52:24 +0000563 return sb_bp;
564}
565
566SBBreakpoint
567SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
568{
Greg Claytone005f2c2010-11-06 01:53:30 +0000569 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000570
Chris Lattner24943d22010-06-08 16:52:24 +0000571 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000572 if (m_opaque_sp.get() && symbol_name && symbol_name[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000573 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000574 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000575 if (module_name && module_name[0])
576 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000577 FileSpec module_file_spec(module_name, false);
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000578 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000579 }
580 else
581 {
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000582 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 }
584 }
Caroline Tice7826c882010-10-26 03:11:13 +0000585
586 if (log)
587 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000588 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
589 m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000590 }
591
Chris Lattner24943d22010-06-08 16:52:24 +0000592 return sb_bp;
593}
594
595SBBreakpoint
596SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
597{
Greg Claytone005f2c2010-11-06 01:53:30 +0000598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000599
Chris Lattner24943d22010-06-08 16:52:24 +0000600 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000601 if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000602 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000603 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000604 RegularExpression regexp(symbol_name_regex);
605
606 if (module_name && module_name[0])
607 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000608 FileSpec module_file_spec(module_name, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000609
Greg Clayton63094e02010-06-23 01:19:29 +0000610 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000611 }
612 else
613 {
Greg Clayton63094e02010-06-23 01:19:29 +0000614 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000615 }
616 }
Caroline Tice7826c882010-10-26 03:11:13 +0000617
618 if (log)
619 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000620 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
621 m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000622 }
623
Chris Lattner24943d22010-06-08 16:52:24 +0000624 return sb_bp;
625}
626
627
628
629SBBreakpoint
630SBTarget::BreakpointCreateByAddress (addr_t address)
631{
Greg Claytone005f2c2010-11-06 01:53:30 +0000632 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000633
Chris Lattner24943d22010-06-08 16:52:24 +0000634 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000635 if (m_opaque_sp.get())
Greg Claytonbdcda462010-12-20 20:49:23 +0000636 {
637 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000638 *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000639 }
Caroline Tice7826c882010-10-26 03:11:13 +0000640
641 if (log)
642 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000643 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (%p, address=%p) => SBBreakpoint(%p)", m_opaque_sp.get(), address, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000644 }
645
Chris Lattner24943d22010-06-08 16:52:24 +0000646 return sb_bp;
647}
648
Chris Lattner24943d22010-06-08 16:52:24 +0000649SBBreakpoint
650SBTarget::FindBreakpointByID (break_id_t bp_id)
651{
Greg Claytone005f2c2010-11-06 01:53:30 +0000652 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000653
Chris Lattner24943d22010-06-08 16:52:24 +0000654 SBBreakpoint sb_breakpoint;
Greg Clayton63094e02010-06-23 01:19:29 +0000655 if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonbdcda462010-12-20 20:49:23 +0000656 {
657 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000658 *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000659 }
Caroline Tice7826c882010-10-26 03:11:13 +0000660
661 if (log)
662 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000663 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
664 m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000665 }
666
Chris Lattner24943d22010-06-08 16:52:24 +0000667 return sb_breakpoint;
668}
669
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000670uint32_t
671SBTarget::GetNumBreakpoints () const
672{
673 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000674 {
675 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000676 return m_opaque_sp->GetBreakpointList().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +0000677 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000678 return 0;
679}
680
681SBBreakpoint
682SBTarget::GetBreakpointAtIndex (uint32_t idx) const
683{
684 SBBreakpoint sb_breakpoint;
685 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000686 {
687 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000688 *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonbdcda462010-12-20 20:49:23 +0000689 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000690 return sb_breakpoint;
691}
Chris Lattner24943d22010-06-08 16:52:24 +0000692
693bool
694SBTarget::BreakpointDelete (break_id_t bp_id)
695{
Greg Claytone005f2c2010-11-06 01:53:30 +0000696 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000697
Caroline Tice7826c882010-10-26 03:11:13 +0000698 bool result = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000699 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000700 {
701 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Caroline Tice7826c882010-10-26 03:11:13 +0000702 result = m_opaque_sp->RemoveBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000703 }
Caroline Tice7826c882010-10-26 03:11:13 +0000704
705 if (log)
706 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000707 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
Caroline Tice7826c882010-10-26 03:11:13 +0000708 }
709
710 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000711}
712
713bool
714SBTarget::EnableAllBreakpoints ()
715{
Greg Clayton63094e02010-06-23 01:19:29 +0000716 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000717 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000718 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000719 m_opaque_sp->EnableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000720 return true;
721 }
722 return false;
723}
724
725bool
726SBTarget::DisableAllBreakpoints ()
727{
Greg Clayton63094e02010-06-23 01:19:29 +0000728 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000729 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000730 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000731 m_opaque_sp->DisableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000732 return true;
733 }
734 return false;
735}
736
737bool
738SBTarget::DeleteAllBreakpoints ()
739{
Greg Clayton63094e02010-06-23 01:19:29 +0000740 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000741 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000742 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000743 m_opaque_sp->RemoveAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000744 return true;
745 }
746 return false;
747}
748
749
750uint32_t
751SBTarget::GetNumModules () const
752{
Greg Claytone005f2c2010-11-06 01:53:30 +0000753 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000754
Caroline Tice7826c882010-10-26 03:11:13 +0000755 uint32_t num = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000756 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000757 {
758 // The module list is thread safe, no need to lock
759 num = m_opaque_sp->GetImages().GetSize();
760 }
Caroline Tice7826c882010-10-26 03:11:13 +0000761
762 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000763 log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
Caroline Tice7826c882010-10-26 03:11:13 +0000764
765 return num;
Chris Lattner24943d22010-06-08 16:52:24 +0000766}
767
Greg Clayton43490d12010-07-30 20:12:55 +0000768void
769SBTarget::Clear ()
770{
Greg Claytone005f2c2010-11-06 01:53:30 +0000771 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000772
773 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000774 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000775
Greg Clayton43490d12010-07-30 20:12:55 +0000776 m_opaque_sp.reset();
777}
778
779
Chris Lattner24943d22010-06-08 16:52:24 +0000780SBModule
781SBTarget::FindModule (const SBFileSpec &sb_file_spec)
782{
783 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000784 if (m_opaque_sp && sb_file_spec.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +0000785 {
786 // The module list is thread safe, no need to lock
Greg Clayton24bc5d92011-03-30 18:16:51 +0000787 sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
Greg Claytonbdcda462010-12-20 20:49:23 +0000788 }
Chris Lattner24943d22010-06-08 16:52:24 +0000789 return sb_module;
790}
791
792SBModule
793SBTarget::GetModuleAtIndex (uint32_t idx)
794{
Greg Claytone005f2c2010-11-06 01:53:30 +0000795 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000796
Chris Lattner24943d22010-06-08 16:52:24 +0000797 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000798 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000799 {
800 // The module list is thread safe, no need to lock
Greg Clayton63094e02010-06-23 01:19:29 +0000801 sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
Greg Claytonbdcda462010-12-20 20:49:23 +0000802 }
Caroline Tice7826c882010-10-26 03:11:13 +0000803
804 if (log)
805 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000806 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
807 m_opaque_sp.get(), idx, sb_module.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000808 }
809
Chris Lattner24943d22010-06-08 16:52:24 +0000810 return sb_module;
811}
812
813
814SBBroadcaster
815SBTarget::GetBroadcaster () const
816{
Greg Claytone005f2c2010-11-06 01:53:30 +0000817 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000818
Greg Clayton63094e02010-06-23 01:19:29 +0000819 SBBroadcaster broadcaster(m_opaque_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000820
821 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000822 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000823 m_opaque_sp.get(), broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000824
Chris Lattner24943d22010-06-08 16:52:24 +0000825 return broadcaster;
826}
827
Caroline Tice98f930f2010-09-20 05:20:02 +0000828
829bool
Caroline Tice7826c882010-10-26 03:11:13 +0000830SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Tice98f930f2010-09-20 05:20:02 +0000831{
832 if (m_opaque_sp)
Caroline Ticee7a566e2010-09-20 16:21:41 +0000833 {
Caroline Ticee49ec182010-09-22 23:01:29 +0000834 description.ref();
Caroline Tice7826c882010-10-26 03:11:13 +0000835 m_opaque_sp->Dump (description.get(), description_level);
836 }
837 else
838 description.Printf ("No value");
839
840 return true;
841}
842
843bool
844SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
845{
846 if (m_opaque_sp)
847 {
848 description.ref();
849 m_opaque_sp->Dump (description.get(), description_level);
Caroline Ticee7a566e2010-09-20 16:21:41 +0000850 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000851 else
852 description.Printf ("No value");
853
854 return true;
855}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000856
857
858uint32_t
859SBTarget::FindFunctions (const char *name,
860 uint32_t name_type_mask,
861 bool append,
862 lldb::SBSymbolContextList& sc_list)
863{
864 if (!append)
865 sc_list.Clear();
866 if (m_opaque_sp)
867 {
868 const bool symbols_ok = true;
869 return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
870 name_type_mask,
871 symbols_ok,
872 append,
873 *sc_list);
874 }
875 return 0;
876}
877
Greg Clayton917c0002011-06-29 22:09:02 +0000878SBValueList
879SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
880{
881 SBValueList sb_value_list;
882
883 if (m_opaque_sp)
884 {
885 VariableList variable_list;
886 const bool append = true;
887 const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
888 append,
889 max_matches,
890 variable_list);
891
892 if (match_count > 0)
893 {
894 ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
895 if (exe_scope == NULL)
896 exe_scope = m_opaque_sp.get();
897 ValueObjectList &value_object_list = sb_value_list.ref();
898 for (uint32_t i=0; i<match_count; ++i)
899 {
900 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
901 if (valobj_sp)
902 value_object_list.Append(valobj_sp);
903 }
904 }
905 }
906
907 return sb_value_list;
908}
909