blob: dd26c572159ffd7aebf9233ec2c276f7ae2eb337 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Eli Friedman4c5de692010-06-09 07:44:37 +000012#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytone0d378b2011-03-24 21:19:54 +000014#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Greg Claytondea8cb42011-06-29 22:09:02 +000016#include "lldb/API/SBDebugger.h"
17#include "lldb/API/SBBreakpoint.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000018#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000020#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000022#include "lldb/API/SBModuleSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000023#include "lldb/API/SBSourceManager.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000024#include "lldb/API/SBProcess.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000025#include "lldb/API/SBStream.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000026#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Breakpoint/BreakpointID.h"
28#include "lldb/Breakpoint/BreakpointIDList.h"
29#include "lldb/Breakpoint/BreakpointList.h"
30#include "lldb/Breakpoint/BreakpointLocation.h"
31#include "lldb/Core/Address.h"
32#include "lldb/Core/AddressResolver.h"
33#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/ArchSpec.h"
35#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000037#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Core/Module.h"
39#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/RegularExpression.h"
41#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000042#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Core/STLUtils.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000044#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000045#include "lldb/Core/ValueObjectList.h"
46#include "lldb/Core/ValueObjectVariable.h"
47#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000048#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000049#include "lldb/Interpreter/Args.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000050#include "lldb/Symbol/ClangASTContext.h"
51#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000052#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000053#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000054#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000055#include "lldb/Target/ABI.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000056#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000057#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000059#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "lldb/Target/Target.h"
61#include "lldb/Target/TargetList.h"
62
63#include "lldb/Interpreter/CommandReturnObject.h"
64#include "../source/Commands/CommandObjectBreakpoint.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000065#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
68using namespace lldb;
69using namespace lldb_private;
70
71#define DEFAULT_DISASM_BYTE_SIZE 32
72
Oleksiy Vyalov37386142015-02-10 22:49:57 +000073namespace {
74
75Error
76AttachToProcess (ProcessAttachInfo &attach_info, Target &target)
77{
78 Mutex::Locker api_locker (target.GetAPIMutex ());
79
80 auto process_sp = target.GetProcessSP ();
81 if (process_sp)
82 {
83 const auto state = process_sp->GetState ();
84 if (process_sp->IsAlive () && state == eStateConnected)
85 {
86 // If we are already connected, then we have already specified the
87 // listener, so if a valid listener is supplied, we need to error out
88 // to let the client know.
89 if (attach_info.GetListener ())
90 return Error ("process is connected and already has a listener, pass empty listener");
91 }
92 }
93
94 return target.Attach (attach_info, nullptr);
95}
96
97} // namespace
98
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099//----------------------------------------------------------------------
100// SBTarget constructor
101//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000102SBTarget::SBTarget () :
103 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104{
105}
106
107SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000108 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109{
110}
111
112SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000113 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114{
115}
116
Greg Claytonefabb122010-11-05 23:17:00 +0000117const SBTarget&
118SBTarget::operator = (const SBTarget& rhs)
119{
120 if (this != &rhs)
121 m_opaque_sp = rhs.m_opaque_sp;
122 return *this;
123}
124
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
128SBTarget::~SBTarget()
129{
130}
131
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000132const char *
133SBTarget::GetBroadcasterClassName ()
134{
135 return Target::GetStaticBroadcasterClass().AsCString();
136}
137
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138bool
139SBTarget::IsValid () const
140{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000141 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
144SBProcess
145SBTarget::GetProcess ()
146{
147 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000148 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000149 TargetSP target_sp(GetSP());
150 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000151 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000152 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000153 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000154 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000155
Greg Clayton5160ce52013-03-27 23:08:40 +0000156 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000157 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000158 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
159 static_cast<void*>(target_sp.get()),
160 static_cast<void*>(process_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000161
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 return sb_process;
163}
164
Matthew Gardinerc928de32014-10-22 07:22:56 +0000165SBPlatform
166SBTarget::GetPlatform ()
167{
168 TargetSP target_sp(GetSP());
169 if (!target_sp)
170 return SBPlatform();
171
172 SBPlatform platform;
173 platform.m_opaque_sp = target_sp->GetPlatform();
174
175 return platform;
176}
177
Greg Clayton66111032010-06-23 01:19:29 +0000178SBDebugger
179SBTarget::GetDebugger () const
180{
181 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000182 TargetSP target_sp(GetSP());
183 if (target_sp)
184 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000185 return debugger;
186}
187
Jim Ingham270684d2011-03-31 00:01:24 +0000188SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000189SBTarget::LoadCore (const char *core_file)
190{
191 SBProcess sb_process;
192 TargetSP target_sp(GetSP());
193 if (target_sp)
194 {
195 FileSpec filespec(core_file, true);
196 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
197 NULL,
198 &filespec));
199 if (process_sp)
200 {
201 process_sp->LoadCore();
202 sb_process.SetSP (process_sp);
203 }
204 }
205 return sb_process;
206}
207
208SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000209SBTarget::LaunchSimple
210(
211 char const **argv,
212 char const **envp,
213 const char *working_directory
214)
215{
216 char *stdin_path = NULL;
217 char *stdout_path = NULL;
218 char *stderr_path = NULL;
219 uint32_t launch_flags = 0;
220 bool stop_at_entry = false;
221 SBError error;
222 SBListener listener = GetDebugger().GetListener();
223 return Launch (listener,
224 argv,
225 envp,
226 stdin_path,
227 stdout_path,
228 stderr_path,
229 working_directory,
230 launch_flags,
231 stop_at_entry,
232 error);
233}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000234
Greg Claytonfbb76342013-11-20 21:07:01 +0000235SBError
236SBTarget::Install()
237{
238 SBError sb_error;
239 TargetSP target_sp(GetSP());
240 if (target_sp)
241 {
242 Mutex::Locker api_locker (target_sp->GetAPIMutex());
243 sb_error.ref() = target_sp->Install(NULL);
244 }
245 return sb_error;
246}
247
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000248SBProcess
249SBTarget::Launch
250(
Greg Clayton4b045622011-02-03 21:28:34 +0000251 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000252 char const **argv,
253 char const **envp,
254 const char *stdin_path,
255 const char *stdout_path,
256 const char *stderr_path,
257 const char *working_directory,
258 uint32_t launch_flags, // See LaunchFlags
259 bool stop_at_entry,
260 lldb::SBError& error
261)
262{
Greg Clayton5160ce52013-03-27 23:08:40 +0000263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000264
Greg Claytonacdbe812012-01-30 09:04:36 +0000265 SBProcess sb_process;
266 ProcessSP process_sp;
267 TargetSP target_sp(GetSP());
268
Caroline Ticeceb6b132010-10-26 03:11:13 +0000269 if (log)
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000270 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))...",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000271 static_cast<void*>(target_sp.get()),
272 static_cast<void*>(argv), static_cast<void*>(envp),
273 stdin_path ? stdin_path : "NULL",
274 stdout_path ? stdout_path : "NULL",
275 stderr_path ? stderr_path : "NULL",
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000276 working_directory ? working_directory : "NULL",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000277 launch_flags, stop_at_entry,
278 static_cast<void*>(error.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000279
280 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000282 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000283
Zachary Turner6fd3f342015-02-02 18:50:01 +0000284 if (stop_at_entry)
285 launch_flags |= eLaunchFlagStopAtEntry;
286
Greg Clayton645bf542011-01-27 01:01:10 +0000287 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
288 launch_flags |= eLaunchFlagDisableASLR;
289
Greg Clayton2289fa42011-04-30 01:09:13 +0000290 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000291 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000292 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000293 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000294 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000295
Greg Claytonb9556ac2012-01-30 07:41:31 +0000296 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000297 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000298 if (state == eStateAttaching)
299 error.SetErrorString ("process attach is in progress");
300 else
301 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000302 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000303 }
Greg Clayton931180e2011-01-27 06:44:37 +0000304 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000305
Greg Clayton2289fa42011-04-30 01:09:13 +0000306 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");
Greg Clayton2289fa42011-04-30 01:09:13 +0000314 return sb_process;
315 }
316 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000317
318 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
319 launch_flags |= eLaunchFlagDisableSTDIO;
320
321 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000322
Greg Claytonb09c5382013-12-13 17:20:18 +0000323 Module *exe_module = target_sp->GetExecutableModulePointer();
324 if (exe_module)
325 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
326 if (argv)
327 launch_info.GetArguments().AppendArguments (argv);
328 if (envp)
329 launch_info.GetEnvironmentEntries ().SetArguments (envp);
330
331 if (listener.IsValid())
Greg Clayton8012cad2014-11-17 19:39:20 +0000332 launch_info.SetListener(listener.GetSP());
333
334 error.SetError (target_sp->Launch(launch_info, NULL));
Greg Clayton645bf542011-01-27 01:01:10 +0000335
Greg Claytonb09c5382013-12-13 17:20:18 +0000336 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton524e60b2010-10-06 22:10:17 +0000337 }
338 else
339 {
340 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000342
Caroline Tice20ad3c42010-10-29 21:48:37 +0000343 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000344 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000345 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
346 static_cast<void*>(target_sp.get()),
347 static_cast<void*>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000348
Greg Clayton5d5028b2010-10-06 03:53:16 +0000349 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Greg Clayton0e615682012-02-24 05:03:03 +0000352SBProcess
353SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
354{
Greg Clayton5160ce52013-03-27 23:08:40 +0000355 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000356
Greg Clayton0e615682012-02-24 05:03:03 +0000357 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000358 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000359
Greg Clayton0e615682012-02-24 05:03:03 +0000360 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000361 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
362 static_cast<void*>(target_sp.get()));
363
Greg Clayton0e615682012-02-24 05:03:03 +0000364 if (target_sp)
365 {
366 Mutex::Locker api_locker (target_sp->GetAPIMutex());
367 StateType state = eStateInvalid;
Greg Claytonb09c5382013-12-13 17:20:18 +0000368 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000369 ProcessSP process_sp = target_sp->GetProcessSP();
370 if (process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000371 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000372 state = process_sp->GetState();
373
374 if (process_sp->IsAlive() && state != eStateConnected)
375 {
376 if (state == eStateAttaching)
377 error.SetErrorString ("process attach is in progress");
378 else
379 error.SetErrorString ("a process is already being debugged");
380 return sb_process;
381 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000382 }
Greg Clayton0e615682012-02-24 05:03:03 +0000383 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000384
385 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
386
Greg Clayton3e32ad62014-05-07 20:16:06 +0000387 if (!launch_info.GetExecutableFile())
388 {
389 Module *exe_module = target_sp->GetExecutableModulePointer();
390 if (exe_module)
391 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
392 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000393
394 const ArchSpec &arch_spec = target_sp->GetArchitecture();
395 if (arch_spec.IsValid())
396 launch_info.GetArchitecture () = arch_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000397
Greg Clayton8012cad2014-11-17 19:39:20 +0000398 error.SetError (target_sp->Launch (launch_info, NULL));
Greg Claytonb09c5382013-12-13 17:20:18 +0000399 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000400 }
401 else
402 {
403 error.SetErrorString ("SBTarget is invalid");
404 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000405
Greg Clayton0e615682012-02-24 05:03:03 +0000406 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
407 if (log)
Greg Claytonb09c5382013-12-13 17:20:18 +0000408 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000409 static_cast<void*>(target_sp.get()),
410 static_cast<void*>(sb_process.GetSP().get()));
411
Greg Clayton0e615682012-02-24 05:03:03 +0000412 return sb_process;
413}
414
415lldb::SBProcess
416SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
417{
Greg Clayton5160ce52013-03-27 23:08:40 +0000418 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000419
Greg Clayton0e615682012-02-24 05:03:03 +0000420 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000421 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000422
Sean Callanan575a4542012-10-20 00:21:31 +0000423 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000424 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
425 static_cast<void*>(target_sp.get()));
426
Greg Clayton0e615682012-02-24 05:03:03 +0000427 if (target_sp)
428 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000429 ProcessAttachInfo &attach_info = sb_attach_info.ref();
430 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Greg Clayton0e615682012-02-24 05:03:03 +0000431 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000432 PlatformSP platform_sp = target_sp->GetPlatform();
433 // See if we can pre-verify if a process exists or not
434 if (platform_sp && platform_sp->IsConnected())
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000436 lldb::pid_t attach_pid = attach_info.GetProcessID();
437 ProcessInstanceInfo instance_info;
438 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
Han Ming Ongcec8c902012-02-29 00:12:56 +0000439 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000440 attach_info.SetUserID(instance_info.GetEffectiveUserID());
441 }
442 else
443 {
444 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
445 if (log)
Greg Clayton91e407e2012-09-27 00:03:39 +0000446 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000447 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
448 static_cast<void*>(target_sp.get()), error.GetCString());
Greg Clayton91e407e2012-09-27 00:03:39 +0000449 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000450 return sb_process;
Han Ming Ongcec8c902012-02-29 00:12:56 +0000451 }
452 }
Greg Clayton0e615682012-02-24 05:03:03 +0000453 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000454 error.SetError(AttachToProcess(attach_info, *target_sp));
455 if (error.Success())
456 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000457 }
458 else
459 {
460 error.SetErrorString ("SBTarget is invalid");
461 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462
Sean Callanan575a4542012-10-20 00:21:31 +0000463 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000464 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000465 static_cast<void*>(target_sp.get()),
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000466 static_cast<void*>(sb_process.GetSP().get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000467
Greg Clayton0e615682012-02-24 05:03:03 +0000468 return sb_process;
469}
470
471
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000472#if defined(__APPLE__)
473
474lldb::SBProcess
475SBTarget::AttachToProcessWithID (SBListener &listener,
476 ::pid_t pid,
477 lldb::SBError& error)
478{
479 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
480}
481
482#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000483
484lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000485SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000486(
Greg Clayton4b045622011-02-03 21:28:34 +0000487 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000488 lldb::pid_t pid,// The process ID to attach to
489 SBError& error // An error explaining what went wrong if attach fails
490)
491{
Greg Clayton5160ce52013-03-27 23:08:40 +0000492 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000493
Greg Clayton524e60b2010-10-06 22:10:17 +0000494 SBProcess sb_process;
Greg Claytonacdbe812012-01-30 09:04:36 +0000495 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000496
497 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000498 log->Printf ("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
499 static_cast<void*>(target_sp.get()),
500 __FUNCTION__,
501 pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000502
Greg Claytonacdbe812012-01-30 09:04:36 +0000503 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000504 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000505 ProcessAttachInfo attach_info;
506 attach_info.SetProcessID (pid);
507 if (listener.IsValid())
508 attach_info.SetListener(listener.GetSP());
Greg Clayton0c74e782011-06-24 03:21:43 +0000509
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000510 ProcessInstanceInfo instance_info;
511 if (target_sp->GetPlatform ()->GetProcessInfo (pid, instance_info))
512 attach_info.SetUserID (instance_info.GetEffectiveUserID ());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000513
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000514 error.SetError (AttachToProcess (attach_info, *target_sp));
515 if (error.Success ())
516 sb_process.SetSP (target_sp->GetProcessSP ());
Greg Clayton524e60b2010-10-06 22:10:17 +0000517 }
518 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000519 error.SetErrorString ("SBTarget is invalid");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000520
Sean Callanan575a4542012-10-20 00:21:31 +0000521 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000522 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
523 static_cast<void*>(target_sp.get ()),
524 __FUNCTION__,
525 static_cast<void*>(sb_process.GetSP().get ()));
Greg Clayton524e60b2010-10-06 22:10:17 +0000526 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +0000527}
528
529lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000530SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +0000531(
Greg Clayton4b045622011-02-03 21:28:34 +0000532 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000533 const char *name, // basename of process to attach to
534 bool wait_for, // if true wait for a new instance of "name" to be launched
535 SBError& error // An error explaining what went wrong if attach fails
536)
537{
Greg Clayton5160ce52013-03-27 23:08:40 +0000538 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000539
Greg Clayton524e60b2010-10-06 22:10:17 +0000540 SBProcess sb_process;
Greg Claytonacdbe812012-01-30 09:04:36 +0000541 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000542
Sean Callanan575a4542012-10-20 00:21:31 +0000543 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000544 log->Printf ("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
545 static_cast<void*>(target_sp.get()),
546 __FUNCTION__,
547 name,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000548 wait_for ? "true" : "false");
549
Greg Claytonacdbe812012-01-30 09:04:36 +0000550 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000551 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000552 ProcessAttachInfo attach_info;
553 attach_info.GetExecutableFile().SetFile(name, false);
554 attach_info.SetWaitForLaunch(wait_for);
555 if (listener.IsValid())
556 attach_info.SetListener(listener.GetSP());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000557
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000558 error.SetError (AttachToProcess (attach_info, *target_sp));
559 if (error.Success ())
560 sb_process.SetSP (target_sp->GetProcessSP ());
Greg Clayton524e60b2010-10-06 22:10:17 +0000561 }
562 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000563 error.SetErrorString ("SBTarget is invalid");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000564
Sean Callanan575a4542012-10-20 00:21:31 +0000565 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000566 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000567 static_cast<void*>(target_sp.get()),
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000568 __FUNCTION__,
569 static_cast<void*>(sb_process.GetSP().get()));
Greg Clayton524e60b2010-10-06 22:10:17 +0000570 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +0000571}
572
James McIlree9631aae2011-03-04 00:31:13 +0000573lldb::SBProcess
574SBTarget::ConnectRemote
575(
576 SBListener &listener,
577 const char *url,
578 const char *plugin_name,
579 SBError& error
580)
581{
Greg Clayton5160ce52013-03-27 23:08:40 +0000582 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000583
James McIlree9631aae2011-03-04 00:31:13 +0000584 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000585 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000586 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000587
Sean Callanan575a4542012-10-20 00:21:31 +0000588 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000589 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
590 static_cast<void*>(target_sp.get()), url, plugin_name);
591
Greg Claytonacdbe812012-01-30 09:04:36 +0000592 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +0000593 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000594 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +0000595 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000596 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000597 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000598 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000599
Greg Claytonb9556ac2012-01-30 07:41:31 +0000600 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +0000601 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000602 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000603 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +0000604 }
605 else
606 {
607 error.SetErrorString ("unable to create lldb_private::Process");
608 }
609 }
610 else
611 {
612 error.SetErrorString ("SBTarget is invalid");
613 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000614
Sean Callanan575a4542012-10-20 00:21:31 +0000615 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000616 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000617 static_cast<void*>(target_sp.get()),
618 static_cast<void*>(process_sp.get()));
James McIlree9631aae2011-03-04 00:31:13 +0000619 return sb_process;
620}
621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622SBFileSpec
623SBTarget::GetExecutable ()
624{
Caroline Ticeceb6b132010-10-26 03:11:13 +0000625
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +0000627 TargetSP target_sp(GetSP());
628 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000630 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +0000631 if (exe_module)
632 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000634
Greg Clayton5160ce52013-03-27 23:08:40 +0000635 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000636 if (log)
637 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000638 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
639 static_cast<void*>(target_sp.get()),
640 static_cast<const void*>(exe_file_spec.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000641 }
642
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 return exe_file_spec;
644}
645
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647SBTarget::operator == (const SBTarget &rhs) const
648{
Greg Clayton66111032010-06-23 01:19:29 +0000649 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650}
651
652bool
653SBTarget::operator != (const SBTarget &rhs) const
654{
Greg Clayton66111032010-06-23 01:19:29 +0000655 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656}
657
Greg Claytonb9556ac2012-01-30 07:41:31 +0000658lldb::TargetSP
659SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +0000660{
661 return m_opaque_sp;
662}
663
Greg Clayton66111032010-06-23 01:19:29 +0000664void
Greg Claytonb9556ac2012-01-30 07:41:31 +0000665SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +0000666{
667 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668}
669
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000670lldb::SBAddress
671SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000672{
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000673 lldb::SBAddress sb_addr;
674 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +0000675 TargetSP target_sp(GetSP());
676 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000677 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000678 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytond5944cd2013-12-06 01:12:00 +0000679 if (target_sp->ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000680 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +0000681 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000682
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000683 // We have a load address that isn't in a section, just return an address
684 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +0000685 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000686 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000687}
688
Matthew Gardinerc928de32014-10-22 07:22:56 +0000689lldb::SBAddress
690SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
691{
692 lldb::SBAddress sb_addr;
693 Address &addr = sb_addr.ref();
694 TargetSP target_sp(GetSP());
695 if (target_sp)
696 {
697 Mutex::Locker api_locker (target_sp->GetAPIMutex());
698 if (target_sp->ResolveFileAddress (file_addr, addr))
699 return sb_addr;
700 }
701
702 addr.SetRawAddress(file_addr);
703 return sb_addr;
704}
Greg Claytond5944cd2013-12-06 01:12:00 +0000705
706lldb::SBAddress
707SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
708{
709 lldb::SBAddress sb_addr;
710 Address &addr = sb_addr.ref();
711 TargetSP target_sp(GetSP());
712 if (target_sp)
713 {
714 Mutex::Locker api_locker (target_sp->GetAPIMutex());
715 if (target_sp->ResolveLoadAddress (vm_addr, addr))
716 return sb_addr;
717 }
718
719 // We have a load address that isn't in a section, just return an address
720 // with the offset filled in (the address) and the section set to NULL
721 addr.SetRawAddress(vm_addr);
722 return sb_addr;
723}
724
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000725SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +0000726SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
727 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000728{
729 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +0000730 if (addr.IsValid())
731 {
732 TargetSP target_sp(GetSP());
733 if (target_sp)
734 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
735 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000736 return sc;
737}
738
Matthew Gardinerc928de32014-10-22 07:22:56 +0000739size_t
740SBTarget::ReadMemory (const SBAddress addr,
741 void *buf,
742 size_t size,
743 lldb::SBError &error)
744{
745 SBError sb_error;
746 size_t bytes_read = 0;
747 TargetSP target_sp(GetSP());
748 if (target_sp)
749 {
750 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton8691dc52014-11-04 00:56:30 +0000751 bytes_read = target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
752 }
753 else
754 {
755 sb_error.SetErrorString("invalid target");
Matthew Gardinerc928de32014-10-22 07:22:56 +0000756 }
757
758 return bytes_read;
759}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000760
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000762SBTarget::BreakpointCreateByLocation (const char *file,
763 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764{
Greg Clayton7481c202010-11-08 00:28:40 +0000765 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766}
767
768SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000769SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
770 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771{
Greg Clayton5160ce52013-03-27 23:08:40 +0000772 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000773
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000775 TargetSP target_sp(GetSP());
776 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000777 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000778 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000779
Greg Clayton1f746072012-08-29 21:13:06 +0000780 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +0000781 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +0000782 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000783 const bool hardware = false;
784 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000785 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000786
787 if (log)
788 {
789 SBStream sstr;
790 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000791 char path[PATH_MAX];
792 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000793 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
794 static_cast<void*>(target_sp.get()), path, line,
795 static_cast<void*>(sb_bp.get()), sstr.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000796 }
797
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 return sb_bp;
799}
800
801SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000802SBTarget::BreakpointCreateByName (const char *symbol_name,
803 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804{
Greg Clayton5160ce52013-03-27 23:08:40 +0000805 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000806
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000808 TargetSP target_sp(GetSP());
809 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000811 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000812
Jim Inghama8558b62012-05-22 00:12:20 +0000813 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000814 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000815 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 if (module_name && module_name[0])
817 {
Jim Ingham969795f2011-09-21 01:17:13 +0000818 FileSpecList module_spec_list;
819 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +0000820 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 }
822 else
823 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000824 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 }
826 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000827
Caroline Ticeceb6b132010-10-26 03:11:13 +0000828 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000829 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
830 static_cast<void*>(target_sp.get()), symbol_name,
831 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000832
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 return sb_bp;
834}
835
Jim Ingham87df91b2011-09-23 00:54:11 +0000836lldb::SBBreakpoint
837SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +0000838 const SBFileSpecList &module_list,
839 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +0000840{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +0000841 uint32_t name_type_mask = eFunctionNameTypeAuto;
842 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
843}
844
845lldb::SBBreakpoint
846SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +0000847 uint32_t name_type_mask,
848 const SBFileSpecList &module_list,
849 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +0000850{
Greg Clayton5160ce52013-03-27 23:08:40 +0000851 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +0000852
853 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000854 TargetSP target_sp(GetSP());
855 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +0000856 {
Jim Inghama8558b62012-05-22 00:12:20 +0000857 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000858 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000859 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +0000860 Mutex::Locker api_locker (target_sp->GetAPIMutex());
861 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +0000862 comp_unit_list.get(),
863 symbol_name,
864 name_type_mask,
865 skip_prologue,
866 internal,
867 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +0000868 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000869
Jim Ingham87df91b2011-09-23 00:54:11 +0000870 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000871 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
872 static_cast<void*>(target_sp.get()), symbol_name,
873 name_type_mask, static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +0000874
875 return sb_bp;
876}
877
Jim Inghamfab10e82012-03-06 00:37:27 +0000878lldb::SBBreakpoint
879SBTarget::BreakpointCreateByNames (const char *symbol_names[],
880 uint32_t num_names,
881 uint32_t name_type_mask,
882 const SBFileSpecList &module_list,
883 const SBFileSpecList &comp_unit_list)
884{
Greg Clayton5160ce52013-03-27 23:08:40 +0000885 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +0000886
887 SBBreakpoint sb_bp;
888 TargetSP target_sp(GetSP());
889 if (target_sp && num_names > 0)
890 {
891 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +0000892 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000893 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000894 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +0000895 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
896 comp_unit_list.get(),
897 symbol_names,
898 num_names,
899 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000900 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000901 internal,
902 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +0000903 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000904
Jim Inghamfab10e82012-03-06 00:37:27 +0000905 if (log)
906 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000907 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
908 static_cast<void*>(target_sp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +0000909 for (uint32_t i = 0 ; i < num_names; i++)
910 {
911 char sep;
912 if (i < num_names - 1)
913 sep = ',';
914 else
915 sep = '}';
916 if (symbol_names[i] != NULL)
917 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
918 else
919 log->Printf ("\"<NULL>\"%c ", sep);
Jim Inghamfab10e82012-03-06 00:37:27 +0000920 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000921 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
922 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +0000923 }
924
925 return sb_bp;
926}
Jim Ingham87df91b2011-09-23 00:54:11 +0000927
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000929SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
930 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931{
Greg Clayton5160ce52013-03-27 23:08:40 +0000932 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000933
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000935 TargetSP target_sp(GetSP());
936 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000938 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +0000940 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000941 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000942 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000943
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 if (module_name && module_name[0])
945 {
Jim Ingham969795f2011-09-21 01:17:13 +0000946 FileSpecList module_spec_list;
947 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000948
Greg Claytoneb023e72013-10-11 19:48:25 +0000949 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 }
951 else
952 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000953 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 }
955 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000956
957 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000958 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
959 static_cast<void*>(target_sp.get()), symbol_name_regex,
960 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000961
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 return sb_bp;
963}
964
Jim Ingham87df91b2011-09-23 00:54:11 +0000965lldb::SBBreakpoint
966SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000967 const SBFileSpecList &module_list,
968 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +0000969{
Greg Clayton5160ce52013-03-27 23:08:40 +0000970 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971
Jim Ingham87df91b2011-09-23 00:54:11 +0000972 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000973 TargetSP target_sp(GetSP());
974 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +0000975 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000976 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +0000977 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +0000978 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000979 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000980 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000981
Greg Claytoneb023e72013-10-11 19:48:25 +0000982 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +0000983 }
984
985 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000986 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
987 static_cast<void*>(target_sp.get()), symbol_name_regex,
988 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +0000989
990 return sb_bp;
991}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992
993SBBreakpoint
994SBTarget::BreakpointCreateByAddress (addr_t address)
995{
Greg Clayton5160ce52013-03-27 23:08:40 +0000996 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000997
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000999 TargetSP target_sp(GetSP());
1000 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001001 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001002 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001003 const bool hardware = false;
1004 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001005 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001006
Caroline Ticeceb6b132010-10-26 03:11:13 +00001007 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001008 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1009 static_cast<void*>(target_sp.get()),
1010 static_cast<uint64_t>(address),
1011 static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001012
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 return sb_bp;
1014}
1015
Jim Ingham969795f2011-09-21 01:17:13 +00001016lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001017SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1018 const lldb::SBFileSpec &source_file,
1019 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001020{
Greg Clayton5160ce52013-03-27 23:08:40 +00001021 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001022
1023 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001024 TargetSP target_sp(GetSP());
1025 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001026 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001027 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001028 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001029 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001030 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001031 source_file_spec_list.Append (source_file.ref());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001032
Jim Ingham969795f2011-09-21 01:17:13 +00001033 if (module_name && module_name[0])
1034 {
1035 FileSpecList module_spec_list;
1036 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001037
Greg Claytoneb023e72013-10-11 19:48:25 +00001038 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001039 }
1040 else
1041 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001042 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001043 }
1044 }
1045
1046 if (log)
1047 {
1048 char path[PATH_MAX];
1049 source_file->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001050 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1051 static_cast<void*>(target_sp.get()), source_regex, path,
1052 module_name, static_cast<void*>(sb_bp.get()));
Jim Ingham969795f2011-09-21 01:17:13 +00001053 }
1054
1055 return sb_bp;
1056}
1057
Jim Ingham87df91b2011-09-23 00:54:11 +00001058lldb::SBBreakpoint
Jim Inghame7320522015-02-12 17:37:46 +00001059SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1060 const SBFileSpecList &module_list,
1061 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001062{
Greg Clayton5160ce52013-03-27 23:08:40 +00001063 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001064
1065 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001066 TargetSP target_sp(GetSP());
1067 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001068 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001069 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001070 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001071 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001072 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001073 }
1074
1075 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001076 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1077 static_cast<void*>(target_sp.get()), source_regex,
1078 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001079
1080 return sb_bp;
1081}
Jim Ingham969795f2011-09-21 01:17:13 +00001082
Jim Inghamfab10e82012-03-06 00:37:27 +00001083lldb::SBBreakpoint
1084SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001085 bool catch_bp,
1086 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001087{
Greg Clayton5160ce52013-03-27 23:08:40 +00001088 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001089
1090 SBBreakpoint sb_bp;
1091 TargetSP target_sp(GetSP());
1092 if (target_sp)
1093 {
1094 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001095 const bool hardware = false;
1096 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001097 }
1098
1099 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001100 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1101 static_cast<void*>(target_sp.get()),
Jim Inghamfab10e82012-03-06 00:37:27 +00001102 LanguageRuntime::GetNameForLanguageType(language),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001103 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1104 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001105
1106 return sb_bp;
1107}
1108
Greg Clayton9fed0d82010-07-23 23:33:17 +00001109uint32_t
1110SBTarget::GetNumBreakpoints () const
1111{
Greg Claytonacdbe812012-01-30 09:04:36 +00001112 TargetSP target_sp(GetSP());
1113 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001114 {
1115 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001116 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001117 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001118 return 0;
1119}
1120
1121SBBreakpoint
1122SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1123{
1124 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001125 TargetSP target_sp(GetSP());
1126 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001127 {
1128 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001129 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001130 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001131 return sb_breakpoint;
1132}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
1134bool
1135SBTarget::BreakpointDelete (break_id_t bp_id)
1136{
Greg Clayton5160ce52013-03-27 23:08:40 +00001137 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001138
Caroline Ticeceb6b132010-10-26 03:11:13 +00001139 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001140 TargetSP target_sp(GetSP());
1141 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001142 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001143 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1144 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001145 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001146
1147 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001148 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1149 static_cast<void*>(target_sp.get()),
1150 static_cast<uint32_t>(bp_id), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001151
1152 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153}
1154
Johnny Chen5d043462011-09-26 22:40:50 +00001155SBBreakpoint
1156SBTarget::FindBreakpointByID (break_id_t bp_id)
1157{
Greg Clayton5160ce52013-03-27 23:08:40 +00001158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001159
1160 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001161 TargetSP target_sp(GetSP());
1162 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001163 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001164 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1165 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001166 }
1167
1168 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001169 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1170 static_cast<void*>(target_sp.get()),
1171 static_cast<uint32_t>(bp_id),
1172 static_cast<void*>(sb_breakpoint.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001173
1174 return sb_breakpoint;
1175}
1176
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177bool
1178SBTarget::EnableAllBreakpoints ()
1179{
Greg Claytonacdbe812012-01-30 09:04:36 +00001180 TargetSP target_sp(GetSP());
1181 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001183 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1184 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185 return true;
1186 }
1187 return false;
1188}
1189
1190bool
1191SBTarget::DisableAllBreakpoints ()
1192{
Greg Claytonacdbe812012-01-30 09:04:36 +00001193 TargetSP target_sp(GetSP());
1194 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001196 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1197 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 return true;
1199 }
1200 return false;
1201}
1202
1203bool
1204SBTarget::DeleteAllBreakpoints ()
1205{
Greg Claytonacdbe812012-01-30 09:04:36 +00001206 TargetSP target_sp(GetSP());
1207 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001209 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1210 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211 return true;
1212 }
1213 return false;
1214}
1215
Johnny Chen5d043462011-09-26 22:40:50 +00001216uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001217SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001218{
Greg Claytonacdbe812012-01-30 09:04:36 +00001219 TargetSP target_sp(GetSP());
1220 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001221 {
Johnny Chen01a67862011-10-14 00:42:25 +00001222 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001223 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001224 }
1225 return 0;
1226}
1227
Greg Clayton1b282f92011-10-13 18:08:26 +00001228SBWatchpoint
1229SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001230{
Johnny Chen01a67862011-10-14 00:42:25 +00001231 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001232 TargetSP target_sp(GetSP());
1233 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001234 {
Johnny Chen01a67862011-10-14 00:42:25 +00001235 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001236 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001237 }
Johnny Chen01a67862011-10-14 00:42:25 +00001238 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001239}
1240
1241bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001242SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001243{
Greg Clayton5160ce52013-03-27 23:08:40 +00001244 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001245
1246 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001247 TargetSP target_sp(GetSP());
1248 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001249 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001250 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001251 Mutex::Locker locker;
1252 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001253 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001254 }
1255
1256 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001257 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1258 static_cast<void*>(target_sp.get()),
1259 static_cast<uint32_t>(wp_id), result);
Johnny Chen5d043462011-09-26 22:40:50 +00001260
1261 return result;
1262}
1263
Greg Clayton1b282f92011-10-13 18:08:26 +00001264SBWatchpoint
1265SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001266{
Greg Clayton5160ce52013-03-27 23:08:40 +00001267 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001268
Greg Clayton1b282f92011-10-13 18:08:26 +00001269 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001270 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001271 TargetSP target_sp(GetSP());
1272 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001273 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001274 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001275 Mutex::Locker locker;
1276 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001277 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1278 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001279 }
1280
1281 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001282 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1283 static_cast<void*>(target_sp.get()),
1284 static_cast<uint32_t>(wp_id),
1285 static_cast<void*>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001286
Greg Clayton1b282f92011-10-13 18:08:26 +00001287 return sb_watchpoint;
1288}
1289
1290lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001291SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001292{
Greg Clayton5160ce52013-03-27 23:08:40 +00001293 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001294
Greg Clayton1b282f92011-10-13 18:08:26 +00001295 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001296 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001297 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001298 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001299 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001300 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001301 uint32_t watch_type = 0;
1302 if (read)
1303 watch_type |= LLDB_WATCH_TYPE_READ;
1304 if (write)
1305 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001306 if (watch_type == 0)
1307 {
1308 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1309 return sb_watchpoint;
1310 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001311
Johnny Chen7385a5a2012-05-31 22:56:36 +00001312 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001313 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001314 // This API doesn't take in a type, so we can't figure out what it is.
1315 ClangASTType *type = NULL;
1316 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001317 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001318 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001319 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001320
Greg Clayton1b282f92011-10-13 18:08:26 +00001321 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001322 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001323 static_cast<void*>(target_sp.get()), addr,
1324 static_cast<uint32_t>(size),
1325 static_cast<void*>(watchpoint_sp.get()));
1326
Greg Clayton1b282f92011-10-13 18:08:26 +00001327 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001328}
1329
1330bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001331SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001332{
Greg Claytonacdbe812012-01-30 09:04:36 +00001333 TargetSP target_sp(GetSP());
1334 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001335 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001336 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001337 Mutex::Locker locker;
1338 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001339 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001340 return true;
1341 }
1342 return false;
1343}
1344
1345bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001346SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001347{
Greg Claytonacdbe812012-01-30 09:04:36 +00001348 TargetSP target_sp(GetSP());
1349 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001350 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001351 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001352 Mutex::Locker locker;
1353 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001354 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001355 return true;
1356 }
1357 return false;
1358}
1359
Enrico Granata347c2aa2013-10-08 21:49:02 +00001360SBValue
1361SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1362{
1363 SBValue sb_value;
1364 lldb::ValueObjectSP new_value_sp;
1365 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1366 {
Enrico Granata972be532014-12-17 21:18:43 +00001367 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1368 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1369 ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1370 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, exe_ctx, ast_type);
Enrico Granata347c2aa2013-10-08 21:49:02 +00001371 }
1372 sb_value.SetSP(new_value_sp);
1373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1374 if (log)
1375 {
1376 if (new_value_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001377 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1378 static_cast<void*>(m_opaque_sp.get()),
1379 new_value_sp->GetName().AsCString());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001380 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001381 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1382 static_cast<void*>(m_opaque_sp.get()));
Enrico Granata347c2aa2013-10-08 21:49:02 +00001383 }
1384 return sb_value;
1385}
1386
Enrico Granata972be532014-12-17 21:18:43 +00001387lldb::SBValue
1388SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1389{
1390 SBValue sb_value;
1391 lldb::ValueObjectSP new_value_sp;
1392 if (IsValid() && name && *name && data.IsValid() && type.IsValid())
1393 {
1394 DataExtractorSP extractor(*data);
1395 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1396 ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1397 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, exe_ctx, ast_type);
1398 }
1399 sb_value.SetSP(new_value_sp);
1400 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1401 if (log)
1402 {
1403 if (new_value_sp)
1404 log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1405 static_cast<void*>(m_opaque_sp.get()),
1406 new_value_sp->GetName().AsCString());
1407 else
1408 log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1409 static_cast<void*>(m_opaque_sp.get()));
1410 }
1411 return sb_value;
1412}
1413
1414lldb::SBValue
1415SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1416{
1417 SBValue sb_value;
1418 lldb::ValueObjectSP new_value_sp;
1419 if (IsValid() && name && *name && expr && *expr)
1420 {
1421 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1422 new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1423 }
1424 sb_value.SetSP(new_value_sp);
1425 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1426 if (log)
1427 {
1428 if (new_value_sp)
1429 log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1430 static_cast<void*>(m_opaque_sp.get()),
1431 new_value_sp->GetName().AsCString());
1432 else
1433 log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1434 static_cast<void*>(m_opaque_sp.get()));
1435 }
1436 return sb_value;
1437}
1438
Johnny Chen5d043462011-09-26 22:40:50 +00001439bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001440SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001441{
Greg Claytonacdbe812012-01-30 09:04:36 +00001442 TargetSP target_sp(GetSP());
1443 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001444 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001445 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001446 Mutex::Locker locker;
1447 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001448 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001449 return true;
1450 }
1451 return false;
1452}
1453
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001455lldb::SBModule
1456SBTarget::AddModule (const char *path,
1457 const char *triple,
1458 const char *uuid_cstr)
1459{
Greg Claytonb210aec2012-04-23 20:23:39 +00001460 return AddModule (path, triple, uuid_cstr, NULL);
1461}
1462
1463lldb::SBModule
1464SBTarget::AddModule (const char *path,
1465 const char *triple,
1466 const char *uuid_cstr,
1467 const char *symfile)
1468{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001469 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001470 TargetSP target_sp(GetSP());
1471 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001472 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001473 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001474 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001475 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001476
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001477 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001478 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001479
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001480 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001481 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001482 else
1483 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001484
1485 if (symfile)
1486 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001487
Greg Claytonb9a01b32012-02-26 05:51:37 +00001488 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001489 }
1490 return sb_module;
1491}
1492
Greg Clayton226cce22013-07-08 22:22:41 +00001493lldb::SBModule
1494SBTarget::AddModule (const SBModuleSpec &module_spec)
1495{
1496 lldb::SBModule sb_module;
1497 TargetSP target_sp(GetSP());
1498 if (target_sp)
1499 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1500 return sb_module;
1501}
1502
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001503bool
1504SBTarget::AddModule (lldb::SBModule &module)
1505{
Greg Claytonacdbe812012-01-30 09:04:36 +00001506 TargetSP target_sp(GetSP());
1507 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001508 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001509 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001510 return true;
1511 }
1512 return false;
1513}
1514
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515uint32_t
1516SBTarget::GetNumModules () const
1517{
Greg Clayton5160ce52013-03-27 23:08:40 +00001518 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001519
Caroline Ticeceb6b132010-10-26 03:11:13 +00001520 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001521 TargetSP target_sp(GetSP());
1522 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001523 {
1524 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001525 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001526 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001527
1528 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001529 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1530 static_cast<void*>(target_sp.get()), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001531
1532 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533}
1534
Greg Clayton48e42542010-07-30 20:12:55 +00001535void
1536SBTarget::Clear ()
1537{
Greg Clayton5160ce52013-03-27 23:08:40 +00001538 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001539
1540 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001541 log->Printf ("SBTarget(%p)::Clear ()",
1542 static_cast<void*>(m_opaque_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001543
Greg Clayton48e42542010-07-30 20:12:55 +00001544 m_opaque_sp.reset();
1545}
1546
1547
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001548SBModule
1549SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1550{
1551 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001552 TargetSP target_sp(GetSP());
1553 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001554 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001555 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001556 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00001557 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001558 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001559 return sb_module;
1560}
1561
Greg Clayton13d19502012-01-29 06:07:39 +00001562lldb::ByteOrder
1563SBTarget::GetByteOrder ()
1564{
Greg Claytonacdbe812012-01-30 09:04:36 +00001565 TargetSP target_sp(GetSP());
1566 if (target_sp)
1567 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00001568 return eByteOrderInvalid;
1569}
1570
1571const char *
1572SBTarget::GetTriple ()
1573{
Greg Claytonacdbe812012-01-30 09:04:36 +00001574 TargetSP target_sp(GetSP());
1575 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00001576 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001577 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00001578 // Unique the string so we don't run into ownership issues since
1579 // the const strings put the string into the string pool once and
1580 // the strings never comes out
1581 ConstString const_triple (triple.c_str());
1582 return const_triple.GetCString();
1583 }
1584 return NULL;
1585}
1586
1587uint32_t
Matthew Gardinerc928de32014-10-22 07:22:56 +00001588SBTarget::GetDataByteSize ()
1589{
1590 TargetSP target_sp(GetSP());
1591 if (target_sp)
1592 {
1593 return target_sp->GetArchitecture().GetDataByteSize() ;
1594 }
1595 return 0;
1596}
1597
1598uint32_t
1599SBTarget::GetCodeByteSize ()
1600{
1601 TargetSP target_sp(GetSP());
1602 if (target_sp)
1603 {
1604 return target_sp->GetArchitecture().GetCodeByteSize() ;
1605 }
1606 return 0;
1607}
1608
1609uint32_t
Greg Clayton13d19502012-01-29 06:07:39 +00001610SBTarget::GetAddressByteSize()
1611{
Greg Claytonacdbe812012-01-30 09:04:36 +00001612 TargetSP target_sp(GetSP());
1613 if (target_sp)
1614 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00001615 return sizeof(void*);
1616}
1617
1618
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001619SBModule
1620SBTarget::GetModuleAtIndex (uint32_t idx)
1621{
Greg Clayton5160ce52013-03-27 23:08:40 +00001622 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001623
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001624 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001625 ModuleSP module_sp;
1626 TargetSP target_sp(GetSP());
1627 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001628 {
1629 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001630 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1631 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001632 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001633
1634 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001635 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1636 static_cast<void*>(target_sp.get()), idx,
1637 static_cast<void*>(module_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001638
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 return sb_module;
1640}
1641
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001642bool
1643SBTarget::RemoveModule (lldb::SBModule module)
1644{
Greg Claytonacdbe812012-01-30 09:04:36 +00001645 TargetSP target_sp(GetSP());
1646 if (target_sp)
1647 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001648 return false;
1649}
1650
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651
1652SBBroadcaster
1653SBTarget::GetBroadcaster () const
1654{
Greg Clayton5160ce52013-03-27 23:08:40 +00001655 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001656
Greg Claytonacdbe812012-01-30 09:04:36 +00001657 TargetSP target_sp(GetSP());
1658 SBBroadcaster broadcaster(target_sp.get(), false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001659
Caroline Ticeceb6b132010-10-26 03:11:13 +00001660 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001661 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1662 static_cast<void*>(target_sp.get()),
1663 static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001664
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665 return broadcaster;
1666}
1667
Caroline Ticedde9cff2010-09-20 05:20:02 +00001668bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001669SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00001670{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001671 Stream &strm = description.ref();
1672
Greg Claytonacdbe812012-01-30 09:04:36 +00001673 TargetSP target_sp(GetSP());
1674 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00001675 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001676 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001677 }
1678 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001679 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001680
1681 return true;
1682}
1683
Greg Clayton5569e642012-02-06 01:44:54 +00001684lldb::SBSymbolContextList
1685SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00001686{
Greg Clayton5569e642012-02-06 01:44:54 +00001687 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00001688 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00001689 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001690 TargetSP target_sp(GetSP());
1691 if (target_sp)
1692 {
1693 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00001694 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00001695 const bool append = true;
1696 target_sp->GetImages().FindFunctions (ConstString(name),
1697 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001698 symbols_ok,
1699 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00001700 append,
1701 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00001702 }
Greg Claytonfe356d32011-06-21 01:34:41 +00001703 }
Greg Clayton5569e642012-02-06 01:44:54 +00001704 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00001705}
1706
Carlo Kok0fd6fd42014-09-19 19:38:19 +00001707lldb::SBSymbolContextList
1708SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1709{
1710 lldb::SBSymbolContextList sb_sc_list;
1711 if (name && name[0])
1712 {
1713 TargetSP target_sp(GetSP());
1714 if (target_sp)
1715 {
1716 std::string regexstr;
1717 switch (matchtype)
1718 {
1719 case eMatchTypeRegex:
1720 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1721 break;
1722 case eMatchTypeStartsWith:
1723 regexstr = llvm::Regex::escape(name) + ".*";
1724 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1725 break;
1726 default:
1727 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1728 break;
1729 }
1730 }
1731 }
1732 return sb_sc_list;
1733}
1734
Enrico Granata6f3533f2011-07-29 19:53:35 +00001735lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00001736SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001737{
Greg Claytonacdbe812012-01-30 09:04:36 +00001738 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00001739 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001740 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001741 ConstString const_typename(typename_cstr);
1742 SymbolContext sc;
1743 const bool exact_match = false;
1744
1745 const ModuleList &module_list = target_sp->GetImages();
1746 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001747 for (size_t idx = 0; idx < count; idx++)
1748 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001749 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1750 if (module_sp)
1751 {
1752 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1753 if (type_sp)
1754 return SBType(type_sp);
1755 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001756 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001757
1758 // Didn't find the type in the symbols; try the Objective-C runtime
1759 // if one is installed
1760
1761 ProcessSP process_sp(target_sp->GetProcessSP());
1762
1763 if (process_sp)
1764 {
1765 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1766
1767 if (objc_language_runtime)
1768 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001769 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00001770
Sean Callanan9998acd2014-12-05 01:21:59 +00001771 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00001772 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001773 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00001774
Sean Callanan9998acd2014-12-05 01:21:59 +00001775 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1776 {
1777 if (ClangASTType type = ClangASTContext::GetTypeForDecl(decls[0]))
1778 {
1779 return SBType(type);
1780 }
1781 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001782 }
1783 }
1784 }
Greg Claytonb43165b2012-12-05 21:24:42 +00001785
1786 // No matches, search for basic typename matches
1787 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1788 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001789 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00001790 }
1791 return SBType();
1792}
1793
Greg Claytonb43165b2012-12-05 21:24:42 +00001794SBType
1795SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001796{
Greg Claytonacdbe812012-01-30 09:04:36 +00001797 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00001798 if (target_sp)
1799 {
1800 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1801 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001802 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00001803 }
1804 return SBType();
1805}
1806
1807
1808lldb::SBTypeList
1809SBTarget::FindTypes (const char* typename_cstr)
1810{
1811 SBTypeList sb_type_list;
1812 TargetSP target_sp(GetSP());
1813 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001814 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001815 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00001816 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001817 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001818 SymbolContext sc;
1819 TypeList type_list;
1820
Greg Clayton29399a22012-04-06 17:41:13 +00001821 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00001822 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00001823 exact_match,
1824 UINT32_MAX,
1825 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001826
Greg Claytonb43165b2012-12-05 21:24:42 +00001827 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001828 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001829 for (size_t idx = 0; idx < num_matches; idx++)
1830 {
1831 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1832 if (type_sp)
1833 sb_type_list.Append(SBType(type_sp));
1834 }
1835 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001836
1837 // Try the Objective-C runtime if one is installed
1838
1839 ProcessSP process_sp(target_sp->GetProcessSP());
1840
1841 if (process_sp)
1842 {
1843 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1844
1845 if (objc_language_runtime)
1846 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001847 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00001848
Sean Callanan9998acd2014-12-05 01:21:59 +00001849 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00001850 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001851 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00001852
Sean Callanan9998acd2014-12-05 01:21:59 +00001853 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
Sean Callanan7be70e82012-12-19 23:05:01 +00001854 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001855 for (clang::NamedDecl *decl : decls)
Sean Callanan7be70e82012-12-19 23:05:01 +00001856 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001857 if (ClangASTType type = ClangASTContext::GetTypeForDecl(decl))
1858 {
1859 sb_type_list.Append(SBType(type));
1860 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001861 }
1862 }
1863 }
1864 }
1865 }
1866
1867 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00001868 {
1869 // No matches, search for basic typename matches
1870 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1871 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001872 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00001873 }
1874 }
Greg Claytonb43165b2012-12-05 21:24:42 +00001875 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001876}
1877
Greg Claytondea8cb42011-06-29 22:09:02 +00001878SBValueList
1879SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1880{
1881 SBValueList sb_value_list;
1882
Greg Claytonacdbe812012-01-30 09:04:36 +00001883 TargetSP target_sp(GetSP());
1884 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00001885 {
1886 VariableList variable_list;
1887 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00001888 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
1889 append,
1890 max_matches,
1891 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00001892
1893 if (match_count > 0)
1894 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001895 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00001896 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00001897 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00001898 for (uint32_t i=0; i<match_count; ++i)
1899 {
1900 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1901 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00001902 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00001903 }
1904 }
1905 }
1906
1907 return sb_value_list;
1908}
1909
Carlo Kok0fd6fd42014-09-19 19:38:19 +00001910SBValueList
1911SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
1912{
1913 SBValueList sb_value_list;
1914
1915 TargetSP target_sp(GetSP());
1916 if (name && target_sp)
1917 {
1918 VariableList variable_list;
1919 const bool append = true;
1920
1921 std::string regexstr;
1922 uint32_t match_count;
1923 switch (matchtype)
1924 {
1925 case eMatchTypeNormal:
1926 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
1927 append,
1928 max_matches,
1929 variable_list);
1930 break;
1931 case eMatchTypeRegex:
1932 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
1933 append,
1934 max_matches,
1935 variable_list);
1936 break;
1937 case eMatchTypeStartsWith:
1938 regexstr = llvm::Regex::escape(name) + ".*";
1939 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
1940 append,
1941 max_matches,
1942 variable_list);
1943 break;
1944 }
1945
1946
1947 if (match_count > 0)
1948 {
1949 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1950 if (exe_scope == NULL)
1951 exe_scope = target_sp.get();
1952 for (uint32_t i = 0; i<match_count; ++i)
1953 {
1954 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
1955 if (valobj_sp)
1956 sb_value_list.Append(SBValue(valobj_sp));
1957 }
1958 }
1959 }
1960
1961 return sb_value_list;
1962}
1963
1964
Enrico Granatabcd80b42013-01-16 18:53:52 +00001965lldb::SBValue
1966SBTarget::FindFirstGlobalVariable (const char* name)
1967{
1968 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1969 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1970 return sb_value_list.GetValueAtIndex(0);
1971 return SBValue();
1972}
1973
Jim Inghame37d6052011-09-13 00:29:56 +00001974SBSourceManager
1975SBTarget::GetSourceManager()
1976{
1977 SBSourceManager source_manager (*this);
1978 return source_manager;
1979}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001980
Sean Callanan50952e92011-12-14 23:49:37 +00001981lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00001982SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
1983{
Jim Ingham0f063ba2013-03-02 00:26:47 +00001984 return ReadInstructions (base_addr, count, NULL);
1985}
1986
1987lldb::SBInstructionList
1988SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
1989{
Greg Clayton9c766112012-03-06 22:24:44 +00001990 SBInstructionList sb_instructions;
1991
1992 TargetSP target_sp(GetSP());
1993 if (target_sp)
1994 {
1995 Address *addr_ptr = base_addr.get();
1996
1997 if (addr_ptr)
1998 {
1999 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2000 bool prefer_file_cache = false;
2001 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002002 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2003 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2004 prefer_file_cache,
2005 data.GetBytes(),
2006 data.GetByteSize(),
2007 error,
2008 &load_addr);
2009 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002010 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2011 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002012 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002013 *addr_ptr,
2014 data.GetBytes(),
2015 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002016 count,
2017 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002018 }
2019 }
2020
2021 return sb_instructions;
2022
2023}
2024
2025lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002026SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2027{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002028 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2029}
2030
2031lldb::SBInstructionList
2032SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2033{
Sean Callanan50952e92011-12-14 23:49:37 +00002034 SBInstructionList sb_instructions;
2035
Greg Claytonacdbe812012-01-30 09:04:36 +00002036 TargetSP target_sp(GetSP());
2037 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002038 {
2039 Address addr;
2040
2041 if (base_addr.get())
2042 addr = *base_addr.get();
2043
Greg Clayton3faf47c2013-03-28 23:42:53 +00002044 const bool data_from_file = true;
2045
Greg Claytonacdbe812012-01-30 09:04:36 +00002046 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002047 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002048 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002049 addr,
2050 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002051 size,
2052 UINT32_MAX,
2053 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002054 }
2055
2056 return sb_instructions;
2057}
2058
2059lldb::SBInstructionList
2060SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2061{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002062 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2063}
2064
2065lldb::SBInstructionList
2066SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2067{
2068 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002069}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002070
2071SBError
2072SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2073 lldb::addr_t section_base_addr)
2074{
2075 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002076 TargetSP target_sp(GetSP());
2077 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002078 {
2079 if (!section.IsValid())
2080 {
2081 sb_error.SetErrorStringWithFormat ("invalid section");
2082 }
2083 else
2084 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002085 SectionSP section_sp (section.GetSP());
2086 if (section_sp)
2087 {
2088 if (section_sp->IsThreadSpecific())
2089 {
2090 sb_error.SetErrorString ("thread specific sections are not yet supported");
2091 }
2092 else
2093 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002094 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002095 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
Greg Clayton3c947372013-01-29 01:17:09 +00002096 {
2097 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002098 if (process_sp)
2099 process_sp->Flush();
2100 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002101 }
2102 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002103 }
2104 }
2105 else
2106 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002107 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002108 }
2109 return sb_error;
2110}
2111
2112SBError
2113SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2114{
2115 SBError sb_error;
2116
Greg Claytonacdbe812012-01-30 09:04:36 +00002117 TargetSP target_sp(GetSP());
2118 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002119 {
2120 if (!section.IsValid())
2121 {
2122 sb_error.SetErrorStringWithFormat ("invalid section");
2123 }
2124 else
2125 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002126 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002127 if (target_sp->SetSectionUnloaded (section.GetSP()))
Greg Clayton3c947372013-01-29 01:17:09 +00002128 {
2129 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002130 if (process_sp)
2131 process_sp->Flush();
2132 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002133 }
2134 }
2135 else
2136 {
2137 sb_error.SetErrorStringWithFormat ("invalid target");
2138 }
2139 return sb_error;
2140}
2141
2142SBError
2143SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2144{
2145 SBError sb_error;
2146
Greg Claytonacdbe812012-01-30 09:04:36 +00002147 TargetSP target_sp(GetSP());
2148 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002149 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002150 ModuleSP module_sp (module.GetSP());
2151 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002152 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002153 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +00002154 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002155 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002156 // The load was successful, make sure that at least some sections
2157 // changed before we notify that our module was loaded.
2158 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002159 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002160 ModuleList module_list;
2161 module_list.Append(module_sp);
2162 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002163 // Flush info in the process (stack frames, etc)
2164 ProcessSP process_sp (target_sp->GetProcessSP());
2165 if (process_sp)
2166 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002167 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002168 }
2169 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002170 else
2171 {
2172 sb_error.SetErrorStringWithFormat ("invalid module");
2173 }
2174
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002175 }
2176 else
2177 {
2178 sb_error.SetErrorStringWithFormat ("invalid target");
2179 }
2180 return sb_error;
2181}
2182
2183SBError
2184SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2185{
2186 SBError sb_error;
2187
2188 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002189 TargetSP target_sp(GetSP());
2190 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002191 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002192 ModuleSP module_sp (module.GetSP());
2193 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002194 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002195 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002196 if (objfile)
2197 {
2198 SectionList *section_list = objfile->GetSectionList();
2199 if (section_list)
2200 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002201 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002202
Greg Clayton3c947372013-01-29 01:17:09 +00002203 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002204 const size_t num_sections = section_list->GetSize();
2205 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2206 {
2207 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2208 if (section_sp)
Zachary Turner40411162014-07-16 20:28:24 +00002209 changed |= target_sp->SetSectionUnloaded (section_sp);
Greg Clayton3c947372013-01-29 01:17:09 +00002210 }
2211 if (changed)
2212 {
2213 // Flush info in the process (stack frames, etc)
2214 ProcessSP process_sp (target_sp->GetProcessSP());
2215 if (process_sp)
2216 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002217 }
2218 }
2219 else
2220 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002221 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002222 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2223 }
2224 }
2225 else
2226 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002227 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002228 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2229 }
2230 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002231 else
2232 {
2233 sb_error.SetErrorStringWithFormat ("invalid module");
2234 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002235 }
2236 else
2237 {
2238 sb_error.SetErrorStringWithFormat ("invalid target");
2239 }
2240 return sb_error;
2241}
2242
2243
Greg Claytone14e1922012-12-04 02:22:16 +00002244lldb::SBSymbolContextList
2245SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2246{
2247 SBSymbolContextList sb_sc_list;
2248 if (name && name[0])
2249 {
2250 TargetSP target_sp(GetSP());
2251 if (target_sp)
2252 {
2253 bool append = true;
2254 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2255 symbol_type,
2256 *sb_sc_list,
2257 append);
2258 }
2259 }
2260 return sb_sc_list;
2261
2262}
2263
2264
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002265lldb::SBValue
2266SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2267{
Greg Clayton5160ce52013-03-27 23:08:40 +00002268 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2269 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002270 SBValue expr_result;
Jim Ingham8646d3c2014-05-05 02:47:44 +00002271 ExpressionResults exe_results = eExpressionSetupError;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002272 ValueObjectSP expr_value_sp;
2273 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002274 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002275 if (target_sp)
2276 {
2277 if (expr == NULL || expr[0] == '\0')
2278 {
2279 if (log)
2280 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2281 return expr_result;
2282 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002283
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002284 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2285 ExecutionContext exe_ctx (m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002286
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002287 if (log)
2288 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002289
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002290 frame = exe_ctx.GetFramePtr();
2291 Target *target = exe_ctx.GetTargetPtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002292
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002293 if (target)
2294 {
2295#ifdef LLDB_CONFIGURATION_DEBUG
2296 StreamString frame_description;
2297 if (frame)
2298 frame->DumpUsingSettingsFormat (&frame_description);
2299 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2300 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2301#endif
2302 exe_results = target->EvaluateExpression (expr,
2303 frame,
2304 expr_value_sp,
2305 options.ref());
2306
2307 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2308#ifdef LLDB_CONFIGURATION_DEBUG
2309 Host::SetCrashDescription (NULL);
2310#endif
2311 }
2312 else
2313 {
2314 if (log)
2315 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2316 }
2317 }
2318#ifndef LLDB_DISABLE_PYTHON
2319 if (expr_log)
2320 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002321 expr_result.GetValue(), expr_result.GetSummary());
2322
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002323 if (log)
2324 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002325 static_cast<void*>(frame), expr,
2326 static_cast<void*>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002327#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002328
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002329 return expr_result;
2330}
2331
Greg Clayton13fbb992013-02-01 00:47:49 +00002332
2333lldb::addr_t
2334SBTarget::GetStackRedZoneSize()
2335{
2336 TargetSP target_sp(GetSP());
2337 if (target_sp)
2338 {
2339 ABISP abi_sp;
2340 ProcessSP process_sp (target_sp->GetProcessSP());
2341 if (process_sp)
2342 abi_sp = process_sp->GetABI();
2343 else
2344 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2345 if (abi_sp)
2346 return abi_sp->GetRedZoneSize();
2347 }
2348 return 0;
2349}
Ilia K8f37ca52015-02-13 14:31:06 +00002350
2351lldb::SBLaunchInfo
2352SBTarget::GetLaunchInfo () const
2353{
2354 lldb::SBLaunchInfo launch_info(NULL);
2355 TargetSP target_sp(GetSP());
2356 if (target_sp)
2357 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2358 return launch_info;
2359}
2360
2361void
2362SBTarget::SetLaunchInfo (const lldb::SBLaunchInfo &launch_info)
2363{
2364 TargetSP target_sp(GetSP());
2365 if (target_sp)
2366 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
2367}