blob: 7719764631fbf94e88ca7a2dfc919e61a0aa25c7 [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"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Greg Claytone0d378b2011-03-24 21:19:54 +000012#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytondea8cb42011-06-29 22:09:02 +000014#include "lldb/API/SBBreakpoint.h"
Ilia Keb2c19a2015-03-10 21:59:55 +000015#include "lldb/API/SBDebugger.h"
16#include "lldb/API/SBEvent.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000017#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000019#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000021#include "lldb/API/SBModuleSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000022#include "lldb/API/SBProcess.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Jim Ingham76bb8d62016-04-28 01:40:57 +000025#include "lldb/API/SBStringList.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000026#include "lldb/API/SBStructuredData.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000027#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Breakpoint/BreakpointID.h"
29#include "lldb/Breakpoint/BreakpointIDList.h"
30#include "lldb/Breakpoint/BreakpointList.h"
31#include "lldb/Breakpoint/BreakpointLocation.h"
32#include "lldb/Core/Address.h"
33#include "lldb/Core/AddressResolver.h"
34#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/Module.h"
38#include "lldb/Core/ModuleSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000039#include "lldb/Core/STLUtils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Core/Section.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000042#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000043#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000044#include "lldb/Core/ValueObjectList.h"
45#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000046#include "lldb/Host/Host.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Symbol/ClangASTContext.h"
48#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000049#include "lldb/Symbol/ObjectFile.h"
Greg Claytonae088e52016-02-10 21:28:13 +000050#include "lldb/Symbol/SymbolFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000053#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000054#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000055#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000056#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000058#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059#include "lldb/Target/Target.h"
60#include "lldb/Target/TargetList.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000061#include "lldb/Utility/ArchSpec.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000062#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000063#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000064#include "lldb/Utility/Log.h"
Zachary Turner805e7102019-03-04 21:51:03 +000065#include "lldb/Utility/ProcessInfo.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000066#include "lldb/Utility/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
James Y Knight2ad48212018-05-22 22:53:50 +000068#include "Commands/CommandObjectBreakpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000069#include "lldb/Interpreter/CommandReturnObject.h"
Sean Callanan237c3ed2016-12-14 21:31:31 +000070#include "llvm/Support/PrettyStackTrace.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000071#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073using namespace lldb;
74using namespace lldb_private;
75
76#define DEFAULT_DISASM_BYTE_SIZE 32
77
Oleksiy Vyalov37386142015-02-10 22:49:57 +000078namespace {
79
Zachary Turner97206d52017-05-12 04:51:55 +000080Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
Oleksiy Vyalov37386142015-02-10 22:49:57 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 auto process_sp = target.GetProcessSP();
84 if (process_sp) {
85 const auto state = process_sp->GetState();
86 if (process_sp->IsAlive() && state == eStateConnected) {
87 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +000088 // listener, so if a valid listener is supplied, we need to error out to
89 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 if (attach_info.GetListener())
Zachary Turner97206d52017-05-12 04:51:55 +000091 return Status("process is connected and already has a listener, pass "
92 "empty listener");
Oleksiy Vyalov37386142015-02-10 22:49:57 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 return target.Attach(attach_info, nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099} // namespace
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000100
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101//----------------------------------------------------------------------
102// SBTarget constructor
103//----------------------------------------------------------------------
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000104SBTarget::SBTarget() : m_opaque_sp() {
105 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTarget);
106}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000108SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
109 LLDB_RECORD_CONSTRUCTOR(SBTarget, (const lldb::SBTarget &), rhs);
110}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000112SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {
113 LLDB_RECORD_CONSTRUCTOR(SBTarget, (const lldb::TargetSP &), target_sp);
114}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000117 LLDB_RECORD_METHOD(const lldb::SBTarget &,
118 SBTarget, operator=,(const lldb::SBTarget &), rhs);
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (this != &rhs)
121 m_opaque_sp = rhs.m_opaque_sp;
122 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000123}
124
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128SBTarget::~SBTarget() {}
129
130bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000131 LLDB_RECORD_STATIC_METHOD(bool, SBTarget, EventIsTargetEvent,
132 (const lldb::SBEvent &), event);
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000138 LLDB_RECORD_STATIC_METHOD(lldb::SBTarget, SBTarget, GetTargetFromEvent,
139 (const lldb::SBEvent &), event);
140
141 return LLDB_RECORD_RESULT(
142 Target::TargetEventData::GetTargetFromEvent(event.get()));
Ilia Keb2c19a2015-03-10 21:59:55 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000146 LLDB_RECORD_STATIC_METHOD(uint32_t, SBTarget, GetNumModulesFromEvent,
147 (const lldb::SBEvent &), event);
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 const ModuleList module_list =
150 Target::TargetEventData::GetModuleListFromEvent(event.get());
151 return module_list.GetSize();
Ilia Keb2c19a2015-03-10 21:59:55 +0000152}
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
155 const SBEvent &event) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000156 LLDB_RECORD_STATIC_METHOD(lldb::SBModule, SBTarget, GetModuleAtIndexFromEvent,
157 (const uint32_t, const lldb::SBEvent &), idx,
158 event);
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 const ModuleList module_list =
161 Target::TargetEventData::GetModuleListFromEvent(event.get());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000162 return LLDB_RECORD_RESULT(SBModule(module_list.GetModuleAtIndex(idx)));
Ilia Keb2c19a2015-03-10 21:59:55 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165const char *SBTarget::GetBroadcasterClassName() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000166 LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBTarget,
167 GetBroadcasterClassName);
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 return Target::GetStaticBroadcasterClass().AsCString();
Ilia Keb2c19a2015-03-10 21:59:55 +0000170}
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172bool SBTarget::IsValid() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000173 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTarget, IsValid);
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178SBProcess SBTarget::GetProcess() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000179 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBProcess, SBTarget, GetProcess);
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 SBProcess sb_process;
182 ProcessSP process_sp;
183 TargetSP target_sp(GetSP());
184 if (target_sp) {
185 process_sp = target_sp->GetProcessSP();
186 sb_process.SetSP(process_sp);
187 }
188
189 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
190 if (log)
191 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
192 static_cast<void *>(target_sp.get()),
193 static_cast<void *>(process_sp.get()));
194
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000195 return LLDB_RECORD_RESULT(sb_process);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198SBPlatform SBTarget::GetPlatform() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000199 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBTarget, GetPlatform);
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 TargetSP target_sp(GetSP());
202 if (!target_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000203 return LLDB_RECORD_RESULT(SBPlatform());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204
205 SBPlatform platform;
206 platform.m_opaque_sp = target_sp->GetPlatform();
207
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000208 return LLDB_RECORD_RESULT(platform);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209}
210
211SBDebugger SBTarget::GetDebugger() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000212 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBDebugger, SBTarget, GetDebugger);
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 SBDebugger debugger;
215 TargetSP target_sp(GetSP());
216 if (target_sp)
217 debugger.reset(target_sp->GetDebugger().shared_from_this());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000218 return LLDB_RECORD_RESULT(debugger);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219}
220
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000221SBStructuredData SBTarget::GetStatistics() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000222 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStructuredData, SBTarget, GetStatistics);
223
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000224 SBStructuredData data;
225 TargetSP target_sp(GetSP());
226 if (!target_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000227 return LLDB_RECORD_RESULT(data);
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000228
229 auto stats_up = llvm::make_unique<StructuredData::Dictionary>();
230 int i = 0;
231 for (auto &Entry : target_sp->GetStatistics()) {
232 std::string Desc = lldb_private::GetStatDescription(
233 static_cast<lldb_private::StatisticKind>(i));
234 stats_up->AddIntegerItem(Desc, Entry);
235 i += 1;
236 }
237
238 data.m_impl_up->SetObjectSP(std::move(stats_up));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000239 return LLDB_RECORD_RESULT(data);
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000240}
241
Davide Italianoe1747462018-09-28 23:27:54 +0000242void SBTarget::SetCollectingStats(bool v) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000243 LLDB_RECORD_METHOD(void, SBTarget, SetCollectingStats, (bool), v);
244
Davide Italianoe1747462018-09-28 23:27:54 +0000245 TargetSP target_sp(GetSP());
246 if (!target_sp)
247 return;
248 return target_sp->SetCollectingStats(v);
249}
250
251bool SBTarget::GetCollectingStats() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000252 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, GetCollectingStats);
253
Davide Italianoe1747462018-09-28 23:27:54 +0000254 TargetSP target_sp(GetSP());
255 if (!target_sp)
256 return false;
257 return target_sp->GetCollectingStats();
258}
259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260SBProcess SBTarget::LoadCore(const char *core_file) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000261 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, LoadCore, (const char *),
262 core_file);
263
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000264 lldb::SBError error; // Ignored
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000265 return LLDB_RECORD_RESULT(LoadCore(core_file, error));
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000266}
267
268SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000269 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, LoadCore,
270 (const char *, lldb::SBError &), core_file, error);
271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 SBProcess sb_process;
273 TargetSP target_sp(GetSP());
274 if (target_sp) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000275 FileSpec filespec(core_file);
276 FileSystem::Instance().Resolve(filespec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000278 target_sp->GetDebugger().GetListener(), "", &filespec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 if (process_sp) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000280 error.SetError(process_sp->LoadCore());
281 if (error.Success())
282 sb_process.SetSP(process_sp);
283 } else {
284 error.SetErrorString("Failed to create the process");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 }
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000286 } else {
287 error.SetErrorString("SBTarget is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000289 return LLDB_RECORD_RESULT(sb_process);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290}
291
292SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
293 const char *working_directory) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000294 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, LaunchSimple,
295 (const char **, const char **, const char *), argv, envp,
296 working_directory);
297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 char *stdin_path = NULL;
299 char *stdout_path = NULL;
300 char *stderr_path = NULL;
301 uint32_t launch_flags = 0;
302 bool stop_at_entry = false;
303 SBError error;
304 SBListener listener = GetDebugger().GetListener();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000305 return LLDB_RECORD_RESULT(Launch(listener, argv, envp, stdin_path,
306 stdout_path, stderr_path, working_directory,
307 launch_flags, stop_at_entry, error));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308}
309
310SBError SBTarget::Install() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000311 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBTarget, Install);
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 SBError sb_error;
314 TargetSP target_sp(GetSP());
315 if (target_sp) {
316 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
317 sb_error.ref() = target_sp->Install(NULL);
318 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000319 return LLDB_RECORD_RESULT(sb_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320}
321
322SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
323 char const **envp, const char *stdin_path,
324 const char *stdout_path, const char *stderr_path,
325 const char *working_directory,
326 uint32_t launch_flags, // See LaunchFlags
327 bool stop_at_entry, lldb::SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000328 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, Launch,
329 (lldb::SBListener &, const char **, const char **,
330 const char *, const char *, const char *, const char *,
331 uint32_t, bool, lldb::SBError &),
332 listener, argv, envp, stdin_path, stdout_path, stderr_path,
333 working_directory, launch_flags, stop_at_entry, error);
334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
336
337 SBProcess sb_process;
338 ProcessSP process_sp;
339 TargetSP target_sp(GetSP());
340
341 if (log)
342 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
343 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
344 "stop_at_entry=%i, &error (%p))...",
345 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
346 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
347 stdout_path ? stdout_path : "NULL",
348 stderr_path ? stderr_path : "NULL",
349 working_directory ? working_directory : "NULL", launch_flags,
350 stop_at_entry, static_cast<void *>(error.get()));
351
352 if (target_sp) {
353 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
354
355 if (stop_at_entry)
356 launch_flags |= eLaunchFlagStopAtEntry;
357
358 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
359 launch_flags |= eLaunchFlagDisableASLR;
360
361 StateType state = eStateInvalid;
362 process_sp = target_sp->GetProcessSP();
363 if (process_sp) {
364 state = process_sp->GetState();
365
366 if (process_sp->IsAlive() && state != eStateConnected) {
367 if (state == eStateAttaching)
368 error.SetErrorString("process attach is in progress");
369 else
370 error.SetErrorString("a process is already being debugged");
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000371 return LLDB_RECORD_RESULT(sb_process);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000373 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 if (state == eStateConnected) {
376 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +0000377 // listener, so if a valid listener is supplied, we need to error out to
378 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 if (listener.IsValid()) {
380 error.SetErrorString("process is connected and already has a listener, "
381 "pass empty listener");
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000382 return LLDB_RECORD_RESULT(sb_process);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 }
384 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
387 launch_flags |= eLaunchFlagDisableSTDIO;
388
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000389 ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),
390 FileSpec(stderr_path),
391 FileSpec(working_directory), launch_flags);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392
393 Module *exe_module = target_sp->GetExecutableModulePointer();
394 if (exe_module)
395 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
396 if (argv)
397 launch_info.GetArguments().AppendArguments(argv);
398 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000399 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400
401 if (listener.IsValid())
402 launch_info.SetListener(listener.GetSP());
403
404 error.SetError(target_sp->Launch(launch_info, NULL));
405
406 sb_process.SetSP(target_sp->GetProcessSP());
407 } else {
408 error.SetErrorString("SBTarget is invalid");
409 }
410
411 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
412 if (log)
413 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
414 static_cast<void *>(target_sp.get()),
415 static_cast<void *>(sb_process.GetSP().get()),
416 error.GetCString());
417
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000418 return LLDB_RECORD_RESULT(sb_process);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419}
420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000422 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, Launch,
423 (lldb::SBLaunchInfo &, lldb::SBError &), sb_launch_info,
424 error);
425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 SBProcess sb_process;
429 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 if (log)
432 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
433 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 if (target_sp) {
436 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
437 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000438 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 ProcessSP process_sp = target_sp->GetProcessSP();
440 if (process_sp) {
441 state = process_sp->GetState();
442
443 if (process_sp->IsAlive() && state != eStateConnected) {
444 if (state == eStateAttaching)
445 error.SetErrorString("process attach is in progress");
446 else
447 error.SetErrorString("a process is already being debugged");
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000448 return LLDB_RECORD_RESULT(sb_process);
Greg Clayton4d8ad552013-03-25 22:40:51 +0000449 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000451 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000452
Pavel Labath62930e52018-01-10 11:57:31 +0000453 lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 if (!launch_info.GetExecutableFile()) {
456 Module *exe_module = target_sp->GetExecutableModulePointer();
457 if (exe_module)
458 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000459 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460
461 const ArchSpec &arch_spec = target_sp->GetArchitecture();
462 if (arch_spec.IsValid())
463 launch_info.GetArchitecture() = arch_spec;
464
465 error.SetError(target_sp->Launch(launch_info, NULL));
Pavel Labath62930e52018-01-10 11:57:31 +0000466 sb_launch_info.set_ref(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 sb_process.SetSP(target_sp->GetProcessSP());
468 } else {
469 error.SetErrorString("SBTarget is invalid");
470 }
471
472 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
473 if (log)
474 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
475 static_cast<void *>(target_sp.get()),
476 static_cast<void *>(sb_process.GetSP().get()));
477
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000478 return LLDB_RECORD_RESULT(sb_process);
Greg Claytonfbb76342013-11-20 21:07:01 +0000479}
480
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000482 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, Attach,
483 (lldb::SBAttachInfo &, lldb::SBError &), sb_attach_info,
484 error);
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000487
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 SBProcess sb_process;
489 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 if (log)
492 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
493 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 if (target_sp) {
496 ProcessAttachInfo &attach_info = sb_attach_info.ref();
497 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
498 PlatformSP platform_sp = target_sp->GetPlatform();
499 // See if we can pre-verify if a process exists or not
500 if (platform_sp && platform_sp->IsConnected()) {
501 lldb::pid_t attach_pid = attach_info.GetProcessID();
502 ProcessInstanceInfo instance_info;
503 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
504 attach_info.SetUserID(instance_info.GetEffectiveUserID());
505 } else {
506 error.ref().SetErrorStringWithFormat(
507 "no process found with process ID %" PRIu64, attach_pid);
508 if (log) {
509 log->Printf("SBTarget(%p)::Attach (...) => error %s",
510 static_cast<void *>(target_sp.get()),
511 error.GetCString());
512 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000513 return LLDB_RECORD_RESULT(sb_process);
Greg Clayton931180e2011-01-27 06:44:37 +0000514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 error.SetError(AttachToProcess(attach_info, *target_sp));
518 if (error.Success())
519 sb_process.SetSP(target_sp->GetProcessSP());
520 } else {
521 error.SetErrorString("SBTarget is invalid");
522 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 if (log)
525 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
526 static_cast<void *>(target_sp.get()),
527 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000528
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000529 return LLDB_RECORD_RESULT(sb_process);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530}
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000533 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 lldb::pid_t pid, // The process ID to attach to
535 SBError &error // An error explaining what went wrong if attach fails
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000536) {
537 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, AttachToProcessWithID,
538 (lldb::SBListener &, lldb::pid_t, lldb::SBError &),
539 listener, pid, error);
540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 SBProcess sb_process;
544 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546 if (log)
547 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
548 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 if (target_sp) {
551 ProcessAttachInfo attach_info;
552 attach_info.SetProcessID(pid);
553 if (listener.IsValid())
554 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 ProcessInstanceInfo instance_info;
557 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
558 attach_info.SetUserID(instance_info.GetEffectiveUserID());
559
560 error.SetError(AttachToProcess(attach_info, *target_sp));
561 if (error.Success())
562 sb_process.SetSP(target_sp->GetProcessSP());
563 } else
564 error.SetErrorString("SBTarget is invalid");
565
566 if (log)
567 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
568 static_cast<void *>(target_sp.get()), __FUNCTION__,
569 static_cast<void *>(sb_process.GetSP().get()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000570 return LLDB_RECORD_RESULT(sb_process);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571}
572
573lldb::SBProcess SBTarget::AttachToProcessWithName(
574 SBListener &listener,
575 const char *name, // basename of process to attach to
576 bool wait_for, // if true wait for a new instance of "name" to be launched
577 SBError &error // An error explaining what went wrong if attach fails
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000578) {
579 LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, AttachToProcessWithName,
580 (lldb::SBListener &, const char *, bool, lldb::SBError &),
581 listener, name, wait_for, error);
582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
584
585 SBProcess sb_process;
586 TargetSP target_sp(GetSP());
587
588 if (log)
589 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
590 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
591 wait_for ? "true" : "false");
592
593 if (name && target_sp) {
594 ProcessAttachInfo attach_info;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000595 attach_info.GetExecutableFile().SetFile(name, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 attach_info.SetWaitForLaunch(wait_for);
597 if (listener.IsValid())
598 attach_info.SetListener(listener.GetSP());
599
600 error.SetError(AttachToProcess(attach_info, *target_sp));
601 if (error.Success())
602 sb_process.SetSP(target_sp->GetProcessSP());
603 } else
604 error.SetErrorString("SBTarget is invalid");
605
606 if (log)
607 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
608 static_cast<void *>(target_sp.get()), __FUNCTION__,
609 static_cast<void *>(sb_process.GetSP().get()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000610 return LLDB_RECORD_RESULT(sb_process);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611}
612
613lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
614 const char *plugin_name,
615 SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000616 LLDB_RECORD_METHOD(
617 lldb::SBProcess, SBTarget, ConnectRemote,
618 (lldb::SBListener &, const char *, const char *, lldb::SBError &),
619 listener, url, plugin_name, error);
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
622
623 SBProcess sb_process;
624 ProcessSP process_sp;
625 TargetSP target_sp(GetSP());
626
627 if (log)
628 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
629 "plugin_name=%s, error)...",
630 static_cast<void *>(target_sp.get()), url, plugin_name);
631
632 if (target_sp) {
633 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
634 if (listener.IsValid())
635 process_sp =
636 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000637 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 process_sp = target_sp->CreateProcess(
639 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
640
641 if (process_sp) {
642 sb_process.SetSP(process_sp);
643 error.SetError(process_sp->ConnectRemote(NULL, url));
644 } else {
645 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000646 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 } else {
648 error.SetErrorString("SBTarget is invalid");
649 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 if (log)
652 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
653 static_cast<void *>(target_sp.get()),
654 static_cast<void *>(process_sp.get()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000655 return LLDB_RECORD_RESULT(sb_process);
James McIlree9631aae2011-03-04 00:31:13 +0000656}
657
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658SBFileSpec SBTarget::GetExecutable() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000659 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBTarget, GetExecutable);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 SBFileSpec exe_file_spec;
662 TargetSP target_sp(GetSP());
663 if (target_sp) {
664 Module *exe_module = target_sp->GetExecutableModulePointer();
665 if (exe_module)
666 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
667 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000668
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
670 if (log) {
671 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
672 static_cast<void *>(target_sp.get()),
673 static_cast<const void *>(exe_file_spec.get()));
674 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000675
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000676 return LLDB_RECORD_RESULT(exe_file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677}
678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679bool SBTarget::operator==(const SBTarget &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000680 LLDB_RECORD_METHOD_CONST(bool, SBTarget, operator==,(const lldb::SBTarget &),
681 rhs);
682
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684}
685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686bool SBTarget::operator!=(const SBTarget &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000687 LLDB_RECORD_METHOD_CONST(bool, SBTarget, operator!=,(const lldb::SBTarget &),
688 rhs);
689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691}
692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
694
695void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
696 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000697}
698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000700 LLDB_RECORD_METHOD(lldb::SBAddress, SBTarget, ResolveLoadAddress,
701 (lldb::addr_t), vm_addr);
702
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 lldb::SBAddress sb_addr;
704 Address &addr = sb_addr.ref();
705 TargetSP target_sp(GetSP());
706 if (target_sp) {
707 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
708 if (target_sp->ResolveLoadAddress(vm_addr, addr))
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000709 return LLDB_RECORD_RESULT(sb_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 }
711
712 // We have a load address that isn't in a section, just return an address
713 // with the offset filled in (the address) and the section set to NULL
714 addr.SetRawAddress(vm_addr);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000715 return LLDB_RECORD_RESULT(sb_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716}
717
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000719 LLDB_RECORD_METHOD(lldb::SBAddress, SBTarget, ResolveFileAddress,
720 (lldb::addr_t), file_addr);
721
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 lldb::SBAddress sb_addr;
723 Address &addr = sb_addr.ref();
724 TargetSP target_sp(GetSP());
725 if (target_sp) {
726 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
727 if (target_sp->ResolveFileAddress(file_addr, addr))
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000728 return LLDB_RECORD_RESULT(sb_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000730
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 addr.SetRawAddress(file_addr);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000732 return LLDB_RECORD_RESULT(sb_addr);
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000733}
734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
736 lldb::addr_t vm_addr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000737 LLDB_RECORD_METHOD(lldb::SBAddress, SBTarget, ResolvePastLoadAddress,
738 (uint32_t, lldb::addr_t), stop_id, vm_addr);
739
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 lldb::SBAddress sb_addr;
741 Address &addr = sb_addr.ref();
742 TargetSP target_sp(GetSP());
743 if (target_sp) {
744 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
745 if (target_sp->ResolveLoadAddress(vm_addr, addr))
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000746 return LLDB_RECORD_RESULT(sb_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 // We have a load address that isn't in a section, just return an address
750 // with the offset filled in (the address) and the section set to NULL
751 addr.SetRawAddress(vm_addr);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000752 return LLDB_RECORD_RESULT(sb_addr);
Greg Claytond5944cd2013-12-06 01:12:00 +0000753}
754
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000755SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
757 uint32_t resolve_scope) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000758 LLDB_RECORD_METHOD(lldb::SBSymbolContext, SBTarget,
759 ResolveSymbolContextForAddress,
760 (const lldb::SBAddress &, uint32_t), addr, resolve_scope);
761
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 SBSymbolContext sc;
Zachary Turner991e4452018-10-25 20:45:19 +0000763 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000765 TargetSP target_sp(GetSP());
766 if (target_sp)
Zachary Turner991e4452018-10-25 20:45:19 +0000767 target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
768 sc.ref());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000770 return LLDB_RECORD_RESULT(sc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
774 lldb::SBError &error) {
775 SBError sb_error;
776 size_t bytes_read = 0;
777 TargetSP target_sp(GetSP());
778 if (target_sp) {
779 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
780 bytes_read =
781 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
782 } else {
783 sb_error.SetErrorString("invalid target");
784 }
785
786 return bytes_read;
787}
788
789SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
790 uint32_t line) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000791 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
792 (const char *, uint32_t), file, line);
793
794 return LLDB_RECORD_RESULT(
795 SBBreakpoint(BreakpointCreateByLocation(SBFileSpec(file, false), line)));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000796}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000797
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
800 uint32_t line) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000801 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
802 (const lldb::SBFileSpec &, uint32_t), sb_file_spec, line);
803
804 return LLDB_RECORD_RESULT(BreakpointCreateByLocation(sb_file_spec, line, 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805}
806
807SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
809 uint32_t line, lldb::addr_t offset) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000810 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
811 (const lldb::SBFileSpec &, uint32_t, lldb::addr_t),
812 sb_file_spec, line, offset);
813
Jim Ingham92d19602016-09-20 22:54:49 +0000814 SBFileSpecList empty_list;
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000815 return LLDB_RECORD_RESULT(
816 BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list));
Jim Ingham92d19602016-09-20 22:54:49 +0000817}
818
819SBBreakpoint
820SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
821 uint32_t line, lldb::addr_t offset,
822 SBFileSpecList &sb_module_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000823 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
824 (const lldb::SBFileSpec &, uint32_t, lldb::addr_t,
825 lldb::SBFileSpecList &),
826 sb_file_spec, line, offset, sb_module_list);
827
828 return LLDB_RECORD_RESULT(BreakpointCreateByLocation(sb_file_spec, line, 0,
829 offset, sb_module_list));
Adrian Prantl431b1582018-08-30 15:11:00 +0000830}
831
832SBBreakpoint SBTarget::BreakpointCreateByLocation(
833 const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
834 lldb::addr_t offset, SBFileSpecList &sb_module_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000835 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
836 (const lldb::SBFileSpec &, uint32_t, uint32_t,
837 lldb::addr_t, lldb::SBFileSpecList &),
838 sb_file_spec, line, column, offset, sb_module_list);
839
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 SBBreakpoint sb_bp;
843 TargetSP target_sp(GetSP());
844 if (target_sp && line != 0) {
845 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000846
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 const LazyBool check_inlines = eLazyBoolCalculate;
848 const LazyBool skip_prologue = eLazyBoolCalculate;
849 const bool internal = false;
850 const bool hardware = false;
851 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000852 const FileSpecList *module_list = nullptr;
853 if (sb_module_list.GetSize() > 0) {
854 module_list = sb_module_list.get();
855 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000856 sb_bp = target_sp->CreateBreakpoint(
Adrian Prantl431b1582018-08-30 15:11:00 +0000857 module_list, *sb_file_spec, line, column, offset, check_inlines,
858 skip_prologue, internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000860
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 if (log) {
862 SBStream sstr;
863 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000864 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 sb_file_spec->GetPath(path, sizeof(path));
866 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
867 "SBBreakpoint(%p): %s",
868 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000869 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000871
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000872 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873}
874
875SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
876 const char *module_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000877 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
878 (const char *, const char *), symbol_name, module_name);
879
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
881
882 SBBreakpoint sb_bp;
883 TargetSP target_sp(GetSP());
884 if (target_sp.get()) {
885 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
886
887 const bool internal = false;
888 const bool hardware = false;
889 const LazyBool skip_prologue = eLazyBoolCalculate;
890 const lldb::addr_t offset = 0;
891 if (module_name && module_name[0]) {
892 FileSpecList module_spec_list;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000893 module_spec_list.Append(FileSpec(module_name));
Pavel Labath6ac84032017-02-27 11:05:34 +0000894 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
896 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
897 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000898 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
900 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 }
903
904 if (log)
905 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
906 "module=\"%s\") => SBBreakpoint(%p)",
907 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000908 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000910 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911}
912
913lldb::SBBreakpoint
914SBTarget::BreakpointCreateByName(const char *symbol_name,
915 const SBFileSpecList &module_list,
916 const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000917 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
918 (const char *, const lldb::SBFileSpecList &,
919 const lldb::SBFileSpecList &),
920 symbol_name, module_list, comp_unit_list);
921
Zachary Turner117b1fa2018-10-25 20:45:40 +0000922 lldb::FunctionNameType name_type_mask = eFunctionNameTypeAuto;
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000923 return LLDB_RECORD_RESULT(
924 BreakpointCreateByName(symbol_name, name_type_mask, eLanguageTypeUnknown,
925 module_list, comp_unit_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926}
927
928lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
929 const char *symbol_name, uint32_t name_type_mask,
930 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000931 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
932 (const char *, uint32_t, const lldb::SBFileSpecList &,
933 const lldb::SBFileSpecList &),
934 symbol_name, name_type_mask, module_list, comp_unit_list);
935
936 return LLDB_RECORD_RESULT(
937 BreakpointCreateByName(symbol_name, name_type_mask, eLanguageTypeUnknown,
938 module_list, comp_unit_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939}
940
941lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
942 const char *symbol_name, uint32_t name_type_mask,
943 LanguageType symbol_language, const SBFileSpecList &module_list,
944 const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000945 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
946 (const char *, uint32_t, lldb::LanguageType,
947 const lldb::SBFileSpecList &,
948 const lldb::SBFileSpecList &),
949 symbol_name, name_type_mask, symbol_language, module_list,
950 comp_unit_list);
951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
953
954 SBBreakpoint sb_bp;
955 TargetSP target_sp(GetSP());
956 if (target_sp && symbol_name && symbol_name[0]) {
957 const bool internal = false;
958 const bool hardware = false;
959 const LazyBool skip_prologue = eLazyBoolCalculate;
960 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner117b1fa2018-10-25 20:45:40 +0000961 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
962 sb_bp = target_sp->CreateBreakpoint(module_list.get(), comp_unit_list.get(),
963 symbol_name, mask, symbol_language, 0,
964 skip_prologue, internal, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 }
966
967 if (log)
968 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
969 "name_type: %d) => SBBreakpoint(%p)",
970 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000971 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000973 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974}
975
976lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
977 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
978 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000979 LLDB_RECORD_METHOD(
980 lldb::SBBreakpoint, SBTarget, BreakpointCreateByNames,
981 (const char **, uint32_t, uint32_t, const lldb::SBFileSpecList &,
982 const lldb::SBFileSpecList &),
983 symbol_names, num_names, name_type_mask, module_list, comp_unit_list);
984
985 return LLDB_RECORD_RESULT(BreakpointCreateByNames(
986 symbol_names, num_names, name_type_mask, eLanguageTypeUnknown,
987 module_list, comp_unit_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988}
989
990lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
991 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
992 LanguageType symbol_language, const SBFileSpecList &module_list,
993 const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000994 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByNames,
995 (const char **, uint32_t, uint32_t, lldb::LanguageType,
996 const lldb::SBFileSpecList &,
997 const lldb::SBFileSpecList &),
998 symbol_names, num_names, name_type_mask, symbol_language,
999 module_list, comp_unit_list);
1000
1001 return LLDB_RECORD_RESULT(BreakpointCreateByNames(
1002 symbol_names, num_names, name_type_mask, eLanguageTypeUnknown, 0,
1003 module_list, comp_unit_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004}
1005
1006lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
1007 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
1008 LanguageType symbol_language, lldb::addr_t offset,
1009 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001010 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByNames,
1011 (const char **, uint32_t, uint32_t, lldb::LanguageType,
1012 lldb::addr_t, const lldb::SBFileSpecList &,
1013 const lldb::SBFileSpecList &),
1014 symbol_names, num_names, name_type_mask, symbol_language,
1015 offset, module_list, comp_unit_list);
1016
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1018
1019 SBBreakpoint sb_bp;
1020 TargetSP target_sp(GetSP());
1021 if (target_sp && num_names > 0) {
1022 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1023 const bool internal = false;
1024 const bool hardware = false;
Zachary Turner117b1fa2018-10-25 20:45:40 +00001025 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +00001027 sb_bp = target_sp->CreateBreakpoint(
Zachary Turner117b1fa2018-10-25 20:45:40 +00001028 module_list.get(), comp_unit_list.get(), symbol_names, num_names, mask,
1029 symbol_language, offset, skip_prologue, internal, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 }
1031
1032 if (log) {
1033 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
1034 static_cast<void *>(target_sp.get()));
1035 for (uint32_t i = 0; i < num_names; i++) {
1036 char sep;
1037 if (i < num_names - 1)
1038 sep = ',';
1039 else
1040 sep = '}';
1041 if (symbol_names[i] != NULL)
1042 log->Printf("\"%s\"%c ", symbol_names[i], sep);
1043 else
1044 log->Printf("\"<NULL>\"%c ", sep);
1045 }
1046 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +00001047 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 }
1049
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001050 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051}
1052
1053SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
1054 const char *module_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001055 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByRegex,
1056 (const char *, const char *), symbol_name_regex,
1057 module_name);
1058
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 SBFileSpecList module_spec_list;
1060 SBFileSpecList comp_unit_list;
1061 if (module_name && module_name[0]) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001062 module_spec_list.Append(FileSpec(module_name));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001064 return LLDB_RECORD_RESULT(
1065 BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
1066 module_spec_list, comp_unit_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067}
1068
1069lldb::SBBreakpoint
1070SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
1071 const SBFileSpecList &module_list,
1072 const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001073 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByRegex,
1074 (const char *, const lldb::SBFileSpecList &,
1075 const lldb::SBFileSpecList &),
1076 symbol_name_regex, module_list, comp_unit_list);
1077
1078 return LLDB_RECORD_RESULT(BreakpointCreateByRegex(
1079 symbol_name_regex, eLanguageTypeUnknown, module_list, comp_unit_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080}
1081
1082lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
1083 const char *symbol_name_regex, LanguageType symbol_language,
1084 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001085 LLDB_RECORD_METHOD(
1086 lldb::SBBreakpoint, SBTarget, BreakpointCreateByRegex,
1087 (const char *, lldb::LanguageType, const lldb::SBFileSpecList &,
1088 const lldb::SBFileSpecList &),
1089 symbol_name_regex, symbol_language, module_list, comp_unit_list);
1090
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1092
1093 SBBreakpoint sb_bp;
1094 TargetSP target_sp(GetSP());
1095 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
1096 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +00001097 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 const bool internal = false;
1099 const bool hardware = false;
1100 const LazyBool skip_prologue = eLazyBoolCalculate;
1101
Pavel Labath6ac84032017-02-27 11:05:34 +00001102 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
1104 skip_prologue, internal, hardware);
1105 }
1106
1107 if (log)
1108 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
1109 "=> SBBreakpoint(%p)",
1110 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001111 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001113 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114}
1115
1116SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001117 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByAddress,
1118 (lldb::addr_t), address);
1119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1121
1122 SBBreakpoint sb_bp;
1123 TargetSP target_sp(GetSP());
1124 if (target_sp) {
1125 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1126 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001127 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 }
1129
1130 if (log)
1131 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
1132 ") => SBBreakpoint(%p)",
1133 static_cast<void *>(target_sp.get()),
1134 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +00001135 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001137 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138}
1139
1140SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001141 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateBySBAddress,
1142 (lldb::SBAddress &), sb_address);
1143
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1145
1146 SBBreakpoint sb_bp;
1147 TargetSP target_sp(GetSP());
1148 if (!sb_address.IsValid()) {
1149 if (log)
1150 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
1151 "invalid address",
1152 static_cast<void *>(target_sp.get()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001153 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 }
1155
1156 if (target_sp) {
1157 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1158 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001159 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 }
1161
1162 if (log) {
1163 SBStream s;
1164 sb_address.GetDescription(s);
1165 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
1166 "SBBreakpoint(%p)",
1167 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +00001168 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 }
1170
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001171 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172}
1173
1174lldb::SBBreakpoint
1175SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
1176 const lldb::SBFileSpec &source_file,
1177 const char *module_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001178 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget,
1179 BreakpointCreateBySourceRegex,
1180 (const char *, const lldb::SBFileSpec &, const char *),
1181 source_regex, source_file, module_name);
1182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 SBFileSpecList module_spec_list;
1184
1185 if (module_name && module_name[0]) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001186 module_spec_list.Append(FileSpec(module_name));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187 }
1188
1189 SBFileSpecList source_file_list;
1190 if (source_file.IsValid()) {
1191 source_file_list.Append(source_file);
1192 }
1193
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001194 return LLDB_RECORD_RESULT(BreakpointCreateBySourceRegex(
1195 source_regex, module_spec_list, source_file_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196}
1197
1198lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1199 const char *source_regex, const SBFileSpecList &module_list,
1200 const lldb::SBFileSpecList &source_file_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001201 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget,
1202 BreakpointCreateBySourceRegex,
1203 (const char *, const lldb::SBFileSpecList &,
1204 const lldb::SBFileSpecList &),
1205 source_regex, module_list, source_file_list);
1206
1207 return LLDB_RECORD_RESULT(BreakpointCreateBySourceRegex(
1208 source_regex, module_list, source_file_list, SBStringList()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209}
1210
1211lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1212 const char *source_regex, const SBFileSpecList &module_list,
1213 const lldb::SBFileSpecList &source_file_list,
1214 const SBStringList &func_names) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001215 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget,
1216 BreakpointCreateBySourceRegex,
1217 (const char *, const lldb::SBFileSpecList &,
1218 const lldb::SBFileSpecList &, const lldb::SBStringList &),
1219 source_regex, module_list, source_file_list, func_names);
1220
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1222
1223 SBBreakpoint sb_bp;
1224 TargetSP target_sp(GetSP());
1225 if (target_sp && source_regex && source_regex[0]) {
1226 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1227 const bool hardware = false;
1228 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +00001229 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 std::unordered_set<std::string> func_names_set;
1231 for (size_t i = 0; i < func_names.GetSize(); i++) {
1232 func_names_set.insert(func_names.GetStringAtIndex(i));
1233 }
1234
Pavel Labath6ac84032017-02-27 11:05:34 +00001235 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 module_list.get(), source_file_list.get(), func_names_set, regexp,
1237 false, hardware, move_to_nearest_code);
1238 }
1239
1240 if (log)
1241 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1242 "=> SBBreakpoint(%p)",
1243 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001244 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001246 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247}
1248
1249lldb::SBBreakpoint
1250SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1251 bool catch_bp, bool throw_bp) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001252 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateForException,
1253 (lldb::LanguageType, bool, bool), language, catch_bp,
1254 throw_bp);
1255
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1257
1258 SBBreakpoint sb_bp;
1259 TargetSP target_sp(GetSP());
1260 if (target_sp) {
1261 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1262 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001263 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264 hardware);
1265 }
1266
1267 if (log)
Jim Ingham3815e702018-09-13 21:35:32 +00001268 log->Printf("SBTarget(%p)::BreakpointCreateForException (Language: %s, catch: "
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 "%s throw: %s) => SBBreakpoint(%p)",
1270 static_cast<void *>(target_sp.get()),
1271 Language::GetNameForLanguageType(language),
1272 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001273 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001275 return LLDB_RECORD_RESULT(sb_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276}
1277
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001278lldb::SBBreakpoint SBTarget::BreakpointCreateFromScript(
1279 const char *class_name, SBStructuredData &extra_args,
1280 const SBFileSpecList &module_list, const SBFileSpecList &file_list,
1281 bool request_hardware) {
1282 LLDB_RECORD_METHOD(
1283 lldb::SBBreakpoint, SBTarget, BreakpointCreateFromScript,
1284 (const char *, lldb::SBStructuredData &, const lldb::SBFileSpecList &,
1285 const lldb::SBFileSpecList &, bool),
1286 class_name, extra_args, module_list, file_list, request_hardware);
1287
Jim Ingham3815e702018-09-13 21:35:32 +00001288 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1289
1290 SBBreakpoint sb_bp;
1291 TargetSP target_sp(GetSP());
1292 if (target_sp) {
1293 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1294 Status error;
1295
1296 StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP();
1297 sb_bp =
1298 target_sp->CreateScriptedBreakpoint(class_name,
1299 module_list.get(),
1300 file_list.get(),
1301 false, /* internal */
1302 request_hardware,
1303 obj_sp,
1304 &error);
1305 }
1306 if (log)
1307 log->Printf("SBTarget(%p)::BreakpointCreateFromScript (class name: %s) "
1308 " => SBBreakpoint(%p)",
1309 static_cast<void *>(target_sp.get()),
1310 class_name,
1311 static_cast<void *>(sb_bp.GetSP().get()));
1312
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001313 return LLDB_RECORD_RESULT(sb_bp);
Jim Ingham3815e702018-09-13 21:35:32 +00001314}
1315
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316uint32_t SBTarget::GetNumBreakpoints() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001317 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetNumBreakpoints);
1318
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 TargetSP target_sp(GetSP());
1320 if (target_sp) {
1321 // The breakpoint list is thread safe, no need to lock
1322 return target_sp->GetBreakpointList().GetSize();
1323 }
1324 return 0;
1325}
1326
1327SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001328 LLDB_RECORD_METHOD_CONST(lldb::SBBreakpoint, SBTarget, GetBreakpointAtIndex,
1329 (uint32_t), idx);
1330
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 SBBreakpoint sb_breakpoint;
1332 TargetSP target_sp(GetSP());
1333 if (target_sp) {
1334 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001335 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001337 return LLDB_RECORD_RESULT(sb_breakpoint);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338}
1339
1340bool SBTarget::BreakpointDelete(break_id_t bp_id) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001341 LLDB_RECORD_METHOD(bool, SBTarget, BreakpointDelete, (lldb::break_id_t),
1342 bp_id);
1343
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1345
1346 bool result = false;
1347 TargetSP target_sp(GetSP());
1348 if (target_sp) {
1349 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1350 result = target_sp->RemoveBreakpointByID(bp_id);
1351 }
1352
1353 if (log)
1354 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1355 static_cast<void *>(target_sp.get()),
1356 static_cast<uint32_t>(bp_id), result);
1357
1358 return result;
1359}
1360
1361SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001362 LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, FindBreakpointByID,
1363 (lldb::break_id_t), bp_id);
1364
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1366
1367 SBBreakpoint sb_breakpoint;
1368 TargetSP target_sp(GetSP());
1369 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1370 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001371 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001372 }
1373
1374 if (log)
1375 log->Printf(
1376 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1377 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001378 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001380 return LLDB_RECORD_RESULT(sb_breakpoint);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381}
1382
Jim Inghamff9a91e2016-09-21 01:21:19 +00001383bool SBTarget::FindBreakpointsByName(const char *name,
1384 SBBreakpointList &bkpts) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001385 LLDB_RECORD_METHOD(bool, SBTarget, FindBreakpointsByName,
1386 (const char *, lldb::SBBreakpointList &), name, bkpts);
1387
Jim Inghamff9a91e2016-09-21 01:21:19 +00001388 TargetSP target_sp(GetSP());
1389 if (target_sp) {
1390 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1391 BreakpointList bkpt_list(false);
1392 bool is_valid =
1393 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1394 if (!is_valid)
1395 return false;
1396 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1397 bkpts.AppendByID(bkpt_sp->GetID());
1398 }
1399 }
1400 return true;
1401}
1402
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001403void SBTarget::GetBreakpointNames(SBStringList &names) {
1404 LLDB_RECORD_METHOD(void, SBTarget, GetBreakpointNames, (lldb::SBStringList &),
1405 names);
1406
Jim Inghamb842f2e2017-09-14 20:22:49 +00001407 names.Clear();
1408
1409 TargetSP target_sp(GetSP());
1410 if (target_sp) {
1411 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1412
1413 std::vector<std::string> name_vec;
1414 target_sp->GetBreakpointNames(name_vec);
1415 for (auto name : name_vec)
1416 names.AppendString(name.c_str());
1417 }
1418}
1419
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001420void SBTarget::DeleteBreakpointName(const char *name) {
1421 LLDB_RECORD_METHOD(void, SBTarget, DeleteBreakpointName, (const char *),
1422 name);
1423
Jim Inghamb842f2e2017-09-14 20:22:49 +00001424 TargetSP target_sp(GetSP());
1425 if (target_sp) {
1426 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001427 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001428 }
1429}
1430
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431bool SBTarget::EnableAllBreakpoints() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001432 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, EnableAllBreakpoints);
1433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 TargetSP target_sp(GetSP());
1435 if (target_sp) {
1436 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001437 target_sp->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 return true;
1439 }
1440 return false;
1441}
1442
1443bool SBTarget::DisableAllBreakpoints() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001444 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DisableAllBreakpoints);
1445
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446 TargetSP target_sp(GetSP());
1447 if (target_sp) {
1448 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001449 target_sp->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 return true;
1451 }
1452 return false;
1453}
1454
1455bool SBTarget::DeleteAllBreakpoints() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001456 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DeleteAllBreakpoints);
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 TargetSP target_sp(GetSP());
1459 if (target_sp) {
1460 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001461 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 return true;
1463 }
1464 return false;
1465}
1466
Jim Ingham01f16662016-09-14 19:07:35 +00001467lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1468 SBBreakpointList &new_bps) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001469 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, BreakpointsCreateFromFile,
1470 (lldb::SBFileSpec &, lldb::SBBreakpointList &),
1471 source_file, new_bps);
1472
Jim Ingham3acdf382016-09-22 22:20:28 +00001473 SBStringList empty_name_list;
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001474 return LLDB_RECORD_RESULT(
1475 BreakpointsCreateFromFile(source_file, empty_name_list, new_bps));
Jim Ingham3acdf382016-09-22 22:20:28 +00001476}
1477
1478lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1479 SBStringList &matching_names,
1480 SBBreakpointList &new_bps) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001481 LLDB_RECORD_METHOD(
1482 lldb::SBError, SBTarget, BreakpointsCreateFromFile,
1483 (lldb::SBFileSpec &, lldb::SBStringList &, lldb::SBBreakpointList &),
1484 source_file, matching_names, new_bps);
1485
Jim Ingham01f16662016-09-14 19:07:35 +00001486 SBError sberr;
1487 TargetSP target_sp(GetSP());
1488 if (!target_sp) {
1489 sberr.SetErrorString(
1490 "BreakpointCreateFromFile called with invalid target.");
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001491 return LLDB_RECORD_RESULT(sberr);
Jim Ingham01f16662016-09-14 19:07:35 +00001492 }
1493 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1494
1495 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001496
1497 std::vector<std::string> name_vector;
1498 size_t num_names = matching_names.GetSize();
1499 for (size_t i = 0; i < num_names; i++)
1500 name_vector.push_back(matching_names.GetStringAtIndex(i));
1501
1502 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1503 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001504 if (sberr.Fail())
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001505 return LLDB_RECORD_RESULT(sberr);
Jim Ingham01f16662016-09-14 19:07:35 +00001506
1507 size_t num_bkpts = bp_ids.GetSize();
1508 for (size_t i = 0; i < num_bkpts; i++) {
1509 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1510 new_bps.AppendByID(bp_id.GetBreakpointID());
1511 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001512 return LLDB_RECORD_RESULT(sberr);
Jim Ingham01f16662016-09-14 19:07:35 +00001513}
1514
1515lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001516 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, BreakpointsWriteToFile,
1517 (lldb::SBFileSpec &), dest_file);
1518
Jim Ingham01f16662016-09-14 19:07:35 +00001519 SBError sberr;
1520 TargetSP target_sp(GetSP());
1521 if (!target_sp) {
1522 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001523 return LLDB_RECORD_RESULT(sberr);
Jim Ingham01f16662016-09-14 19:07:35 +00001524 }
1525 SBBreakpointList bkpt_list(*this);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001526 return LLDB_RECORD_RESULT(BreakpointsWriteToFile(dest_file, bkpt_list));
Jim Ingham01f16662016-09-14 19:07:35 +00001527}
1528
1529lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001530 SBBreakpointList &bkpt_list,
1531 bool append) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001532 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, BreakpointsWriteToFile,
1533 (lldb::SBFileSpec &, lldb::SBBreakpointList &, bool),
1534 dest_file, bkpt_list, append);
1535
Jim Ingham01f16662016-09-14 19:07:35 +00001536 SBError sberr;
1537 TargetSP target_sp(GetSP());
1538 if (!target_sp) {
1539 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001540 return LLDB_RECORD_RESULT(sberr);
Jim Ingham01f16662016-09-14 19:07:35 +00001541 }
1542
1543 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1544 BreakpointIDList bp_id_list;
1545 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001546 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1547 bp_id_list, append);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001548 return LLDB_RECORD_RESULT(sberr);
Jim Ingham01f16662016-09-14 19:07:35 +00001549}
1550
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551uint32_t SBTarget::GetNumWatchpoints() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001552 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetNumWatchpoints);
1553
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 TargetSP target_sp(GetSP());
1555 if (target_sp) {
1556 // The watchpoint list is thread safe, no need to lock
1557 return target_sp->GetWatchpointList().GetSize();
1558 }
1559 return 0;
1560}
1561
1562SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001563 LLDB_RECORD_METHOD_CONST(lldb::SBWatchpoint, SBTarget, GetWatchpointAtIndex,
1564 (uint32_t), idx);
1565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 SBWatchpoint sb_watchpoint;
1567 TargetSP target_sp(GetSP());
1568 if (target_sp) {
1569 // The watchpoint list is thread safe, no need to lock
1570 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1571 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001572 return LLDB_RECORD_RESULT(sb_watchpoint);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573}
1574
1575bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001576 LLDB_RECORD_METHOD(bool, SBTarget, DeleteWatchpoint, (lldb::watch_id_t),
1577 wp_id);
1578
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1580
1581 bool result = false;
1582 TargetSP target_sp(GetSP());
1583 if (target_sp) {
1584 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1585 std::unique_lock<std::recursive_mutex> lock;
1586 target_sp->GetWatchpointList().GetListMutex(lock);
1587 result = target_sp->RemoveWatchpointByID(wp_id);
1588 }
1589
1590 if (log)
1591 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1592 static_cast<void *>(target_sp.get()),
1593 static_cast<uint32_t>(wp_id), result);
1594
1595 return result;
1596}
1597
1598SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001599 LLDB_RECORD_METHOD(lldb::SBWatchpoint, SBTarget, FindWatchpointByID,
1600 (lldb::watch_id_t), wp_id);
1601
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1603
1604 SBWatchpoint sb_watchpoint;
1605 lldb::WatchpointSP watchpoint_sp;
1606 TargetSP target_sp(GetSP());
1607 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1608 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1609 std::unique_lock<std::recursive_mutex> lock;
1610 target_sp->GetWatchpointList().GetListMutex(lock);
1611 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1612 sb_watchpoint.SetSP(watchpoint_sp);
1613 }
1614
1615 if (log)
1616 log->Printf(
1617 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1618 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1619 static_cast<void *>(watchpoint_sp.get()));
1620
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001621 return LLDB_RECORD_RESULT(sb_watchpoint);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622}
1623
1624lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1625 bool read, bool write,
1626 SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001627 LLDB_RECORD_METHOD(lldb::SBWatchpoint, SBTarget, WatchAddress,
1628 (lldb::addr_t, size_t, bool, bool, lldb::SBError &), addr,
1629 size, read, write, error);
1630
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1632
1633 SBWatchpoint sb_watchpoint;
1634 lldb::WatchpointSP watchpoint_sp;
1635 TargetSP target_sp(GetSP());
1636 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1637 size > 0) {
1638 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1639 uint32_t watch_type = 0;
1640 if (read)
1641 watch_type |= LLDB_WATCH_TYPE_READ;
1642 if (write)
1643 watch_type |= LLDB_WATCH_TYPE_WRITE;
1644 if (watch_type == 0) {
1645 error.SetErrorString(
1646 "Can't create a watchpoint that is neither read nor write.");
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001647 return LLDB_RECORD_RESULT(sb_watchpoint);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 }
1649
1650 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001651 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652 // This API doesn't take in a type, so we can't figure out what it is.
1653 CompilerType *type = NULL;
1654 watchpoint_sp =
1655 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1656 error.SetError(cw_error);
1657 sb_watchpoint.SetSP(watchpoint_sp);
1658 }
1659
1660 if (log)
1661 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1662 ", 0x%u) => SBWatchpoint(%p)",
1663 static_cast<void *>(target_sp.get()), addr,
1664 static_cast<uint32_t>(size),
1665 static_cast<void *>(watchpoint_sp.get()));
1666
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001667 return LLDB_RECORD_RESULT(sb_watchpoint);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668}
1669
1670bool SBTarget::EnableAllWatchpoints() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001671 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, EnableAllWatchpoints);
1672
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 TargetSP target_sp(GetSP());
1674 if (target_sp) {
1675 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1676 std::unique_lock<std::recursive_mutex> lock;
1677 target_sp->GetWatchpointList().GetListMutex(lock);
1678 target_sp->EnableAllWatchpoints();
1679 return true;
1680 }
1681 return false;
1682}
1683
1684bool SBTarget::DisableAllWatchpoints() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001685 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DisableAllWatchpoints);
1686
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 TargetSP target_sp(GetSP());
1688 if (target_sp) {
1689 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1690 std::unique_lock<std::recursive_mutex> lock;
1691 target_sp->GetWatchpointList().GetListMutex(lock);
1692 target_sp->DisableAllWatchpoints();
1693 return true;
1694 }
1695 return false;
1696}
1697
1698SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1699 SBType type) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001700 LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, CreateValueFromAddress,
1701 (const char *, lldb::SBAddress, lldb::SBType), name, addr,
1702 type);
1703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704 SBValue sb_value;
1705 lldb::ValueObjectSP new_value_sp;
1706 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1707 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1708 ExecutionContext exe_ctx(
1709 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1710 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1711 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1712 exe_ctx, ast_type);
1713 }
1714 sb_value.SetSP(new_value_sp);
1715 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1716 if (log) {
1717 if (new_value_sp)
1718 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1719 static_cast<void *>(m_opaque_sp.get()),
1720 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001721 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001722 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1723 static_cast<void *>(m_opaque_sp.get()));
1724 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001725 return LLDB_RECORD_RESULT(sb_value);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001726}
1727
Kate Stoneb9c1b512016-09-06 20:57:50 +00001728lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1729 lldb::SBType type) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001730 LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, CreateValueFromData,
1731 (const char *, lldb::SBData, lldb::SBType), name, data,
1732 type);
1733
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734 SBValue sb_value;
1735 lldb::ValueObjectSP new_value_sp;
1736 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1737 DataExtractorSP extractor(*data);
1738 ExecutionContext exe_ctx(
1739 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1740 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1741 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1742 exe_ctx, ast_type);
1743 }
1744 sb_value.SetSP(new_value_sp);
1745 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1746 if (log) {
1747 if (new_value_sp)
1748 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1749 static_cast<void *>(m_opaque_sp.get()),
1750 new_value_sp->GetName().AsCString());
1751 else
1752 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1753 static_cast<void *>(m_opaque_sp.get()));
1754 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001755 return LLDB_RECORD_RESULT(sb_value);
Greg Claytone14e1922012-12-04 02:22:16 +00001756}
1757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1759 const char *expr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001760 LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, CreateValueFromExpression,
1761 (const char *, const char *), name, expr);
1762
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 SBValue sb_value;
1764 lldb::ValueObjectSP new_value_sp;
1765 if (IsValid() && name && *name && expr && *expr) {
1766 ExecutionContext exe_ctx(
1767 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1768 new_value_sp =
1769 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1770 }
1771 sb_value.SetSP(new_value_sp);
1772 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1773 if (log) {
1774 if (new_value_sp)
1775 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1776 static_cast<void *>(m_opaque_sp.get()),
1777 new_value_sp->GetName().AsCString());
1778 else
1779 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1780 static_cast<void *>(m_opaque_sp.get()));
1781 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001782 return LLDB_RECORD_RESULT(sb_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783}
1784
1785bool SBTarget::DeleteAllWatchpoints() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001786 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DeleteAllWatchpoints);
1787
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 TargetSP target_sp(GetSP());
1789 if (target_sp) {
1790 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1791 std::unique_lock<std::recursive_mutex> lock;
1792 target_sp->GetWatchpointList().GetListMutex(lock);
1793 target_sp->RemoveAllWatchpoints();
1794 return true;
1795 }
1796 return false;
1797}
1798
Alexander Polyakov3e7b9db2018-08-07 20:23:57 +00001799void SBTarget::AppendImageSearchPath(const char *from, const char *to,
1800 lldb::SBError &error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001801 LLDB_RECORD_METHOD(void, SBTarget, AppendImageSearchPath,
1802 (const char *, const char *, lldb::SBError &), from, to,
1803 error);
1804
Alexander Polyakov3e7b9db2018-08-07 20:23:57 +00001805 TargetSP target_sp(GetSP());
1806 if (!target_sp)
1807 return error.SetErrorString("invalid target");
1808
1809 const ConstString csFrom(from), csTo(to);
1810 if (!csFrom)
1811 return error.SetErrorString("<from> path can't be empty");
1812 if (!csTo)
1813 return error.SetErrorString("<to> path can't be empty");
1814
1815 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1816 if (log)
1817 log->Printf("SBTarget(%p)::%s: '%s' -> '%s'",
1818 static_cast<void *>(target_sp.get()), __FUNCTION__,
1819 from, to);
1820 target_sp->GetImageSearchPathList().Append(csFrom, csTo, true);
1821}
1822
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1824 const char *uuid_cstr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001825 LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, AddModule,
1826 (const char *, const char *, const char *), path, triple,
1827 uuid_cstr);
1828
1829 return LLDB_RECORD_RESULT(AddModule(path, triple, uuid_cstr, NULL));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830}
1831
1832lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1833 const char *uuid_cstr, const char *symfile) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001834 LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, AddModule,
1835 (const char *, const char *, const char *, const char *),
1836 path, triple, uuid_cstr, symfile);
1837
Kate Stoneb9c1b512016-09-06 20:57:50 +00001838 lldb::SBModule sb_module;
1839 TargetSP target_sp(GetSP());
1840 if (target_sp) {
1841 ModuleSpec module_spec;
1842 if (path)
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001843 module_spec.GetFileSpec().SetFile(path, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844
1845 if (uuid_cstr)
Pavel Labatha174bcb2018-06-21 15:24:39 +00001846 module_spec.GetUUID().SetFromStringRef(uuid_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001847
1848 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001849 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1850 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851 else
1852 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1853
1854 if (symfile)
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001855 module_spec.GetSymbolFileSpec().SetFile(symfile, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856
1857 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1858 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001859 return LLDB_RECORD_RESULT(sb_module);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860}
1861
1862lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001863 LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, AddModule,
1864 (const lldb::SBModuleSpec &), module_spec);
1865
Kate Stoneb9c1b512016-09-06 20:57:50 +00001866 lldb::SBModule sb_module;
1867 TargetSP target_sp(GetSP());
1868 if (target_sp)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001869 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_up));
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001870 return LLDB_RECORD_RESULT(sb_module);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001871}
1872
1873bool SBTarget::AddModule(lldb::SBModule &module) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001874 LLDB_RECORD_METHOD(bool, SBTarget, AddModule, (lldb::SBModule &), module);
1875
Kate Stoneb9c1b512016-09-06 20:57:50 +00001876 TargetSP target_sp(GetSP());
1877 if (target_sp) {
1878 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1879 return true;
1880 }
1881 return false;
1882}
1883
1884uint32_t SBTarget::GetNumModules() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001885 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetNumModules);
1886
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1888
1889 uint32_t num = 0;
1890 TargetSP target_sp(GetSP());
1891 if (target_sp) {
1892 // The module list is thread safe, no need to lock
1893 num = target_sp->GetImages().GetSize();
1894 }
1895
1896 if (log)
1897 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1898 static_cast<void *>(target_sp.get()), num);
1899
1900 return num;
1901}
1902
1903void SBTarget::Clear() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001904 LLDB_RECORD_METHOD_NO_ARGS(void, SBTarget, Clear);
1905
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1907
1908 if (log)
1909 log->Printf("SBTarget(%p)::Clear ()",
1910 static_cast<void *>(m_opaque_sp.get()));
1911
1912 m_opaque_sp.reset();
1913}
1914
1915SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001916 LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, FindModule,
1917 (const lldb::SBFileSpec &), sb_file_spec);
1918
Kate Stoneb9c1b512016-09-06 20:57:50 +00001919 SBModule sb_module;
1920 TargetSP target_sp(GetSP());
1921 if (target_sp && sb_file_spec.IsValid()) {
1922 ModuleSpec module_spec(*sb_file_spec);
1923 // The module list is thread safe, no need to lock
1924 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1925 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001926 return LLDB_RECORD_RESULT(sb_module);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927}
1928
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001929SBSymbolContextList SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
1930 LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindCompileUnits,
1931 (const lldb::SBFileSpec &), sb_file_spec);
1932
Alexander Polyakovda0c0812018-07-03 14:22:44 +00001933 SBSymbolContextList sb_sc_list;
1934 const TargetSP target_sp(GetSP());
1935 if (target_sp && sb_file_spec.IsValid()) {
1936 const bool append = true;
1937 target_sp->GetImages().FindCompileUnits(*sb_file_spec,
1938 append, *sb_sc_list);
1939 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001940 return LLDB_RECORD_RESULT(sb_sc_list);
Alexander Polyakovda0c0812018-07-03 14:22:44 +00001941}
1942
Kate Stoneb9c1b512016-09-06 20:57:50 +00001943lldb::ByteOrder SBTarget::GetByteOrder() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001944 LLDB_RECORD_METHOD_NO_ARGS(lldb::ByteOrder, SBTarget, GetByteOrder);
1945
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946 TargetSP target_sp(GetSP());
1947 if (target_sp)
1948 return target_sp->GetArchitecture().GetByteOrder();
1949 return eByteOrderInvalid;
1950}
1951
1952const char *SBTarget::GetTriple() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001953 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTarget, GetTriple);
1954
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955 TargetSP target_sp(GetSP());
1956 if (target_sp) {
1957 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001958 // Unique the string so we don't run into ownership issues since the const
1959 // strings put the string into the string pool once and the strings never
1960 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001961 ConstString const_triple(triple.c_str());
1962 return const_triple.GetCString();
1963 }
1964 return NULL;
1965}
1966
1967uint32_t SBTarget::GetDataByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001968 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTarget, GetDataByteSize);
1969
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 TargetSP target_sp(GetSP());
1971 if (target_sp) {
1972 return target_sp->GetArchitecture().GetDataByteSize();
1973 }
1974 return 0;
1975}
1976
1977uint32_t SBTarget::GetCodeByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001978 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTarget, GetCodeByteSize);
1979
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 TargetSP target_sp(GetSP());
1981 if (target_sp) {
1982 return target_sp->GetArchitecture().GetCodeByteSize();
1983 }
1984 return 0;
1985}
1986
1987uint32_t SBTarget::GetAddressByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001988 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTarget, GetAddressByteSize);
1989
Kate Stoneb9c1b512016-09-06 20:57:50 +00001990 TargetSP target_sp(GetSP());
1991 if (target_sp)
1992 return target_sp->GetArchitecture().GetAddressByteSize();
1993 return sizeof(void *);
1994}
1995
1996SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001997 LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, GetModuleAtIndex, (uint32_t),
1998 idx);
1999
Kate Stoneb9c1b512016-09-06 20:57:50 +00002000 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
2001
2002 SBModule sb_module;
2003 ModuleSP module_sp;
2004 TargetSP target_sp(GetSP());
2005 if (target_sp) {
2006 // The module list is thread safe, no need to lock
2007 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2008 sb_module.SetSP(module_sp);
2009 }
2010
2011 if (log)
2012 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2013 static_cast<void *>(target_sp.get()), idx,
2014 static_cast<void *>(module_sp.get()));
2015
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002016 return LLDB_RECORD_RESULT(sb_module);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002017}
2018
2019bool SBTarget::RemoveModule(lldb::SBModule module) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002020 LLDB_RECORD_METHOD(bool, SBTarget, RemoveModule, (lldb::SBModule), module);
2021
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022 TargetSP target_sp(GetSP());
2023 if (target_sp)
2024 return target_sp->GetImages().Remove(module.GetSP());
2025 return false;
2026}
2027
2028SBBroadcaster SBTarget::GetBroadcaster() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002029 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBBroadcaster, SBTarget,
2030 GetBroadcaster);
2031
Kate Stoneb9c1b512016-09-06 20:57:50 +00002032 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
2033
2034 TargetSP target_sp(GetSP());
2035 SBBroadcaster broadcaster(target_sp.get(), false);
2036
2037 if (log)
2038 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2039 static_cast<void *>(target_sp.get()),
2040 static_cast<void *>(broadcaster.get()));
2041
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002042 return LLDB_RECORD_RESULT(broadcaster);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002043}
2044
2045bool SBTarget::GetDescription(SBStream &description,
2046 lldb::DescriptionLevel description_level) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002047 LLDB_RECORD_METHOD(bool, SBTarget, GetDescription,
2048 (lldb::SBStream &, lldb::DescriptionLevel), description,
2049 description_level);
2050
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051 Stream &strm = description.ref();
2052
2053 TargetSP target_sp(GetSP());
2054 if (target_sp) {
2055 target_sp->Dump(&strm, description_level);
2056 } else
2057 strm.PutCString("No value");
2058
2059 return true;
2060}
2061
2062lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
2063 uint32_t name_type_mask) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002064 LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindFunctions,
2065 (const char *, uint32_t), name, name_type_mask);
2066
Kate Stoneb9c1b512016-09-06 20:57:50 +00002067 lldb::SBSymbolContextList sb_sc_list;
Zachary Turner117b1fa2018-10-25 20:45:40 +00002068 if (!name | !name[0])
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002069 return LLDB_RECORD_RESULT(sb_sc_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +00002070
2071 TargetSP target_sp(GetSP());
2072 if (!target_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002073 return LLDB_RECORD_RESULT(sb_sc_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +00002074
2075 const bool symbols_ok = true;
2076 const bool inlines_ok = true;
2077 const bool append = true;
2078 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
2079 target_sp->GetImages().FindFunctions(ConstString(name), mask, symbols_ok,
2080 inlines_ok, append, *sb_sc_list);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002081 return LLDB_RECORD_RESULT(sb_sc_list);
Jim Ingham763b2b22015-07-07 22:12:17 +00002082}
Greg Claytone14e1922012-12-04 02:22:16 +00002083
Kate Stoneb9c1b512016-09-06 20:57:50 +00002084lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
2085 uint32_t max_matches,
2086 MatchType matchtype) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002087 LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindGlobalFunctions,
2088 (const char *, uint32_t, lldb::MatchType), name,
2089 max_matches, matchtype);
2090
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 lldb::SBSymbolContextList sb_sc_list;
2092 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00002093 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094 TargetSP target_sp(GetSP());
2095 if (target_sp) {
2096 std::string regexstr;
2097 switch (matchtype) {
2098 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00002099 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002100 true, true, *sb_sc_list);
2101 break;
2102 case eMatchTypeStartsWith:
2103 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00002104 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
2105 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002106 break;
2107 default:
2108 target_sp->GetImages().FindFunctions(ConstString(name),
2109 eFunctionNameTypeAny, true, true,
2110 true, *sb_sc_list);
2111 break;
2112 }
2113 }
2114 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002115 return LLDB_RECORD_RESULT(sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116}
2117
2118lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002119 LLDB_RECORD_METHOD(lldb::SBType, SBTarget, FindFirstType, (const char *),
2120 typename_cstr);
2121
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122 TargetSP target_sp(GetSP());
2123 if (typename_cstr && typename_cstr[0] && target_sp) {
2124 ConstString const_typename(typename_cstr);
2125 SymbolContext sc;
2126 const bool exact_match = false;
2127
2128 const ModuleList &module_list = target_sp->GetImages();
2129 size_t count = module_list.GetSize();
2130 for (size_t idx = 0; idx < count; idx++) {
2131 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
2132 if (module_sp) {
2133 TypeSP type_sp(
2134 module_sp->FindFirstType(sc, const_typename, exact_match));
2135 if (type_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002136 return LLDB_RECORD_RESULT(SBType(type_sp));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137 }
2138 }
2139
Adrian Prantl05097242018-04-30 16:49:04 +00002140 // Didn't find the type in the symbols; try the Objective-C runtime if one
2141 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002142
2143 ProcessSP process_sp(target_sp->GetProcessSP());
2144
2145 if (process_sp) {
2146 ObjCLanguageRuntime *objc_language_runtime =
2147 process_sp->GetObjCLanguageRuntime();
2148
2149 if (objc_language_runtime) {
2150 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
2151
2152 if (objc_decl_vendor) {
2153 std::vector<clang::NamedDecl *> decls;
2154
2155 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
2156 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002157 return LLDB_RECORD_RESULT(SBType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002158 }
2159 }
2160 }
2161 }
2162 }
2163
2164 // No matches, search for basic typename matches
2165 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2166 if (clang_ast)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002167 return LLDB_RECORD_RESULT(SBType(ClangASTContext::GetBasicType(
2168 clang_ast->getASTContext(), const_typename)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002170 return LLDB_RECORD_RESULT(SBType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171}
2172
2173SBType SBTarget::GetBasicType(lldb::BasicType type) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002174 LLDB_RECORD_METHOD(lldb::SBType, SBTarget, GetBasicType, (lldb::BasicType),
2175 type);
2176
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177 TargetSP target_sp(GetSP());
2178 if (target_sp) {
2179 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2180 if (clang_ast)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002181 return LLDB_RECORD_RESULT(SBType(
2182 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002184 return LLDB_RECORD_RESULT(SBType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002185}
2186
2187lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002188 LLDB_RECORD_METHOD(lldb::SBTypeList, SBTarget, FindTypes, (const char *),
2189 typename_cstr);
2190
Kate Stoneb9c1b512016-09-06 20:57:50 +00002191 SBTypeList sb_type_list;
2192 TargetSP target_sp(GetSP());
2193 if (typename_cstr && typename_cstr[0] && target_sp) {
2194 ModuleList &images = target_sp->GetImages();
2195 ConstString const_typename(typename_cstr);
2196 bool exact_match = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002197 TypeList type_list;
2198 llvm::DenseSet<SymbolFile *> searched_symbol_files;
2199 uint32_t num_matches =
Zachary Turner576495e2019-01-14 22:41:21 +00002200 images.FindTypes(nullptr, const_typename, exact_match, UINT32_MAX,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 searched_symbol_files, type_list);
2202
2203 if (num_matches > 0) {
2204 for (size_t idx = 0; idx < num_matches; idx++) {
2205 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
2206 if (type_sp)
2207 sb_type_list.Append(SBType(type_sp));
2208 }
2209 }
2210
2211 // Try the Objective-C runtime if one is installed
2212
2213 ProcessSP process_sp(target_sp->GetProcessSP());
2214
2215 if (process_sp) {
2216 ObjCLanguageRuntime *objc_language_runtime =
2217 process_sp->GetObjCLanguageRuntime();
2218
2219 if (objc_language_runtime) {
2220 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
2221
2222 if (objc_decl_vendor) {
2223 std::vector<clang::NamedDecl *> decls;
2224
2225 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
2226 for (clang::NamedDecl *decl : decls) {
2227 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
2228 sb_type_list.Append(SBType(type));
2229 }
2230 }
2231 }
2232 }
2233 }
2234 }
2235
2236 if (sb_type_list.GetSize() == 0) {
2237 // No matches, search for basic typename matches
2238 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2239 if (clang_ast)
2240 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
2241 clang_ast->getASTContext(), const_typename)));
2242 }
2243 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002244 return LLDB_RECORD_RESULT(sb_type_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245}
2246
2247SBValueList SBTarget::FindGlobalVariables(const char *name,
2248 uint32_t max_matches) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002249 LLDB_RECORD_METHOD(lldb::SBValueList, SBTarget, FindGlobalVariables,
2250 (const char *, uint32_t), name, max_matches);
2251
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 SBValueList sb_value_list;
2253
2254 TargetSP target_sp(GetSP());
2255 if (name && target_sp) {
2256 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00002258 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259
2260 if (match_count > 0) {
2261 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2262 if (exe_scope == NULL)
2263 exe_scope = target_sp.get();
2264 for (uint32_t i = 0; i < match_count; ++i) {
2265 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
2266 exe_scope, variable_list.GetVariableAtIndex(i)));
2267 if (valobj_sp)
2268 sb_value_list.Append(SBValue(valobj_sp));
2269 }
2270 }
2271 }
2272
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002273 return LLDB_RECORD_RESULT(sb_value_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274}
2275
2276SBValueList SBTarget::FindGlobalVariables(const char *name,
2277 uint32_t max_matches,
2278 MatchType matchtype) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002279 LLDB_RECORD_METHOD(lldb::SBValueList, SBTarget, FindGlobalVariables,
2280 (const char *, uint32_t, lldb::MatchType), name,
2281 max_matches, matchtype);
2282
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283 SBValueList sb_value_list;
2284
2285 TargetSP target_sp(GetSP());
2286 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00002287 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002289
2290 std::string regexstr;
2291 uint32_t match_count;
2292 switch (matchtype) {
2293 case eMatchTypeNormal:
2294 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00002295 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002296 break;
2297 case eMatchTypeRegex:
2298 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00002299 RegularExpression(name_ref), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002300 break;
2301 case eMatchTypeStartsWith:
2302 regexstr = llvm::Regex::escape(name) + ".*";
2303 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00002304 RegularExpression(regexstr), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002305 break;
2306 }
2307
2308 if (match_count > 0) {
2309 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2310 if (exe_scope == NULL)
2311 exe_scope = target_sp.get();
2312 for (uint32_t i = 0; i < match_count; ++i) {
2313 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
2314 exe_scope, variable_list.GetVariableAtIndex(i)));
2315 if (valobj_sp)
2316 sb_value_list.Append(SBValue(valobj_sp));
2317 }
2318 }
2319 }
2320
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002321 return LLDB_RECORD_RESULT(sb_value_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322}
2323
2324lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002325 LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, FindFirstGlobalVariable,
2326 (const char *), name);
2327
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2329 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002330 return LLDB_RECORD_RESULT(sb_value_list.GetValueAtIndex(0));
2331 return LLDB_RECORD_RESULT(SBValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002332}
2333
2334SBSourceManager SBTarget::GetSourceManager() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002335 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSourceManager, SBTarget, GetSourceManager);
2336
Kate Stoneb9c1b512016-09-06 20:57:50 +00002337 SBSourceManager source_manager(*this);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002338 return LLDB_RECORD_RESULT(source_manager);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002339}
2340
2341lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
2342 uint32_t count) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002343 LLDB_RECORD_METHOD(lldb::SBInstructionList, SBTarget, ReadInstructions,
2344 (lldb::SBAddress, uint32_t), base_addr, count);
2345
2346 return LLDB_RECORD_RESULT(ReadInstructions(base_addr, count, NULL));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347}
2348
2349lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
2350 uint32_t count,
2351 const char *flavor_string) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002352 LLDB_RECORD_METHOD(lldb::SBInstructionList, SBTarget, ReadInstructions,
2353 (lldb::SBAddress, uint32_t, const char *), base_addr,
2354 count, flavor_string);
2355
Kate Stoneb9c1b512016-09-06 20:57:50 +00002356 SBInstructionList sb_instructions;
2357
2358 TargetSP target_sp(GetSP());
2359 if (target_sp) {
2360 Address *addr_ptr = base_addr.get();
2361
2362 if (addr_ptr) {
2363 DataBufferHeap data(
2364 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2365 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00002366 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002367 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2368 const size_t bytes_read =
2369 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
2370 data.GetByteSize(), error, &load_addr);
2371 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2372 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2373 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
2374 data.GetBytes(), bytes_read, count, data_from_file));
2375 }
2376 }
2377
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002378 return LLDB_RECORD_RESULT(sb_instructions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002379}
2380
2381lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
2382 const void *buf,
2383 size_t size) {
2384 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
2385}
2386
2387lldb::SBInstructionList
2388SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
2389 const char *flavor_string, const void *buf,
2390 size_t size) {
2391 SBInstructionList sb_instructions;
2392
2393 TargetSP target_sp(GetSP());
2394 if (target_sp) {
2395 Address addr;
2396
2397 if (base_addr.get())
2398 addr = *base_addr.get();
2399
2400 const bool data_from_file = true;
2401
2402 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2403 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
2404 UINT32_MAX, data_from_file));
2405 }
2406
2407 return sb_instructions;
2408}
2409
2410lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
2411 const void *buf,
2412 size_t size) {
2413 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
2414 size);
2415}
2416
2417lldb::SBInstructionList
2418SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
2419 const char *flavor_string, const void *buf,
2420 size_t size) {
2421 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
2422 buf, size);
2423}
2424
2425SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
2426 lldb::addr_t section_base_addr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002427 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, SetSectionLoadAddress,
2428 (lldb::SBSection, lldb::addr_t), section,
2429 section_base_addr);
2430
Kate Stoneb9c1b512016-09-06 20:57:50 +00002431 SBError sb_error;
2432 TargetSP target_sp(GetSP());
2433 if (target_sp) {
2434 if (!section.IsValid()) {
2435 sb_error.SetErrorStringWithFormat("invalid section");
2436 } else {
2437 SectionSP section_sp(section.GetSP());
2438 if (section_sp) {
2439 if (section_sp->IsThreadSpecific()) {
2440 sb_error.SetErrorString(
2441 "thread specific sections are not yet supported");
2442 } else {
2443 ProcessSP process_sp(target_sp->GetProcessSP());
2444 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
2445 ModuleSP module_sp(section_sp->GetModule());
2446 if (module_sp) {
2447 ModuleList module_list;
2448 module_list.Append(module_sp);
2449 target_sp->ModulesDidLoad(module_list);
2450 }
2451 // Flush info in the process (stack frames, etc)
2452 if (process_sp)
2453 process_sp->Flush();
2454 }
2455 }
2456 }
2457 }
2458 } else {
2459 sb_error.SetErrorString("invalid target");
2460 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002461 return LLDB_RECORD_RESULT(sb_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002462}
2463
2464SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002465 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, ClearSectionLoadAddress,
2466 (lldb::SBSection), section);
2467
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468 SBError sb_error;
2469
2470 TargetSP target_sp(GetSP());
2471 if (target_sp) {
2472 if (!section.IsValid()) {
2473 sb_error.SetErrorStringWithFormat("invalid section");
2474 } else {
2475 SectionSP section_sp(section.GetSP());
2476 if (section_sp) {
2477 ProcessSP process_sp(target_sp->GetProcessSP());
2478 if (target_sp->SetSectionUnloaded(section_sp)) {
2479 ModuleSP module_sp(section_sp->GetModule());
2480 if (module_sp) {
2481 ModuleList module_list;
2482 module_list.Append(module_sp);
2483 target_sp->ModulesDidUnload(module_list, false);
2484 }
2485 // Flush info in the process (stack frames, etc)
2486 if (process_sp)
2487 process_sp->Flush();
2488 }
2489 } else {
2490 sb_error.SetErrorStringWithFormat("invalid section");
2491 }
2492 }
2493 } else {
2494 sb_error.SetErrorStringWithFormat("invalid target");
2495 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002496 return LLDB_RECORD_RESULT(sb_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002497}
2498
2499SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2500 int64_t slide_offset) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002501 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, SetModuleLoadAddress,
2502 (lldb::SBModule, int64_t), module, slide_offset);
2503
Kate Stoneb9c1b512016-09-06 20:57:50 +00002504 SBError sb_error;
2505
2506 TargetSP target_sp(GetSP());
2507 if (target_sp) {
2508 ModuleSP module_sp(module.GetSP());
2509 if (module_sp) {
2510 bool changed = false;
2511 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2512 // The load was successful, make sure that at least some sections
2513 // changed before we notify that our module was loaded.
2514 if (changed) {
2515 ModuleList module_list;
2516 module_list.Append(module_sp);
2517 target_sp->ModulesDidLoad(module_list);
2518 // Flush info in the process (stack frames, etc)
2519 ProcessSP process_sp(target_sp->GetProcessSP());
2520 if (process_sp)
2521 process_sp->Flush();
2522 }
2523 }
2524 } else {
2525 sb_error.SetErrorStringWithFormat("invalid module");
2526 }
2527
2528 } else {
2529 sb_error.SetErrorStringWithFormat("invalid target");
2530 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002531 return LLDB_RECORD_RESULT(sb_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002532}
2533
2534SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002535 LLDB_RECORD_METHOD(lldb::SBError, SBTarget, ClearModuleLoadAddress,
2536 (lldb::SBModule), module);
2537
Kate Stoneb9c1b512016-09-06 20:57:50 +00002538 SBError sb_error;
2539
2540 char path[PATH_MAX];
2541 TargetSP target_sp(GetSP());
2542 if (target_sp) {
2543 ModuleSP module_sp(module.GetSP());
2544 if (module_sp) {
2545 ObjectFile *objfile = module_sp->GetObjectFile();
2546 if (objfile) {
2547 SectionList *section_list = objfile->GetSectionList();
2548 if (section_list) {
2549 ProcessSP process_sp(target_sp->GetProcessSP());
2550
2551 bool changed = false;
2552 const size_t num_sections = section_list->GetSize();
2553 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2554 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2555 if (section_sp)
2556 changed |= target_sp->SetSectionUnloaded(section_sp);
2557 }
2558 if (changed) {
2559 ModuleList module_list;
2560 module_list.Append(module_sp);
2561 target_sp->ModulesDidUnload(module_list, false);
2562 // Flush info in the process (stack frames, etc)
2563 ProcessSP process_sp(target_sp->GetProcessSP());
2564 if (process_sp)
2565 process_sp->Flush();
2566 }
2567 } else {
2568 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2569 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2570 path);
2571 }
2572 } else {
2573 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2574 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2575 path);
2576 }
2577 } else {
2578 sb_error.SetErrorStringWithFormat("invalid module");
2579 }
2580 } else {
2581 sb_error.SetErrorStringWithFormat("invalid target");
2582 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002583 return LLDB_RECORD_RESULT(sb_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002584}
2585
2586lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2587 lldb::SymbolType symbol_type) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002588 LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindSymbols,
2589 (const char *, lldb::SymbolType), name, symbol_type);
2590
Kate Stoneb9c1b512016-09-06 20:57:50 +00002591 SBSymbolContextList sb_sc_list;
2592 if (name && name[0]) {
2593 TargetSP target_sp(GetSP());
2594 if (target_sp) {
2595 bool append = true;
2596 target_sp->GetImages().FindSymbolsWithNameAndType(
2597 ConstString(name), symbol_type, *sb_sc_list, append);
2598 }
2599 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002600 return LLDB_RECORD_RESULT(sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002601}
2602
2603lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002604 LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, EvaluateExpression,
2605 (const char *), expr);
2606
Kate Stoneb9c1b512016-09-06 20:57:50 +00002607 TargetSP target_sp(GetSP());
2608 if (!target_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002609 return LLDB_RECORD_RESULT(SBValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002610
2611 SBExpressionOptions options;
2612 lldb::DynamicValueType fetch_dynamic_value =
2613 target_sp->GetPreferDynamicValue();
2614 options.SetFetchDynamicValue(fetch_dynamic_value);
2615 options.SetUnwindOnError(true);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002616 return LLDB_RECORD_RESULT(EvaluateExpression(expr, options));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002617}
2618
2619lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2620 const SBExpressionOptions &options) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002621 LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, EvaluateExpression,
2622 (const char *, const lldb::SBExpressionOptions &), expr,
2623 options);
2624
Kate Stoneb9c1b512016-09-06 20:57:50 +00002625 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002626#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002627 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002628#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002629 SBValue expr_result;
2630 ExpressionResults exe_results = eExpressionSetupError;
2631 ValueObjectSP expr_value_sp;
2632 TargetSP target_sp(GetSP());
2633 StackFrame *frame = NULL;
2634 if (target_sp) {
2635 if (expr == NULL || expr[0] == '\0') {
2636 if (log)
2637 log->Printf(
2638 "SBTarget::EvaluateExpression called with an empty expression");
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002639 return LLDB_RECORD_RESULT(expr_result);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002641
2642 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2643 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002644
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002645 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002646 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2647
2648 frame = exe_ctx.GetFramePtr();
2649 Target *target = exe_ctx.GetTargetPtr();
2650
2651 if (target) {
2652#ifdef LLDB_CONFIGURATION_DEBUG
2653 StreamString frame_description;
2654 if (frame)
2655 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002656 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002657 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2658 "%u) %s",
2659 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002660 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002661#endif
2662 exe_results =
2663 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2664
2665 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002666 } else {
2667 if (log)
2668 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2669 "reconstruct frame object for this SBTarget.");
2670 }
2671 }
2672#ifndef LLDB_DISABLE_PYTHON
2673 if (expr_log)
2674 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2675 "%s, summary %s **",
2676 expr_result.GetValue(), expr_result.GetSummary());
2677
2678 if (log)
2679 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2680 "(execution result=%d)",
2681 static_cast<void *>(frame), expr,
2682 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002683#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002684
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002685 return LLDB_RECORD_RESULT(expr_result);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002686}
2687
Kate Stoneb9c1b512016-09-06 20:57:50 +00002688lldb::addr_t SBTarget::GetStackRedZoneSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002689 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBTarget, GetStackRedZoneSize);
2690
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691 TargetSP target_sp(GetSP());
2692 if (target_sp) {
2693 ABISP abi_sp;
2694 ProcessSP process_sp(target_sp->GetProcessSP());
2695 if (process_sp)
2696 abi_sp = process_sp->GetABI();
2697 else
Jason Molenda43294c92017-06-29 02:57:03 +00002698 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002699 if (abi_sp)
2700 return abi_sp->GetRedZoneSize();
2701 }
2702 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002703}
Ilia K8f37ca52015-02-13 14:31:06 +00002704
Kate Stoneb9c1b512016-09-06 20:57:50 +00002705lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002706 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBLaunchInfo, SBTarget, GetLaunchInfo);
2707
Kate Stoneb9c1b512016-09-06 20:57:50 +00002708 lldb::SBLaunchInfo launch_info(NULL);
2709 TargetSP target_sp(GetSP());
2710 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002711 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002712 return LLDB_RECORD_RESULT(launch_info);
Ilia K8f37ca52015-02-13 14:31:06 +00002713}
2714
Kate Stoneb9c1b512016-09-06 20:57:50 +00002715void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00002716 LLDB_RECORD_METHOD(void, SBTarget, SetLaunchInfo,
2717 (const lldb::SBLaunchInfo &), launch_info);
2718
Kate Stoneb9c1b512016-09-06 20:57:50 +00002719 TargetSP target_sp(GetSP());
2720 if (target_sp)
2721 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002722}