blob: c7dee634319a5889a645ed08e433e74325dec4a6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman4c5de692010-06-09 07:44:37 +00009#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Greg Claytone0d378b2011-03-24 21:19:54 +000011#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012
Greg Claytondea8cb42011-06-29 22:09:02 +000013#include "lldb/API/SBBreakpoint.h"
Ilia Keb2c19a2015-03-10 21:59:55 +000014#include "lldb/API/SBDebugger.h"
15#include "lldb/API/SBEvent.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000016#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000018#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000020#include "lldb/API/SBModuleSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000021#include "lldb/API/SBProcess.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000023#include "lldb/API/SBStream.h"
Jim Ingham76bb8d62016-04-28 01:40:57 +000024#include "lldb/API/SBStringList.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000025#include "lldb/API/SBStructuredData.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000026#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Breakpoint/BreakpointID.h"
28#include "lldb/Breakpoint/BreakpointIDList.h"
29#include "lldb/Breakpoint/BreakpointList.h"
30#include "lldb/Breakpoint/BreakpointLocation.h"
31#include "lldb/Core/Address.h"
32#include "lldb/Core/AddressResolver.h"
33#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/Debugger.h"
35#include "lldb/Core/Disassembler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000036#include "lldb/Core/Module.h"
37#include "lldb/Core/ModuleSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000038#include "lldb/Core/STLUtils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000040#include "lldb/Core/Section.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000041#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000042#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000043#include "lldb/Core/ValueObjectList.h"
44#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000045#include "lldb/Host/Host.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000046#include "lldb/Symbol/ClangASTContext.h"
47#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000048#include "lldb/Symbol/ObjectFile.h"
Greg Claytonae088e52016-02-10 21:28:13 +000049#include "lldb/Symbol/SymbolFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000050#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000051#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000052#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000053#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000054#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000055#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000057#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058#include "lldb/Target/Target.h"
59#include "lldb/Target/TargetList.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000060#include "lldb/Utility/ArchSpec.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000061#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000062#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000063#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000064#include "lldb/Utility/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
James Y Knight2ad48212018-05-22 22:53:50 +000066#include "Commands/CommandObjectBreakpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000067#include "lldb/Interpreter/CommandReturnObject.h"
Sean Callanan237c3ed2016-12-14 21:31:31 +000068#include "llvm/Support/PrettyStackTrace.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000069#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071using namespace lldb;
72using namespace lldb_private;
73
74#define DEFAULT_DISASM_BYTE_SIZE 32
75
Oleksiy Vyalov37386142015-02-10 22:49:57 +000076namespace {
77
Zachary Turner97206d52017-05-12 04:51:55 +000078Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
Oleksiy Vyalov37386142015-02-10 22:49:57 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 auto process_sp = target.GetProcessSP();
82 if (process_sp) {
83 const auto state = process_sp->GetState();
84 if (process_sp->IsAlive() && state == eStateConnected) {
85 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +000086 // listener, so if a valid listener is supplied, we need to error out to
87 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 if (attach_info.GetListener())
Zachary Turner97206d52017-05-12 04:51:55 +000089 return Status("process is connected and already has a listener, pass "
90 "empty listener");
Oleksiy Vyalov37386142015-02-10 22:49:57 +000091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 return target.Attach(attach_info, nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097} // namespace
Oleksiy Vyalov37386142015-02-10 22:49:57 +000098
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099//----------------------------------------------------------------------
100// SBTarget constructor
101//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102SBTarget::SBTarget() : m_opaque_sp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
109 if (this != &rhs)
110 m_opaque_sp = rhs.m_opaque_sp;
111 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000112}
113
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114//----------------------------------------------------------------------
115// Destructor
116//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117SBTarget::~SBTarget() {}
118
119bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
120 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
124 return Target::TargetEventData::GetTargetFromEvent(event.get());
Ilia Keb2c19a2015-03-10 21:59:55 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
128 const ModuleList module_list =
129 Target::TargetEventData::GetModuleListFromEvent(event.get());
130 return module_list.GetSize();
Ilia Keb2c19a2015-03-10 21:59:55 +0000131}
132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
134 const SBEvent &event) {
135 const ModuleList module_list =
136 Target::TargetEventData::GetModuleListFromEvent(event.get());
137 return SBModule(module_list.GetModuleAtIndex(idx));
Ilia Keb2c19a2015-03-10 21:59:55 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140const char *SBTarget::GetBroadcasterClassName() {
141 return Target::GetStaticBroadcasterClass().AsCString();
Ilia Keb2c19a2015-03-10 21:59:55 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144bool SBTarget::IsValid() const {
145 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000146}
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148SBProcess SBTarget::GetProcess() {
149 SBProcess sb_process;
150 ProcessSP process_sp;
151 TargetSP target_sp(GetSP());
152 if (target_sp) {
153 process_sp = target_sp->GetProcessSP();
154 sb_process.SetSP(process_sp);
155 }
156
157 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
158 if (log)
159 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
160 static_cast<void *>(target_sp.get()),
161 static_cast<void *>(process_sp.get()));
162
163 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164}
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166SBPlatform SBTarget::GetPlatform() {
167 TargetSP target_sp(GetSP());
168 if (!target_sp)
169 return SBPlatform();
170
171 SBPlatform platform;
172 platform.m_opaque_sp = target_sp->GetPlatform();
173
174 return platform;
175}
176
177SBDebugger SBTarget::GetDebugger() const {
178 SBDebugger debugger;
179 TargetSP target_sp(GetSP());
180 if (target_sp)
181 debugger.reset(target_sp->GetDebugger().shared_from_this());
182 return debugger;
183}
184
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000185SBStructuredData SBTarget::GetStatistics() {
186 SBStructuredData data;
187 TargetSP target_sp(GetSP());
188 if (!target_sp)
189 return data;
190
191 auto stats_up = llvm::make_unique<StructuredData::Dictionary>();
192 int i = 0;
193 for (auto &Entry : target_sp->GetStatistics()) {
194 std::string Desc = lldb_private::GetStatDescription(
195 static_cast<lldb_private::StatisticKind>(i));
196 stats_up->AddIntegerItem(Desc, Entry);
197 i += 1;
198 }
199
200 data.m_impl_up->SetObjectSP(std::move(stats_up));
201 return data;
202}
203
Davide Italianoe1747462018-09-28 23:27:54 +0000204void SBTarget::SetCollectingStats(bool v) {
205 TargetSP target_sp(GetSP());
206 if (!target_sp)
207 return;
208 return target_sp->SetCollectingStats(v);
209}
210
211bool SBTarget::GetCollectingStats() {
212 TargetSP target_sp(GetSP());
213 if (!target_sp)
214 return false;
215 return target_sp->GetCollectingStats();
216}
217
218
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219SBProcess SBTarget::LoadCore(const char *core_file) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000220 lldb::SBError error; // Ignored
221 return LoadCore(core_file, error);
222}
223
224SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 SBProcess sb_process;
226 TargetSP target_sp(GetSP());
227 if (target_sp) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000228 FileSpec filespec(core_file);
229 FileSystem::Instance().Resolve(filespec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000231 target_sp->GetDebugger().GetListener(), "", &filespec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 if (process_sp) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000233 error.SetError(process_sp->LoadCore());
234 if (error.Success())
235 sb_process.SetSP(process_sp);
236 } else {
237 error.SetErrorString("Failed to create the process");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 }
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000239 } else {
240 error.SetErrorString("SBTarget is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 }
242 return sb_process;
243}
244
245SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
246 const char *working_directory) {
247 char *stdin_path = NULL;
248 char *stdout_path = NULL;
249 char *stderr_path = NULL;
250 uint32_t launch_flags = 0;
251 bool stop_at_entry = false;
252 SBError error;
253 SBListener listener = GetDebugger().GetListener();
254 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path,
255 working_directory, launch_flags, stop_at_entry, error);
256}
257
258SBError SBTarget::Install() {
259 SBError sb_error;
260 TargetSP target_sp(GetSP());
261 if (target_sp) {
262 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
263 sb_error.ref() = target_sp->Install(NULL);
264 }
265 return sb_error;
266}
267
268SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
269 char const **envp, const char *stdin_path,
270 const char *stdout_path, const char *stderr_path,
271 const char *working_directory,
272 uint32_t launch_flags, // See LaunchFlags
273 bool stop_at_entry, lldb::SBError &error) {
274 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
275
276 SBProcess sb_process;
277 ProcessSP process_sp;
278 TargetSP target_sp(GetSP());
279
280 if (log)
281 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
282 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
283 "stop_at_entry=%i, &error (%p))...",
284 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
285 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
286 stdout_path ? stdout_path : "NULL",
287 stderr_path ? stderr_path : "NULL",
288 working_directory ? working_directory : "NULL", launch_flags,
289 stop_at_entry, static_cast<void *>(error.get()));
290
291 if (target_sp) {
292 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
293
294 if (stop_at_entry)
295 launch_flags |= eLaunchFlagStopAtEntry;
296
297 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
298 launch_flags |= eLaunchFlagDisableASLR;
299
300 StateType state = eStateInvalid;
301 process_sp = target_sp->GetProcessSP();
302 if (process_sp) {
303 state = process_sp->GetState();
304
305 if (process_sp->IsAlive() && state != eStateConnected) {
306 if (state == eStateAttaching)
307 error.SetErrorString("process attach is in progress");
308 else
309 error.SetErrorString("a process is already being debugged");
310 return sb_process;
311 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000312 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 if (state == eStateConnected) {
315 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +0000316 // listener, so if a valid listener is supplied, we need to error out to
317 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 if (listener.IsValid()) {
319 error.SetErrorString("process is connected and already has a listener, "
320 "pass empty listener");
321 return sb_process;
322 }
323 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
326 launch_flags |= eLaunchFlagDisableSTDIO;
327
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000328 ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),
329 FileSpec(stderr_path),
330 FileSpec(working_directory), launch_flags);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331
332 Module *exe_module = target_sp->GetExecutableModulePointer();
333 if (exe_module)
334 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
335 if (argv)
336 launch_info.GetArguments().AppendArguments(argv);
337 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000338 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339
340 if (listener.IsValid())
341 launch_info.SetListener(listener.GetSP());
342
343 error.SetError(target_sp->Launch(launch_info, NULL));
344
345 sb_process.SetSP(target_sp->GetProcessSP());
346 } else {
347 error.SetErrorString("SBTarget is invalid");
348 }
349
350 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
351 if (log)
352 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
353 static_cast<void *>(target_sp.get()),
354 static_cast<void *>(sb_process.GetSP().get()),
355 error.GetCString());
356
357 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
361 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 SBProcess sb_process;
364 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 if (log)
367 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
368 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 if (target_sp) {
371 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
372 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000373 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 ProcessSP process_sp = target_sp->GetProcessSP();
375 if (process_sp) {
376 state = process_sp->GetState();
377
378 if (process_sp->IsAlive() && state != eStateConnected) {
379 if (state == eStateAttaching)
380 error.SetErrorString("process attach is in progress");
381 else
382 error.SetErrorString("a process is already being debugged");
383 return sb_process;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000386 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000387
Pavel Labath62930e52018-01-10 11:57:31 +0000388 lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 if (!launch_info.GetExecutableFile()) {
391 Module *exe_module = target_sp->GetExecutableModulePointer();
392 if (exe_module)
393 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395
396 const ArchSpec &arch_spec = target_sp->GetArchitecture();
397 if (arch_spec.IsValid())
398 launch_info.GetArchitecture() = arch_spec;
399
400 error.SetError(target_sp->Launch(launch_info, NULL));
Pavel Labath62930e52018-01-10 11:57:31 +0000401 sb_launch_info.set_ref(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 sb_process.SetSP(target_sp->GetProcessSP());
403 } else {
404 error.SetErrorString("SBTarget is invalid");
405 }
406
407 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
408 if (log)
409 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
410 static_cast<void *>(target_sp.get()),
411 static_cast<void *>(sb_process.GetSP().get()));
412
413 return sb_process;
Greg Claytonfbb76342013-11-20 21:07:01 +0000414}
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
417 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 SBProcess sb_process;
420 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 if (log)
423 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
424 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (target_sp) {
427 ProcessAttachInfo &attach_info = sb_attach_info.ref();
428 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
429 PlatformSP platform_sp = target_sp->GetPlatform();
430 // See if we can pre-verify if a process exists or not
431 if (platform_sp && platform_sp->IsConnected()) {
432 lldb::pid_t attach_pid = attach_info.GetProcessID();
433 ProcessInstanceInfo instance_info;
434 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
435 attach_info.SetUserID(instance_info.GetEffectiveUserID());
436 } else {
437 error.ref().SetErrorStringWithFormat(
438 "no process found with process ID %" PRIu64, attach_pid);
439 if (log) {
440 log->Printf("SBTarget(%p)::Attach (...) => error %s",
441 static_cast<void *>(target_sp.get()),
442 error.GetCString());
443 }
444 return sb_process;
Greg Clayton931180e2011-01-27 06:44:37 +0000445 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 error.SetError(AttachToProcess(attach_info, *target_sp));
449 if (error.Success())
450 sb_process.SetSP(target_sp->GetProcessSP());
451 } else {
452 error.SetErrorString("SBTarget is invalid");
453 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 if (log)
456 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
457 static_cast<void *>(target_sp.get()),
458 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000464 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 lldb::pid_t pid, // The process ID to attach to
466 SBError &error // An error explaining what went wrong if attach fails
467 ) {
468 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 SBProcess sb_process;
471 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 if (log)
474 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
475 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 if (target_sp) {
478 ProcessAttachInfo attach_info;
479 attach_info.SetProcessID(pid);
480 if (listener.IsValid())
481 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 ProcessInstanceInfo instance_info;
484 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
485 attach_info.SetUserID(instance_info.GetEffectiveUserID());
486
487 error.SetError(AttachToProcess(attach_info, *target_sp));
488 if (error.Success())
489 sb_process.SetSP(target_sp->GetProcessSP());
490 } else
491 error.SetErrorString("SBTarget is invalid");
492
493 if (log)
494 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
495 static_cast<void *>(target_sp.get()), __FUNCTION__,
496 static_cast<void *>(sb_process.GetSP().get()));
497 return sb_process;
498}
499
500lldb::SBProcess SBTarget::AttachToProcessWithName(
501 SBListener &listener,
502 const char *name, // basename of process to attach to
503 bool wait_for, // if true wait for a new instance of "name" to be launched
504 SBError &error // An error explaining what went wrong if attach fails
505 ) {
506 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
507
508 SBProcess sb_process;
509 TargetSP target_sp(GetSP());
510
511 if (log)
512 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
513 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
514 wait_for ? "true" : "false");
515
516 if (name && target_sp) {
517 ProcessAttachInfo attach_info;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000518 attach_info.GetExecutableFile().SetFile(name, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 attach_info.SetWaitForLaunch(wait_for);
520 if (listener.IsValid())
521 attach_info.SetListener(listener.GetSP());
522
523 error.SetError(AttachToProcess(attach_info, *target_sp));
524 if (error.Success())
525 sb_process.SetSP(target_sp->GetProcessSP());
526 } else
527 error.SetErrorString("SBTarget is invalid");
528
529 if (log)
530 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
531 static_cast<void *>(target_sp.get()), __FUNCTION__,
532 static_cast<void *>(sb_process.GetSP().get()));
533 return sb_process;
534}
535
536lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
537 const char *plugin_name,
538 SBError &error) {
539 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
540
541 SBProcess sb_process;
542 ProcessSP process_sp;
543 TargetSP target_sp(GetSP());
544
545 if (log)
546 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
547 "plugin_name=%s, error)...",
548 static_cast<void *>(target_sp.get()), url, plugin_name);
549
550 if (target_sp) {
551 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
552 if (listener.IsValid())
553 process_sp =
554 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000555 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 process_sp = target_sp->CreateProcess(
557 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
558
559 if (process_sp) {
560 sb_process.SetSP(process_sp);
561 error.SetError(process_sp->ConnectRemote(NULL, url));
562 } else {
563 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 } else {
566 error.SetErrorString("SBTarget is invalid");
567 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 if (log)
570 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
571 static_cast<void *>(target_sp.get()),
572 static_cast<void *>(process_sp.get()));
573 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000574}
575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000577
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 SBFileSpec exe_file_spec;
579 TargetSP target_sp(GetSP());
580 if (target_sp) {
581 Module *exe_module = target_sp->GetExecutableModulePointer();
582 if (exe_module)
583 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
584 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
587 if (log) {
588 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
589 static_cast<void *>(target_sp.get()),
590 static_cast<const void *>(exe_file_spec.get()));
591 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000592
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594}
595
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596bool SBTarget::operator==(const SBTarget &rhs) const {
597 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598}
599
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600bool SBTarget::operator!=(const SBTarget &rhs) const {
601 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
605
606void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
607 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000608}
609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
611 lldb::SBAddress sb_addr;
612 Address &addr = sb_addr.ref();
613 TargetSP target_sp(GetSP());
614 if (target_sp) {
615 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
616 if (target_sp->ResolveLoadAddress(vm_addr, addr))
617 return sb_addr;
618 }
619
620 // We have a load address that isn't in a section, just return an address
621 // with the offset filled in (the address) and the section set to NULL
622 addr.SetRawAddress(vm_addr);
623 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624}
625
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
627 lldb::SBAddress sb_addr;
628 Address &addr = sb_addr.ref();
629 TargetSP target_sp(GetSP());
630 if (target_sp) {
631 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
632 if (target_sp->ResolveFileAddress(file_addr, addr))
633 return sb_addr;
634 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 addr.SetRawAddress(file_addr);
637 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000638}
639
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
641 lldb::addr_t vm_addr) {
642 lldb::SBAddress sb_addr;
643 Address &addr = sb_addr.ref();
644 TargetSP target_sp(GetSP());
645 if (target_sp) {
646 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
647 if (target_sp->ResolveLoadAddress(vm_addr, addr))
648 return sb_addr;
649 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 // We have a load address that isn't in a section, just return an address
652 // with the offset filled in (the address) and the section set to NULL
653 addr.SetRawAddress(vm_addr);
654 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000655}
656
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000657SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
659 uint32_t resolve_scope) {
660 SBSymbolContext sc;
Zachary Turner991e4452018-10-25 20:45:19 +0000661 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000663 TargetSP target_sp(GetSP());
664 if (target_sp)
Zachary Turner991e4452018-10-25 20:45:19 +0000665 target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
666 sc.ref());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 }
668 return sc;
669}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000670
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
672 lldb::SBError &error) {
673 SBError sb_error;
674 size_t bytes_read = 0;
675 TargetSP target_sp(GetSP());
676 if (target_sp) {
677 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
678 bytes_read =
679 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
680 } else {
681 sb_error.SetErrorString("invalid target");
682 }
683
684 return bytes_read;
685}
686
687SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
688 uint32_t line) {
689 return SBBreakpoint(
690 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000691}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000692
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
695 uint32_t line) {
696 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
699SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
701 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000702 SBFileSpecList empty_list;
703 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
704}
705
706SBBreakpoint
707SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
708 uint32_t line, lldb::addr_t offset,
709 SBFileSpecList &sb_module_list) {
Adrian Prantl431b1582018-08-30 15:11:00 +0000710 return BreakpointCreateByLocation(sb_file_spec, line, 0, offset,
711 sb_module_list);
712}
713
714SBBreakpoint SBTarget::BreakpointCreateByLocation(
715 const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
716 lldb::addr_t offset, SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000718
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 SBBreakpoint sb_bp;
720 TargetSP target_sp(GetSP());
721 if (target_sp && line != 0) {
722 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000723
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 const LazyBool check_inlines = eLazyBoolCalculate;
725 const LazyBool skip_prologue = eLazyBoolCalculate;
726 const bool internal = false;
727 const bool hardware = false;
728 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000729 const FileSpecList *module_list = nullptr;
730 if (sb_module_list.GetSize() > 0) {
731 module_list = sb_module_list.get();
732 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000733 sb_bp = target_sp->CreateBreakpoint(
Adrian Prantl431b1582018-08-30 15:11:00 +0000734 module_list, *sb_file_spec, line, column, offset, check_inlines,
735 skip_prologue, internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 if (log) {
739 SBStream sstr;
740 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000741 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 sb_file_spec->GetPath(path, sizeof(path));
743 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
744 "SBBreakpoint(%p): %s",
745 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000746 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 return sb_bp;
750}
751
752SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
753 const char *module_name) {
754 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
755
756 SBBreakpoint sb_bp;
757 TargetSP target_sp(GetSP());
758 if (target_sp.get()) {
759 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
760
761 const bool internal = false;
762 const bool hardware = false;
763 const LazyBool skip_prologue = eLazyBoolCalculate;
764 const lldb::addr_t offset = 0;
765 if (module_name && module_name[0]) {
766 FileSpecList module_spec_list;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000767 module_spec_list.Append(FileSpec(module_name));
Pavel Labath6ac84032017-02-27 11:05:34 +0000768 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
770 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
771 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000772 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
774 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 }
777
778 if (log)
779 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
780 "module=\"%s\") => SBBreakpoint(%p)",
781 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000782 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783
784 return sb_bp;
785}
786
787lldb::SBBreakpoint
788SBTarget::BreakpointCreateByName(const char *symbol_name,
789 const SBFileSpecList &module_list,
790 const SBFileSpecList &comp_unit_list) {
Zachary Turner117b1fa2018-10-25 20:45:40 +0000791 lldb::FunctionNameType name_type_mask = eFunctionNameTypeAuto;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 return BreakpointCreateByName(symbol_name, name_type_mask,
793 eLanguageTypeUnknown, module_list,
794 comp_unit_list);
795}
796
797lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
798 const char *symbol_name, uint32_t name_type_mask,
799 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
800 return BreakpointCreateByName(symbol_name, name_type_mask,
801 eLanguageTypeUnknown, module_list,
802 comp_unit_list);
803}
804
805lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
806 const char *symbol_name, uint32_t name_type_mask,
807 LanguageType symbol_language, const SBFileSpecList &module_list,
808 const SBFileSpecList &comp_unit_list) {
809 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
810
811 SBBreakpoint sb_bp;
812 TargetSP target_sp(GetSP());
813 if (target_sp && symbol_name && symbol_name[0]) {
814 const bool internal = false;
815 const bool hardware = false;
816 const LazyBool skip_prologue = eLazyBoolCalculate;
817 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner117b1fa2018-10-25 20:45:40 +0000818 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
819 sb_bp = target_sp->CreateBreakpoint(module_list.get(), comp_unit_list.get(),
820 symbol_name, mask, symbol_language, 0,
821 skip_prologue, internal, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 }
823
824 if (log)
825 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
826 "name_type: %d) => SBBreakpoint(%p)",
827 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000828 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829
830 return sb_bp;
831}
832
833lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
834 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
835 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
836 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
837 eLanguageTypeUnknown, module_list,
838 comp_unit_list);
839}
840
841lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
842 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
843 LanguageType symbol_language, const SBFileSpecList &module_list,
844 const SBFileSpecList &comp_unit_list) {
845 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
846 eLanguageTypeUnknown, 0, module_list,
847 comp_unit_list);
848}
849
850lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
851 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
852 LanguageType symbol_language, lldb::addr_t offset,
853 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
854 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
855
856 SBBreakpoint sb_bp;
857 TargetSP target_sp(GetSP());
858 if (target_sp && num_names > 0) {
859 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
860 const bool internal = false;
861 const bool hardware = false;
Zachary Turner117b1fa2018-10-25 20:45:40 +0000862 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +0000864 sb_bp = target_sp->CreateBreakpoint(
Zachary Turner117b1fa2018-10-25 20:45:40 +0000865 module_list.get(), comp_unit_list.get(), symbol_names, num_names, mask,
866 symbol_language, offset, skip_prologue, internal, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 }
868
869 if (log) {
870 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
871 static_cast<void *>(target_sp.get()));
872 for (uint32_t i = 0; i < num_names; i++) {
873 char sep;
874 if (i < num_names - 1)
875 sep = ',';
876 else
877 sep = '}';
878 if (symbol_names[i] != NULL)
879 log->Printf("\"%s\"%c ", symbol_names[i], sep);
880 else
881 log->Printf("\"<NULL>\"%c ", sep);
882 }
883 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +0000884 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 }
886
887 return sb_bp;
888}
889
890SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
891 const char *module_name) {
892 SBFileSpecList module_spec_list;
893 SBFileSpecList comp_unit_list;
894 if (module_name && module_name[0]) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000895 module_spec_list.Append(FileSpec(module_name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 }
897 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
898 module_spec_list, comp_unit_list);
899}
900
901lldb::SBBreakpoint
902SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
903 const SBFileSpecList &module_list,
904 const SBFileSpecList &comp_unit_list) {
905 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
906 module_list, comp_unit_list);
907}
908
909lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
910 const char *symbol_name_regex, LanguageType symbol_language,
911 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
912 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
913
914 SBBreakpoint sb_bp;
915 TargetSP target_sp(GetSP());
916 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
917 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000918 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 const bool internal = false;
920 const bool hardware = false;
921 const LazyBool skip_prologue = eLazyBoolCalculate;
922
Pavel Labath6ac84032017-02-27 11:05:34 +0000923 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
925 skip_prologue, internal, hardware);
926 }
927
928 if (log)
929 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
930 "=> SBBreakpoint(%p)",
931 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +0000932 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933
934 return sb_bp;
935}
936
937SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
938 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
939
940 SBBreakpoint sb_bp;
941 TargetSP target_sp(GetSP());
942 if (target_sp) {
943 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
944 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000945 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 }
947
948 if (log)
949 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
950 ") => SBBreakpoint(%p)",
951 static_cast<void *>(target_sp.get()),
952 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +0000953 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954
955 return sb_bp;
956}
957
958SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
959 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
960
961 SBBreakpoint sb_bp;
962 TargetSP target_sp(GetSP());
963 if (!sb_address.IsValid()) {
964 if (log)
965 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
966 "invalid address",
967 static_cast<void *>(target_sp.get()));
968 return sb_bp;
969 }
970
971 if (target_sp) {
972 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
973 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000974 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 }
976
977 if (log) {
978 SBStream s;
979 sb_address.GetDescription(s);
980 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
981 "SBBreakpoint(%p)",
982 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +0000983 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 }
985
986 return sb_bp;
987}
988
989lldb::SBBreakpoint
990SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
991 const lldb::SBFileSpec &source_file,
992 const char *module_name) {
993 SBFileSpecList module_spec_list;
994
995 if (module_name && module_name[0]) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000996 module_spec_list.Append(FileSpec(module_name));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 }
998
999 SBFileSpecList source_file_list;
1000 if (source_file.IsValid()) {
1001 source_file_list.Append(source_file);
1002 }
1003
1004 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
1005 source_file_list);
1006}
1007
1008lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1009 const char *source_regex, const SBFileSpecList &module_list,
1010 const lldb::SBFileSpecList &source_file_list) {
1011 return BreakpointCreateBySourceRegex(source_regex, module_list,
1012 source_file_list, SBStringList());
1013}
1014
1015lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1016 const char *source_regex, const SBFileSpecList &module_list,
1017 const lldb::SBFileSpecList &source_file_list,
1018 const SBStringList &func_names) {
1019 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1020
1021 SBBreakpoint sb_bp;
1022 TargetSP target_sp(GetSP());
1023 if (target_sp && source_regex && source_regex[0]) {
1024 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1025 const bool hardware = false;
1026 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +00001027 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 std::unordered_set<std::string> func_names_set;
1029 for (size_t i = 0; i < func_names.GetSize(); i++) {
1030 func_names_set.insert(func_names.GetStringAtIndex(i));
1031 }
1032
Pavel Labath6ac84032017-02-27 11:05:34 +00001033 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 module_list.get(), source_file_list.get(), func_names_set, regexp,
1035 false, hardware, move_to_nearest_code);
1036 }
1037
1038 if (log)
1039 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1040 "=> SBBreakpoint(%p)",
1041 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001042 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043
1044 return sb_bp;
1045}
1046
1047lldb::SBBreakpoint
1048SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1049 bool catch_bp, bool throw_bp) {
1050 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1051
1052 SBBreakpoint sb_bp;
1053 TargetSP target_sp(GetSP());
1054 if (target_sp) {
1055 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1056 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001057 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 hardware);
1059 }
1060
1061 if (log)
Jim Ingham3815e702018-09-13 21:35:32 +00001062 log->Printf("SBTarget(%p)::BreakpointCreateForException (Language: %s, catch: "
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 "%s throw: %s) => SBBreakpoint(%p)",
1064 static_cast<void *>(target_sp.get()),
1065 Language::GetNameForLanguageType(language),
1066 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001067 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068
1069 return sb_bp;
1070}
1071
Jim Ingham3815e702018-09-13 21:35:32 +00001072lldb::SBBreakpoint
1073SBTarget::BreakpointCreateFromScript(const char *class_name,
1074 SBStructuredData &extra_args,
1075 const SBFileSpecList &module_list,
1076 const SBFileSpecList &file_list,
1077 bool request_hardware)
1078{
1079 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1080
1081 SBBreakpoint sb_bp;
1082 TargetSP target_sp(GetSP());
1083 if (target_sp) {
1084 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1085 Status error;
1086
1087 StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP();
1088 sb_bp =
1089 target_sp->CreateScriptedBreakpoint(class_name,
1090 module_list.get(),
1091 file_list.get(),
1092 false, /* internal */
1093 request_hardware,
1094 obj_sp,
1095 &error);
1096 }
1097 if (log)
1098 log->Printf("SBTarget(%p)::BreakpointCreateFromScript (class name: %s) "
1099 " => SBBreakpoint(%p)",
1100 static_cast<void *>(target_sp.get()),
1101 class_name,
1102 static_cast<void *>(sb_bp.GetSP().get()));
1103
1104 return sb_bp;
1105}
1106
1107
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108uint32_t SBTarget::GetNumBreakpoints() const {
1109 TargetSP target_sp(GetSP());
1110 if (target_sp) {
1111 // The breakpoint list is thread safe, no need to lock
1112 return target_sp->GetBreakpointList().GetSize();
1113 }
1114 return 0;
1115}
1116
1117SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1118 SBBreakpoint sb_breakpoint;
1119 TargetSP target_sp(GetSP());
1120 if (target_sp) {
1121 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001122 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 }
1124 return sb_breakpoint;
1125}
1126
1127bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1128 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1129
1130 bool result = false;
1131 TargetSP target_sp(GetSP());
1132 if (target_sp) {
1133 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1134 result = target_sp->RemoveBreakpointByID(bp_id);
1135 }
1136
1137 if (log)
1138 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1139 static_cast<void *>(target_sp.get()),
1140 static_cast<uint32_t>(bp_id), result);
1141
1142 return result;
1143}
1144
1145SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1146 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1147
1148 SBBreakpoint sb_breakpoint;
1149 TargetSP target_sp(GetSP());
1150 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1151 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001152 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 }
1154
1155 if (log)
1156 log->Printf(
1157 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1158 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001159 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160
1161 return sb_breakpoint;
1162}
1163
Jim Inghamff9a91e2016-09-21 01:21:19 +00001164bool SBTarget::FindBreakpointsByName(const char *name,
1165 SBBreakpointList &bkpts) {
1166 TargetSP target_sp(GetSP());
1167 if (target_sp) {
1168 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1169 BreakpointList bkpt_list(false);
1170 bool is_valid =
1171 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1172 if (!is_valid)
1173 return false;
1174 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1175 bkpts.AppendByID(bkpt_sp->GetID());
1176 }
1177 }
1178 return true;
1179}
1180
Jim Inghamb842f2e2017-09-14 20:22:49 +00001181void SBTarget::GetBreakpointNames(SBStringList &names)
1182{
1183 names.Clear();
1184
1185 TargetSP target_sp(GetSP());
1186 if (target_sp) {
1187 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1188
1189 std::vector<std::string> name_vec;
1190 target_sp->GetBreakpointNames(name_vec);
1191 for (auto name : name_vec)
1192 names.AppendString(name.c_str());
1193 }
1194}
1195
1196void SBTarget::DeleteBreakpointName(const char *name)
1197{
1198 TargetSP target_sp(GetSP());
1199 if (target_sp) {
1200 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001201 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001202 }
1203}
1204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205bool SBTarget::EnableAllBreakpoints() {
1206 TargetSP target_sp(GetSP());
1207 if (target_sp) {
1208 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001209 target_sp->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 return true;
1211 }
1212 return false;
1213}
1214
1215bool SBTarget::DisableAllBreakpoints() {
1216 TargetSP target_sp(GetSP());
1217 if (target_sp) {
1218 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001219 target_sp->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 return true;
1221 }
1222 return false;
1223}
1224
1225bool SBTarget::DeleteAllBreakpoints() {
1226 TargetSP target_sp(GetSP());
1227 if (target_sp) {
1228 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001229 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 return true;
1231 }
1232 return false;
1233}
1234
Jim Ingham01f16662016-09-14 19:07:35 +00001235lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1236 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001237 SBStringList empty_name_list;
1238 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1239}
1240
1241lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1242 SBStringList &matching_names,
1243 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001244 SBError sberr;
1245 TargetSP target_sp(GetSP());
1246 if (!target_sp) {
1247 sberr.SetErrorString(
1248 "BreakpointCreateFromFile called with invalid target.");
1249 return sberr;
1250 }
1251 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1252
1253 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001254
1255 std::vector<std::string> name_vector;
1256 size_t num_names = matching_names.GetSize();
1257 for (size_t i = 0; i < num_names; i++)
1258 name_vector.push_back(matching_names.GetStringAtIndex(i));
1259
1260 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1261 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001262 if (sberr.Fail())
1263 return sberr;
1264
1265 size_t num_bkpts = bp_ids.GetSize();
1266 for (size_t i = 0; i < num_bkpts; i++) {
1267 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1268 new_bps.AppendByID(bp_id.GetBreakpointID());
1269 }
1270 return sberr;
1271}
1272
1273lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1274 SBError sberr;
1275 TargetSP target_sp(GetSP());
1276 if (!target_sp) {
1277 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1278 return sberr;
1279 }
1280 SBBreakpointList bkpt_list(*this);
1281 return BreakpointsWriteToFile(dest_file, bkpt_list);
1282}
1283
1284lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001285 SBBreakpointList &bkpt_list,
1286 bool append) {
Jim Ingham01f16662016-09-14 19:07:35 +00001287 SBError sberr;
1288 TargetSP target_sp(GetSP());
1289 if (!target_sp) {
1290 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1291 return sberr;
1292 }
1293
1294 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1295 BreakpointIDList bp_id_list;
1296 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001297 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1298 bp_id_list, append);
Jim Ingham01f16662016-09-14 19:07:35 +00001299 return sberr;
1300}
1301
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302uint32_t SBTarget::GetNumWatchpoints() const {
1303 TargetSP target_sp(GetSP());
1304 if (target_sp) {
1305 // The watchpoint list is thread safe, no need to lock
1306 return target_sp->GetWatchpointList().GetSize();
1307 }
1308 return 0;
1309}
1310
1311SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1312 SBWatchpoint sb_watchpoint;
1313 TargetSP target_sp(GetSP());
1314 if (target_sp) {
1315 // The watchpoint list is thread safe, no need to lock
1316 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1317 }
1318 return sb_watchpoint;
1319}
1320
1321bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1322 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1323
1324 bool result = false;
1325 TargetSP target_sp(GetSP());
1326 if (target_sp) {
1327 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1328 std::unique_lock<std::recursive_mutex> lock;
1329 target_sp->GetWatchpointList().GetListMutex(lock);
1330 result = target_sp->RemoveWatchpointByID(wp_id);
1331 }
1332
1333 if (log)
1334 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1335 static_cast<void *>(target_sp.get()),
1336 static_cast<uint32_t>(wp_id), result);
1337
1338 return result;
1339}
1340
1341SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1342 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1343
1344 SBWatchpoint sb_watchpoint;
1345 lldb::WatchpointSP watchpoint_sp;
1346 TargetSP target_sp(GetSP());
1347 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1348 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1349 std::unique_lock<std::recursive_mutex> lock;
1350 target_sp->GetWatchpointList().GetListMutex(lock);
1351 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1352 sb_watchpoint.SetSP(watchpoint_sp);
1353 }
1354
1355 if (log)
1356 log->Printf(
1357 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1358 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1359 static_cast<void *>(watchpoint_sp.get()));
1360
1361 return sb_watchpoint;
1362}
1363
1364lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1365 bool read, bool write,
1366 SBError &error) {
1367 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1368
1369 SBWatchpoint sb_watchpoint;
1370 lldb::WatchpointSP watchpoint_sp;
1371 TargetSP target_sp(GetSP());
1372 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1373 size > 0) {
1374 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1375 uint32_t watch_type = 0;
1376 if (read)
1377 watch_type |= LLDB_WATCH_TYPE_READ;
1378 if (write)
1379 watch_type |= LLDB_WATCH_TYPE_WRITE;
1380 if (watch_type == 0) {
1381 error.SetErrorString(
1382 "Can't create a watchpoint that is neither read nor write.");
1383 return sb_watchpoint;
1384 }
1385
1386 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001387 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 // This API doesn't take in a type, so we can't figure out what it is.
1389 CompilerType *type = NULL;
1390 watchpoint_sp =
1391 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1392 error.SetError(cw_error);
1393 sb_watchpoint.SetSP(watchpoint_sp);
1394 }
1395
1396 if (log)
1397 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1398 ", 0x%u) => SBWatchpoint(%p)",
1399 static_cast<void *>(target_sp.get()), addr,
1400 static_cast<uint32_t>(size),
1401 static_cast<void *>(watchpoint_sp.get()));
1402
1403 return sb_watchpoint;
1404}
1405
1406bool SBTarget::EnableAllWatchpoints() {
1407 TargetSP target_sp(GetSP());
1408 if (target_sp) {
1409 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1410 std::unique_lock<std::recursive_mutex> lock;
1411 target_sp->GetWatchpointList().GetListMutex(lock);
1412 target_sp->EnableAllWatchpoints();
1413 return true;
1414 }
1415 return false;
1416}
1417
1418bool SBTarget::DisableAllWatchpoints() {
1419 TargetSP target_sp(GetSP());
1420 if (target_sp) {
1421 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1422 std::unique_lock<std::recursive_mutex> lock;
1423 target_sp->GetWatchpointList().GetListMutex(lock);
1424 target_sp->DisableAllWatchpoints();
1425 return true;
1426 }
1427 return false;
1428}
1429
1430SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1431 SBType type) {
1432 SBValue sb_value;
1433 lldb::ValueObjectSP new_value_sp;
1434 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1435 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1436 ExecutionContext exe_ctx(
1437 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1438 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1439 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1440 exe_ctx, ast_type);
1441 }
1442 sb_value.SetSP(new_value_sp);
1443 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1444 if (log) {
1445 if (new_value_sp)
1446 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1447 static_cast<void *>(m_opaque_sp.get()),
1448 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001449 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1451 static_cast<void *>(m_opaque_sp.get()));
1452 }
1453 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001454}
1455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1457 lldb::SBType type) {
1458 SBValue sb_value;
1459 lldb::ValueObjectSP new_value_sp;
1460 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1461 DataExtractorSP extractor(*data);
1462 ExecutionContext exe_ctx(
1463 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1464 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1465 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1466 exe_ctx, ast_type);
1467 }
1468 sb_value.SetSP(new_value_sp);
1469 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1470 if (log) {
1471 if (new_value_sp)
1472 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1473 static_cast<void *>(m_opaque_sp.get()),
1474 new_value_sp->GetName().AsCString());
1475 else
1476 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1477 static_cast<void *>(m_opaque_sp.get()));
1478 }
1479 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001480}
1481
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1483 const char *expr) {
1484 SBValue sb_value;
1485 lldb::ValueObjectSP new_value_sp;
1486 if (IsValid() && name && *name && expr && *expr) {
1487 ExecutionContext exe_ctx(
1488 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1489 new_value_sp =
1490 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1491 }
1492 sb_value.SetSP(new_value_sp);
1493 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1494 if (log) {
1495 if (new_value_sp)
1496 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1497 static_cast<void *>(m_opaque_sp.get()),
1498 new_value_sp->GetName().AsCString());
1499 else
1500 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1501 static_cast<void *>(m_opaque_sp.get()));
1502 }
1503 return sb_value;
1504}
1505
1506bool SBTarget::DeleteAllWatchpoints() {
1507 TargetSP target_sp(GetSP());
1508 if (target_sp) {
1509 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1510 std::unique_lock<std::recursive_mutex> lock;
1511 target_sp->GetWatchpointList().GetListMutex(lock);
1512 target_sp->RemoveAllWatchpoints();
1513 return true;
1514 }
1515 return false;
1516}
1517
Alexander Polyakov3e7b9db2018-08-07 20:23:57 +00001518void SBTarget::AppendImageSearchPath(const char *from, const char *to,
1519 lldb::SBError &error) {
1520 TargetSP target_sp(GetSP());
1521 if (!target_sp)
1522 return error.SetErrorString("invalid target");
1523
1524 const ConstString csFrom(from), csTo(to);
1525 if (!csFrom)
1526 return error.SetErrorString("<from> path can't be empty");
1527 if (!csTo)
1528 return error.SetErrorString("<to> path can't be empty");
1529
1530 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1531 if (log)
1532 log->Printf("SBTarget(%p)::%s: '%s' -> '%s'",
1533 static_cast<void *>(target_sp.get()), __FUNCTION__,
1534 from, to);
1535 target_sp->GetImageSearchPathList().Append(csFrom, csTo, true);
1536}
1537
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1539 const char *uuid_cstr) {
1540 return AddModule(path, triple, uuid_cstr, NULL);
1541}
1542
1543lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1544 const char *uuid_cstr, const char *symfile) {
1545 lldb::SBModule sb_module;
1546 TargetSP target_sp(GetSP());
1547 if (target_sp) {
1548 ModuleSpec module_spec;
1549 if (path)
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001550 module_spec.GetFileSpec().SetFile(path, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551
1552 if (uuid_cstr)
Pavel Labatha174bcb2018-06-21 15:24:39 +00001553 module_spec.GetUUID().SetFromStringRef(uuid_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554
1555 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001556 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1557 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 else
1559 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1560
1561 if (symfile)
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001562 module_spec.GetSymbolFileSpec().SetFile(symfile, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563
1564 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1565 }
1566 return sb_module;
1567}
1568
1569lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1570 lldb::SBModule sb_module;
1571 TargetSP target_sp(GetSP());
1572 if (target_sp)
1573 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1574 return sb_module;
1575}
1576
1577bool SBTarget::AddModule(lldb::SBModule &module) {
1578 TargetSP target_sp(GetSP());
1579 if (target_sp) {
1580 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1581 return true;
1582 }
1583 return false;
1584}
1585
1586uint32_t SBTarget::GetNumModules() const {
1587 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1588
1589 uint32_t num = 0;
1590 TargetSP target_sp(GetSP());
1591 if (target_sp) {
1592 // The module list is thread safe, no need to lock
1593 num = target_sp->GetImages().GetSize();
1594 }
1595
1596 if (log)
1597 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1598 static_cast<void *>(target_sp.get()), num);
1599
1600 return num;
1601}
1602
1603void SBTarget::Clear() {
1604 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1605
1606 if (log)
1607 log->Printf("SBTarget(%p)::Clear ()",
1608 static_cast<void *>(m_opaque_sp.get()));
1609
1610 m_opaque_sp.reset();
1611}
1612
1613SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1614 SBModule sb_module;
1615 TargetSP target_sp(GetSP());
1616 if (target_sp && sb_file_spec.IsValid()) {
1617 ModuleSpec module_spec(*sb_file_spec);
1618 // The module list is thread safe, no need to lock
1619 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1620 }
1621 return sb_module;
1622}
1623
Alexander Polyakovda0c0812018-07-03 14:22:44 +00001624SBSymbolContextList
1625SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
1626 SBSymbolContextList sb_sc_list;
1627 const TargetSP target_sp(GetSP());
1628 if (target_sp && sb_file_spec.IsValid()) {
1629 const bool append = true;
1630 target_sp->GetImages().FindCompileUnits(*sb_file_spec,
1631 append, *sb_sc_list);
1632 }
1633 return sb_sc_list;
1634}
1635
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636lldb::ByteOrder SBTarget::GetByteOrder() {
1637 TargetSP target_sp(GetSP());
1638 if (target_sp)
1639 return target_sp->GetArchitecture().GetByteOrder();
1640 return eByteOrderInvalid;
1641}
1642
1643const char *SBTarget::GetTriple() {
1644 TargetSP target_sp(GetSP());
1645 if (target_sp) {
1646 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001647 // Unique the string so we don't run into ownership issues since the const
1648 // strings put the string into the string pool once and the strings never
1649 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001650 ConstString const_triple(triple.c_str());
1651 return const_triple.GetCString();
1652 }
1653 return NULL;
1654}
1655
1656uint32_t SBTarget::GetDataByteSize() {
1657 TargetSP target_sp(GetSP());
1658 if (target_sp) {
1659 return target_sp->GetArchitecture().GetDataByteSize();
1660 }
1661 return 0;
1662}
1663
1664uint32_t SBTarget::GetCodeByteSize() {
1665 TargetSP target_sp(GetSP());
1666 if (target_sp) {
1667 return target_sp->GetArchitecture().GetCodeByteSize();
1668 }
1669 return 0;
1670}
1671
1672uint32_t SBTarget::GetAddressByteSize() {
1673 TargetSP target_sp(GetSP());
1674 if (target_sp)
1675 return target_sp->GetArchitecture().GetAddressByteSize();
1676 return sizeof(void *);
1677}
1678
1679SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1680 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1681
1682 SBModule sb_module;
1683 ModuleSP module_sp;
1684 TargetSP target_sp(GetSP());
1685 if (target_sp) {
1686 // The module list is thread safe, no need to lock
1687 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1688 sb_module.SetSP(module_sp);
1689 }
1690
1691 if (log)
1692 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1693 static_cast<void *>(target_sp.get()), idx,
1694 static_cast<void *>(module_sp.get()));
1695
1696 return sb_module;
1697}
1698
1699bool SBTarget::RemoveModule(lldb::SBModule module) {
1700 TargetSP target_sp(GetSP());
1701 if (target_sp)
1702 return target_sp->GetImages().Remove(module.GetSP());
1703 return false;
1704}
1705
1706SBBroadcaster SBTarget::GetBroadcaster() const {
1707 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1708
1709 TargetSP target_sp(GetSP());
1710 SBBroadcaster broadcaster(target_sp.get(), false);
1711
1712 if (log)
1713 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1714 static_cast<void *>(target_sp.get()),
1715 static_cast<void *>(broadcaster.get()));
1716
1717 return broadcaster;
1718}
1719
1720bool SBTarget::GetDescription(SBStream &description,
1721 lldb::DescriptionLevel description_level) {
1722 Stream &strm = description.ref();
1723
1724 TargetSP target_sp(GetSP());
1725 if (target_sp) {
1726 target_sp->Dump(&strm, description_level);
1727 } else
1728 strm.PutCString("No value");
1729
1730 return true;
1731}
1732
1733lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1734 uint32_t name_type_mask) {
1735 lldb::SBSymbolContextList sb_sc_list;
Zachary Turner117b1fa2018-10-25 20:45:40 +00001736 if (!name | !name[0])
1737 return sb_sc_list;
1738
1739 TargetSP target_sp(GetSP());
1740 if (!target_sp)
1741 return sb_sc_list;
1742
1743 const bool symbols_ok = true;
1744 const bool inlines_ok = true;
1745 const bool append = true;
1746 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
1747 target_sp->GetImages().FindFunctions(ConstString(name), mask, symbols_ok,
1748 inlines_ok, append, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001750}
Greg Claytone14e1922012-12-04 02:22:16 +00001751
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1753 uint32_t max_matches,
1754 MatchType matchtype) {
1755 lldb::SBSymbolContextList sb_sc_list;
1756 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001757 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 TargetSP target_sp(GetSP());
1759 if (target_sp) {
1760 std::string regexstr;
1761 switch (matchtype) {
1762 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001763 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 true, true, *sb_sc_list);
1765 break;
1766 case eMatchTypeStartsWith:
1767 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001768 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1769 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 break;
1771 default:
1772 target_sp->GetImages().FindFunctions(ConstString(name),
1773 eFunctionNameTypeAny, true, true,
1774 true, *sb_sc_list);
1775 break;
1776 }
1777 }
1778 }
1779 return sb_sc_list;
1780}
1781
1782lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1783 TargetSP target_sp(GetSP());
1784 if (typename_cstr && typename_cstr[0] && target_sp) {
1785 ConstString const_typename(typename_cstr);
1786 SymbolContext sc;
1787 const bool exact_match = false;
1788
1789 const ModuleList &module_list = target_sp->GetImages();
1790 size_t count = module_list.GetSize();
1791 for (size_t idx = 0; idx < count; idx++) {
1792 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1793 if (module_sp) {
1794 TypeSP type_sp(
1795 module_sp->FindFirstType(sc, const_typename, exact_match));
1796 if (type_sp)
1797 return SBType(type_sp);
1798 }
1799 }
1800
Adrian Prantl05097242018-04-30 16:49:04 +00001801 // Didn't find the type in the symbols; try the Objective-C runtime if one
1802 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803
1804 ProcessSP process_sp(target_sp->GetProcessSP());
1805
1806 if (process_sp) {
1807 ObjCLanguageRuntime *objc_language_runtime =
1808 process_sp->GetObjCLanguageRuntime();
1809
1810 if (objc_language_runtime) {
1811 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1812
1813 if (objc_decl_vendor) {
1814 std::vector<clang::NamedDecl *> decls;
1815
1816 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1817 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1818 return SBType(type);
1819 }
1820 }
1821 }
1822 }
1823 }
1824
1825 // No matches, search for basic typename matches
1826 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1827 if (clang_ast)
1828 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1829 const_typename));
1830 }
1831 return SBType();
1832}
1833
1834SBType SBTarget::GetBasicType(lldb::BasicType type) {
1835 TargetSP target_sp(GetSP());
1836 if (target_sp) {
1837 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1838 if (clang_ast)
1839 return SBType(
1840 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1841 }
1842 return SBType();
1843}
1844
1845lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1846 SBTypeList sb_type_list;
1847 TargetSP target_sp(GetSP());
1848 if (typename_cstr && typename_cstr[0] && target_sp) {
1849 ModuleList &images = target_sp->GetImages();
1850 ConstString const_typename(typename_cstr);
1851 bool exact_match = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001852 TypeList type_list;
1853 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1854 uint32_t num_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00001855 images.FindTypes(nullptr, const_typename, exact_match, UINT32_MAX,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 searched_symbol_files, type_list);
1857
1858 if (num_matches > 0) {
1859 for (size_t idx = 0; idx < num_matches; idx++) {
1860 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1861 if (type_sp)
1862 sb_type_list.Append(SBType(type_sp));
1863 }
1864 }
1865
1866 // Try the Objective-C runtime if one is installed
1867
1868 ProcessSP process_sp(target_sp->GetProcessSP());
1869
1870 if (process_sp) {
1871 ObjCLanguageRuntime *objc_language_runtime =
1872 process_sp->GetObjCLanguageRuntime();
1873
1874 if (objc_language_runtime) {
1875 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1876
1877 if (objc_decl_vendor) {
1878 std::vector<clang::NamedDecl *> decls;
1879
1880 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1881 for (clang::NamedDecl *decl : decls) {
1882 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1883 sb_type_list.Append(SBType(type));
1884 }
1885 }
1886 }
1887 }
1888 }
1889 }
1890
1891 if (sb_type_list.GetSize() == 0) {
1892 // No matches, search for basic typename matches
1893 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1894 if (clang_ast)
1895 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1896 clang_ast->getASTContext(), const_typename)));
1897 }
1898 }
1899 return sb_type_list;
1900}
1901
1902SBValueList SBTarget::FindGlobalVariables(const char *name,
1903 uint32_t max_matches) {
1904 SBValueList sb_value_list;
1905
1906 TargetSP target_sp(GetSP());
1907 if (name && target_sp) {
1908 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001909 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001910 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911
1912 if (match_count > 0) {
1913 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1914 if (exe_scope == NULL)
1915 exe_scope = target_sp.get();
1916 for (uint32_t i = 0; i < match_count; ++i) {
1917 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1918 exe_scope, variable_list.GetVariableAtIndex(i)));
1919 if (valobj_sp)
1920 sb_value_list.Append(SBValue(valobj_sp));
1921 }
1922 }
1923 }
1924
1925 return sb_value_list;
1926}
1927
1928SBValueList SBTarget::FindGlobalVariables(const char *name,
1929 uint32_t max_matches,
1930 MatchType matchtype) {
1931 SBValueList sb_value_list;
1932
1933 TargetSP target_sp(GetSP());
1934 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001935 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001936 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001937
1938 std::string regexstr;
1939 uint32_t match_count;
1940 switch (matchtype) {
1941 case eMatchTypeNormal:
1942 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001943 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001944 break;
1945 case eMatchTypeRegex:
1946 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001947 RegularExpression(name_ref), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001948 break;
1949 case eMatchTypeStartsWith:
1950 regexstr = llvm::Regex::escape(name) + ".*";
1951 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001952 RegularExpression(regexstr), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 break;
1954 }
1955
1956 if (match_count > 0) {
1957 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1958 if (exe_scope == NULL)
1959 exe_scope = target_sp.get();
1960 for (uint32_t i = 0; i < match_count; ++i) {
1961 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1962 exe_scope, variable_list.GetVariableAtIndex(i)));
1963 if (valobj_sp)
1964 sb_value_list.Append(SBValue(valobj_sp));
1965 }
1966 }
1967 }
1968
1969 return sb_value_list;
1970}
1971
1972lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1973 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1974 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1975 return sb_value_list.GetValueAtIndex(0);
1976 return SBValue();
1977}
1978
1979SBSourceManager SBTarget::GetSourceManager() {
1980 SBSourceManager source_manager(*this);
1981 return source_manager;
1982}
1983
1984lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1985 uint32_t count) {
1986 return ReadInstructions(base_addr, count, NULL);
1987}
1988
1989lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1990 uint32_t count,
1991 const char *flavor_string) {
1992 SBInstructionList sb_instructions;
1993
1994 TargetSP target_sp(GetSP());
1995 if (target_sp) {
1996 Address *addr_ptr = base_addr.get();
1997
1998 if (addr_ptr) {
1999 DataBufferHeap data(
2000 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2001 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00002002 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2004 const size_t bytes_read =
2005 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
2006 data.GetByteSize(), error, &load_addr);
2007 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2008 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2009 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
2010 data.GetBytes(), bytes_read, count, data_from_file));
2011 }
2012 }
2013
2014 return sb_instructions;
2015}
2016
2017lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
2018 const void *buf,
2019 size_t size) {
2020 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
2021}
2022
2023lldb::SBInstructionList
2024SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
2025 const char *flavor_string, const void *buf,
2026 size_t size) {
2027 SBInstructionList sb_instructions;
2028
2029 TargetSP target_sp(GetSP());
2030 if (target_sp) {
2031 Address addr;
2032
2033 if (base_addr.get())
2034 addr = *base_addr.get();
2035
2036 const bool data_from_file = true;
2037
2038 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2039 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
2040 UINT32_MAX, data_from_file));
2041 }
2042
2043 return sb_instructions;
2044}
2045
2046lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
2047 const void *buf,
2048 size_t size) {
2049 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
2050 size);
2051}
2052
2053lldb::SBInstructionList
2054SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
2055 const char *flavor_string, const void *buf,
2056 size_t size) {
2057 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
2058 buf, size);
2059}
2060
2061SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
2062 lldb::addr_t section_base_addr) {
2063 SBError sb_error;
2064 TargetSP target_sp(GetSP());
2065 if (target_sp) {
2066 if (!section.IsValid()) {
2067 sb_error.SetErrorStringWithFormat("invalid section");
2068 } else {
2069 SectionSP section_sp(section.GetSP());
2070 if (section_sp) {
2071 if (section_sp->IsThreadSpecific()) {
2072 sb_error.SetErrorString(
2073 "thread specific sections are not yet supported");
2074 } else {
2075 ProcessSP process_sp(target_sp->GetProcessSP());
2076 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
2077 ModuleSP module_sp(section_sp->GetModule());
2078 if (module_sp) {
2079 ModuleList module_list;
2080 module_list.Append(module_sp);
2081 target_sp->ModulesDidLoad(module_list);
2082 }
2083 // Flush info in the process (stack frames, etc)
2084 if (process_sp)
2085 process_sp->Flush();
2086 }
2087 }
2088 }
2089 }
2090 } else {
2091 sb_error.SetErrorString("invalid target");
2092 }
2093 return sb_error;
2094}
2095
2096SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
2097 SBError sb_error;
2098
2099 TargetSP target_sp(GetSP());
2100 if (target_sp) {
2101 if (!section.IsValid()) {
2102 sb_error.SetErrorStringWithFormat("invalid section");
2103 } else {
2104 SectionSP section_sp(section.GetSP());
2105 if (section_sp) {
2106 ProcessSP process_sp(target_sp->GetProcessSP());
2107 if (target_sp->SetSectionUnloaded(section_sp)) {
2108 ModuleSP module_sp(section_sp->GetModule());
2109 if (module_sp) {
2110 ModuleList module_list;
2111 module_list.Append(module_sp);
2112 target_sp->ModulesDidUnload(module_list, false);
2113 }
2114 // Flush info in the process (stack frames, etc)
2115 if (process_sp)
2116 process_sp->Flush();
2117 }
2118 } else {
2119 sb_error.SetErrorStringWithFormat("invalid section");
2120 }
2121 }
2122 } else {
2123 sb_error.SetErrorStringWithFormat("invalid target");
2124 }
2125 return sb_error;
2126}
2127
2128SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2129 int64_t slide_offset) {
2130 SBError sb_error;
2131
2132 TargetSP target_sp(GetSP());
2133 if (target_sp) {
2134 ModuleSP module_sp(module.GetSP());
2135 if (module_sp) {
2136 bool changed = false;
2137 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2138 // The load was successful, make sure that at least some sections
2139 // changed before we notify that our module was loaded.
2140 if (changed) {
2141 ModuleList module_list;
2142 module_list.Append(module_sp);
2143 target_sp->ModulesDidLoad(module_list);
2144 // Flush info in the process (stack frames, etc)
2145 ProcessSP process_sp(target_sp->GetProcessSP());
2146 if (process_sp)
2147 process_sp->Flush();
2148 }
2149 }
2150 } else {
2151 sb_error.SetErrorStringWithFormat("invalid module");
2152 }
2153
2154 } else {
2155 sb_error.SetErrorStringWithFormat("invalid target");
2156 }
2157 return sb_error;
2158}
2159
2160SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2161 SBError sb_error;
2162
2163 char path[PATH_MAX];
2164 TargetSP target_sp(GetSP());
2165 if (target_sp) {
2166 ModuleSP module_sp(module.GetSP());
2167 if (module_sp) {
2168 ObjectFile *objfile = module_sp->GetObjectFile();
2169 if (objfile) {
2170 SectionList *section_list = objfile->GetSectionList();
2171 if (section_list) {
2172 ProcessSP process_sp(target_sp->GetProcessSP());
2173
2174 bool changed = false;
2175 const size_t num_sections = section_list->GetSize();
2176 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2177 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2178 if (section_sp)
2179 changed |= target_sp->SetSectionUnloaded(section_sp);
2180 }
2181 if (changed) {
2182 ModuleList module_list;
2183 module_list.Append(module_sp);
2184 target_sp->ModulesDidUnload(module_list, false);
2185 // Flush info in the process (stack frames, etc)
2186 ProcessSP process_sp(target_sp->GetProcessSP());
2187 if (process_sp)
2188 process_sp->Flush();
2189 }
2190 } else {
2191 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2192 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2193 path);
2194 }
2195 } else {
2196 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2197 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2198 path);
2199 }
2200 } else {
2201 sb_error.SetErrorStringWithFormat("invalid module");
2202 }
2203 } else {
2204 sb_error.SetErrorStringWithFormat("invalid target");
2205 }
2206 return sb_error;
2207}
2208
2209lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2210 lldb::SymbolType symbol_type) {
2211 SBSymbolContextList sb_sc_list;
2212 if (name && name[0]) {
2213 TargetSP target_sp(GetSP());
2214 if (target_sp) {
2215 bool append = true;
2216 target_sp->GetImages().FindSymbolsWithNameAndType(
2217 ConstString(name), symbol_type, *sb_sc_list, append);
2218 }
2219 }
2220 return sb_sc_list;
2221}
2222
2223lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2224 TargetSP target_sp(GetSP());
2225 if (!target_sp)
2226 return SBValue();
2227
2228 SBExpressionOptions options;
2229 lldb::DynamicValueType fetch_dynamic_value =
2230 target_sp->GetPreferDynamicValue();
2231 options.SetFetchDynamicValue(fetch_dynamic_value);
2232 options.SetUnwindOnError(true);
2233 return EvaluateExpression(expr, options);
2234}
2235
2236lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2237 const SBExpressionOptions &options) {
2238 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002239#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002241#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002242 SBValue expr_result;
2243 ExpressionResults exe_results = eExpressionSetupError;
2244 ValueObjectSP expr_value_sp;
2245 TargetSP target_sp(GetSP());
2246 StackFrame *frame = NULL;
2247 if (target_sp) {
2248 if (expr == NULL || expr[0] == '\0') {
2249 if (log)
2250 log->Printf(
2251 "SBTarget::EvaluateExpression called with an empty expression");
2252 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002254
2255 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2256 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002257
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002258 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2260
2261 frame = exe_ctx.GetFramePtr();
2262 Target *target = exe_ctx.GetTargetPtr();
2263
2264 if (target) {
2265#ifdef LLDB_CONFIGURATION_DEBUG
2266 StreamString frame_description;
2267 if (frame)
2268 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002269 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2271 "%u) %s",
2272 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002273 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274#endif
2275 exe_results =
2276 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2277
2278 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002279 } else {
2280 if (log)
2281 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2282 "reconstruct frame object for this SBTarget.");
2283 }
2284 }
2285#ifndef LLDB_DISABLE_PYTHON
2286 if (expr_log)
2287 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2288 "%s, summary %s **",
2289 expr_result.GetValue(), expr_result.GetSummary());
2290
2291 if (log)
2292 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2293 "(execution result=%d)",
2294 static_cast<void *>(frame), expr,
2295 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002296#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002297
Kate Stoneb9c1b512016-09-06 20:57:50 +00002298 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002299}
2300
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301lldb::addr_t SBTarget::GetStackRedZoneSize() {
2302 TargetSP target_sp(GetSP());
2303 if (target_sp) {
2304 ABISP abi_sp;
2305 ProcessSP process_sp(target_sp->GetProcessSP());
2306 if (process_sp)
2307 abi_sp = process_sp->GetABI();
2308 else
Jason Molenda43294c92017-06-29 02:57:03 +00002309 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002310 if (abi_sp)
2311 return abi_sp->GetRedZoneSize();
2312 }
2313 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002314}
Ilia K8f37ca52015-02-13 14:31:06 +00002315
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2317 lldb::SBLaunchInfo launch_info(NULL);
2318 TargetSP target_sp(GetSP());
2319 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002320 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002321 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002322}
2323
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2325 TargetSP target_sp(GetSP());
2326 if (target_sp)
2327 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002328}