blob: a265e64ec07748549625c8c78e6af1a80fbaba85 [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) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000206 lldb::SBError error; // Ignored
207 return LoadCore(core_file, error);
208}
209
210SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 SBProcess sb_process;
212 TargetSP target_sp(GetSP());
213 if (target_sp) {
214 FileSpec filespec(core_file, true);
215 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000216 target_sp->GetDebugger().GetListener(), "", &filespec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 if (process_sp) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000218 error.SetError(process_sp->LoadCore());
219 if (error.Success())
220 sb_process.SetSP(process_sp);
221 } else {
222 error.SetErrorString("Failed to create the process");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 }
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000224 } else {
225 error.SetErrorString("SBTarget is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 }
227 return sb_process;
228}
229
230SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
231 const char *working_directory) {
232 char *stdin_path = NULL;
233 char *stdout_path = NULL;
234 char *stderr_path = NULL;
235 uint32_t launch_flags = 0;
236 bool stop_at_entry = false;
237 SBError error;
238 SBListener listener = GetDebugger().GetListener();
239 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path,
240 working_directory, launch_flags, stop_at_entry, error);
241}
242
243SBError SBTarget::Install() {
244 SBError sb_error;
245 TargetSP target_sp(GetSP());
246 if (target_sp) {
247 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
248 sb_error.ref() = target_sp->Install(NULL);
249 }
250 return sb_error;
251}
252
253SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
254 char const **envp, const char *stdin_path,
255 const char *stdout_path, const char *stderr_path,
256 const char *working_directory,
257 uint32_t launch_flags, // See LaunchFlags
258 bool stop_at_entry, lldb::SBError &error) {
259 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
260
261 SBProcess sb_process;
262 ProcessSP process_sp;
263 TargetSP target_sp(GetSP());
264
265 if (log)
266 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
267 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
268 "stop_at_entry=%i, &error (%p))...",
269 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
270 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
271 stdout_path ? stdout_path : "NULL",
272 stderr_path ? stderr_path : "NULL",
273 working_directory ? working_directory : "NULL", launch_flags,
274 stop_at_entry, static_cast<void *>(error.get()));
275
276 if (target_sp) {
277 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
278
279 if (stop_at_entry)
280 launch_flags |= eLaunchFlagStopAtEntry;
281
282 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
283 launch_flags |= eLaunchFlagDisableASLR;
284
285 StateType state = eStateInvalid;
286 process_sp = target_sp->GetProcessSP();
287 if (process_sp) {
288 state = process_sp->GetState();
289
290 if (process_sp->IsAlive() && state != eStateConnected) {
291 if (state == eStateAttaching)
292 error.SetErrorString("process attach is in progress");
293 else
294 error.SetErrorString("a process is already being debugged");
295 return sb_process;
296 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000297 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 if (state == eStateConnected) {
300 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +0000301 // listener, so if a valid listener is supplied, we need to error out to
302 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 if (listener.IsValid()) {
304 error.SetErrorString("process is connected and already has a listener, "
305 "pass empty listener");
306 return sb_process;
307 }
308 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
311 launch_flags |= eLaunchFlagDisableSTDIO;
312
313 ProcessLaunchInfo launch_info(
314 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
315 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
316 launch_flags);
317
318 Module *exe_module = target_sp->GetExecutableModulePointer();
319 if (exe_module)
320 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
321 if (argv)
322 launch_info.GetArguments().AppendArguments(argv);
323 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000324 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325
326 if (listener.IsValid())
327 launch_info.SetListener(listener.GetSP());
328
329 error.SetError(target_sp->Launch(launch_info, NULL));
330
331 sb_process.SetSP(target_sp->GetProcessSP());
332 } else {
333 error.SetErrorString("SBTarget is invalid");
334 }
335
336 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
337 if (log)
338 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
339 static_cast<void *>(target_sp.get()),
340 static_cast<void *>(sb_process.GetSP().get()),
341 error.GetCString());
342
343 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
347 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 SBProcess sb_process;
350 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 if (log)
353 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
354 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 if (target_sp) {
357 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
358 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000359 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 ProcessSP process_sp = target_sp->GetProcessSP();
361 if (process_sp) {
362 state = process_sp->GetState();
363
364 if (process_sp->IsAlive() && state != eStateConnected) {
365 if (state == eStateAttaching)
366 error.SetErrorString("process attach is in progress");
367 else
368 error.SetErrorString("a process is already being debugged");
369 return sb_process;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000372 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000373
Pavel Labath62930e52018-01-10 11:57:31 +0000374 lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 if (!launch_info.GetExecutableFile()) {
377 Module *exe_module = target_sp->GetExecutableModulePointer();
378 if (exe_module)
379 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000380 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381
382 const ArchSpec &arch_spec = target_sp->GetArchitecture();
383 if (arch_spec.IsValid())
384 launch_info.GetArchitecture() = arch_spec;
385
386 error.SetError(target_sp->Launch(launch_info, NULL));
Pavel Labath62930e52018-01-10 11:57:31 +0000387 sb_launch_info.set_ref(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 sb_process.SetSP(target_sp->GetProcessSP());
389 } else {
390 error.SetErrorString("SBTarget is invalid");
391 }
392
393 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
394 if (log)
395 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
396 static_cast<void *>(target_sp.get()),
397 static_cast<void *>(sb_process.GetSP().get()));
398
399 return sb_process;
Greg Claytonfbb76342013-11-20 21:07:01 +0000400}
401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
403 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 SBProcess sb_process;
406 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 if (log)
409 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
410 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 if (target_sp) {
413 ProcessAttachInfo &attach_info = sb_attach_info.ref();
414 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
415 PlatformSP platform_sp = target_sp->GetPlatform();
416 // See if we can pre-verify if a process exists or not
417 if (platform_sp && platform_sp->IsConnected()) {
418 lldb::pid_t attach_pid = attach_info.GetProcessID();
419 ProcessInstanceInfo instance_info;
420 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
421 attach_info.SetUserID(instance_info.GetEffectiveUserID());
422 } else {
423 error.ref().SetErrorStringWithFormat(
424 "no process found with process ID %" PRIu64, attach_pid);
425 if (log) {
426 log->Printf("SBTarget(%p)::Attach (...) => error %s",
427 static_cast<void *>(target_sp.get()),
428 error.GetCString());
429 }
430 return sb_process;
Greg Clayton931180e2011-01-27 06:44:37 +0000431 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 error.SetError(AttachToProcess(attach_info, *target_sp));
435 if (error.Success())
436 sb_process.SetSP(target_sp->GetProcessSP());
437 } else {
438 error.SetErrorString("SBTarget is invalid");
439 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 if (log)
442 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
443 static_cast<void *>(target_sp.get()),
444 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000450 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 lldb::pid_t pid, // The process ID to attach to
452 SBError &error // An error explaining what went wrong if attach fails
453 ) {
454 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 SBProcess sb_process;
457 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 if (log)
460 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
461 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 if (target_sp) {
464 ProcessAttachInfo attach_info;
465 attach_info.SetProcessID(pid);
466 if (listener.IsValid())
467 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 ProcessInstanceInfo instance_info;
470 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
471 attach_info.SetUserID(instance_info.GetEffectiveUserID());
472
473 error.SetError(AttachToProcess(attach_info, *target_sp));
474 if (error.Success())
475 sb_process.SetSP(target_sp->GetProcessSP());
476 } else
477 error.SetErrorString("SBTarget is invalid");
478
479 if (log)
480 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
481 static_cast<void *>(target_sp.get()), __FUNCTION__,
482 static_cast<void *>(sb_process.GetSP().get()));
483 return sb_process;
484}
485
486lldb::SBProcess SBTarget::AttachToProcessWithName(
487 SBListener &listener,
488 const char *name, // basename of process to attach to
489 bool wait_for, // if true wait for a new instance of "name" to be launched
490 SBError &error // An error explaining what went wrong if attach fails
491 ) {
492 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
493
494 SBProcess sb_process;
495 TargetSP target_sp(GetSP());
496
497 if (log)
498 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
499 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
500 wait_for ? "true" : "false");
501
502 if (name && target_sp) {
503 ProcessAttachInfo attach_info;
504 attach_info.GetExecutableFile().SetFile(name, false);
505 attach_info.SetWaitForLaunch(wait_for);
506 if (listener.IsValid())
507 attach_info.SetListener(listener.GetSP());
508
509 error.SetError(AttachToProcess(attach_info, *target_sp));
510 if (error.Success())
511 sb_process.SetSP(target_sp->GetProcessSP());
512 } else
513 error.SetErrorString("SBTarget is invalid");
514
515 if (log)
516 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
517 static_cast<void *>(target_sp.get()), __FUNCTION__,
518 static_cast<void *>(sb_process.GetSP().get()));
519 return sb_process;
520}
521
522lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
523 const char *plugin_name,
524 SBError &error) {
525 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
526
527 SBProcess sb_process;
528 ProcessSP process_sp;
529 TargetSP target_sp(GetSP());
530
531 if (log)
532 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
533 "plugin_name=%s, error)...",
534 static_cast<void *>(target_sp.get()), url, plugin_name);
535
536 if (target_sp) {
537 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
538 if (listener.IsValid())
539 process_sp =
540 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000541 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542 process_sp = target_sp->CreateProcess(
543 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
544
545 if (process_sp) {
546 sb_process.SetSP(process_sp);
547 error.SetError(process_sp->ConnectRemote(NULL, url));
548 } else {
549 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551 } else {
552 error.SetErrorString("SBTarget is invalid");
553 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 if (log)
556 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
557 static_cast<void *>(target_sp.get()),
558 static_cast<void *>(process_sp.get()));
559 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 SBFileSpec exe_file_spec;
565 TargetSP target_sp(GetSP());
566 if (target_sp) {
567 Module *exe_module = target_sp->GetExecutableModulePointer();
568 if (exe_module)
569 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
570 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000571
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
573 if (log) {
574 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
575 static_cast<void *>(target_sp.get()),
576 static_cast<const void *>(exe_file_spec.get()));
577 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000578
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580}
581
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582bool SBTarget::operator==(const SBTarget &rhs) const {
583 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584}
585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586bool SBTarget::operator!=(const SBTarget &rhs) const {
587 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588}
589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
591
592void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
593 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000594}
595
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
597 lldb::SBAddress sb_addr;
598 Address &addr = sb_addr.ref();
599 TargetSP target_sp(GetSP());
600 if (target_sp) {
601 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
602 if (target_sp->ResolveLoadAddress(vm_addr, addr))
603 return sb_addr;
604 }
605
606 // We have a load address that isn't in a section, just return an address
607 // with the offset filled in (the address) and the section set to NULL
608 addr.SetRawAddress(vm_addr);
609 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
613 lldb::SBAddress sb_addr;
614 Address &addr = sb_addr.ref();
615 TargetSP target_sp(GetSP());
616 if (target_sp) {
617 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
618 if (target_sp->ResolveFileAddress(file_addr, addr))
619 return sb_addr;
620 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 addr.SetRawAddress(file_addr);
623 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000624}
625
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
627 lldb::addr_t vm_addr) {
628 lldb::SBAddress sb_addr;
629 Address &addr = sb_addr.ref();
630 TargetSP target_sp(GetSP());
631 if (target_sp) {
632 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
633 if (target_sp->ResolveLoadAddress(vm_addr, addr))
634 return sb_addr;
635 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 // We have a load address that isn't in a section, just return an address
638 // with the offset filled in (the address) and the section set to NULL
639 addr.SetRawAddress(vm_addr);
640 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000641}
642
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000643SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
645 uint32_t resolve_scope) {
646 SBSymbolContext sc;
647 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000648 TargetSP target_sp(GetSP());
649 if (target_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 target_sp->GetImages().ResolveSymbolContextForAddress(
651 addr.ref(), resolve_scope, sc.ref());
652 }
653 return sc;
654}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
657 lldb::SBError &error) {
658 SBError sb_error;
659 size_t bytes_read = 0;
660 TargetSP target_sp(GetSP());
661 if (target_sp) {
662 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
663 bytes_read =
664 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
665 } else {
666 sb_error.SetErrorString("invalid target");
667 }
668
669 return bytes_read;
670}
671
672SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
673 uint32_t line) {
674 return SBBreakpoint(
675 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000676}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000677
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
680 uint32_t line) {
681 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682}
683
684SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
686 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000687 SBFileSpecList empty_list;
688 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
689}
690
691SBBreakpoint
692SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
693 uint32_t line, lldb::addr_t offset,
694 SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 SBBreakpoint sb_bp;
698 TargetSP target_sp(GetSP());
699 if (target_sp && line != 0) {
700 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000701
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 const LazyBool check_inlines = eLazyBoolCalculate;
703 const LazyBool skip_prologue = eLazyBoolCalculate;
704 const bool internal = false;
705 const bool hardware = false;
706 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000707 const FileSpecList *module_list = nullptr;
708 if (sb_module_list.GetSize() > 0) {
709 module_list = sb_module_list.get();
710 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000711 sb_bp = target_sp->CreateBreakpoint(
Jim Ingham92d19602016-09-20 22:54:49 +0000712 module_list, *sb_file_spec, line, offset, check_inlines, skip_prologue,
713 internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000715
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716 if (log) {
717 SBStream sstr;
718 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000719 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 sb_file_spec->GetPath(path, sizeof(path));
721 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
722 "SBBreakpoint(%p): %s",
723 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000724 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000726
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 return sb_bp;
728}
729
730SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
731 const char *module_name) {
732 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
733
734 SBBreakpoint sb_bp;
735 TargetSP target_sp(GetSP());
736 if (target_sp.get()) {
737 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
738
739 const bool internal = false;
740 const bool hardware = false;
741 const LazyBool skip_prologue = eLazyBoolCalculate;
742 const lldb::addr_t offset = 0;
743 if (module_name && module_name[0]) {
744 FileSpecList module_spec_list;
745 module_spec_list.Append(FileSpec(module_name, false));
Pavel Labath6ac84032017-02-27 11:05:34 +0000746 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
748 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
749 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000750 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
752 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
755
756 if (log)
757 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
758 "module=\"%s\") => SBBreakpoint(%p)",
759 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000760 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761
762 return sb_bp;
763}
764
765lldb::SBBreakpoint
766SBTarget::BreakpointCreateByName(const char *symbol_name,
767 const SBFileSpecList &module_list,
768 const SBFileSpecList &comp_unit_list) {
769 uint32_t name_type_mask = eFunctionNameTypeAuto;
770 return BreakpointCreateByName(symbol_name, name_type_mask,
771 eLanguageTypeUnknown, module_list,
772 comp_unit_list);
773}
774
775lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
776 const char *symbol_name, uint32_t name_type_mask,
777 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
778 return BreakpointCreateByName(symbol_name, name_type_mask,
779 eLanguageTypeUnknown, module_list,
780 comp_unit_list);
781}
782
783lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
784 const char *symbol_name, uint32_t name_type_mask,
785 LanguageType symbol_language, const SBFileSpecList &module_list,
786 const SBFileSpecList &comp_unit_list) {
787 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
788
789 SBBreakpoint sb_bp;
790 TargetSP target_sp(GetSP());
791 if (target_sp && symbol_name && symbol_name[0]) {
792 const bool internal = false;
793 const bool hardware = false;
794 const LazyBool skip_prologue = eLazyBoolCalculate;
795 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +0000796 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask,
798 symbol_language, 0, skip_prologue, internal, hardware);
799 }
800
801 if (log)
802 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
803 "name_type: %d) => SBBreakpoint(%p)",
804 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000805 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806
807 return sb_bp;
808}
809
810lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
811 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
812 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
813 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
814 eLanguageTypeUnknown, module_list,
815 comp_unit_list);
816}
817
818lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
819 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
820 LanguageType symbol_language, const SBFileSpecList &module_list,
821 const SBFileSpecList &comp_unit_list) {
822 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
823 eLanguageTypeUnknown, 0, module_list,
824 comp_unit_list);
825}
826
827lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
828 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
829 LanguageType symbol_language, lldb::addr_t offset,
830 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
831 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
832
833 SBBreakpoint sb_bp;
834 TargetSP target_sp(GetSP());
835 if (target_sp && num_names > 0) {
836 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
837 const bool internal = false;
838 const bool hardware = false;
839 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +0000840 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 module_list.get(), comp_unit_list.get(), symbol_names, num_names,
842 name_type_mask, symbol_language, offset, skip_prologue, internal,
843 hardware);
844 }
845
846 if (log) {
847 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
848 static_cast<void *>(target_sp.get()));
849 for (uint32_t i = 0; i < num_names; i++) {
850 char sep;
851 if (i < num_names - 1)
852 sep = ',';
853 else
854 sep = '}';
855 if (symbol_names[i] != NULL)
856 log->Printf("\"%s\"%c ", symbol_names[i], sep);
857 else
858 log->Printf("\"<NULL>\"%c ", sep);
859 }
860 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +0000861 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 }
863
864 return sb_bp;
865}
866
867SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
868 const char *module_name) {
869 SBFileSpecList module_spec_list;
870 SBFileSpecList comp_unit_list;
871 if (module_name && module_name[0]) {
872 module_spec_list.Append(FileSpec(module_name, false));
873 }
874 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
875 module_spec_list, comp_unit_list);
876}
877
878lldb::SBBreakpoint
879SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
880 const SBFileSpecList &module_list,
881 const SBFileSpecList &comp_unit_list) {
882 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
883 module_list, comp_unit_list);
884}
885
886lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
887 const char *symbol_name_regex, LanguageType symbol_language,
888 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
889 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
890
891 SBBreakpoint sb_bp;
892 TargetSP target_sp(GetSP());
893 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
894 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000895 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 const bool internal = false;
897 const bool hardware = false;
898 const LazyBool skip_prologue = eLazyBoolCalculate;
899
Pavel Labath6ac84032017-02-27 11:05:34 +0000900 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
902 skip_prologue, internal, hardware);
903 }
904
905 if (log)
906 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
907 "=> SBBreakpoint(%p)",
908 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +0000909 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910
911 return sb_bp;
912}
913
914SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
915 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
916
917 SBBreakpoint sb_bp;
918 TargetSP target_sp(GetSP());
919 if (target_sp) {
920 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
921 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000922 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 }
924
925 if (log)
926 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
927 ") => SBBreakpoint(%p)",
928 static_cast<void *>(target_sp.get()),
929 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +0000930 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931
932 return sb_bp;
933}
934
935SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
936 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
937
938 SBBreakpoint sb_bp;
939 TargetSP target_sp(GetSP());
940 if (!sb_address.IsValid()) {
941 if (log)
942 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
943 "invalid address",
944 static_cast<void *>(target_sp.get()));
945 return sb_bp;
946 }
947
948 if (target_sp) {
949 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
950 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000951 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 }
953
954 if (log) {
955 SBStream s;
956 sb_address.GetDescription(s);
957 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
958 "SBBreakpoint(%p)",
959 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +0000960 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
962
963 return sb_bp;
964}
965
966lldb::SBBreakpoint
967SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
968 const lldb::SBFileSpec &source_file,
969 const char *module_name) {
970 SBFileSpecList module_spec_list;
971
972 if (module_name && module_name[0]) {
973 module_spec_list.Append(FileSpec(module_name, false));
974 }
975
976 SBFileSpecList source_file_list;
977 if (source_file.IsValid()) {
978 source_file_list.Append(source_file);
979 }
980
981 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
982 source_file_list);
983}
984
985lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
986 const char *source_regex, const SBFileSpecList &module_list,
987 const lldb::SBFileSpecList &source_file_list) {
988 return BreakpointCreateBySourceRegex(source_regex, module_list,
989 source_file_list, SBStringList());
990}
991
992lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
993 const char *source_regex, const SBFileSpecList &module_list,
994 const lldb::SBFileSpecList &source_file_list,
995 const SBStringList &func_names) {
996 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
997
998 SBBreakpoint sb_bp;
999 TargetSP target_sp(GetSP());
1000 if (target_sp && source_regex && source_regex[0]) {
1001 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1002 const bool hardware = false;
1003 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +00001004 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 std::unordered_set<std::string> func_names_set;
1006 for (size_t i = 0; i < func_names.GetSize(); i++) {
1007 func_names_set.insert(func_names.GetStringAtIndex(i));
1008 }
1009
Pavel Labath6ac84032017-02-27 11:05:34 +00001010 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 module_list.get(), source_file_list.get(), func_names_set, regexp,
1012 false, hardware, move_to_nearest_code);
1013 }
1014
1015 if (log)
1016 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1017 "=> SBBreakpoint(%p)",
1018 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001019 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020
1021 return sb_bp;
1022}
1023
1024lldb::SBBreakpoint
1025SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1026 bool catch_bp, bool throw_bp) {
1027 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1028
1029 SBBreakpoint sb_bp;
1030 TargetSP target_sp(GetSP());
1031 if (target_sp) {
1032 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1033 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001034 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 hardware);
1036 }
1037
1038 if (log)
1039 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: "
1040 "%s throw: %s) => SBBreakpoint(%p)",
1041 static_cast<void *>(target_sp.get()),
1042 Language::GetNameForLanguageType(language),
1043 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001044 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045
1046 return sb_bp;
1047}
1048
1049uint32_t SBTarget::GetNumBreakpoints() const {
1050 TargetSP target_sp(GetSP());
1051 if (target_sp) {
1052 // The breakpoint list is thread safe, no need to lock
1053 return target_sp->GetBreakpointList().GetSize();
1054 }
1055 return 0;
1056}
1057
1058SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1059 SBBreakpoint sb_breakpoint;
1060 TargetSP target_sp(GetSP());
1061 if (target_sp) {
1062 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001063 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 }
1065 return sb_breakpoint;
1066}
1067
1068bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1069 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1070
1071 bool result = false;
1072 TargetSP target_sp(GetSP());
1073 if (target_sp) {
1074 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1075 result = target_sp->RemoveBreakpointByID(bp_id);
1076 }
1077
1078 if (log)
1079 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1080 static_cast<void *>(target_sp.get()),
1081 static_cast<uint32_t>(bp_id), result);
1082
1083 return result;
1084}
1085
1086SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1087 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1088
1089 SBBreakpoint sb_breakpoint;
1090 TargetSP target_sp(GetSP());
1091 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1092 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001093 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 }
1095
1096 if (log)
1097 log->Printf(
1098 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1099 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001100 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101
1102 return sb_breakpoint;
1103}
1104
Jim Inghamff9a91e2016-09-21 01:21:19 +00001105bool SBTarget::FindBreakpointsByName(const char *name,
1106 SBBreakpointList &bkpts) {
1107 TargetSP target_sp(GetSP());
1108 if (target_sp) {
1109 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1110 BreakpointList bkpt_list(false);
1111 bool is_valid =
1112 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1113 if (!is_valid)
1114 return false;
1115 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1116 bkpts.AppendByID(bkpt_sp->GetID());
1117 }
1118 }
1119 return true;
1120}
1121
Jim Inghamb842f2e2017-09-14 20:22:49 +00001122void SBTarget::GetBreakpointNames(SBStringList &names)
1123{
1124 names.Clear();
1125
1126 TargetSP target_sp(GetSP());
1127 if (target_sp) {
1128 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1129
1130 std::vector<std::string> name_vec;
1131 target_sp->GetBreakpointNames(name_vec);
1132 for (auto name : name_vec)
1133 names.AppendString(name.c_str());
1134 }
1135}
1136
1137void SBTarget::DeleteBreakpointName(const char *name)
1138{
1139 TargetSP target_sp(GetSP());
1140 if (target_sp) {
1141 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001142 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001143 }
1144}
1145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146bool SBTarget::EnableAllBreakpoints() {
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->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 return true;
1152 }
1153 return false;
1154}
1155
1156bool SBTarget::DisableAllBreakpoints() {
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->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 return true;
1162 }
1163 return false;
1164}
1165
1166bool SBTarget::DeleteAllBreakpoints() {
1167 TargetSP target_sp(GetSP());
1168 if (target_sp) {
1169 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001170 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 return true;
1172 }
1173 return false;
1174}
1175
Jim Ingham01f16662016-09-14 19:07:35 +00001176lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1177 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001178 SBStringList empty_name_list;
1179 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1180}
1181
1182lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1183 SBStringList &matching_names,
1184 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001185 SBError sberr;
1186 TargetSP target_sp(GetSP());
1187 if (!target_sp) {
1188 sberr.SetErrorString(
1189 "BreakpointCreateFromFile called with invalid target.");
1190 return sberr;
1191 }
1192 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1193
1194 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001195
1196 std::vector<std::string> name_vector;
1197 size_t num_names = matching_names.GetSize();
1198 for (size_t i = 0; i < num_names; i++)
1199 name_vector.push_back(matching_names.GetStringAtIndex(i));
1200
1201 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1202 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001203 if (sberr.Fail())
1204 return sberr;
1205
1206 size_t num_bkpts = bp_ids.GetSize();
1207 for (size_t i = 0; i < num_bkpts; i++) {
1208 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1209 new_bps.AppendByID(bp_id.GetBreakpointID());
1210 }
1211 return sberr;
1212}
1213
1214lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1215 SBError sberr;
1216 TargetSP target_sp(GetSP());
1217 if (!target_sp) {
1218 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1219 return sberr;
1220 }
1221 SBBreakpointList bkpt_list(*this);
1222 return BreakpointsWriteToFile(dest_file, bkpt_list);
1223}
1224
1225lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001226 SBBreakpointList &bkpt_list,
1227 bool append) {
Jim Ingham01f16662016-09-14 19:07:35 +00001228 SBError sberr;
1229 TargetSP target_sp(GetSP());
1230 if (!target_sp) {
1231 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1232 return sberr;
1233 }
1234
1235 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1236 BreakpointIDList bp_id_list;
1237 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001238 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1239 bp_id_list, append);
Jim Ingham01f16662016-09-14 19:07:35 +00001240 return sberr;
1241}
1242
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243uint32_t SBTarget::GetNumWatchpoints() const {
1244 TargetSP target_sp(GetSP());
1245 if (target_sp) {
1246 // The watchpoint list is thread safe, no need to lock
1247 return target_sp->GetWatchpointList().GetSize();
1248 }
1249 return 0;
1250}
1251
1252SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1253 SBWatchpoint sb_watchpoint;
1254 TargetSP target_sp(GetSP());
1255 if (target_sp) {
1256 // The watchpoint list is thread safe, no need to lock
1257 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1258 }
1259 return sb_watchpoint;
1260}
1261
1262bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1263 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1264
1265 bool result = false;
1266 TargetSP target_sp(GetSP());
1267 if (target_sp) {
1268 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1269 std::unique_lock<std::recursive_mutex> lock;
1270 target_sp->GetWatchpointList().GetListMutex(lock);
1271 result = target_sp->RemoveWatchpointByID(wp_id);
1272 }
1273
1274 if (log)
1275 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1276 static_cast<void *>(target_sp.get()),
1277 static_cast<uint32_t>(wp_id), result);
1278
1279 return result;
1280}
1281
1282SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1283 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1284
1285 SBWatchpoint sb_watchpoint;
1286 lldb::WatchpointSP watchpoint_sp;
1287 TargetSP target_sp(GetSP());
1288 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1289 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1290 std::unique_lock<std::recursive_mutex> lock;
1291 target_sp->GetWatchpointList().GetListMutex(lock);
1292 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1293 sb_watchpoint.SetSP(watchpoint_sp);
1294 }
1295
1296 if (log)
1297 log->Printf(
1298 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1299 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1300 static_cast<void *>(watchpoint_sp.get()));
1301
1302 return sb_watchpoint;
1303}
1304
1305lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1306 bool read, bool write,
1307 SBError &error) {
1308 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1309
1310 SBWatchpoint sb_watchpoint;
1311 lldb::WatchpointSP watchpoint_sp;
1312 TargetSP target_sp(GetSP());
1313 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1314 size > 0) {
1315 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1316 uint32_t watch_type = 0;
1317 if (read)
1318 watch_type |= LLDB_WATCH_TYPE_READ;
1319 if (write)
1320 watch_type |= LLDB_WATCH_TYPE_WRITE;
1321 if (watch_type == 0) {
1322 error.SetErrorString(
1323 "Can't create a watchpoint that is neither read nor write.");
1324 return sb_watchpoint;
1325 }
1326
1327 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001328 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 // This API doesn't take in a type, so we can't figure out what it is.
1330 CompilerType *type = NULL;
1331 watchpoint_sp =
1332 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1333 error.SetError(cw_error);
1334 sb_watchpoint.SetSP(watchpoint_sp);
1335 }
1336
1337 if (log)
1338 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1339 ", 0x%u) => SBWatchpoint(%p)",
1340 static_cast<void *>(target_sp.get()), addr,
1341 static_cast<uint32_t>(size),
1342 static_cast<void *>(watchpoint_sp.get()));
1343
1344 return sb_watchpoint;
1345}
1346
1347bool SBTarget::EnableAllWatchpoints() {
1348 TargetSP target_sp(GetSP());
1349 if (target_sp) {
1350 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1351 std::unique_lock<std::recursive_mutex> lock;
1352 target_sp->GetWatchpointList().GetListMutex(lock);
1353 target_sp->EnableAllWatchpoints();
1354 return true;
1355 }
1356 return false;
1357}
1358
1359bool SBTarget::DisableAllWatchpoints() {
1360 TargetSP target_sp(GetSP());
1361 if (target_sp) {
1362 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1363 std::unique_lock<std::recursive_mutex> lock;
1364 target_sp->GetWatchpointList().GetListMutex(lock);
1365 target_sp->DisableAllWatchpoints();
1366 return true;
1367 }
1368 return false;
1369}
1370
1371SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1372 SBType type) {
1373 SBValue sb_value;
1374 lldb::ValueObjectSP new_value_sp;
1375 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1376 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1377 ExecutionContext exe_ctx(
1378 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1379 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1380 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1381 exe_ctx, ast_type);
1382 }
1383 sb_value.SetSP(new_value_sp);
1384 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1385 if (log) {
1386 if (new_value_sp)
1387 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1388 static_cast<void *>(m_opaque_sp.get()),
1389 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001390 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1392 static_cast<void *>(m_opaque_sp.get()));
1393 }
1394 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001395}
1396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1398 lldb::SBType type) {
1399 SBValue sb_value;
1400 lldb::ValueObjectSP new_value_sp;
1401 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1402 DataExtractorSP extractor(*data);
1403 ExecutionContext exe_ctx(
1404 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1405 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1406 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1407 exe_ctx, ast_type);
1408 }
1409 sb_value.SetSP(new_value_sp);
1410 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1411 if (log) {
1412 if (new_value_sp)
1413 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1414 static_cast<void *>(m_opaque_sp.get()),
1415 new_value_sp->GetName().AsCString());
1416 else
1417 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1418 static_cast<void *>(m_opaque_sp.get()));
1419 }
1420 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001421}
1422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1424 const char *expr) {
1425 SBValue sb_value;
1426 lldb::ValueObjectSP new_value_sp;
1427 if (IsValid() && name && *name && expr && *expr) {
1428 ExecutionContext exe_ctx(
1429 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1430 new_value_sp =
1431 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1432 }
1433 sb_value.SetSP(new_value_sp);
1434 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1435 if (log) {
1436 if (new_value_sp)
1437 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1438 static_cast<void *>(m_opaque_sp.get()),
1439 new_value_sp->GetName().AsCString());
1440 else
1441 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1442 static_cast<void *>(m_opaque_sp.get()));
1443 }
1444 return sb_value;
1445}
1446
1447bool SBTarget::DeleteAllWatchpoints() {
1448 TargetSP target_sp(GetSP());
1449 if (target_sp) {
1450 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1451 std::unique_lock<std::recursive_mutex> lock;
1452 target_sp->GetWatchpointList().GetListMutex(lock);
1453 target_sp->RemoveAllWatchpoints();
1454 return true;
1455 }
1456 return false;
1457}
1458
1459lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1460 const char *uuid_cstr) {
1461 return AddModule(path, triple, uuid_cstr, NULL);
1462}
1463
1464lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1465 const char *uuid_cstr, const char *symfile) {
1466 lldb::SBModule sb_module;
1467 TargetSP target_sp(GetSP());
1468 if (target_sp) {
1469 ModuleSpec module_spec;
1470 if (path)
1471 module_spec.GetFileSpec().SetFile(path, false);
1472
1473 if (uuid_cstr)
1474 module_spec.GetUUID().SetFromCString(uuid_cstr);
1475
1476 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001477 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1478 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479 else
1480 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1481
1482 if (symfile)
1483 module_spec.GetSymbolFileSpec().SetFile(symfile, false);
1484
1485 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1486 }
1487 return sb_module;
1488}
1489
1490lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1491 lldb::SBModule sb_module;
1492 TargetSP target_sp(GetSP());
1493 if (target_sp)
1494 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1495 return sb_module;
1496}
1497
1498bool SBTarget::AddModule(lldb::SBModule &module) {
1499 TargetSP target_sp(GetSP());
1500 if (target_sp) {
1501 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1502 return true;
1503 }
1504 return false;
1505}
1506
1507uint32_t SBTarget::GetNumModules() const {
1508 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1509
1510 uint32_t num = 0;
1511 TargetSP target_sp(GetSP());
1512 if (target_sp) {
1513 // The module list is thread safe, no need to lock
1514 num = target_sp->GetImages().GetSize();
1515 }
1516
1517 if (log)
1518 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1519 static_cast<void *>(target_sp.get()), num);
1520
1521 return num;
1522}
1523
1524void SBTarget::Clear() {
1525 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1526
1527 if (log)
1528 log->Printf("SBTarget(%p)::Clear ()",
1529 static_cast<void *>(m_opaque_sp.get()));
1530
1531 m_opaque_sp.reset();
1532}
1533
1534SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1535 SBModule sb_module;
1536 TargetSP target_sp(GetSP());
1537 if (target_sp && sb_file_spec.IsValid()) {
1538 ModuleSpec module_spec(*sb_file_spec);
1539 // The module list is thread safe, no need to lock
1540 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1541 }
1542 return sb_module;
1543}
1544
1545lldb::ByteOrder SBTarget::GetByteOrder() {
1546 TargetSP target_sp(GetSP());
1547 if (target_sp)
1548 return target_sp->GetArchitecture().GetByteOrder();
1549 return eByteOrderInvalid;
1550}
1551
1552const char *SBTarget::GetTriple() {
1553 TargetSP target_sp(GetSP());
1554 if (target_sp) {
1555 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001556 // Unique the string so we don't run into ownership issues since the const
1557 // strings put the string into the string pool once and the strings never
1558 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 ConstString const_triple(triple.c_str());
1560 return const_triple.GetCString();
1561 }
1562 return NULL;
1563}
1564
1565uint32_t SBTarget::GetDataByteSize() {
1566 TargetSP target_sp(GetSP());
1567 if (target_sp) {
1568 return target_sp->GetArchitecture().GetDataByteSize();
1569 }
1570 return 0;
1571}
1572
1573uint32_t SBTarget::GetCodeByteSize() {
1574 TargetSP target_sp(GetSP());
1575 if (target_sp) {
1576 return target_sp->GetArchitecture().GetCodeByteSize();
1577 }
1578 return 0;
1579}
1580
1581uint32_t SBTarget::GetAddressByteSize() {
1582 TargetSP target_sp(GetSP());
1583 if (target_sp)
1584 return target_sp->GetArchitecture().GetAddressByteSize();
1585 return sizeof(void *);
1586}
1587
1588SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1589 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1590
1591 SBModule sb_module;
1592 ModuleSP module_sp;
1593 TargetSP target_sp(GetSP());
1594 if (target_sp) {
1595 // The module list is thread safe, no need to lock
1596 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1597 sb_module.SetSP(module_sp);
1598 }
1599
1600 if (log)
1601 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1602 static_cast<void *>(target_sp.get()), idx,
1603 static_cast<void *>(module_sp.get()));
1604
1605 return sb_module;
1606}
1607
1608bool SBTarget::RemoveModule(lldb::SBModule module) {
1609 TargetSP target_sp(GetSP());
1610 if (target_sp)
1611 return target_sp->GetImages().Remove(module.GetSP());
1612 return false;
1613}
1614
1615SBBroadcaster SBTarget::GetBroadcaster() const {
1616 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1617
1618 TargetSP target_sp(GetSP());
1619 SBBroadcaster broadcaster(target_sp.get(), false);
1620
1621 if (log)
1622 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1623 static_cast<void *>(target_sp.get()),
1624 static_cast<void *>(broadcaster.get()));
1625
1626 return broadcaster;
1627}
1628
1629bool SBTarget::GetDescription(SBStream &description,
1630 lldb::DescriptionLevel description_level) {
1631 Stream &strm = description.ref();
1632
1633 TargetSP target_sp(GetSP());
1634 if (target_sp) {
1635 target_sp->Dump(&strm, description_level);
1636 } else
1637 strm.PutCString("No value");
1638
1639 return true;
1640}
1641
1642lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1643 uint32_t name_type_mask) {
1644 lldb::SBSymbolContextList sb_sc_list;
1645 if (name && name[0]) {
Jim Ingham763b2b22015-07-07 22:12:17 +00001646 TargetSP target_sp(GetSP());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647 if (target_sp) {
1648 const bool symbols_ok = true;
1649 const bool inlines_ok = true;
1650 const bool append = true;
1651 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask,
1652 symbols_ok, inlines_ok, append,
1653 *sb_sc_list);
1654 }
1655 }
1656 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001657}
Greg Claytone14e1922012-12-04 02:22:16 +00001658
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1660 uint32_t max_matches,
1661 MatchType matchtype) {
1662 lldb::SBSymbolContextList sb_sc_list;
1663 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001664 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 TargetSP target_sp(GetSP());
1666 if (target_sp) {
1667 std::string regexstr;
1668 switch (matchtype) {
1669 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001670 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 true, true, *sb_sc_list);
1672 break;
1673 case eMatchTypeStartsWith:
1674 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001675 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1676 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 break;
1678 default:
1679 target_sp->GetImages().FindFunctions(ConstString(name),
1680 eFunctionNameTypeAny, true, true,
1681 true, *sb_sc_list);
1682 break;
1683 }
1684 }
1685 }
1686 return sb_sc_list;
1687}
1688
1689lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1690 TargetSP target_sp(GetSP());
1691 if (typename_cstr && typename_cstr[0] && target_sp) {
1692 ConstString const_typename(typename_cstr);
1693 SymbolContext sc;
1694 const bool exact_match = false;
1695
1696 const ModuleList &module_list = target_sp->GetImages();
1697 size_t count = module_list.GetSize();
1698 for (size_t idx = 0; idx < count; idx++) {
1699 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1700 if (module_sp) {
1701 TypeSP type_sp(
1702 module_sp->FindFirstType(sc, const_typename, exact_match));
1703 if (type_sp)
1704 return SBType(type_sp);
1705 }
1706 }
1707
Adrian Prantl05097242018-04-30 16:49:04 +00001708 // Didn't find the type in the symbols; try the Objective-C runtime if one
1709 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710
1711 ProcessSP process_sp(target_sp->GetProcessSP());
1712
1713 if (process_sp) {
1714 ObjCLanguageRuntime *objc_language_runtime =
1715 process_sp->GetObjCLanguageRuntime();
1716
1717 if (objc_language_runtime) {
1718 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1719
1720 if (objc_decl_vendor) {
1721 std::vector<clang::NamedDecl *> decls;
1722
1723 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1724 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1725 return SBType(type);
1726 }
1727 }
1728 }
1729 }
1730 }
1731
1732 // No matches, search for basic typename matches
1733 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1734 if (clang_ast)
1735 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1736 const_typename));
1737 }
1738 return SBType();
1739}
1740
1741SBType SBTarget::GetBasicType(lldb::BasicType type) {
1742 TargetSP target_sp(GetSP());
1743 if (target_sp) {
1744 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1745 if (clang_ast)
1746 return SBType(
1747 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1748 }
1749 return SBType();
1750}
1751
1752lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1753 SBTypeList sb_type_list;
1754 TargetSP target_sp(GetSP());
1755 if (typename_cstr && typename_cstr[0] && target_sp) {
1756 ModuleList &images = target_sp->GetImages();
1757 ConstString const_typename(typename_cstr);
1758 bool exact_match = false;
1759 SymbolContext sc;
1760 TypeList type_list;
1761 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1762 uint32_t num_matches =
1763 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX,
1764 searched_symbol_files, type_list);
1765
1766 if (num_matches > 0) {
1767 for (size_t idx = 0; idx < num_matches; idx++) {
1768 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1769 if (type_sp)
1770 sb_type_list.Append(SBType(type_sp));
1771 }
1772 }
1773
1774 // Try the Objective-C runtime if one is installed
1775
1776 ProcessSP process_sp(target_sp->GetProcessSP());
1777
1778 if (process_sp) {
1779 ObjCLanguageRuntime *objc_language_runtime =
1780 process_sp->GetObjCLanguageRuntime();
1781
1782 if (objc_language_runtime) {
1783 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1784
1785 if (objc_decl_vendor) {
1786 std::vector<clang::NamedDecl *> decls;
1787
1788 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1789 for (clang::NamedDecl *decl : decls) {
1790 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1791 sb_type_list.Append(SBType(type));
1792 }
1793 }
1794 }
1795 }
1796 }
1797 }
1798
1799 if (sb_type_list.GetSize() == 0) {
1800 // No matches, search for basic typename matches
1801 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1802 if (clang_ast)
1803 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1804 clang_ast->getASTContext(), const_typename)));
1805 }
1806 }
1807 return sb_type_list;
1808}
1809
1810SBValueList SBTarget::FindGlobalVariables(const char *name,
1811 uint32_t max_matches) {
1812 SBValueList sb_value_list;
1813
1814 TargetSP target_sp(GetSP());
1815 if (name && target_sp) {
1816 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001818 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001819
1820 if (match_count > 0) {
1821 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1822 if (exe_scope == NULL)
1823 exe_scope = target_sp.get();
1824 for (uint32_t i = 0; i < match_count; ++i) {
1825 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1826 exe_scope, variable_list.GetVariableAtIndex(i)));
1827 if (valobj_sp)
1828 sb_value_list.Append(SBValue(valobj_sp));
1829 }
1830 }
1831 }
1832
1833 return sb_value_list;
1834}
1835
1836SBValueList SBTarget::FindGlobalVariables(const char *name,
1837 uint32_t max_matches,
1838 MatchType matchtype) {
1839 SBValueList sb_value_list;
1840
1841 TargetSP target_sp(GetSP());
1842 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001843 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001845
1846 std::string regexstr;
1847 uint32_t match_count;
1848 switch (matchtype) {
1849 case eMatchTypeNormal:
1850 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001851 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001852 break;
1853 case eMatchTypeRegex:
1854 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001855 RegularExpression(name_ref), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 break;
1857 case eMatchTypeStartsWith:
1858 regexstr = llvm::Regex::escape(name) + ".*";
1859 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001860 RegularExpression(regexstr), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861 break;
1862 }
1863
1864 if (match_count > 0) {
1865 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1866 if (exe_scope == NULL)
1867 exe_scope = target_sp.get();
1868 for (uint32_t i = 0; i < match_count; ++i) {
1869 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1870 exe_scope, variable_list.GetVariableAtIndex(i)));
1871 if (valobj_sp)
1872 sb_value_list.Append(SBValue(valobj_sp));
1873 }
1874 }
1875 }
1876
1877 return sb_value_list;
1878}
1879
1880lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1881 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1882 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1883 return sb_value_list.GetValueAtIndex(0);
1884 return SBValue();
1885}
1886
1887SBSourceManager SBTarget::GetSourceManager() {
1888 SBSourceManager source_manager(*this);
1889 return source_manager;
1890}
1891
1892lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1893 uint32_t count) {
1894 return ReadInstructions(base_addr, count, NULL);
1895}
1896
1897lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1898 uint32_t count,
1899 const char *flavor_string) {
1900 SBInstructionList sb_instructions;
1901
1902 TargetSP target_sp(GetSP());
1903 if (target_sp) {
1904 Address *addr_ptr = base_addr.get();
1905
1906 if (addr_ptr) {
1907 DataBufferHeap data(
1908 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1909 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00001910 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1912 const size_t bytes_read =
1913 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
1914 data.GetByteSize(), error, &load_addr);
1915 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1916 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1917 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
1918 data.GetBytes(), bytes_read, count, data_from_file));
1919 }
1920 }
1921
1922 return sb_instructions;
1923}
1924
1925lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
1926 const void *buf,
1927 size_t size) {
1928 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
1929}
1930
1931lldb::SBInstructionList
1932SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
1933 const char *flavor_string, const void *buf,
1934 size_t size) {
1935 SBInstructionList sb_instructions;
1936
1937 TargetSP target_sp(GetSP());
1938 if (target_sp) {
1939 Address addr;
1940
1941 if (base_addr.get())
1942 addr = *base_addr.get();
1943
1944 const bool data_from_file = true;
1945
1946 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1947 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
1948 UINT32_MAX, data_from_file));
1949 }
1950
1951 return sb_instructions;
1952}
1953
1954lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
1955 const void *buf,
1956 size_t size) {
1957 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
1958 size);
1959}
1960
1961lldb::SBInstructionList
1962SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
1963 const char *flavor_string, const void *buf,
1964 size_t size) {
1965 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
1966 buf, size);
1967}
1968
1969SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
1970 lldb::addr_t section_base_addr) {
1971 SBError sb_error;
1972 TargetSP target_sp(GetSP());
1973 if (target_sp) {
1974 if (!section.IsValid()) {
1975 sb_error.SetErrorStringWithFormat("invalid section");
1976 } else {
1977 SectionSP section_sp(section.GetSP());
1978 if (section_sp) {
1979 if (section_sp->IsThreadSpecific()) {
1980 sb_error.SetErrorString(
1981 "thread specific sections are not yet supported");
1982 } else {
1983 ProcessSP process_sp(target_sp->GetProcessSP());
1984 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
1985 ModuleSP module_sp(section_sp->GetModule());
1986 if (module_sp) {
1987 ModuleList module_list;
1988 module_list.Append(module_sp);
1989 target_sp->ModulesDidLoad(module_list);
1990 }
1991 // Flush info in the process (stack frames, etc)
1992 if (process_sp)
1993 process_sp->Flush();
1994 }
1995 }
1996 }
1997 }
1998 } else {
1999 sb_error.SetErrorString("invalid target");
2000 }
2001 return sb_error;
2002}
2003
2004SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
2005 SBError sb_error;
2006
2007 TargetSP target_sp(GetSP());
2008 if (target_sp) {
2009 if (!section.IsValid()) {
2010 sb_error.SetErrorStringWithFormat("invalid section");
2011 } else {
2012 SectionSP section_sp(section.GetSP());
2013 if (section_sp) {
2014 ProcessSP process_sp(target_sp->GetProcessSP());
2015 if (target_sp->SetSectionUnloaded(section_sp)) {
2016 ModuleSP module_sp(section_sp->GetModule());
2017 if (module_sp) {
2018 ModuleList module_list;
2019 module_list.Append(module_sp);
2020 target_sp->ModulesDidUnload(module_list, false);
2021 }
2022 // Flush info in the process (stack frames, etc)
2023 if (process_sp)
2024 process_sp->Flush();
2025 }
2026 } else {
2027 sb_error.SetErrorStringWithFormat("invalid section");
2028 }
2029 }
2030 } else {
2031 sb_error.SetErrorStringWithFormat("invalid target");
2032 }
2033 return sb_error;
2034}
2035
2036SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2037 int64_t slide_offset) {
2038 SBError sb_error;
2039
2040 TargetSP target_sp(GetSP());
2041 if (target_sp) {
2042 ModuleSP module_sp(module.GetSP());
2043 if (module_sp) {
2044 bool changed = false;
2045 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2046 // The load was successful, make sure that at least some sections
2047 // changed before we notify that our module was loaded.
2048 if (changed) {
2049 ModuleList module_list;
2050 module_list.Append(module_sp);
2051 target_sp->ModulesDidLoad(module_list);
2052 // Flush info in the process (stack frames, etc)
2053 ProcessSP process_sp(target_sp->GetProcessSP());
2054 if (process_sp)
2055 process_sp->Flush();
2056 }
2057 }
2058 } else {
2059 sb_error.SetErrorStringWithFormat("invalid module");
2060 }
2061
2062 } else {
2063 sb_error.SetErrorStringWithFormat("invalid target");
2064 }
2065 return sb_error;
2066}
2067
2068SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2069 SBError sb_error;
2070
2071 char path[PATH_MAX];
2072 TargetSP target_sp(GetSP());
2073 if (target_sp) {
2074 ModuleSP module_sp(module.GetSP());
2075 if (module_sp) {
2076 ObjectFile *objfile = module_sp->GetObjectFile();
2077 if (objfile) {
2078 SectionList *section_list = objfile->GetSectionList();
2079 if (section_list) {
2080 ProcessSP process_sp(target_sp->GetProcessSP());
2081
2082 bool changed = false;
2083 const size_t num_sections = section_list->GetSize();
2084 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2085 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2086 if (section_sp)
2087 changed |= target_sp->SetSectionUnloaded(section_sp);
2088 }
2089 if (changed) {
2090 ModuleList module_list;
2091 module_list.Append(module_sp);
2092 target_sp->ModulesDidUnload(module_list, false);
2093 // Flush info in the process (stack frames, etc)
2094 ProcessSP process_sp(target_sp->GetProcessSP());
2095 if (process_sp)
2096 process_sp->Flush();
2097 }
2098 } else {
2099 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2100 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2101 path);
2102 }
2103 } else {
2104 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2105 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2106 path);
2107 }
2108 } else {
2109 sb_error.SetErrorStringWithFormat("invalid module");
2110 }
2111 } else {
2112 sb_error.SetErrorStringWithFormat("invalid target");
2113 }
2114 return sb_error;
2115}
2116
2117lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2118 lldb::SymbolType symbol_type) {
2119 SBSymbolContextList sb_sc_list;
2120 if (name && name[0]) {
2121 TargetSP target_sp(GetSP());
2122 if (target_sp) {
2123 bool append = true;
2124 target_sp->GetImages().FindSymbolsWithNameAndType(
2125 ConstString(name), symbol_type, *sb_sc_list, append);
2126 }
2127 }
2128 return sb_sc_list;
2129}
2130
2131lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2132 TargetSP target_sp(GetSP());
2133 if (!target_sp)
2134 return SBValue();
2135
2136 SBExpressionOptions options;
2137 lldb::DynamicValueType fetch_dynamic_value =
2138 target_sp->GetPreferDynamicValue();
2139 options.SetFetchDynamicValue(fetch_dynamic_value);
2140 options.SetUnwindOnError(true);
2141 return EvaluateExpression(expr, options);
2142}
2143
2144lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2145 const SBExpressionOptions &options) {
2146 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002147#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002148 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002149#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002150 SBValue expr_result;
2151 ExpressionResults exe_results = eExpressionSetupError;
2152 ValueObjectSP expr_value_sp;
2153 TargetSP target_sp(GetSP());
2154 StackFrame *frame = NULL;
2155 if (target_sp) {
2156 if (expr == NULL || expr[0] == '\0') {
2157 if (log)
2158 log->Printf(
2159 "SBTarget::EvaluateExpression called with an empty expression");
2160 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002162
2163 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2164 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002165
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002166 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2168
2169 frame = exe_ctx.GetFramePtr();
2170 Target *target = exe_ctx.GetTargetPtr();
2171
2172 if (target) {
2173#ifdef LLDB_CONFIGURATION_DEBUG
2174 StreamString frame_description;
2175 if (frame)
2176 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002177 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2179 "%u) %s",
2180 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002181 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182#endif
2183 exe_results =
2184 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2185
2186 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002187 } else {
2188 if (log)
2189 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2190 "reconstruct frame object for this SBTarget.");
2191 }
2192 }
2193#ifndef LLDB_DISABLE_PYTHON
2194 if (expr_log)
2195 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2196 "%s, summary %s **",
2197 expr_result.GetValue(), expr_result.GetSummary());
2198
2199 if (log)
2200 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2201 "(execution result=%d)",
2202 static_cast<void *>(frame), expr,
2203 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002204#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002205
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002207}
2208
Kate Stoneb9c1b512016-09-06 20:57:50 +00002209lldb::addr_t SBTarget::GetStackRedZoneSize() {
2210 TargetSP target_sp(GetSP());
2211 if (target_sp) {
2212 ABISP abi_sp;
2213 ProcessSP process_sp(target_sp->GetProcessSP());
2214 if (process_sp)
2215 abi_sp = process_sp->GetABI();
2216 else
Jason Molenda43294c92017-06-29 02:57:03 +00002217 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002218 if (abi_sp)
2219 return abi_sp->GetRedZoneSize();
2220 }
2221 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002222}
Ilia K8f37ca52015-02-13 14:31:06 +00002223
Kate Stoneb9c1b512016-09-06 20:57:50 +00002224lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2225 lldb::SBLaunchInfo launch_info(NULL);
2226 TargetSP target_sp(GetSP());
2227 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002228 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002229 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002230}
2231
Kate Stoneb9c1b512016-09-06 20:57:50 +00002232void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2233 TargetSP target_sp(GetSP());
2234 if (target_sp)
2235 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002236}