blob: d1a1f275e336f442e240762816c2494f2dad213a [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
12#include "lldb/lldb-include.h"
13
14#include "lldb/API/SBFileSpec.h"
15#include "lldb/API/SBModule.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000016#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Breakpoint/BreakpointID.h"
18#include "lldb/Breakpoint/BreakpointIDList.h"
19#include "lldb/Breakpoint/BreakpointList.h"
20#include "lldb/Breakpoint/BreakpointLocation.h"
21#include "lldb/Core/Address.h"
22#include "lldb/Core/AddressResolver.h"
23#include "lldb/Core/AddressResolverName.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000024#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/ArchSpec.h"
26#include "lldb/Core/Debugger.h"
27#include "lldb/Core/Disassembler.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000028#include "lldb/Host/FileSpec.h"
Caroline Tice7826c882010-10-26 03:11:13 +000029#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Core/RegularExpression.h"
31#include "lldb/Core/SearchFilter.h"
32#include "lldb/Core/STLUtils.h"
Greg Claytoncd548032011-02-01 01:31:41 +000033#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/Target/Process.h"
35#include "lldb/Target/Target.h"
36#include "lldb/Target/TargetList.h"
37
38#include "lldb/Interpreter/CommandReturnObject.h"
39#include "../source/Commands/CommandObjectBreakpoint.h"
40
Eli Friedman7a62c8b2010-06-09 07:44:37 +000041#include "lldb/API/SBDebugger.h"
42#include "lldb/API/SBProcess.h"
43#include "lldb/API/SBListener.h"
44#include "lldb/API/SBBreakpoint.h"
Chris Lattner24943d22010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
49#define DEFAULT_DISASM_BYTE_SIZE 32
50
51//----------------------------------------------------------------------
52// SBTarget constructor
53//----------------------------------------------------------------------
Greg Claytonc3b61d22010-12-15 05:08:08 +000054SBTarget::SBTarget () :
55 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000056{
57}
58
59SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000060 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000061{
62}
63
64SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000065 m_opaque_sp (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000066{
67}
68
Greg Clayton538eb822010-11-05 23:17:00 +000069const SBTarget&
70SBTarget::operator = (const SBTarget& rhs)
71{
72 if (this != &rhs)
73 m_opaque_sp = rhs.m_opaque_sp;
74 return *this;
75}
76
Chris Lattner24943d22010-06-08 16:52:24 +000077//----------------------------------------------------------------------
78// Destructor
79//----------------------------------------------------------------------
80SBTarget::~SBTarget()
81{
82}
83
84bool
85SBTarget::IsValid () const
86{
Greg Clayton63094e02010-06-23 01:19:29 +000087 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000088}
89
90SBProcess
91SBTarget::GetProcess ()
92{
Caroline Tice7826c882010-10-26 03:11:13 +000093
Chris Lattner24943d22010-06-08 16:52:24 +000094 SBProcess sb_process;
Greg Clayton63094e02010-06-23 01:19:29 +000095 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +000096 {
97 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +000098 sb_process.SetProcess (m_opaque_sp->GetProcessSP());
Greg Claytonbdcda462010-12-20 20:49:23 +000099 }
Caroline Tice7826c882010-10-26 03:11:13 +0000100
Greg Claytone005f2c2010-11-06 01:53:30 +0000101 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000102 if (log)
103 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000104 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
105 m_opaque_sp.get(), sb_process.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000106 }
107
Chris Lattner24943d22010-06-08 16:52:24 +0000108 return sb_process;
109}
110
Greg Clayton63094e02010-06-23 01:19:29 +0000111SBDebugger
112SBTarget::GetDebugger () const
113{
114 SBDebugger debugger;
115 if (m_opaque_sp)
116 debugger.reset (m_opaque_sp->GetDebugger().GetSP());
117 return debugger;
118}
119
Greg Claytonde915be2011-01-23 05:56:20 +0000120
121SBProcess
122SBTarget::Launch
123(
Greg Clayton271a5db2011-02-03 21:28:34 +0000124 SBListener &listener,
Greg Claytonde915be2011-01-23 05:56:20 +0000125 char const **argv,
126 char const **envp,
127 const char *stdin_path,
128 const char *stdout_path,
129 const char *stderr_path,
130 const char *working_directory,
131 uint32_t launch_flags, // See LaunchFlags
132 bool stop_at_entry,
133 lldb::SBError& error
134)
135{
Greg Claytone005f2c2010-11-06 01:53:30 +0000136 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000137
138 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000139 {
Greg Claytonde915be2011-01-23 05:56:20 +0000140 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))...",
141 m_opaque_sp.get(),
142 argv,
143 envp,
144 stdin_path ? stdin_path : "NULL",
145 stdout_path ? stdout_path : "NULL",
146 stderr_path ? stderr_path : "NULL",
147 working_directory ? working_directory : "NULL",
148 launch_flags,
149 stop_at_entry,
150 error.get());
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000151 }
Greg Clayton1a3083a2010-10-06 03:53:16 +0000152 SBProcess sb_process;
153 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000154 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000155 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000156
Greg Clayton7c330d62011-01-27 01:01:10 +0000157 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
158 launch_flags |= eLaunchFlagDisableASLR;
159
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000160 static const char *g_launch_tty = NULL;
161 static bool g_got_launch_tty = false;
162 if (!g_got_launch_tty)
163 {
164 // Get the LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY only once
165 g_got_launch_tty = true;
166 g_launch_tty = ::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY");
167 if (g_launch_tty)
168 {
169 // LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY is a path to a terminal to reuse
170 // if the first character is '/', else it is a boolean value.
171 if (g_launch_tty[0] != '/')
172 {
173 if (Args::StringToBoolean(g_launch_tty, false, NULL))
174 g_launch_tty = "";
175 else
176 g_launch_tty = NULL;
177 }
178 }
179 }
180
181 if ((launch_flags & eLaunchFlagLaunchInTTY) || g_launch_tty)
Chris Lattner24943d22010-06-08 16:52:24 +0000182 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000183 ArchSpec arch (m_opaque_sp->GetArchitecture ());
184
185 Module *exe_module = m_opaque_sp->GetExecutableModule().get();
186 if (exe_module)
Chris Lattner24943d22010-06-08 16:52:24 +0000187 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000188 char exec_file_path[PATH_MAX];
189 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
190 if (exe_module->GetFileSpec().Exists())
Greg Clayton1a3083a2010-10-06 03:53:16 +0000191 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000192 // Make a new argument vector
193 std::vector<const char *> exec_path_plus_argv;
194 // Append the resolved executable path
195 exec_path_plus_argv.push_back (exec_file_path);
196
197 // Push all args if there are any
198 if (argv)
Greg Clayton1a3083a2010-10-06 03:53:16 +0000199 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000200 for (int i = 0; argv[i]; ++i)
201 exec_path_plus_argv.push_back(argv[i]);
202 }
203
204 // Push a NULL to terminate the args.
205 exec_path_plus_argv.push_back(NULL);
206
207
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000208 const char *tty_name = NULL;
209 if (g_launch_tty && g_launch_tty[0] == '/')
210 tty_name = g_launch_tty;
211
212 lldb::pid_t pid = Host::LaunchInNewTerminal (tty_name,
213 &exec_path_plus_argv[0],
214 envp,
215 working_directory,
216 &arch,
217 true,
218 launch_flags & eLaunchFlagDisableASLR);
219
Greg Clayton7c330d62011-01-27 01:01:10 +0000220 if (pid != LLDB_INVALID_PROCESS_ID)
221 {
Greg Clayton271a5db2011-02-03 21:28:34 +0000222 sb_process = AttachToProcessWithID(listener, pid, error);
Greg Clayton7c330d62011-01-27 01:01:10 +0000223 }
224 else
225 {
226 error.SetErrorStringWithFormat("failed to launch process in terminal");
Greg Clayton1a3083a2010-10-06 03:53:16 +0000227 }
228 }
Greg Clayton7c330d62011-01-27 01:01:10 +0000229 else
230 {
231 error.SetErrorStringWithFormat("executable doesn't exist: \"%s\"", exec_file_path);
232 }
233 }
234 else
235 {
236 error.SetErrorStringWithFormat("invalid executable");
Chris Lattner24943d22010-06-08 16:52:24 +0000237 }
238 }
Greg Claytonc5f728c2010-10-06 22:10:17 +0000239 else
240 {
Greg Clayton271a5db2011-02-03 21:28:34 +0000241 if (listener.IsValid())
242 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
243 else
244 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
Greg Clayton7c330d62011-01-27 01:01:10 +0000245
246 if (sb_process.IsValid())
247 {
248
249 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
250 launch_flags |= eLaunchFlagDisableSTDIO;
251
252
253 error.SetError (sb_process->Launch (argv, envp, launch_flags, stdin_path, stdout_path, stderr_path, working_directory));
254 if (error.Success())
255 {
256 // We we are stopping at the entry point, we can return now!
257 if (stop_at_entry)
258 return sb_process;
259
260 // Make sure we are stopped at the entry
261 StateType state = sb_process->WaitForProcessToStop (NULL);
262 if (state == eStateStopped)
263 {
264 // resume the process to skip the entry point
265 error.SetError (sb_process->Resume());
266 if (error.Success())
267 {
268 // If we are doing synchronous mode, then wait for the
269 // process to stop yet again!
270 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
271 sb_process->WaitForProcessToStop (NULL);
272 }
273 }
274 }
275 }
276 else
277 {
278 error.SetErrorString ("unable to create lldb_private::Process");
279 }
Greg Claytonc5f728c2010-10-06 22:10:17 +0000280 }
281 }
282 else
283 {
284 error.SetErrorString ("SBTarget is invalid");
Chris Lattner24943d22010-06-08 16:52:24 +0000285 }
Caroline Tice7826c882010-10-26 03:11:13 +0000286
Caroline Tice926060e2010-10-29 21:48:37 +0000287 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000288 if (log)
289 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000290 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
291 m_opaque_sp.get(), sb_process.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000292 }
293
Greg Clayton1a3083a2010-10-06 03:53:16 +0000294 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000295}
296
Greg Claytonc5f728c2010-10-06 22:10:17 +0000297
298lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000299SBTarget::AttachToProcessWithID
Greg Claytonc5f728c2010-10-06 22:10:17 +0000300(
Greg Clayton271a5db2011-02-03 21:28:34 +0000301 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000302 lldb::pid_t pid,// The process ID to attach to
303 SBError& error // An error explaining what went wrong if attach fails
304)
305{
306 SBProcess sb_process;
307 if (m_opaque_sp)
308 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000309 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton271a5db2011-02-03 21:28:34 +0000310 if (listener.IsValid())
311 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
312 else
313 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
314
Greg Claytonc5f728c2010-10-06 22:10:17 +0000315
316 if (sb_process.IsValid())
317 {
318 error.SetError (sb_process->Attach (pid));
319 }
320 else
321 {
322 error.SetErrorString ("unable to create lldb_private::Process");
323 }
324 }
325 else
326 {
327 error.SetErrorString ("SBTarget is invalid");
328 }
329 return sb_process;
330
331}
332
333lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000334SBTarget::AttachToProcessWithName
Greg Claytonc5f728c2010-10-06 22:10:17 +0000335(
Greg Clayton271a5db2011-02-03 21:28:34 +0000336 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000337 const char *name, // basename of process to attach to
338 bool wait_for, // if true wait for a new instance of "name" to be launched
339 SBError& error // An error explaining what went wrong if attach fails
340)
341{
342 SBProcess sb_process;
343 if (m_opaque_sp)
344 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000345 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
346
Greg Clayton271a5db2011-02-03 21:28:34 +0000347 if (listener.IsValid())
348 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
349 else
350 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
Greg Claytonc5f728c2010-10-06 22:10:17 +0000351
352 if (sb_process.IsValid())
353 {
354 error.SetError (sb_process->Attach (name, wait_for));
355 }
356 else
357 {
358 error.SetErrorString ("unable to create lldb_private::Process");
359 }
360 }
361 else
362 {
363 error.SetErrorString ("SBTarget is invalid");
364 }
365 return sb_process;
366
367}
368
James McIlree38093402011-03-04 00:31:13 +0000369lldb::SBProcess
370SBTarget::ConnectRemote
371(
372 SBListener &listener,
373 const char *url,
374 const char *plugin_name,
375 SBError& error
376)
377{
378 SBProcess sb_process;
379 if (m_opaque_sp)
380 {
381 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
382 if (listener.IsValid())
383 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
384 else
385 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
386
387
388 if (sb_process.IsValid())
389 {
390 error.SetError (sb_process->ConnectRemote (url));
391 }
392 else
393 {
394 error.SetErrorString ("unable to create lldb_private::Process");
395 }
396 }
397 else
398 {
399 error.SetErrorString ("SBTarget is invalid");
400 }
401 return sb_process;
402}
403
Chris Lattner24943d22010-06-08 16:52:24 +0000404SBFileSpec
405SBTarget::GetExecutable ()
406{
Caroline Tice7826c882010-10-26 03:11:13 +0000407
Chris Lattner24943d22010-06-08 16:52:24 +0000408 SBFileSpec exe_file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +0000409 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000410 {
Greg Clayton63094e02010-06-23 01:19:29 +0000411 ModuleSP exe_module_sp (m_opaque_sp->GetExecutableModule ());
Chris Lattner24943d22010-06-08 16:52:24 +0000412 if (exe_module_sp)
413 exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec());
414 }
Caroline Tice7826c882010-10-26 03:11:13 +0000415
Greg Claytone005f2c2010-11-06 01:53:30 +0000416 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000417 if (log)
418 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000419 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
420 m_opaque_sp.get(), exe_file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000421 }
422
Chris Lattner24943d22010-06-08 16:52:24 +0000423 return exe_file_spec;
424}
425
426
427bool
428SBTarget::DeleteTargetFromList (TargetList *list)
429{
Greg Clayton63094e02010-06-23 01:19:29 +0000430 if (m_opaque_sp)
431 return list->DeleteTarget (m_opaque_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000432 else
433 return false;
434}
435
436bool
Chris Lattner24943d22010-06-08 16:52:24 +0000437SBTarget::operator == (const SBTarget &rhs) const
438{
Greg Clayton63094e02010-06-23 01:19:29 +0000439 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000440}
441
442bool
443SBTarget::operator != (const SBTarget &rhs) const
444{
Greg Clayton63094e02010-06-23 01:19:29 +0000445 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000446}
447
448lldb_private::Target *
Greg Clayton63094e02010-06-23 01:19:29 +0000449SBTarget::operator ->() const
Chris Lattner24943d22010-06-08 16:52:24 +0000450{
Greg Clayton63094e02010-06-23 01:19:29 +0000451 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000452}
Greg Clayton63094e02010-06-23 01:19:29 +0000453
454lldb_private::Target *
455SBTarget::get() const
Chris Lattner24943d22010-06-08 16:52:24 +0000456{
Greg Clayton63094e02010-06-23 01:19:29 +0000457 return m_opaque_sp.get();
458}
459
460void
461SBTarget::reset (const lldb::TargetSP& target_sp)
462{
463 m_opaque_sp = target_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000464}
465
Greg Claytonea49cc72010-12-12 19:25:26 +0000466bool
467SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr,
468 lldb::SBAddress& addr)
469{
470 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000471 {
472 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Claytonea49cc72010-12-12 19:25:26 +0000473 return m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, *addr);
Greg Claytonbdcda462010-12-20 20:49:23 +0000474 }
Greg Claytonea49cc72010-12-12 19:25:26 +0000475
476 addr->Clear();
477 return false;
478}
479
Greg Claytonafb81862011-03-02 21:34:46 +0000480SBSymbolContext
481SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
482{
483 SBSymbolContext sc;
484 if (m_opaque_sp)
485 m_opaque_sp->GetImages().ResolveSymbolContextForAddress (*addr, resolve_scope, sc.ref());
486 return sc;
487}
488
489
Chris Lattner24943d22010-06-08 16:52:24 +0000490SBBreakpoint
491SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
492{
Greg Claytond6d806c2010-11-08 00:28:40 +0000493 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner24943d22010-06-08 16:52:24 +0000494}
495
496SBBreakpoint
497SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
498{
Greg Claytone005f2c2010-11-06 01:53:30 +0000499 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000500
Chris Lattner24943d22010-06-08 16:52:24 +0000501 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000502 if (m_opaque_sp.get() && line != 0)
Greg Claytonbdcda462010-12-20 20:49:23 +0000503 {
504 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000505 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000506 }
Caroline Tice7826c882010-10-26 03:11:13 +0000507
508 if (log)
509 {
510 SBStream sstr;
511 sb_bp.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +0000512 char path[PATH_MAX];
513 sb_file_spec->GetPath (path, sizeof(path));
514 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytona66ba462010-10-30 04:51:46 +0000515 m_opaque_sp.get(),
Greg Clayton49ce6822010-10-31 03:01:06 +0000516 path,
Greg Claytona66ba462010-10-30 04:51:46 +0000517 line,
Greg Clayton49ce6822010-10-31 03:01:06 +0000518 sb_bp.get(),
Caroline Tice7826c882010-10-26 03:11:13 +0000519 sstr.GetData());
520 }
521
Chris Lattner24943d22010-06-08 16:52:24 +0000522 return sb_bp;
523}
524
525SBBreakpoint
526SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
527{
Greg Claytone005f2c2010-11-06 01:53:30 +0000528 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000529
Chris Lattner24943d22010-06-08 16:52:24 +0000530 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000531 if (m_opaque_sp.get() && symbol_name && symbol_name[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000532 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000533 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000534 if (module_name && module_name[0])
535 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000536 FileSpec module_file_spec(module_name, false);
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000537 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000538 }
539 else
540 {
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000541 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000542 }
543 }
Caroline Tice7826c882010-10-26 03:11:13 +0000544
545 if (log)
546 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000547 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
548 m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000549 }
550
Chris Lattner24943d22010-06-08 16:52:24 +0000551 return sb_bp;
552}
553
554SBBreakpoint
555SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
556{
Greg Claytone005f2c2010-11-06 01:53:30 +0000557 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000558
Chris Lattner24943d22010-06-08 16:52:24 +0000559 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000560 if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000561 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000562 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000563 RegularExpression regexp(symbol_name_regex);
564
565 if (module_name && module_name[0])
566 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000567 FileSpec module_file_spec(module_name, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000568
Greg Clayton63094e02010-06-23 01:19:29 +0000569 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000570 }
571 else
572 {
Greg Clayton63094e02010-06-23 01:19:29 +0000573 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000574 }
575 }
Caroline Tice7826c882010-10-26 03:11:13 +0000576
577 if (log)
578 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000579 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
580 m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000581 }
582
Chris Lattner24943d22010-06-08 16:52:24 +0000583 return sb_bp;
584}
585
586
587
588SBBreakpoint
589SBTarget::BreakpointCreateByAddress (addr_t address)
590{
Greg Claytone005f2c2010-11-06 01:53:30 +0000591 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000592
Chris Lattner24943d22010-06-08 16:52:24 +0000593 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000594 if (m_opaque_sp.get())
Greg Claytonbdcda462010-12-20 20:49:23 +0000595 {
596 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000597 *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000598 }
Caroline Tice7826c882010-10-26 03:11:13 +0000599
600 if (log)
601 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000602 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 +0000603 }
604
Chris Lattner24943d22010-06-08 16:52:24 +0000605 return sb_bp;
606}
607
Chris Lattner24943d22010-06-08 16:52:24 +0000608SBBreakpoint
609SBTarget::FindBreakpointByID (break_id_t bp_id)
610{
Greg Claytone005f2c2010-11-06 01:53:30 +0000611 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000612
Chris Lattner24943d22010-06-08 16:52:24 +0000613 SBBreakpoint sb_breakpoint;
Greg Clayton63094e02010-06-23 01:19:29 +0000614 if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonbdcda462010-12-20 20:49:23 +0000615 {
616 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000617 *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000618 }
Caroline Tice7826c882010-10-26 03:11:13 +0000619
620 if (log)
621 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000622 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
623 m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000624 }
625
Chris Lattner24943d22010-06-08 16:52:24 +0000626 return sb_breakpoint;
627}
628
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000629uint32_t
630SBTarget::GetNumBreakpoints () const
631{
632 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000633 {
634 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000635 return m_opaque_sp->GetBreakpointList().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +0000636 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000637 return 0;
638}
639
640SBBreakpoint
641SBTarget::GetBreakpointAtIndex (uint32_t idx) const
642{
643 SBBreakpoint sb_breakpoint;
644 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000645 {
646 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000647 *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonbdcda462010-12-20 20:49:23 +0000648 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000649 return sb_breakpoint;
650}
Chris Lattner24943d22010-06-08 16:52:24 +0000651
652bool
653SBTarget::BreakpointDelete (break_id_t bp_id)
654{
Greg Claytone005f2c2010-11-06 01:53:30 +0000655 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000656
Caroline Tice7826c882010-10-26 03:11:13 +0000657 bool result = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000658 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000659 {
660 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Caroline Tice7826c882010-10-26 03:11:13 +0000661 result = m_opaque_sp->RemoveBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000662 }
Caroline Tice7826c882010-10-26 03:11:13 +0000663
664 if (log)
665 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000666 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 +0000667 }
668
669 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000670}
671
672bool
673SBTarget::EnableAllBreakpoints ()
674{
Greg Clayton63094e02010-06-23 01:19:29 +0000675 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000676 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000677 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000678 m_opaque_sp->EnableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000679 return true;
680 }
681 return false;
682}
683
684bool
685SBTarget::DisableAllBreakpoints ()
686{
Greg Clayton63094e02010-06-23 01:19:29 +0000687 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000688 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000689 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000690 m_opaque_sp->DisableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000691 return true;
692 }
693 return false;
694}
695
696bool
697SBTarget::DeleteAllBreakpoints ()
698{
Greg Clayton63094e02010-06-23 01:19:29 +0000699 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000700 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000701 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000702 m_opaque_sp->RemoveAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000703 return true;
704 }
705 return false;
706}
707
708
709uint32_t
710SBTarget::GetNumModules () const
711{
Greg Claytone005f2c2010-11-06 01:53:30 +0000712 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000713
Caroline Tice7826c882010-10-26 03:11:13 +0000714 uint32_t num = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000715 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000716 {
717 // The module list is thread safe, no need to lock
718 num = m_opaque_sp->GetImages().GetSize();
719 }
Caroline Tice7826c882010-10-26 03:11:13 +0000720
721 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000722 log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
Caroline Tice7826c882010-10-26 03:11:13 +0000723
724 return num;
Chris Lattner24943d22010-06-08 16:52:24 +0000725}
726
Greg Clayton43490d12010-07-30 20:12:55 +0000727void
728SBTarget::Clear ()
729{
Greg Claytone005f2c2010-11-06 01:53:30 +0000730 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000731
732 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000733 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000734
Greg Clayton43490d12010-07-30 20:12:55 +0000735 m_opaque_sp.reset();
736}
737
738
Chris Lattner24943d22010-06-08 16:52:24 +0000739SBModule
740SBTarget::FindModule (const SBFileSpec &sb_file_spec)
741{
742 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000743 if (m_opaque_sp && sb_file_spec.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +0000744 {
745 // The module list is thread safe, no need to lock
Greg Clayton63094e02010-06-23 01:19:29 +0000746 sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL));
Greg Claytonbdcda462010-12-20 20:49:23 +0000747 }
Chris Lattner24943d22010-06-08 16:52:24 +0000748 return sb_module;
749}
750
751SBModule
752SBTarget::GetModuleAtIndex (uint32_t idx)
753{
Greg Claytone005f2c2010-11-06 01:53:30 +0000754 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000755
Chris Lattner24943d22010-06-08 16:52:24 +0000756 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000757 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000758 {
759 // The module list is thread safe, no need to lock
Greg Clayton63094e02010-06-23 01:19:29 +0000760 sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
Greg Claytonbdcda462010-12-20 20:49:23 +0000761 }
Caroline Tice7826c882010-10-26 03:11:13 +0000762
763 if (log)
764 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000765 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
766 m_opaque_sp.get(), idx, sb_module.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000767 }
768
Chris Lattner24943d22010-06-08 16:52:24 +0000769 return sb_module;
770}
771
772
773SBBroadcaster
774SBTarget::GetBroadcaster () const
775{
Greg Claytone005f2c2010-11-06 01:53:30 +0000776 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000777
Greg Clayton63094e02010-06-23 01:19:29 +0000778 SBBroadcaster broadcaster(m_opaque_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000779
780 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000781 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000782 m_opaque_sp.get(), broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000783
Chris Lattner24943d22010-06-08 16:52:24 +0000784 return broadcaster;
785}
786
Caroline Tice98f930f2010-09-20 05:20:02 +0000787
788bool
Caroline Tice7826c882010-10-26 03:11:13 +0000789SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Tice98f930f2010-09-20 05:20:02 +0000790{
791 if (m_opaque_sp)
Caroline Ticee7a566e2010-09-20 16:21:41 +0000792 {
Caroline Ticee49ec182010-09-22 23:01:29 +0000793 description.ref();
Caroline Tice7826c882010-10-26 03:11:13 +0000794 m_opaque_sp->Dump (description.get(), description_level);
795 }
796 else
797 description.Printf ("No value");
798
799 return true;
800}
801
802bool
803SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
804{
805 if (m_opaque_sp)
806 {
807 description.ref();
808 m_opaque_sp->Dump (description.get(), description_level);
Caroline Ticee7a566e2010-09-20 16:21:41 +0000809 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000810 else
811 description.Printf ("No value");
812
813 return true;
814}