blob: 2250440211533ed1d810deb5671c7ed1a55b0301 [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 Claytona3955062011-07-22 16:46:35 +0000506lldb::SBAddress
507SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonea49cc72010-12-12 19:25:26 +0000508{
Greg Claytona3955062011-07-22 16:46:35 +0000509 lldb::SBAddress sb_addr;
510 Address &addr = sb_addr.ref();
511 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000512 {
513 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Claytona3955062011-07-22 16:46:35 +0000514 if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
515 return sb_addr;
Greg Claytonbdcda462010-12-20 20:49:23 +0000516 }
Greg Claytonea49cc72010-12-12 19:25:26 +0000517
Greg Claytona3955062011-07-22 16:46:35 +0000518 // We have a load address that isn't in a section, just return an address
519 // with the offset filled in (the address) and the section set to NULL
520 addr.SetSection(NULL);
521 addr.SetOffset(vm_addr);
522 return sb_addr;
Greg Claytonea49cc72010-12-12 19:25:26 +0000523}
524
Greg Claytonafb81862011-03-02 21:34:46 +0000525SBSymbolContext
526SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
527{
528 SBSymbolContext sc;
Johnny Chene657fbc2011-06-29 00:05:40 +0000529 if (m_opaque_sp && addr.IsValid())
Greg Claytona3955062011-07-22 16:46:35 +0000530 m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
Greg Claytonafb81862011-03-02 21:34:46 +0000531 return sc;
532}
533
534
Chris Lattner24943d22010-06-08 16:52:24 +0000535SBBreakpoint
536SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
537{
Greg Claytond6d806c2010-11-08 00:28:40 +0000538 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner24943d22010-06-08 16:52:24 +0000539}
540
541SBBreakpoint
542SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
543{
Greg Claytone005f2c2010-11-06 01:53:30 +0000544 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000545
Chris Lattner24943d22010-06-08 16:52:24 +0000546 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000547 if (m_opaque_sp.get() && line != 0)
Greg Claytonbdcda462010-12-20 20:49:23 +0000548 {
549 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000550 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000551 }
Caroline Tice7826c882010-10-26 03:11:13 +0000552
553 if (log)
554 {
555 SBStream sstr;
556 sb_bp.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +0000557 char path[PATH_MAX];
558 sb_file_spec->GetPath (path, sizeof(path));
559 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytona66ba462010-10-30 04:51:46 +0000560 m_opaque_sp.get(),
Greg Clayton49ce6822010-10-31 03:01:06 +0000561 path,
Greg Claytona66ba462010-10-30 04:51:46 +0000562 line,
Greg Clayton49ce6822010-10-31 03:01:06 +0000563 sb_bp.get(),
Caroline Tice7826c882010-10-26 03:11:13 +0000564 sstr.GetData());
565 }
566
Chris Lattner24943d22010-06-08 16:52:24 +0000567 return sb_bp;
568}
569
570SBBreakpoint
571SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
572{
Greg Claytone005f2c2010-11-06 01:53:30 +0000573 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000574
Chris Lattner24943d22010-06-08 16:52:24 +0000575 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000576 if (m_opaque_sp.get() && symbol_name && symbol_name[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000577 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000578 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000579 if (module_name && module_name[0])
580 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000581 FileSpec module_file_spec(module_name, false);
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000582 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 }
584 else
585 {
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000586 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000587 }
588 }
Caroline Tice7826c882010-10-26 03:11:13 +0000589
590 if (log)
591 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000592 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
593 m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000594 }
595
Chris Lattner24943d22010-06-08 16:52:24 +0000596 return sb_bp;
597}
598
599SBBreakpoint
600SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
601{
Greg Claytone005f2c2010-11-06 01:53:30 +0000602 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000603
Chris Lattner24943d22010-06-08 16:52:24 +0000604 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000605 if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000606 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000607 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000608 RegularExpression regexp(symbol_name_regex);
609
610 if (module_name && module_name[0])
611 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000612 FileSpec module_file_spec(module_name, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000613
Greg Clayton63094e02010-06-23 01:19:29 +0000614 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000615 }
616 else
617 {
Greg Clayton63094e02010-06-23 01:19:29 +0000618 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000619 }
620 }
Caroline Tice7826c882010-10-26 03:11:13 +0000621
622 if (log)
623 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000624 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
625 m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000626 }
627
Chris Lattner24943d22010-06-08 16:52:24 +0000628 return sb_bp;
629}
630
631
632
633SBBreakpoint
634SBTarget::BreakpointCreateByAddress (addr_t address)
635{
Greg Claytone005f2c2010-11-06 01:53:30 +0000636 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000637
Chris Lattner24943d22010-06-08 16:52:24 +0000638 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000639 if (m_opaque_sp.get())
Greg Claytonbdcda462010-12-20 20:49:23 +0000640 {
641 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000642 *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000643 }
Caroline Tice7826c882010-10-26 03:11:13 +0000644
645 if (log)
646 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000647 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 +0000648 }
649
Chris Lattner24943d22010-06-08 16:52:24 +0000650 return sb_bp;
651}
652
Chris Lattner24943d22010-06-08 16:52:24 +0000653SBBreakpoint
654SBTarget::FindBreakpointByID (break_id_t bp_id)
655{
Greg Claytone005f2c2010-11-06 01:53:30 +0000656 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000657
Chris Lattner24943d22010-06-08 16:52:24 +0000658 SBBreakpoint sb_breakpoint;
Greg Clayton63094e02010-06-23 01:19:29 +0000659 if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonbdcda462010-12-20 20:49:23 +0000660 {
661 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000662 *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000663 }
Caroline Tice7826c882010-10-26 03:11:13 +0000664
665 if (log)
666 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000667 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
668 m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000669 }
670
Chris Lattner24943d22010-06-08 16:52:24 +0000671 return sb_breakpoint;
672}
673
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000674uint32_t
675SBTarget::GetNumBreakpoints () const
676{
677 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000678 {
679 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000680 return m_opaque_sp->GetBreakpointList().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +0000681 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000682 return 0;
683}
684
685SBBreakpoint
686SBTarget::GetBreakpointAtIndex (uint32_t idx) const
687{
688 SBBreakpoint sb_breakpoint;
689 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000690 {
691 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000692 *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonbdcda462010-12-20 20:49:23 +0000693 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000694 return sb_breakpoint;
695}
Chris Lattner24943d22010-06-08 16:52:24 +0000696
697bool
698SBTarget::BreakpointDelete (break_id_t bp_id)
699{
Greg Claytone005f2c2010-11-06 01:53:30 +0000700 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000701
Caroline Tice7826c882010-10-26 03:11:13 +0000702 bool result = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000703 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000704 {
705 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Caroline Tice7826c882010-10-26 03:11:13 +0000706 result = m_opaque_sp->RemoveBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000707 }
Caroline Tice7826c882010-10-26 03:11:13 +0000708
709 if (log)
710 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000711 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 +0000712 }
713
714 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000715}
716
717bool
718SBTarget::EnableAllBreakpoints ()
719{
Greg Clayton63094e02010-06-23 01:19:29 +0000720 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000721 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000722 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000723 m_opaque_sp->EnableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000724 return true;
725 }
726 return false;
727}
728
729bool
730SBTarget::DisableAllBreakpoints ()
731{
Greg Clayton63094e02010-06-23 01:19:29 +0000732 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000733 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000734 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000735 m_opaque_sp->DisableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000736 return true;
737 }
738 return false;
739}
740
741bool
742SBTarget::DeleteAllBreakpoints ()
743{
Greg Clayton63094e02010-06-23 01:19:29 +0000744 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000745 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000746 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000747 m_opaque_sp->RemoveAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000748 return true;
749 }
750 return false;
751}
752
753
754uint32_t
755SBTarget::GetNumModules () const
756{
Greg Claytone005f2c2010-11-06 01:53:30 +0000757 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000758
Caroline Tice7826c882010-10-26 03:11:13 +0000759 uint32_t num = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000760 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000761 {
762 // The module list is thread safe, no need to lock
763 num = m_opaque_sp->GetImages().GetSize();
764 }
Caroline Tice7826c882010-10-26 03:11:13 +0000765
766 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000767 log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
Caroline Tice7826c882010-10-26 03:11:13 +0000768
769 return num;
Chris Lattner24943d22010-06-08 16:52:24 +0000770}
771
Greg Clayton43490d12010-07-30 20:12:55 +0000772void
773SBTarget::Clear ()
774{
Greg Claytone005f2c2010-11-06 01:53:30 +0000775 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000776
777 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000778 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000779
Greg Clayton43490d12010-07-30 20:12:55 +0000780 m_opaque_sp.reset();
781}
782
783
Chris Lattner24943d22010-06-08 16:52:24 +0000784SBModule
785SBTarget::FindModule (const SBFileSpec &sb_file_spec)
786{
787 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000788 if (m_opaque_sp && sb_file_spec.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +0000789 {
790 // The module list is thread safe, no need to lock
Greg Clayton24bc5d92011-03-30 18:16:51 +0000791 sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
Greg Claytonbdcda462010-12-20 20:49:23 +0000792 }
Chris Lattner24943d22010-06-08 16:52:24 +0000793 return sb_module;
794}
795
796SBModule
797SBTarget::GetModuleAtIndex (uint32_t idx)
798{
Greg Claytone005f2c2010-11-06 01:53:30 +0000799 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000800
Chris Lattner24943d22010-06-08 16:52:24 +0000801 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000802 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000803 {
804 // The module list is thread safe, no need to lock
Greg Clayton63094e02010-06-23 01:19:29 +0000805 sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
Greg Claytonbdcda462010-12-20 20:49:23 +0000806 }
Caroline Tice7826c882010-10-26 03:11:13 +0000807
808 if (log)
809 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000810 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
811 m_opaque_sp.get(), idx, sb_module.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000812 }
813
Chris Lattner24943d22010-06-08 16:52:24 +0000814 return sb_module;
815}
816
817
818SBBroadcaster
819SBTarget::GetBroadcaster () const
820{
Greg Claytone005f2c2010-11-06 01:53:30 +0000821 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000822
Greg Clayton63094e02010-06-23 01:19:29 +0000823 SBBroadcaster broadcaster(m_opaque_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000824
825 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000826 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000827 m_opaque_sp.get(), broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000828
Chris Lattner24943d22010-06-08 16:52:24 +0000829 return broadcaster;
830}
831
Caroline Tice98f930f2010-09-20 05:20:02 +0000832
833bool
Caroline Tice7826c882010-10-26 03:11:13 +0000834SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Tice98f930f2010-09-20 05:20:02 +0000835{
836 if (m_opaque_sp)
Caroline Ticee7a566e2010-09-20 16:21:41 +0000837 {
Caroline Ticee49ec182010-09-22 23:01:29 +0000838 description.ref();
Caroline Tice7826c882010-10-26 03:11:13 +0000839 m_opaque_sp->Dump (description.get(), description_level);
840 }
841 else
842 description.Printf ("No value");
843
844 return true;
845}
846
847bool
848SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
849{
850 if (m_opaque_sp)
851 {
852 description.ref();
853 m_opaque_sp->Dump (description.get(), description_level);
Caroline Ticee7a566e2010-09-20 16:21:41 +0000854 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000855 else
856 description.Printf ("No value");
857
858 return true;
859}
Greg Clayton4ed315f2011-06-21 01:34:41 +0000860
861
862uint32_t
863SBTarget::FindFunctions (const char *name,
864 uint32_t name_type_mask,
865 bool append,
866 lldb::SBSymbolContextList& sc_list)
867{
868 if (!append)
869 sc_list.Clear();
870 if (m_opaque_sp)
871 {
872 const bool symbols_ok = true;
873 return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
874 name_type_mask,
875 symbols_ok,
876 append,
877 *sc_list);
878 }
879 return 0;
880}
881
Greg Clayton917c0002011-06-29 22:09:02 +0000882SBValueList
883SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
884{
885 SBValueList sb_value_list;
886
887 if (m_opaque_sp)
888 {
889 VariableList variable_list;
890 const bool append = true;
891 const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
892 append,
893 max_matches,
894 variable_list);
895
896 if (match_count > 0)
897 {
898 ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
899 if (exe_scope == NULL)
900 exe_scope = m_opaque_sp.get();
901 ValueObjectList &value_object_list = sb_value_list.ref();
902 for (uint32_t i=0; i<match_count; ++i)
903 {
904 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
905 if (valobj_sp)
906 value_object_list.Append(valobj_sp);
907 }
908 }
909 }
910
911 return sb_value_list;
912}
913