blob: 54ed0989daf82a18a228713dab25a51668cfc071 [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"
Greg Claytondea8cb42011-06-29 22:09:02 +000022#include "lldb/API/SBProcess.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Jim Ingham76bb8d62016-04-28 01:40:57 +000025#include "lldb/API/SBStringList.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000026#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Breakpoint/BreakpointID.h"
28#include "lldb/Breakpoint/BreakpointIDList.h"
29#include "lldb/Breakpoint/BreakpointList.h"
30#include "lldb/Breakpoint/BreakpointLocation.h"
31#include "lldb/Core/Address.h"
32#include "lldb/Core/AddressResolver.h"
33#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/ArchSpec.h"
35#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000037#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Core/Module.h"
39#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/RegularExpression.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000041#include "lldb/Core/STLUtils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000043#include "lldb/Core/Section.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000044#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000045#include "lldb/Core/ValueObjectList.h"
46#include "lldb/Core/ValueObjectVariable.h"
47#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000048#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000049#include "lldb/Interpreter/Args.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000050#include "lldb/Symbol/ClangASTContext.h"
51#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000052#include "lldb/Symbol/ObjectFile.h"
Greg Claytonae088e52016-02-10 21:28:13 +000053#include "lldb/Symbol/SymbolFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000054#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000055#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000056#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000057#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000058#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000059#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000061#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062#include "lldb/Target/Target.h"
63#include "lldb/Target/TargetList.h"
64
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "../source/Commands/CommandObjectBreakpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000066#include "lldb/Interpreter/CommandReturnObject.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000067#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069using namespace lldb;
70using namespace lldb_private;
71
72#define DEFAULT_DISASM_BYTE_SIZE 32
73
Oleksiy Vyalov37386142015-02-10 22:49:57 +000074namespace {
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076Error AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
77 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
Oleksiy Vyalov37386142015-02-10 22:49:57 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 auto process_sp = target.GetProcessSP();
80 if (process_sp) {
81 const auto state = process_sp->GetState();
82 if (process_sp->IsAlive() && state == eStateConnected) {
83 // If we are already connected, then we have already specified the
84 // listener, so if a valid listener is supplied, we need to error out
85 // to let the client know.
86 if (attach_info.GetListener())
87 return Error("process is connected and already has a listener, pass "
88 "empty listener");
Oleksiy Vyalov37386142015-02-10 22:49:57 +000089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 return target.Attach(attach_info, nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095} // namespace
Oleksiy Vyalov37386142015-02-10 22:49:57 +000096
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097//----------------------------------------------------------------------
98// SBTarget constructor
99//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100SBTarget::SBTarget() : m_opaque_sp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
107 if (this != &rhs)
108 m_opaque_sp = rhs.m_opaque_sp;
109 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000110}
111
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112//----------------------------------------------------------------------
113// Destructor
114//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115SBTarget::~SBTarget() {}
116
117bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
118 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119}
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
122 return Target::TargetEventData::GetTargetFromEvent(event.get());
Ilia Keb2c19a2015-03-10 21:59:55 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
126 const ModuleList module_list =
127 Target::TargetEventData::GetModuleListFromEvent(event.get());
128 return module_list.GetSize();
Ilia Keb2c19a2015-03-10 21:59:55 +0000129}
130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
132 const SBEvent &event) {
133 const ModuleList module_list =
134 Target::TargetEventData::GetModuleListFromEvent(event.get());
135 return SBModule(module_list.GetModuleAtIndex(idx));
Ilia Keb2c19a2015-03-10 21:59:55 +0000136}
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138const char *SBTarget::GetBroadcasterClassName() {
139 return Target::GetStaticBroadcasterClass().AsCString();
Ilia Keb2c19a2015-03-10 21:59:55 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142bool SBTarget::IsValid() const {
143 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146SBProcess SBTarget::GetProcess() {
147 SBProcess sb_process;
148 ProcessSP process_sp;
149 TargetSP target_sp(GetSP());
150 if (target_sp) {
151 process_sp = target_sp->GetProcessSP();
152 sb_process.SetSP(process_sp);
153 }
154
155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
156 if (log)
157 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
158 static_cast<void *>(target_sp.get()),
159 static_cast<void *>(process_sp.get()));
160
161 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164SBPlatform SBTarget::GetPlatform() {
165 TargetSP target_sp(GetSP());
166 if (!target_sp)
167 return SBPlatform();
168
169 SBPlatform platform;
170 platform.m_opaque_sp = target_sp->GetPlatform();
171
172 return platform;
173}
174
175SBDebugger SBTarget::GetDebugger() const {
176 SBDebugger debugger;
177 TargetSP target_sp(GetSP());
178 if (target_sp)
179 debugger.reset(target_sp->GetDebugger().shared_from_this());
180 return debugger;
181}
182
183SBProcess SBTarget::LoadCore(const char *core_file) {
184 SBProcess sb_process;
185 TargetSP target_sp(GetSP());
186 if (target_sp) {
187 FileSpec filespec(core_file, true);
188 ProcessSP process_sp(target_sp->CreateProcess(
189 target_sp->GetDebugger().GetListener(), NULL, &filespec));
190 if (process_sp) {
191 process_sp->LoadCore();
192 sb_process.SetSP(process_sp);
193 }
194 }
195 return sb_process;
196}
197
198SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
199 const char *working_directory) {
200 char *stdin_path = NULL;
201 char *stdout_path = NULL;
202 char *stderr_path = NULL;
203 uint32_t launch_flags = 0;
204 bool stop_at_entry = false;
205 SBError error;
206 SBListener listener = GetDebugger().GetListener();
207 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path,
208 working_directory, launch_flags, stop_at_entry, error);
209}
210
211SBError SBTarget::Install() {
212 SBError sb_error;
213 TargetSP target_sp(GetSP());
214 if (target_sp) {
215 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
216 sb_error.ref() = target_sp->Install(NULL);
217 }
218 return sb_error;
219}
220
221SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
222 char const **envp, const char *stdin_path,
223 const char *stdout_path, const char *stderr_path,
224 const char *working_directory,
225 uint32_t launch_flags, // See LaunchFlags
226 bool stop_at_entry, lldb::SBError &error) {
227 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
228
229 SBProcess sb_process;
230 ProcessSP process_sp;
231 TargetSP target_sp(GetSP());
232
233 if (log)
234 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
235 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
236 "stop_at_entry=%i, &error (%p))...",
237 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
238 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
239 stdout_path ? stdout_path : "NULL",
240 stderr_path ? stderr_path : "NULL",
241 working_directory ? working_directory : "NULL", launch_flags,
242 stop_at_entry, static_cast<void *>(error.get()));
243
244 if (target_sp) {
245 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
246
247 if (stop_at_entry)
248 launch_flags |= eLaunchFlagStopAtEntry;
249
250 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
251 launch_flags |= eLaunchFlagDisableASLR;
252
253 StateType state = eStateInvalid;
254 process_sp = target_sp->GetProcessSP();
255 if (process_sp) {
256 state = process_sp->GetState();
257
258 if (process_sp->IsAlive() && state != eStateConnected) {
259 if (state == eStateAttaching)
260 error.SetErrorString("process attach is in progress");
261 else
262 error.SetErrorString("a process is already being debugged");
263 return sb_process;
264 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000265 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 if (state == eStateConnected) {
268 // If we are already connected, then we have already specified the
269 // listener, so if a valid listener is supplied, we need to error out
270 // to let the client know.
271 if (listener.IsValid()) {
272 error.SetErrorString("process is connected and already has a listener, "
273 "pass empty listener");
274 return sb_process;
275 }
276 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
279 launch_flags |= eLaunchFlagDisableSTDIO;
280
281 ProcessLaunchInfo launch_info(
282 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
283 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
284 launch_flags);
285
286 Module *exe_module = target_sp->GetExecutableModulePointer();
287 if (exe_module)
288 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
289 if (argv)
290 launch_info.GetArguments().AppendArguments(argv);
291 if (envp)
292 launch_info.GetEnvironmentEntries().SetArguments(envp);
293
294 if (listener.IsValid())
295 launch_info.SetListener(listener.GetSP());
296
297 error.SetError(target_sp->Launch(launch_info, NULL));
298
299 sb_process.SetSP(target_sp->GetProcessSP());
300 } else {
301 error.SetErrorString("SBTarget is invalid");
302 }
303
304 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
305 if (log)
306 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
307 static_cast<void *>(target_sp.get()),
308 static_cast<void *>(sb_process.GetSP().get()),
309 error.GetCString());
310
311 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312}
313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
315 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 SBProcess sb_process;
318 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 if (log)
321 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
322 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 if (target_sp) {
325 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
326 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000327 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 ProcessSP process_sp = target_sp->GetProcessSP();
329 if (process_sp) {
330 state = process_sp->GetState();
331
332 if (process_sp->IsAlive() && state != eStateConnected) {
333 if (state == eStateAttaching)
334 error.SetErrorString("process attach is in progress");
335 else
336 error.SetErrorString("a process is already being debugged");
337 return sb_process;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000340 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (!launch_info.GetExecutableFile()) {
345 Module *exe_module = target_sp->GetExecutableModulePointer();
346 if (exe_module)
347 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000348 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349
350 const ArchSpec &arch_spec = target_sp->GetArchitecture();
351 if (arch_spec.IsValid())
352 launch_info.GetArchitecture() = arch_spec;
353
354 error.SetError(target_sp->Launch(launch_info, NULL));
355 sb_process.SetSP(target_sp->GetProcessSP());
356 } else {
357 error.SetErrorString("SBTarget is invalid");
358 }
359
360 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
361 if (log)
362 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
363 static_cast<void *>(target_sp.get()),
364 static_cast<void *>(sb_process.GetSP().get()));
365
366 return sb_process;
Greg Claytonfbb76342013-11-20 21:07:01 +0000367}
368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
370 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 SBProcess sb_process;
373 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 if (log)
376 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
377 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 if (target_sp) {
380 ProcessAttachInfo &attach_info = sb_attach_info.ref();
381 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
382 PlatformSP platform_sp = target_sp->GetPlatform();
383 // See if we can pre-verify if a process exists or not
384 if (platform_sp && platform_sp->IsConnected()) {
385 lldb::pid_t attach_pid = attach_info.GetProcessID();
386 ProcessInstanceInfo instance_info;
387 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
388 attach_info.SetUserID(instance_info.GetEffectiveUserID());
389 } else {
390 error.ref().SetErrorStringWithFormat(
391 "no process found with process ID %" PRIu64, attach_pid);
392 if (log) {
393 log->Printf("SBTarget(%p)::Attach (...) => error %s",
394 static_cast<void *>(target_sp.get()),
395 error.GetCString());
396 }
397 return sb_process;
Greg Clayton931180e2011-01-27 06:44:37 +0000398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 error.SetError(AttachToProcess(attach_info, *target_sp));
402 if (error.Success())
403 sb_process.SetSP(target_sp->GetProcessSP());
404 } else {
405 error.SetErrorString("SBTarget is invalid");
406 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 if (log)
409 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
410 static_cast<void *>(target_sp.get()),
411 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000416#if defined(__APPLE__)
417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418lldb::SBProcess SBTarget::AttachToProcessWithID(SBListener &listener,
419 ::pid_t pid,
420 lldb::SBError &error) {
421 return AttachToProcessWithID(listener, (lldb::pid_t)pid, error);
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000422}
423
424#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000427 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 lldb::pid_t pid, // The process ID to attach to
429 SBError &error // An error explaining what went wrong if attach fails
430 ) {
431 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000432
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 SBProcess sb_process;
434 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 if (log)
437 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
438 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 if (target_sp) {
441 ProcessAttachInfo attach_info;
442 attach_info.SetProcessID(pid);
443 if (listener.IsValid())
444 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 ProcessInstanceInfo instance_info;
447 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
448 attach_info.SetUserID(instance_info.GetEffectiveUserID());
449
450 error.SetError(AttachToProcess(attach_info, *target_sp));
451 if (error.Success())
452 sb_process.SetSP(target_sp->GetProcessSP());
453 } else
454 error.SetErrorString("SBTarget is invalid");
455
456 if (log)
457 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
458 static_cast<void *>(target_sp.get()), __FUNCTION__,
459 static_cast<void *>(sb_process.GetSP().get()));
460 return sb_process;
461}
462
463lldb::SBProcess SBTarget::AttachToProcessWithName(
464 SBListener &listener,
465 const char *name, // basename of process to attach to
466 bool wait_for, // if true wait for a new instance of "name" to be launched
467 SBError &error // An error explaining what went wrong if attach fails
468 ) {
469 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
470
471 SBProcess sb_process;
472 TargetSP target_sp(GetSP());
473
474 if (log)
475 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
476 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
477 wait_for ? "true" : "false");
478
479 if (name && target_sp) {
480 ProcessAttachInfo attach_info;
481 attach_info.GetExecutableFile().SetFile(name, false);
482 attach_info.SetWaitForLaunch(wait_for);
483 if (listener.IsValid())
484 attach_info.SetListener(listener.GetSP());
485
486 error.SetError(AttachToProcess(attach_info, *target_sp));
487 if (error.Success())
488 sb_process.SetSP(target_sp->GetProcessSP());
489 } else
490 error.SetErrorString("SBTarget is invalid");
491
492 if (log)
493 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
494 static_cast<void *>(target_sp.get()), __FUNCTION__,
495 static_cast<void *>(sb_process.GetSP().get()));
496 return sb_process;
497}
498
499lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
500 const char *plugin_name,
501 SBError &error) {
502 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
503
504 SBProcess sb_process;
505 ProcessSP process_sp;
506 TargetSP target_sp(GetSP());
507
508 if (log)
509 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
510 "plugin_name=%s, error)...",
511 static_cast<void *>(target_sp.get()), url, plugin_name);
512
513 if (target_sp) {
514 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
515 if (listener.IsValid())
516 process_sp =
517 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000518 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 process_sp = target_sp->CreateProcess(
520 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
521
522 if (process_sp) {
523 sb_process.SetSP(process_sp);
524 error.SetError(process_sp->ConnectRemote(NULL, url));
525 } else {
526 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 } else {
529 error.SetErrorString("SBTarget is invalid");
530 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 if (log)
533 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
534 static_cast<void *>(target_sp.get()),
535 static_cast<void *>(process_sp.get()));
536 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000537}
538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 SBFileSpec exe_file_spec;
542 TargetSP target_sp(GetSP());
543 if (target_sp) {
544 Module *exe_module = target_sp->GetExecutableModulePointer();
545 if (exe_module)
546 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
547 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
550 if (log) {
551 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
552 static_cast<void *>(target_sp.get()),
553 static_cast<const void *>(exe_file_spec.get()));
554 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557}
558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559bool SBTarget::operator==(const SBTarget &rhs) const {
560 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563bool SBTarget::operator!=(const SBTarget &rhs) const {
564 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
568
569void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
570 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000571}
572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
574 lldb::SBAddress sb_addr;
575 Address &addr = sb_addr.ref();
576 TargetSP target_sp(GetSP());
577 if (target_sp) {
578 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
579 if (target_sp->ResolveLoadAddress(vm_addr, addr))
580 return sb_addr;
581 }
582
583 // We have a load address that isn't in a section, just return an address
584 // with the offset filled in (the address) and the section set to NULL
585 addr.SetRawAddress(vm_addr);
586 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
590 lldb::SBAddress sb_addr;
591 Address &addr = sb_addr.ref();
592 TargetSP target_sp(GetSP());
593 if (target_sp) {
594 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
595 if (target_sp->ResolveFileAddress(file_addr, addr))
596 return sb_addr;
597 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000598
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 addr.SetRawAddress(file_addr);
600 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000601}
602
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
604 lldb::addr_t vm_addr) {
605 lldb::SBAddress sb_addr;
606 Address &addr = sb_addr.ref();
607 TargetSP target_sp(GetSP());
608 if (target_sp) {
609 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
610 if (target_sp->ResolveLoadAddress(vm_addr, addr))
611 return sb_addr;
612 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000613
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 // We have a load address that isn't in a section, just return an address
615 // with the offset filled in (the address) and the section set to NULL
616 addr.SetRawAddress(vm_addr);
617 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000618}
619
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000620SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
622 uint32_t resolve_scope) {
623 SBSymbolContext sc;
624 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000625 TargetSP target_sp(GetSP());
626 if (target_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 target_sp->GetImages().ResolveSymbolContextForAddress(
628 addr.ref(), resolve_scope, sc.ref());
629 }
630 return sc;
631}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
634 lldb::SBError &error) {
635 SBError sb_error;
636 size_t bytes_read = 0;
637 TargetSP target_sp(GetSP());
638 if (target_sp) {
639 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
640 bytes_read =
641 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
642 } else {
643 sb_error.SetErrorString("invalid target");
644 }
645
646 return bytes_read;
647}
648
649SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
650 uint32_t line) {
651 return SBBreakpoint(
652 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000653}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000654
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
657 uint32_t line) {
658 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659}
660
661SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
663 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000664 SBFileSpecList empty_list;
665 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
666}
667
668SBBreakpoint
669SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
670 uint32_t line, lldb::addr_t offset,
671 SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674 SBBreakpoint sb_bp;
675 TargetSP target_sp(GetSP());
676 if (target_sp && line != 0) {
677 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 const LazyBool check_inlines = eLazyBoolCalculate;
680 const LazyBool skip_prologue = eLazyBoolCalculate;
681 const bool internal = false;
682 const bool hardware = false;
683 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000684 const FileSpecList *module_list = nullptr;
685 if (sb_module_list.GetSize() > 0) {
686 module_list = sb_module_list.get();
687 }
688 *sb_bp = target_sp->CreateBreakpoint(
689 module_list, *sb_file_spec, line, offset, check_inlines, skip_prologue,
690 internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 if (log) {
694 SBStream sstr;
695 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000696 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 sb_file_spec->GetPath(path, sizeof(path));
698 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
699 "SBBreakpoint(%p): %s",
700 static_cast<void *>(target_sp.get()), path, line,
701 static_cast<void *>(sb_bp.get()), sstr.GetData());
702 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000703
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 return sb_bp;
705}
706
707SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
708 const char *module_name) {
709 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
710
711 SBBreakpoint sb_bp;
712 TargetSP target_sp(GetSP());
713 if (target_sp.get()) {
714 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
715
716 const bool internal = false;
717 const bool hardware = false;
718 const LazyBool skip_prologue = eLazyBoolCalculate;
719 const lldb::addr_t offset = 0;
720 if (module_name && module_name[0]) {
721 FileSpecList module_spec_list;
722 module_spec_list.Append(FileSpec(module_name, false));
723 *sb_bp = target_sp->CreateBreakpoint(
724 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
725 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
726 } else {
727 *sb_bp = target_sp->CreateBreakpoint(
728 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
729 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000730 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 }
732
733 if (log)
734 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
735 "module=\"%s\") => SBBreakpoint(%p)",
736 static_cast<void *>(target_sp.get()), symbol_name, module_name,
737 static_cast<void *>(sb_bp.get()));
738
739 return sb_bp;
740}
741
742lldb::SBBreakpoint
743SBTarget::BreakpointCreateByName(const char *symbol_name,
744 const SBFileSpecList &module_list,
745 const SBFileSpecList &comp_unit_list) {
746 uint32_t name_type_mask = eFunctionNameTypeAuto;
747 return BreakpointCreateByName(symbol_name, name_type_mask,
748 eLanguageTypeUnknown, module_list,
749 comp_unit_list);
750}
751
752lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
753 const char *symbol_name, uint32_t name_type_mask,
754 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
755 return BreakpointCreateByName(symbol_name, name_type_mask,
756 eLanguageTypeUnknown, module_list,
757 comp_unit_list);
758}
759
760lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
761 const char *symbol_name, uint32_t name_type_mask,
762 LanguageType symbol_language, const SBFileSpecList &module_list,
763 const SBFileSpecList &comp_unit_list) {
764 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
765
766 SBBreakpoint sb_bp;
767 TargetSP target_sp(GetSP());
768 if (target_sp && symbol_name && symbol_name[0]) {
769 const bool internal = false;
770 const bool hardware = false;
771 const LazyBool skip_prologue = eLazyBoolCalculate;
772 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
773 *sb_bp = target_sp->CreateBreakpoint(
774 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask,
775 symbol_language, 0, skip_prologue, internal, hardware);
776 }
777
778 if (log)
779 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
780 "name_type: %d) => SBBreakpoint(%p)",
781 static_cast<void *>(target_sp.get()), symbol_name,
782 name_type_mask, static_cast<void *>(sb_bp.get()));
783
784 return sb_bp;
785}
786
787lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
788 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
789 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
790 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
791 eLanguageTypeUnknown, module_list,
792 comp_unit_list);
793}
794
795lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
796 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
797 LanguageType symbol_language, const SBFileSpecList &module_list,
798 const SBFileSpecList &comp_unit_list) {
799 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
800 eLanguageTypeUnknown, 0, module_list,
801 comp_unit_list);
802}
803
804lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
805 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
806 LanguageType symbol_language, lldb::addr_t offset,
807 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
808 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
809
810 SBBreakpoint sb_bp;
811 TargetSP target_sp(GetSP());
812 if (target_sp && num_names > 0) {
813 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
814 const bool internal = false;
815 const bool hardware = false;
816 const LazyBool skip_prologue = eLazyBoolCalculate;
817 *sb_bp = target_sp->CreateBreakpoint(
818 module_list.get(), comp_unit_list.get(), symbol_names, num_names,
819 name_type_mask, symbol_language, offset, skip_prologue, internal,
820 hardware);
821 }
822
823 if (log) {
824 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
825 static_cast<void *>(target_sp.get()));
826 for (uint32_t i = 0; i < num_names; i++) {
827 char sep;
828 if (i < num_names - 1)
829 sep = ',';
830 else
831 sep = '}';
832 if (symbol_names[i] != NULL)
833 log->Printf("\"%s\"%c ", symbol_names[i], sep);
834 else
835 log->Printf("\"<NULL>\"%c ", sep);
836 }
837 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
838 static_cast<void *>(sb_bp.get()));
839 }
840
841 return sb_bp;
842}
843
844SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
845 const char *module_name) {
846 SBFileSpecList module_spec_list;
847 SBFileSpecList comp_unit_list;
848 if (module_name && module_name[0]) {
849 module_spec_list.Append(FileSpec(module_name, false));
850 }
851 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
852 module_spec_list, comp_unit_list);
853}
854
855lldb::SBBreakpoint
856SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
857 const SBFileSpecList &module_list,
858 const SBFileSpecList &comp_unit_list) {
859 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
860 module_list, comp_unit_list);
861}
862
863lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
864 const char *symbol_name_regex, LanguageType symbol_language,
865 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
866 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
867
868 SBBreakpoint sb_bp;
869 TargetSP target_sp(GetSP());
870 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
871 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000872 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873 const bool internal = false;
874 const bool hardware = false;
875 const LazyBool skip_prologue = eLazyBoolCalculate;
876
877 *sb_bp = target_sp->CreateFuncRegexBreakpoint(
878 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
879 skip_prologue, internal, hardware);
880 }
881
882 if (log)
883 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
884 "=> SBBreakpoint(%p)",
885 static_cast<void *>(target_sp.get()), symbol_name_regex,
886 static_cast<void *>(sb_bp.get()));
887
888 return sb_bp;
889}
890
891SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
892 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
893
894 SBBreakpoint sb_bp;
895 TargetSP target_sp(GetSP());
896 if (target_sp) {
897 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
898 const bool hardware = false;
899 *sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
900 }
901
902 if (log)
903 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
904 ") => SBBreakpoint(%p)",
905 static_cast<void *>(target_sp.get()),
906 static_cast<uint64_t>(address),
907 static_cast<void *>(sb_bp.get()));
908
909 return sb_bp;
910}
911
912SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
913 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
914
915 SBBreakpoint sb_bp;
916 TargetSP target_sp(GetSP());
917 if (!sb_address.IsValid()) {
918 if (log)
919 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
920 "invalid address",
921 static_cast<void *>(target_sp.get()));
922 return sb_bp;
923 }
924
925 if (target_sp) {
926 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
927 const bool hardware = false;
928 *sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
929 }
930
931 if (log) {
932 SBStream s;
933 sb_address.GetDescription(s);
934 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
935 "SBBreakpoint(%p)",
936 static_cast<void *>(target_sp.get()), s.GetData(),
937 static_cast<void *>(sb_bp.get()));
938 }
939
940 return sb_bp;
941}
942
943lldb::SBBreakpoint
944SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
945 const lldb::SBFileSpec &source_file,
946 const char *module_name) {
947 SBFileSpecList module_spec_list;
948
949 if (module_name && module_name[0]) {
950 module_spec_list.Append(FileSpec(module_name, false));
951 }
952
953 SBFileSpecList source_file_list;
954 if (source_file.IsValid()) {
955 source_file_list.Append(source_file);
956 }
957
958 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
959 source_file_list);
960}
961
962lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
963 const char *source_regex, const SBFileSpecList &module_list,
964 const lldb::SBFileSpecList &source_file_list) {
965 return BreakpointCreateBySourceRegex(source_regex, module_list,
966 source_file_list, SBStringList());
967}
968
969lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
970 const char *source_regex, const SBFileSpecList &module_list,
971 const lldb::SBFileSpecList &source_file_list,
972 const SBStringList &func_names) {
973 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
974
975 SBBreakpoint sb_bp;
976 TargetSP target_sp(GetSP());
977 if (target_sp && source_regex && source_regex[0]) {
978 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
979 const bool hardware = false;
980 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +0000981 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 std::unordered_set<std::string> func_names_set;
983 for (size_t i = 0; i < func_names.GetSize(); i++) {
984 func_names_set.insert(func_names.GetStringAtIndex(i));
985 }
986
987 *sb_bp = target_sp->CreateSourceRegexBreakpoint(
988 module_list.get(), source_file_list.get(), func_names_set, regexp,
989 false, hardware, move_to_nearest_code);
990 }
991
992 if (log)
993 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
994 "=> SBBreakpoint(%p)",
995 static_cast<void *>(target_sp.get()), source_regex,
996 static_cast<void *>(sb_bp.get()));
997
998 return sb_bp;
999}
1000
1001lldb::SBBreakpoint
1002SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1003 bool catch_bp, bool throw_bp) {
1004 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1005
1006 SBBreakpoint sb_bp;
1007 TargetSP target_sp(GetSP());
1008 if (target_sp) {
1009 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1010 const bool hardware = false;
1011 *sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
1012 hardware);
1013 }
1014
1015 if (log)
1016 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: "
1017 "%s throw: %s) => SBBreakpoint(%p)",
1018 static_cast<void *>(target_sp.get()),
1019 Language::GetNameForLanguageType(language),
1020 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1021 static_cast<void *>(sb_bp.get()));
1022
1023 return sb_bp;
1024}
1025
1026uint32_t SBTarget::GetNumBreakpoints() const {
1027 TargetSP target_sp(GetSP());
1028 if (target_sp) {
1029 // The breakpoint list is thread safe, no need to lock
1030 return target_sp->GetBreakpointList().GetSize();
1031 }
1032 return 0;
1033}
1034
1035SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1036 SBBreakpoint sb_breakpoint;
1037 TargetSP target_sp(GetSP());
1038 if (target_sp) {
1039 // The breakpoint list is thread safe, no need to lock
1040 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1041 }
1042 return sb_breakpoint;
1043}
1044
1045bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1046 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1047
1048 bool result = false;
1049 TargetSP target_sp(GetSP());
1050 if (target_sp) {
1051 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1052 result = target_sp->RemoveBreakpointByID(bp_id);
1053 }
1054
1055 if (log)
1056 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1057 static_cast<void *>(target_sp.get()),
1058 static_cast<uint32_t>(bp_id), result);
1059
1060 return result;
1061}
1062
1063SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1064 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1065
1066 SBBreakpoint sb_breakpoint;
1067 TargetSP target_sp(GetSP());
1068 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1069 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1070 *sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
1071 }
1072
1073 if (log)
1074 log->Printf(
1075 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1076 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
1077 static_cast<void *>(sb_breakpoint.get()));
1078
1079 return sb_breakpoint;
1080}
1081
Jim Inghamff9a91e2016-09-21 01:21:19 +00001082bool SBTarget::FindBreakpointsByName(const char *name,
1083 SBBreakpointList &bkpts) {
1084 TargetSP target_sp(GetSP());
1085 if (target_sp) {
1086 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1087 BreakpointList bkpt_list(false);
1088 bool is_valid =
1089 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1090 if (!is_valid)
1091 return false;
1092 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1093 bkpts.AppendByID(bkpt_sp->GetID());
1094 }
1095 }
1096 return true;
1097}
1098
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099bool SBTarget::EnableAllBreakpoints() {
1100 TargetSP target_sp(GetSP());
1101 if (target_sp) {
1102 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1103 target_sp->EnableAllBreakpoints();
1104 return true;
1105 }
1106 return false;
1107}
1108
1109bool SBTarget::DisableAllBreakpoints() {
1110 TargetSP target_sp(GetSP());
1111 if (target_sp) {
1112 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1113 target_sp->DisableAllBreakpoints();
1114 return true;
1115 }
1116 return false;
1117}
1118
1119bool SBTarget::DeleteAllBreakpoints() {
1120 TargetSP target_sp(GetSP());
1121 if (target_sp) {
1122 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1123 target_sp->RemoveAllBreakpoints();
1124 return true;
1125 }
1126 return false;
1127}
1128
Jim Ingham01f16662016-09-14 19:07:35 +00001129lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1130 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001131 SBStringList empty_name_list;
1132 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1133}
1134
1135lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1136 SBStringList &matching_names,
1137 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001138 SBError sberr;
1139 TargetSP target_sp(GetSP());
1140 if (!target_sp) {
1141 sberr.SetErrorString(
1142 "BreakpointCreateFromFile called with invalid target.");
1143 return sberr;
1144 }
1145 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1146
1147 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001148
1149 std::vector<std::string> name_vector;
1150 size_t num_names = matching_names.GetSize();
1151 for (size_t i = 0; i < num_names; i++)
1152 name_vector.push_back(matching_names.GetStringAtIndex(i));
1153
1154 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1155 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001156 if (sberr.Fail())
1157 return sberr;
1158
1159 size_t num_bkpts = bp_ids.GetSize();
1160 for (size_t i = 0; i < num_bkpts; i++) {
1161 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1162 new_bps.AppendByID(bp_id.GetBreakpointID());
1163 }
1164 return sberr;
1165}
1166
1167lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1168 SBError sberr;
1169 TargetSP target_sp(GetSP());
1170 if (!target_sp) {
1171 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1172 return sberr;
1173 }
1174 SBBreakpointList bkpt_list(*this);
1175 return BreakpointsWriteToFile(dest_file, bkpt_list);
1176}
1177
1178lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
1179 SBBreakpointList &bkpt_list) {
1180 SBError sberr;
1181 TargetSP target_sp(GetSP());
1182 if (!target_sp) {
1183 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1184 return sberr;
1185 }
1186
1187 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1188 BreakpointIDList bp_id_list;
1189 bkpt_list.CopyToBreakpointIDList(bp_id_list);
1190 sberr.ref() =
1191 target_sp->SerializeBreakpointsToFile(dest_file.ref(), bp_id_list);
1192 return sberr;
1193}
1194
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195uint32_t SBTarget::GetNumWatchpoints() const {
1196 TargetSP target_sp(GetSP());
1197 if (target_sp) {
1198 // The watchpoint list is thread safe, no need to lock
1199 return target_sp->GetWatchpointList().GetSize();
1200 }
1201 return 0;
1202}
1203
1204SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1205 SBWatchpoint sb_watchpoint;
1206 TargetSP target_sp(GetSP());
1207 if (target_sp) {
1208 // The watchpoint list is thread safe, no need to lock
1209 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1210 }
1211 return sb_watchpoint;
1212}
1213
1214bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1215 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1216
1217 bool result = false;
1218 TargetSP target_sp(GetSP());
1219 if (target_sp) {
1220 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1221 std::unique_lock<std::recursive_mutex> lock;
1222 target_sp->GetWatchpointList().GetListMutex(lock);
1223 result = target_sp->RemoveWatchpointByID(wp_id);
1224 }
1225
1226 if (log)
1227 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1228 static_cast<void *>(target_sp.get()),
1229 static_cast<uint32_t>(wp_id), result);
1230
1231 return result;
1232}
1233
1234SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1235 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1236
1237 SBWatchpoint sb_watchpoint;
1238 lldb::WatchpointSP watchpoint_sp;
1239 TargetSP target_sp(GetSP());
1240 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1241 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1242 std::unique_lock<std::recursive_mutex> lock;
1243 target_sp->GetWatchpointList().GetListMutex(lock);
1244 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1245 sb_watchpoint.SetSP(watchpoint_sp);
1246 }
1247
1248 if (log)
1249 log->Printf(
1250 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1251 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1252 static_cast<void *>(watchpoint_sp.get()));
1253
1254 return sb_watchpoint;
1255}
1256
1257lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1258 bool read, bool write,
1259 SBError &error) {
1260 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1261
1262 SBWatchpoint sb_watchpoint;
1263 lldb::WatchpointSP watchpoint_sp;
1264 TargetSP target_sp(GetSP());
1265 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1266 size > 0) {
1267 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1268 uint32_t watch_type = 0;
1269 if (read)
1270 watch_type |= LLDB_WATCH_TYPE_READ;
1271 if (write)
1272 watch_type |= LLDB_WATCH_TYPE_WRITE;
1273 if (watch_type == 0) {
1274 error.SetErrorString(
1275 "Can't create a watchpoint that is neither read nor write.");
1276 return sb_watchpoint;
1277 }
1278
1279 // Target::CreateWatchpoint() is thread safe.
1280 Error cw_error;
1281 // This API doesn't take in a type, so we can't figure out what it is.
1282 CompilerType *type = NULL;
1283 watchpoint_sp =
1284 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1285 error.SetError(cw_error);
1286 sb_watchpoint.SetSP(watchpoint_sp);
1287 }
1288
1289 if (log)
1290 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1291 ", 0x%u) => SBWatchpoint(%p)",
1292 static_cast<void *>(target_sp.get()), addr,
1293 static_cast<uint32_t>(size),
1294 static_cast<void *>(watchpoint_sp.get()));
1295
1296 return sb_watchpoint;
1297}
1298
1299bool SBTarget::EnableAllWatchpoints() {
1300 TargetSP target_sp(GetSP());
1301 if (target_sp) {
1302 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1303 std::unique_lock<std::recursive_mutex> lock;
1304 target_sp->GetWatchpointList().GetListMutex(lock);
1305 target_sp->EnableAllWatchpoints();
1306 return true;
1307 }
1308 return false;
1309}
1310
1311bool SBTarget::DisableAllWatchpoints() {
1312 TargetSP target_sp(GetSP());
1313 if (target_sp) {
1314 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1315 std::unique_lock<std::recursive_mutex> lock;
1316 target_sp->GetWatchpointList().GetListMutex(lock);
1317 target_sp->DisableAllWatchpoints();
1318 return true;
1319 }
1320 return false;
1321}
1322
1323SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1324 SBType type) {
1325 SBValue sb_value;
1326 lldb::ValueObjectSP new_value_sp;
1327 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1328 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1329 ExecutionContext exe_ctx(
1330 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1331 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1332 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1333 exe_ctx, ast_type);
1334 }
1335 sb_value.SetSP(new_value_sp);
1336 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1337 if (log) {
1338 if (new_value_sp)
1339 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1340 static_cast<void *>(m_opaque_sp.get()),
1341 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001342 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1344 static_cast<void *>(m_opaque_sp.get()));
1345 }
1346 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001347}
1348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1350 lldb::SBType type) {
1351 SBValue sb_value;
1352 lldb::ValueObjectSP new_value_sp;
1353 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1354 DataExtractorSP extractor(*data);
1355 ExecutionContext exe_ctx(
1356 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1357 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1358 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1359 exe_ctx, ast_type);
1360 }
1361 sb_value.SetSP(new_value_sp);
1362 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1363 if (log) {
1364 if (new_value_sp)
1365 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1366 static_cast<void *>(m_opaque_sp.get()),
1367 new_value_sp->GetName().AsCString());
1368 else
1369 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1370 static_cast<void *>(m_opaque_sp.get()));
1371 }
1372 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001373}
1374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1376 const char *expr) {
1377 SBValue sb_value;
1378 lldb::ValueObjectSP new_value_sp;
1379 if (IsValid() && name && *name && expr && *expr) {
1380 ExecutionContext exe_ctx(
1381 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1382 new_value_sp =
1383 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1384 }
1385 sb_value.SetSP(new_value_sp);
1386 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1387 if (log) {
1388 if (new_value_sp)
1389 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1390 static_cast<void *>(m_opaque_sp.get()),
1391 new_value_sp->GetName().AsCString());
1392 else
1393 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1394 static_cast<void *>(m_opaque_sp.get()));
1395 }
1396 return sb_value;
1397}
1398
1399bool SBTarget::DeleteAllWatchpoints() {
1400 TargetSP target_sp(GetSP());
1401 if (target_sp) {
1402 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1403 std::unique_lock<std::recursive_mutex> lock;
1404 target_sp->GetWatchpointList().GetListMutex(lock);
1405 target_sp->RemoveAllWatchpoints();
1406 return true;
1407 }
1408 return false;
1409}
1410
1411lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1412 const char *uuid_cstr) {
1413 return AddModule(path, triple, uuid_cstr, NULL);
1414}
1415
1416lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1417 const char *uuid_cstr, const char *symfile) {
1418 lldb::SBModule sb_module;
1419 TargetSP target_sp(GetSP());
1420 if (target_sp) {
1421 ModuleSpec module_spec;
1422 if (path)
1423 module_spec.GetFileSpec().SetFile(path, false);
1424
1425 if (uuid_cstr)
1426 module_spec.GetUUID().SetFromCString(uuid_cstr);
1427
1428 if (triple)
1429 module_spec.GetArchitecture().SetTriple(triple,
1430 target_sp->GetPlatform().get());
1431 else
1432 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1433
1434 if (symfile)
1435 module_spec.GetSymbolFileSpec().SetFile(symfile, false);
1436
1437 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1438 }
1439 return sb_module;
1440}
1441
1442lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1443 lldb::SBModule sb_module;
1444 TargetSP target_sp(GetSP());
1445 if (target_sp)
1446 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1447 return sb_module;
1448}
1449
1450bool SBTarget::AddModule(lldb::SBModule &module) {
1451 TargetSP target_sp(GetSP());
1452 if (target_sp) {
1453 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1454 return true;
1455 }
1456 return false;
1457}
1458
1459uint32_t SBTarget::GetNumModules() const {
1460 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1461
1462 uint32_t num = 0;
1463 TargetSP target_sp(GetSP());
1464 if (target_sp) {
1465 // The module list is thread safe, no need to lock
1466 num = target_sp->GetImages().GetSize();
1467 }
1468
1469 if (log)
1470 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1471 static_cast<void *>(target_sp.get()), num);
1472
1473 return num;
1474}
1475
1476void SBTarget::Clear() {
1477 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1478
1479 if (log)
1480 log->Printf("SBTarget(%p)::Clear ()",
1481 static_cast<void *>(m_opaque_sp.get()));
1482
1483 m_opaque_sp.reset();
1484}
1485
1486SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1487 SBModule sb_module;
1488 TargetSP target_sp(GetSP());
1489 if (target_sp && sb_file_spec.IsValid()) {
1490 ModuleSpec module_spec(*sb_file_spec);
1491 // The module list is thread safe, no need to lock
1492 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1493 }
1494 return sb_module;
1495}
1496
1497lldb::ByteOrder SBTarget::GetByteOrder() {
1498 TargetSP target_sp(GetSP());
1499 if (target_sp)
1500 return target_sp->GetArchitecture().GetByteOrder();
1501 return eByteOrderInvalid;
1502}
1503
1504const char *SBTarget::GetTriple() {
1505 TargetSP target_sp(GetSP());
1506 if (target_sp) {
1507 std::string triple(target_sp->GetArchitecture().GetTriple().str());
1508 // Unique the string so we don't run into ownership issues since
1509 // the const strings put the string into the string pool once and
1510 // the strings never comes out
1511 ConstString const_triple(triple.c_str());
1512 return const_triple.GetCString();
1513 }
1514 return NULL;
1515}
1516
1517uint32_t SBTarget::GetDataByteSize() {
1518 TargetSP target_sp(GetSP());
1519 if (target_sp) {
1520 return target_sp->GetArchitecture().GetDataByteSize();
1521 }
1522 return 0;
1523}
1524
1525uint32_t SBTarget::GetCodeByteSize() {
1526 TargetSP target_sp(GetSP());
1527 if (target_sp) {
1528 return target_sp->GetArchitecture().GetCodeByteSize();
1529 }
1530 return 0;
1531}
1532
1533uint32_t SBTarget::GetAddressByteSize() {
1534 TargetSP target_sp(GetSP());
1535 if (target_sp)
1536 return target_sp->GetArchitecture().GetAddressByteSize();
1537 return sizeof(void *);
1538}
1539
1540SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1541 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1542
1543 SBModule sb_module;
1544 ModuleSP module_sp;
1545 TargetSP target_sp(GetSP());
1546 if (target_sp) {
1547 // The module list is thread safe, no need to lock
1548 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1549 sb_module.SetSP(module_sp);
1550 }
1551
1552 if (log)
1553 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1554 static_cast<void *>(target_sp.get()), idx,
1555 static_cast<void *>(module_sp.get()));
1556
1557 return sb_module;
1558}
1559
1560bool SBTarget::RemoveModule(lldb::SBModule module) {
1561 TargetSP target_sp(GetSP());
1562 if (target_sp)
1563 return target_sp->GetImages().Remove(module.GetSP());
1564 return false;
1565}
1566
1567SBBroadcaster SBTarget::GetBroadcaster() const {
1568 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1569
1570 TargetSP target_sp(GetSP());
1571 SBBroadcaster broadcaster(target_sp.get(), false);
1572
1573 if (log)
1574 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1575 static_cast<void *>(target_sp.get()),
1576 static_cast<void *>(broadcaster.get()));
1577
1578 return broadcaster;
1579}
1580
1581bool SBTarget::GetDescription(SBStream &description,
1582 lldb::DescriptionLevel description_level) {
1583 Stream &strm = description.ref();
1584
1585 TargetSP target_sp(GetSP());
1586 if (target_sp) {
1587 target_sp->Dump(&strm, description_level);
1588 } else
1589 strm.PutCString("No value");
1590
1591 return true;
1592}
1593
1594lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1595 uint32_t name_type_mask) {
1596 lldb::SBSymbolContextList sb_sc_list;
1597 if (name && name[0]) {
Jim Ingham763b2b22015-07-07 22:12:17 +00001598 TargetSP target_sp(GetSP());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 if (target_sp) {
1600 const bool symbols_ok = true;
1601 const bool inlines_ok = true;
1602 const bool append = true;
1603 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask,
1604 symbols_ok, inlines_ok, append,
1605 *sb_sc_list);
1606 }
1607 }
1608 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001609}
Greg Claytone14e1922012-12-04 02:22:16 +00001610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1612 uint32_t max_matches,
1613 MatchType matchtype) {
1614 lldb::SBSymbolContextList sb_sc_list;
1615 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001616 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 TargetSP target_sp(GetSP());
1618 if (target_sp) {
1619 std::string regexstr;
1620 switch (matchtype) {
1621 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001622 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 true, true, *sb_sc_list);
1624 break;
1625 case eMatchTypeStartsWith:
1626 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001627 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1628 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 break;
1630 default:
1631 target_sp->GetImages().FindFunctions(ConstString(name),
1632 eFunctionNameTypeAny, true, true,
1633 true, *sb_sc_list);
1634 break;
1635 }
1636 }
1637 }
1638 return sb_sc_list;
1639}
1640
1641lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1642 TargetSP target_sp(GetSP());
1643 if (typename_cstr && typename_cstr[0] && target_sp) {
1644 ConstString const_typename(typename_cstr);
1645 SymbolContext sc;
1646 const bool exact_match = false;
1647
1648 const ModuleList &module_list = target_sp->GetImages();
1649 size_t count = module_list.GetSize();
1650 for (size_t idx = 0; idx < count; idx++) {
1651 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1652 if (module_sp) {
1653 TypeSP type_sp(
1654 module_sp->FindFirstType(sc, const_typename, exact_match));
1655 if (type_sp)
1656 return SBType(type_sp);
1657 }
1658 }
1659
1660 // Didn't find the type in the symbols; try the Objective-C runtime
1661 // if one is installed
1662
1663 ProcessSP process_sp(target_sp->GetProcessSP());
1664
1665 if (process_sp) {
1666 ObjCLanguageRuntime *objc_language_runtime =
1667 process_sp->GetObjCLanguageRuntime();
1668
1669 if (objc_language_runtime) {
1670 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1671
1672 if (objc_decl_vendor) {
1673 std::vector<clang::NamedDecl *> decls;
1674
1675 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1676 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1677 return SBType(type);
1678 }
1679 }
1680 }
1681 }
1682 }
1683
1684 // No matches, search for basic typename matches
1685 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1686 if (clang_ast)
1687 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1688 const_typename));
1689 }
1690 return SBType();
1691}
1692
1693SBType SBTarget::GetBasicType(lldb::BasicType type) {
1694 TargetSP target_sp(GetSP());
1695 if (target_sp) {
1696 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1697 if (clang_ast)
1698 return SBType(
1699 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1700 }
1701 return SBType();
1702}
1703
1704lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1705 SBTypeList sb_type_list;
1706 TargetSP target_sp(GetSP());
1707 if (typename_cstr && typename_cstr[0] && target_sp) {
1708 ModuleList &images = target_sp->GetImages();
1709 ConstString const_typename(typename_cstr);
1710 bool exact_match = false;
1711 SymbolContext sc;
1712 TypeList type_list;
1713 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1714 uint32_t num_matches =
1715 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX,
1716 searched_symbol_files, type_list);
1717
1718 if (num_matches > 0) {
1719 for (size_t idx = 0; idx < num_matches; idx++) {
1720 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1721 if (type_sp)
1722 sb_type_list.Append(SBType(type_sp));
1723 }
1724 }
1725
1726 // Try the Objective-C runtime if one is installed
1727
1728 ProcessSP process_sp(target_sp->GetProcessSP());
1729
1730 if (process_sp) {
1731 ObjCLanguageRuntime *objc_language_runtime =
1732 process_sp->GetObjCLanguageRuntime();
1733
1734 if (objc_language_runtime) {
1735 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1736
1737 if (objc_decl_vendor) {
1738 std::vector<clang::NamedDecl *> decls;
1739
1740 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1741 for (clang::NamedDecl *decl : decls) {
1742 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1743 sb_type_list.Append(SBType(type));
1744 }
1745 }
1746 }
1747 }
1748 }
1749 }
1750
1751 if (sb_type_list.GetSize() == 0) {
1752 // No matches, search for basic typename matches
1753 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1754 if (clang_ast)
1755 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1756 clang_ast->getASTContext(), const_typename)));
1757 }
1758 }
1759 return sb_type_list;
1760}
1761
1762SBValueList SBTarget::FindGlobalVariables(const char *name,
1763 uint32_t max_matches) {
1764 SBValueList sb_value_list;
1765
1766 TargetSP target_sp(GetSP());
1767 if (name && target_sp) {
1768 VariableList variable_list;
1769 const bool append = true;
1770 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
1771 ConstString(name), append, max_matches, variable_list);
1772
1773 if (match_count > 0) {
1774 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1775 if (exe_scope == NULL)
1776 exe_scope = target_sp.get();
1777 for (uint32_t i = 0; i < match_count; ++i) {
1778 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1779 exe_scope, variable_list.GetVariableAtIndex(i)));
1780 if (valobj_sp)
1781 sb_value_list.Append(SBValue(valobj_sp));
1782 }
1783 }
1784 }
1785
1786 return sb_value_list;
1787}
1788
1789SBValueList SBTarget::FindGlobalVariables(const char *name,
1790 uint32_t max_matches,
1791 MatchType matchtype) {
1792 SBValueList sb_value_list;
1793
1794 TargetSP target_sp(GetSP());
1795 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001796 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001797 VariableList variable_list;
1798 const bool append = true;
1799
1800 std::string regexstr;
1801 uint32_t match_count;
1802 switch (matchtype) {
1803 case eMatchTypeNormal:
1804 match_count = target_sp->GetImages().FindGlobalVariables(
1805 ConstString(name), append, max_matches, variable_list);
1806 break;
1807 case eMatchTypeRegex:
1808 match_count = target_sp->GetImages().FindGlobalVariables(
Zachary Turner95eae422016-09-21 16:01:28 +00001809 RegularExpression(name_ref), append, max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001810 break;
1811 case eMatchTypeStartsWith:
1812 regexstr = llvm::Regex::escape(name) + ".*";
1813 match_count = target_sp->GetImages().FindGlobalVariables(
Zachary Turner95eae422016-09-21 16:01:28 +00001814 RegularExpression(regexstr), append, max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001815 break;
1816 }
1817
1818 if (match_count > 0) {
1819 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1820 if (exe_scope == NULL)
1821 exe_scope = target_sp.get();
1822 for (uint32_t i = 0; i < match_count; ++i) {
1823 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1824 exe_scope, variable_list.GetVariableAtIndex(i)));
1825 if (valobj_sp)
1826 sb_value_list.Append(SBValue(valobj_sp));
1827 }
1828 }
1829 }
1830
1831 return sb_value_list;
1832}
1833
1834lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1835 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1836 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1837 return sb_value_list.GetValueAtIndex(0);
1838 return SBValue();
1839}
1840
1841SBSourceManager SBTarget::GetSourceManager() {
1842 SBSourceManager source_manager(*this);
1843 return source_manager;
1844}
1845
1846lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1847 uint32_t count) {
1848 return ReadInstructions(base_addr, count, NULL);
1849}
1850
1851lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1852 uint32_t count,
1853 const char *flavor_string) {
1854 SBInstructionList sb_instructions;
1855
1856 TargetSP target_sp(GetSP());
1857 if (target_sp) {
1858 Address *addr_ptr = base_addr.get();
1859
1860 if (addr_ptr) {
1861 DataBufferHeap data(
1862 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1863 bool prefer_file_cache = false;
1864 lldb_private::Error error;
1865 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1866 const size_t bytes_read =
1867 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
1868 data.GetByteSize(), error, &load_addr);
1869 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1870 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1871 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
1872 data.GetBytes(), bytes_read, count, data_from_file));
1873 }
1874 }
1875
1876 return sb_instructions;
1877}
1878
1879lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
1880 const void *buf,
1881 size_t size) {
1882 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
1883}
1884
1885lldb::SBInstructionList
1886SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
1887 const char *flavor_string, const void *buf,
1888 size_t size) {
1889 SBInstructionList sb_instructions;
1890
1891 TargetSP target_sp(GetSP());
1892 if (target_sp) {
1893 Address addr;
1894
1895 if (base_addr.get())
1896 addr = *base_addr.get();
1897
1898 const bool data_from_file = true;
1899
1900 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1901 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
1902 UINT32_MAX, data_from_file));
1903 }
1904
1905 return sb_instructions;
1906}
1907
1908lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
1909 const void *buf,
1910 size_t size) {
1911 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
1912 size);
1913}
1914
1915lldb::SBInstructionList
1916SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
1917 const char *flavor_string, const void *buf,
1918 size_t size) {
1919 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
1920 buf, size);
1921}
1922
1923SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
1924 lldb::addr_t section_base_addr) {
1925 SBError sb_error;
1926 TargetSP target_sp(GetSP());
1927 if (target_sp) {
1928 if (!section.IsValid()) {
1929 sb_error.SetErrorStringWithFormat("invalid section");
1930 } else {
1931 SectionSP section_sp(section.GetSP());
1932 if (section_sp) {
1933 if (section_sp->IsThreadSpecific()) {
1934 sb_error.SetErrorString(
1935 "thread specific sections are not yet supported");
1936 } else {
1937 ProcessSP process_sp(target_sp->GetProcessSP());
1938 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
1939 ModuleSP module_sp(section_sp->GetModule());
1940 if (module_sp) {
1941 ModuleList module_list;
1942 module_list.Append(module_sp);
1943 target_sp->ModulesDidLoad(module_list);
1944 }
1945 // Flush info in the process (stack frames, etc)
1946 if (process_sp)
1947 process_sp->Flush();
1948 }
1949 }
1950 }
1951 }
1952 } else {
1953 sb_error.SetErrorString("invalid target");
1954 }
1955 return sb_error;
1956}
1957
1958SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
1959 SBError sb_error;
1960
1961 TargetSP target_sp(GetSP());
1962 if (target_sp) {
1963 if (!section.IsValid()) {
1964 sb_error.SetErrorStringWithFormat("invalid section");
1965 } else {
1966 SectionSP section_sp(section.GetSP());
1967 if (section_sp) {
1968 ProcessSP process_sp(target_sp->GetProcessSP());
1969 if (target_sp->SetSectionUnloaded(section_sp)) {
1970 ModuleSP module_sp(section_sp->GetModule());
1971 if (module_sp) {
1972 ModuleList module_list;
1973 module_list.Append(module_sp);
1974 target_sp->ModulesDidUnload(module_list, false);
1975 }
1976 // Flush info in the process (stack frames, etc)
1977 if (process_sp)
1978 process_sp->Flush();
1979 }
1980 } else {
1981 sb_error.SetErrorStringWithFormat("invalid section");
1982 }
1983 }
1984 } else {
1985 sb_error.SetErrorStringWithFormat("invalid target");
1986 }
1987 return sb_error;
1988}
1989
1990SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
1991 int64_t slide_offset) {
1992 SBError sb_error;
1993
1994 TargetSP target_sp(GetSP());
1995 if (target_sp) {
1996 ModuleSP module_sp(module.GetSP());
1997 if (module_sp) {
1998 bool changed = false;
1999 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2000 // The load was successful, make sure that at least some sections
2001 // changed before we notify that our module was loaded.
2002 if (changed) {
2003 ModuleList module_list;
2004 module_list.Append(module_sp);
2005 target_sp->ModulesDidLoad(module_list);
2006 // Flush info in the process (stack frames, etc)
2007 ProcessSP process_sp(target_sp->GetProcessSP());
2008 if (process_sp)
2009 process_sp->Flush();
2010 }
2011 }
2012 } else {
2013 sb_error.SetErrorStringWithFormat("invalid module");
2014 }
2015
2016 } else {
2017 sb_error.SetErrorStringWithFormat("invalid target");
2018 }
2019 return sb_error;
2020}
2021
2022SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2023 SBError sb_error;
2024
2025 char path[PATH_MAX];
2026 TargetSP target_sp(GetSP());
2027 if (target_sp) {
2028 ModuleSP module_sp(module.GetSP());
2029 if (module_sp) {
2030 ObjectFile *objfile = module_sp->GetObjectFile();
2031 if (objfile) {
2032 SectionList *section_list = objfile->GetSectionList();
2033 if (section_list) {
2034 ProcessSP process_sp(target_sp->GetProcessSP());
2035
2036 bool changed = false;
2037 const size_t num_sections = section_list->GetSize();
2038 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2039 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2040 if (section_sp)
2041 changed |= target_sp->SetSectionUnloaded(section_sp);
2042 }
2043 if (changed) {
2044 ModuleList module_list;
2045 module_list.Append(module_sp);
2046 target_sp->ModulesDidUnload(module_list, false);
2047 // Flush info in the process (stack frames, etc)
2048 ProcessSP process_sp(target_sp->GetProcessSP());
2049 if (process_sp)
2050 process_sp->Flush();
2051 }
2052 } else {
2053 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2054 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2055 path);
2056 }
2057 } else {
2058 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2059 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2060 path);
2061 }
2062 } else {
2063 sb_error.SetErrorStringWithFormat("invalid module");
2064 }
2065 } else {
2066 sb_error.SetErrorStringWithFormat("invalid target");
2067 }
2068 return sb_error;
2069}
2070
2071lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2072 lldb::SymbolType symbol_type) {
2073 SBSymbolContextList sb_sc_list;
2074 if (name && name[0]) {
2075 TargetSP target_sp(GetSP());
2076 if (target_sp) {
2077 bool append = true;
2078 target_sp->GetImages().FindSymbolsWithNameAndType(
2079 ConstString(name), symbol_type, *sb_sc_list, append);
2080 }
2081 }
2082 return sb_sc_list;
2083}
2084
2085lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2086 TargetSP target_sp(GetSP());
2087 if (!target_sp)
2088 return SBValue();
2089
2090 SBExpressionOptions options;
2091 lldb::DynamicValueType fetch_dynamic_value =
2092 target_sp->GetPreferDynamicValue();
2093 options.SetFetchDynamicValue(fetch_dynamic_value);
2094 options.SetUnwindOnError(true);
2095 return EvaluateExpression(expr, options);
2096}
2097
2098lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2099 const SBExpressionOptions &options) {
2100 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002101#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002102 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002103#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002104 SBValue expr_result;
2105 ExpressionResults exe_results = eExpressionSetupError;
2106 ValueObjectSP expr_value_sp;
2107 TargetSP target_sp(GetSP());
2108 StackFrame *frame = NULL;
2109 if (target_sp) {
2110 if (expr == NULL || expr[0] == '\0') {
2111 if (log)
2112 log->Printf(
2113 "SBTarget::EvaluateExpression called with an empty expression");
2114 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116
2117 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2118 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002119
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002120 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002121 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2122
2123 frame = exe_ctx.GetFramePtr();
2124 Target *target = exe_ctx.GetTargetPtr();
2125
2126 if (target) {
2127#ifdef LLDB_CONFIGURATION_DEBUG
2128 StreamString frame_description;
2129 if (frame)
2130 frame->DumpUsingSettingsFormat(&frame_description);
2131 Host::SetCrashDescriptionWithFormat(
2132 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2133 "%u) %s",
2134 expr, options.GetFetchDynamicValue(),
2135 frame_description.GetString().c_str());
2136#endif
2137 exe_results =
2138 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2139
2140 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2141#ifdef LLDB_CONFIGURATION_DEBUG
2142 Host::SetCrashDescription(NULL);
2143#endif
2144 } else {
2145 if (log)
2146 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2147 "reconstruct frame object for this SBTarget.");
2148 }
2149 }
2150#ifndef LLDB_DISABLE_PYTHON
2151 if (expr_log)
2152 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2153 "%s, summary %s **",
2154 expr_result.GetValue(), expr_result.GetSummary());
2155
2156 if (log)
2157 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2158 "(execution result=%d)",
2159 static_cast<void *>(frame), expr,
2160 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002161#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002162
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002164}
2165
Kate Stoneb9c1b512016-09-06 20:57:50 +00002166lldb::addr_t SBTarget::GetStackRedZoneSize() {
2167 TargetSP target_sp(GetSP());
2168 if (target_sp) {
2169 ABISP abi_sp;
2170 ProcessSP process_sp(target_sp->GetProcessSP());
2171 if (process_sp)
2172 abi_sp = process_sp->GetABI();
2173 else
2174 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2175 if (abi_sp)
2176 return abi_sp->GetRedZoneSize();
2177 }
2178 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002179}
Ilia K8f37ca52015-02-13 14:31:06 +00002180
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2182 lldb::SBLaunchInfo launch_info(NULL);
2183 TargetSP target_sp(GetSP());
2184 if (target_sp)
2185 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2186 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002187}
2188
Kate Stoneb9c1b512016-09-06 20:57:50 +00002189void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2190 TargetSP target_sp(GetSP());
2191 if (target_sp)
2192 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002193}