blob: b67d12ebe5a6e4e4a0e1877593d1b21513c6e9cf [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"
Greg Clayton1f746072012-08-29 21:13:06 +000050#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000053#include "lldb/Target/ABI.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000054#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055#include "lldb/Target/Process.h"
56#include "lldb/Target/Target.h"
57#include "lldb/Target/TargetList.h"
58
59#include "lldb/Interpreter/CommandReturnObject.h"
60#include "../source/Commands/CommandObjectBreakpoint.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000061#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
64using namespace lldb;
65using namespace lldb_private;
66
67#define DEFAULT_DISASM_BYTE_SIZE 32
68
Oleksiy Vyalov37386142015-02-10 22:49:57 +000069namespace {
70
71Error
72AttachToProcess (ProcessAttachInfo &attach_info, Target &target)
73{
74 Mutex::Locker api_locker (target.GetAPIMutex ());
75
76 auto process_sp = target.GetProcessSP ();
77 if (process_sp)
78 {
79 const auto state = process_sp->GetState ();
80 if (process_sp->IsAlive () && state == eStateConnected)
81 {
82 // If we are already connected, then we have already specified the
83 // listener, so if a valid listener is supplied, we need to error out
84 // to let the client know.
85 if (attach_info.GetListener ())
86 return Error ("process is connected and already has a listener, pass empty listener");
87 }
88 }
89
90 return target.Attach (attach_info, nullptr);
91}
92
93} // namespace
94
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095//----------------------------------------------------------------------
96// SBTarget constructor
97//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +000098SBTarget::SBTarget () :
99 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100{
101}
102
103SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000104 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105{
106}
107
108SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000109 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110{
111}
112
Greg Claytonefabb122010-11-05 23:17:00 +0000113const SBTarget&
114SBTarget::operator = (const SBTarget& rhs)
115{
116 if (this != &rhs)
117 m_opaque_sp = rhs.m_opaque_sp;
118 return *this;
119}
120
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121//----------------------------------------------------------------------
122// Destructor
123//----------------------------------------------------------------------
124SBTarget::~SBTarget()
125{
126}
127
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000128const char *
129SBTarget::GetBroadcasterClassName ()
130{
131 return Target::GetStaticBroadcasterClass().AsCString();
132}
133
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134bool
135SBTarget::IsValid () const
136{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000137 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138}
139
140SBProcess
141SBTarget::GetProcess ()
142{
143 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000144 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000145 TargetSP target_sp(GetSP());
146 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000147 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000148 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000149 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000150 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000151
Greg Clayton5160ce52013-03-27 23:08:40 +0000152 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000153 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000154 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
155 static_cast<void*>(target_sp.get()),
156 static_cast<void*>(process_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000157
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 return sb_process;
159}
160
Matthew Gardinerc928de32014-10-22 07:22:56 +0000161SBPlatform
162SBTarget::GetPlatform ()
163{
164 TargetSP target_sp(GetSP());
165 if (!target_sp)
166 return SBPlatform();
167
168 SBPlatform platform;
169 platform.m_opaque_sp = target_sp->GetPlatform();
170
171 return platform;
172}
173
Greg Clayton66111032010-06-23 01:19:29 +0000174SBDebugger
175SBTarget::GetDebugger () const
176{
177 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000178 TargetSP target_sp(GetSP());
179 if (target_sp)
180 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000181 return debugger;
182}
183
Jim Ingham270684d2011-03-31 00:01:24 +0000184SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000185SBTarget::LoadCore (const char *core_file)
186{
187 SBProcess sb_process;
188 TargetSP target_sp(GetSP());
189 if (target_sp)
190 {
191 FileSpec filespec(core_file, true);
192 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
193 NULL,
194 &filespec));
195 if (process_sp)
196 {
197 process_sp->LoadCore();
198 sb_process.SetSP (process_sp);
199 }
200 }
201 return sb_process;
202}
203
204SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000205SBTarget::LaunchSimple
206(
207 char const **argv,
208 char const **envp,
209 const char *working_directory
210)
211{
212 char *stdin_path = NULL;
213 char *stdout_path = NULL;
214 char *stderr_path = NULL;
215 uint32_t launch_flags = 0;
216 bool stop_at_entry = false;
217 SBError error;
218 SBListener listener = GetDebugger().GetListener();
219 return Launch (listener,
220 argv,
221 envp,
222 stdin_path,
223 stdout_path,
224 stderr_path,
225 working_directory,
226 launch_flags,
227 stop_at_entry,
228 error);
229}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000230
Greg Claytonfbb76342013-11-20 21:07:01 +0000231SBError
232SBTarget::Install()
233{
234 SBError sb_error;
235 TargetSP target_sp(GetSP());
236 if (target_sp)
237 {
238 Mutex::Locker api_locker (target_sp->GetAPIMutex());
239 sb_error.ref() = target_sp->Install(NULL);
240 }
241 return sb_error;
242}
243
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000244SBProcess
245SBTarget::Launch
246(
Greg Clayton4b045622011-02-03 21:28:34 +0000247 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000248 char const **argv,
249 char const **envp,
250 const char *stdin_path,
251 const char *stdout_path,
252 const char *stderr_path,
253 const char *working_directory,
254 uint32_t launch_flags, // See LaunchFlags
255 bool stop_at_entry,
256 lldb::SBError& error
257)
258{
Greg Clayton5160ce52013-03-27 23:08:40 +0000259 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000260
Greg Claytonacdbe812012-01-30 09:04:36 +0000261 SBProcess sb_process;
262 ProcessSP process_sp;
263 TargetSP target_sp(GetSP());
264
Caroline Ticeceb6b132010-10-26 03:11:13 +0000265 if (log)
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000266 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 +0000267 static_cast<void*>(target_sp.get()),
268 static_cast<void*>(argv), static_cast<void*>(envp),
269 stdin_path ? stdin_path : "NULL",
270 stdout_path ? stdout_path : "NULL",
271 stderr_path ? stderr_path : "NULL",
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000272 working_directory ? working_directory : "NULL",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000273 launch_flags, stop_at_entry,
274 static_cast<void*>(error.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000275
276 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000278 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000279
Zachary Turner6fd3f342015-02-02 18:50:01 +0000280 if (stop_at_entry)
281 launch_flags |= eLaunchFlagStopAtEntry;
282
Greg Clayton645bf542011-01-27 01:01:10 +0000283 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
284 launch_flags |= eLaunchFlagDisableASLR;
285
Greg Clayton2289fa42011-04-30 01:09:13 +0000286 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000287 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000288 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000289 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000290 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000291
Greg Claytonb9556ac2012-01-30 07:41:31 +0000292 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000293 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000294 if (state == eStateAttaching)
295 error.SetErrorString ("process attach is in progress");
296 else
297 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000298 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000299 }
Greg Clayton931180e2011-01-27 06:44:37 +0000300 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000301
Greg Clayton2289fa42011-04-30 01:09:13 +0000302 if (state == eStateConnected)
303 {
304 // If we are already connected, then we have already specified the
305 // listener, so if a valid listener is supplied, we need to error out
306 // to let the client know.
307 if (listener.IsValid())
308 {
309 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000310 return sb_process;
311 }
312 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000313
314 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
315 launch_flags |= eLaunchFlagDisableSTDIO;
316
317 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318
Greg Claytonb09c5382013-12-13 17:20:18 +0000319 Module *exe_module = target_sp->GetExecutableModulePointer();
320 if (exe_module)
321 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
322 if (argv)
323 launch_info.GetArguments().AppendArguments (argv);
324 if (envp)
325 launch_info.GetEnvironmentEntries ().SetArguments (envp);
326
327 if (listener.IsValid())
Greg Clayton8012cad2014-11-17 19:39:20 +0000328 launch_info.SetListener(listener.GetSP());
329
330 error.SetError (target_sp->Launch(launch_info, NULL));
Greg Clayton645bf542011-01-27 01:01:10 +0000331
Greg Claytonb09c5382013-12-13 17:20:18 +0000332 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton524e60b2010-10-06 22:10:17 +0000333 }
334 else
335 {
336 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000338
Caroline Tice20ad3c42010-10-29 21:48:37 +0000339 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000340 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000341 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
342 static_cast<void*>(target_sp.get()),
343 static_cast<void*>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000344
Greg Clayton5d5028b2010-10-06 03:53:16 +0000345 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
Greg Clayton0e615682012-02-24 05:03:03 +0000348SBProcess
349SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
350{
Greg Clayton5160ce52013-03-27 23:08:40 +0000351 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000352
Greg Clayton0e615682012-02-24 05:03:03 +0000353 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000354 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000355
Greg Clayton0e615682012-02-24 05:03:03 +0000356 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000357 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
358 static_cast<void*>(target_sp.get()));
359
Greg Clayton0e615682012-02-24 05:03:03 +0000360 if (target_sp)
361 {
362 Mutex::Locker api_locker (target_sp->GetAPIMutex());
363 StateType state = eStateInvalid;
Greg Claytonb09c5382013-12-13 17:20:18 +0000364 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000365 ProcessSP process_sp = target_sp->GetProcessSP();
366 if (process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000367 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000368 state = process_sp->GetState();
369
370 if (process_sp->IsAlive() && state != eStateConnected)
371 {
372 if (state == eStateAttaching)
373 error.SetErrorString ("process attach is in progress");
374 else
375 error.SetErrorString ("a process is already being debugged");
376 return sb_process;
377 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000378 }
Greg Clayton0e615682012-02-24 05:03:03 +0000379 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000380
381 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
382
Greg Clayton3e32ad62014-05-07 20:16:06 +0000383 if (!launch_info.GetExecutableFile())
384 {
385 Module *exe_module = target_sp->GetExecutableModulePointer();
386 if (exe_module)
387 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
388 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000389
390 const ArchSpec &arch_spec = target_sp->GetArchitecture();
391 if (arch_spec.IsValid())
392 launch_info.GetArchitecture () = arch_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000393
Greg Clayton8012cad2014-11-17 19:39:20 +0000394 error.SetError (target_sp->Launch (launch_info, NULL));
Greg Claytonb09c5382013-12-13 17:20:18 +0000395 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000396 }
397 else
398 {
399 error.SetErrorString ("SBTarget is invalid");
400 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000401
Greg Clayton0e615682012-02-24 05:03:03 +0000402 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
403 if (log)
Greg Claytonb09c5382013-12-13 17:20:18 +0000404 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000405 static_cast<void*>(target_sp.get()),
406 static_cast<void*>(sb_process.GetSP().get()));
407
Greg Clayton0e615682012-02-24 05:03:03 +0000408 return sb_process;
409}
410
411lldb::SBProcess
412SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
413{
Greg Clayton5160ce52013-03-27 23:08:40 +0000414 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000415
Greg Clayton0e615682012-02-24 05:03:03 +0000416 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000417 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000418
Sean Callanan575a4542012-10-20 00:21:31 +0000419 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000420 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
421 static_cast<void*>(target_sp.get()));
422
Greg Clayton0e615682012-02-24 05:03:03 +0000423 if (target_sp)
424 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000425 ProcessAttachInfo &attach_info = sb_attach_info.ref();
426 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Greg Clayton0e615682012-02-24 05:03:03 +0000427 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000428 PlatformSP platform_sp = target_sp->GetPlatform();
429 // See if we can pre-verify if a process exists or not
430 if (platform_sp && platform_sp->IsConnected())
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000431 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000432 lldb::pid_t attach_pid = attach_info.GetProcessID();
433 ProcessInstanceInfo instance_info;
434 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
Han Ming Ongcec8c902012-02-29 00:12:56 +0000435 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000436 attach_info.SetUserID(instance_info.GetEffectiveUserID());
437 }
438 else
439 {
440 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
441 if (log)
Greg Clayton91e407e2012-09-27 00:03:39 +0000442 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000443 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
444 static_cast<void*>(target_sp.get()), error.GetCString());
Greg Clayton91e407e2012-09-27 00:03:39 +0000445 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000446 return sb_process;
Han Ming Ongcec8c902012-02-29 00:12:56 +0000447 }
448 }
Greg Clayton0e615682012-02-24 05:03:03 +0000449 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000450 error.SetError(AttachToProcess(attach_info, *target_sp));
451 if (error.Success())
452 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000453 }
454 else
455 {
456 error.SetErrorString ("SBTarget is invalid");
457 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000458
Sean Callanan575a4542012-10-20 00:21:31 +0000459 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000460 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000461 static_cast<void*>(target_sp.get()),
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000462 static_cast<void*>(sb_process.GetSP().get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000463
Greg Clayton0e615682012-02-24 05:03:03 +0000464 return sb_process;
465}
466
467
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000468#if defined(__APPLE__)
469
470lldb::SBProcess
471SBTarget::AttachToProcessWithID (SBListener &listener,
472 ::pid_t pid,
473 lldb::SBError& error)
474{
475 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
476}
477
478#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000479
480lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000481SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000482(
Greg Clayton4b045622011-02-03 21:28:34 +0000483 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000484 lldb::pid_t pid,// The process ID to attach to
485 SBError& error // An error explaining what went wrong if attach fails
486)
487{
Greg Clayton5160ce52013-03-27 23:08:40 +0000488 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000489
Greg Clayton524e60b2010-10-06 22:10:17 +0000490 SBProcess sb_process;
Greg Claytonacdbe812012-01-30 09:04:36 +0000491 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000492
493 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000494 log->Printf ("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
495 static_cast<void*>(target_sp.get()),
496 __FUNCTION__,
497 pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000498
Greg Claytonacdbe812012-01-30 09:04:36 +0000499 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000500 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000501 ProcessAttachInfo attach_info;
502 attach_info.SetProcessID (pid);
503 if (listener.IsValid())
504 attach_info.SetListener(listener.GetSP());
Greg Clayton0c74e782011-06-24 03:21:43 +0000505
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000506 ProcessInstanceInfo instance_info;
507 if (target_sp->GetPlatform ()->GetProcessInfo (pid, instance_info))
508 attach_info.SetUserID (instance_info.GetEffectiveUserID ());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000509
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000510 error.SetError (AttachToProcess (attach_info, *target_sp));
511 if (error.Success ())
512 sb_process.SetSP (target_sp->GetProcessSP ());
Greg Clayton524e60b2010-10-06 22:10:17 +0000513 }
514 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000515 error.SetErrorString ("SBTarget is invalid");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000516
Sean Callanan575a4542012-10-20 00:21:31 +0000517 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000518 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
519 static_cast<void*>(target_sp.get ()),
520 __FUNCTION__,
521 static_cast<void*>(sb_process.GetSP().get ()));
Greg Clayton524e60b2010-10-06 22:10:17 +0000522 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +0000523}
524
525lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000526SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +0000527(
Greg Clayton4b045622011-02-03 21:28:34 +0000528 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000529 const char *name, // basename of process to attach to
530 bool wait_for, // if true wait for a new instance of "name" to be launched
531 SBError& error // An error explaining what went wrong if attach fails
532)
533{
Greg Clayton5160ce52013-03-27 23:08:40 +0000534 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000535
Greg Clayton524e60b2010-10-06 22:10:17 +0000536 SBProcess sb_process;
Greg Claytonacdbe812012-01-30 09:04:36 +0000537 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000538
Sean Callanan575a4542012-10-20 00:21:31 +0000539 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000540 log->Printf ("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
541 static_cast<void*>(target_sp.get()),
542 __FUNCTION__,
543 name,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000544 wait_for ? "true" : "false");
545
Greg Claytonacdbe812012-01-30 09:04:36 +0000546 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000547 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000548 ProcessAttachInfo attach_info;
549 attach_info.GetExecutableFile().SetFile(name, false);
550 attach_info.SetWaitForLaunch(wait_for);
551 if (listener.IsValid())
552 attach_info.SetListener(listener.GetSP());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000553
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000554 error.SetError (AttachToProcess (attach_info, *target_sp));
555 if (error.Success ())
556 sb_process.SetSP (target_sp->GetProcessSP ());
Greg Clayton524e60b2010-10-06 22:10:17 +0000557 }
558 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000559 error.SetErrorString ("SBTarget is invalid");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000560
Sean Callanan575a4542012-10-20 00:21:31 +0000561 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000562 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000563 static_cast<void*>(target_sp.get()),
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000564 __FUNCTION__,
565 static_cast<void*>(sb_process.GetSP().get()));
Greg Clayton524e60b2010-10-06 22:10:17 +0000566 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +0000567}
568
James McIlree9631aae2011-03-04 00:31:13 +0000569lldb::SBProcess
570SBTarget::ConnectRemote
571(
572 SBListener &listener,
573 const char *url,
574 const char *plugin_name,
575 SBError& error
576)
577{
Greg Clayton5160ce52013-03-27 23:08:40 +0000578 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000579
James McIlree9631aae2011-03-04 00:31:13 +0000580 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000581 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000582 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000583
Sean Callanan575a4542012-10-20 00:21:31 +0000584 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000585 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
586 static_cast<void*>(target_sp.get()), url, plugin_name);
587
Greg Claytonacdbe812012-01-30 09:04:36 +0000588 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +0000589 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000590 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +0000591 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000592 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000593 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000594 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000595
Greg Claytonb9556ac2012-01-30 07:41:31 +0000596 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +0000597 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000598 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000599 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +0000600 }
601 else
602 {
603 error.SetErrorString ("unable to create lldb_private::Process");
604 }
605 }
606 else
607 {
608 error.SetErrorString ("SBTarget is invalid");
609 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000610
Sean Callanan575a4542012-10-20 00:21:31 +0000611 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000612 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000613 static_cast<void*>(target_sp.get()),
614 static_cast<void*>(process_sp.get()));
James McIlree9631aae2011-03-04 00:31:13 +0000615 return sb_process;
616}
617
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618SBFileSpec
619SBTarget::GetExecutable ()
620{
Caroline Ticeceb6b132010-10-26 03:11:13 +0000621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +0000623 TargetSP target_sp(GetSP());
624 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000626 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +0000627 if (exe_module)
628 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000630
Greg Clayton5160ce52013-03-27 23:08:40 +0000631 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000632 if (log)
633 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000634 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
635 static_cast<void*>(target_sp.get()),
636 static_cast<const void*>(exe_file_spec.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000637 }
638
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 return exe_file_spec;
640}
641
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643SBTarget::operator == (const SBTarget &rhs) const
644{
Greg Clayton66111032010-06-23 01:19:29 +0000645 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646}
647
648bool
649SBTarget::operator != (const SBTarget &rhs) const
650{
Greg Clayton66111032010-06-23 01:19:29 +0000651 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652}
653
Greg Claytonb9556ac2012-01-30 07:41:31 +0000654lldb::TargetSP
655SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +0000656{
657 return m_opaque_sp;
658}
659
Greg Clayton66111032010-06-23 01:19:29 +0000660void
Greg Claytonb9556ac2012-01-30 07:41:31 +0000661SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +0000662{
663 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664}
665
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000666lldb::SBAddress
667SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000668{
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000669 lldb::SBAddress sb_addr;
670 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +0000671 TargetSP target_sp(GetSP());
672 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000673 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000674 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytond5944cd2013-12-06 01:12:00 +0000675 if (target_sp->ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000676 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +0000677 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000678
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000679 // We have a load address that isn't in a section, just return an address
680 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +0000681 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000682 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000683}
684
Matthew Gardinerc928de32014-10-22 07:22:56 +0000685lldb::SBAddress
686SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
687{
688 lldb::SBAddress sb_addr;
689 Address &addr = sb_addr.ref();
690 TargetSP target_sp(GetSP());
691 if (target_sp)
692 {
693 Mutex::Locker api_locker (target_sp->GetAPIMutex());
694 if (target_sp->ResolveFileAddress (file_addr, addr))
695 return sb_addr;
696 }
697
698 addr.SetRawAddress(file_addr);
699 return sb_addr;
700}
Greg Claytond5944cd2013-12-06 01:12:00 +0000701
702lldb::SBAddress
703SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
704{
705 lldb::SBAddress sb_addr;
706 Address &addr = sb_addr.ref();
707 TargetSP target_sp(GetSP());
708 if (target_sp)
709 {
710 Mutex::Locker api_locker (target_sp->GetAPIMutex());
711 if (target_sp->ResolveLoadAddress (vm_addr, addr))
712 return sb_addr;
713 }
714
715 // We have a load address that isn't in a section, just return an address
716 // with the offset filled in (the address) and the section set to NULL
717 addr.SetRawAddress(vm_addr);
718 return sb_addr;
719}
720
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000721SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +0000722SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
723 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000724{
725 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +0000726 if (addr.IsValid())
727 {
728 TargetSP target_sp(GetSP());
729 if (target_sp)
730 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
731 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000732 return sc;
733}
734
Matthew Gardinerc928de32014-10-22 07:22:56 +0000735size_t
736SBTarget::ReadMemory (const SBAddress addr,
737 void *buf,
738 size_t size,
739 lldb::SBError &error)
740{
741 SBError sb_error;
742 size_t bytes_read = 0;
743 TargetSP target_sp(GetSP());
744 if (target_sp)
745 {
746 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton8691dc52014-11-04 00:56:30 +0000747 bytes_read = target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
748 }
749 else
750 {
751 sb_error.SetErrorString("invalid target");
Matthew Gardinerc928de32014-10-22 07:22:56 +0000752 }
753
754 return bytes_read;
755}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000756
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000758SBTarget::BreakpointCreateByLocation (const char *file,
759 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760{
Greg Clayton7481c202010-11-08 00:28:40 +0000761 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
764SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000765SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
766 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767{
Greg Clayton5160ce52013-03-27 23:08:40 +0000768 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000769
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000771 TargetSP target_sp(GetSP());
772 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000773 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000774 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000775
Greg Clayton1f746072012-08-29 21:13:06 +0000776 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +0000777 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +0000778 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000779 const bool hardware = false;
780 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000781 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000782
783 if (log)
784 {
785 SBStream sstr;
786 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000787 char path[PATH_MAX];
788 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000789 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
790 static_cast<void*>(target_sp.get()), path, line,
791 static_cast<void*>(sb_bp.get()), sstr.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000792 }
793
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794 return sb_bp;
795}
796
797SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000798SBTarget::BreakpointCreateByName (const char *symbol_name,
799 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800{
Greg Clayton5160ce52013-03-27 23:08:40 +0000801 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000802
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000804 TargetSP target_sp(GetSP());
805 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000807 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000808
Jim Inghama8558b62012-05-22 00:12:20 +0000809 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000810 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000811 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 if (module_name && module_name[0])
813 {
Jim Ingham969795f2011-09-21 01:17:13 +0000814 FileSpecList module_spec_list;
815 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +0000816 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 }
818 else
819 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000820 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 }
822 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000823
Caroline Ticeceb6b132010-10-26 03:11:13 +0000824 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000825 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
826 static_cast<void*>(target_sp.get()), symbol_name,
827 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000828
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 return sb_bp;
830}
831
Jim Ingham87df91b2011-09-23 00:54:11 +0000832lldb::SBBreakpoint
833SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +0000834 const SBFileSpecList &module_list,
835 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +0000836{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +0000837 uint32_t name_type_mask = eFunctionNameTypeAuto;
838 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
839}
840
841lldb::SBBreakpoint
842SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +0000843 uint32_t name_type_mask,
844 const SBFileSpecList &module_list,
845 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +0000846{
Greg Clayton5160ce52013-03-27 23:08:40 +0000847 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +0000848
849 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000850 TargetSP target_sp(GetSP());
851 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +0000852 {
Jim Inghama8558b62012-05-22 00:12:20 +0000853 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000854 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000855 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +0000856 Mutex::Locker api_locker (target_sp->GetAPIMutex());
857 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +0000858 comp_unit_list.get(),
859 symbol_name,
860 name_type_mask,
861 skip_prologue,
862 internal,
863 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +0000864 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000865
Jim Ingham87df91b2011-09-23 00:54:11 +0000866 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000867 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
868 static_cast<void*>(target_sp.get()), symbol_name,
869 name_type_mask, static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +0000870
871 return sb_bp;
872}
873
Jim Inghamfab10e82012-03-06 00:37:27 +0000874lldb::SBBreakpoint
875SBTarget::BreakpointCreateByNames (const char *symbol_names[],
876 uint32_t num_names,
877 uint32_t name_type_mask,
878 const SBFileSpecList &module_list,
879 const SBFileSpecList &comp_unit_list)
880{
Greg Clayton5160ce52013-03-27 23:08:40 +0000881 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +0000882
883 SBBreakpoint sb_bp;
884 TargetSP target_sp(GetSP());
885 if (target_sp && num_names > 0)
886 {
887 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +0000888 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000889 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000890 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +0000891 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
892 comp_unit_list.get(),
893 symbol_names,
894 num_names,
895 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000896 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000897 internal,
898 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +0000899 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000900
Jim Inghamfab10e82012-03-06 00:37:27 +0000901 if (log)
902 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000903 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
904 static_cast<void*>(target_sp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +0000905 for (uint32_t i = 0 ; i < num_names; i++)
906 {
907 char sep;
908 if (i < num_names - 1)
909 sep = ',';
910 else
911 sep = '}';
912 if (symbol_names[i] != NULL)
913 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
914 else
915 log->Printf ("\"<NULL>\"%c ", sep);
Jim Inghamfab10e82012-03-06 00:37:27 +0000916 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000917 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
918 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +0000919 }
920
921 return sb_bp;
922}
Jim Ingham87df91b2011-09-23 00:54:11 +0000923
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000925SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
926 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927{
Greg Clayton5160ce52013-03-27 23:08:40 +0000928 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000929
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000931 TargetSP target_sp(GetSP());
932 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000934 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +0000936 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000937 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000938 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000939
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 if (module_name && module_name[0])
941 {
Jim Ingham969795f2011-09-21 01:17:13 +0000942 FileSpecList module_spec_list;
943 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000944
Greg Claytoneb023e72013-10-11 19:48:25 +0000945 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 }
947 else
948 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000949 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 }
951 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000952
953 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000954 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
955 static_cast<void*>(target_sp.get()), symbol_name_regex,
956 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000957
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958 return sb_bp;
959}
960
Jim Ingham87df91b2011-09-23 00:54:11 +0000961lldb::SBBreakpoint
962SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000963 const SBFileSpecList &module_list,
964 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +0000965{
Greg Clayton5160ce52013-03-27 23:08:40 +0000966 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967
Jim Ingham87df91b2011-09-23 00:54:11 +0000968 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000969 TargetSP target_sp(GetSP());
970 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +0000971 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000972 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +0000973 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +0000974 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000975 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000976 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000977
Greg Claytoneb023e72013-10-11 19:48:25 +0000978 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +0000979 }
980
981 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000982 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
983 static_cast<void*>(target_sp.get()), symbol_name_regex,
984 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +0000985
986 return sb_bp;
987}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988
989SBBreakpoint
990SBTarget::BreakpointCreateByAddress (addr_t address)
991{
Greg Clayton5160ce52013-03-27 23:08:40 +0000992 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000993
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000995 TargetSP target_sp(GetSP());
996 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000997 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000998 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +0000999 const bool hardware = false;
1000 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001001 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001002
Caroline Ticeceb6b132010-10-26 03:11:13 +00001003 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001004 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1005 static_cast<void*>(target_sp.get()),
1006 static_cast<uint64_t>(address),
1007 static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001008
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 return sb_bp;
1010}
1011
Jim Ingham969795f2011-09-21 01:17:13 +00001012lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001013SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1014 const lldb::SBFileSpec &source_file,
1015 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001016{
Greg Clayton5160ce52013-03-27 23:08:40 +00001017 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001018
1019 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001020 TargetSP target_sp(GetSP());
1021 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001022 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001023 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001024 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001025 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001026 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001027 source_file_spec_list.Append (source_file.ref());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001028
Jim Ingham969795f2011-09-21 01:17:13 +00001029 if (module_name && module_name[0])
1030 {
1031 FileSpecList module_spec_list;
1032 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001033
Greg Claytoneb023e72013-10-11 19:48:25 +00001034 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001035 }
1036 else
1037 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001038 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001039 }
1040 }
1041
1042 if (log)
1043 {
1044 char path[PATH_MAX];
1045 source_file->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001046 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1047 static_cast<void*>(target_sp.get()), source_regex, path,
1048 module_name, static_cast<void*>(sb_bp.get()));
Jim Ingham969795f2011-09-21 01:17:13 +00001049 }
1050
1051 return sb_bp;
1052}
1053
Jim Ingham87df91b2011-09-23 00:54:11 +00001054lldb::SBBreakpoint
Jim Inghame7320522015-02-12 17:37:46 +00001055SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1056 const SBFileSpecList &module_list,
1057 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001058{
Greg Clayton5160ce52013-03-27 23:08:40 +00001059 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001060
1061 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001062 TargetSP target_sp(GetSP());
1063 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001064 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001065 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001066 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001067 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001068 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001069 }
1070
1071 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001072 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1073 static_cast<void*>(target_sp.get()), source_regex,
1074 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001075
1076 return sb_bp;
1077}
Jim Ingham969795f2011-09-21 01:17:13 +00001078
Jim Inghamfab10e82012-03-06 00:37:27 +00001079lldb::SBBreakpoint
1080SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001081 bool catch_bp,
1082 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001083{
Greg Clayton5160ce52013-03-27 23:08:40 +00001084 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001085
1086 SBBreakpoint sb_bp;
1087 TargetSP target_sp(GetSP());
1088 if (target_sp)
1089 {
1090 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001091 const bool hardware = false;
1092 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001093 }
1094
1095 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001096 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1097 static_cast<void*>(target_sp.get()),
Jim Inghamfab10e82012-03-06 00:37:27 +00001098 LanguageRuntime::GetNameForLanguageType(language),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001099 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1100 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001101
1102 return sb_bp;
1103}
1104
Greg Clayton9fed0d82010-07-23 23:33:17 +00001105uint32_t
1106SBTarget::GetNumBreakpoints () const
1107{
Greg Claytonacdbe812012-01-30 09:04:36 +00001108 TargetSP target_sp(GetSP());
1109 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001110 {
1111 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001112 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001113 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001114 return 0;
1115}
1116
1117SBBreakpoint
1118SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1119{
1120 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001121 TargetSP target_sp(GetSP());
1122 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001123 {
1124 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001125 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001126 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001127 return sb_breakpoint;
1128}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129
1130bool
1131SBTarget::BreakpointDelete (break_id_t bp_id)
1132{
Greg Clayton5160ce52013-03-27 23:08:40 +00001133 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001134
Caroline Ticeceb6b132010-10-26 03:11:13 +00001135 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001136 TargetSP target_sp(GetSP());
1137 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001138 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001139 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1140 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001141 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001142
1143 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001144 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1145 static_cast<void*>(target_sp.get()),
1146 static_cast<uint32_t>(bp_id), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001147
1148 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149}
1150
Johnny Chen5d043462011-09-26 22:40:50 +00001151SBBreakpoint
1152SBTarget::FindBreakpointByID (break_id_t bp_id)
1153{
Greg Clayton5160ce52013-03-27 23:08:40 +00001154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001155
1156 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001157 TargetSP target_sp(GetSP());
1158 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001159 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001160 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1161 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001162 }
1163
1164 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001165 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1166 static_cast<void*>(target_sp.get()),
1167 static_cast<uint32_t>(bp_id),
1168 static_cast<void*>(sb_breakpoint.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001169
1170 return sb_breakpoint;
1171}
1172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173bool
1174SBTarget::EnableAllBreakpoints ()
1175{
Greg Claytonacdbe812012-01-30 09:04:36 +00001176 TargetSP target_sp(GetSP());
1177 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001179 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1180 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 return true;
1182 }
1183 return false;
1184}
1185
1186bool
1187SBTarget::DisableAllBreakpoints ()
1188{
Greg Claytonacdbe812012-01-30 09:04:36 +00001189 TargetSP target_sp(GetSP());
1190 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001192 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1193 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 return true;
1195 }
1196 return false;
1197}
1198
1199bool
1200SBTarget::DeleteAllBreakpoints ()
1201{
Greg Claytonacdbe812012-01-30 09:04:36 +00001202 TargetSP target_sp(GetSP());
1203 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001205 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1206 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207 return true;
1208 }
1209 return false;
1210}
1211
Johnny Chen5d043462011-09-26 22:40:50 +00001212uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001213SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001214{
Greg Claytonacdbe812012-01-30 09:04:36 +00001215 TargetSP target_sp(GetSP());
1216 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001217 {
Johnny Chen01a67862011-10-14 00:42:25 +00001218 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001219 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001220 }
1221 return 0;
1222}
1223
Greg Clayton1b282f92011-10-13 18:08:26 +00001224SBWatchpoint
1225SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001226{
Johnny Chen01a67862011-10-14 00:42:25 +00001227 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001228 TargetSP target_sp(GetSP());
1229 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001230 {
Johnny Chen01a67862011-10-14 00:42:25 +00001231 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001232 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001233 }
Johnny Chen01a67862011-10-14 00:42:25 +00001234 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001235}
1236
1237bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001238SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001239{
Greg Clayton5160ce52013-03-27 23:08:40 +00001240 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001241
1242 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001243 TargetSP target_sp(GetSP());
1244 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001245 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001246 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001247 Mutex::Locker locker;
1248 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001249 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001250 }
1251
1252 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001253 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1254 static_cast<void*>(target_sp.get()),
1255 static_cast<uint32_t>(wp_id), result);
Johnny Chen5d043462011-09-26 22:40:50 +00001256
1257 return result;
1258}
1259
Greg Clayton1b282f92011-10-13 18:08:26 +00001260SBWatchpoint
1261SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001262{
Greg Clayton5160ce52013-03-27 23:08:40 +00001263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001264
Greg Clayton1b282f92011-10-13 18:08:26 +00001265 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001266 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001267 TargetSP target_sp(GetSP());
1268 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001269 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001270 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001271 Mutex::Locker locker;
1272 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001273 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1274 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001275 }
1276
1277 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001278 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1279 static_cast<void*>(target_sp.get()),
1280 static_cast<uint32_t>(wp_id),
1281 static_cast<void*>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001282
Greg Clayton1b282f92011-10-13 18:08:26 +00001283 return sb_watchpoint;
1284}
1285
1286lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001287SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001288{
Greg Clayton5160ce52013-03-27 23:08:40 +00001289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001290
Greg Clayton1b282f92011-10-13 18:08:26 +00001291 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001292 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001293 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001294 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001295 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001296 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001297 uint32_t watch_type = 0;
1298 if (read)
1299 watch_type |= LLDB_WATCH_TYPE_READ;
1300 if (write)
1301 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001302 if (watch_type == 0)
1303 {
1304 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1305 return sb_watchpoint;
1306 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001307
Johnny Chen7385a5a2012-05-31 22:56:36 +00001308 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001309 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001310 // This API doesn't take in a type, so we can't figure out what it is.
1311 ClangASTType *type = NULL;
1312 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001313 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001314 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001315 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001316
Greg Clayton1b282f92011-10-13 18:08:26 +00001317 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001318 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001319 static_cast<void*>(target_sp.get()), addr,
1320 static_cast<uint32_t>(size),
1321 static_cast<void*>(watchpoint_sp.get()));
1322
Greg Clayton1b282f92011-10-13 18:08:26 +00001323 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001324}
1325
1326bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001327SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001328{
Greg Claytonacdbe812012-01-30 09:04:36 +00001329 TargetSP target_sp(GetSP());
1330 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001331 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001332 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001333 Mutex::Locker locker;
1334 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001335 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001336 return true;
1337 }
1338 return false;
1339}
1340
1341bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001342SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001343{
Greg Claytonacdbe812012-01-30 09:04:36 +00001344 TargetSP target_sp(GetSP());
1345 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001346 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001347 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001348 Mutex::Locker locker;
1349 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001350 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001351 return true;
1352 }
1353 return false;
1354}
1355
Enrico Granata347c2aa2013-10-08 21:49:02 +00001356SBValue
1357SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1358{
1359 SBValue sb_value;
1360 lldb::ValueObjectSP new_value_sp;
1361 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1362 {
Enrico Granata972be532014-12-17 21:18:43 +00001363 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1364 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1365 ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1366 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, exe_ctx, ast_type);
Enrico Granata347c2aa2013-10-08 21:49:02 +00001367 }
1368 sb_value.SetSP(new_value_sp);
1369 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1370 if (log)
1371 {
1372 if (new_value_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001373 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1374 static_cast<void*>(m_opaque_sp.get()),
1375 new_value_sp->GetName().AsCString());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001376 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001377 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1378 static_cast<void*>(m_opaque_sp.get()));
Enrico Granata347c2aa2013-10-08 21:49:02 +00001379 }
1380 return sb_value;
1381}
1382
Enrico Granata972be532014-12-17 21:18:43 +00001383lldb::SBValue
1384SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1385{
1386 SBValue sb_value;
1387 lldb::ValueObjectSP new_value_sp;
1388 if (IsValid() && name && *name && data.IsValid() && type.IsValid())
1389 {
1390 DataExtractorSP extractor(*data);
1391 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1392 ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1393 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, exe_ctx, ast_type);
1394 }
1395 sb_value.SetSP(new_value_sp);
1396 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1397 if (log)
1398 {
1399 if (new_value_sp)
1400 log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1401 static_cast<void*>(m_opaque_sp.get()),
1402 new_value_sp->GetName().AsCString());
1403 else
1404 log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1405 static_cast<void*>(m_opaque_sp.get()));
1406 }
1407 return sb_value;
1408}
1409
1410lldb::SBValue
1411SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1412{
1413 SBValue sb_value;
1414 lldb::ValueObjectSP new_value_sp;
1415 if (IsValid() && name && *name && expr && *expr)
1416 {
1417 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1418 new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1419 }
1420 sb_value.SetSP(new_value_sp);
1421 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1422 if (log)
1423 {
1424 if (new_value_sp)
1425 log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1426 static_cast<void*>(m_opaque_sp.get()),
1427 new_value_sp->GetName().AsCString());
1428 else
1429 log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1430 static_cast<void*>(m_opaque_sp.get()));
1431 }
1432 return sb_value;
1433}
1434
Johnny Chen5d043462011-09-26 22:40:50 +00001435bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001436SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001437{
Greg Claytonacdbe812012-01-30 09:04:36 +00001438 TargetSP target_sp(GetSP());
1439 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001440 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001441 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001442 Mutex::Locker locker;
1443 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001444 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001445 return true;
1446 }
1447 return false;
1448}
1449
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001450
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001451lldb::SBModule
1452SBTarget::AddModule (const char *path,
1453 const char *triple,
1454 const char *uuid_cstr)
1455{
Greg Claytonb210aec2012-04-23 20:23:39 +00001456 return AddModule (path, triple, uuid_cstr, NULL);
1457}
1458
1459lldb::SBModule
1460SBTarget::AddModule (const char *path,
1461 const char *triple,
1462 const char *uuid_cstr,
1463 const char *symfile)
1464{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001465 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001466 TargetSP target_sp(GetSP());
1467 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001468 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001469 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001470 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001471 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001472
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001473 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001474 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001475
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001476 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001477 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001478 else
1479 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001480
1481 if (symfile)
1482 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001483
Greg Claytonb9a01b32012-02-26 05:51:37 +00001484 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001485 }
1486 return sb_module;
1487}
1488
Greg Clayton226cce22013-07-08 22:22:41 +00001489lldb::SBModule
1490SBTarget::AddModule (const SBModuleSpec &module_spec)
1491{
1492 lldb::SBModule sb_module;
1493 TargetSP target_sp(GetSP());
1494 if (target_sp)
1495 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1496 return sb_module;
1497}
1498
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001499bool
1500SBTarget::AddModule (lldb::SBModule &module)
1501{
Greg Claytonacdbe812012-01-30 09:04:36 +00001502 TargetSP target_sp(GetSP());
1503 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001504 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001505 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001506 return true;
1507 }
1508 return false;
1509}
1510
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511uint32_t
1512SBTarget::GetNumModules () const
1513{
Greg Clayton5160ce52013-03-27 23:08:40 +00001514 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001515
Caroline Ticeceb6b132010-10-26 03:11:13 +00001516 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001517 TargetSP target_sp(GetSP());
1518 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001519 {
1520 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001521 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001522 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001523
1524 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001525 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1526 static_cast<void*>(target_sp.get()), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001527
1528 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529}
1530
Greg Clayton48e42542010-07-30 20:12:55 +00001531void
1532SBTarget::Clear ()
1533{
Greg Clayton5160ce52013-03-27 23:08:40 +00001534 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001535
1536 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001537 log->Printf ("SBTarget(%p)::Clear ()",
1538 static_cast<void*>(m_opaque_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001539
Greg Clayton48e42542010-07-30 20:12:55 +00001540 m_opaque_sp.reset();
1541}
1542
1543
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544SBModule
1545SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1546{
1547 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001548 TargetSP target_sp(GetSP());
1549 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001550 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001551 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001552 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00001553 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001554 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001555 return sb_module;
1556}
1557
Greg Clayton13d19502012-01-29 06:07:39 +00001558lldb::ByteOrder
1559SBTarget::GetByteOrder ()
1560{
Greg Claytonacdbe812012-01-30 09:04:36 +00001561 TargetSP target_sp(GetSP());
1562 if (target_sp)
1563 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00001564 return eByteOrderInvalid;
1565}
1566
1567const char *
1568SBTarget::GetTriple ()
1569{
Greg Claytonacdbe812012-01-30 09:04:36 +00001570 TargetSP target_sp(GetSP());
1571 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00001572 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001573 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00001574 // Unique the string so we don't run into ownership issues since
1575 // the const strings put the string into the string pool once and
1576 // the strings never comes out
1577 ConstString const_triple (triple.c_str());
1578 return const_triple.GetCString();
1579 }
1580 return NULL;
1581}
1582
1583uint32_t
Matthew Gardinerc928de32014-10-22 07:22:56 +00001584SBTarget::GetDataByteSize ()
1585{
1586 TargetSP target_sp(GetSP());
1587 if (target_sp)
1588 {
1589 return target_sp->GetArchitecture().GetDataByteSize() ;
1590 }
1591 return 0;
1592}
1593
1594uint32_t
1595SBTarget::GetCodeByteSize ()
1596{
1597 TargetSP target_sp(GetSP());
1598 if (target_sp)
1599 {
1600 return target_sp->GetArchitecture().GetCodeByteSize() ;
1601 }
1602 return 0;
1603}
1604
1605uint32_t
Greg Clayton13d19502012-01-29 06:07:39 +00001606SBTarget::GetAddressByteSize()
1607{
Greg Claytonacdbe812012-01-30 09:04:36 +00001608 TargetSP target_sp(GetSP());
1609 if (target_sp)
1610 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00001611 return sizeof(void*);
1612}
1613
1614
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001615SBModule
1616SBTarget::GetModuleAtIndex (uint32_t idx)
1617{
Greg Clayton5160ce52013-03-27 23:08:40 +00001618 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001619
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001620 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001621 ModuleSP module_sp;
1622 TargetSP target_sp(GetSP());
1623 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001624 {
1625 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001626 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1627 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001628 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001629
1630 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001631 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1632 static_cast<void*>(target_sp.get()), idx,
1633 static_cast<void*>(module_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001634
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 return sb_module;
1636}
1637
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001638bool
1639SBTarget::RemoveModule (lldb::SBModule module)
1640{
Greg Claytonacdbe812012-01-30 09:04:36 +00001641 TargetSP target_sp(GetSP());
1642 if (target_sp)
1643 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001644 return false;
1645}
1646
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647
1648SBBroadcaster
1649SBTarget::GetBroadcaster () const
1650{
Greg Clayton5160ce52013-03-27 23:08:40 +00001651 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001652
Greg Claytonacdbe812012-01-30 09:04:36 +00001653 TargetSP target_sp(GetSP());
1654 SBBroadcaster broadcaster(target_sp.get(), false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001655
Caroline Ticeceb6b132010-10-26 03:11:13 +00001656 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001657 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1658 static_cast<void*>(target_sp.get()),
1659 static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001660
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001661 return broadcaster;
1662}
1663
Caroline Ticedde9cff2010-09-20 05:20:02 +00001664bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001665SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00001666{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001667 Stream &strm = description.ref();
1668
Greg Claytonacdbe812012-01-30 09:04:36 +00001669 TargetSP target_sp(GetSP());
1670 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00001671 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001672 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001673 }
1674 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001675 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001676
1677 return true;
1678}
1679
Greg Clayton5569e642012-02-06 01:44:54 +00001680lldb::SBSymbolContextList
1681SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00001682{
Greg Clayton5569e642012-02-06 01:44:54 +00001683 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00001684 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00001685 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001686 TargetSP target_sp(GetSP());
1687 if (target_sp)
1688 {
1689 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00001690 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00001691 const bool append = true;
1692 target_sp->GetImages().FindFunctions (ConstString(name),
1693 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001694 symbols_ok,
1695 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00001696 append,
1697 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00001698 }
Greg Claytonfe356d32011-06-21 01:34:41 +00001699 }
Greg Clayton5569e642012-02-06 01:44:54 +00001700 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00001701}
1702
Carlo Kok0fd6fd42014-09-19 19:38:19 +00001703lldb::SBSymbolContextList
1704SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1705{
1706 lldb::SBSymbolContextList sb_sc_list;
1707 if (name && name[0])
1708 {
1709 TargetSP target_sp(GetSP());
1710 if (target_sp)
1711 {
1712 std::string regexstr;
1713 switch (matchtype)
1714 {
1715 case eMatchTypeRegex:
1716 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1717 break;
1718 case eMatchTypeStartsWith:
1719 regexstr = llvm::Regex::escape(name) + ".*";
1720 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1721 break;
1722 default:
1723 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1724 break;
1725 }
1726 }
1727 }
1728 return sb_sc_list;
1729}
1730
Enrico Granata6f3533f2011-07-29 19:53:35 +00001731lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00001732SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001733{
Greg Claytonacdbe812012-01-30 09:04:36 +00001734 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00001735 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001736 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001737 ConstString const_typename(typename_cstr);
1738 SymbolContext sc;
1739 const bool exact_match = false;
1740
1741 const ModuleList &module_list = target_sp->GetImages();
1742 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001743 for (size_t idx = 0; idx < count; idx++)
1744 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001745 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1746 if (module_sp)
1747 {
1748 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1749 if (type_sp)
1750 return SBType(type_sp);
1751 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001752 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001753
1754 // Didn't find the type in the symbols; try the Objective-C runtime
1755 // if one is installed
1756
1757 ProcessSP process_sp(target_sp->GetProcessSP());
1758
1759 if (process_sp)
1760 {
1761 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1762
1763 if (objc_language_runtime)
1764 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001765 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00001766
Sean Callanan9998acd2014-12-05 01:21:59 +00001767 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00001768 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001769 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00001770
Sean Callanan9998acd2014-12-05 01:21:59 +00001771 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1772 {
1773 if (ClangASTType type = ClangASTContext::GetTypeForDecl(decls[0]))
1774 {
1775 return SBType(type);
1776 }
1777 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001778 }
1779 }
1780 }
Greg Claytonb43165b2012-12-05 21:24:42 +00001781
1782 // No matches, search for basic typename matches
1783 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1784 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001785 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00001786 }
1787 return SBType();
1788}
1789
Greg Claytonb43165b2012-12-05 21:24:42 +00001790SBType
1791SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001792{
Greg Claytonacdbe812012-01-30 09:04:36 +00001793 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00001794 if (target_sp)
1795 {
1796 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1797 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001798 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00001799 }
1800 return SBType();
1801}
1802
1803
1804lldb::SBTypeList
1805SBTarget::FindTypes (const char* typename_cstr)
1806{
1807 SBTypeList sb_type_list;
1808 TargetSP target_sp(GetSP());
1809 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001810 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001811 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00001812 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001813 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001814 SymbolContext sc;
1815 TypeList type_list;
1816
Greg Clayton29399a22012-04-06 17:41:13 +00001817 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00001818 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00001819 exact_match,
1820 UINT32_MAX,
1821 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001822
Greg Claytonb43165b2012-12-05 21:24:42 +00001823 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001824 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001825 for (size_t idx = 0; idx < num_matches; idx++)
1826 {
1827 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1828 if (type_sp)
1829 sb_type_list.Append(SBType(type_sp));
1830 }
1831 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001832
1833 // Try the Objective-C runtime if one is installed
1834
1835 ProcessSP process_sp(target_sp->GetProcessSP());
1836
1837 if (process_sp)
1838 {
1839 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1840
1841 if (objc_language_runtime)
1842 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001843 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00001844
Sean Callanan9998acd2014-12-05 01:21:59 +00001845 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00001846 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001847 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00001848
Sean Callanan9998acd2014-12-05 01:21:59 +00001849 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
Sean Callanan7be70e82012-12-19 23:05:01 +00001850 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001851 for (clang::NamedDecl *decl : decls)
Sean Callanan7be70e82012-12-19 23:05:01 +00001852 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001853 if (ClangASTType type = ClangASTContext::GetTypeForDecl(decl))
1854 {
1855 sb_type_list.Append(SBType(type));
1856 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001857 }
1858 }
1859 }
1860 }
1861 }
1862
1863 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00001864 {
1865 // No matches, search for basic typename matches
1866 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1867 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001868 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00001869 }
1870 }
Greg Claytonb43165b2012-12-05 21:24:42 +00001871 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001872}
1873
Greg Claytondea8cb42011-06-29 22:09:02 +00001874SBValueList
1875SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1876{
1877 SBValueList sb_value_list;
1878
Greg Claytonacdbe812012-01-30 09:04:36 +00001879 TargetSP target_sp(GetSP());
1880 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00001881 {
1882 VariableList variable_list;
1883 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00001884 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
1885 append,
1886 max_matches,
1887 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00001888
1889 if (match_count > 0)
1890 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001891 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00001892 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00001893 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00001894 for (uint32_t i=0; i<match_count; ++i)
1895 {
1896 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1897 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00001898 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00001899 }
1900 }
1901 }
1902
1903 return sb_value_list;
1904}
1905
Carlo Kok0fd6fd42014-09-19 19:38:19 +00001906SBValueList
1907SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
1908{
1909 SBValueList sb_value_list;
1910
1911 TargetSP target_sp(GetSP());
1912 if (name && target_sp)
1913 {
1914 VariableList variable_list;
1915 const bool append = true;
1916
1917 std::string regexstr;
1918 uint32_t match_count;
1919 switch (matchtype)
1920 {
1921 case eMatchTypeNormal:
1922 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
1923 append,
1924 max_matches,
1925 variable_list);
1926 break;
1927 case eMatchTypeRegex:
1928 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
1929 append,
1930 max_matches,
1931 variable_list);
1932 break;
1933 case eMatchTypeStartsWith:
1934 regexstr = llvm::Regex::escape(name) + ".*";
1935 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
1936 append,
1937 max_matches,
1938 variable_list);
1939 break;
1940 }
1941
1942
1943 if (match_count > 0)
1944 {
1945 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1946 if (exe_scope == NULL)
1947 exe_scope = target_sp.get();
1948 for (uint32_t i = 0; i<match_count; ++i)
1949 {
1950 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
1951 if (valobj_sp)
1952 sb_value_list.Append(SBValue(valobj_sp));
1953 }
1954 }
1955 }
1956
1957 return sb_value_list;
1958}
1959
1960
Enrico Granatabcd80b42013-01-16 18:53:52 +00001961lldb::SBValue
1962SBTarget::FindFirstGlobalVariable (const char* name)
1963{
1964 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1965 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1966 return sb_value_list.GetValueAtIndex(0);
1967 return SBValue();
1968}
1969
Jim Inghame37d6052011-09-13 00:29:56 +00001970SBSourceManager
1971SBTarget::GetSourceManager()
1972{
1973 SBSourceManager source_manager (*this);
1974 return source_manager;
1975}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001976
Sean Callanan50952e92011-12-14 23:49:37 +00001977lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00001978SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
1979{
Jim Ingham0f063ba2013-03-02 00:26:47 +00001980 return ReadInstructions (base_addr, count, NULL);
1981}
1982
1983lldb::SBInstructionList
1984SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
1985{
Greg Clayton9c766112012-03-06 22:24:44 +00001986 SBInstructionList sb_instructions;
1987
1988 TargetSP target_sp(GetSP());
1989 if (target_sp)
1990 {
1991 Address *addr_ptr = base_addr.get();
1992
1993 if (addr_ptr)
1994 {
1995 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1996 bool prefer_file_cache = false;
1997 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00001998 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1999 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2000 prefer_file_cache,
2001 data.GetBytes(),
2002 data.GetByteSize(),
2003 error,
2004 &load_addr);
2005 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002006 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2007 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002008 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002009 *addr_ptr,
2010 data.GetBytes(),
2011 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002012 count,
2013 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002014 }
2015 }
2016
2017 return sb_instructions;
2018
2019}
2020
2021lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002022SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2023{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002024 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2025}
2026
2027lldb::SBInstructionList
2028SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2029{
Sean Callanan50952e92011-12-14 23:49:37 +00002030 SBInstructionList sb_instructions;
2031
Greg Claytonacdbe812012-01-30 09:04:36 +00002032 TargetSP target_sp(GetSP());
2033 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002034 {
2035 Address addr;
2036
2037 if (base_addr.get())
2038 addr = *base_addr.get();
2039
Greg Clayton3faf47c2013-03-28 23:42:53 +00002040 const bool data_from_file = true;
2041
Greg Claytonacdbe812012-01-30 09:04:36 +00002042 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002043 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002044 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002045 addr,
2046 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002047 size,
2048 UINT32_MAX,
2049 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002050 }
2051
2052 return sb_instructions;
2053}
2054
2055lldb::SBInstructionList
2056SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2057{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002058 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2059}
2060
2061lldb::SBInstructionList
2062SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2063{
2064 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002065}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002066
2067SBError
2068SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2069 lldb::addr_t section_base_addr)
2070{
2071 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002072 TargetSP target_sp(GetSP());
2073 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002074 {
2075 if (!section.IsValid())
2076 {
2077 sb_error.SetErrorStringWithFormat ("invalid section");
2078 }
2079 else
2080 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002081 SectionSP section_sp (section.GetSP());
2082 if (section_sp)
2083 {
2084 if (section_sp->IsThreadSpecific())
2085 {
2086 sb_error.SetErrorString ("thread specific sections are not yet supported");
2087 }
2088 else
2089 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002090 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002091 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
Greg Clayton3c947372013-01-29 01:17:09 +00002092 {
2093 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002094 if (process_sp)
2095 process_sp->Flush();
2096 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002097 }
2098 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002099 }
2100 }
2101 else
2102 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002103 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002104 }
2105 return sb_error;
2106}
2107
2108SBError
2109SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2110{
2111 SBError sb_error;
2112
Greg Claytonacdbe812012-01-30 09:04:36 +00002113 TargetSP target_sp(GetSP());
2114 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002115 {
2116 if (!section.IsValid())
2117 {
2118 sb_error.SetErrorStringWithFormat ("invalid section");
2119 }
2120 else
2121 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002122 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002123 if (target_sp->SetSectionUnloaded (section.GetSP()))
Greg Clayton3c947372013-01-29 01:17:09 +00002124 {
2125 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002126 if (process_sp)
2127 process_sp->Flush();
2128 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002129 }
2130 }
2131 else
2132 {
2133 sb_error.SetErrorStringWithFormat ("invalid target");
2134 }
2135 return sb_error;
2136}
2137
2138SBError
2139SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2140{
2141 SBError sb_error;
2142
Greg Claytonacdbe812012-01-30 09:04:36 +00002143 TargetSP target_sp(GetSP());
2144 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002145 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002146 ModuleSP module_sp (module.GetSP());
2147 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002148 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002149 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +00002150 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002151 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002152 // The load was successful, make sure that at least some sections
2153 // changed before we notify that our module was loaded.
2154 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002155 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002156 ModuleList module_list;
2157 module_list.Append(module_sp);
2158 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002159 // Flush info in the process (stack frames, etc)
2160 ProcessSP process_sp (target_sp->GetProcessSP());
2161 if (process_sp)
2162 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002163 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002164 }
2165 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002166 else
2167 {
2168 sb_error.SetErrorStringWithFormat ("invalid module");
2169 }
2170
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002171 }
2172 else
2173 {
2174 sb_error.SetErrorStringWithFormat ("invalid target");
2175 }
2176 return sb_error;
2177}
2178
2179SBError
2180SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2181{
2182 SBError sb_error;
2183
2184 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002185 TargetSP target_sp(GetSP());
2186 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002187 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002188 ModuleSP module_sp (module.GetSP());
2189 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002190 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002191 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002192 if (objfile)
2193 {
2194 SectionList *section_list = objfile->GetSectionList();
2195 if (section_list)
2196 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002197 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002198
Greg Clayton3c947372013-01-29 01:17:09 +00002199 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002200 const size_t num_sections = section_list->GetSize();
2201 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2202 {
2203 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2204 if (section_sp)
Zachary Turner40411162014-07-16 20:28:24 +00002205 changed |= target_sp->SetSectionUnloaded (section_sp);
Greg Clayton3c947372013-01-29 01:17:09 +00002206 }
2207 if (changed)
2208 {
2209 // Flush info in the process (stack frames, etc)
2210 ProcessSP process_sp (target_sp->GetProcessSP());
2211 if (process_sp)
2212 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002213 }
2214 }
2215 else
2216 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002217 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002218 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2219 }
2220 }
2221 else
2222 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002223 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002224 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2225 }
2226 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002227 else
2228 {
2229 sb_error.SetErrorStringWithFormat ("invalid module");
2230 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002231 }
2232 else
2233 {
2234 sb_error.SetErrorStringWithFormat ("invalid target");
2235 }
2236 return sb_error;
2237}
2238
2239
Greg Claytone14e1922012-12-04 02:22:16 +00002240lldb::SBSymbolContextList
2241SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2242{
2243 SBSymbolContextList sb_sc_list;
2244 if (name && name[0])
2245 {
2246 TargetSP target_sp(GetSP());
2247 if (target_sp)
2248 {
2249 bool append = true;
2250 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2251 symbol_type,
2252 *sb_sc_list,
2253 append);
2254 }
2255 }
2256 return sb_sc_list;
2257
2258}
2259
2260
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002261lldb::SBValue
2262SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2263{
Greg Clayton5160ce52013-03-27 23:08:40 +00002264 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2265 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002266 SBValue expr_result;
Jim Ingham8646d3c2014-05-05 02:47:44 +00002267 ExpressionResults exe_results = eExpressionSetupError;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002268 ValueObjectSP expr_value_sp;
2269 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002270 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002271 if (target_sp)
2272 {
2273 if (expr == NULL || expr[0] == '\0')
2274 {
2275 if (log)
2276 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2277 return expr_result;
2278 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002279
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002280 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2281 ExecutionContext exe_ctx (m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002282
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002283 if (log)
2284 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002285
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002286 frame = exe_ctx.GetFramePtr();
2287 Target *target = exe_ctx.GetTargetPtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002288
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002289 if (target)
2290 {
2291#ifdef LLDB_CONFIGURATION_DEBUG
2292 StreamString frame_description;
2293 if (frame)
2294 frame->DumpUsingSettingsFormat (&frame_description);
2295 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2296 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2297#endif
2298 exe_results = target->EvaluateExpression (expr,
2299 frame,
2300 expr_value_sp,
2301 options.ref());
2302
2303 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2304#ifdef LLDB_CONFIGURATION_DEBUG
2305 Host::SetCrashDescription (NULL);
2306#endif
2307 }
2308 else
2309 {
2310 if (log)
2311 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2312 }
2313 }
2314#ifndef LLDB_DISABLE_PYTHON
2315 if (expr_log)
2316 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002317 expr_result.GetValue(), expr_result.GetSummary());
2318
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002319 if (log)
2320 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002321 static_cast<void*>(frame), expr,
2322 static_cast<void*>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002323#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002324
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002325 return expr_result;
2326}
2327
Greg Clayton13fbb992013-02-01 00:47:49 +00002328
2329lldb::addr_t
2330SBTarget::GetStackRedZoneSize()
2331{
2332 TargetSP target_sp(GetSP());
2333 if (target_sp)
2334 {
2335 ABISP abi_sp;
2336 ProcessSP process_sp (target_sp->GetProcessSP());
2337 if (process_sp)
2338 abi_sp = process_sp->GetABI();
2339 else
2340 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2341 if (abi_sp)
2342 return abi_sp->GetRedZoneSize();
2343 }
2344 return 0;
2345}
Ilia K8f37ca52015-02-13 14:31:06 +00002346
2347lldb::SBLaunchInfo
2348SBTarget::GetLaunchInfo () const
2349{
2350 lldb::SBLaunchInfo launch_info(NULL);
2351 TargetSP target_sp(GetSP());
2352 if (target_sp)
2353 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2354 return launch_info;
2355}
2356
2357void
2358SBTarget::SetLaunchInfo (const lldb::SBLaunchInfo &launch_info)
2359{
2360 TargetSP target_sp(GetSP());
2361 if (target_sp)
2362 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
2363}