blob: 4f168168368e7cf2e7bdc9bf8781cea2767571ab [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"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000026#include "lldb/API/SBStructuredData.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000027#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Breakpoint/BreakpointID.h"
29#include "lldb/Breakpoint/BreakpointIDList.h"
30#include "lldb/Breakpoint/BreakpointList.h"
31#include "lldb/Breakpoint/BreakpointLocation.h"
32#include "lldb/Core/Address.h"
33#include "lldb/Core/AddressResolver.h"
34#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/Module.h"
38#include "lldb/Core/ModuleSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000039#include "lldb/Core/STLUtils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Core/Section.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000042#include "lldb/Core/StructuredDataImpl.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"
Greg Clayton7fb56d02011-02-01 01:31:41 +000046#include "lldb/Host/Host.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Symbol/ClangASTContext.h"
48#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000049#include "lldb/Symbol/ObjectFile.h"
Greg Claytonae088e52016-02-10 21:28:13 +000050#include "lldb/Symbol/SymbolFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000053#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000054#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000055#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000056#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000058#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059#include "lldb/Target/Target.h"
60#include "lldb/Target/TargetList.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000061#include "lldb/Utility/ArchSpec.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000062#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000063#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000064#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000065#include "lldb/Utility/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
James Y Knight2ad48212018-05-22 22:53:50 +000067#include "Commands/CommandObjectBreakpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000068#include "lldb/Interpreter/CommandReturnObject.h"
Sean Callanan237c3ed2016-12-14 21:31:31 +000069#include "llvm/Support/PrettyStackTrace.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000070#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072using namespace lldb;
73using namespace lldb_private;
74
75#define DEFAULT_DISASM_BYTE_SIZE 32
76
Oleksiy Vyalov37386142015-02-10 22:49:57 +000077namespace {
78
Zachary Turner97206d52017-05-12 04:51:55 +000079Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
Oleksiy Vyalov37386142015-02-10 22:49:57 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 auto process_sp = target.GetProcessSP();
83 if (process_sp) {
84 const auto state = process_sp->GetState();
85 if (process_sp->IsAlive() && state == eStateConnected) {
86 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +000087 // listener, so if a valid listener is supplied, we need to error out to
88 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (attach_info.GetListener())
Zachary Turner97206d52017-05-12 04:51:55 +000090 return Status("process is connected and already has a listener, pass "
91 "empty listener");
Oleksiy Vyalov37386142015-02-10 22:49:57 +000092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 return target.Attach(attach_info, nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098} // namespace
Oleksiy Vyalov37386142015-02-10 22:49:57 +000099
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100//----------------------------------------------------------------------
101// SBTarget constructor
102//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103SBTarget::SBTarget() : m_opaque_sp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
110 if (this != &rhs)
111 m_opaque_sp = rhs.m_opaque_sp;
112 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000113}
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115//----------------------------------------------------------------------
116// Destructor
117//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118SBTarget::~SBTarget() {}
119
120bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
121 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
125 return Target::TargetEventData::GetTargetFromEvent(event.get());
Ilia Keb2c19a2015-03-10 21:59:55 +0000126}
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
129 const ModuleList module_list =
130 Target::TargetEventData::GetModuleListFromEvent(event.get());
131 return module_list.GetSize();
Ilia Keb2c19a2015-03-10 21:59:55 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
135 const SBEvent &event) {
136 const ModuleList module_list =
137 Target::TargetEventData::GetModuleListFromEvent(event.get());
138 return SBModule(module_list.GetModuleAtIndex(idx));
Ilia Keb2c19a2015-03-10 21:59:55 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141const char *SBTarget::GetBroadcasterClassName() {
142 return Target::GetStaticBroadcasterClass().AsCString();
Ilia Keb2c19a2015-03-10 21:59:55 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145bool SBTarget::IsValid() const {
146 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149SBProcess SBTarget::GetProcess() {
150 SBProcess sb_process;
151 ProcessSP process_sp;
152 TargetSP target_sp(GetSP());
153 if (target_sp) {
154 process_sp = target_sp->GetProcessSP();
155 sb_process.SetSP(process_sp);
156 }
157
158 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
159 if (log)
160 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
161 static_cast<void *>(target_sp.get()),
162 static_cast<void *>(process_sp.get()));
163
164 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167SBPlatform SBTarget::GetPlatform() {
168 TargetSP target_sp(GetSP());
169 if (!target_sp)
170 return SBPlatform();
171
172 SBPlatform platform;
173 platform.m_opaque_sp = target_sp->GetPlatform();
174
175 return platform;
176}
177
178SBDebugger SBTarget::GetDebugger() const {
179 SBDebugger debugger;
180 TargetSP target_sp(GetSP());
181 if (target_sp)
182 debugger.reset(target_sp->GetDebugger().shared_from_this());
183 return debugger;
184}
185
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000186SBStructuredData SBTarget::GetStatistics() {
187 SBStructuredData data;
188 TargetSP target_sp(GetSP());
189 if (!target_sp)
190 return data;
191
192 auto stats_up = llvm::make_unique<StructuredData::Dictionary>();
193 int i = 0;
194 for (auto &Entry : target_sp->GetStatistics()) {
195 std::string Desc = lldb_private::GetStatDescription(
196 static_cast<lldb_private::StatisticKind>(i));
197 stats_up->AddIntegerItem(Desc, Entry);
198 i += 1;
199 }
200
201 data.m_impl_up->SetObjectSP(std::move(stats_up));
202 return data;
203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205SBProcess SBTarget::LoadCore(const char *core_file) {
206 SBProcess sb_process;
207 TargetSP target_sp(GetSP());
208 if (target_sp) {
209 FileSpec filespec(core_file, true);
210 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000211 target_sp->GetDebugger().GetListener(), "", &filespec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 if (process_sp) {
213 process_sp->LoadCore();
214 sb_process.SetSP(process_sp);
215 }
216 }
217 return sb_process;
218}
219
220SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
221 const char *working_directory) {
222 char *stdin_path = NULL;
223 char *stdout_path = NULL;
224 char *stderr_path = NULL;
225 uint32_t launch_flags = 0;
226 bool stop_at_entry = false;
227 SBError error;
228 SBListener listener = GetDebugger().GetListener();
229 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path,
230 working_directory, launch_flags, stop_at_entry, error);
231}
232
233SBError SBTarget::Install() {
234 SBError sb_error;
235 TargetSP target_sp(GetSP());
236 if (target_sp) {
237 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
238 sb_error.ref() = target_sp->Install(NULL);
239 }
240 return sb_error;
241}
242
243SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
244 char const **envp, const char *stdin_path,
245 const char *stdout_path, const char *stderr_path,
246 const char *working_directory,
247 uint32_t launch_flags, // See LaunchFlags
248 bool stop_at_entry, lldb::SBError &error) {
249 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
250
251 SBProcess sb_process;
252 ProcessSP process_sp;
253 TargetSP target_sp(GetSP());
254
255 if (log)
256 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
257 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
258 "stop_at_entry=%i, &error (%p))...",
259 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
260 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
261 stdout_path ? stdout_path : "NULL",
262 stderr_path ? stderr_path : "NULL",
263 working_directory ? working_directory : "NULL", launch_flags,
264 stop_at_entry, static_cast<void *>(error.get()));
265
266 if (target_sp) {
267 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
268
269 if (stop_at_entry)
270 launch_flags |= eLaunchFlagStopAtEntry;
271
272 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
273 launch_flags |= eLaunchFlagDisableASLR;
274
275 StateType state = eStateInvalid;
276 process_sp = target_sp->GetProcessSP();
277 if (process_sp) {
278 state = process_sp->GetState();
279
280 if (process_sp->IsAlive() && state != eStateConnected) {
281 if (state == eStateAttaching)
282 error.SetErrorString("process attach is in progress");
283 else
284 error.SetErrorString("a process is already being debugged");
285 return sb_process;
286 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000287 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (state == eStateConnected) {
290 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +0000291 // listener, so if a valid listener is supplied, we need to error out to
292 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 if (listener.IsValid()) {
294 error.SetErrorString("process is connected and already has a listener, "
295 "pass empty listener");
296 return sb_process;
297 }
298 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
301 launch_flags |= eLaunchFlagDisableSTDIO;
302
303 ProcessLaunchInfo launch_info(
304 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
305 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
306 launch_flags);
307
308 Module *exe_module = target_sp->GetExecutableModulePointer();
309 if (exe_module)
310 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
311 if (argv)
312 launch_info.GetArguments().AppendArguments(argv);
313 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000314 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315
316 if (listener.IsValid())
317 launch_info.SetListener(listener.GetSP());
318
319 error.SetError(target_sp->Launch(launch_info, NULL));
320
321 sb_process.SetSP(target_sp->GetProcessSP());
322 } else {
323 error.SetErrorString("SBTarget is invalid");
324 }
325
326 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
327 if (log)
328 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
329 static_cast<void *>(target_sp.get()),
330 static_cast<void *>(sb_process.GetSP().get()),
331 error.GetCString());
332
333 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334}
335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
337 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 SBProcess sb_process;
340 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 if (log)
343 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
344 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 if (target_sp) {
347 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
348 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000349 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 ProcessSP process_sp = target_sp->GetProcessSP();
351 if (process_sp) {
352 state = process_sp->GetState();
353
354 if (process_sp->IsAlive() && state != eStateConnected) {
355 if (state == eStateAttaching)
356 error.SetErrorString("process attach is in progress");
357 else
358 error.SetErrorString("a process is already being debugged");
359 return sb_process;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000360 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000362 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000363
Pavel Labath62930e52018-01-10 11:57:31 +0000364 lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 if (!launch_info.GetExecutableFile()) {
367 Module *exe_module = target_sp->GetExecutableModulePointer();
368 if (exe_module)
369 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371
372 const ArchSpec &arch_spec = target_sp->GetArchitecture();
373 if (arch_spec.IsValid())
374 launch_info.GetArchitecture() = arch_spec;
375
376 error.SetError(target_sp->Launch(launch_info, NULL));
Pavel Labath62930e52018-01-10 11:57:31 +0000377 sb_launch_info.set_ref(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 sb_process.SetSP(target_sp->GetProcessSP());
379 } else {
380 error.SetErrorString("SBTarget is invalid");
381 }
382
383 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
384 if (log)
385 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
386 static_cast<void *>(target_sp.get()),
387 static_cast<void *>(sb_process.GetSP().get()));
388
389 return sb_process;
Greg Claytonfbb76342013-11-20 21:07:01 +0000390}
391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
393 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 SBProcess sb_process;
396 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 if (log)
399 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
400 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 if (target_sp) {
403 ProcessAttachInfo &attach_info = sb_attach_info.ref();
404 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
405 PlatformSP platform_sp = target_sp->GetPlatform();
406 // See if we can pre-verify if a process exists or not
407 if (platform_sp && platform_sp->IsConnected()) {
408 lldb::pid_t attach_pid = attach_info.GetProcessID();
409 ProcessInstanceInfo instance_info;
410 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
411 attach_info.SetUserID(instance_info.GetEffectiveUserID());
412 } else {
413 error.ref().SetErrorStringWithFormat(
414 "no process found with process ID %" PRIu64, attach_pid);
415 if (log) {
416 log->Printf("SBTarget(%p)::Attach (...) => error %s",
417 static_cast<void *>(target_sp.get()),
418 error.GetCString());
419 }
420 return sb_process;
Greg Clayton931180e2011-01-27 06:44:37 +0000421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000423 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 error.SetError(AttachToProcess(attach_info, *target_sp));
425 if (error.Success())
426 sb_process.SetSP(target_sp->GetProcessSP());
427 } else {
428 error.SetErrorString("SBTarget is invalid");
429 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 if (log)
432 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
433 static_cast<void *>(target_sp.get()),
434 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437}
438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000440 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 lldb::pid_t pid, // The process ID to attach to
442 SBError &error // An error explaining what went wrong if attach fails
443 ) {
444 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 SBProcess sb_process;
447 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 if (log)
450 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
451 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 if (target_sp) {
454 ProcessAttachInfo attach_info;
455 attach_info.SetProcessID(pid);
456 if (listener.IsValid())
457 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 ProcessInstanceInfo instance_info;
460 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
461 attach_info.SetUserID(instance_info.GetEffectiveUserID());
462
463 error.SetError(AttachToProcess(attach_info, *target_sp));
464 if (error.Success())
465 sb_process.SetSP(target_sp->GetProcessSP());
466 } else
467 error.SetErrorString("SBTarget is invalid");
468
469 if (log)
470 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
471 static_cast<void *>(target_sp.get()), __FUNCTION__,
472 static_cast<void *>(sb_process.GetSP().get()));
473 return sb_process;
474}
475
476lldb::SBProcess SBTarget::AttachToProcessWithName(
477 SBListener &listener,
478 const char *name, // basename of process to attach to
479 bool wait_for, // if true wait for a new instance of "name" to be launched
480 SBError &error // An error explaining what went wrong if attach fails
481 ) {
482 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
483
484 SBProcess sb_process;
485 TargetSP target_sp(GetSP());
486
487 if (log)
488 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
489 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
490 wait_for ? "true" : "false");
491
492 if (name && target_sp) {
493 ProcessAttachInfo attach_info;
494 attach_info.GetExecutableFile().SetFile(name, false);
495 attach_info.SetWaitForLaunch(wait_for);
496 if (listener.IsValid())
497 attach_info.SetListener(listener.GetSP());
498
499 error.SetError(AttachToProcess(attach_info, *target_sp));
500 if (error.Success())
501 sb_process.SetSP(target_sp->GetProcessSP());
502 } else
503 error.SetErrorString("SBTarget is invalid");
504
505 if (log)
506 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
507 static_cast<void *>(target_sp.get()), __FUNCTION__,
508 static_cast<void *>(sb_process.GetSP().get()));
509 return sb_process;
510}
511
512lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
513 const char *plugin_name,
514 SBError &error) {
515 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
516
517 SBProcess sb_process;
518 ProcessSP process_sp;
519 TargetSP target_sp(GetSP());
520
521 if (log)
522 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
523 "plugin_name=%s, error)...",
524 static_cast<void *>(target_sp.get()), url, plugin_name);
525
526 if (target_sp) {
527 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
528 if (listener.IsValid())
529 process_sp =
530 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000531 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 process_sp = target_sp->CreateProcess(
533 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
534
535 if (process_sp) {
536 sb_process.SetSP(process_sp);
537 error.SetError(process_sp->ConnectRemote(NULL, url));
538 } else {
539 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 } else {
542 error.SetErrorString("SBTarget is invalid");
543 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 if (log)
546 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
547 static_cast<void *>(target_sp.get()),
548 static_cast<void *>(process_sp.get()));
549 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000550}
551
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000553
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 SBFileSpec exe_file_spec;
555 TargetSP target_sp(GetSP());
556 if (target_sp) {
557 Module *exe_module = target_sp->GetExecutableModulePointer();
558 if (exe_module)
559 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
560 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
563 if (log) {
564 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
565 static_cast<void *>(target_sp.get()),
566 static_cast<const void *>(exe_file_spec.get()));
567 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572bool SBTarget::operator==(const SBTarget &rhs) const {
573 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574}
575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576bool SBTarget::operator!=(const SBTarget &rhs) const {
577 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578}
579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
581
582void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
583 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000584}
585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
587 lldb::SBAddress sb_addr;
588 Address &addr = sb_addr.ref();
589 TargetSP target_sp(GetSP());
590 if (target_sp) {
591 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
592 if (target_sp->ResolveLoadAddress(vm_addr, addr))
593 return sb_addr;
594 }
595
596 // We have a load address that isn't in a section, just return an address
597 // with the offset filled in (the address) and the section set to NULL
598 addr.SetRawAddress(vm_addr);
599 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
603 lldb::SBAddress sb_addr;
604 Address &addr = sb_addr.ref();
605 TargetSP target_sp(GetSP());
606 if (target_sp) {
607 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
608 if (target_sp->ResolveFileAddress(file_addr, addr))
609 return sb_addr;
610 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 addr.SetRawAddress(file_addr);
613 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000614}
615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
617 lldb::addr_t vm_addr) {
618 lldb::SBAddress sb_addr;
619 Address &addr = sb_addr.ref();
620 TargetSP target_sp(GetSP());
621 if (target_sp) {
622 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
623 if (target_sp->ResolveLoadAddress(vm_addr, addr))
624 return sb_addr;
625 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 // We have a load address that isn't in a section, just return an address
628 // with the offset filled in (the address) and the section set to NULL
629 addr.SetRawAddress(vm_addr);
630 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000631}
632
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000633SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
635 uint32_t resolve_scope) {
636 SBSymbolContext sc;
637 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000638 TargetSP target_sp(GetSP());
639 if (target_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 target_sp->GetImages().ResolveSymbolContextForAddress(
641 addr.ref(), resolve_scope, sc.ref());
642 }
643 return sc;
644}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000645
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
647 lldb::SBError &error) {
648 SBError sb_error;
649 size_t bytes_read = 0;
650 TargetSP target_sp(GetSP());
651 if (target_sp) {
652 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
653 bytes_read =
654 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
655 } else {
656 sb_error.SetErrorString("invalid target");
657 }
658
659 return bytes_read;
660}
661
662SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
663 uint32_t line) {
664 return SBBreakpoint(
665 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000666}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000667
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
670 uint32_t line) {
671 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
674SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
676 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000677 SBFileSpecList empty_list;
678 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
679}
680
681SBBreakpoint
682SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
683 uint32_t line, lldb::addr_t offset,
684 SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000686
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 SBBreakpoint sb_bp;
688 TargetSP target_sp(GetSP());
689 if (target_sp && line != 0) {
690 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000691
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 const LazyBool check_inlines = eLazyBoolCalculate;
693 const LazyBool skip_prologue = eLazyBoolCalculate;
694 const bool internal = false;
695 const bool hardware = false;
696 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000697 const FileSpecList *module_list = nullptr;
698 if (sb_module_list.GetSize() > 0) {
699 module_list = sb_module_list.get();
700 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000701 sb_bp = target_sp->CreateBreakpoint(
Jim Ingham92d19602016-09-20 22:54:49 +0000702 module_list, *sb_file_spec, line, offset, check_inlines, skip_prologue,
703 internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 if (log) {
707 SBStream sstr;
708 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000709 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 sb_file_spec->GetPath(path, sizeof(path));
711 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
712 "SBBreakpoint(%p): %s",
713 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000714 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000716
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 return sb_bp;
718}
719
720SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
721 const char *module_name) {
722 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
723
724 SBBreakpoint sb_bp;
725 TargetSP target_sp(GetSP());
726 if (target_sp.get()) {
727 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
728
729 const bool internal = false;
730 const bool hardware = false;
731 const LazyBool skip_prologue = eLazyBoolCalculate;
732 const lldb::addr_t offset = 0;
733 if (module_name && module_name[0]) {
734 FileSpecList module_spec_list;
735 module_spec_list.Append(FileSpec(module_name, false));
Pavel Labath6ac84032017-02-27 11:05:34 +0000736 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
738 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
739 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000740 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
742 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000743 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 }
745
746 if (log)
747 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
748 "module=\"%s\") => SBBreakpoint(%p)",
749 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000750 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751
752 return sb_bp;
753}
754
755lldb::SBBreakpoint
756SBTarget::BreakpointCreateByName(const char *symbol_name,
757 const SBFileSpecList &module_list,
758 const SBFileSpecList &comp_unit_list) {
759 uint32_t name_type_mask = eFunctionNameTypeAuto;
760 return BreakpointCreateByName(symbol_name, name_type_mask,
761 eLanguageTypeUnknown, module_list,
762 comp_unit_list);
763}
764
765lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
766 const char *symbol_name, uint32_t name_type_mask,
767 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
768 return BreakpointCreateByName(symbol_name, name_type_mask,
769 eLanguageTypeUnknown, module_list,
770 comp_unit_list);
771}
772
773lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
774 const char *symbol_name, uint32_t name_type_mask,
775 LanguageType symbol_language, const SBFileSpecList &module_list,
776 const SBFileSpecList &comp_unit_list) {
777 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
778
779 SBBreakpoint sb_bp;
780 TargetSP target_sp(GetSP());
781 if (target_sp && symbol_name && symbol_name[0]) {
782 const bool internal = false;
783 const bool hardware = false;
784 const LazyBool skip_prologue = eLazyBoolCalculate;
785 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +0000786 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask,
788 symbol_language, 0, skip_prologue, internal, hardware);
789 }
790
791 if (log)
792 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
793 "name_type: %d) => SBBreakpoint(%p)",
794 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000795 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796
797 return sb_bp;
798}
799
800lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
801 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
802 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
803 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
804 eLanguageTypeUnknown, module_list,
805 comp_unit_list);
806}
807
808lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
809 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
810 LanguageType symbol_language, const SBFileSpecList &module_list,
811 const SBFileSpecList &comp_unit_list) {
812 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
813 eLanguageTypeUnknown, 0, module_list,
814 comp_unit_list);
815}
816
817lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
818 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
819 LanguageType symbol_language, lldb::addr_t offset,
820 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
821 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
822
823 SBBreakpoint sb_bp;
824 TargetSP target_sp(GetSP());
825 if (target_sp && num_names > 0) {
826 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
827 const bool internal = false;
828 const bool hardware = false;
829 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +0000830 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 module_list.get(), comp_unit_list.get(), symbol_names, num_names,
832 name_type_mask, symbol_language, offset, skip_prologue, internal,
833 hardware);
834 }
835
836 if (log) {
837 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
838 static_cast<void *>(target_sp.get()));
839 for (uint32_t i = 0; i < num_names; i++) {
840 char sep;
841 if (i < num_names - 1)
842 sep = ',';
843 else
844 sep = '}';
845 if (symbol_names[i] != NULL)
846 log->Printf("\"%s\"%c ", symbol_names[i], sep);
847 else
848 log->Printf("\"<NULL>\"%c ", sep);
849 }
850 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +0000851 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 }
853
854 return sb_bp;
855}
856
857SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
858 const char *module_name) {
859 SBFileSpecList module_spec_list;
860 SBFileSpecList comp_unit_list;
861 if (module_name && module_name[0]) {
862 module_spec_list.Append(FileSpec(module_name, false));
863 }
864 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
865 module_spec_list, comp_unit_list);
866}
867
868lldb::SBBreakpoint
869SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
870 const SBFileSpecList &module_list,
871 const SBFileSpecList &comp_unit_list) {
872 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
873 module_list, comp_unit_list);
874}
875
876lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
877 const char *symbol_name_regex, LanguageType symbol_language,
878 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
879 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
880
881 SBBreakpoint sb_bp;
882 TargetSP target_sp(GetSP());
883 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
884 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000885 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 const bool internal = false;
887 const bool hardware = false;
888 const LazyBool skip_prologue = eLazyBoolCalculate;
889
Pavel Labath6ac84032017-02-27 11:05:34 +0000890 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
892 skip_prologue, internal, hardware);
893 }
894
895 if (log)
896 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
897 "=> SBBreakpoint(%p)",
898 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +0000899 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900
901 return sb_bp;
902}
903
904SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
905 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
906
907 SBBreakpoint sb_bp;
908 TargetSP target_sp(GetSP());
909 if (target_sp) {
910 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
911 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000912 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913 }
914
915 if (log)
916 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
917 ") => SBBreakpoint(%p)",
918 static_cast<void *>(target_sp.get()),
919 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +0000920 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921
922 return sb_bp;
923}
924
925SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
926 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
927
928 SBBreakpoint sb_bp;
929 TargetSP target_sp(GetSP());
930 if (!sb_address.IsValid()) {
931 if (log)
932 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
933 "invalid address",
934 static_cast<void *>(target_sp.get()));
935 return sb_bp;
936 }
937
938 if (target_sp) {
939 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
940 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000941 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942 }
943
944 if (log) {
945 SBStream s;
946 sb_address.GetDescription(s);
947 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
948 "SBBreakpoint(%p)",
949 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +0000950 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 }
952
953 return sb_bp;
954}
955
956lldb::SBBreakpoint
957SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
958 const lldb::SBFileSpec &source_file,
959 const char *module_name) {
960 SBFileSpecList module_spec_list;
961
962 if (module_name && module_name[0]) {
963 module_spec_list.Append(FileSpec(module_name, false));
964 }
965
966 SBFileSpecList source_file_list;
967 if (source_file.IsValid()) {
968 source_file_list.Append(source_file);
969 }
970
971 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
972 source_file_list);
973}
974
975lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
976 const char *source_regex, const SBFileSpecList &module_list,
977 const lldb::SBFileSpecList &source_file_list) {
978 return BreakpointCreateBySourceRegex(source_regex, module_list,
979 source_file_list, SBStringList());
980}
981
982lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
983 const char *source_regex, const SBFileSpecList &module_list,
984 const lldb::SBFileSpecList &source_file_list,
985 const SBStringList &func_names) {
986 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
987
988 SBBreakpoint sb_bp;
989 TargetSP target_sp(GetSP());
990 if (target_sp && source_regex && source_regex[0]) {
991 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
992 const bool hardware = false;
993 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +0000994 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 std::unordered_set<std::string> func_names_set;
996 for (size_t i = 0; i < func_names.GetSize(); i++) {
997 func_names_set.insert(func_names.GetStringAtIndex(i));
998 }
999
Pavel Labath6ac84032017-02-27 11:05:34 +00001000 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 module_list.get(), source_file_list.get(), func_names_set, regexp,
1002 false, hardware, move_to_nearest_code);
1003 }
1004
1005 if (log)
1006 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1007 "=> SBBreakpoint(%p)",
1008 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001009 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010
1011 return sb_bp;
1012}
1013
1014lldb::SBBreakpoint
1015SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1016 bool catch_bp, bool throw_bp) {
1017 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1018
1019 SBBreakpoint sb_bp;
1020 TargetSP target_sp(GetSP());
1021 if (target_sp) {
1022 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1023 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001024 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 hardware);
1026 }
1027
1028 if (log)
1029 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: "
1030 "%s throw: %s) => SBBreakpoint(%p)",
1031 static_cast<void *>(target_sp.get()),
1032 Language::GetNameForLanguageType(language),
1033 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001034 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035
1036 return sb_bp;
1037}
1038
1039uint32_t SBTarget::GetNumBreakpoints() const {
1040 TargetSP target_sp(GetSP());
1041 if (target_sp) {
1042 // The breakpoint list is thread safe, no need to lock
1043 return target_sp->GetBreakpointList().GetSize();
1044 }
1045 return 0;
1046}
1047
1048SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1049 SBBreakpoint sb_breakpoint;
1050 TargetSP target_sp(GetSP());
1051 if (target_sp) {
1052 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001053 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 }
1055 return sb_breakpoint;
1056}
1057
1058bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1059 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1060
1061 bool result = false;
1062 TargetSP target_sp(GetSP());
1063 if (target_sp) {
1064 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1065 result = target_sp->RemoveBreakpointByID(bp_id);
1066 }
1067
1068 if (log)
1069 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1070 static_cast<void *>(target_sp.get()),
1071 static_cast<uint32_t>(bp_id), result);
1072
1073 return result;
1074}
1075
1076SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1077 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1078
1079 SBBreakpoint sb_breakpoint;
1080 TargetSP target_sp(GetSP());
1081 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1082 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001083 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 }
1085
1086 if (log)
1087 log->Printf(
1088 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1089 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001090 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091
1092 return sb_breakpoint;
1093}
1094
Jim Inghamff9a91e2016-09-21 01:21:19 +00001095bool SBTarget::FindBreakpointsByName(const char *name,
1096 SBBreakpointList &bkpts) {
1097 TargetSP target_sp(GetSP());
1098 if (target_sp) {
1099 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1100 BreakpointList bkpt_list(false);
1101 bool is_valid =
1102 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1103 if (!is_valid)
1104 return false;
1105 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1106 bkpts.AppendByID(bkpt_sp->GetID());
1107 }
1108 }
1109 return true;
1110}
1111
Jim Inghamb842f2e2017-09-14 20:22:49 +00001112void SBTarget::GetBreakpointNames(SBStringList &names)
1113{
1114 names.Clear();
1115
1116 TargetSP target_sp(GetSP());
1117 if (target_sp) {
1118 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1119
1120 std::vector<std::string> name_vec;
1121 target_sp->GetBreakpointNames(name_vec);
1122 for (auto name : name_vec)
1123 names.AppendString(name.c_str());
1124 }
1125}
1126
1127void SBTarget::DeleteBreakpointName(const char *name)
1128{
1129 TargetSP target_sp(GetSP());
1130 if (target_sp) {
1131 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001132 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001133 }
1134}
1135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136bool SBTarget::EnableAllBreakpoints() {
1137 TargetSP target_sp(GetSP());
1138 if (target_sp) {
1139 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001140 target_sp->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 return true;
1142 }
1143 return false;
1144}
1145
1146bool SBTarget::DisableAllBreakpoints() {
1147 TargetSP target_sp(GetSP());
1148 if (target_sp) {
1149 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001150 target_sp->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 return true;
1152 }
1153 return false;
1154}
1155
1156bool SBTarget::DeleteAllBreakpoints() {
1157 TargetSP target_sp(GetSP());
1158 if (target_sp) {
1159 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001160 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 return true;
1162 }
1163 return false;
1164}
1165
Jim Ingham01f16662016-09-14 19:07:35 +00001166lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1167 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001168 SBStringList empty_name_list;
1169 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1170}
1171
1172lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1173 SBStringList &matching_names,
1174 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001175 SBError sberr;
1176 TargetSP target_sp(GetSP());
1177 if (!target_sp) {
1178 sberr.SetErrorString(
1179 "BreakpointCreateFromFile called with invalid target.");
1180 return sberr;
1181 }
1182 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1183
1184 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001185
1186 std::vector<std::string> name_vector;
1187 size_t num_names = matching_names.GetSize();
1188 for (size_t i = 0; i < num_names; i++)
1189 name_vector.push_back(matching_names.GetStringAtIndex(i));
1190
1191 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1192 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001193 if (sberr.Fail())
1194 return sberr;
1195
1196 size_t num_bkpts = bp_ids.GetSize();
1197 for (size_t i = 0; i < num_bkpts; i++) {
1198 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1199 new_bps.AppendByID(bp_id.GetBreakpointID());
1200 }
1201 return sberr;
1202}
1203
1204lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1205 SBError sberr;
1206 TargetSP target_sp(GetSP());
1207 if (!target_sp) {
1208 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1209 return sberr;
1210 }
1211 SBBreakpointList bkpt_list(*this);
1212 return BreakpointsWriteToFile(dest_file, bkpt_list);
1213}
1214
1215lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001216 SBBreakpointList &bkpt_list,
1217 bool append) {
Jim Ingham01f16662016-09-14 19:07:35 +00001218 SBError sberr;
1219 TargetSP target_sp(GetSP());
1220 if (!target_sp) {
1221 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1222 return sberr;
1223 }
1224
1225 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1226 BreakpointIDList bp_id_list;
1227 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001228 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1229 bp_id_list, append);
Jim Ingham01f16662016-09-14 19:07:35 +00001230 return sberr;
1231}
1232
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233uint32_t SBTarget::GetNumWatchpoints() const {
1234 TargetSP target_sp(GetSP());
1235 if (target_sp) {
1236 // The watchpoint list is thread safe, no need to lock
1237 return target_sp->GetWatchpointList().GetSize();
1238 }
1239 return 0;
1240}
1241
1242SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1243 SBWatchpoint sb_watchpoint;
1244 TargetSP target_sp(GetSP());
1245 if (target_sp) {
1246 // The watchpoint list is thread safe, no need to lock
1247 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1248 }
1249 return sb_watchpoint;
1250}
1251
1252bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1253 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1254
1255 bool result = false;
1256 TargetSP target_sp(GetSP());
1257 if (target_sp) {
1258 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1259 std::unique_lock<std::recursive_mutex> lock;
1260 target_sp->GetWatchpointList().GetListMutex(lock);
1261 result = target_sp->RemoveWatchpointByID(wp_id);
1262 }
1263
1264 if (log)
1265 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1266 static_cast<void *>(target_sp.get()),
1267 static_cast<uint32_t>(wp_id), result);
1268
1269 return result;
1270}
1271
1272SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1273 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1274
1275 SBWatchpoint sb_watchpoint;
1276 lldb::WatchpointSP watchpoint_sp;
1277 TargetSP target_sp(GetSP());
1278 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1279 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1280 std::unique_lock<std::recursive_mutex> lock;
1281 target_sp->GetWatchpointList().GetListMutex(lock);
1282 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1283 sb_watchpoint.SetSP(watchpoint_sp);
1284 }
1285
1286 if (log)
1287 log->Printf(
1288 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1289 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1290 static_cast<void *>(watchpoint_sp.get()));
1291
1292 return sb_watchpoint;
1293}
1294
1295lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1296 bool read, bool write,
1297 SBError &error) {
1298 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1299
1300 SBWatchpoint sb_watchpoint;
1301 lldb::WatchpointSP watchpoint_sp;
1302 TargetSP target_sp(GetSP());
1303 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1304 size > 0) {
1305 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1306 uint32_t watch_type = 0;
1307 if (read)
1308 watch_type |= LLDB_WATCH_TYPE_READ;
1309 if (write)
1310 watch_type |= LLDB_WATCH_TYPE_WRITE;
1311 if (watch_type == 0) {
1312 error.SetErrorString(
1313 "Can't create a watchpoint that is neither read nor write.");
1314 return sb_watchpoint;
1315 }
1316
1317 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001318 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 // This API doesn't take in a type, so we can't figure out what it is.
1320 CompilerType *type = NULL;
1321 watchpoint_sp =
1322 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1323 error.SetError(cw_error);
1324 sb_watchpoint.SetSP(watchpoint_sp);
1325 }
1326
1327 if (log)
1328 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1329 ", 0x%u) => SBWatchpoint(%p)",
1330 static_cast<void *>(target_sp.get()), addr,
1331 static_cast<uint32_t>(size),
1332 static_cast<void *>(watchpoint_sp.get()));
1333
1334 return sb_watchpoint;
1335}
1336
1337bool SBTarget::EnableAllWatchpoints() {
1338 TargetSP target_sp(GetSP());
1339 if (target_sp) {
1340 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1341 std::unique_lock<std::recursive_mutex> lock;
1342 target_sp->GetWatchpointList().GetListMutex(lock);
1343 target_sp->EnableAllWatchpoints();
1344 return true;
1345 }
1346 return false;
1347}
1348
1349bool SBTarget::DisableAllWatchpoints() {
1350 TargetSP target_sp(GetSP());
1351 if (target_sp) {
1352 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1353 std::unique_lock<std::recursive_mutex> lock;
1354 target_sp->GetWatchpointList().GetListMutex(lock);
1355 target_sp->DisableAllWatchpoints();
1356 return true;
1357 }
1358 return false;
1359}
1360
1361SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1362 SBType type) {
1363 SBValue sb_value;
1364 lldb::ValueObjectSP new_value_sp;
1365 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1366 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1367 ExecutionContext exe_ctx(
1368 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1369 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1370 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1371 exe_ctx, ast_type);
1372 }
1373 sb_value.SetSP(new_value_sp);
1374 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1375 if (log) {
1376 if (new_value_sp)
1377 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1378 static_cast<void *>(m_opaque_sp.get()),
1379 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001380 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1382 static_cast<void *>(m_opaque_sp.get()));
1383 }
1384 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001385}
1386
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1388 lldb::SBType type) {
1389 SBValue sb_value;
1390 lldb::ValueObjectSP new_value_sp;
1391 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1392 DataExtractorSP extractor(*data);
1393 ExecutionContext exe_ctx(
1394 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1395 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1396 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1397 exe_ctx, ast_type);
1398 }
1399 sb_value.SetSP(new_value_sp);
1400 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1401 if (log) {
1402 if (new_value_sp)
1403 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1404 static_cast<void *>(m_opaque_sp.get()),
1405 new_value_sp->GetName().AsCString());
1406 else
1407 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1408 static_cast<void *>(m_opaque_sp.get()));
1409 }
1410 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001411}
1412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1414 const char *expr) {
1415 SBValue sb_value;
1416 lldb::ValueObjectSP new_value_sp;
1417 if (IsValid() && name && *name && expr && *expr) {
1418 ExecutionContext exe_ctx(
1419 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1420 new_value_sp =
1421 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1422 }
1423 sb_value.SetSP(new_value_sp);
1424 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1425 if (log) {
1426 if (new_value_sp)
1427 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1428 static_cast<void *>(m_opaque_sp.get()),
1429 new_value_sp->GetName().AsCString());
1430 else
1431 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1432 static_cast<void *>(m_opaque_sp.get()));
1433 }
1434 return sb_value;
1435}
1436
1437bool SBTarget::DeleteAllWatchpoints() {
1438 TargetSP target_sp(GetSP());
1439 if (target_sp) {
1440 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1441 std::unique_lock<std::recursive_mutex> lock;
1442 target_sp->GetWatchpointList().GetListMutex(lock);
1443 target_sp->RemoveAllWatchpoints();
1444 return true;
1445 }
1446 return false;
1447}
1448
1449lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1450 const char *uuid_cstr) {
1451 return AddModule(path, triple, uuid_cstr, NULL);
1452}
1453
1454lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1455 const char *uuid_cstr, const char *symfile) {
1456 lldb::SBModule sb_module;
1457 TargetSP target_sp(GetSP());
1458 if (target_sp) {
1459 ModuleSpec module_spec;
1460 if (path)
1461 module_spec.GetFileSpec().SetFile(path, false);
1462
1463 if (uuid_cstr)
1464 module_spec.GetUUID().SetFromCString(uuid_cstr);
1465
1466 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001467 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1468 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469 else
1470 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1471
1472 if (symfile)
1473 module_spec.GetSymbolFileSpec().SetFile(symfile, false);
1474
1475 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1476 }
1477 return sb_module;
1478}
1479
1480lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1481 lldb::SBModule sb_module;
1482 TargetSP target_sp(GetSP());
1483 if (target_sp)
1484 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1485 return sb_module;
1486}
1487
1488bool SBTarget::AddModule(lldb::SBModule &module) {
1489 TargetSP target_sp(GetSP());
1490 if (target_sp) {
1491 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1492 return true;
1493 }
1494 return false;
1495}
1496
1497uint32_t SBTarget::GetNumModules() const {
1498 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1499
1500 uint32_t num = 0;
1501 TargetSP target_sp(GetSP());
1502 if (target_sp) {
1503 // The module list is thread safe, no need to lock
1504 num = target_sp->GetImages().GetSize();
1505 }
1506
1507 if (log)
1508 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1509 static_cast<void *>(target_sp.get()), num);
1510
1511 return num;
1512}
1513
1514void SBTarget::Clear() {
1515 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1516
1517 if (log)
1518 log->Printf("SBTarget(%p)::Clear ()",
1519 static_cast<void *>(m_opaque_sp.get()));
1520
1521 m_opaque_sp.reset();
1522}
1523
1524SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1525 SBModule sb_module;
1526 TargetSP target_sp(GetSP());
1527 if (target_sp && sb_file_spec.IsValid()) {
1528 ModuleSpec module_spec(*sb_file_spec);
1529 // The module list is thread safe, no need to lock
1530 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1531 }
1532 return sb_module;
1533}
1534
1535lldb::ByteOrder SBTarget::GetByteOrder() {
1536 TargetSP target_sp(GetSP());
1537 if (target_sp)
1538 return target_sp->GetArchitecture().GetByteOrder();
1539 return eByteOrderInvalid;
1540}
1541
1542const char *SBTarget::GetTriple() {
1543 TargetSP target_sp(GetSP());
1544 if (target_sp) {
1545 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001546 // Unique the string so we don't run into ownership issues since the const
1547 // strings put the string into the string pool once and the strings never
1548 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001549 ConstString const_triple(triple.c_str());
1550 return const_triple.GetCString();
1551 }
1552 return NULL;
1553}
1554
1555uint32_t SBTarget::GetDataByteSize() {
1556 TargetSP target_sp(GetSP());
1557 if (target_sp) {
1558 return target_sp->GetArchitecture().GetDataByteSize();
1559 }
1560 return 0;
1561}
1562
1563uint32_t SBTarget::GetCodeByteSize() {
1564 TargetSP target_sp(GetSP());
1565 if (target_sp) {
1566 return target_sp->GetArchitecture().GetCodeByteSize();
1567 }
1568 return 0;
1569}
1570
1571uint32_t SBTarget::GetAddressByteSize() {
1572 TargetSP target_sp(GetSP());
1573 if (target_sp)
1574 return target_sp->GetArchitecture().GetAddressByteSize();
1575 return sizeof(void *);
1576}
1577
1578SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1579 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1580
1581 SBModule sb_module;
1582 ModuleSP module_sp;
1583 TargetSP target_sp(GetSP());
1584 if (target_sp) {
1585 // The module list is thread safe, no need to lock
1586 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1587 sb_module.SetSP(module_sp);
1588 }
1589
1590 if (log)
1591 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1592 static_cast<void *>(target_sp.get()), idx,
1593 static_cast<void *>(module_sp.get()));
1594
1595 return sb_module;
1596}
1597
1598bool SBTarget::RemoveModule(lldb::SBModule module) {
1599 TargetSP target_sp(GetSP());
1600 if (target_sp)
1601 return target_sp->GetImages().Remove(module.GetSP());
1602 return false;
1603}
1604
1605SBBroadcaster SBTarget::GetBroadcaster() const {
1606 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1607
1608 TargetSP target_sp(GetSP());
1609 SBBroadcaster broadcaster(target_sp.get(), false);
1610
1611 if (log)
1612 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1613 static_cast<void *>(target_sp.get()),
1614 static_cast<void *>(broadcaster.get()));
1615
1616 return broadcaster;
1617}
1618
1619bool SBTarget::GetDescription(SBStream &description,
1620 lldb::DescriptionLevel description_level) {
1621 Stream &strm = description.ref();
1622
1623 TargetSP target_sp(GetSP());
1624 if (target_sp) {
1625 target_sp->Dump(&strm, description_level);
1626 } else
1627 strm.PutCString("No value");
1628
1629 return true;
1630}
1631
1632lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1633 uint32_t name_type_mask) {
1634 lldb::SBSymbolContextList sb_sc_list;
1635 if (name && name[0]) {
Jim Ingham763b2b22015-07-07 22:12:17 +00001636 TargetSP target_sp(GetSP());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 if (target_sp) {
1638 const bool symbols_ok = true;
1639 const bool inlines_ok = true;
1640 const bool append = true;
1641 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask,
1642 symbols_ok, inlines_ok, append,
1643 *sb_sc_list);
1644 }
1645 }
1646 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001647}
Greg Claytone14e1922012-12-04 02:22:16 +00001648
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1650 uint32_t max_matches,
1651 MatchType matchtype) {
1652 lldb::SBSymbolContextList sb_sc_list;
1653 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001654 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001655 TargetSP target_sp(GetSP());
1656 if (target_sp) {
1657 std::string regexstr;
1658 switch (matchtype) {
1659 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001660 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661 true, true, *sb_sc_list);
1662 break;
1663 case eMatchTypeStartsWith:
1664 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001665 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1666 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 break;
1668 default:
1669 target_sp->GetImages().FindFunctions(ConstString(name),
1670 eFunctionNameTypeAny, true, true,
1671 true, *sb_sc_list);
1672 break;
1673 }
1674 }
1675 }
1676 return sb_sc_list;
1677}
1678
1679lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1680 TargetSP target_sp(GetSP());
1681 if (typename_cstr && typename_cstr[0] && target_sp) {
1682 ConstString const_typename(typename_cstr);
1683 SymbolContext sc;
1684 const bool exact_match = false;
1685
1686 const ModuleList &module_list = target_sp->GetImages();
1687 size_t count = module_list.GetSize();
1688 for (size_t idx = 0; idx < count; idx++) {
1689 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1690 if (module_sp) {
1691 TypeSP type_sp(
1692 module_sp->FindFirstType(sc, const_typename, exact_match));
1693 if (type_sp)
1694 return SBType(type_sp);
1695 }
1696 }
1697
Adrian Prantl05097242018-04-30 16:49:04 +00001698 // Didn't find the type in the symbols; try the Objective-C runtime if one
1699 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700
1701 ProcessSP process_sp(target_sp->GetProcessSP());
1702
1703 if (process_sp) {
1704 ObjCLanguageRuntime *objc_language_runtime =
1705 process_sp->GetObjCLanguageRuntime();
1706
1707 if (objc_language_runtime) {
1708 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1709
1710 if (objc_decl_vendor) {
1711 std::vector<clang::NamedDecl *> decls;
1712
1713 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1714 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1715 return SBType(type);
1716 }
1717 }
1718 }
1719 }
1720 }
1721
1722 // No matches, search for basic typename matches
1723 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1724 if (clang_ast)
1725 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1726 const_typename));
1727 }
1728 return SBType();
1729}
1730
1731SBType SBTarget::GetBasicType(lldb::BasicType type) {
1732 TargetSP target_sp(GetSP());
1733 if (target_sp) {
1734 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1735 if (clang_ast)
1736 return SBType(
1737 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1738 }
1739 return SBType();
1740}
1741
1742lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1743 SBTypeList sb_type_list;
1744 TargetSP target_sp(GetSP());
1745 if (typename_cstr && typename_cstr[0] && target_sp) {
1746 ModuleList &images = target_sp->GetImages();
1747 ConstString const_typename(typename_cstr);
1748 bool exact_match = false;
1749 SymbolContext sc;
1750 TypeList type_list;
1751 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1752 uint32_t num_matches =
1753 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX,
1754 searched_symbol_files, type_list);
1755
1756 if (num_matches > 0) {
1757 for (size_t idx = 0; idx < num_matches; idx++) {
1758 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1759 if (type_sp)
1760 sb_type_list.Append(SBType(type_sp));
1761 }
1762 }
1763
1764 // Try the Objective-C runtime if one is installed
1765
1766 ProcessSP process_sp(target_sp->GetProcessSP());
1767
1768 if (process_sp) {
1769 ObjCLanguageRuntime *objc_language_runtime =
1770 process_sp->GetObjCLanguageRuntime();
1771
1772 if (objc_language_runtime) {
1773 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1774
1775 if (objc_decl_vendor) {
1776 std::vector<clang::NamedDecl *> decls;
1777
1778 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1779 for (clang::NamedDecl *decl : decls) {
1780 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1781 sb_type_list.Append(SBType(type));
1782 }
1783 }
1784 }
1785 }
1786 }
1787 }
1788
1789 if (sb_type_list.GetSize() == 0) {
1790 // No matches, search for basic typename matches
1791 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1792 if (clang_ast)
1793 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1794 clang_ast->getASTContext(), const_typename)));
1795 }
1796 }
1797 return sb_type_list;
1798}
1799
1800SBValueList SBTarget::FindGlobalVariables(const char *name,
1801 uint32_t max_matches) {
1802 SBValueList sb_value_list;
1803
1804 TargetSP target_sp(GetSP());
1805 if (name && target_sp) {
1806 VariableList variable_list;
1807 const bool append = true;
1808 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
1809 ConstString(name), append, max_matches, variable_list);
1810
1811 if (match_count > 0) {
1812 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1813 if (exe_scope == NULL)
1814 exe_scope = target_sp.get();
1815 for (uint32_t i = 0; i < match_count; ++i) {
1816 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1817 exe_scope, variable_list.GetVariableAtIndex(i)));
1818 if (valobj_sp)
1819 sb_value_list.Append(SBValue(valobj_sp));
1820 }
1821 }
1822 }
1823
1824 return sb_value_list;
1825}
1826
1827SBValueList SBTarget::FindGlobalVariables(const char *name,
1828 uint32_t max_matches,
1829 MatchType matchtype) {
1830 SBValueList sb_value_list;
1831
1832 TargetSP target_sp(GetSP());
1833 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001834 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835 VariableList variable_list;
1836 const bool append = true;
1837
1838 std::string regexstr;
1839 uint32_t match_count;
1840 switch (matchtype) {
1841 case eMatchTypeNormal:
1842 match_count = target_sp->GetImages().FindGlobalVariables(
1843 ConstString(name), append, max_matches, variable_list);
1844 break;
1845 case eMatchTypeRegex:
1846 match_count = target_sp->GetImages().FindGlobalVariables(
Zachary Turner95eae422016-09-21 16:01:28 +00001847 RegularExpression(name_ref), append, max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 break;
1849 case eMatchTypeStartsWith:
1850 regexstr = llvm::Regex::escape(name) + ".*";
1851 match_count = target_sp->GetImages().FindGlobalVariables(
Zachary Turner95eae422016-09-21 16:01:28 +00001852 RegularExpression(regexstr), append, max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853 break;
1854 }
1855
1856 if (match_count > 0) {
1857 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1858 if (exe_scope == NULL)
1859 exe_scope = target_sp.get();
1860 for (uint32_t i = 0; i < match_count; ++i) {
1861 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1862 exe_scope, variable_list.GetVariableAtIndex(i)));
1863 if (valobj_sp)
1864 sb_value_list.Append(SBValue(valobj_sp));
1865 }
1866 }
1867 }
1868
1869 return sb_value_list;
1870}
1871
1872lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1873 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1874 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1875 return sb_value_list.GetValueAtIndex(0);
1876 return SBValue();
1877}
1878
1879SBSourceManager SBTarget::GetSourceManager() {
1880 SBSourceManager source_manager(*this);
1881 return source_manager;
1882}
1883
1884lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1885 uint32_t count) {
1886 return ReadInstructions(base_addr, count, NULL);
1887}
1888
1889lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1890 uint32_t count,
1891 const char *flavor_string) {
1892 SBInstructionList sb_instructions;
1893
1894 TargetSP target_sp(GetSP());
1895 if (target_sp) {
1896 Address *addr_ptr = base_addr.get();
1897
1898 if (addr_ptr) {
1899 DataBufferHeap data(
1900 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1901 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00001902 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1904 const size_t bytes_read =
1905 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
1906 data.GetByteSize(), error, &load_addr);
1907 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1908 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1909 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
1910 data.GetBytes(), bytes_read, count, data_from_file));
1911 }
1912 }
1913
1914 return sb_instructions;
1915}
1916
1917lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
1918 const void *buf,
1919 size_t size) {
1920 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
1921}
1922
1923lldb::SBInstructionList
1924SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
1925 const char *flavor_string, const void *buf,
1926 size_t size) {
1927 SBInstructionList sb_instructions;
1928
1929 TargetSP target_sp(GetSP());
1930 if (target_sp) {
1931 Address addr;
1932
1933 if (base_addr.get())
1934 addr = *base_addr.get();
1935
1936 const bool data_from_file = true;
1937
1938 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1939 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
1940 UINT32_MAX, data_from_file));
1941 }
1942
1943 return sb_instructions;
1944}
1945
1946lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
1947 const void *buf,
1948 size_t size) {
1949 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
1950 size);
1951}
1952
1953lldb::SBInstructionList
1954SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
1955 const char *flavor_string, const void *buf,
1956 size_t size) {
1957 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
1958 buf, size);
1959}
1960
1961SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
1962 lldb::addr_t section_base_addr) {
1963 SBError sb_error;
1964 TargetSP target_sp(GetSP());
1965 if (target_sp) {
1966 if (!section.IsValid()) {
1967 sb_error.SetErrorStringWithFormat("invalid section");
1968 } else {
1969 SectionSP section_sp(section.GetSP());
1970 if (section_sp) {
1971 if (section_sp->IsThreadSpecific()) {
1972 sb_error.SetErrorString(
1973 "thread specific sections are not yet supported");
1974 } else {
1975 ProcessSP process_sp(target_sp->GetProcessSP());
1976 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
1977 ModuleSP module_sp(section_sp->GetModule());
1978 if (module_sp) {
1979 ModuleList module_list;
1980 module_list.Append(module_sp);
1981 target_sp->ModulesDidLoad(module_list);
1982 }
1983 // Flush info in the process (stack frames, etc)
1984 if (process_sp)
1985 process_sp->Flush();
1986 }
1987 }
1988 }
1989 }
1990 } else {
1991 sb_error.SetErrorString("invalid target");
1992 }
1993 return sb_error;
1994}
1995
1996SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
1997 SBError sb_error;
1998
1999 TargetSP target_sp(GetSP());
2000 if (target_sp) {
2001 if (!section.IsValid()) {
2002 sb_error.SetErrorStringWithFormat("invalid section");
2003 } else {
2004 SectionSP section_sp(section.GetSP());
2005 if (section_sp) {
2006 ProcessSP process_sp(target_sp->GetProcessSP());
2007 if (target_sp->SetSectionUnloaded(section_sp)) {
2008 ModuleSP module_sp(section_sp->GetModule());
2009 if (module_sp) {
2010 ModuleList module_list;
2011 module_list.Append(module_sp);
2012 target_sp->ModulesDidUnload(module_list, false);
2013 }
2014 // Flush info in the process (stack frames, etc)
2015 if (process_sp)
2016 process_sp->Flush();
2017 }
2018 } else {
2019 sb_error.SetErrorStringWithFormat("invalid section");
2020 }
2021 }
2022 } else {
2023 sb_error.SetErrorStringWithFormat("invalid target");
2024 }
2025 return sb_error;
2026}
2027
2028SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2029 int64_t slide_offset) {
2030 SBError sb_error;
2031
2032 TargetSP target_sp(GetSP());
2033 if (target_sp) {
2034 ModuleSP module_sp(module.GetSP());
2035 if (module_sp) {
2036 bool changed = false;
2037 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2038 // The load was successful, make sure that at least some sections
2039 // changed before we notify that our module was loaded.
2040 if (changed) {
2041 ModuleList module_list;
2042 module_list.Append(module_sp);
2043 target_sp->ModulesDidLoad(module_list);
2044 // Flush info in the process (stack frames, etc)
2045 ProcessSP process_sp(target_sp->GetProcessSP());
2046 if (process_sp)
2047 process_sp->Flush();
2048 }
2049 }
2050 } else {
2051 sb_error.SetErrorStringWithFormat("invalid module");
2052 }
2053
2054 } else {
2055 sb_error.SetErrorStringWithFormat("invalid target");
2056 }
2057 return sb_error;
2058}
2059
2060SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2061 SBError sb_error;
2062
2063 char path[PATH_MAX];
2064 TargetSP target_sp(GetSP());
2065 if (target_sp) {
2066 ModuleSP module_sp(module.GetSP());
2067 if (module_sp) {
2068 ObjectFile *objfile = module_sp->GetObjectFile();
2069 if (objfile) {
2070 SectionList *section_list = objfile->GetSectionList();
2071 if (section_list) {
2072 ProcessSP process_sp(target_sp->GetProcessSP());
2073
2074 bool changed = false;
2075 const size_t num_sections = section_list->GetSize();
2076 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2077 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2078 if (section_sp)
2079 changed |= target_sp->SetSectionUnloaded(section_sp);
2080 }
2081 if (changed) {
2082 ModuleList module_list;
2083 module_list.Append(module_sp);
2084 target_sp->ModulesDidUnload(module_list, false);
2085 // Flush info in the process (stack frames, etc)
2086 ProcessSP process_sp(target_sp->GetProcessSP());
2087 if (process_sp)
2088 process_sp->Flush();
2089 }
2090 } else {
2091 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2092 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2093 path);
2094 }
2095 } else {
2096 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2097 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2098 path);
2099 }
2100 } else {
2101 sb_error.SetErrorStringWithFormat("invalid module");
2102 }
2103 } else {
2104 sb_error.SetErrorStringWithFormat("invalid target");
2105 }
2106 return sb_error;
2107}
2108
2109lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2110 lldb::SymbolType symbol_type) {
2111 SBSymbolContextList sb_sc_list;
2112 if (name && name[0]) {
2113 TargetSP target_sp(GetSP());
2114 if (target_sp) {
2115 bool append = true;
2116 target_sp->GetImages().FindSymbolsWithNameAndType(
2117 ConstString(name), symbol_type, *sb_sc_list, append);
2118 }
2119 }
2120 return sb_sc_list;
2121}
2122
2123lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2124 TargetSP target_sp(GetSP());
2125 if (!target_sp)
2126 return SBValue();
2127
2128 SBExpressionOptions options;
2129 lldb::DynamicValueType fetch_dynamic_value =
2130 target_sp->GetPreferDynamicValue();
2131 options.SetFetchDynamicValue(fetch_dynamic_value);
2132 options.SetUnwindOnError(true);
2133 return EvaluateExpression(expr, options);
2134}
2135
2136lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2137 const SBExpressionOptions &options) {
2138 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002139#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002140 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002141#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002142 SBValue expr_result;
2143 ExpressionResults exe_results = eExpressionSetupError;
2144 ValueObjectSP expr_value_sp;
2145 TargetSP target_sp(GetSP());
2146 StackFrame *frame = NULL;
2147 if (target_sp) {
2148 if (expr == NULL || expr[0] == '\0') {
2149 if (log)
2150 log->Printf(
2151 "SBTarget::EvaluateExpression called with an empty expression");
2152 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002153 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002154
2155 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2156 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002157
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002158 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2160
2161 frame = exe_ctx.GetFramePtr();
2162 Target *target = exe_ctx.GetTargetPtr();
2163
2164 if (target) {
2165#ifdef LLDB_CONFIGURATION_DEBUG
2166 StreamString frame_description;
2167 if (frame)
2168 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002169 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002170 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2171 "%u) %s",
2172 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002173 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174#endif
2175 exe_results =
2176 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2177
2178 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002179 } else {
2180 if (log)
2181 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2182 "reconstruct frame object for this SBTarget.");
2183 }
2184 }
2185#ifndef LLDB_DISABLE_PYTHON
2186 if (expr_log)
2187 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2188 "%s, summary %s **",
2189 expr_result.GetValue(), expr_result.GetSummary());
2190
2191 if (log)
2192 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2193 "(execution result=%d)",
2194 static_cast<void *>(frame), expr,
2195 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002196#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002197
Kate Stoneb9c1b512016-09-06 20:57:50 +00002198 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002199}
2200
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201lldb::addr_t SBTarget::GetStackRedZoneSize() {
2202 TargetSP target_sp(GetSP());
2203 if (target_sp) {
2204 ABISP abi_sp;
2205 ProcessSP process_sp(target_sp->GetProcessSP());
2206 if (process_sp)
2207 abi_sp = process_sp->GetABI();
2208 else
Jason Molenda43294c92017-06-29 02:57:03 +00002209 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002210 if (abi_sp)
2211 return abi_sp->GetRedZoneSize();
2212 }
2213 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002214}
Ilia K8f37ca52015-02-13 14:31:06 +00002215
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2217 lldb::SBLaunchInfo launch_info(NULL);
2218 TargetSP target_sp(GetSP());
2219 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002220 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002222}
2223
Kate Stoneb9c1b512016-09-06 20:57:50 +00002224void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2225 TargetSP target_sp(GetSP());
2226 if (target_sp)
2227 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002228}