blob: 4d2417c711bf0ab730765340e5c22aaa6bd5cab1 [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;
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000504 attach_info.GetExecutableFile().SetFile(name, false,
505 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 attach_info.SetWaitForLaunch(wait_for);
507 if (listener.IsValid())
508 attach_info.SetListener(listener.GetSP());
509
510 error.SetError(AttachToProcess(attach_info, *target_sp));
511 if (error.Success())
512 sb_process.SetSP(target_sp->GetProcessSP());
513 } else
514 error.SetErrorString("SBTarget is invalid");
515
516 if (log)
517 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
518 static_cast<void *>(target_sp.get()), __FUNCTION__,
519 static_cast<void *>(sb_process.GetSP().get()));
520 return sb_process;
521}
522
523lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
524 const char *plugin_name,
525 SBError &error) {
526 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
527
528 SBProcess sb_process;
529 ProcessSP process_sp;
530 TargetSP target_sp(GetSP());
531
532 if (log)
533 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
534 "plugin_name=%s, error)...",
535 static_cast<void *>(target_sp.get()), url, plugin_name);
536
537 if (target_sp) {
538 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
539 if (listener.IsValid())
540 process_sp =
541 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000542 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 process_sp = target_sp->CreateProcess(
544 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
545
546 if (process_sp) {
547 sb_process.SetSP(process_sp);
548 error.SetError(process_sp->ConnectRemote(NULL, url));
549 } else {
550 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 } else {
553 error.SetErrorString("SBTarget is invalid");
554 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 if (log)
557 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
558 static_cast<void *>(target_sp.get()),
559 static_cast<void *>(process_sp.get()));
560 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000561}
562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 SBFileSpec exe_file_spec;
566 TargetSP target_sp(GetSP());
567 if (target_sp) {
568 Module *exe_module = target_sp->GetExecutableModulePointer();
569 if (exe_module)
570 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
571 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
574 if (log) {
575 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
576 static_cast<void *>(target_sp.get()),
577 static_cast<const void *>(exe_file_spec.get()));
578 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583bool SBTarget::operator==(const SBTarget &rhs) const {
584 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587bool SBTarget::operator!=(const SBTarget &rhs) const {
588 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589}
590
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
592
593void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
594 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000595}
596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
598 lldb::SBAddress sb_addr;
599 Address &addr = sb_addr.ref();
600 TargetSP target_sp(GetSP());
601 if (target_sp) {
602 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
603 if (target_sp->ResolveLoadAddress(vm_addr, addr))
604 return sb_addr;
605 }
606
607 // We have a load address that isn't in a section, just return an address
608 // with the offset filled in (the address) and the section set to NULL
609 addr.SetRawAddress(vm_addr);
610 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611}
612
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
614 lldb::SBAddress sb_addr;
615 Address &addr = sb_addr.ref();
616 TargetSP target_sp(GetSP());
617 if (target_sp) {
618 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
619 if (target_sp->ResolveFileAddress(file_addr, addr))
620 return sb_addr;
621 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 addr.SetRawAddress(file_addr);
624 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000625}
626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
628 lldb::addr_t vm_addr) {
629 lldb::SBAddress sb_addr;
630 Address &addr = sb_addr.ref();
631 TargetSP target_sp(GetSP());
632 if (target_sp) {
633 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
634 if (target_sp->ResolveLoadAddress(vm_addr, addr))
635 return sb_addr;
636 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 // We have a load address that isn't in a section, just return an address
639 // with the offset filled in (the address) and the section set to NULL
640 addr.SetRawAddress(vm_addr);
641 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000642}
643
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000644SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
646 uint32_t resolve_scope) {
647 SBSymbolContext sc;
648 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000649 TargetSP target_sp(GetSP());
650 if (target_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 target_sp->GetImages().ResolveSymbolContextForAddress(
652 addr.ref(), resolve_scope, sc.ref());
653 }
654 return sc;
655}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
658 lldb::SBError &error) {
659 SBError sb_error;
660 size_t bytes_read = 0;
661 TargetSP target_sp(GetSP());
662 if (target_sp) {
663 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
664 bytes_read =
665 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
666 } else {
667 sb_error.SetErrorString("invalid target");
668 }
669
670 return bytes_read;
671}
672
673SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
674 uint32_t line) {
675 return SBBreakpoint(
676 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000677}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000678
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
681 uint32_t line) {
682 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683}
684
685SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
687 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000688 SBFileSpecList empty_list;
689 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
690}
691
692SBBreakpoint
693SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
694 uint32_t line, lldb::addr_t offset,
695 SBFileSpecList &sb_module_list) {
Adrian Prantl431b1582018-08-30 15:11:00 +0000696 return BreakpointCreateByLocation(sb_file_spec, line, 0, offset,
697 sb_module_list);
698}
699
700SBBreakpoint SBTarget::BreakpointCreateByLocation(
701 const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
702 lldb::addr_t offset, SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 SBBreakpoint sb_bp;
706 TargetSP target_sp(GetSP());
707 if (target_sp && line != 0) {
708 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 const LazyBool check_inlines = eLazyBoolCalculate;
711 const LazyBool skip_prologue = eLazyBoolCalculate;
712 const bool internal = false;
713 const bool hardware = false;
714 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000715 const FileSpecList *module_list = nullptr;
716 if (sb_module_list.GetSize() > 0) {
717 module_list = sb_module_list.get();
718 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000719 sb_bp = target_sp->CreateBreakpoint(
Adrian Prantl431b1582018-08-30 15:11:00 +0000720 module_list, *sb_file_spec, line, column, offset, check_inlines,
721 skip_prologue, internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000723
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 if (log) {
725 SBStream sstr;
726 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000727 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 sb_file_spec->GetPath(path, sizeof(path));
729 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
730 "SBBreakpoint(%p): %s",
731 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000732 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 return sb_bp;
736}
737
738SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
739 const char *module_name) {
740 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
741
742 SBBreakpoint sb_bp;
743 TargetSP target_sp(GetSP());
744 if (target_sp.get()) {
745 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
746
747 const bool internal = false;
748 const bool hardware = false;
749 const LazyBool skip_prologue = eLazyBoolCalculate;
750 const lldb::addr_t offset = 0;
751 if (module_name && module_name[0]) {
752 FileSpecList module_spec_list;
753 module_spec_list.Append(FileSpec(module_name, false));
Pavel Labath6ac84032017-02-27 11:05:34 +0000754 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
756 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
757 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000758 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
760 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000761 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 }
763
764 if (log)
765 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
766 "module=\"%s\") => SBBreakpoint(%p)",
767 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000768 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769
770 return sb_bp;
771}
772
773lldb::SBBreakpoint
774SBTarget::BreakpointCreateByName(const char *symbol_name,
775 const SBFileSpecList &module_list,
776 const SBFileSpecList &comp_unit_list) {
777 uint32_t name_type_mask = eFunctionNameTypeAuto;
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 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
786 return BreakpointCreateByName(symbol_name, name_type_mask,
787 eLanguageTypeUnknown, module_list,
788 comp_unit_list);
789}
790
791lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
792 const char *symbol_name, uint32_t name_type_mask,
793 LanguageType symbol_language, const SBFileSpecList &module_list,
794 const SBFileSpecList &comp_unit_list) {
795 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
796
797 SBBreakpoint sb_bp;
798 TargetSP target_sp(GetSP());
799 if (target_sp && symbol_name && symbol_name[0]) {
800 const bool internal = false;
801 const bool hardware = false;
802 const LazyBool skip_prologue = eLazyBoolCalculate;
803 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +0000804 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask,
806 symbol_language, 0, skip_prologue, internal, hardware);
807 }
808
809 if (log)
810 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
811 "name_type: %d) => SBBreakpoint(%p)",
812 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000813 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814
815 return sb_bp;
816}
817
818lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
819 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
820 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
821 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
822 eLanguageTypeUnknown, module_list,
823 comp_unit_list);
824}
825
826lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
827 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
828 LanguageType symbol_language, const SBFileSpecList &module_list,
829 const SBFileSpecList &comp_unit_list) {
830 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
831 eLanguageTypeUnknown, 0, module_list,
832 comp_unit_list);
833}
834
835lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
836 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
837 LanguageType symbol_language, lldb::addr_t offset,
838 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
839 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
840
841 SBBreakpoint sb_bp;
842 TargetSP target_sp(GetSP());
843 if (target_sp && num_names > 0) {
844 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
845 const bool internal = false;
846 const bool hardware = false;
847 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +0000848 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 module_list.get(), comp_unit_list.get(), symbol_names, num_names,
850 name_type_mask, symbol_language, offset, skip_prologue, internal,
851 hardware);
852 }
853
854 if (log) {
855 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
856 static_cast<void *>(target_sp.get()));
857 for (uint32_t i = 0; i < num_names; i++) {
858 char sep;
859 if (i < num_names - 1)
860 sep = ',';
861 else
862 sep = '}';
863 if (symbol_names[i] != NULL)
864 log->Printf("\"%s\"%c ", symbol_names[i], sep);
865 else
866 log->Printf("\"<NULL>\"%c ", sep);
867 }
868 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +0000869 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 }
871
872 return sb_bp;
873}
874
875SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
876 const char *module_name) {
877 SBFileSpecList module_spec_list;
878 SBFileSpecList comp_unit_list;
879 if (module_name && module_name[0]) {
880 module_spec_list.Append(FileSpec(module_name, false));
881 }
882 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
883 module_spec_list, comp_unit_list);
884}
885
886lldb::SBBreakpoint
887SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
888 const SBFileSpecList &module_list,
889 const SBFileSpecList &comp_unit_list) {
890 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
891 module_list, comp_unit_list);
892}
893
894lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
895 const char *symbol_name_regex, LanguageType symbol_language,
896 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
897 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
898
899 SBBreakpoint sb_bp;
900 TargetSP target_sp(GetSP());
901 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
902 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000903 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 const bool internal = false;
905 const bool hardware = false;
906 const LazyBool skip_prologue = eLazyBoolCalculate;
907
Pavel Labath6ac84032017-02-27 11:05:34 +0000908 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
910 skip_prologue, internal, hardware);
911 }
912
913 if (log)
914 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
915 "=> SBBreakpoint(%p)",
916 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +0000917 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918
919 return sb_bp;
920}
921
922SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
923 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
924
925 SBBreakpoint sb_bp;
926 TargetSP target_sp(GetSP());
927 if (target_sp) {
928 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
929 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000930 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 }
932
933 if (log)
934 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
935 ") => SBBreakpoint(%p)",
936 static_cast<void *>(target_sp.get()),
937 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +0000938 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939
940 return sb_bp;
941}
942
943SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
944 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
945
946 SBBreakpoint sb_bp;
947 TargetSP target_sp(GetSP());
948 if (!sb_address.IsValid()) {
949 if (log)
950 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
951 "invalid address",
952 static_cast<void *>(target_sp.get()));
953 return sb_bp;
954 }
955
956 if (target_sp) {
957 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
958 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000959 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 }
961
962 if (log) {
963 SBStream s;
964 sb_address.GetDescription(s);
965 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
966 "SBBreakpoint(%p)",
967 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +0000968 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 }
970
971 return sb_bp;
972}
973
974lldb::SBBreakpoint
975SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
976 const lldb::SBFileSpec &source_file,
977 const char *module_name) {
978 SBFileSpecList module_spec_list;
979
980 if (module_name && module_name[0]) {
981 module_spec_list.Append(FileSpec(module_name, false));
982 }
983
984 SBFileSpecList source_file_list;
985 if (source_file.IsValid()) {
986 source_file_list.Append(source_file);
987 }
988
989 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
990 source_file_list);
991}
992
993lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
994 const char *source_regex, const SBFileSpecList &module_list,
995 const lldb::SBFileSpecList &source_file_list) {
996 return BreakpointCreateBySourceRegex(source_regex, module_list,
997 source_file_list, SBStringList());
998}
999
1000lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1001 const char *source_regex, const SBFileSpecList &module_list,
1002 const lldb::SBFileSpecList &source_file_list,
1003 const SBStringList &func_names) {
1004 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1005
1006 SBBreakpoint sb_bp;
1007 TargetSP target_sp(GetSP());
1008 if (target_sp && source_regex && source_regex[0]) {
1009 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1010 const bool hardware = false;
1011 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +00001012 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 std::unordered_set<std::string> func_names_set;
1014 for (size_t i = 0; i < func_names.GetSize(); i++) {
1015 func_names_set.insert(func_names.GetStringAtIndex(i));
1016 }
1017
Pavel Labath6ac84032017-02-27 11:05:34 +00001018 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 module_list.get(), source_file_list.get(), func_names_set, regexp,
1020 false, hardware, move_to_nearest_code);
1021 }
1022
1023 if (log)
1024 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1025 "=> SBBreakpoint(%p)",
1026 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001027 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028
1029 return sb_bp;
1030}
1031
1032lldb::SBBreakpoint
1033SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1034 bool catch_bp, bool throw_bp) {
1035 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1036
1037 SBBreakpoint sb_bp;
1038 TargetSP target_sp(GetSP());
1039 if (target_sp) {
1040 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1041 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001042 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 hardware);
1044 }
1045
1046 if (log)
Jim Ingham3815e702018-09-13 21:35:32 +00001047 log->Printf("SBTarget(%p)::BreakpointCreateForException (Language: %s, catch: "
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 "%s throw: %s) => SBBreakpoint(%p)",
1049 static_cast<void *>(target_sp.get()),
1050 Language::GetNameForLanguageType(language),
1051 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001052 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053
1054 return sb_bp;
1055}
1056
Jim Ingham3815e702018-09-13 21:35:32 +00001057lldb::SBBreakpoint
1058SBTarget::BreakpointCreateFromScript(const char *class_name,
1059 SBStructuredData &extra_args,
1060 const SBFileSpecList &module_list,
1061 const SBFileSpecList &file_list,
1062 bool request_hardware)
1063{
1064 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1065
1066 SBBreakpoint sb_bp;
1067 TargetSP target_sp(GetSP());
1068 if (target_sp) {
1069 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1070 Status error;
1071
1072 StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP();
1073 sb_bp =
1074 target_sp->CreateScriptedBreakpoint(class_name,
1075 module_list.get(),
1076 file_list.get(),
1077 false, /* internal */
1078 request_hardware,
1079 obj_sp,
1080 &error);
1081 }
1082 if (log)
1083 log->Printf("SBTarget(%p)::BreakpointCreateFromScript (class name: %s) "
1084 " => SBBreakpoint(%p)",
1085 static_cast<void *>(target_sp.get()),
1086 class_name,
1087 static_cast<void *>(sb_bp.GetSP().get()));
1088
1089 return sb_bp;
1090}
1091
1092
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093uint32_t SBTarget::GetNumBreakpoints() const {
1094 TargetSP target_sp(GetSP());
1095 if (target_sp) {
1096 // The breakpoint list is thread safe, no need to lock
1097 return target_sp->GetBreakpointList().GetSize();
1098 }
1099 return 0;
1100}
1101
1102SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1103 SBBreakpoint sb_breakpoint;
1104 TargetSP target_sp(GetSP());
1105 if (target_sp) {
1106 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001107 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 }
1109 return sb_breakpoint;
1110}
1111
1112bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1114
1115 bool result = false;
1116 TargetSP target_sp(GetSP());
1117 if (target_sp) {
1118 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1119 result = target_sp->RemoveBreakpointByID(bp_id);
1120 }
1121
1122 if (log)
1123 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1124 static_cast<void *>(target_sp.get()),
1125 static_cast<uint32_t>(bp_id), result);
1126
1127 return result;
1128}
1129
1130SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1131 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1132
1133 SBBreakpoint sb_breakpoint;
1134 TargetSP target_sp(GetSP());
1135 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1136 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001137 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 }
1139
1140 if (log)
1141 log->Printf(
1142 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1143 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001144 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145
1146 return sb_breakpoint;
1147}
1148
Jim Inghamff9a91e2016-09-21 01:21:19 +00001149bool SBTarget::FindBreakpointsByName(const char *name,
1150 SBBreakpointList &bkpts) {
1151 TargetSP target_sp(GetSP());
1152 if (target_sp) {
1153 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1154 BreakpointList bkpt_list(false);
1155 bool is_valid =
1156 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1157 if (!is_valid)
1158 return false;
1159 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1160 bkpts.AppendByID(bkpt_sp->GetID());
1161 }
1162 }
1163 return true;
1164}
1165
Jim Inghamb842f2e2017-09-14 20:22:49 +00001166void SBTarget::GetBreakpointNames(SBStringList &names)
1167{
1168 names.Clear();
1169
1170 TargetSP target_sp(GetSP());
1171 if (target_sp) {
1172 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1173
1174 std::vector<std::string> name_vec;
1175 target_sp->GetBreakpointNames(name_vec);
1176 for (auto name : name_vec)
1177 names.AppendString(name.c_str());
1178 }
1179}
1180
1181void SBTarget::DeleteBreakpointName(const char *name)
1182{
1183 TargetSP target_sp(GetSP());
1184 if (target_sp) {
1185 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001186 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001187 }
1188}
1189
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190bool SBTarget::EnableAllBreakpoints() {
1191 TargetSP target_sp(GetSP());
1192 if (target_sp) {
1193 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001194 target_sp->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 return true;
1196 }
1197 return false;
1198}
1199
1200bool SBTarget::DisableAllBreakpoints() {
1201 TargetSP target_sp(GetSP());
1202 if (target_sp) {
1203 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001204 target_sp->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 return true;
1206 }
1207 return false;
1208}
1209
1210bool SBTarget::DeleteAllBreakpoints() {
1211 TargetSP target_sp(GetSP());
1212 if (target_sp) {
1213 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001214 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 return true;
1216 }
1217 return false;
1218}
1219
Jim Ingham01f16662016-09-14 19:07:35 +00001220lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1221 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001222 SBStringList empty_name_list;
1223 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1224}
1225
1226lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1227 SBStringList &matching_names,
1228 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001229 SBError sberr;
1230 TargetSP target_sp(GetSP());
1231 if (!target_sp) {
1232 sberr.SetErrorString(
1233 "BreakpointCreateFromFile called with invalid target.");
1234 return sberr;
1235 }
1236 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1237
1238 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001239
1240 std::vector<std::string> name_vector;
1241 size_t num_names = matching_names.GetSize();
1242 for (size_t i = 0; i < num_names; i++)
1243 name_vector.push_back(matching_names.GetStringAtIndex(i));
1244
1245 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1246 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001247 if (sberr.Fail())
1248 return sberr;
1249
1250 size_t num_bkpts = bp_ids.GetSize();
1251 for (size_t i = 0; i < num_bkpts; i++) {
1252 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1253 new_bps.AppendByID(bp_id.GetBreakpointID());
1254 }
1255 return sberr;
1256}
1257
1258lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1259 SBError sberr;
1260 TargetSP target_sp(GetSP());
1261 if (!target_sp) {
1262 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1263 return sberr;
1264 }
1265 SBBreakpointList bkpt_list(*this);
1266 return BreakpointsWriteToFile(dest_file, bkpt_list);
1267}
1268
1269lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001270 SBBreakpointList &bkpt_list,
1271 bool append) {
Jim Ingham01f16662016-09-14 19:07:35 +00001272 SBError sberr;
1273 TargetSP target_sp(GetSP());
1274 if (!target_sp) {
1275 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1276 return sberr;
1277 }
1278
1279 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1280 BreakpointIDList bp_id_list;
1281 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001282 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1283 bp_id_list, append);
Jim Ingham01f16662016-09-14 19:07:35 +00001284 return sberr;
1285}
1286
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287uint32_t SBTarget::GetNumWatchpoints() const {
1288 TargetSP target_sp(GetSP());
1289 if (target_sp) {
1290 // The watchpoint list is thread safe, no need to lock
1291 return target_sp->GetWatchpointList().GetSize();
1292 }
1293 return 0;
1294}
1295
1296SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1297 SBWatchpoint sb_watchpoint;
1298 TargetSP target_sp(GetSP());
1299 if (target_sp) {
1300 // The watchpoint list is thread safe, no need to lock
1301 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1302 }
1303 return sb_watchpoint;
1304}
1305
1306bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1307 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1308
1309 bool result = false;
1310 TargetSP target_sp(GetSP());
1311 if (target_sp) {
1312 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1313 std::unique_lock<std::recursive_mutex> lock;
1314 target_sp->GetWatchpointList().GetListMutex(lock);
1315 result = target_sp->RemoveWatchpointByID(wp_id);
1316 }
1317
1318 if (log)
1319 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1320 static_cast<void *>(target_sp.get()),
1321 static_cast<uint32_t>(wp_id), result);
1322
1323 return result;
1324}
1325
1326SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1327 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1328
1329 SBWatchpoint sb_watchpoint;
1330 lldb::WatchpointSP watchpoint_sp;
1331 TargetSP target_sp(GetSP());
1332 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1333 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1334 std::unique_lock<std::recursive_mutex> lock;
1335 target_sp->GetWatchpointList().GetListMutex(lock);
1336 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1337 sb_watchpoint.SetSP(watchpoint_sp);
1338 }
1339
1340 if (log)
1341 log->Printf(
1342 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1343 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1344 static_cast<void *>(watchpoint_sp.get()));
1345
1346 return sb_watchpoint;
1347}
1348
1349lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1350 bool read, bool write,
1351 SBError &error) {
1352 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1353
1354 SBWatchpoint sb_watchpoint;
1355 lldb::WatchpointSP watchpoint_sp;
1356 TargetSP target_sp(GetSP());
1357 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1358 size > 0) {
1359 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1360 uint32_t watch_type = 0;
1361 if (read)
1362 watch_type |= LLDB_WATCH_TYPE_READ;
1363 if (write)
1364 watch_type |= LLDB_WATCH_TYPE_WRITE;
1365 if (watch_type == 0) {
1366 error.SetErrorString(
1367 "Can't create a watchpoint that is neither read nor write.");
1368 return sb_watchpoint;
1369 }
1370
1371 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001372 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 // This API doesn't take in a type, so we can't figure out what it is.
1374 CompilerType *type = NULL;
1375 watchpoint_sp =
1376 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1377 error.SetError(cw_error);
1378 sb_watchpoint.SetSP(watchpoint_sp);
1379 }
1380
1381 if (log)
1382 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1383 ", 0x%u) => SBWatchpoint(%p)",
1384 static_cast<void *>(target_sp.get()), addr,
1385 static_cast<uint32_t>(size),
1386 static_cast<void *>(watchpoint_sp.get()));
1387
1388 return sb_watchpoint;
1389}
1390
1391bool SBTarget::EnableAllWatchpoints() {
1392 TargetSP target_sp(GetSP());
1393 if (target_sp) {
1394 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1395 std::unique_lock<std::recursive_mutex> lock;
1396 target_sp->GetWatchpointList().GetListMutex(lock);
1397 target_sp->EnableAllWatchpoints();
1398 return true;
1399 }
1400 return false;
1401}
1402
1403bool SBTarget::DisableAllWatchpoints() {
1404 TargetSP target_sp(GetSP());
1405 if (target_sp) {
1406 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1407 std::unique_lock<std::recursive_mutex> lock;
1408 target_sp->GetWatchpointList().GetListMutex(lock);
1409 target_sp->DisableAllWatchpoints();
1410 return true;
1411 }
1412 return false;
1413}
1414
1415SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1416 SBType type) {
1417 SBValue sb_value;
1418 lldb::ValueObjectSP new_value_sp;
1419 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1420 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1421 ExecutionContext exe_ctx(
1422 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1423 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1424 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1425 exe_ctx, ast_type);
1426 }
1427 sb_value.SetSP(new_value_sp);
1428 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1429 if (log) {
1430 if (new_value_sp)
1431 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1432 static_cast<void *>(m_opaque_sp.get()),
1433 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001434 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1436 static_cast<void *>(m_opaque_sp.get()));
1437 }
1438 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001439}
1440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1442 lldb::SBType type) {
1443 SBValue sb_value;
1444 lldb::ValueObjectSP new_value_sp;
1445 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1446 DataExtractorSP extractor(*data);
1447 ExecutionContext exe_ctx(
1448 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1449 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1450 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1451 exe_ctx, ast_type);
1452 }
1453 sb_value.SetSP(new_value_sp);
1454 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1455 if (log) {
1456 if (new_value_sp)
1457 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1458 static_cast<void *>(m_opaque_sp.get()),
1459 new_value_sp->GetName().AsCString());
1460 else
1461 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1462 static_cast<void *>(m_opaque_sp.get()));
1463 }
1464 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001465}
1466
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1468 const char *expr) {
1469 SBValue sb_value;
1470 lldb::ValueObjectSP new_value_sp;
1471 if (IsValid() && name && *name && expr && *expr) {
1472 ExecutionContext exe_ctx(
1473 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1474 new_value_sp =
1475 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1476 }
1477 sb_value.SetSP(new_value_sp);
1478 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1479 if (log) {
1480 if (new_value_sp)
1481 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1482 static_cast<void *>(m_opaque_sp.get()),
1483 new_value_sp->GetName().AsCString());
1484 else
1485 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1486 static_cast<void *>(m_opaque_sp.get()));
1487 }
1488 return sb_value;
1489}
1490
1491bool SBTarget::DeleteAllWatchpoints() {
1492 TargetSP target_sp(GetSP());
1493 if (target_sp) {
1494 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1495 std::unique_lock<std::recursive_mutex> lock;
1496 target_sp->GetWatchpointList().GetListMutex(lock);
1497 target_sp->RemoveAllWatchpoints();
1498 return true;
1499 }
1500 return false;
1501}
1502
Alexander Polyakov3e7b9db2018-08-07 20:23:57 +00001503void SBTarget::AppendImageSearchPath(const char *from, const char *to,
1504 lldb::SBError &error) {
1505 TargetSP target_sp(GetSP());
1506 if (!target_sp)
1507 return error.SetErrorString("invalid target");
1508
1509 const ConstString csFrom(from), csTo(to);
1510 if (!csFrom)
1511 return error.SetErrorString("<from> path can't be empty");
1512 if (!csTo)
1513 return error.SetErrorString("<to> path can't be empty");
1514
1515 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1516 if (log)
1517 log->Printf("SBTarget(%p)::%s: '%s' -> '%s'",
1518 static_cast<void *>(target_sp.get()), __FUNCTION__,
1519 from, to);
1520 target_sp->GetImageSearchPathList().Append(csFrom, csTo, true);
1521}
1522
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1524 const char *uuid_cstr) {
1525 return AddModule(path, triple, uuid_cstr, NULL);
1526}
1527
1528lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1529 const char *uuid_cstr, const char *symfile) {
1530 lldb::SBModule sb_module;
1531 TargetSP target_sp(GetSP());
1532 if (target_sp) {
1533 ModuleSpec module_spec;
1534 if (path)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001535 module_spec.GetFileSpec().SetFile(path, false, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536
1537 if (uuid_cstr)
Pavel Labatha174bcb2018-06-21 15:24:39 +00001538 module_spec.GetUUID().SetFromStringRef(uuid_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539
1540 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001541 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1542 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543 else
1544 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1545
1546 if (symfile)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001547 module_spec.GetSymbolFileSpec().SetFile(symfile, false,
1548 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001549
1550 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1551 }
1552 return sb_module;
1553}
1554
1555lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1556 lldb::SBModule sb_module;
1557 TargetSP target_sp(GetSP());
1558 if (target_sp)
1559 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1560 return sb_module;
1561}
1562
1563bool SBTarget::AddModule(lldb::SBModule &module) {
1564 TargetSP target_sp(GetSP());
1565 if (target_sp) {
1566 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1567 return true;
1568 }
1569 return false;
1570}
1571
1572uint32_t SBTarget::GetNumModules() const {
1573 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1574
1575 uint32_t num = 0;
1576 TargetSP target_sp(GetSP());
1577 if (target_sp) {
1578 // The module list is thread safe, no need to lock
1579 num = target_sp->GetImages().GetSize();
1580 }
1581
1582 if (log)
1583 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1584 static_cast<void *>(target_sp.get()), num);
1585
1586 return num;
1587}
1588
1589void SBTarget::Clear() {
1590 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1591
1592 if (log)
1593 log->Printf("SBTarget(%p)::Clear ()",
1594 static_cast<void *>(m_opaque_sp.get()));
1595
1596 m_opaque_sp.reset();
1597}
1598
1599SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1600 SBModule sb_module;
1601 TargetSP target_sp(GetSP());
1602 if (target_sp && sb_file_spec.IsValid()) {
1603 ModuleSpec module_spec(*sb_file_spec);
1604 // The module list is thread safe, no need to lock
1605 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1606 }
1607 return sb_module;
1608}
1609
Alexander Polyakovda0c0812018-07-03 14:22:44 +00001610SBSymbolContextList
1611SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
1612 SBSymbolContextList sb_sc_list;
1613 const TargetSP target_sp(GetSP());
1614 if (target_sp && sb_file_spec.IsValid()) {
1615 const bool append = true;
1616 target_sp->GetImages().FindCompileUnits(*sb_file_spec,
1617 append, *sb_sc_list);
1618 }
1619 return sb_sc_list;
1620}
1621
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622lldb::ByteOrder SBTarget::GetByteOrder() {
1623 TargetSP target_sp(GetSP());
1624 if (target_sp)
1625 return target_sp->GetArchitecture().GetByteOrder();
1626 return eByteOrderInvalid;
1627}
1628
1629const char *SBTarget::GetTriple() {
1630 TargetSP target_sp(GetSP());
1631 if (target_sp) {
1632 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001633 // Unique the string so we don't run into ownership issues since the const
1634 // strings put the string into the string pool once and the strings never
1635 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 ConstString const_triple(triple.c_str());
1637 return const_triple.GetCString();
1638 }
1639 return NULL;
1640}
1641
1642uint32_t SBTarget::GetDataByteSize() {
1643 TargetSP target_sp(GetSP());
1644 if (target_sp) {
1645 return target_sp->GetArchitecture().GetDataByteSize();
1646 }
1647 return 0;
1648}
1649
1650uint32_t SBTarget::GetCodeByteSize() {
1651 TargetSP target_sp(GetSP());
1652 if (target_sp) {
1653 return target_sp->GetArchitecture().GetCodeByteSize();
1654 }
1655 return 0;
1656}
1657
1658uint32_t SBTarget::GetAddressByteSize() {
1659 TargetSP target_sp(GetSP());
1660 if (target_sp)
1661 return target_sp->GetArchitecture().GetAddressByteSize();
1662 return sizeof(void *);
1663}
1664
1665SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1666 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1667
1668 SBModule sb_module;
1669 ModuleSP module_sp;
1670 TargetSP target_sp(GetSP());
1671 if (target_sp) {
1672 // The module list is thread safe, no need to lock
1673 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1674 sb_module.SetSP(module_sp);
1675 }
1676
1677 if (log)
1678 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1679 static_cast<void *>(target_sp.get()), idx,
1680 static_cast<void *>(module_sp.get()));
1681
1682 return sb_module;
1683}
1684
1685bool SBTarget::RemoveModule(lldb::SBModule module) {
1686 TargetSP target_sp(GetSP());
1687 if (target_sp)
1688 return target_sp->GetImages().Remove(module.GetSP());
1689 return false;
1690}
1691
1692SBBroadcaster SBTarget::GetBroadcaster() const {
1693 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1694
1695 TargetSP target_sp(GetSP());
1696 SBBroadcaster broadcaster(target_sp.get(), false);
1697
1698 if (log)
1699 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1700 static_cast<void *>(target_sp.get()),
1701 static_cast<void *>(broadcaster.get()));
1702
1703 return broadcaster;
1704}
1705
1706bool SBTarget::GetDescription(SBStream &description,
1707 lldb::DescriptionLevel description_level) {
1708 Stream &strm = description.ref();
1709
1710 TargetSP target_sp(GetSP());
1711 if (target_sp) {
1712 target_sp->Dump(&strm, description_level);
1713 } else
1714 strm.PutCString("No value");
1715
1716 return true;
1717}
1718
1719lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1720 uint32_t name_type_mask) {
1721 lldb::SBSymbolContextList sb_sc_list;
1722 if (name && name[0]) {
Jim Ingham763b2b22015-07-07 22:12:17 +00001723 TargetSP target_sp(GetSP());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724 if (target_sp) {
1725 const bool symbols_ok = true;
1726 const bool inlines_ok = true;
1727 const bool append = true;
1728 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask,
1729 symbols_ok, inlines_ok, append,
1730 *sb_sc_list);
1731 }
1732 }
1733 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001734}
Greg Claytone14e1922012-12-04 02:22:16 +00001735
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1737 uint32_t max_matches,
1738 MatchType matchtype) {
1739 lldb::SBSymbolContextList sb_sc_list;
1740 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001741 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742 TargetSP target_sp(GetSP());
1743 if (target_sp) {
1744 std::string regexstr;
1745 switch (matchtype) {
1746 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001747 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748 true, true, *sb_sc_list);
1749 break;
1750 case eMatchTypeStartsWith:
1751 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001752 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1753 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754 break;
1755 default:
1756 target_sp->GetImages().FindFunctions(ConstString(name),
1757 eFunctionNameTypeAny, true, true,
1758 true, *sb_sc_list);
1759 break;
1760 }
1761 }
1762 }
1763 return sb_sc_list;
1764}
1765
1766lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1767 TargetSP target_sp(GetSP());
1768 if (typename_cstr && typename_cstr[0] && target_sp) {
1769 ConstString const_typename(typename_cstr);
1770 SymbolContext sc;
1771 const bool exact_match = false;
1772
1773 const ModuleList &module_list = target_sp->GetImages();
1774 size_t count = module_list.GetSize();
1775 for (size_t idx = 0; idx < count; idx++) {
1776 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1777 if (module_sp) {
1778 TypeSP type_sp(
1779 module_sp->FindFirstType(sc, const_typename, exact_match));
1780 if (type_sp)
1781 return SBType(type_sp);
1782 }
1783 }
1784
Adrian Prantl05097242018-04-30 16:49:04 +00001785 // Didn't find the type in the symbols; try the Objective-C runtime if one
1786 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787
1788 ProcessSP process_sp(target_sp->GetProcessSP());
1789
1790 if (process_sp) {
1791 ObjCLanguageRuntime *objc_language_runtime =
1792 process_sp->GetObjCLanguageRuntime();
1793
1794 if (objc_language_runtime) {
1795 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1796
1797 if (objc_decl_vendor) {
1798 std::vector<clang::NamedDecl *> decls;
1799
1800 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1801 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1802 return SBType(type);
1803 }
1804 }
1805 }
1806 }
1807 }
1808
1809 // No matches, search for basic typename matches
1810 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1811 if (clang_ast)
1812 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1813 const_typename));
1814 }
1815 return SBType();
1816}
1817
1818SBType SBTarget::GetBasicType(lldb::BasicType type) {
1819 TargetSP target_sp(GetSP());
1820 if (target_sp) {
1821 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1822 if (clang_ast)
1823 return SBType(
1824 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1825 }
1826 return SBType();
1827}
1828
1829lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1830 SBTypeList sb_type_list;
1831 TargetSP target_sp(GetSP());
1832 if (typename_cstr && typename_cstr[0] && target_sp) {
1833 ModuleList &images = target_sp->GetImages();
1834 ConstString const_typename(typename_cstr);
1835 bool exact_match = false;
1836 SymbolContext sc;
1837 TypeList type_list;
1838 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1839 uint32_t num_matches =
1840 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX,
1841 searched_symbol_files, type_list);
1842
1843 if (num_matches > 0) {
1844 for (size_t idx = 0; idx < num_matches; idx++) {
1845 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1846 if (type_sp)
1847 sb_type_list.Append(SBType(type_sp));
1848 }
1849 }
1850
1851 // Try the Objective-C runtime if one is installed
1852
1853 ProcessSP process_sp(target_sp->GetProcessSP());
1854
1855 if (process_sp) {
1856 ObjCLanguageRuntime *objc_language_runtime =
1857 process_sp->GetObjCLanguageRuntime();
1858
1859 if (objc_language_runtime) {
1860 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1861
1862 if (objc_decl_vendor) {
1863 std::vector<clang::NamedDecl *> decls;
1864
1865 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1866 for (clang::NamedDecl *decl : decls) {
1867 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1868 sb_type_list.Append(SBType(type));
1869 }
1870 }
1871 }
1872 }
1873 }
1874 }
1875
1876 if (sb_type_list.GetSize() == 0) {
1877 // No matches, search for basic typename matches
1878 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1879 if (clang_ast)
1880 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1881 clang_ast->getASTContext(), const_typename)));
1882 }
1883 }
1884 return sb_type_list;
1885}
1886
1887SBValueList SBTarget::FindGlobalVariables(const char *name,
1888 uint32_t max_matches) {
1889 SBValueList sb_value_list;
1890
1891 TargetSP target_sp(GetSP());
1892 if (name && target_sp) {
1893 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001894 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001895 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896
1897 if (match_count > 0) {
1898 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1899 if (exe_scope == NULL)
1900 exe_scope = target_sp.get();
1901 for (uint32_t i = 0; i < match_count; ++i) {
1902 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1903 exe_scope, variable_list.GetVariableAtIndex(i)));
1904 if (valobj_sp)
1905 sb_value_list.Append(SBValue(valobj_sp));
1906 }
1907 }
1908 }
1909
1910 return sb_value_list;
1911}
1912
1913SBValueList SBTarget::FindGlobalVariables(const char *name,
1914 uint32_t max_matches,
1915 MatchType matchtype) {
1916 SBValueList sb_value_list;
1917
1918 TargetSP target_sp(GetSP());
1919 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001920 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001921 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001922
1923 std::string regexstr;
1924 uint32_t match_count;
1925 switch (matchtype) {
1926 case eMatchTypeNormal:
1927 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001928 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929 break;
1930 case eMatchTypeRegex:
1931 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001932 RegularExpression(name_ref), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001933 break;
1934 case eMatchTypeStartsWith:
1935 regexstr = llvm::Regex::escape(name) + ".*";
1936 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001937 RegularExpression(regexstr), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001938 break;
1939 }
1940
1941 if (match_count > 0) {
1942 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1943 if (exe_scope == NULL)
1944 exe_scope = target_sp.get();
1945 for (uint32_t i = 0; i < match_count; ++i) {
1946 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1947 exe_scope, variable_list.GetVariableAtIndex(i)));
1948 if (valobj_sp)
1949 sb_value_list.Append(SBValue(valobj_sp));
1950 }
1951 }
1952 }
1953
1954 return sb_value_list;
1955}
1956
1957lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1958 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1959 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1960 return sb_value_list.GetValueAtIndex(0);
1961 return SBValue();
1962}
1963
1964SBSourceManager SBTarget::GetSourceManager() {
1965 SBSourceManager source_manager(*this);
1966 return source_manager;
1967}
1968
1969lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1970 uint32_t count) {
1971 return ReadInstructions(base_addr, count, NULL);
1972}
1973
1974lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1975 uint32_t count,
1976 const char *flavor_string) {
1977 SBInstructionList sb_instructions;
1978
1979 TargetSP target_sp(GetSP());
1980 if (target_sp) {
1981 Address *addr_ptr = base_addr.get();
1982
1983 if (addr_ptr) {
1984 DataBufferHeap data(
1985 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1986 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00001987 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001988 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1989 const size_t bytes_read =
1990 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
1991 data.GetByteSize(), error, &load_addr);
1992 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1993 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1994 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
1995 data.GetBytes(), bytes_read, count, data_from_file));
1996 }
1997 }
1998
1999 return sb_instructions;
2000}
2001
2002lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
2003 const void *buf,
2004 size_t size) {
2005 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
2006}
2007
2008lldb::SBInstructionList
2009SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
2010 const char *flavor_string, const void *buf,
2011 size_t size) {
2012 SBInstructionList sb_instructions;
2013
2014 TargetSP target_sp(GetSP());
2015 if (target_sp) {
2016 Address addr;
2017
2018 if (base_addr.get())
2019 addr = *base_addr.get();
2020
2021 const bool data_from_file = true;
2022
2023 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2024 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
2025 UINT32_MAX, data_from_file));
2026 }
2027
2028 return sb_instructions;
2029}
2030
2031lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
2032 const void *buf,
2033 size_t size) {
2034 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
2035 size);
2036}
2037
2038lldb::SBInstructionList
2039SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
2040 const char *flavor_string, const void *buf,
2041 size_t size) {
2042 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
2043 buf, size);
2044}
2045
2046SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
2047 lldb::addr_t section_base_addr) {
2048 SBError sb_error;
2049 TargetSP target_sp(GetSP());
2050 if (target_sp) {
2051 if (!section.IsValid()) {
2052 sb_error.SetErrorStringWithFormat("invalid section");
2053 } else {
2054 SectionSP section_sp(section.GetSP());
2055 if (section_sp) {
2056 if (section_sp->IsThreadSpecific()) {
2057 sb_error.SetErrorString(
2058 "thread specific sections are not yet supported");
2059 } else {
2060 ProcessSP process_sp(target_sp->GetProcessSP());
2061 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
2062 ModuleSP module_sp(section_sp->GetModule());
2063 if (module_sp) {
2064 ModuleList module_list;
2065 module_list.Append(module_sp);
2066 target_sp->ModulesDidLoad(module_list);
2067 }
2068 // Flush info in the process (stack frames, etc)
2069 if (process_sp)
2070 process_sp->Flush();
2071 }
2072 }
2073 }
2074 }
2075 } else {
2076 sb_error.SetErrorString("invalid target");
2077 }
2078 return sb_error;
2079}
2080
2081SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
2082 SBError sb_error;
2083
2084 TargetSP target_sp(GetSP());
2085 if (target_sp) {
2086 if (!section.IsValid()) {
2087 sb_error.SetErrorStringWithFormat("invalid section");
2088 } else {
2089 SectionSP section_sp(section.GetSP());
2090 if (section_sp) {
2091 ProcessSP process_sp(target_sp->GetProcessSP());
2092 if (target_sp->SetSectionUnloaded(section_sp)) {
2093 ModuleSP module_sp(section_sp->GetModule());
2094 if (module_sp) {
2095 ModuleList module_list;
2096 module_list.Append(module_sp);
2097 target_sp->ModulesDidUnload(module_list, false);
2098 }
2099 // Flush info in the process (stack frames, etc)
2100 if (process_sp)
2101 process_sp->Flush();
2102 }
2103 } else {
2104 sb_error.SetErrorStringWithFormat("invalid section");
2105 }
2106 }
2107 } else {
2108 sb_error.SetErrorStringWithFormat("invalid target");
2109 }
2110 return sb_error;
2111}
2112
2113SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2114 int64_t slide_offset) {
2115 SBError sb_error;
2116
2117 TargetSP target_sp(GetSP());
2118 if (target_sp) {
2119 ModuleSP module_sp(module.GetSP());
2120 if (module_sp) {
2121 bool changed = false;
2122 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2123 // The load was successful, make sure that at least some sections
2124 // changed before we notify that our module was loaded.
2125 if (changed) {
2126 ModuleList module_list;
2127 module_list.Append(module_sp);
2128 target_sp->ModulesDidLoad(module_list);
2129 // Flush info in the process (stack frames, etc)
2130 ProcessSP process_sp(target_sp->GetProcessSP());
2131 if (process_sp)
2132 process_sp->Flush();
2133 }
2134 }
2135 } else {
2136 sb_error.SetErrorStringWithFormat("invalid module");
2137 }
2138
2139 } else {
2140 sb_error.SetErrorStringWithFormat("invalid target");
2141 }
2142 return sb_error;
2143}
2144
2145SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2146 SBError sb_error;
2147
2148 char path[PATH_MAX];
2149 TargetSP target_sp(GetSP());
2150 if (target_sp) {
2151 ModuleSP module_sp(module.GetSP());
2152 if (module_sp) {
2153 ObjectFile *objfile = module_sp->GetObjectFile();
2154 if (objfile) {
2155 SectionList *section_list = objfile->GetSectionList();
2156 if (section_list) {
2157 ProcessSP process_sp(target_sp->GetProcessSP());
2158
2159 bool changed = false;
2160 const size_t num_sections = section_list->GetSize();
2161 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2162 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2163 if (section_sp)
2164 changed |= target_sp->SetSectionUnloaded(section_sp);
2165 }
2166 if (changed) {
2167 ModuleList module_list;
2168 module_list.Append(module_sp);
2169 target_sp->ModulesDidUnload(module_list, false);
2170 // Flush info in the process (stack frames, etc)
2171 ProcessSP process_sp(target_sp->GetProcessSP());
2172 if (process_sp)
2173 process_sp->Flush();
2174 }
2175 } else {
2176 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2177 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2178 path);
2179 }
2180 } else {
2181 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2182 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2183 path);
2184 }
2185 } else {
2186 sb_error.SetErrorStringWithFormat("invalid module");
2187 }
2188 } else {
2189 sb_error.SetErrorStringWithFormat("invalid target");
2190 }
2191 return sb_error;
2192}
2193
2194lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2195 lldb::SymbolType symbol_type) {
2196 SBSymbolContextList sb_sc_list;
2197 if (name && name[0]) {
2198 TargetSP target_sp(GetSP());
2199 if (target_sp) {
2200 bool append = true;
2201 target_sp->GetImages().FindSymbolsWithNameAndType(
2202 ConstString(name), symbol_type, *sb_sc_list, append);
2203 }
2204 }
2205 return sb_sc_list;
2206}
2207
2208lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2209 TargetSP target_sp(GetSP());
2210 if (!target_sp)
2211 return SBValue();
2212
2213 SBExpressionOptions options;
2214 lldb::DynamicValueType fetch_dynamic_value =
2215 target_sp->GetPreferDynamicValue();
2216 options.SetFetchDynamicValue(fetch_dynamic_value);
2217 options.SetUnwindOnError(true);
2218 return EvaluateExpression(expr, options);
2219}
2220
2221lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2222 const SBExpressionOptions &options) {
2223 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002224#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002226#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227 SBValue expr_result;
2228 ExpressionResults exe_results = eExpressionSetupError;
2229 ValueObjectSP expr_value_sp;
2230 TargetSP target_sp(GetSP());
2231 StackFrame *frame = NULL;
2232 if (target_sp) {
2233 if (expr == NULL || expr[0] == '\0') {
2234 if (log)
2235 log->Printf(
2236 "SBTarget::EvaluateExpression called with an empty expression");
2237 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002238 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002239
2240 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2241 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002242
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002243 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2245
2246 frame = exe_ctx.GetFramePtr();
2247 Target *target = exe_ctx.GetTargetPtr();
2248
2249 if (target) {
2250#ifdef LLDB_CONFIGURATION_DEBUG
2251 StreamString frame_description;
2252 if (frame)
2253 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002254 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002255 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2256 "%u) %s",
2257 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002258 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259#endif
2260 exe_results =
2261 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2262
2263 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264 } else {
2265 if (log)
2266 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2267 "reconstruct frame object for this SBTarget.");
2268 }
2269 }
2270#ifndef LLDB_DISABLE_PYTHON
2271 if (expr_log)
2272 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2273 "%s, summary %s **",
2274 expr_result.GetValue(), expr_result.GetSummary());
2275
2276 if (log)
2277 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2278 "(execution result=%d)",
2279 static_cast<void *>(frame), expr,
2280 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002281#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002282
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002284}
2285
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286lldb::addr_t SBTarget::GetStackRedZoneSize() {
2287 TargetSP target_sp(GetSP());
2288 if (target_sp) {
2289 ABISP abi_sp;
2290 ProcessSP process_sp(target_sp->GetProcessSP());
2291 if (process_sp)
2292 abi_sp = process_sp->GetABI();
2293 else
Jason Molenda43294c92017-06-29 02:57:03 +00002294 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002295 if (abi_sp)
2296 return abi_sp->GetRedZoneSize();
2297 }
2298 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002299}
Ilia K8f37ca52015-02-13 14:31:06 +00002300
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2302 lldb::SBLaunchInfo launch_info(NULL);
2303 TargetSP target_sp(GetSP());
2304 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002305 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002306 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002307}
2308
Kate Stoneb9c1b512016-09-06 20:57:50 +00002309void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2310 TargetSP target_sp(GetSP());
2311 if (target_sp)
2312 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002313}