blob: 2d2c6bd3e924d97db602f8273f0b6a0b3839aaa9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman4c5de692010-06-09 07:44:37 +000010#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Greg Claytone0d378b2011-03-24 21:19:54 +000012#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytondea8cb42011-06-29 22:09:02 +000014#include "lldb/API/SBBreakpoint.h"
Ilia Keb2c19a2015-03-10 21:59:55 +000015#include "lldb/API/SBDebugger.h"
16#include "lldb/API/SBEvent.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000017#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000019#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000021#include "lldb/API/SBModuleSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000022#include "lldb/API/SBProcess.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Jim Ingham76bb8d62016-04-28 01:40:57 +000025#include "lldb/API/SBStringList.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000026#include "lldb/API/SBStructuredData.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000027#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Breakpoint/BreakpointID.h"
29#include "lldb/Breakpoint/BreakpointIDList.h"
30#include "lldb/Breakpoint/BreakpointList.h"
31#include "lldb/Breakpoint/BreakpointLocation.h"
32#include "lldb/Core/Address.h"
33#include "lldb/Core/AddressResolver.h"
34#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/Module.h"
38#include "lldb/Core/ModuleSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000039#include "lldb/Core/STLUtils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Core/Section.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000042#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000043#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000044#include "lldb/Core/ValueObjectList.h"
45#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000046#include "lldb/Host/Host.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Symbol/ClangASTContext.h"
48#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000049#include "lldb/Symbol/ObjectFile.h"
Greg Claytonae088e52016-02-10 21:28:13 +000050#include "lldb/Symbol/SymbolFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000053#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000054#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000055#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000056#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000058#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059#include "lldb/Target/Target.h"
60#include "lldb/Target/TargetList.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000061#include "lldb/Utility/ArchSpec.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000062#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000063#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000064#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000065#include "lldb/Utility/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
James Y Knight2ad48212018-05-22 22:53:50 +000067#include "Commands/CommandObjectBreakpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000068#include "lldb/Interpreter/CommandReturnObject.h"
Sean Callanan237c3ed2016-12-14 21:31:31 +000069#include "llvm/Support/PrettyStackTrace.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000070#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072using namespace lldb;
73using namespace lldb_private;
74
75#define DEFAULT_DISASM_BYTE_SIZE 32
76
Oleksiy Vyalov37386142015-02-10 22:49:57 +000077namespace {
78
Zachary Turner97206d52017-05-12 04:51:55 +000079Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
Oleksiy Vyalov37386142015-02-10 22:49:57 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 auto process_sp = target.GetProcessSP();
83 if (process_sp) {
84 const auto state = process_sp->GetState();
85 if (process_sp->IsAlive() && state == eStateConnected) {
86 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +000087 // listener, so if a valid listener is supplied, we need to error out to
88 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (attach_info.GetListener())
Zachary Turner97206d52017-05-12 04:51:55 +000090 return Status("process is connected and already has a listener, pass "
91 "empty listener");
Oleksiy Vyalov37386142015-02-10 22:49:57 +000092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 return target.Attach(attach_info, nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098} // namespace
Oleksiy Vyalov37386142015-02-10 22:49:57 +000099
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100//----------------------------------------------------------------------
101// SBTarget constructor
102//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103SBTarget::SBTarget() : m_opaque_sp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
110 if (this != &rhs)
111 m_opaque_sp = rhs.m_opaque_sp;
112 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000113}
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115//----------------------------------------------------------------------
116// Destructor
117//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118SBTarget::~SBTarget() {}
119
120bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
121 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
125 return Target::TargetEventData::GetTargetFromEvent(event.get());
Ilia Keb2c19a2015-03-10 21:59:55 +0000126}
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
129 const ModuleList module_list =
130 Target::TargetEventData::GetModuleListFromEvent(event.get());
131 return module_list.GetSize();
Ilia Keb2c19a2015-03-10 21:59:55 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
135 const SBEvent &event) {
136 const ModuleList module_list =
137 Target::TargetEventData::GetModuleListFromEvent(event.get());
138 return SBModule(module_list.GetModuleAtIndex(idx));
Ilia Keb2c19a2015-03-10 21:59:55 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141const char *SBTarget::GetBroadcasterClassName() {
142 return Target::GetStaticBroadcasterClass().AsCString();
Ilia Keb2c19a2015-03-10 21:59:55 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145bool SBTarget::IsValid() const {
146 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149SBProcess SBTarget::GetProcess() {
150 SBProcess sb_process;
151 ProcessSP process_sp;
152 TargetSP target_sp(GetSP());
153 if (target_sp) {
154 process_sp = target_sp->GetProcessSP();
155 sb_process.SetSP(process_sp);
156 }
157
158 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
159 if (log)
160 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
161 static_cast<void *>(target_sp.get()),
162 static_cast<void *>(process_sp.get()));
163
164 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167SBPlatform SBTarget::GetPlatform() {
168 TargetSP target_sp(GetSP());
169 if (!target_sp)
170 return SBPlatform();
171
172 SBPlatform platform;
173 platform.m_opaque_sp = target_sp->GetPlatform();
174
175 return platform;
176}
177
178SBDebugger SBTarget::GetDebugger() const {
179 SBDebugger debugger;
180 TargetSP target_sp(GetSP());
181 if (target_sp)
182 debugger.reset(target_sp->GetDebugger().shared_from_this());
183 return debugger;
184}
185
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000186SBStructuredData SBTarget::GetStatistics() {
187 SBStructuredData data;
188 TargetSP target_sp(GetSP());
189 if (!target_sp)
190 return data;
191
192 auto stats_up = llvm::make_unique<StructuredData::Dictionary>();
193 int i = 0;
194 for (auto &Entry : target_sp->GetStatistics()) {
195 std::string Desc = lldb_private::GetStatDescription(
196 static_cast<lldb_private::StatisticKind>(i));
197 stats_up->AddIntegerItem(Desc, Entry);
198 i += 1;
199 }
200
201 data.m_impl_up->SetObjectSP(std::move(stats_up));
202 return data;
203}
204
Davide Italianoe1747462018-09-28 23:27:54 +0000205void SBTarget::SetCollectingStats(bool v) {
206 TargetSP target_sp(GetSP());
207 if (!target_sp)
208 return;
209 return target_sp->SetCollectingStats(v);
210}
211
212bool SBTarget::GetCollectingStats() {
213 TargetSP target_sp(GetSP());
214 if (!target_sp)
215 return false;
216 return target_sp->GetCollectingStats();
217}
218
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220SBProcess SBTarget::LoadCore(const char *core_file) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000221 lldb::SBError error; // Ignored
222 return LoadCore(core_file, error);
223}
224
225SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 SBProcess sb_process;
227 TargetSP target_sp(GetSP());
228 if (target_sp) {
229 FileSpec filespec(core_file, true);
230 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000231 target_sp->GetDebugger().GetListener(), "", &filespec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 if (process_sp) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000233 error.SetError(process_sp->LoadCore());
234 if (error.Success())
235 sb_process.SetSP(process_sp);
236 } else {
237 error.SetErrorString("Failed to create the process");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 }
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000239 } else {
240 error.SetErrorString("SBTarget is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 }
242 return sb_process;
243}
244
245SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
246 const char *working_directory) {
247 char *stdin_path = NULL;
248 char *stdout_path = NULL;
249 char *stderr_path = NULL;
250 uint32_t launch_flags = 0;
251 bool stop_at_entry = false;
252 SBError error;
253 SBListener listener = GetDebugger().GetListener();
254 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path,
255 working_directory, launch_flags, stop_at_entry, error);
256}
257
258SBError SBTarget::Install() {
259 SBError sb_error;
260 TargetSP target_sp(GetSP());
261 if (target_sp) {
262 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
263 sb_error.ref() = target_sp->Install(NULL);
264 }
265 return sb_error;
266}
267
268SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
269 char const **envp, const char *stdin_path,
270 const char *stdout_path, const char *stderr_path,
271 const char *working_directory,
272 uint32_t launch_flags, // See LaunchFlags
273 bool stop_at_entry, lldb::SBError &error) {
274 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
275
276 SBProcess sb_process;
277 ProcessSP process_sp;
278 TargetSP target_sp(GetSP());
279
280 if (log)
281 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
282 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
283 "stop_at_entry=%i, &error (%p))...",
284 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
285 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
286 stdout_path ? stdout_path : "NULL",
287 stderr_path ? stderr_path : "NULL",
288 working_directory ? working_directory : "NULL", launch_flags,
289 stop_at_entry, static_cast<void *>(error.get()));
290
291 if (target_sp) {
292 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
293
294 if (stop_at_entry)
295 launch_flags |= eLaunchFlagStopAtEntry;
296
297 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
298 launch_flags |= eLaunchFlagDisableASLR;
299
300 StateType state = eStateInvalid;
301 process_sp = target_sp->GetProcessSP();
302 if (process_sp) {
303 state = process_sp->GetState();
304
305 if (process_sp->IsAlive() && state != eStateConnected) {
306 if (state == eStateAttaching)
307 error.SetErrorString("process attach is in progress");
308 else
309 error.SetErrorString("a process is already being debugged");
310 return sb_process;
311 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000312 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 if (state == eStateConnected) {
315 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +0000316 // listener, so if a valid listener is supplied, we need to error out to
317 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 if (listener.IsValid()) {
319 error.SetErrorString("process is connected and already has a listener, "
320 "pass empty listener");
321 return sb_process;
322 }
323 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
326 launch_flags |= eLaunchFlagDisableSTDIO;
327
328 ProcessLaunchInfo launch_info(
329 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
330 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
331 launch_flags);
332
333 Module *exe_module = target_sp->GetExecutableModulePointer();
334 if (exe_module)
335 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
336 if (argv)
337 launch_info.GetArguments().AppendArguments(argv);
338 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000339 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340
341 if (listener.IsValid())
342 launch_info.SetListener(listener.GetSP());
343
344 error.SetError(target_sp->Launch(launch_info, NULL));
345
346 sb_process.SetSP(target_sp->GetProcessSP());
347 } else {
348 error.SetErrorString("SBTarget is invalid");
349 }
350
351 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
352 if (log)
353 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
354 static_cast<void *>(target_sp.get()),
355 static_cast<void *>(sb_process.GetSP().get()),
356 error.GetCString());
357
358 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
362 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 SBProcess sb_process;
365 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 if (log)
368 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
369 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 if (target_sp) {
372 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
373 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000374 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 ProcessSP process_sp = target_sp->GetProcessSP();
376 if (process_sp) {
377 state = process_sp->GetState();
378
379 if (process_sp->IsAlive() && state != eStateConnected) {
380 if (state == eStateAttaching)
381 error.SetErrorString("process attach is in progress");
382 else
383 error.SetErrorString("a process is already being debugged");
384 return sb_process;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000385 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000387 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000388
Pavel Labath62930e52018-01-10 11:57:31 +0000389 lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 if (!launch_info.GetExecutableFile()) {
392 Module *exe_module = target_sp->GetExecutableModulePointer();
393 if (exe_module)
394 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396
397 const ArchSpec &arch_spec = target_sp->GetArchitecture();
398 if (arch_spec.IsValid())
399 launch_info.GetArchitecture() = arch_spec;
400
401 error.SetError(target_sp->Launch(launch_info, NULL));
Pavel Labath62930e52018-01-10 11:57:31 +0000402 sb_launch_info.set_ref(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 sb_process.SetSP(target_sp->GetProcessSP());
404 } else {
405 error.SetErrorString("SBTarget is invalid");
406 }
407
408 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
409 if (log)
410 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
411 static_cast<void *>(target_sp.get()),
412 static_cast<void *>(sb_process.GetSP().get()));
413
414 return sb_process;
Greg Claytonfbb76342013-11-20 21:07:01 +0000415}
416
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
418 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 SBProcess sb_process;
421 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 if (log)
424 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
425 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 if (target_sp) {
428 ProcessAttachInfo &attach_info = sb_attach_info.ref();
429 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
430 PlatformSP platform_sp = target_sp->GetPlatform();
431 // See if we can pre-verify if a process exists or not
432 if (platform_sp && platform_sp->IsConnected()) {
433 lldb::pid_t attach_pid = attach_info.GetProcessID();
434 ProcessInstanceInfo instance_info;
435 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
436 attach_info.SetUserID(instance_info.GetEffectiveUserID());
437 } else {
438 error.ref().SetErrorStringWithFormat(
439 "no process found with process ID %" PRIu64, attach_pid);
440 if (log) {
441 log->Printf("SBTarget(%p)::Attach (...) => error %s",
442 static_cast<void *>(target_sp.get()),
443 error.GetCString());
444 }
445 return sb_process;
Greg Clayton931180e2011-01-27 06:44:37 +0000446 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000448 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 error.SetError(AttachToProcess(attach_info, *target_sp));
450 if (error.Success())
451 sb_process.SetSP(target_sp->GetProcessSP());
452 } else {
453 error.SetErrorString("SBTarget is invalid");
454 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 if (log)
457 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
458 static_cast<void *>(target_sp.get()),
459 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462}
463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000465 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 lldb::pid_t pid, // The process ID to attach to
467 SBError &error // An error explaining what went wrong if attach fails
468 ) {
469 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000470
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 SBProcess sb_process;
472 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 if (log)
475 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
476 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 if (target_sp) {
479 ProcessAttachInfo attach_info;
480 attach_info.SetProcessID(pid);
481 if (listener.IsValid())
482 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000483
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 ProcessInstanceInfo instance_info;
485 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
486 attach_info.SetUserID(instance_info.GetEffectiveUserID());
487
488 error.SetError(AttachToProcess(attach_info, *target_sp));
489 if (error.Success())
490 sb_process.SetSP(target_sp->GetProcessSP());
491 } else
492 error.SetErrorString("SBTarget is invalid");
493
494 if (log)
495 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
496 static_cast<void *>(target_sp.get()), __FUNCTION__,
497 static_cast<void *>(sb_process.GetSP().get()));
498 return sb_process;
499}
500
501lldb::SBProcess SBTarget::AttachToProcessWithName(
502 SBListener &listener,
503 const char *name, // basename of process to attach to
504 bool wait_for, // if true wait for a new instance of "name" to be launched
505 SBError &error // An error explaining what went wrong if attach fails
506 ) {
507 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
508
509 SBProcess sb_process;
510 TargetSP target_sp(GetSP());
511
512 if (log)
513 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
514 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
515 wait_for ? "true" : "false");
516
517 if (name && target_sp) {
518 ProcessAttachInfo attach_info;
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000519 attach_info.GetExecutableFile().SetFile(name, false,
520 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 attach_info.SetWaitForLaunch(wait_for);
522 if (listener.IsValid())
523 attach_info.SetListener(listener.GetSP());
524
525 error.SetError(AttachToProcess(attach_info, *target_sp));
526 if (error.Success())
527 sb_process.SetSP(target_sp->GetProcessSP());
528 } else
529 error.SetErrorString("SBTarget is invalid");
530
531 if (log)
532 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
533 static_cast<void *>(target_sp.get()), __FUNCTION__,
534 static_cast<void *>(sb_process.GetSP().get()));
535 return sb_process;
536}
537
538lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
539 const char *plugin_name,
540 SBError &error) {
541 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
542
543 SBProcess sb_process;
544 ProcessSP process_sp;
545 TargetSP target_sp(GetSP());
546
547 if (log)
548 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
549 "plugin_name=%s, error)...",
550 static_cast<void *>(target_sp.get()), url, plugin_name);
551
552 if (target_sp) {
553 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
554 if (listener.IsValid())
555 process_sp =
556 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000557 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 process_sp = target_sp->CreateProcess(
559 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
560
561 if (process_sp) {
562 sb_process.SetSP(process_sp);
563 error.SetError(process_sp->ConnectRemote(NULL, url));
564 } else {
565 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 } else {
568 error.SetErrorString("SBTarget is invalid");
569 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (log)
572 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
573 static_cast<void *>(target_sp.get()),
574 static_cast<void *>(process_sp.get()));
575 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000576}
577
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 SBFileSpec exe_file_spec;
581 TargetSP target_sp(GetSP());
582 if (target_sp) {
583 Module *exe_module = target_sp->GetExecutableModulePointer();
584 if (exe_module)
585 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
586 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
589 if (log) {
590 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
591 static_cast<void *>(target_sp.get()),
592 static_cast<const void *>(exe_file_spec.get()));
593 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596}
597
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598bool SBTarget::operator==(const SBTarget &rhs) const {
599 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602bool SBTarget::operator!=(const SBTarget &rhs) const {
603 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604}
605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
607
608void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
609 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
613 lldb::SBAddress sb_addr;
614 Address &addr = sb_addr.ref();
615 TargetSP target_sp(GetSP());
616 if (target_sp) {
617 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
618 if (target_sp->ResolveLoadAddress(vm_addr, addr))
619 return sb_addr;
620 }
621
622 // We have a load address that isn't in a section, just return an address
623 // with the offset filled in (the address) and the section set to NULL
624 addr.SetRawAddress(vm_addr);
625 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626}
627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
629 lldb::SBAddress sb_addr;
630 Address &addr = sb_addr.ref();
631 TargetSP target_sp(GetSP());
632 if (target_sp) {
633 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
634 if (target_sp->ResolveFileAddress(file_addr, addr))
635 return sb_addr;
636 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 addr.SetRawAddress(file_addr);
639 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000640}
641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
643 lldb::addr_t vm_addr) {
644 lldb::SBAddress sb_addr;
645 Address &addr = sb_addr.ref();
646 TargetSP target_sp(GetSP());
647 if (target_sp) {
648 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
649 if (target_sp->ResolveLoadAddress(vm_addr, addr))
650 return sb_addr;
651 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000652
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 // We have a load address that isn't in a section, just return an address
654 // with the offset filled in (the address) and the section set to NULL
655 addr.SetRawAddress(vm_addr);
656 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000657}
658
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000659SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
661 uint32_t resolve_scope) {
662 SBSymbolContext sc;
Zachary Turner991e4452018-10-25 20:45:19 +0000663 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000665 TargetSP target_sp(GetSP());
666 if (target_sp)
Zachary Turner991e4452018-10-25 20:45:19 +0000667 target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
668 sc.ref());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
670 return sc;
671}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
674 lldb::SBError &error) {
675 SBError sb_error;
676 size_t bytes_read = 0;
677 TargetSP target_sp(GetSP());
678 if (target_sp) {
679 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
680 bytes_read =
681 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
682 } else {
683 sb_error.SetErrorString("invalid target");
684 }
685
686 return bytes_read;
687}
688
689SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
690 uint32_t line) {
691 return SBBreakpoint(
692 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000693}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000694
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
697 uint32_t line) {
698 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699}
700
701SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
703 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000704 SBFileSpecList empty_list;
705 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
706}
707
708SBBreakpoint
709SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
710 uint32_t line, lldb::addr_t offset,
711 SBFileSpecList &sb_module_list) {
Adrian Prantl431b1582018-08-30 15:11:00 +0000712 return BreakpointCreateByLocation(sb_file_spec, line, 0, offset,
713 sb_module_list);
714}
715
716SBBreakpoint SBTarget::BreakpointCreateByLocation(
717 const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
718 lldb::addr_t offset, SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000720
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 SBBreakpoint sb_bp;
722 TargetSP target_sp(GetSP());
723 if (target_sp && line != 0) {
724 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000725
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 const LazyBool check_inlines = eLazyBoolCalculate;
727 const LazyBool skip_prologue = eLazyBoolCalculate;
728 const bool internal = false;
729 const bool hardware = false;
730 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000731 const FileSpecList *module_list = nullptr;
732 if (sb_module_list.GetSize() > 0) {
733 module_list = sb_module_list.get();
734 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000735 sb_bp = target_sp->CreateBreakpoint(
Adrian Prantl431b1582018-08-30 15:11:00 +0000736 module_list, *sb_file_spec, line, column, offset, check_inlines,
737 skip_prologue, internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000739
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 if (log) {
741 SBStream sstr;
742 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000743 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 sb_file_spec->GetPath(path, sizeof(path));
745 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
746 "SBBreakpoint(%p): %s",
747 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000748 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000750
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 return sb_bp;
752}
753
754SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
755 const char *module_name) {
756 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
757
758 SBBreakpoint sb_bp;
759 TargetSP target_sp(GetSP());
760 if (target_sp.get()) {
761 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
762
763 const bool internal = false;
764 const bool hardware = false;
765 const LazyBool skip_prologue = eLazyBoolCalculate;
766 const lldb::addr_t offset = 0;
767 if (module_name && module_name[0]) {
768 FileSpecList module_spec_list;
769 module_spec_list.Append(FileSpec(module_name, false));
Pavel Labath6ac84032017-02-27 11:05:34 +0000770 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
772 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
773 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000774 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
776 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000777 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 }
779
780 if (log)
781 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
782 "module=\"%s\") => SBBreakpoint(%p)",
783 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000784 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785
786 return sb_bp;
787}
788
789lldb::SBBreakpoint
790SBTarget::BreakpointCreateByName(const char *symbol_name,
791 const SBFileSpecList &module_list,
792 const SBFileSpecList &comp_unit_list) {
793 uint32_t name_type_mask = eFunctionNameTypeAuto;
794 return BreakpointCreateByName(symbol_name, name_type_mask,
795 eLanguageTypeUnknown, module_list,
796 comp_unit_list);
797}
798
799lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
800 const char *symbol_name, uint32_t name_type_mask,
801 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
802 return BreakpointCreateByName(symbol_name, name_type_mask,
803 eLanguageTypeUnknown, module_list,
804 comp_unit_list);
805}
806
807lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
808 const char *symbol_name, uint32_t name_type_mask,
809 LanguageType symbol_language, const SBFileSpecList &module_list,
810 const SBFileSpecList &comp_unit_list) {
811 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
812
813 SBBreakpoint sb_bp;
814 TargetSP target_sp(GetSP());
815 if (target_sp && symbol_name && symbol_name[0]) {
816 const bool internal = false;
817 const bool hardware = false;
818 const LazyBool skip_prologue = eLazyBoolCalculate;
819 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +0000820 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask,
822 symbol_language, 0, skip_prologue, internal, hardware);
823 }
824
825 if (log)
826 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
827 "name_type: %d) => SBBreakpoint(%p)",
828 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000829 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830
831 return sb_bp;
832}
833
834lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
835 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
836 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
837 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
838 eLanguageTypeUnknown, module_list,
839 comp_unit_list);
840}
841
842lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
843 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
844 LanguageType symbol_language, const SBFileSpecList &module_list,
845 const SBFileSpecList &comp_unit_list) {
846 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
847 eLanguageTypeUnknown, 0, module_list,
848 comp_unit_list);
849}
850
851lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
852 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
853 LanguageType symbol_language, lldb::addr_t offset,
854 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
855 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
856
857 SBBreakpoint sb_bp;
858 TargetSP target_sp(GetSP());
859 if (target_sp && num_names > 0) {
860 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
861 const bool internal = false;
862 const bool hardware = false;
863 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +0000864 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 module_list.get(), comp_unit_list.get(), symbol_names, num_names,
866 name_type_mask, symbol_language, offset, skip_prologue, internal,
867 hardware);
868 }
869
870 if (log) {
871 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
872 static_cast<void *>(target_sp.get()));
873 for (uint32_t i = 0; i < num_names; i++) {
874 char sep;
875 if (i < num_names - 1)
876 sep = ',';
877 else
878 sep = '}';
879 if (symbol_names[i] != NULL)
880 log->Printf("\"%s\"%c ", symbol_names[i], sep);
881 else
882 log->Printf("\"<NULL>\"%c ", sep);
883 }
884 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +0000885 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 }
887
888 return sb_bp;
889}
890
891SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
892 const char *module_name) {
893 SBFileSpecList module_spec_list;
894 SBFileSpecList comp_unit_list;
895 if (module_name && module_name[0]) {
896 module_spec_list.Append(FileSpec(module_name, false));
897 }
898 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
899 module_spec_list, comp_unit_list);
900}
901
902lldb::SBBreakpoint
903SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
904 const SBFileSpecList &module_list,
905 const SBFileSpecList &comp_unit_list) {
906 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
907 module_list, comp_unit_list);
908}
909
910lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
911 const char *symbol_name_regex, LanguageType symbol_language,
912 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
913 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
914
915 SBBreakpoint sb_bp;
916 TargetSP target_sp(GetSP());
917 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
918 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000919 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920 const bool internal = false;
921 const bool hardware = false;
922 const LazyBool skip_prologue = eLazyBoolCalculate;
923
Pavel Labath6ac84032017-02-27 11:05:34 +0000924 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
926 skip_prologue, internal, hardware);
927 }
928
929 if (log)
930 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
931 "=> SBBreakpoint(%p)",
932 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +0000933 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934
935 return sb_bp;
936}
937
938SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
939 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
940
941 SBBreakpoint sb_bp;
942 TargetSP target_sp(GetSP());
943 if (target_sp) {
944 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
945 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000946 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 }
948
949 if (log)
950 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
951 ") => SBBreakpoint(%p)",
952 static_cast<void *>(target_sp.get()),
953 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +0000954 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955
956 return sb_bp;
957}
958
959SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
960 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
961
962 SBBreakpoint sb_bp;
963 TargetSP target_sp(GetSP());
964 if (!sb_address.IsValid()) {
965 if (log)
966 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
967 "invalid address",
968 static_cast<void *>(target_sp.get()));
969 return sb_bp;
970 }
971
972 if (target_sp) {
973 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
974 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000975 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 }
977
978 if (log) {
979 SBStream s;
980 sb_address.GetDescription(s);
981 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
982 "SBBreakpoint(%p)",
983 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +0000984 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 }
986
987 return sb_bp;
988}
989
990lldb::SBBreakpoint
991SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
992 const lldb::SBFileSpec &source_file,
993 const char *module_name) {
994 SBFileSpecList module_spec_list;
995
996 if (module_name && module_name[0]) {
997 module_spec_list.Append(FileSpec(module_name, false));
998 }
999
1000 SBFileSpecList source_file_list;
1001 if (source_file.IsValid()) {
1002 source_file_list.Append(source_file);
1003 }
1004
1005 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
1006 source_file_list);
1007}
1008
1009lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1010 const char *source_regex, const SBFileSpecList &module_list,
1011 const lldb::SBFileSpecList &source_file_list) {
1012 return BreakpointCreateBySourceRegex(source_regex, module_list,
1013 source_file_list, SBStringList());
1014}
1015
1016lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1017 const char *source_regex, const SBFileSpecList &module_list,
1018 const lldb::SBFileSpecList &source_file_list,
1019 const SBStringList &func_names) {
1020 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1021
1022 SBBreakpoint sb_bp;
1023 TargetSP target_sp(GetSP());
1024 if (target_sp && source_regex && source_regex[0]) {
1025 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1026 const bool hardware = false;
1027 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +00001028 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 std::unordered_set<std::string> func_names_set;
1030 for (size_t i = 0; i < func_names.GetSize(); i++) {
1031 func_names_set.insert(func_names.GetStringAtIndex(i));
1032 }
1033
Pavel Labath6ac84032017-02-27 11:05:34 +00001034 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 module_list.get(), source_file_list.get(), func_names_set, regexp,
1036 false, hardware, move_to_nearest_code);
1037 }
1038
1039 if (log)
1040 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1041 "=> SBBreakpoint(%p)",
1042 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001043 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044
1045 return sb_bp;
1046}
1047
1048lldb::SBBreakpoint
1049SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1050 bool catch_bp, bool throw_bp) {
1051 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1052
1053 SBBreakpoint sb_bp;
1054 TargetSP target_sp(GetSP());
1055 if (target_sp) {
1056 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1057 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001058 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 hardware);
1060 }
1061
1062 if (log)
Jim Ingham3815e702018-09-13 21:35:32 +00001063 log->Printf("SBTarget(%p)::BreakpointCreateForException (Language: %s, catch: "
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 "%s throw: %s) => SBBreakpoint(%p)",
1065 static_cast<void *>(target_sp.get()),
1066 Language::GetNameForLanguageType(language),
1067 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001068 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069
1070 return sb_bp;
1071}
1072
Jim Ingham3815e702018-09-13 21:35:32 +00001073lldb::SBBreakpoint
1074SBTarget::BreakpointCreateFromScript(const char *class_name,
1075 SBStructuredData &extra_args,
1076 const SBFileSpecList &module_list,
1077 const SBFileSpecList &file_list,
1078 bool request_hardware)
1079{
1080 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1081
1082 SBBreakpoint sb_bp;
1083 TargetSP target_sp(GetSP());
1084 if (target_sp) {
1085 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1086 Status error;
1087
1088 StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP();
1089 sb_bp =
1090 target_sp->CreateScriptedBreakpoint(class_name,
1091 module_list.get(),
1092 file_list.get(),
1093 false, /* internal */
1094 request_hardware,
1095 obj_sp,
1096 &error);
1097 }
1098 if (log)
1099 log->Printf("SBTarget(%p)::BreakpointCreateFromScript (class name: %s) "
1100 " => SBBreakpoint(%p)",
1101 static_cast<void *>(target_sp.get()),
1102 class_name,
1103 static_cast<void *>(sb_bp.GetSP().get()));
1104
1105 return sb_bp;
1106}
1107
1108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109uint32_t SBTarget::GetNumBreakpoints() const {
1110 TargetSP target_sp(GetSP());
1111 if (target_sp) {
1112 // The breakpoint list is thread safe, no need to lock
1113 return target_sp->GetBreakpointList().GetSize();
1114 }
1115 return 0;
1116}
1117
1118SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1119 SBBreakpoint sb_breakpoint;
1120 TargetSP target_sp(GetSP());
1121 if (target_sp) {
1122 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001123 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 }
1125 return sb_breakpoint;
1126}
1127
1128bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1129 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1130
1131 bool result = false;
1132 TargetSP target_sp(GetSP());
1133 if (target_sp) {
1134 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1135 result = target_sp->RemoveBreakpointByID(bp_id);
1136 }
1137
1138 if (log)
1139 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1140 static_cast<void *>(target_sp.get()),
1141 static_cast<uint32_t>(bp_id), result);
1142
1143 return result;
1144}
1145
1146SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1147 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1148
1149 SBBreakpoint sb_breakpoint;
1150 TargetSP target_sp(GetSP());
1151 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1152 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001153 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 }
1155
1156 if (log)
1157 log->Printf(
1158 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1159 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001160 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161
1162 return sb_breakpoint;
1163}
1164
Jim Inghamff9a91e2016-09-21 01:21:19 +00001165bool SBTarget::FindBreakpointsByName(const char *name,
1166 SBBreakpointList &bkpts) {
1167 TargetSP target_sp(GetSP());
1168 if (target_sp) {
1169 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1170 BreakpointList bkpt_list(false);
1171 bool is_valid =
1172 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1173 if (!is_valid)
1174 return false;
1175 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1176 bkpts.AppendByID(bkpt_sp->GetID());
1177 }
1178 }
1179 return true;
1180}
1181
Jim Inghamb842f2e2017-09-14 20:22:49 +00001182void SBTarget::GetBreakpointNames(SBStringList &names)
1183{
1184 names.Clear();
1185
1186 TargetSP target_sp(GetSP());
1187 if (target_sp) {
1188 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1189
1190 std::vector<std::string> name_vec;
1191 target_sp->GetBreakpointNames(name_vec);
1192 for (auto name : name_vec)
1193 names.AppendString(name.c_str());
1194 }
1195}
1196
1197void SBTarget::DeleteBreakpointName(const char *name)
1198{
1199 TargetSP target_sp(GetSP());
1200 if (target_sp) {
1201 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001202 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001203 }
1204}
1205
Kate Stoneb9c1b512016-09-06 20:57:50 +00001206bool SBTarget::EnableAllBreakpoints() {
1207 TargetSP target_sp(GetSP());
1208 if (target_sp) {
1209 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001210 target_sp->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211 return true;
1212 }
1213 return false;
1214}
1215
1216bool SBTarget::DisableAllBreakpoints() {
1217 TargetSP target_sp(GetSP());
1218 if (target_sp) {
1219 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001220 target_sp->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 return true;
1222 }
1223 return false;
1224}
1225
1226bool SBTarget::DeleteAllBreakpoints() {
1227 TargetSP target_sp(GetSP());
1228 if (target_sp) {
1229 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001230 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 return true;
1232 }
1233 return false;
1234}
1235
Jim Ingham01f16662016-09-14 19:07:35 +00001236lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1237 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001238 SBStringList empty_name_list;
1239 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1240}
1241
1242lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1243 SBStringList &matching_names,
1244 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001245 SBError sberr;
1246 TargetSP target_sp(GetSP());
1247 if (!target_sp) {
1248 sberr.SetErrorString(
1249 "BreakpointCreateFromFile called with invalid target.");
1250 return sberr;
1251 }
1252 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1253
1254 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001255
1256 std::vector<std::string> name_vector;
1257 size_t num_names = matching_names.GetSize();
1258 for (size_t i = 0; i < num_names; i++)
1259 name_vector.push_back(matching_names.GetStringAtIndex(i));
1260
1261 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1262 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001263 if (sberr.Fail())
1264 return sberr;
1265
1266 size_t num_bkpts = bp_ids.GetSize();
1267 for (size_t i = 0; i < num_bkpts; i++) {
1268 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1269 new_bps.AppendByID(bp_id.GetBreakpointID());
1270 }
1271 return sberr;
1272}
1273
1274lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1275 SBError sberr;
1276 TargetSP target_sp(GetSP());
1277 if (!target_sp) {
1278 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1279 return sberr;
1280 }
1281 SBBreakpointList bkpt_list(*this);
1282 return BreakpointsWriteToFile(dest_file, bkpt_list);
1283}
1284
1285lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001286 SBBreakpointList &bkpt_list,
1287 bool append) {
Jim Ingham01f16662016-09-14 19:07:35 +00001288 SBError sberr;
1289 TargetSP target_sp(GetSP());
1290 if (!target_sp) {
1291 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1292 return sberr;
1293 }
1294
1295 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1296 BreakpointIDList bp_id_list;
1297 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001298 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1299 bp_id_list, append);
Jim Ingham01f16662016-09-14 19:07:35 +00001300 return sberr;
1301}
1302
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303uint32_t SBTarget::GetNumWatchpoints() const {
1304 TargetSP target_sp(GetSP());
1305 if (target_sp) {
1306 // The watchpoint list is thread safe, no need to lock
1307 return target_sp->GetWatchpointList().GetSize();
1308 }
1309 return 0;
1310}
1311
1312SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1313 SBWatchpoint sb_watchpoint;
1314 TargetSP target_sp(GetSP());
1315 if (target_sp) {
1316 // The watchpoint list is thread safe, no need to lock
1317 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1318 }
1319 return sb_watchpoint;
1320}
1321
1322bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1323 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1324
1325 bool result = false;
1326 TargetSP target_sp(GetSP());
1327 if (target_sp) {
1328 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1329 std::unique_lock<std::recursive_mutex> lock;
1330 target_sp->GetWatchpointList().GetListMutex(lock);
1331 result = target_sp->RemoveWatchpointByID(wp_id);
1332 }
1333
1334 if (log)
1335 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1336 static_cast<void *>(target_sp.get()),
1337 static_cast<uint32_t>(wp_id), result);
1338
1339 return result;
1340}
1341
1342SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1343 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1344
1345 SBWatchpoint sb_watchpoint;
1346 lldb::WatchpointSP watchpoint_sp;
1347 TargetSP target_sp(GetSP());
1348 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1349 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1350 std::unique_lock<std::recursive_mutex> lock;
1351 target_sp->GetWatchpointList().GetListMutex(lock);
1352 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1353 sb_watchpoint.SetSP(watchpoint_sp);
1354 }
1355
1356 if (log)
1357 log->Printf(
1358 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1359 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1360 static_cast<void *>(watchpoint_sp.get()));
1361
1362 return sb_watchpoint;
1363}
1364
1365lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1366 bool read, bool write,
1367 SBError &error) {
1368 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1369
1370 SBWatchpoint sb_watchpoint;
1371 lldb::WatchpointSP watchpoint_sp;
1372 TargetSP target_sp(GetSP());
1373 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1374 size > 0) {
1375 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1376 uint32_t watch_type = 0;
1377 if (read)
1378 watch_type |= LLDB_WATCH_TYPE_READ;
1379 if (write)
1380 watch_type |= LLDB_WATCH_TYPE_WRITE;
1381 if (watch_type == 0) {
1382 error.SetErrorString(
1383 "Can't create a watchpoint that is neither read nor write.");
1384 return sb_watchpoint;
1385 }
1386
1387 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001388 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 // This API doesn't take in a type, so we can't figure out what it is.
1390 CompilerType *type = NULL;
1391 watchpoint_sp =
1392 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1393 error.SetError(cw_error);
1394 sb_watchpoint.SetSP(watchpoint_sp);
1395 }
1396
1397 if (log)
1398 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1399 ", 0x%u) => SBWatchpoint(%p)",
1400 static_cast<void *>(target_sp.get()), addr,
1401 static_cast<uint32_t>(size),
1402 static_cast<void *>(watchpoint_sp.get()));
1403
1404 return sb_watchpoint;
1405}
1406
1407bool SBTarget::EnableAllWatchpoints() {
1408 TargetSP target_sp(GetSP());
1409 if (target_sp) {
1410 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1411 std::unique_lock<std::recursive_mutex> lock;
1412 target_sp->GetWatchpointList().GetListMutex(lock);
1413 target_sp->EnableAllWatchpoints();
1414 return true;
1415 }
1416 return false;
1417}
1418
1419bool SBTarget::DisableAllWatchpoints() {
1420 TargetSP target_sp(GetSP());
1421 if (target_sp) {
1422 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1423 std::unique_lock<std::recursive_mutex> lock;
1424 target_sp->GetWatchpointList().GetListMutex(lock);
1425 target_sp->DisableAllWatchpoints();
1426 return true;
1427 }
1428 return false;
1429}
1430
1431SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1432 SBType type) {
1433 SBValue sb_value;
1434 lldb::ValueObjectSP new_value_sp;
1435 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1436 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1437 ExecutionContext exe_ctx(
1438 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1439 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1440 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1441 exe_ctx, ast_type);
1442 }
1443 sb_value.SetSP(new_value_sp);
1444 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1445 if (log) {
1446 if (new_value_sp)
1447 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1448 static_cast<void *>(m_opaque_sp.get()),
1449 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001450 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1452 static_cast<void *>(m_opaque_sp.get()));
1453 }
1454 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001455}
1456
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1458 lldb::SBType type) {
1459 SBValue sb_value;
1460 lldb::ValueObjectSP new_value_sp;
1461 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1462 DataExtractorSP extractor(*data);
1463 ExecutionContext exe_ctx(
1464 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1465 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1466 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1467 exe_ctx, ast_type);
1468 }
1469 sb_value.SetSP(new_value_sp);
1470 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1471 if (log) {
1472 if (new_value_sp)
1473 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1474 static_cast<void *>(m_opaque_sp.get()),
1475 new_value_sp->GetName().AsCString());
1476 else
1477 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1478 static_cast<void *>(m_opaque_sp.get()));
1479 }
1480 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001481}
1482
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1484 const char *expr) {
1485 SBValue sb_value;
1486 lldb::ValueObjectSP new_value_sp;
1487 if (IsValid() && name && *name && expr && *expr) {
1488 ExecutionContext exe_ctx(
1489 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1490 new_value_sp =
1491 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1492 }
1493 sb_value.SetSP(new_value_sp);
1494 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1495 if (log) {
1496 if (new_value_sp)
1497 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1498 static_cast<void *>(m_opaque_sp.get()),
1499 new_value_sp->GetName().AsCString());
1500 else
1501 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1502 static_cast<void *>(m_opaque_sp.get()));
1503 }
1504 return sb_value;
1505}
1506
1507bool SBTarget::DeleteAllWatchpoints() {
1508 TargetSP target_sp(GetSP());
1509 if (target_sp) {
1510 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1511 std::unique_lock<std::recursive_mutex> lock;
1512 target_sp->GetWatchpointList().GetListMutex(lock);
1513 target_sp->RemoveAllWatchpoints();
1514 return true;
1515 }
1516 return false;
1517}
1518
Alexander Polyakov3e7b9db2018-08-07 20:23:57 +00001519void SBTarget::AppendImageSearchPath(const char *from, const char *to,
1520 lldb::SBError &error) {
1521 TargetSP target_sp(GetSP());
1522 if (!target_sp)
1523 return error.SetErrorString("invalid target");
1524
1525 const ConstString csFrom(from), csTo(to);
1526 if (!csFrom)
1527 return error.SetErrorString("<from> path can't be empty");
1528 if (!csTo)
1529 return error.SetErrorString("<to> path can't be empty");
1530
1531 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1532 if (log)
1533 log->Printf("SBTarget(%p)::%s: '%s' -> '%s'",
1534 static_cast<void *>(target_sp.get()), __FUNCTION__,
1535 from, to);
1536 target_sp->GetImageSearchPathList().Append(csFrom, csTo, true);
1537}
1538
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1540 const char *uuid_cstr) {
1541 return AddModule(path, triple, uuid_cstr, NULL);
1542}
1543
1544lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1545 const char *uuid_cstr, const char *symfile) {
1546 lldb::SBModule sb_module;
1547 TargetSP target_sp(GetSP());
1548 if (target_sp) {
1549 ModuleSpec module_spec;
1550 if (path)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001551 module_spec.GetFileSpec().SetFile(path, false, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552
1553 if (uuid_cstr)
Pavel Labatha174bcb2018-06-21 15:24:39 +00001554 module_spec.GetUUID().SetFromStringRef(uuid_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555
1556 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001557 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1558 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 else
1560 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1561
1562 if (symfile)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001563 module_spec.GetSymbolFileSpec().SetFile(symfile, false,
1564 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565
1566 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1567 }
1568 return sb_module;
1569}
1570
1571lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1572 lldb::SBModule sb_module;
1573 TargetSP target_sp(GetSP());
1574 if (target_sp)
1575 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1576 return sb_module;
1577}
1578
1579bool SBTarget::AddModule(lldb::SBModule &module) {
1580 TargetSP target_sp(GetSP());
1581 if (target_sp) {
1582 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1583 return true;
1584 }
1585 return false;
1586}
1587
1588uint32_t SBTarget::GetNumModules() const {
1589 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1590
1591 uint32_t num = 0;
1592 TargetSP target_sp(GetSP());
1593 if (target_sp) {
1594 // The module list is thread safe, no need to lock
1595 num = target_sp->GetImages().GetSize();
1596 }
1597
1598 if (log)
1599 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1600 static_cast<void *>(target_sp.get()), num);
1601
1602 return num;
1603}
1604
1605void SBTarget::Clear() {
1606 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1607
1608 if (log)
1609 log->Printf("SBTarget(%p)::Clear ()",
1610 static_cast<void *>(m_opaque_sp.get()));
1611
1612 m_opaque_sp.reset();
1613}
1614
1615SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1616 SBModule sb_module;
1617 TargetSP target_sp(GetSP());
1618 if (target_sp && sb_file_spec.IsValid()) {
1619 ModuleSpec module_spec(*sb_file_spec);
1620 // The module list is thread safe, no need to lock
1621 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1622 }
1623 return sb_module;
1624}
1625
Alexander Polyakovda0c0812018-07-03 14:22:44 +00001626SBSymbolContextList
1627SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
1628 SBSymbolContextList sb_sc_list;
1629 const TargetSP target_sp(GetSP());
1630 if (target_sp && sb_file_spec.IsValid()) {
1631 const bool append = true;
1632 target_sp->GetImages().FindCompileUnits(*sb_file_spec,
1633 append, *sb_sc_list);
1634 }
1635 return sb_sc_list;
1636}
1637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638lldb::ByteOrder SBTarget::GetByteOrder() {
1639 TargetSP target_sp(GetSP());
1640 if (target_sp)
1641 return target_sp->GetArchitecture().GetByteOrder();
1642 return eByteOrderInvalid;
1643}
1644
1645const char *SBTarget::GetTriple() {
1646 TargetSP target_sp(GetSP());
1647 if (target_sp) {
1648 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001649 // Unique the string so we don't run into ownership issues since the const
1650 // strings put the string into the string pool once and the strings never
1651 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652 ConstString const_triple(triple.c_str());
1653 return const_triple.GetCString();
1654 }
1655 return NULL;
1656}
1657
1658uint32_t SBTarget::GetDataByteSize() {
1659 TargetSP target_sp(GetSP());
1660 if (target_sp) {
1661 return target_sp->GetArchitecture().GetDataByteSize();
1662 }
1663 return 0;
1664}
1665
1666uint32_t SBTarget::GetCodeByteSize() {
1667 TargetSP target_sp(GetSP());
1668 if (target_sp) {
1669 return target_sp->GetArchitecture().GetCodeByteSize();
1670 }
1671 return 0;
1672}
1673
1674uint32_t SBTarget::GetAddressByteSize() {
1675 TargetSP target_sp(GetSP());
1676 if (target_sp)
1677 return target_sp->GetArchitecture().GetAddressByteSize();
1678 return sizeof(void *);
1679}
1680
1681SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1682 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1683
1684 SBModule sb_module;
1685 ModuleSP module_sp;
1686 TargetSP target_sp(GetSP());
1687 if (target_sp) {
1688 // The module list is thread safe, no need to lock
1689 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1690 sb_module.SetSP(module_sp);
1691 }
1692
1693 if (log)
1694 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1695 static_cast<void *>(target_sp.get()), idx,
1696 static_cast<void *>(module_sp.get()));
1697
1698 return sb_module;
1699}
1700
1701bool SBTarget::RemoveModule(lldb::SBModule module) {
1702 TargetSP target_sp(GetSP());
1703 if (target_sp)
1704 return target_sp->GetImages().Remove(module.GetSP());
1705 return false;
1706}
1707
1708SBBroadcaster SBTarget::GetBroadcaster() const {
1709 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1710
1711 TargetSP target_sp(GetSP());
1712 SBBroadcaster broadcaster(target_sp.get(), false);
1713
1714 if (log)
1715 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1716 static_cast<void *>(target_sp.get()),
1717 static_cast<void *>(broadcaster.get()));
1718
1719 return broadcaster;
1720}
1721
1722bool SBTarget::GetDescription(SBStream &description,
1723 lldb::DescriptionLevel description_level) {
1724 Stream &strm = description.ref();
1725
1726 TargetSP target_sp(GetSP());
1727 if (target_sp) {
1728 target_sp->Dump(&strm, description_level);
1729 } else
1730 strm.PutCString("No value");
1731
1732 return true;
1733}
1734
1735lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1736 uint32_t name_type_mask) {
1737 lldb::SBSymbolContextList sb_sc_list;
1738 if (name && name[0]) {
Jim Ingham763b2b22015-07-07 22:12:17 +00001739 TargetSP target_sp(GetSP());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 if (target_sp) {
1741 const bool symbols_ok = true;
1742 const bool inlines_ok = true;
1743 const bool append = true;
1744 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask,
1745 symbols_ok, inlines_ok, append,
1746 *sb_sc_list);
1747 }
1748 }
1749 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001750}
Greg Claytone14e1922012-12-04 02:22:16 +00001751
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1753 uint32_t max_matches,
1754 MatchType matchtype) {
1755 lldb::SBSymbolContextList sb_sc_list;
1756 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001757 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 TargetSP target_sp(GetSP());
1759 if (target_sp) {
1760 std::string regexstr;
1761 switch (matchtype) {
1762 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001763 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 true, true, *sb_sc_list);
1765 break;
1766 case eMatchTypeStartsWith:
1767 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001768 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1769 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 break;
1771 default:
1772 target_sp->GetImages().FindFunctions(ConstString(name),
1773 eFunctionNameTypeAny, true, true,
1774 true, *sb_sc_list);
1775 break;
1776 }
1777 }
1778 }
1779 return sb_sc_list;
1780}
1781
1782lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1783 TargetSP target_sp(GetSP());
1784 if (typename_cstr && typename_cstr[0] && target_sp) {
1785 ConstString const_typename(typename_cstr);
1786 SymbolContext sc;
1787 const bool exact_match = false;
1788
1789 const ModuleList &module_list = target_sp->GetImages();
1790 size_t count = module_list.GetSize();
1791 for (size_t idx = 0; idx < count; idx++) {
1792 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1793 if (module_sp) {
1794 TypeSP type_sp(
1795 module_sp->FindFirstType(sc, const_typename, exact_match));
1796 if (type_sp)
1797 return SBType(type_sp);
1798 }
1799 }
1800
Adrian Prantl05097242018-04-30 16:49:04 +00001801 // Didn't find the type in the symbols; try the Objective-C runtime if one
1802 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803
1804 ProcessSP process_sp(target_sp->GetProcessSP());
1805
1806 if (process_sp) {
1807 ObjCLanguageRuntime *objc_language_runtime =
1808 process_sp->GetObjCLanguageRuntime();
1809
1810 if (objc_language_runtime) {
1811 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1812
1813 if (objc_decl_vendor) {
1814 std::vector<clang::NamedDecl *> decls;
1815
1816 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1817 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1818 return SBType(type);
1819 }
1820 }
1821 }
1822 }
1823 }
1824
1825 // No matches, search for basic typename matches
1826 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1827 if (clang_ast)
1828 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1829 const_typename));
1830 }
1831 return SBType();
1832}
1833
1834SBType SBTarget::GetBasicType(lldb::BasicType type) {
1835 TargetSP target_sp(GetSP());
1836 if (target_sp) {
1837 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1838 if (clang_ast)
1839 return SBType(
1840 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1841 }
1842 return SBType();
1843}
1844
1845lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1846 SBTypeList sb_type_list;
1847 TargetSP target_sp(GetSP());
1848 if (typename_cstr && typename_cstr[0] && target_sp) {
1849 ModuleList &images = target_sp->GetImages();
1850 ConstString const_typename(typename_cstr);
1851 bool exact_match = false;
1852 SymbolContext sc;
1853 TypeList type_list;
1854 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1855 uint32_t num_matches =
1856 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX,
1857 searched_symbol_files, type_list);
1858
1859 if (num_matches > 0) {
1860 for (size_t idx = 0; idx < num_matches; idx++) {
1861 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1862 if (type_sp)
1863 sb_type_list.Append(SBType(type_sp));
1864 }
1865 }
1866
1867 // Try the Objective-C runtime if one is installed
1868
1869 ProcessSP process_sp(target_sp->GetProcessSP());
1870
1871 if (process_sp) {
1872 ObjCLanguageRuntime *objc_language_runtime =
1873 process_sp->GetObjCLanguageRuntime();
1874
1875 if (objc_language_runtime) {
1876 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1877
1878 if (objc_decl_vendor) {
1879 std::vector<clang::NamedDecl *> decls;
1880
1881 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1882 for (clang::NamedDecl *decl : decls) {
1883 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1884 sb_type_list.Append(SBType(type));
1885 }
1886 }
1887 }
1888 }
1889 }
1890 }
1891
1892 if (sb_type_list.GetSize() == 0) {
1893 // No matches, search for basic typename matches
1894 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1895 if (clang_ast)
1896 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1897 clang_ast->getASTContext(), const_typename)));
1898 }
1899 }
1900 return sb_type_list;
1901}
1902
1903SBValueList SBTarget::FindGlobalVariables(const char *name,
1904 uint32_t max_matches) {
1905 SBValueList sb_value_list;
1906
1907 TargetSP target_sp(GetSP());
1908 if (name && target_sp) {
1909 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001911 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001912
1913 if (match_count > 0) {
1914 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1915 if (exe_scope == NULL)
1916 exe_scope = target_sp.get();
1917 for (uint32_t i = 0; i < match_count; ++i) {
1918 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1919 exe_scope, variable_list.GetVariableAtIndex(i)));
1920 if (valobj_sp)
1921 sb_value_list.Append(SBValue(valobj_sp));
1922 }
1923 }
1924 }
1925
1926 return sb_value_list;
1927}
1928
1929SBValueList SBTarget::FindGlobalVariables(const char *name,
1930 uint32_t max_matches,
1931 MatchType matchtype) {
1932 SBValueList sb_value_list;
1933
1934 TargetSP target_sp(GetSP());
1935 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001936 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001937 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001938
1939 std::string regexstr;
1940 uint32_t match_count;
1941 switch (matchtype) {
1942 case eMatchTypeNormal:
1943 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001944 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001945 break;
1946 case eMatchTypeRegex:
1947 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001948 RegularExpression(name_ref), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 break;
1950 case eMatchTypeStartsWith:
1951 regexstr = llvm::Regex::escape(name) + ".*";
1952 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001953 RegularExpression(regexstr), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954 break;
1955 }
1956
1957 if (match_count > 0) {
1958 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1959 if (exe_scope == NULL)
1960 exe_scope = target_sp.get();
1961 for (uint32_t i = 0; i < match_count; ++i) {
1962 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1963 exe_scope, variable_list.GetVariableAtIndex(i)));
1964 if (valobj_sp)
1965 sb_value_list.Append(SBValue(valobj_sp));
1966 }
1967 }
1968 }
1969
1970 return sb_value_list;
1971}
1972
1973lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1974 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1975 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1976 return sb_value_list.GetValueAtIndex(0);
1977 return SBValue();
1978}
1979
1980SBSourceManager SBTarget::GetSourceManager() {
1981 SBSourceManager source_manager(*this);
1982 return source_manager;
1983}
1984
1985lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1986 uint32_t count) {
1987 return ReadInstructions(base_addr, count, NULL);
1988}
1989
1990lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1991 uint32_t count,
1992 const char *flavor_string) {
1993 SBInstructionList sb_instructions;
1994
1995 TargetSP target_sp(GetSP());
1996 if (target_sp) {
1997 Address *addr_ptr = base_addr.get();
1998
1999 if (addr_ptr) {
2000 DataBufferHeap data(
2001 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2002 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00002003 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002004 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2005 const size_t bytes_read =
2006 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
2007 data.GetByteSize(), error, &load_addr);
2008 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2009 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2010 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
2011 data.GetBytes(), bytes_read, count, data_from_file));
2012 }
2013 }
2014
2015 return sb_instructions;
2016}
2017
2018lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
2019 const void *buf,
2020 size_t size) {
2021 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
2022}
2023
2024lldb::SBInstructionList
2025SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
2026 const char *flavor_string, const void *buf,
2027 size_t size) {
2028 SBInstructionList sb_instructions;
2029
2030 TargetSP target_sp(GetSP());
2031 if (target_sp) {
2032 Address addr;
2033
2034 if (base_addr.get())
2035 addr = *base_addr.get();
2036
2037 const bool data_from_file = true;
2038
2039 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2040 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
2041 UINT32_MAX, data_from_file));
2042 }
2043
2044 return sb_instructions;
2045}
2046
2047lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
2048 const void *buf,
2049 size_t size) {
2050 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
2051 size);
2052}
2053
2054lldb::SBInstructionList
2055SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
2056 const char *flavor_string, const void *buf,
2057 size_t size) {
2058 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
2059 buf, size);
2060}
2061
2062SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
2063 lldb::addr_t section_base_addr) {
2064 SBError sb_error;
2065 TargetSP target_sp(GetSP());
2066 if (target_sp) {
2067 if (!section.IsValid()) {
2068 sb_error.SetErrorStringWithFormat("invalid section");
2069 } else {
2070 SectionSP section_sp(section.GetSP());
2071 if (section_sp) {
2072 if (section_sp->IsThreadSpecific()) {
2073 sb_error.SetErrorString(
2074 "thread specific sections are not yet supported");
2075 } else {
2076 ProcessSP process_sp(target_sp->GetProcessSP());
2077 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
2078 ModuleSP module_sp(section_sp->GetModule());
2079 if (module_sp) {
2080 ModuleList module_list;
2081 module_list.Append(module_sp);
2082 target_sp->ModulesDidLoad(module_list);
2083 }
2084 // Flush info in the process (stack frames, etc)
2085 if (process_sp)
2086 process_sp->Flush();
2087 }
2088 }
2089 }
2090 }
2091 } else {
2092 sb_error.SetErrorString("invalid target");
2093 }
2094 return sb_error;
2095}
2096
2097SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
2098 SBError sb_error;
2099
2100 TargetSP target_sp(GetSP());
2101 if (target_sp) {
2102 if (!section.IsValid()) {
2103 sb_error.SetErrorStringWithFormat("invalid section");
2104 } else {
2105 SectionSP section_sp(section.GetSP());
2106 if (section_sp) {
2107 ProcessSP process_sp(target_sp->GetProcessSP());
2108 if (target_sp->SetSectionUnloaded(section_sp)) {
2109 ModuleSP module_sp(section_sp->GetModule());
2110 if (module_sp) {
2111 ModuleList module_list;
2112 module_list.Append(module_sp);
2113 target_sp->ModulesDidUnload(module_list, false);
2114 }
2115 // Flush info in the process (stack frames, etc)
2116 if (process_sp)
2117 process_sp->Flush();
2118 }
2119 } else {
2120 sb_error.SetErrorStringWithFormat("invalid section");
2121 }
2122 }
2123 } else {
2124 sb_error.SetErrorStringWithFormat("invalid target");
2125 }
2126 return sb_error;
2127}
2128
2129SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2130 int64_t slide_offset) {
2131 SBError sb_error;
2132
2133 TargetSP target_sp(GetSP());
2134 if (target_sp) {
2135 ModuleSP module_sp(module.GetSP());
2136 if (module_sp) {
2137 bool changed = false;
2138 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2139 // The load was successful, make sure that at least some sections
2140 // changed before we notify that our module was loaded.
2141 if (changed) {
2142 ModuleList module_list;
2143 module_list.Append(module_sp);
2144 target_sp->ModulesDidLoad(module_list);
2145 // Flush info in the process (stack frames, etc)
2146 ProcessSP process_sp(target_sp->GetProcessSP());
2147 if (process_sp)
2148 process_sp->Flush();
2149 }
2150 }
2151 } else {
2152 sb_error.SetErrorStringWithFormat("invalid module");
2153 }
2154
2155 } else {
2156 sb_error.SetErrorStringWithFormat("invalid target");
2157 }
2158 return sb_error;
2159}
2160
2161SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2162 SBError sb_error;
2163
2164 char path[PATH_MAX];
2165 TargetSP target_sp(GetSP());
2166 if (target_sp) {
2167 ModuleSP module_sp(module.GetSP());
2168 if (module_sp) {
2169 ObjectFile *objfile = module_sp->GetObjectFile();
2170 if (objfile) {
2171 SectionList *section_list = objfile->GetSectionList();
2172 if (section_list) {
2173 ProcessSP process_sp(target_sp->GetProcessSP());
2174
2175 bool changed = false;
2176 const size_t num_sections = section_list->GetSize();
2177 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2178 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2179 if (section_sp)
2180 changed |= target_sp->SetSectionUnloaded(section_sp);
2181 }
2182 if (changed) {
2183 ModuleList module_list;
2184 module_list.Append(module_sp);
2185 target_sp->ModulesDidUnload(module_list, false);
2186 // Flush info in the process (stack frames, etc)
2187 ProcessSP process_sp(target_sp->GetProcessSP());
2188 if (process_sp)
2189 process_sp->Flush();
2190 }
2191 } else {
2192 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2193 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2194 path);
2195 }
2196 } else {
2197 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2198 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2199 path);
2200 }
2201 } else {
2202 sb_error.SetErrorStringWithFormat("invalid module");
2203 }
2204 } else {
2205 sb_error.SetErrorStringWithFormat("invalid target");
2206 }
2207 return sb_error;
2208}
2209
2210lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2211 lldb::SymbolType symbol_type) {
2212 SBSymbolContextList sb_sc_list;
2213 if (name && name[0]) {
2214 TargetSP target_sp(GetSP());
2215 if (target_sp) {
2216 bool append = true;
2217 target_sp->GetImages().FindSymbolsWithNameAndType(
2218 ConstString(name), symbol_type, *sb_sc_list, append);
2219 }
2220 }
2221 return sb_sc_list;
2222}
2223
2224lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2225 TargetSP target_sp(GetSP());
2226 if (!target_sp)
2227 return SBValue();
2228
2229 SBExpressionOptions options;
2230 lldb::DynamicValueType fetch_dynamic_value =
2231 target_sp->GetPreferDynamicValue();
2232 options.SetFetchDynamicValue(fetch_dynamic_value);
2233 options.SetUnwindOnError(true);
2234 return EvaluateExpression(expr, options);
2235}
2236
2237lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2238 const SBExpressionOptions &options) {
2239 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002240#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002241 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002242#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243 SBValue expr_result;
2244 ExpressionResults exe_results = eExpressionSetupError;
2245 ValueObjectSP expr_value_sp;
2246 TargetSP target_sp(GetSP());
2247 StackFrame *frame = NULL;
2248 if (target_sp) {
2249 if (expr == NULL || expr[0] == '\0') {
2250 if (log)
2251 log->Printf(
2252 "SBTarget::EvaluateExpression called with an empty expression");
2253 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002255
2256 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2257 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002258
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002259 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002260 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2261
2262 frame = exe_ctx.GetFramePtr();
2263 Target *target = exe_ctx.GetTargetPtr();
2264
2265 if (target) {
2266#ifdef LLDB_CONFIGURATION_DEBUG
2267 StreamString frame_description;
2268 if (frame)
2269 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002270 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2272 "%u) %s",
2273 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002274 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275#endif
2276 exe_results =
2277 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2278
2279 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002280 } else {
2281 if (log)
2282 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2283 "reconstruct frame object for this SBTarget.");
2284 }
2285 }
2286#ifndef LLDB_DISABLE_PYTHON
2287 if (expr_log)
2288 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2289 "%s, summary %s **",
2290 expr_result.GetValue(), expr_result.GetSummary());
2291
2292 if (log)
2293 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2294 "(execution result=%d)",
2295 static_cast<void *>(frame), expr,
2296 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002297#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002298
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002300}
2301
Kate Stoneb9c1b512016-09-06 20:57:50 +00002302lldb::addr_t SBTarget::GetStackRedZoneSize() {
2303 TargetSP target_sp(GetSP());
2304 if (target_sp) {
2305 ABISP abi_sp;
2306 ProcessSP process_sp(target_sp->GetProcessSP());
2307 if (process_sp)
2308 abi_sp = process_sp->GetABI();
2309 else
Jason Molenda43294c92017-06-29 02:57:03 +00002310 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311 if (abi_sp)
2312 return abi_sp->GetRedZoneSize();
2313 }
2314 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002315}
Ilia K8f37ca52015-02-13 14:31:06 +00002316
Kate Stoneb9c1b512016-09-06 20:57:50 +00002317lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2318 lldb::SBLaunchInfo launch_info(NULL);
2319 TargetSP target_sp(GetSP());
2320 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002321 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002323}
2324
Kate Stoneb9c1b512016-09-06 20:57:50 +00002325void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2326 TargetSP target_sp(GetSP());
2327 if (target_sp)
2328 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002329}