blob: a15b654246df871d1ecf9e7e819b8fed4a75944a [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
Eli Friedman4c5de692010-06-09 07:44:37 +000010#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Greg Claytone0d378b2011-03-24 21:19:54 +000012#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytondea8cb42011-06-29 22:09:02 +000014#include "lldb/API/SBBreakpoint.h"
Ilia Keb2c19a2015-03-10 21:59:55 +000015#include "lldb/API/SBDebugger.h"
16#include "lldb/API/SBEvent.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000017#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000019#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000021#include "lldb/API/SBModuleSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000022#include "lldb/API/SBSourceManager.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000023#include "lldb/API/SBProcess.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000025#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Breakpoint/BreakpointID.h"
27#include "lldb/Breakpoint/BreakpointIDList.h"
28#include "lldb/Breakpoint/BreakpointList.h"
29#include "lldb/Breakpoint/BreakpointLocation.h"
30#include "lldb/Core/Address.h"
31#include "lldb/Core/AddressResolver.h"
32#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/ArchSpec.h"
34#include "lldb/Core/Debugger.h"
35#include "lldb/Core/Disassembler.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000036#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/Module.h"
38#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/RegularExpression.h"
40#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Core/STLUtils.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000043#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000044#include "lldb/Core/ValueObjectList.h"
45#include "lldb/Core/ValueObjectVariable.h"
46#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000047#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000048#include "lldb/Interpreter/Args.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000049#include "lldb/Symbol/ClangASTContext.h"
50#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000051#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000052#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000053#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000054#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000055#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000056#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000057#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000059#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "lldb/Target/Target.h"
61#include "lldb/Target/TargetList.h"
62
63#include "lldb/Interpreter/CommandReturnObject.h"
64#include "../source/Commands/CommandObjectBreakpoint.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000065#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
68using namespace lldb;
69using namespace lldb_private;
70
71#define DEFAULT_DISASM_BYTE_SIZE 32
72
Oleksiy Vyalov37386142015-02-10 22:49:57 +000073namespace {
74
75Error
76AttachToProcess (ProcessAttachInfo &attach_info, Target &target)
77{
78 Mutex::Locker api_locker (target.GetAPIMutex ());
79
80 auto process_sp = target.GetProcessSP ();
81 if (process_sp)
82 {
83 const auto state = process_sp->GetState ();
84 if (process_sp->IsAlive () && state == eStateConnected)
85 {
86 // If we are already connected, then we have already specified the
87 // listener, so if a valid listener is supplied, we need to error out
88 // to let the client know.
89 if (attach_info.GetListener ())
90 return Error ("process is connected and already has a listener, pass empty listener");
91 }
92 }
93
94 return target.Attach (attach_info, nullptr);
95}
96
97} // namespace
98
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099//----------------------------------------------------------------------
100// SBTarget constructor
101//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000102SBTarget::SBTarget () :
103 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104{
105}
106
107SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000108 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109{
110}
111
112SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000113 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114{
115}
116
Greg Claytonefabb122010-11-05 23:17:00 +0000117const SBTarget&
118SBTarget::operator = (const SBTarget& rhs)
119{
120 if (this != &rhs)
121 m_opaque_sp = rhs.m_opaque_sp;
122 return *this;
123}
124
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
128SBTarget::~SBTarget()
129{
130}
131
Ilia Keb2c19a2015-03-10 21:59:55 +0000132bool
133SBTarget::EventIsTargetEvent (const SBEvent &event)
134{
135 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
136}
137
138SBTarget
139SBTarget::GetTargetFromEvent (const SBEvent &event)
140{
141 return Target::TargetEventData::GetTargetFromEvent (event.get());
142}
143
144uint32_t
145SBTarget::GetNumModulesFromEvent (const SBEvent &event)
146{
147 const ModuleList module_list = Target::TargetEventData::GetModuleListFromEvent (event.get());
148 return module_list.GetSize();
149}
150
151SBModule
152SBTarget::GetModuleAtIndexFromEvent (const uint32_t idx, const SBEvent &event)
153{
154 const ModuleList module_list = Target::TargetEventData::GetModuleListFromEvent (event.get());
155 return SBModule(module_list.GetModuleAtIndex(idx));
156}
157
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000158const char *
159SBTarget::GetBroadcasterClassName ()
160{
161 return Target::GetStaticBroadcasterClass().AsCString();
162}
163
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164bool
165SBTarget::IsValid () const
166{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000167 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
170SBProcess
171SBTarget::GetProcess ()
172{
173 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000174 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000175 TargetSP target_sp(GetSP());
176 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000177 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000178 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000179 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000180 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000181
Greg Clayton5160ce52013-03-27 23:08:40 +0000182 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000183 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000184 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
185 static_cast<void*>(target_sp.get()),
186 static_cast<void*>(process_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000187
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 return sb_process;
189}
190
Matthew Gardinerc928de32014-10-22 07:22:56 +0000191SBPlatform
192SBTarget::GetPlatform ()
193{
194 TargetSP target_sp(GetSP());
195 if (!target_sp)
196 return SBPlatform();
197
198 SBPlatform platform;
199 platform.m_opaque_sp = target_sp->GetPlatform();
200
201 return platform;
202}
203
Greg Clayton66111032010-06-23 01:19:29 +0000204SBDebugger
205SBTarget::GetDebugger () const
206{
207 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000208 TargetSP target_sp(GetSP());
209 if (target_sp)
210 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000211 return debugger;
212}
213
Jim Ingham270684d2011-03-31 00:01:24 +0000214SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000215SBTarget::LoadCore (const char *core_file)
216{
217 SBProcess sb_process;
218 TargetSP target_sp(GetSP());
219 if (target_sp)
220 {
221 FileSpec filespec(core_file, true);
222 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
223 NULL,
224 &filespec));
225 if (process_sp)
226 {
227 process_sp->LoadCore();
228 sb_process.SetSP (process_sp);
229 }
230 }
231 return sb_process;
232}
233
234SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000235SBTarget::LaunchSimple
236(
237 char const **argv,
238 char const **envp,
239 const char *working_directory
240)
241{
242 char *stdin_path = NULL;
243 char *stdout_path = NULL;
244 char *stderr_path = NULL;
245 uint32_t launch_flags = 0;
246 bool stop_at_entry = false;
247 SBError error;
248 SBListener listener = GetDebugger().GetListener();
249 return Launch (listener,
250 argv,
251 envp,
252 stdin_path,
253 stdout_path,
254 stderr_path,
255 working_directory,
256 launch_flags,
257 stop_at_entry,
258 error);
259}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000260
Greg Claytonfbb76342013-11-20 21:07:01 +0000261SBError
262SBTarget::Install()
263{
264 SBError sb_error;
265 TargetSP target_sp(GetSP());
266 if (target_sp)
267 {
268 Mutex::Locker api_locker (target_sp->GetAPIMutex());
269 sb_error.ref() = target_sp->Install(NULL);
270 }
271 return sb_error;
272}
273
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000274SBProcess
275SBTarget::Launch
276(
Greg Clayton4b045622011-02-03 21:28:34 +0000277 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000278 char const **argv,
279 char const **envp,
280 const char *stdin_path,
281 const char *stdout_path,
282 const char *stderr_path,
283 const char *working_directory,
284 uint32_t launch_flags, // See LaunchFlags
285 bool stop_at_entry,
286 lldb::SBError& error
287)
288{
Greg Clayton5160ce52013-03-27 23:08:40 +0000289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000290
Greg Claytonacdbe812012-01-30 09:04:36 +0000291 SBProcess sb_process;
292 ProcessSP process_sp;
293 TargetSP target_sp(GetSP());
294
Caroline Ticeceb6b132010-10-26 03:11:13 +0000295 if (log)
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000296 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 +0000297 static_cast<void*>(target_sp.get()),
298 static_cast<void*>(argv), static_cast<void*>(envp),
299 stdin_path ? stdin_path : "NULL",
300 stdout_path ? stdout_path : "NULL",
301 stderr_path ? stderr_path : "NULL",
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000302 working_directory ? working_directory : "NULL",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000303 launch_flags, stop_at_entry,
304 static_cast<void*>(error.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000305
306 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000308 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000309
Zachary Turner6fd3f342015-02-02 18:50:01 +0000310 if (stop_at_entry)
311 launch_flags |= eLaunchFlagStopAtEntry;
312
Greg Clayton645bf542011-01-27 01:01:10 +0000313 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
314 launch_flags |= eLaunchFlagDisableASLR;
315
Greg Clayton2289fa42011-04-30 01:09:13 +0000316 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000317 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000318 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000319 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000320 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000321
Greg Claytonb9556ac2012-01-30 07:41:31 +0000322 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000323 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000324 if (state == eStateAttaching)
325 error.SetErrorString ("process attach is in progress");
326 else
327 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000328 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329 }
Greg Clayton931180e2011-01-27 06:44:37 +0000330 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000331
Greg Clayton2289fa42011-04-30 01:09:13 +0000332 if (state == eStateConnected)
333 {
334 // If we are already connected, then we have already specified the
335 // listener, so if a valid listener is supplied, we need to error out
336 // to let the client know.
337 if (listener.IsValid())
338 {
339 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000340 return sb_process;
341 }
342 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000343
344 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
345 launch_flags |= eLaunchFlagDisableSTDIO;
346
Chaoren Lind3173f32015-05-29 19:52:29 +0000347 ProcessLaunchInfo launch_info(FileSpec{stdin_path, false},
348 FileSpec{stdout_path, false},
349 FileSpec{stderr_path, false},
350 FileSpec{working_directory, false},
351 launch_flags);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000352
Greg Claytonb09c5382013-12-13 17:20:18 +0000353 Module *exe_module = target_sp->GetExecutableModulePointer();
354 if (exe_module)
355 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
356 if (argv)
357 launch_info.GetArguments().AppendArguments (argv);
358 if (envp)
359 launch_info.GetEnvironmentEntries ().SetArguments (envp);
360
361 if (listener.IsValid())
Greg Clayton8012cad2014-11-17 19:39:20 +0000362 launch_info.SetListener(listener.GetSP());
363
364 error.SetError (target_sp->Launch(launch_info, NULL));
Greg Clayton645bf542011-01-27 01:01:10 +0000365
Greg Claytonb09c5382013-12-13 17:20:18 +0000366 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton524e60b2010-10-06 22:10:17 +0000367 }
368 else
369 {
370 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000372
Caroline Tice20ad3c42010-10-29 21:48:37 +0000373 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000374 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000375 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
376 static_cast<void*>(target_sp.get()),
377 static_cast<void*>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000378
Greg Clayton5d5028b2010-10-06 03:53:16 +0000379 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380}
381
Greg Clayton0e615682012-02-24 05:03:03 +0000382SBProcess
383SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
384{
Greg Clayton5160ce52013-03-27 23:08:40 +0000385 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000386
Greg Clayton0e615682012-02-24 05:03:03 +0000387 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000388 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000389
Greg Clayton0e615682012-02-24 05:03:03 +0000390 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000391 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
392 static_cast<void*>(target_sp.get()));
393
Greg Clayton0e615682012-02-24 05:03:03 +0000394 if (target_sp)
395 {
396 Mutex::Locker api_locker (target_sp->GetAPIMutex());
397 StateType state = eStateInvalid;
Greg Claytonb09c5382013-12-13 17:20:18 +0000398 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000399 ProcessSP process_sp = target_sp->GetProcessSP();
400 if (process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000401 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000402 state = process_sp->GetState();
403
404 if (process_sp->IsAlive() && state != eStateConnected)
405 {
406 if (state == eStateAttaching)
407 error.SetErrorString ("process attach is in progress");
408 else
409 error.SetErrorString ("a process is already being debugged");
410 return sb_process;
411 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000412 }
Greg Clayton0e615682012-02-24 05:03:03 +0000413 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000414
415 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
416
Greg Clayton3e32ad62014-05-07 20:16:06 +0000417 if (!launch_info.GetExecutableFile())
418 {
419 Module *exe_module = target_sp->GetExecutableModulePointer();
420 if (exe_module)
421 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
422 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000423
424 const ArchSpec &arch_spec = target_sp->GetArchitecture();
425 if (arch_spec.IsValid())
426 launch_info.GetArchitecture () = arch_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000427
Greg Clayton8012cad2014-11-17 19:39:20 +0000428 error.SetError (target_sp->Launch (launch_info, NULL));
Greg Claytonb09c5382013-12-13 17:20:18 +0000429 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000430 }
431 else
432 {
433 error.SetErrorString ("SBTarget is invalid");
434 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435
Greg Clayton0e615682012-02-24 05:03:03 +0000436 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
437 if (log)
Greg Claytonb09c5382013-12-13 17:20:18 +0000438 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000439 static_cast<void*>(target_sp.get()),
440 static_cast<void*>(sb_process.GetSP().get()));
441
Greg Clayton0e615682012-02-24 05:03:03 +0000442 return sb_process;
443}
444
445lldb::SBProcess
446SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
447{
Greg Clayton5160ce52013-03-27 23:08:40 +0000448 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000449
Greg Clayton0e615682012-02-24 05:03:03 +0000450 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000451 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000452
Sean Callanan575a4542012-10-20 00:21:31 +0000453 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000454 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
455 static_cast<void*>(target_sp.get()));
456
Greg Clayton0e615682012-02-24 05:03:03 +0000457 if (target_sp)
458 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000459 ProcessAttachInfo &attach_info = sb_attach_info.ref();
460 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Greg Clayton0e615682012-02-24 05:03:03 +0000461 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000462 PlatformSP platform_sp = target_sp->GetPlatform();
463 // See if we can pre-verify if a process exists or not
464 if (platform_sp && platform_sp->IsConnected())
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000465 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000466 lldb::pid_t attach_pid = attach_info.GetProcessID();
467 ProcessInstanceInfo instance_info;
468 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
Han Ming Ongcec8c902012-02-29 00:12:56 +0000469 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000470 attach_info.SetUserID(instance_info.GetEffectiveUserID());
471 }
472 else
473 {
474 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
475 if (log)
Greg Clayton91e407e2012-09-27 00:03:39 +0000476 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000477 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
478 static_cast<void*>(target_sp.get()), error.GetCString());
Greg Clayton91e407e2012-09-27 00:03:39 +0000479 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000480 return sb_process;
Han Ming Ongcec8c902012-02-29 00:12:56 +0000481 }
482 }
Greg Clayton0e615682012-02-24 05:03:03 +0000483 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000484 error.SetError(AttachToProcess(attach_info, *target_sp));
485 if (error.Success())
486 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000487 }
488 else
489 {
490 error.SetErrorString ("SBTarget is invalid");
491 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000492
Sean Callanan575a4542012-10-20 00:21:31 +0000493 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000494 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000495 static_cast<void*>(target_sp.get()),
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000496 static_cast<void*>(sb_process.GetSP().get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000497
Greg Clayton0e615682012-02-24 05:03:03 +0000498 return sb_process;
499}
500
501
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000502#if defined(__APPLE__)
503
504lldb::SBProcess
505SBTarget::AttachToProcessWithID (SBListener &listener,
506 ::pid_t pid,
507 lldb::SBError& error)
508{
509 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
510}
511
512#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000513
514lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000515SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000516(
Greg Clayton4b045622011-02-03 21:28:34 +0000517 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000518 lldb::pid_t pid,// The process ID to attach to
519 SBError& error // An error explaining what went wrong if attach fails
520)
521{
Greg Clayton5160ce52013-03-27 23:08:40 +0000522 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000523
Greg Clayton524e60b2010-10-06 22:10:17 +0000524 SBProcess sb_process;
Greg Claytonacdbe812012-01-30 09:04:36 +0000525 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000526
527 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000528 log->Printf ("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
529 static_cast<void*>(target_sp.get()),
530 __FUNCTION__,
531 pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000532
Greg Claytonacdbe812012-01-30 09:04:36 +0000533 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000534 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000535 ProcessAttachInfo attach_info;
536 attach_info.SetProcessID (pid);
537 if (listener.IsValid())
538 attach_info.SetListener(listener.GetSP());
Greg Clayton0c74e782011-06-24 03:21:43 +0000539
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000540 ProcessInstanceInfo instance_info;
541 if (target_sp->GetPlatform ()->GetProcessInfo (pid, instance_info))
542 attach_info.SetUserID (instance_info.GetEffectiveUserID ());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000543
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000544 error.SetError (AttachToProcess (attach_info, *target_sp));
545 if (error.Success ())
546 sb_process.SetSP (target_sp->GetProcessSP ());
Greg Clayton524e60b2010-10-06 22:10:17 +0000547 }
548 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000549 error.SetErrorString ("SBTarget is invalid");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000550
Sean Callanan575a4542012-10-20 00:21:31 +0000551 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000552 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
553 static_cast<void*>(target_sp.get ()),
554 __FUNCTION__,
555 static_cast<void*>(sb_process.GetSP().get ()));
Greg Clayton524e60b2010-10-06 22:10:17 +0000556 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +0000557}
558
559lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000560SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +0000561(
Greg Clayton4b045622011-02-03 21:28:34 +0000562 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000563 const char *name, // basename of process to attach to
564 bool wait_for, // if true wait for a new instance of "name" to be launched
565 SBError& error // An error explaining what went wrong if attach fails
566)
567{
Greg Clayton5160ce52013-03-27 23:08:40 +0000568 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000569
Greg Clayton524e60b2010-10-06 22:10:17 +0000570 SBProcess sb_process;
Greg Claytonacdbe812012-01-30 09:04:36 +0000571 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000572
Sean Callanan575a4542012-10-20 00:21:31 +0000573 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000574 log->Printf ("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
575 static_cast<void*>(target_sp.get()),
576 __FUNCTION__,
577 name,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000578 wait_for ? "true" : "false");
579
Greg Claytonacdbe812012-01-30 09:04:36 +0000580 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000581 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000582 ProcessAttachInfo attach_info;
583 attach_info.GetExecutableFile().SetFile(name, false);
584 attach_info.SetWaitForLaunch(wait_for);
585 if (listener.IsValid())
586 attach_info.SetListener(listener.GetSP());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000587
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000588 error.SetError (AttachToProcess (attach_info, *target_sp));
589 if (error.Success ())
590 sb_process.SetSP (target_sp->GetProcessSP ());
Greg Clayton524e60b2010-10-06 22:10:17 +0000591 }
592 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000593 error.SetErrorString ("SBTarget is invalid");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000594
Sean Callanan575a4542012-10-20 00:21:31 +0000595 if (log)
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000596 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000597 static_cast<void*>(target_sp.get()),
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000598 __FUNCTION__,
599 static_cast<void*>(sb_process.GetSP().get()));
Greg Clayton524e60b2010-10-06 22:10:17 +0000600 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +0000601}
602
James McIlree9631aae2011-03-04 00:31:13 +0000603lldb::SBProcess
604SBTarget::ConnectRemote
605(
606 SBListener &listener,
607 const char *url,
608 const char *plugin_name,
609 SBError& error
610)
611{
Greg Clayton5160ce52013-03-27 23:08:40 +0000612 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000613
James McIlree9631aae2011-03-04 00:31:13 +0000614 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000615 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000616 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000617
Sean Callanan575a4542012-10-20 00:21:31 +0000618 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000619 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
620 static_cast<void*>(target_sp.get()), url, plugin_name);
621
Greg Claytonacdbe812012-01-30 09:04:36 +0000622 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +0000623 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000624 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +0000625 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000626 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000627 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000628 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000629
Greg Claytonb9556ac2012-01-30 07:41:31 +0000630 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +0000631 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000632 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000633 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +0000634 }
635 else
636 {
637 error.SetErrorString ("unable to create lldb_private::Process");
638 }
639 }
640 else
641 {
642 error.SetErrorString ("SBTarget is invalid");
643 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000644
Sean Callanan575a4542012-10-20 00:21:31 +0000645 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000646 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000647 static_cast<void*>(target_sp.get()),
648 static_cast<void*>(process_sp.get()));
James McIlree9631aae2011-03-04 00:31:13 +0000649 return sb_process;
650}
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652SBFileSpec
653SBTarget::GetExecutable ()
654{
Caroline Ticeceb6b132010-10-26 03:11:13 +0000655
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +0000657 TargetSP target_sp(GetSP());
658 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000660 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +0000661 if (exe_module)
662 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000664
Greg Clayton5160ce52013-03-27 23:08:40 +0000665 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000666 if (log)
667 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000668 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
669 static_cast<void*>(target_sp.get()),
670 static_cast<const void*>(exe_file_spec.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000671 }
672
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 return exe_file_spec;
674}
675
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677SBTarget::operator == (const SBTarget &rhs) const
678{
Greg Clayton66111032010-06-23 01:19:29 +0000679 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680}
681
682bool
683SBTarget::operator != (const SBTarget &rhs) const
684{
Greg Clayton66111032010-06-23 01:19:29 +0000685 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686}
687
Greg Claytonb9556ac2012-01-30 07:41:31 +0000688lldb::TargetSP
689SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +0000690{
691 return m_opaque_sp;
692}
693
Greg Clayton66111032010-06-23 01:19:29 +0000694void
Greg Claytonb9556ac2012-01-30 07:41:31 +0000695SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +0000696{
697 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698}
699
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000700lldb::SBAddress
701SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000702{
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000703 lldb::SBAddress sb_addr;
704 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +0000705 TargetSP target_sp(GetSP());
706 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000707 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000708 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytond5944cd2013-12-06 01:12:00 +0000709 if (target_sp->ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000710 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +0000711 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000712
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000713 // We have a load address that isn't in a section, just return an address
714 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +0000715 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000716 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000717}
718
Matthew Gardinerc928de32014-10-22 07:22:56 +0000719lldb::SBAddress
720SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
721{
722 lldb::SBAddress sb_addr;
723 Address &addr = sb_addr.ref();
724 TargetSP target_sp(GetSP());
725 if (target_sp)
726 {
727 Mutex::Locker api_locker (target_sp->GetAPIMutex());
728 if (target_sp->ResolveFileAddress (file_addr, addr))
729 return sb_addr;
730 }
731
732 addr.SetRawAddress(file_addr);
733 return sb_addr;
734}
Greg Claytond5944cd2013-12-06 01:12:00 +0000735
736lldb::SBAddress
737SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
738{
739 lldb::SBAddress sb_addr;
740 Address &addr = sb_addr.ref();
741 TargetSP target_sp(GetSP());
742 if (target_sp)
743 {
744 Mutex::Locker api_locker (target_sp->GetAPIMutex());
745 if (target_sp->ResolveLoadAddress (vm_addr, addr))
746 return sb_addr;
747 }
748
749 // We have a load address that isn't in a section, just return an address
750 // with the offset filled in (the address) and the section set to NULL
751 addr.SetRawAddress(vm_addr);
752 return sb_addr;
753}
754
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000755SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +0000756SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
757 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000758{
759 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +0000760 if (addr.IsValid())
761 {
762 TargetSP target_sp(GetSP());
763 if (target_sp)
764 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
765 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000766 return sc;
767}
768
Matthew Gardinerc928de32014-10-22 07:22:56 +0000769size_t
770SBTarget::ReadMemory (const SBAddress addr,
771 void *buf,
772 size_t size,
773 lldb::SBError &error)
774{
775 SBError sb_error;
776 size_t bytes_read = 0;
777 TargetSP target_sp(GetSP());
778 if (target_sp)
779 {
780 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton8691dc52014-11-04 00:56:30 +0000781 bytes_read = target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
782 }
783 else
784 {
785 sb_error.SetErrorString("invalid target");
Matthew Gardinerc928de32014-10-22 07:22:56 +0000786 }
787
788 return bytes_read;
789}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000790
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000792SBTarget::BreakpointCreateByLocation (const char *file,
793 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794{
Greg Clayton7481c202010-11-08 00:28:40 +0000795 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796}
797
798SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000799SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
800 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801{
Greg Clayton5160ce52013-03-27 23:08:40 +0000802 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000803
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000805 TargetSP target_sp(GetSP());
806 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000807 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000808 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000809
Greg Clayton1f746072012-08-29 21:13:06 +0000810 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +0000811 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +0000812 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000813 const bool hardware = false;
Ilia K055ad9b2015-05-18 13:41:01 +0000814 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
815 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware, move_to_nearest_code);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000816 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000817
818 if (log)
819 {
820 SBStream sstr;
821 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000822 char path[PATH_MAX];
823 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000824 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
825 static_cast<void*>(target_sp.get()), path, line,
826 static_cast<void*>(sb_bp.get()), sstr.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000827 }
828
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 return sb_bp;
830}
831
832SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000833SBTarget::BreakpointCreateByName (const char *symbol_name,
834 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835{
Greg Clayton5160ce52013-03-27 23:08:40 +0000836 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000837
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000839 TargetSP target_sp(GetSP());
840 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000842 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000843
Jim Inghama8558b62012-05-22 00:12:20 +0000844 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000845 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000846 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 if (module_name && module_name[0])
848 {
Jim Ingham969795f2011-09-21 01:17:13 +0000849 FileSpecList module_spec_list;
850 module_spec_list.Append (FileSpec (module_name, false));
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000851 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 }
853 else
854 {
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000855 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 }
857 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000858
Caroline Ticeceb6b132010-10-26 03:11:13 +0000859 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000860 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
861 static_cast<void*>(target_sp.get()), symbol_name,
862 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000863
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 return sb_bp;
865}
866
Jim Ingham87df91b2011-09-23 00:54:11 +0000867lldb::SBBreakpoint
868SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +0000869 const SBFileSpecList &module_list,
870 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +0000871{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +0000872 uint32_t name_type_mask = eFunctionNameTypeAuto;
873 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
874}
875
876lldb::SBBreakpoint
877SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +0000878 uint32_t name_type_mask,
879 const SBFileSpecList &module_list,
880 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +0000881{
Greg Clayton5160ce52013-03-27 23:08:40 +0000882 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +0000883
884 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000885 TargetSP target_sp(GetSP());
886 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +0000887 {
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;
Greg Claytonacdbe812012-01-30 09:04:36 +0000891 Mutex::Locker api_locker (target_sp->GetAPIMutex());
892 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +0000893 comp_unit_list.get(),
894 symbol_name,
895 name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000896 eLanguageTypeUnknown,
Greg Claytoneb023e72013-10-11 19:48:25 +0000897 skip_prologue,
898 internal,
899 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +0000900 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000901
Jim Ingham87df91b2011-09-23 00:54:11 +0000902 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000903 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
904 static_cast<void*>(target_sp.get()), symbol_name,
905 name_type_mask, static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +0000906
907 return sb_bp;
908}
909
Jim Inghamfab10e82012-03-06 00:37:27 +0000910lldb::SBBreakpoint
911SBTarget::BreakpointCreateByNames (const char *symbol_names[],
912 uint32_t num_names,
913 uint32_t name_type_mask,
914 const SBFileSpecList &module_list,
915 const SBFileSpecList &comp_unit_list)
916{
Greg Clayton5160ce52013-03-27 23:08:40 +0000917 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +0000918
919 SBBreakpoint sb_bp;
920 TargetSP target_sp(GetSP());
921 if (target_sp && num_names > 0)
922 {
923 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +0000924 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000925 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000926 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +0000927 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
928 comp_unit_list.get(),
929 symbol_names,
930 num_names,
931 name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000932 eLanguageTypeUnknown,
Jim Inghama8558b62012-05-22 00:12:20 +0000933 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000934 internal,
935 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +0000936 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000937
Jim Inghamfab10e82012-03-06 00:37:27 +0000938 if (log)
939 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000940 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
941 static_cast<void*>(target_sp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +0000942 for (uint32_t i = 0 ; i < num_names; i++)
943 {
944 char sep;
945 if (i < num_names - 1)
946 sep = ',';
947 else
948 sep = '}';
949 if (symbol_names[i] != NULL)
950 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
951 else
952 log->Printf ("\"<NULL>\"%c ", sep);
Jim Inghamfab10e82012-03-06 00:37:27 +0000953 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000954 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
955 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +0000956 }
957
958 return sb_bp;
959}
Jim Ingham87df91b2011-09-23 00:54:11 +0000960
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +0000962SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
963 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964{
Greg Clayton5160ce52013-03-27 23:08:40 +0000965 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000966
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000968 TargetSP target_sp(GetSP());
969 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000971 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +0000973 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +0000974 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +0000975 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000976
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 if (module_name && module_name[0])
978 {
Jim Ingham969795f2011-09-21 01:17:13 +0000979 FileSpecList module_spec_list;
980 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000981
Greg Claytoneb023e72013-10-11 19:48:25 +0000982 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983 }
984 else
985 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000986 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987 }
988 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000989
990 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000991 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
992 static_cast<void*>(target_sp.get()), symbol_name_regex,
993 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000994
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995 return sb_bp;
996}
997
Jim Ingham87df91b2011-09-23 00:54:11 +0000998lldb::SBBreakpoint
999SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001000 const SBFileSpecList &module_list,
1001 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001002{
Greg Clayton5160ce52013-03-27 23:08:40 +00001003 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004
Jim Ingham87df91b2011-09-23 00:54:11 +00001005 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001006 TargetSP target_sp(GetSP());
1007 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001008 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001009 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001010 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001011 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001012 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001013 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001014
Greg Claytoneb023e72013-10-11 19:48:25 +00001015 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001016 }
1017
1018 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001019 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1020 static_cast<void*>(target_sp.get()), symbol_name_regex,
1021 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001022
1023 return sb_bp;
1024}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025
1026SBBreakpoint
1027SBTarget::BreakpointCreateByAddress (addr_t address)
1028{
Greg Clayton5160ce52013-03-27 23:08:40 +00001029 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001030
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001032 TargetSP target_sp(GetSP());
1033 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001034 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001035 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001036 const bool hardware = false;
1037 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001038 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001039
Caroline Ticeceb6b132010-10-26 03:11:13 +00001040 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001041 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1042 static_cast<void*>(target_sp.get()),
1043 static_cast<uint64_t>(address),
1044 static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001045
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 return sb_bp;
1047}
1048
Jim Ingham969795f2011-09-21 01:17:13 +00001049lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001050SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1051 const lldb::SBFileSpec &source_file,
1052 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001053{
Greg Clayton5160ce52013-03-27 23:08:40 +00001054 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001055
1056 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001057 TargetSP target_sp(GetSP());
1058 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001059 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001060 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001061 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001062 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001063 const bool hardware = false;
Ilia K055ad9b2015-05-18 13:41:01 +00001064 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001065 source_file_spec_list.Append (source_file.ref());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001066
Jim Ingham969795f2011-09-21 01:17:13 +00001067 if (module_name && module_name[0])
1068 {
1069 FileSpecList module_spec_list;
1070 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001071
Ilia K055ad9b2015-05-18 13:41:01 +00001072 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware, move_to_nearest_code);
Jim Ingham969795f2011-09-21 01:17:13 +00001073 }
1074 else
1075 {
Ilia K055ad9b2015-05-18 13:41:01 +00001076 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware, move_to_nearest_code);
Jim Ingham969795f2011-09-21 01:17:13 +00001077 }
1078 }
1079
1080 if (log)
1081 {
1082 char path[PATH_MAX];
1083 source_file->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001084 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1085 static_cast<void*>(target_sp.get()), source_regex, path,
1086 module_name, static_cast<void*>(sb_bp.get()));
Jim Ingham969795f2011-09-21 01:17:13 +00001087 }
1088
1089 return sb_bp;
1090}
1091
Jim Ingham87df91b2011-09-23 00:54:11 +00001092lldb::SBBreakpoint
Jim Inghame7320522015-02-12 17:37:46 +00001093SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1094 const SBFileSpecList &module_list,
1095 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001096{
Greg Clayton5160ce52013-03-27 23:08:40 +00001097 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001098
1099 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001100 TargetSP target_sp(GetSP());
1101 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001102 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001103 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001104 const bool hardware = false;
Ilia K055ad9b2015-05-18 13:41:01 +00001105 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001106 RegularExpression regexp(source_regex);
Ilia K055ad9b2015-05-18 13:41:01 +00001107 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware, move_to_nearest_code);
Jim Ingham87df91b2011-09-23 00:54:11 +00001108 }
1109
1110 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001111 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1112 static_cast<void*>(target_sp.get()), source_regex,
1113 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001114
1115 return sb_bp;
1116}
Jim Ingham969795f2011-09-21 01:17:13 +00001117
Jim Inghamfab10e82012-03-06 00:37:27 +00001118lldb::SBBreakpoint
1119SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001120 bool catch_bp,
1121 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001122{
Greg Clayton5160ce52013-03-27 23:08:40 +00001123 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001124
1125 SBBreakpoint sb_bp;
1126 TargetSP target_sp(GetSP());
1127 if (target_sp)
1128 {
1129 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001130 const bool hardware = false;
1131 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001132 }
1133
1134 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001135 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1136 static_cast<void*>(target_sp.get()),
Jim Ingham0e0984e2015-09-02 01:06:46 +00001137 Language::GetNameForLanguageType(language),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001138 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1139 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001140
1141 return sb_bp;
1142}
1143
Greg Clayton9fed0d82010-07-23 23:33:17 +00001144uint32_t
1145SBTarget::GetNumBreakpoints () const
1146{
Greg Claytonacdbe812012-01-30 09:04:36 +00001147 TargetSP target_sp(GetSP());
1148 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001149 {
1150 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001151 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001152 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001153 return 0;
1154}
1155
1156SBBreakpoint
1157SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1158{
1159 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001160 TargetSP target_sp(GetSP());
1161 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001162 {
1163 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001164 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001165 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001166 return sb_breakpoint;
1167}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168
1169bool
1170SBTarget::BreakpointDelete (break_id_t bp_id)
1171{
Greg Clayton5160ce52013-03-27 23:08:40 +00001172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001173
Caroline Ticeceb6b132010-10-26 03:11:13 +00001174 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001175 TargetSP target_sp(GetSP());
1176 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001177 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001178 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1179 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001180 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001181
1182 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001183 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1184 static_cast<void*>(target_sp.get()),
1185 static_cast<uint32_t>(bp_id), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001186
1187 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188}
1189
Johnny Chen5d043462011-09-26 22:40:50 +00001190SBBreakpoint
1191SBTarget::FindBreakpointByID (break_id_t bp_id)
1192{
Greg Clayton5160ce52013-03-27 23:08:40 +00001193 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001194
1195 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001196 TargetSP target_sp(GetSP());
1197 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001198 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001199 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1200 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001201 }
1202
1203 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001204 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1205 static_cast<void*>(target_sp.get()),
1206 static_cast<uint32_t>(bp_id),
1207 static_cast<void*>(sb_breakpoint.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001208
1209 return sb_breakpoint;
1210}
1211
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212bool
1213SBTarget::EnableAllBreakpoints ()
1214{
Greg Claytonacdbe812012-01-30 09:04:36 +00001215 TargetSP target_sp(GetSP());
1216 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001218 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1219 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 return true;
1221 }
1222 return false;
1223}
1224
1225bool
1226SBTarget::DisableAllBreakpoints ()
1227{
Greg Claytonacdbe812012-01-30 09:04:36 +00001228 TargetSP target_sp(GetSP());
1229 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001231 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1232 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 return true;
1234 }
1235 return false;
1236}
1237
1238bool
1239SBTarget::DeleteAllBreakpoints ()
1240{
Greg Claytonacdbe812012-01-30 09:04:36 +00001241 TargetSP target_sp(GetSP());
1242 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001244 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1245 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246 return true;
1247 }
1248 return false;
1249}
1250
Johnny Chen5d043462011-09-26 22:40:50 +00001251uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001252SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001253{
Greg Claytonacdbe812012-01-30 09:04:36 +00001254 TargetSP target_sp(GetSP());
1255 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001256 {
Johnny Chen01a67862011-10-14 00:42:25 +00001257 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001258 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001259 }
1260 return 0;
1261}
1262
Greg Clayton1b282f92011-10-13 18:08:26 +00001263SBWatchpoint
1264SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001265{
Johnny Chen01a67862011-10-14 00:42:25 +00001266 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001267 TargetSP target_sp(GetSP());
1268 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001269 {
Johnny Chen01a67862011-10-14 00:42:25 +00001270 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001271 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001272 }
Johnny Chen01a67862011-10-14 00:42:25 +00001273 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001274}
1275
1276bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001277SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001278{
Greg Clayton5160ce52013-03-27 23:08:40 +00001279 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001280
1281 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001282 TargetSP target_sp(GetSP());
1283 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001284 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001285 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001286 Mutex::Locker locker;
1287 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001288 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001289 }
1290
1291 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001292 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1293 static_cast<void*>(target_sp.get()),
1294 static_cast<uint32_t>(wp_id), result);
Johnny Chen5d043462011-09-26 22:40:50 +00001295
1296 return result;
1297}
1298
Greg Clayton1b282f92011-10-13 18:08:26 +00001299SBWatchpoint
1300SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001301{
Greg Clayton5160ce52013-03-27 23:08:40 +00001302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001303
Greg Clayton1b282f92011-10-13 18:08:26 +00001304 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001305 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001306 TargetSP target_sp(GetSP());
1307 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001308 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001309 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001310 Mutex::Locker locker;
1311 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001312 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1313 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001314 }
1315
1316 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001317 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1318 static_cast<void*>(target_sp.get()),
1319 static_cast<uint32_t>(wp_id),
1320 static_cast<void*>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001321
Greg Clayton1b282f92011-10-13 18:08:26 +00001322 return sb_watchpoint;
1323}
1324
1325lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001326SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001327{
Greg Clayton5160ce52013-03-27 23:08:40 +00001328 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001329
Greg Clayton1b282f92011-10-13 18:08:26 +00001330 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001331 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001332 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001333 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001334 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001335 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001336 uint32_t watch_type = 0;
1337 if (read)
1338 watch_type |= LLDB_WATCH_TYPE_READ;
1339 if (write)
1340 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001341 if (watch_type == 0)
1342 {
1343 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1344 return sb_watchpoint;
1345 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001346
Johnny Chen7385a5a2012-05-31 22:56:36 +00001347 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001348 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001349 // This API doesn't take in a type, so we can't figure out what it is.
Greg Claytona1e5dc82015-08-11 22:53:00 +00001350 CompilerType *type = NULL;
Jim Inghama7dfb662012-10-23 07:20:06 +00001351 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001352 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001353 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001354 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001355
Greg Clayton1b282f92011-10-13 18:08:26 +00001356 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001357 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001358 static_cast<void*>(target_sp.get()), addr,
1359 static_cast<uint32_t>(size),
1360 static_cast<void*>(watchpoint_sp.get()));
1361
Greg Clayton1b282f92011-10-13 18:08:26 +00001362 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001363}
1364
1365bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001366SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001367{
Greg Claytonacdbe812012-01-30 09:04:36 +00001368 TargetSP target_sp(GetSP());
1369 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001370 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001371 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001372 Mutex::Locker locker;
1373 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001374 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001375 return true;
1376 }
1377 return false;
1378}
1379
1380bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001381SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001382{
Greg Claytonacdbe812012-01-30 09:04:36 +00001383 TargetSP target_sp(GetSP());
1384 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001385 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001386 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001387 Mutex::Locker locker;
1388 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001389 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001390 return true;
1391 }
1392 return false;
1393}
1394
Enrico Granata347c2aa2013-10-08 21:49:02 +00001395SBValue
1396SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1397{
1398 SBValue sb_value;
1399 lldb::ValueObjectSP new_value_sp;
1400 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1401 {
Enrico Granata972be532014-12-17 21:18:43 +00001402 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1403 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
Greg Claytona1e5dc82015-08-11 22:53:00 +00001404 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
Enrico Granata972be532014-12-17 21:18:43 +00001405 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, exe_ctx, ast_type);
Enrico Granata347c2aa2013-10-08 21:49:02 +00001406 }
1407 sb_value.SetSP(new_value_sp);
1408 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1409 if (log)
1410 {
1411 if (new_value_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001412 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1413 static_cast<void*>(m_opaque_sp.get()),
1414 new_value_sp->GetName().AsCString());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001415 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001416 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1417 static_cast<void*>(m_opaque_sp.get()));
Enrico Granata347c2aa2013-10-08 21:49:02 +00001418 }
1419 return sb_value;
1420}
1421
Enrico Granata972be532014-12-17 21:18:43 +00001422lldb::SBValue
1423SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1424{
1425 SBValue sb_value;
1426 lldb::ValueObjectSP new_value_sp;
1427 if (IsValid() && name && *name && data.IsValid() && type.IsValid())
1428 {
1429 DataExtractorSP extractor(*data);
1430 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
Greg Claytona1e5dc82015-08-11 22:53:00 +00001431 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
Enrico Granata972be532014-12-17 21:18:43 +00001432 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, exe_ctx, ast_type);
1433 }
1434 sb_value.SetSP(new_value_sp);
1435 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1436 if (log)
1437 {
1438 if (new_value_sp)
1439 log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1440 static_cast<void*>(m_opaque_sp.get()),
1441 new_value_sp->GetName().AsCString());
1442 else
1443 log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1444 static_cast<void*>(m_opaque_sp.get()));
1445 }
1446 return sb_value;
1447}
1448
1449lldb::SBValue
1450SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1451{
1452 SBValue sb_value;
1453 lldb::ValueObjectSP new_value_sp;
1454 if (IsValid() && name && *name && expr && *expr)
1455 {
1456 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1457 new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1458 }
1459 sb_value.SetSP(new_value_sp);
1460 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1461 if (log)
1462 {
1463 if (new_value_sp)
1464 log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1465 static_cast<void*>(m_opaque_sp.get()),
1466 new_value_sp->GetName().AsCString());
1467 else
1468 log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1469 static_cast<void*>(m_opaque_sp.get()));
1470 }
1471 return sb_value;
1472}
1473
Johnny Chen5d043462011-09-26 22:40:50 +00001474bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001475SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001476{
Greg Claytonacdbe812012-01-30 09:04:36 +00001477 TargetSP target_sp(GetSP());
1478 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001479 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001480 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001481 Mutex::Locker locker;
1482 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001483 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001484 return true;
1485 }
1486 return false;
1487}
1488
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001490lldb::SBModule
1491SBTarget::AddModule (const char *path,
1492 const char *triple,
1493 const char *uuid_cstr)
1494{
Greg Claytonb210aec2012-04-23 20:23:39 +00001495 return AddModule (path, triple, uuid_cstr, NULL);
1496}
1497
1498lldb::SBModule
1499SBTarget::AddModule (const char *path,
1500 const char *triple,
1501 const char *uuid_cstr,
1502 const char *symfile)
1503{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001504 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001505 TargetSP target_sp(GetSP());
1506 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001507 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001508 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001509 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001510 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001511
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001512 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001513 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001514
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001515 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001516 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001517 else
1518 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001519
1520 if (symfile)
1521 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001522
Greg Claytonb9a01b32012-02-26 05:51:37 +00001523 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001524 }
1525 return sb_module;
1526}
1527
Greg Clayton226cce22013-07-08 22:22:41 +00001528lldb::SBModule
1529SBTarget::AddModule (const SBModuleSpec &module_spec)
1530{
1531 lldb::SBModule sb_module;
1532 TargetSP target_sp(GetSP());
1533 if (target_sp)
1534 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1535 return sb_module;
1536}
1537
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001538bool
1539SBTarget::AddModule (lldb::SBModule &module)
1540{
Greg Claytonacdbe812012-01-30 09:04:36 +00001541 TargetSP target_sp(GetSP());
1542 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001543 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001544 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001545 return true;
1546 }
1547 return false;
1548}
1549
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550uint32_t
1551SBTarget::GetNumModules () const
1552{
Greg Clayton5160ce52013-03-27 23:08:40 +00001553 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001554
Caroline Ticeceb6b132010-10-26 03:11:13 +00001555 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001556 TargetSP target_sp(GetSP());
1557 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001558 {
1559 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001560 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001561 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001562
1563 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001564 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1565 static_cast<void*>(target_sp.get()), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001566
1567 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568}
1569
Greg Clayton48e42542010-07-30 20:12:55 +00001570void
1571SBTarget::Clear ()
1572{
Greg Clayton5160ce52013-03-27 23:08:40 +00001573 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001574
1575 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001576 log->Printf ("SBTarget(%p)::Clear ()",
1577 static_cast<void*>(m_opaque_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001578
Greg Clayton48e42542010-07-30 20:12:55 +00001579 m_opaque_sp.reset();
1580}
1581
1582
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583SBModule
1584SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1585{
1586 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001587 TargetSP target_sp(GetSP());
1588 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001589 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001590 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001591 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00001592 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001593 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594 return sb_module;
1595}
1596
Greg Clayton13d19502012-01-29 06:07:39 +00001597lldb::ByteOrder
1598SBTarget::GetByteOrder ()
1599{
Greg Claytonacdbe812012-01-30 09:04:36 +00001600 TargetSP target_sp(GetSP());
1601 if (target_sp)
1602 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00001603 return eByteOrderInvalid;
1604}
1605
1606const char *
1607SBTarget::GetTriple ()
1608{
Greg Claytonacdbe812012-01-30 09:04:36 +00001609 TargetSP target_sp(GetSP());
1610 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00001611 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001612 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00001613 // Unique the string so we don't run into ownership issues since
1614 // the const strings put the string into the string pool once and
1615 // the strings never comes out
1616 ConstString const_triple (triple.c_str());
1617 return const_triple.GetCString();
1618 }
1619 return NULL;
1620}
1621
1622uint32_t
Matthew Gardinerc928de32014-10-22 07:22:56 +00001623SBTarget::GetDataByteSize ()
1624{
1625 TargetSP target_sp(GetSP());
1626 if (target_sp)
1627 {
1628 return target_sp->GetArchitecture().GetDataByteSize() ;
1629 }
1630 return 0;
1631}
1632
1633uint32_t
1634SBTarget::GetCodeByteSize ()
1635{
1636 TargetSP target_sp(GetSP());
1637 if (target_sp)
1638 {
1639 return target_sp->GetArchitecture().GetCodeByteSize() ;
1640 }
1641 return 0;
1642}
1643
1644uint32_t
Greg Clayton13d19502012-01-29 06:07:39 +00001645SBTarget::GetAddressByteSize()
1646{
Greg Claytonacdbe812012-01-30 09:04:36 +00001647 TargetSP target_sp(GetSP());
1648 if (target_sp)
1649 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00001650 return sizeof(void*);
1651}
1652
1653
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654SBModule
1655SBTarget::GetModuleAtIndex (uint32_t idx)
1656{
Greg Clayton5160ce52013-03-27 23:08:40 +00001657 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001658
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001660 ModuleSP module_sp;
1661 TargetSP target_sp(GetSP());
1662 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001663 {
1664 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001665 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1666 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001667 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001668
1669 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001670 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1671 static_cast<void*>(target_sp.get()), idx,
1672 static_cast<void*>(module_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001673
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674 return sb_module;
1675}
1676
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001677bool
1678SBTarget::RemoveModule (lldb::SBModule module)
1679{
Greg Claytonacdbe812012-01-30 09:04:36 +00001680 TargetSP target_sp(GetSP());
1681 if (target_sp)
1682 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001683 return false;
1684}
1685
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686
1687SBBroadcaster
1688SBTarget::GetBroadcaster () const
1689{
Greg Clayton5160ce52013-03-27 23:08:40 +00001690 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001691
Greg Claytonacdbe812012-01-30 09:04:36 +00001692 TargetSP target_sp(GetSP());
1693 SBBroadcaster broadcaster(target_sp.get(), false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001694
Caroline Ticeceb6b132010-10-26 03:11:13 +00001695 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001696 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1697 static_cast<void*>(target_sp.get()),
1698 static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001699
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 return broadcaster;
1701}
1702
Caroline Ticedde9cff2010-09-20 05:20:02 +00001703bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001704SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00001705{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001706 Stream &strm = description.ref();
1707
Greg Claytonacdbe812012-01-30 09:04:36 +00001708 TargetSP target_sp(GetSP());
1709 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00001710 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001711 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001712 }
1713 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001714 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001715
1716 return true;
1717}
1718
Greg Clayton5569e642012-02-06 01:44:54 +00001719lldb::SBSymbolContextList
1720SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00001721{
Greg Clayton5569e642012-02-06 01:44:54 +00001722 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00001723 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00001724 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001725 TargetSP target_sp(GetSP());
1726 if (target_sp)
1727 {
1728 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00001729 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00001730 const bool append = true;
1731 target_sp->GetImages().FindFunctions (ConstString(name),
1732 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001733 symbols_ok,
1734 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00001735 append,
1736 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00001737 }
Greg Claytonfe356d32011-06-21 01:34:41 +00001738 }
Greg Clayton5569e642012-02-06 01:44:54 +00001739 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00001740}
1741
Carlo Kok0fd6fd42014-09-19 19:38:19 +00001742lldb::SBSymbolContextList
1743SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1744{
1745 lldb::SBSymbolContextList sb_sc_list;
1746 if (name && name[0])
1747 {
1748 TargetSP target_sp(GetSP());
1749 if (target_sp)
1750 {
1751 std::string regexstr;
1752 switch (matchtype)
1753 {
1754 case eMatchTypeRegex:
1755 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1756 break;
1757 case eMatchTypeStartsWith:
1758 regexstr = llvm::Regex::escape(name) + ".*";
1759 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1760 break;
1761 default:
1762 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1763 break;
1764 }
1765 }
1766 }
1767 return sb_sc_list;
1768}
1769
Enrico Granata6f3533f2011-07-29 19:53:35 +00001770lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00001771SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001772{
Greg Claytonacdbe812012-01-30 09:04:36 +00001773 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00001774 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001775 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001776 ConstString const_typename(typename_cstr);
1777 SymbolContext sc;
1778 const bool exact_match = false;
1779
1780 const ModuleList &module_list = target_sp->GetImages();
1781 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001782 for (size_t idx = 0; idx < count; idx++)
1783 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001784 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1785 if (module_sp)
1786 {
1787 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1788 if (type_sp)
1789 return SBType(type_sp);
1790 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001791 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001792
1793 // Didn't find the type in the symbols; try the Objective-C runtime
1794 // if one is installed
1795
1796 ProcessSP process_sp(target_sp->GetProcessSP());
1797
1798 if (process_sp)
1799 {
1800 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1801
1802 if (objc_language_runtime)
1803 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001804 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00001805
Sean Callanan9998acd2014-12-05 01:21:59 +00001806 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00001807 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001808 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00001809
Sean Callanan9998acd2014-12-05 01:21:59 +00001810 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1811 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001812 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0]))
Sean Callanan9998acd2014-12-05 01:21:59 +00001813 {
1814 return SBType(type);
1815 }
1816 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001817 }
1818 }
1819 }
Greg Claytonb43165b2012-12-05 21:24:42 +00001820
1821 // No matches, search for basic typename matches
1822 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1823 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001824 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00001825 }
1826 return SBType();
1827}
1828
Greg Claytonb43165b2012-12-05 21:24:42 +00001829SBType
1830SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001831{
Greg Claytonacdbe812012-01-30 09:04:36 +00001832 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00001833 if (target_sp)
1834 {
1835 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1836 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001837 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00001838 }
1839 return SBType();
1840}
1841
1842
1843lldb::SBTypeList
1844SBTarget::FindTypes (const char* typename_cstr)
1845{
1846 SBTypeList sb_type_list;
1847 TargetSP target_sp(GetSP());
1848 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001849 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001850 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00001851 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001852 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001853 SymbolContext sc;
1854 TypeList type_list;
1855
Greg Clayton29399a22012-04-06 17:41:13 +00001856 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00001857 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00001858 exact_match,
1859 UINT32_MAX,
1860 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001861
Greg Claytonb43165b2012-12-05 21:24:42 +00001862 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001863 {
Greg Claytonb43165b2012-12-05 21:24:42 +00001864 for (size_t idx = 0; idx < num_matches; idx++)
1865 {
1866 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1867 if (type_sp)
1868 sb_type_list.Append(SBType(type_sp));
1869 }
1870 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001871
1872 // Try the Objective-C runtime if one is installed
1873
1874 ProcessSP process_sp(target_sp->GetProcessSP());
1875
1876 if (process_sp)
1877 {
1878 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1879
1880 if (objc_language_runtime)
1881 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001882 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00001883
Sean Callanan9998acd2014-12-05 01:21:59 +00001884 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00001885 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001886 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00001887
Sean Callanan9998acd2014-12-05 01:21:59 +00001888 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
Sean Callanan7be70e82012-12-19 23:05:01 +00001889 {
Sean Callanan9998acd2014-12-05 01:21:59 +00001890 for (clang::NamedDecl *decl : decls)
Sean Callanan7be70e82012-12-19 23:05:01 +00001891 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001892 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl))
Sean Callanan9998acd2014-12-05 01:21:59 +00001893 {
1894 sb_type_list.Append(SBType(type));
1895 }
Sean Callanan7be70e82012-12-19 23:05:01 +00001896 }
1897 }
1898 }
1899 }
1900 }
1901
1902 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00001903 {
1904 // No matches, search for basic typename matches
1905 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1906 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001907 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00001908 }
1909 }
Greg Claytonb43165b2012-12-05 21:24:42 +00001910 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001911}
1912
Greg Claytondea8cb42011-06-29 22:09:02 +00001913SBValueList
1914SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1915{
1916 SBValueList sb_value_list;
1917
Greg Claytonacdbe812012-01-30 09:04:36 +00001918 TargetSP target_sp(GetSP());
1919 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00001920 {
1921 VariableList variable_list;
1922 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00001923 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
1924 append,
1925 max_matches,
1926 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00001927
1928 if (match_count > 0)
1929 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001930 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00001931 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00001932 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00001933 for (uint32_t i=0; i<match_count; ++i)
1934 {
1935 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1936 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00001937 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00001938 }
1939 }
1940 }
1941
1942 return sb_value_list;
1943}
1944
Carlo Kok0fd6fd42014-09-19 19:38:19 +00001945SBValueList
1946SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
1947{
1948 SBValueList sb_value_list;
1949
1950 TargetSP target_sp(GetSP());
1951 if (name && target_sp)
1952 {
1953 VariableList variable_list;
1954 const bool append = true;
1955
1956 std::string regexstr;
1957 uint32_t match_count;
1958 switch (matchtype)
1959 {
1960 case eMatchTypeNormal:
1961 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
1962 append,
1963 max_matches,
1964 variable_list);
1965 break;
1966 case eMatchTypeRegex:
1967 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
1968 append,
1969 max_matches,
1970 variable_list);
1971 break;
1972 case eMatchTypeStartsWith:
1973 regexstr = llvm::Regex::escape(name) + ".*";
1974 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
1975 append,
1976 max_matches,
1977 variable_list);
1978 break;
1979 }
1980
1981
1982 if (match_count > 0)
1983 {
1984 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1985 if (exe_scope == NULL)
1986 exe_scope = target_sp.get();
1987 for (uint32_t i = 0; i<match_count; ++i)
1988 {
1989 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
1990 if (valobj_sp)
1991 sb_value_list.Append(SBValue(valobj_sp));
1992 }
1993 }
1994 }
1995
1996 return sb_value_list;
1997}
1998
1999
Enrico Granatabcd80b42013-01-16 18:53:52 +00002000lldb::SBValue
2001SBTarget::FindFirstGlobalVariable (const char* name)
2002{
2003 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2004 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2005 return sb_value_list.GetValueAtIndex(0);
2006 return SBValue();
2007}
2008
Jim Inghame37d6052011-09-13 00:29:56 +00002009SBSourceManager
2010SBTarget::GetSourceManager()
2011{
2012 SBSourceManager source_manager (*this);
2013 return source_manager;
2014}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002015
Sean Callanan50952e92011-12-14 23:49:37 +00002016lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002017SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2018{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002019 return ReadInstructions (base_addr, count, NULL);
2020}
2021
2022lldb::SBInstructionList
2023SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2024{
Greg Clayton9c766112012-03-06 22:24:44 +00002025 SBInstructionList sb_instructions;
2026
2027 TargetSP target_sp(GetSP());
2028 if (target_sp)
2029 {
2030 Address *addr_ptr = base_addr.get();
2031
2032 if (addr_ptr)
2033 {
2034 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2035 bool prefer_file_cache = false;
2036 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002037 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2038 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2039 prefer_file_cache,
2040 data.GetBytes(),
2041 data.GetByteSize(),
2042 error,
2043 &load_addr);
2044 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002045 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2046 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002047 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002048 *addr_ptr,
2049 data.GetBytes(),
2050 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002051 count,
2052 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002053 }
2054 }
2055
2056 return sb_instructions;
2057
2058}
2059
2060lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002061SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2062{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002063 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2064}
2065
2066lldb::SBInstructionList
2067SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2068{
Sean Callanan50952e92011-12-14 23:49:37 +00002069 SBInstructionList sb_instructions;
2070
Greg Claytonacdbe812012-01-30 09:04:36 +00002071 TargetSP target_sp(GetSP());
2072 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002073 {
2074 Address addr;
2075
2076 if (base_addr.get())
2077 addr = *base_addr.get();
2078
Greg Clayton3faf47c2013-03-28 23:42:53 +00002079 const bool data_from_file = true;
2080
Greg Claytonacdbe812012-01-30 09:04:36 +00002081 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002082 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002083 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002084 addr,
2085 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002086 size,
2087 UINT32_MAX,
2088 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002089 }
2090
2091 return sb_instructions;
2092}
2093
2094lldb::SBInstructionList
2095SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2096{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002097 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2098}
2099
2100lldb::SBInstructionList
2101SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2102{
2103 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002104}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002105
2106SBError
2107SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2108 lldb::addr_t section_base_addr)
2109{
2110 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002111 TargetSP target_sp(GetSP());
2112 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002113 {
2114 if (!section.IsValid())
2115 {
2116 sb_error.SetErrorStringWithFormat ("invalid section");
2117 }
2118 else
2119 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002120 SectionSP section_sp (section.GetSP());
2121 if (section_sp)
2122 {
2123 if (section_sp->IsThreadSpecific())
2124 {
2125 sb_error.SetErrorString ("thread specific sections are not yet supported");
2126 }
2127 else
2128 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002129 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002130 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
Greg Clayton3c947372013-01-29 01:17:09 +00002131 {
2132 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002133 if (process_sp)
2134 process_sp->Flush();
2135 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002136 }
2137 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002138 }
2139 }
2140 else
2141 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002142 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002143 }
2144 return sb_error;
2145}
2146
2147SBError
2148SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2149{
2150 SBError sb_error;
2151
Greg Claytonacdbe812012-01-30 09:04:36 +00002152 TargetSP target_sp(GetSP());
2153 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002154 {
2155 if (!section.IsValid())
2156 {
2157 sb_error.SetErrorStringWithFormat ("invalid section");
2158 }
2159 else
2160 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002161 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002162 if (target_sp->SetSectionUnloaded (section.GetSP()))
Greg Clayton3c947372013-01-29 01:17:09 +00002163 {
2164 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002165 if (process_sp)
2166 process_sp->Flush();
2167 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002168 }
2169 }
2170 else
2171 {
2172 sb_error.SetErrorStringWithFormat ("invalid target");
2173 }
2174 return sb_error;
2175}
2176
2177SBError
2178SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2179{
2180 SBError sb_error;
2181
Greg Claytonacdbe812012-01-30 09:04:36 +00002182 TargetSP target_sp(GetSP());
2183 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002184 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002185 ModuleSP module_sp (module.GetSP());
2186 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002187 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002188 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +00002189 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002190 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002191 // The load was successful, make sure that at least some sections
2192 // changed before we notify that our module was loaded.
2193 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002194 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002195 ModuleList module_list;
2196 module_list.Append(module_sp);
2197 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002198 // Flush info in the process (stack frames, etc)
2199 ProcessSP process_sp (target_sp->GetProcessSP());
2200 if (process_sp)
2201 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002202 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002203 }
2204 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002205 else
2206 {
2207 sb_error.SetErrorStringWithFormat ("invalid module");
2208 }
2209
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002210 }
2211 else
2212 {
2213 sb_error.SetErrorStringWithFormat ("invalid target");
2214 }
2215 return sb_error;
2216}
2217
2218SBError
2219SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2220{
2221 SBError sb_error;
2222
2223 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002224 TargetSP target_sp(GetSP());
2225 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002226 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002227 ModuleSP module_sp (module.GetSP());
2228 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002229 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002230 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002231 if (objfile)
2232 {
2233 SectionList *section_list = objfile->GetSectionList();
2234 if (section_list)
2235 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002236 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002237
Greg Clayton3c947372013-01-29 01:17:09 +00002238 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002239 const size_t num_sections = section_list->GetSize();
2240 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2241 {
2242 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2243 if (section_sp)
Zachary Turner40411162014-07-16 20:28:24 +00002244 changed |= target_sp->SetSectionUnloaded (section_sp);
Greg Clayton3c947372013-01-29 01:17:09 +00002245 }
2246 if (changed)
2247 {
2248 // Flush info in the process (stack frames, etc)
2249 ProcessSP process_sp (target_sp->GetProcessSP());
2250 if (process_sp)
2251 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002252 }
2253 }
2254 else
2255 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002256 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002257 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2258 }
2259 }
2260 else
2261 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002262 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002263 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2264 }
2265 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002266 else
2267 {
2268 sb_error.SetErrorStringWithFormat ("invalid module");
2269 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002270 }
2271 else
2272 {
2273 sb_error.SetErrorStringWithFormat ("invalid target");
2274 }
2275 return sb_error;
2276}
2277
2278
Greg Claytone14e1922012-12-04 02:22:16 +00002279lldb::SBSymbolContextList
2280SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2281{
2282 SBSymbolContextList sb_sc_list;
2283 if (name && name[0])
2284 {
2285 TargetSP target_sp(GetSP());
2286 if (target_sp)
2287 {
2288 bool append = true;
2289 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2290 symbol_type,
2291 *sb_sc_list,
2292 append);
2293 }
2294 }
2295 return sb_sc_list;
2296
2297}
2298
Jim Ingham763b2b22015-07-07 22:12:17 +00002299lldb::SBValue
2300SBTarget::EvaluateExpression (const char *expr)
2301{
2302 TargetSP target_sp(GetSP());
2303 if (!target_sp)
2304 return SBValue();
2305
2306 SBExpressionOptions options;
2307 lldb::DynamicValueType fetch_dynamic_value = target_sp->GetPreferDynamicValue();
2308 options.SetFetchDynamicValue (fetch_dynamic_value);
2309 options.SetUnwindOnError (true);
2310 return EvaluateExpression(expr, options);
2311}
Greg Claytone14e1922012-12-04 02:22:16 +00002312
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002313lldb::SBValue
2314SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2315{
Greg Clayton5160ce52013-03-27 23:08:40 +00002316 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2317 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002318 SBValue expr_result;
Jim Ingham8646d3c2014-05-05 02:47:44 +00002319 ExpressionResults exe_results = eExpressionSetupError;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002320 ValueObjectSP expr_value_sp;
2321 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002322 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002323 if (target_sp)
2324 {
2325 if (expr == NULL || expr[0] == '\0')
2326 {
2327 if (log)
2328 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2329 return expr_result;
2330 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002331
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002332 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2333 ExecutionContext exe_ctx (m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002334
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002335 if (log)
2336 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002337
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002338 frame = exe_ctx.GetFramePtr();
2339 Target *target = exe_ctx.GetTargetPtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002340
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002341 if (target)
2342 {
2343#ifdef LLDB_CONFIGURATION_DEBUG
2344 StreamString frame_description;
2345 if (frame)
2346 frame->DumpUsingSettingsFormat (&frame_description);
2347 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2348 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2349#endif
2350 exe_results = target->EvaluateExpression (expr,
2351 frame,
2352 expr_value_sp,
2353 options.ref());
2354
2355 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2356#ifdef LLDB_CONFIGURATION_DEBUG
2357 Host::SetCrashDescription (NULL);
2358#endif
2359 }
2360 else
2361 {
2362 if (log)
2363 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2364 }
2365 }
2366#ifndef LLDB_DISABLE_PYTHON
2367 if (expr_log)
2368 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002369 expr_result.GetValue(), expr_result.GetSummary());
2370
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002371 if (log)
2372 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002373 static_cast<void*>(frame), expr,
2374 static_cast<void*>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002375#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002376
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002377 return expr_result;
2378}
2379
Greg Clayton13fbb992013-02-01 00:47:49 +00002380
2381lldb::addr_t
2382SBTarget::GetStackRedZoneSize()
2383{
2384 TargetSP target_sp(GetSP());
2385 if (target_sp)
2386 {
2387 ABISP abi_sp;
2388 ProcessSP process_sp (target_sp->GetProcessSP());
2389 if (process_sp)
2390 abi_sp = process_sp->GetABI();
2391 else
2392 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2393 if (abi_sp)
2394 return abi_sp->GetRedZoneSize();
2395 }
2396 return 0;
2397}
Ilia K8f37ca52015-02-13 14:31:06 +00002398
2399lldb::SBLaunchInfo
2400SBTarget::GetLaunchInfo () const
2401{
2402 lldb::SBLaunchInfo launch_info(NULL);
2403 TargetSP target_sp(GetSP());
2404 if (target_sp)
2405 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2406 return launch_info;
2407}
2408
2409void
2410SBTarget::SetLaunchInfo (const lldb::SBLaunchInfo &launch_info)
2411{
2412 TargetSP target_sp(GetSP());
2413 if (target_sp)
2414 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
2415}