blob: 74bf423c1dbc9216011da480056d5e6e28cb7d0a [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"
28#include "lldb/Core/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(
124 char const **argv,
125 char const **envp,
126 const char *stdin_path,
127 const char *stdout_path,
128 const char *stderr_path,
129 const char *working_directory,
130 uint32_t launch_flags, // See LaunchFlags
131 bool stop_at_entry,
132 lldb::SBError& error
133)
134{
Greg Claytone005f2c2010-11-06 01:53:30 +0000135 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000136
137 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000138 {
Greg Claytonde915be2011-01-23 05:56:20 +0000139 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))...",
140 m_opaque_sp.get(),
141 argv,
142 envp,
143 stdin_path ? stdin_path : "NULL",
144 stdout_path ? stdout_path : "NULL",
145 stderr_path ? stderr_path : "NULL",
146 working_directory ? working_directory : "NULL",
147 launch_flags,
148 stop_at_entry,
149 error.get());
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000150 }
Greg Clayton1a3083a2010-10-06 03:53:16 +0000151 SBProcess sb_process;
152 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000153 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000154 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000155
Greg Clayton7c330d62011-01-27 01:01:10 +0000156 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
157 launch_flags |= eLaunchFlagDisableASLR;
158
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000159 static const char *g_launch_tty = NULL;
160 static bool g_got_launch_tty = false;
161 if (!g_got_launch_tty)
162 {
163 // Get the LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY only once
164 g_got_launch_tty = true;
165 g_launch_tty = ::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY");
166 if (g_launch_tty)
167 {
168 // LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY is a path to a terminal to reuse
169 // if the first character is '/', else it is a boolean value.
170 if (g_launch_tty[0] != '/')
171 {
172 if (Args::StringToBoolean(g_launch_tty, false, NULL))
173 g_launch_tty = "";
174 else
175 g_launch_tty = NULL;
176 }
177 }
178 }
179
180 if ((launch_flags & eLaunchFlagLaunchInTTY) || g_launch_tty)
Chris Lattner24943d22010-06-08 16:52:24 +0000181 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000182 ArchSpec arch (m_opaque_sp->GetArchitecture ());
183
184 Module *exe_module = m_opaque_sp->GetExecutableModule().get();
185 if (exe_module)
Chris Lattner24943d22010-06-08 16:52:24 +0000186 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000187 char exec_file_path[PATH_MAX];
188 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
189 if (exe_module->GetFileSpec().Exists())
Greg Clayton1a3083a2010-10-06 03:53:16 +0000190 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000191 // Make a new argument vector
192 std::vector<const char *> exec_path_plus_argv;
193 // Append the resolved executable path
194 exec_path_plus_argv.push_back (exec_file_path);
195
196 // Push all args if there are any
197 if (argv)
Greg Clayton1a3083a2010-10-06 03:53:16 +0000198 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000199 for (int i = 0; argv[i]; ++i)
200 exec_path_plus_argv.push_back(argv[i]);
201 }
202
203 // Push a NULL to terminate the args.
204 exec_path_plus_argv.push_back(NULL);
205
206
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000207 const char *tty_name = NULL;
208 if (g_launch_tty && g_launch_tty[0] == '/')
209 tty_name = g_launch_tty;
210
211 lldb::pid_t pid = Host::LaunchInNewTerminal (tty_name,
212 &exec_path_plus_argv[0],
213 envp,
214 working_directory,
215 &arch,
216 true,
217 launch_flags & eLaunchFlagDisableASLR);
218
Greg Clayton7c330d62011-01-27 01:01:10 +0000219 if (pid != LLDB_INVALID_PROCESS_ID)
220 {
221 sb_process = AttachToProcessWithID(pid, error);
222 }
223 else
224 {
225 error.SetErrorStringWithFormat("failed to launch process in terminal");
Greg Clayton1a3083a2010-10-06 03:53:16 +0000226 }
227 }
Greg Clayton7c330d62011-01-27 01:01:10 +0000228 else
229 {
230 error.SetErrorStringWithFormat("executable doesn't exist: \"%s\"", exec_file_path);
231 }
232 }
233 else
234 {
235 error.SetErrorStringWithFormat("invalid executable");
Chris Lattner24943d22010-06-08 16:52:24 +0000236 }
237 }
Greg Claytonc5f728c2010-10-06 22:10:17 +0000238 else
239 {
Greg Clayton7c330d62011-01-27 01:01:10 +0000240 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
241
242 if (sb_process.IsValid())
243 {
244
245 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
246 launch_flags |= eLaunchFlagDisableSTDIO;
247
248
249 error.SetError (sb_process->Launch (argv, envp, launch_flags, stdin_path, stdout_path, stderr_path, working_directory));
250 if (error.Success())
251 {
252 // We we are stopping at the entry point, we can return now!
253 if (stop_at_entry)
254 return sb_process;
255
256 // Make sure we are stopped at the entry
257 StateType state = sb_process->WaitForProcessToStop (NULL);
258 if (state == eStateStopped)
259 {
260 // resume the process to skip the entry point
261 error.SetError (sb_process->Resume());
262 if (error.Success())
263 {
264 // If we are doing synchronous mode, then wait for the
265 // process to stop yet again!
266 if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
267 sb_process->WaitForProcessToStop (NULL);
268 }
269 }
270 }
271 }
272 else
273 {
274 error.SetErrorString ("unable to create lldb_private::Process");
275 }
Greg Claytonc5f728c2010-10-06 22:10:17 +0000276 }
277 }
278 else
279 {
280 error.SetErrorString ("SBTarget is invalid");
Chris Lattner24943d22010-06-08 16:52:24 +0000281 }
Caroline Tice7826c882010-10-26 03:11:13 +0000282
Caroline Tice926060e2010-10-29 21:48:37 +0000283 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000284 if (log)
285 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000286 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
287 m_opaque_sp.get(), sb_process.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000288 }
289
Greg Clayton1a3083a2010-10-06 03:53:16 +0000290 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000291}
292
Greg Claytonc5f728c2010-10-06 22:10:17 +0000293
294lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000295SBTarget::AttachToProcessWithID
Greg Claytonc5f728c2010-10-06 22:10:17 +0000296(
297 lldb::pid_t pid,// The process ID to attach to
298 SBError& error // An error explaining what went wrong if attach fails
299)
300{
301 SBProcess sb_process;
302 if (m_opaque_sp)
303 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000304 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
305 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
Greg Claytonc5f728c2010-10-06 22:10:17 +0000306
307 if (sb_process.IsValid())
308 {
309 error.SetError (sb_process->Attach (pid));
310 }
311 else
312 {
313 error.SetErrorString ("unable to create lldb_private::Process");
314 }
315 }
316 else
317 {
318 error.SetErrorString ("SBTarget is invalid");
319 }
320 return sb_process;
321
322}
323
324lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000325SBTarget::AttachToProcessWithName
Greg Claytonc5f728c2010-10-06 22:10:17 +0000326(
327 const char *name, // basename of process to attach to
328 bool wait_for, // if true wait for a new instance of "name" to be launched
329 SBError& error // An error explaining what went wrong if attach fails
330)
331{
332 SBProcess sb_process;
333 if (m_opaque_sp)
334 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000335 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
336
337 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
Greg Claytonc5f728c2010-10-06 22:10:17 +0000338
339 if (sb_process.IsValid())
340 {
341 error.SetError (sb_process->Attach (name, wait_for));
342 }
343 else
344 {
345 error.SetErrorString ("unable to create lldb_private::Process");
346 }
347 }
348 else
349 {
350 error.SetErrorString ("SBTarget is invalid");
351 }
352 return sb_process;
353
354}
355
Chris Lattner24943d22010-06-08 16:52:24 +0000356SBFileSpec
357SBTarget::GetExecutable ()
358{
Caroline Tice7826c882010-10-26 03:11:13 +0000359
Chris Lattner24943d22010-06-08 16:52:24 +0000360 SBFileSpec exe_file_spec;
Greg Clayton63094e02010-06-23 01:19:29 +0000361 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000362 {
Greg Clayton63094e02010-06-23 01:19:29 +0000363 ModuleSP exe_module_sp (m_opaque_sp->GetExecutableModule ());
Chris Lattner24943d22010-06-08 16:52:24 +0000364 if (exe_module_sp)
365 exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec());
366 }
Caroline Tice7826c882010-10-26 03:11:13 +0000367
Greg Claytone005f2c2010-11-06 01:53:30 +0000368 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000369 if (log)
370 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000371 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
372 m_opaque_sp.get(), exe_file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000373 }
374
Chris Lattner24943d22010-06-08 16:52:24 +0000375 return exe_file_spec;
376}
377
378
379bool
380SBTarget::DeleteTargetFromList (TargetList *list)
381{
Greg Clayton63094e02010-06-23 01:19:29 +0000382 if (m_opaque_sp)
383 return list->DeleteTarget (m_opaque_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000384 else
385 return false;
386}
387
388bool
Chris Lattner24943d22010-06-08 16:52:24 +0000389SBTarget::operator == (const SBTarget &rhs) const
390{
Greg Clayton63094e02010-06-23 01:19:29 +0000391 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000392}
393
394bool
395SBTarget::operator != (const SBTarget &rhs) const
396{
Greg Clayton63094e02010-06-23 01:19:29 +0000397 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000398}
399
400lldb_private::Target *
Greg Clayton63094e02010-06-23 01:19:29 +0000401SBTarget::operator ->() const
Chris Lattner24943d22010-06-08 16:52:24 +0000402{
Greg Clayton63094e02010-06-23 01:19:29 +0000403 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000404}
Greg Clayton63094e02010-06-23 01:19:29 +0000405
406lldb_private::Target *
407SBTarget::get() const
Chris Lattner24943d22010-06-08 16:52:24 +0000408{
Greg Clayton63094e02010-06-23 01:19:29 +0000409 return m_opaque_sp.get();
410}
411
412void
413SBTarget::reset (const lldb::TargetSP& target_sp)
414{
415 m_opaque_sp = target_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000416}
417
Greg Claytonea49cc72010-12-12 19:25:26 +0000418bool
419SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr,
420 lldb::SBAddress& addr)
421{
422 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000423 {
424 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Claytonea49cc72010-12-12 19:25:26 +0000425 return m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, *addr);
Greg Claytonbdcda462010-12-20 20:49:23 +0000426 }
Greg Claytonea49cc72010-12-12 19:25:26 +0000427
428 addr->Clear();
429 return false;
430}
431
Chris Lattner24943d22010-06-08 16:52:24 +0000432SBBreakpoint
433SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
434{
Greg Claytond6d806c2010-11-08 00:28:40 +0000435 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner24943d22010-06-08 16:52:24 +0000436}
437
438SBBreakpoint
439SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
440{
Greg Claytone005f2c2010-11-06 01:53:30 +0000441 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000442
Chris Lattner24943d22010-06-08 16:52:24 +0000443 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000444 if (m_opaque_sp.get() && line != 0)
Greg Claytonbdcda462010-12-20 20:49:23 +0000445 {
446 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000447 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000448 }
Caroline Tice7826c882010-10-26 03:11:13 +0000449
450 if (log)
451 {
452 SBStream sstr;
453 sb_bp.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +0000454 char path[PATH_MAX];
455 sb_file_spec->GetPath (path, sizeof(path));
456 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytona66ba462010-10-30 04:51:46 +0000457 m_opaque_sp.get(),
Greg Clayton49ce6822010-10-31 03:01:06 +0000458 path,
Greg Claytona66ba462010-10-30 04:51:46 +0000459 line,
Greg Clayton49ce6822010-10-31 03:01:06 +0000460 sb_bp.get(),
Caroline Tice7826c882010-10-26 03:11:13 +0000461 sstr.GetData());
462 }
463
Chris Lattner24943d22010-06-08 16:52:24 +0000464 return sb_bp;
465}
466
467SBBreakpoint
468SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
469{
Greg Claytone005f2c2010-11-06 01:53:30 +0000470 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000471
Chris Lattner24943d22010-06-08 16:52:24 +0000472 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000473 if (m_opaque_sp.get() && symbol_name && symbol_name[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000474 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000475 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000476 if (module_name && module_name[0])
477 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000478 FileSpec module_file_spec(module_name, false);
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000479 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000480 }
481 else
482 {
Jim Ingham7ec03bd2010-09-30 21:21:43 +0000483 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000484 }
485 }
Caroline Tice7826c882010-10-26 03:11:13 +0000486
487 if (log)
488 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000489 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
490 m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000491 }
492
Chris Lattner24943d22010-06-08 16:52:24 +0000493 return sb_bp;
494}
495
496SBBreakpoint
497SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
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() && symbol_name_regex && symbol_name_regex[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000503 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000504 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000505 RegularExpression regexp(symbol_name_regex);
506
507 if (module_name && module_name[0])
508 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000509 FileSpec module_file_spec(module_name, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000510
Greg Clayton63094e02010-06-23 01:19:29 +0000511 *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000512 }
513 else
514 {
Greg Clayton63094e02010-06-23 01:19:29 +0000515 *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000516 }
517 }
Caroline Tice7826c882010-10-26 03:11:13 +0000518
519 if (log)
520 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000521 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
522 m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000523 }
524
Chris Lattner24943d22010-06-08 16:52:24 +0000525 return sb_bp;
526}
527
528
529
530SBBreakpoint
531SBTarget::BreakpointCreateByAddress (addr_t address)
532{
Greg Claytone005f2c2010-11-06 01:53:30 +0000533 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000534
Chris Lattner24943d22010-06-08 16:52:24 +0000535 SBBreakpoint sb_bp;
Greg Clayton63094e02010-06-23 01:19:29 +0000536 if (m_opaque_sp.get())
Greg Claytonbdcda462010-12-20 20:49:23 +0000537 {
538 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000539 *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
Greg Claytonbdcda462010-12-20 20:49:23 +0000540 }
Caroline Tice7826c882010-10-26 03:11:13 +0000541
542 if (log)
543 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000544 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 +0000545 }
546
Chris Lattner24943d22010-06-08 16:52:24 +0000547 return sb_bp;
548}
549
Chris Lattner24943d22010-06-08 16:52:24 +0000550SBBreakpoint
551SBTarget::FindBreakpointByID (break_id_t bp_id)
552{
Greg Claytone005f2c2010-11-06 01:53:30 +0000553 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000554
Chris Lattner24943d22010-06-08 16:52:24 +0000555 SBBreakpoint sb_breakpoint;
Greg Clayton63094e02010-06-23 01:19:29 +0000556 if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonbdcda462010-12-20 20:49:23 +0000557 {
558 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000559 *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000560 }
Caroline Tice7826c882010-10-26 03:11:13 +0000561
562 if (log)
563 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000564 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
565 m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000566 }
567
Chris Lattner24943d22010-06-08 16:52:24 +0000568 return sb_breakpoint;
569}
570
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000571uint32_t
572SBTarget::GetNumBreakpoints () const
573{
574 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000575 {
576 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000577 return m_opaque_sp->GetBreakpointList().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +0000578 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000579 return 0;
580}
581
582SBBreakpoint
583SBTarget::GetBreakpointAtIndex (uint32_t idx) const
584{
585 SBBreakpoint sb_breakpoint;
586 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000587 {
588 // The breakpoint list is thread safe, no need to lock
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000589 *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonbdcda462010-12-20 20:49:23 +0000590 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000591 return sb_breakpoint;
592}
Chris Lattner24943d22010-06-08 16:52:24 +0000593
594bool
595SBTarget::BreakpointDelete (break_id_t bp_id)
596{
Greg Claytone005f2c2010-11-06 01:53:30 +0000597 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000598
Caroline Tice7826c882010-10-26 03:11:13 +0000599 bool result = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000600 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000601 {
602 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Caroline Tice7826c882010-10-26 03:11:13 +0000603 result = m_opaque_sp->RemoveBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +0000604 }
Caroline Tice7826c882010-10-26 03:11:13 +0000605
606 if (log)
607 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000608 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 +0000609 }
610
611 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000612}
613
614bool
615SBTarget::EnableAllBreakpoints ()
616{
Greg Clayton63094e02010-06-23 01:19:29 +0000617 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000618 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000619 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000620 m_opaque_sp->EnableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000621 return true;
622 }
623 return false;
624}
625
626bool
627SBTarget::DisableAllBreakpoints ()
628{
Greg Clayton63094e02010-06-23 01:19:29 +0000629 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000630 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000631 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000632 m_opaque_sp->DisableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000633 return true;
634 }
635 return false;
636}
637
638bool
639SBTarget::DeleteAllBreakpoints ()
640{
Greg Clayton63094e02010-06-23 01:19:29 +0000641 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000642 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000643 Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000644 m_opaque_sp->RemoveAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +0000645 return true;
646 }
647 return false;
648}
649
650
651uint32_t
652SBTarget::GetNumModules () const
653{
Greg Claytone005f2c2010-11-06 01:53:30 +0000654 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000655
Caroline Tice7826c882010-10-26 03:11:13 +0000656 uint32_t num = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000657 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000658 {
659 // The module list is thread safe, no need to lock
660 num = m_opaque_sp->GetImages().GetSize();
661 }
Caroline Tice7826c882010-10-26 03:11:13 +0000662
663 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000664 log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
Caroline Tice7826c882010-10-26 03:11:13 +0000665
666 return num;
Chris Lattner24943d22010-06-08 16:52:24 +0000667}
668
Greg Clayton43490d12010-07-30 20:12:55 +0000669void
670SBTarget::Clear ()
671{
Greg Claytone005f2c2010-11-06 01:53:30 +0000672 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000673
674 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000675 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000676
Greg Clayton43490d12010-07-30 20:12:55 +0000677 m_opaque_sp.reset();
678}
679
680
Chris Lattner24943d22010-06-08 16:52:24 +0000681SBModule
682SBTarget::FindModule (const SBFileSpec &sb_file_spec)
683{
684 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000685 if (m_opaque_sp && sb_file_spec.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +0000686 {
687 // The module list is thread safe, no need to lock
Greg Clayton63094e02010-06-23 01:19:29 +0000688 sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL));
Greg Claytonbdcda462010-12-20 20:49:23 +0000689 }
Chris Lattner24943d22010-06-08 16:52:24 +0000690 return sb_module;
691}
692
693SBModule
694SBTarget::GetModuleAtIndex (uint32_t idx)
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
Chris Lattner24943d22010-06-08 16:52:24 +0000698 SBModule sb_module;
Greg Clayton63094e02010-06-23 01:19:29 +0000699 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000700 {
701 // The module list is thread safe, no need to lock
Greg Clayton63094e02010-06-23 01:19:29 +0000702 sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
Greg Claytonbdcda462010-12-20 20:49:23 +0000703 }
Caroline Tice7826c882010-10-26 03:11:13 +0000704
705 if (log)
706 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000707 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
708 m_opaque_sp.get(), idx, sb_module.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000709 }
710
Chris Lattner24943d22010-06-08 16:52:24 +0000711 return sb_module;
712}
713
714
715SBBroadcaster
716SBTarget::GetBroadcaster () const
717{
Greg Claytone005f2c2010-11-06 01:53:30 +0000718 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000719
Greg Clayton63094e02010-06-23 01:19:29 +0000720 SBBroadcaster broadcaster(m_opaque_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000721
722 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000723 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000724 m_opaque_sp.get(), broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000725
Chris Lattner24943d22010-06-08 16:52:24 +0000726 return broadcaster;
727}
728
Caroline Tice98f930f2010-09-20 05:20:02 +0000729
730bool
Caroline Tice7826c882010-10-26 03:11:13 +0000731SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Tice98f930f2010-09-20 05:20:02 +0000732{
733 if (m_opaque_sp)
Caroline Ticee7a566e2010-09-20 16:21:41 +0000734 {
Caroline Ticee49ec182010-09-22 23:01:29 +0000735 description.ref();
Caroline Tice7826c882010-10-26 03:11:13 +0000736 m_opaque_sp->Dump (description.get(), description_level);
737 }
738 else
739 description.Printf ("No value");
740
741 return true;
742}
743
744bool
745SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
746{
747 if (m_opaque_sp)
748 {
749 description.ref();
750 m_opaque_sp->Dump (description.get(), description_level);
Caroline Ticee7a566e2010-09-20 16:21:41 +0000751 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000752 else
753 description.Printf ("No value");
754
755 return true;
756}