blob: 15cdb0f312c9bc13ce849b3091639d2127b2899e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman4c5de692010-06-09 07:44:37 +000010#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Greg Claytone0d378b2011-03-24 21:19:54 +000012#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytondea8cb42011-06-29 22:09:02 +000014#include "lldb/API/SBBreakpoint.h"
Ilia Keb2c19a2015-03-10 21:59:55 +000015#include "lldb/API/SBDebugger.h"
16#include "lldb/API/SBEvent.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000017#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000019#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000021#include "lldb/API/SBModuleSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000022#include "lldb/API/SBProcess.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Jim Ingham76bb8d62016-04-28 01:40:57 +000025#include "lldb/API/SBStringList.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000026#include "lldb/API/SBStructuredData.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000027#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Breakpoint/BreakpointID.h"
29#include "lldb/Breakpoint/BreakpointIDList.h"
30#include "lldb/Breakpoint/BreakpointList.h"
31#include "lldb/Breakpoint/BreakpointLocation.h"
32#include "lldb/Core/Address.h"
33#include "lldb/Core/AddressResolver.h"
34#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/Module.h"
38#include "lldb/Core/ModuleSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000039#include "lldb/Core/STLUtils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Core/Section.h"
Davide Italiano4a6a2b12018-04-16 22:55:34 +000042#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000043#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000044#include "lldb/Core/ValueObjectList.h"
45#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000046#include "lldb/Host/Host.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Symbol/ClangASTContext.h"
48#include "lldb/Symbol/DeclVendor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000049#include "lldb/Symbol/ObjectFile.h"
Greg Claytonae088e52016-02-10 21:28:13 +000050#include "lldb/Symbol/SymbolFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000053#include "lldb/Target/ABI.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000054#include "lldb/Target/Language.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000055#include "lldb/Target/LanguageRuntime.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000056#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Process.h"
Enrico Granatab10e0032015-03-04 21:33:45 +000058#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059#include "lldb/Target/Target.h"
60#include "lldb/Target/TargetList.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000061#include "lldb/Utility/ArchSpec.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000062#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000063#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000064#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000065#include "lldb/Utility/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
James Y Knight2ad48212018-05-22 22:53:50 +000067#include "Commands/CommandObjectBreakpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000068#include "lldb/Interpreter/CommandReturnObject.h"
Sean Callanan237c3ed2016-12-14 21:31:31 +000069#include "llvm/Support/PrettyStackTrace.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000070#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072using namespace lldb;
73using namespace lldb_private;
74
75#define DEFAULT_DISASM_BYTE_SIZE 32
76
Oleksiy Vyalov37386142015-02-10 22:49:57 +000077namespace {
78
Zachary Turner97206d52017-05-12 04:51:55 +000079Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
Oleksiy Vyalov37386142015-02-10 22:49:57 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 auto process_sp = target.GetProcessSP();
83 if (process_sp) {
84 const auto state = process_sp->GetState();
85 if (process_sp->IsAlive() && state == eStateConnected) {
86 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +000087 // listener, so if a valid listener is supplied, we need to error out to
88 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (attach_info.GetListener())
Zachary Turner97206d52017-05-12 04:51:55 +000090 return Status("process is connected and already has a listener, pass "
91 "empty listener");
Oleksiy Vyalov37386142015-02-10 22:49:57 +000092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 return target.Attach(attach_info, nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098} // namespace
Oleksiy Vyalov37386142015-02-10 22:49:57 +000099
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100//----------------------------------------------------------------------
101// SBTarget constructor
102//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103SBTarget::SBTarget() : m_opaque_sp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
110 if (this != &rhs)
111 m_opaque_sp = rhs.m_opaque_sp;
112 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000113}
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115//----------------------------------------------------------------------
116// Destructor
117//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118SBTarget::~SBTarget() {}
119
120bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
121 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
125 return Target::TargetEventData::GetTargetFromEvent(event.get());
Ilia Keb2c19a2015-03-10 21:59:55 +0000126}
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
129 const ModuleList module_list =
130 Target::TargetEventData::GetModuleListFromEvent(event.get());
131 return module_list.GetSize();
Ilia Keb2c19a2015-03-10 21:59:55 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
135 const SBEvent &event) {
136 const ModuleList module_list =
137 Target::TargetEventData::GetModuleListFromEvent(event.get());
138 return SBModule(module_list.GetModuleAtIndex(idx));
Ilia Keb2c19a2015-03-10 21:59:55 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141const char *SBTarget::GetBroadcasterClassName() {
142 return Target::GetStaticBroadcasterClass().AsCString();
Ilia Keb2c19a2015-03-10 21:59:55 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145bool SBTarget::IsValid() const {
146 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149SBProcess SBTarget::GetProcess() {
150 SBProcess sb_process;
151 ProcessSP process_sp;
152 TargetSP target_sp(GetSP());
153 if (target_sp) {
154 process_sp = target_sp->GetProcessSP();
155 sb_process.SetSP(process_sp);
156 }
157
158 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
159 if (log)
160 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
161 static_cast<void *>(target_sp.get()),
162 static_cast<void *>(process_sp.get()));
163
164 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167SBPlatform SBTarget::GetPlatform() {
168 TargetSP target_sp(GetSP());
169 if (!target_sp)
170 return SBPlatform();
171
172 SBPlatform platform;
173 platform.m_opaque_sp = target_sp->GetPlatform();
174
175 return platform;
176}
177
178SBDebugger SBTarget::GetDebugger() const {
179 SBDebugger debugger;
180 TargetSP target_sp(GetSP());
181 if (target_sp)
182 debugger.reset(target_sp->GetDebugger().shared_from_this());
183 return debugger;
184}
185
Davide Italiano4a6a2b12018-04-16 22:55:34 +0000186SBStructuredData SBTarget::GetStatistics() {
187 SBStructuredData data;
188 TargetSP target_sp(GetSP());
189 if (!target_sp)
190 return data;
191
192 auto stats_up = llvm::make_unique<StructuredData::Dictionary>();
193 int i = 0;
194 for (auto &Entry : target_sp->GetStatistics()) {
195 std::string Desc = lldb_private::GetStatDescription(
196 static_cast<lldb_private::StatisticKind>(i));
197 stats_up->AddIntegerItem(Desc, Entry);
198 i += 1;
199 }
200
201 data.m_impl_up->SetObjectSP(std::move(stats_up));
202 return data;
203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205SBProcess SBTarget::LoadCore(const char *core_file) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000206 lldb::SBError error; // Ignored
207 return LoadCore(core_file, error);
208}
209
210SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 SBProcess sb_process;
212 TargetSP target_sp(GetSP());
213 if (target_sp) {
214 FileSpec filespec(core_file, true);
215 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000216 target_sp->GetDebugger().GetListener(), "", &filespec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 if (process_sp) {
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000218 error.SetError(process_sp->LoadCore());
219 if (error.Success())
220 sb_process.SetSP(process_sp);
221 } else {
222 error.SetErrorString("Failed to create the process");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 }
Leonard Mosescue1bb5172018-06-11 21:19:26 +0000224 } else {
225 error.SetErrorString("SBTarget is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 }
227 return sb_process;
228}
229
230SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
231 const char *working_directory) {
232 char *stdin_path = NULL;
233 char *stdout_path = NULL;
234 char *stderr_path = NULL;
235 uint32_t launch_flags = 0;
236 bool stop_at_entry = false;
237 SBError error;
238 SBListener listener = GetDebugger().GetListener();
239 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path,
240 working_directory, launch_flags, stop_at_entry, error);
241}
242
243SBError SBTarget::Install() {
244 SBError sb_error;
245 TargetSP target_sp(GetSP());
246 if (target_sp) {
247 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
248 sb_error.ref() = target_sp->Install(NULL);
249 }
250 return sb_error;
251}
252
253SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
254 char const **envp, const char *stdin_path,
255 const char *stdout_path, const char *stderr_path,
256 const char *working_directory,
257 uint32_t launch_flags, // See LaunchFlags
258 bool stop_at_entry, lldb::SBError &error) {
259 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
260
261 SBProcess sb_process;
262 ProcessSP process_sp;
263 TargetSP target_sp(GetSP());
264
265 if (log)
266 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
267 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
268 "stop_at_entry=%i, &error (%p))...",
269 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
270 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
271 stdout_path ? stdout_path : "NULL",
272 stderr_path ? stderr_path : "NULL",
273 working_directory ? working_directory : "NULL", launch_flags,
274 stop_at_entry, static_cast<void *>(error.get()));
275
276 if (target_sp) {
277 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
278
279 if (stop_at_entry)
280 launch_flags |= eLaunchFlagStopAtEntry;
281
282 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
283 launch_flags |= eLaunchFlagDisableASLR;
284
285 StateType state = eStateInvalid;
286 process_sp = target_sp->GetProcessSP();
287 if (process_sp) {
288 state = process_sp->GetState();
289
290 if (process_sp->IsAlive() && state != eStateConnected) {
291 if (state == eStateAttaching)
292 error.SetErrorString("process attach is in progress");
293 else
294 error.SetErrorString("a process is already being debugged");
295 return sb_process;
296 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000297 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 if (state == eStateConnected) {
300 // If we are already connected, then we have already specified the
Adrian Prantl05097242018-04-30 16:49:04 +0000301 // listener, so if a valid listener is supplied, we need to error out to
302 // let the client know.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 if (listener.IsValid()) {
304 error.SetErrorString("process is connected and already has a listener, "
305 "pass empty listener");
306 return sb_process;
307 }
308 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
311 launch_flags |= eLaunchFlagDisableSTDIO;
312
313 ProcessLaunchInfo launch_info(
314 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
315 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
316 launch_flags);
317
318 Module *exe_module = target_sp->GetExecutableModulePointer();
319 if (exe_module)
320 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
321 if (argv)
322 launch_info.GetArguments().AppendArguments(argv);
323 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000324 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325
326 if (listener.IsValid())
327 launch_info.SetListener(listener.GetSP());
328
329 error.SetError(target_sp->Launch(launch_info, NULL));
330
331 sb_process.SetSP(target_sp->GetProcessSP());
332 } else {
333 error.SetErrorString("SBTarget is invalid");
334 }
335
336 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
337 if (log)
338 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
339 static_cast<void *>(target_sp.get()),
340 static_cast<void *>(sb_process.GetSP().get()),
341 error.GetCString());
342
343 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
347 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 SBProcess sb_process;
350 TargetSP target_sp(GetSP());
Matthew Gardinerc928de32014-10-22 07:22:56 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 if (log)
353 log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
354 static_cast<void *>(target_sp.get()));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 if (target_sp) {
357 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
358 StateType state = eStateInvalid;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000359 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 ProcessSP process_sp = target_sp->GetProcessSP();
361 if (process_sp) {
362 state = process_sp->GetState();
363
364 if (process_sp->IsAlive() && state != eStateConnected) {
365 if (state == eStateAttaching)
366 error.SetErrorString("process attach is in progress");
367 else
368 error.SetErrorString("a process is already being debugged");
369 return sb_process;
Greg Clayton4d8ad552013-03-25 22:40:51 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000372 }
Greg Clayton4d8ad552013-03-25 22:40:51 +0000373
Pavel Labath62930e52018-01-10 11:57:31 +0000374 lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 if (!launch_info.GetExecutableFile()) {
377 Module *exe_module = target_sp->GetExecutableModulePointer();
378 if (exe_module)
379 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Claytonfbb76342013-11-20 21:07:01 +0000380 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381
382 const ArchSpec &arch_spec = target_sp->GetArchitecture();
383 if (arch_spec.IsValid())
384 launch_info.GetArchitecture() = arch_spec;
385
386 error.SetError(target_sp->Launch(launch_info, NULL));
Pavel Labath62930e52018-01-10 11:57:31 +0000387 sb_launch_info.set_ref(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 sb_process.SetSP(target_sp->GetProcessSP());
389 } else {
390 error.SetErrorString("SBTarget is invalid");
391 }
392
393 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
394 if (log)
395 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
396 static_cast<void *>(target_sp.get()),
397 static_cast<void *>(sb_process.GetSP().get()));
398
399 return sb_process;
Greg Claytonfbb76342013-11-20 21:07:01 +0000400}
401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
403 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 SBProcess sb_process;
406 TargetSP target_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 if (log)
409 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
410 static_cast<void *>(target_sp.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 if (target_sp) {
413 ProcessAttachInfo &attach_info = sb_attach_info.ref();
414 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
415 PlatformSP platform_sp = target_sp->GetPlatform();
416 // See if we can pre-verify if a process exists or not
417 if (platform_sp && platform_sp->IsConnected()) {
418 lldb::pid_t attach_pid = attach_info.GetProcessID();
419 ProcessInstanceInfo instance_info;
420 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
421 attach_info.SetUserID(instance_info.GetEffectiveUserID());
422 } else {
423 error.ref().SetErrorStringWithFormat(
424 "no process found with process ID %" PRIu64, attach_pid);
425 if (log) {
426 log->Printf("SBTarget(%p)::Attach (...) => error %s",
427 static_cast<void *>(target_sp.get()),
428 error.GetCString());
429 }
430 return sb_process;
Greg Clayton931180e2011-01-27 06:44:37 +0000431 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 }
Greg Clayton524e60b2010-10-06 22:10:17 +0000433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 error.SetError(AttachToProcess(attach_info, *target_sp));
435 if (error.Success())
436 sb_process.SetSP(target_sp->GetProcessSP());
437 } else {
438 error.SetErrorString("SBTarget is invalid");
439 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 if (log)
442 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
443 static_cast<void *>(target_sp.get()),
444 static_cast<void *>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449lldb::SBProcess SBTarget::AttachToProcessWithID(
James McIlree9631aae2011-03-04 00:31:13 +0000450 SBListener &listener,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 lldb::pid_t pid, // The process ID to attach to
452 SBError &error // An error explaining what went wrong if attach fails
453 ) {
454 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 SBProcess sb_process;
457 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 if (log)
460 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
461 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 if (target_sp) {
464 ProcessAttachInfo attach_info;
465 attach_info.SetProcessID(pid);
466 if (listener.IsValid())
467 attach_info.SetListener(listener.GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 ProcessInstanceInfo instance_info;
470 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
471 attach_info.SetUserID(instance_info.GetEffectiveUserID());
472
473 error.SetError(AttachToProcess(attach_info, *target_sp));
474 if (error.Success())
475 sb_process.SetSP(target_sp->GetProcessSP());
476 } else
477 error.SetErrorString("SBTarget is invalid");
478
479 if (log)
480 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
481 static_cast<void *>(target_sp.get()), __FUNCTION__,
482 static_cast<void *>(sb_process.GetSP().get()));
483 return sb_process;
484}
485
486lldb::SBProcess SBTarget::AttachToProcessWithName(
487 SBListener &listener,
488 const char *name, // basename of process to attach to
489 bool wait_for, // if true wait for a new instance of "name" to be launched
490 SBError &error // An error explaining what went wrong if attach fails
491 ) {
492 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
493
494 SBProcess sb_process;
495 TargetSP target_sp(GetSP());
496
497 if (log)
498 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
499 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
500 wait_for ? "true" : "false");
501
502 if (name && target_sp) {
503 ProcessAttachInfo attach_info;
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000504 attach_info.GetExecutableFile().SetFile(name, false,
505 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 attach_info.SetWaitForLaunch(wait_for);
507 if (listener.IsValid())
508 attach_info.SetListener(listener.GetSP());
509
510 error.SetError(AttachToProcess(attach_info, *target_sp));
511 if (error.Success())
512 sb_process.SetSP(target_sp->GetProcessSP());
513 } else
514 error.SetErrorString("SBTarget is invalid");
515
516 if (log)
517 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
518 static_cast<void *>(target_sp.get()), __FUNCTION__,
519 static_cast<void *>(sb_process.GetSP().get()));
520 return sb_process;
521}
522
523lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
524 const char *plugin_name,
525 SBError &error) {
526 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
527
528 SBProcess sb_process;
529 ProcessSP process_sp;
530 TargetSP target_sp(GetSP());
531
532 if (log)
533 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
534 "plugin_name=%s, error)...",
535 static_cast<void *>(target_sp.get()), url, plugin_name);
536
537 if (target_sp) {
538 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
539 if (listener.IsValid())
540 process_sp =
541 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +0000542 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 process_sp = target_sp->CreateProcess(
544 target_sp->GetDebugger().GetListener(), plugin_name, NULL);
545
546 if (process_sp) {
547 sb_process.SetSP(process_sp);
548 error.SetError(process_sp->ConnectRemote(NULL, url));
549 } else {
550 error.SetErrorString("unable to create lldb_private::Process");
James McIlree9631aae2011-03-04 00:31:13 +0000551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 } else {
553 error.SetErrorString("SBTarget is invalid");
554 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 if (log)
557 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
558 static_cast<void *>(target_sp.get()),
559 static_cast<void *>(process_sp.get()));
560 return sb_process;
James McIlree9631aae2011-03-04 00:31:13 +0000561}
562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563SBFileSpec SBTarget::GetExecutable() {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 SBFileSpec exe_file_spec;
566 TargetSP target_sp(GetSP());
567 if (target_sp) {
568 Module *exe_module = target_sp->GetExecutableModulePointer();
569 if (exe_module)
570 exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
571 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
574 if (log) {
575 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
576 static_cast<void *>(target_sp.get()),
577 static_cast<const void *>(exe_file_spec.get()));
578 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 return exe_file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583bool SBTarget::operator==(const SBTarget &rhs) const {
584 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587bool SBTarget::operator!=(const SBTarget &rhs) const {
588 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589}
590
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
592
593void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
594 m_opaque_sp = target_sp;
Greg Clayton9a377662011-10-01 02:59:24 +0000595}
596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
598 lldb::SBAddress sb_addr;
599 Address &addr = sb_addr.ref();
600 TargetSP target_sp(GetSP());
601 if (target_sp) {
602 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
603 if (target_sp->ResolveLoadAddress(vm_addr, addr))
604 return sb_addr;
605 }
606
607 // We have a load address that isn't in a section, just return an address
608 // with the offset filled in (the address) and the section set to NULL
609 addr.SetRawAddress(vm_addr);
610 return sb_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611}
612
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
614 lldb::SBAddress sb_addr;
615 Address &addr = sb_addr.ref();
616 TargetSP target_sp(GetSP());
617 if (target_sp) {
618 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
619 if (target_sp->ResolveFileAddress(file_addr, addr))
620 return sb_addr;
621 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 addr.SetRawAddress(file_addr);
624 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +0000625}
626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
628 lldb::addr_t vm_addr) {
629 lldb::SBAddress sb_addr;
630 Address &addr = sb_addr.ref();
631 TargetSP target_sp(GetSP());
632 if (target_sp) {
633 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
634 if (target_sp->ResolveLoadAddress(vm_addr, addr))
635 return sb_addr;
636 }
Matthew Gardinerc928de32014-10-22 07:22:56 +0000637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 // We have a load address that isn't in a section, just return an address
639 // with the offset filled in (the address) and the section set to NULL
640 addr.SetRawAddress(vm_addr);
641 return sb_addr;
Greg Claytond5944cd2013-12-06 01:12:00 +0000642}
643
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000644SBSymbolContext
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
646 uint32_t resolve_scope) {
647 SBSymbolContext sc;
648 if (addr.IsValid()) {
Matthew Gardinerc928de32014-10-22 07:22:56 +0000649 TargetSP target_sp(GetSP());
650 if (target_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 target_sp->GetImages().ResolveSymbolContextForAddress(
652 addr.ref(), resolve_scope, sc.ref());
653 }
654 return sc;
655}
Matthew Gardinerc928de32014-10-22 07:22:56 +0000656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
658 lldb::SBError &error) {
659 SBError sb_error;
660 size_t bytes_read = 0;
661 TargetSP target_sp(GetSP());
662 if (target_sp) {
663 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
664 bytes_read =
665 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
666 } else {
667 sb_error.SetErrorString("invalid target");
668 }
669
670 return bytes_read;
671}
672
673SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
674 uint32_t line) {
675 return SBBreakpoint(
676 BreakpointCreateByLocation(SBFileSpec(file, false), line));
Matthew Gardinerc928de32014-10-22 07:22:56 +0000677}
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000678
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
681 uint32_t line) {
682 return BreakpointCreateByLocation(sb_file_spec, line, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683}
684
685SBBreakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
687 uint32_t line, lldb::addr_t offset) {
Jim Ingham92d19602016-09-20 22:54:49 +0000688 SBFileSpecList empty_list;
689 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
690}
691
692SBBreakpoint
693SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
694 uint32_t line, lldb::addr_t offset,
695 SBFileSpecList &sb_module_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham24111672016-03-09 18:59:13 +0000697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 SBBreakpoint sb_bp;
699 TargetSP target_sp(GetSP());
700 if (target_sp && line != 0) {
701 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000702
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 const LazyBool check_inlines = eLazyBoolCalculate;
704 const LazyBool skip_prologue = eLazyBoolCalculate;
705 const bool internal = false;
706 const bool hardware = false;
707 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Jim Ingham92d19602016-09-20 22:54:49 +0000708 const FileSpecList *module_list = nullptr;
709 if (sb_module_list.GetSize() > 0) {
710 module_list = sb_module_list.get();
711 }
Pavel Labath6ac84032017-02-27 11:05:34 +0000712 sb_bp = target_sp->CreateBreakpoint(
Jim Ingham92d19602016-09-20 22:54:49 +0000713 module_list, *sb_file_spec, line, offset, check_inlines, skip_prologue,
714 internal, hardware, move_to_nearest_code);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000716
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 if (log) {
718 SBStream sstr;
719 sb_bp.GetDescription(sstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000720 char path[PATH_MAX];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 sb_file_spec->GetPath(path, sizeof(path));
722 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
723 "SBBreakpoint(%p): %s",
724 static_cast<void *>(target_sp.get()), path, line,
Pavel Labath6ac84032017-02-27 11:05:34 +0000725 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 }
Greg Claytond5944cd2013-12-06 01:12:00 +0000727
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 return sb_bp;
729}
730
731SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
732 const char *module_name) {
733 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
734
735 SBBreakpoint sb_bp;
736 TargetSP target_sp(GetSP());
737 if (target_sp.get()) {
738 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
739
740 const bool internal = false;
741 const bool hardware = false;
742 const LazyBool skip_prologue = eLazyBoolCalculate;
743 const lldb::addr_t offset = 0;
744 if (module_name && module_name[0]) {
745 FileSpecList module_spec_list;
746 module_spec_list.Append(FileSpec(module_name, false));
Pavel Labath6ac84032017-02-27 11:05:34 +0000747 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
749 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
750 } else {
Pavel Labath6ac84032017-02-27 11:05:34 +0000751 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
753 offset, skip_prologue, internal, hardware);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 }
756
757 if (log)
758 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
759 "module=\"%s\") => SBBreakpoint(%p)",
760 static_cast<void *>(target_sp.get()), symbol_name, module_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000761 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762
763 return sb_bp;
764}
765
766lldb::SBBreakpoint
767SBTarget::BreakpointCreateByName(const char *symbol_name,
768 const SBFileSpecList &module_list,
769 const SBFileSpecList &comp_unit_list) {
770 uint32_t name_type_mask = eFunctionNameTypeAuto;
771 return BreakpointCreateByName(symbol_name, name_type_mask,
772 eLanguageTypeUnknown, module_list,
773 comp_unit_list);
774}
775
776lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
777 const char *symbol_name, uint32_t name_type_mask,
778 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
779 return BreakpointCreateByName(symbol_name, name_type_mask,
780 eLanguageTypeUnknown, module_list,
781 comp_unit_list);
782}
783
784lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
785 const char *symbol_name, uint32_t name_type_mask,
786 LanguageType symbol_language, const SBFileSpecList &module_list,
787 const SBFileSpecList &comp_unit_list) {
788 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
789
790 SBBreakpoint sb_bp;
791 TargetSP target_sp(GetSP());
792 if (target_sp && symbol_name && symbol_name[0]) {
793 const bool internal = false;
794 const bool hardware = false;
795 const LazyBool skip_prologue = eLazyBoolCalculate;
796 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +0000797 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask,
799 symbol_language, 0, skip_prologue, internal, hardware);
800 }
801
802 if (log)
803 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
804 "name_type: %d) => SBBreakpoint(%p)",
805 static_cast<void *>(target_sp.get()), symbol_name,
Pavel Labath6ac84032017-02-27 11:05:34 +0000806 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807
808 return sb_bp;
809}
810
811lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
812 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
813 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
814 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
815 eLanguageTypeUnknown, module_list,
816 comp_unit_list);
817}
818
819lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
820 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
821 LanguageType symbol_language, const SBFileSpecList &module_list,
822 const SBFileSpecList &comp_unit_list) {
823 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
824 eLanguageTypeUnknown, 0, module_list,
825 comp_unit_list);
826}
827
828lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
829 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
830 LanguageType symbol_language, lldb::addr_t offset,
831 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
832 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
833
834 SBBreakpoint sb_bp;
835 TargetSP target_sp(GetSP());
836 if (target_sp && num_names > 0) {
837 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
838 const bool internal = false;
839 const bool hardware = false;
840 const LazyBool skip_prologue = eLazyBoolCalculate;
Pavel Labath6ac84032017-02-27 11:05:34 +0000841 sb_bp = target_sp->CreateBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 module_list.get(), comp_unit_list.get(), symbol_names, num_names,
843 name_type_mask, symbol_language, offset, skip_prologue, internal,
844 hardware);
845 }
846
847 if (log) {
848 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
849 static_cast<void *>(target_sp.get()));
850 for (uint32_t i = 0; i < num_names; i++) {
851 char sep;
852 if (i < num_names - 1)
853 sep = ',';
854 else
855 sep = '}';
856 if (symbol_names[i] != NULL)
857 log->Printf("\"%s\"%c ", symbol_names[i], sep);
858 else
859 log->Printf("\"<NULL>\"%c ", sep);
860 }
861 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
Pavel Labath6ac84032017-02-27 11:05:34 +0000862 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 }
864
865 return sb_bp;
866}
867
868SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
869 const char *module_name) {
870 SBFileSpecList module_spec_list;
871 SBFileSpecList comp_unit_list;
872 if (module_name && module_name[0]) {
873 module_spec_list.Append(FileSpec(module_name, false));
874 }
875 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
876 module_spec_list, comp_unit_list);
877}
878
879lldb::SBBreakpoint
880SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
881 const SBFileSpecList &module_list,
882 const SBFileSpecList &comp_unit_list) {
883 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
884 module_list, comp_unit_list);
885}
886
887lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
888 const char *symbol_name_regex, LanguageType symbol_language,
889 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
890 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
891
892 SBBreakpoint sb_bp;
893 TargetSP target_sp(GetSP());
894 if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
895 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Zachary Turner95eae422016-09-21 16:01:28 +0000896 RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 const bool internal = false;
898 const bool hardware = false;
899 const LazyBool skip_prologue = eLazyBoolCalculate;
900
Pavel Labath6ac84032017-02-27 11:05:34 +0000901 sb_bp = target_sp->CreateFuncRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 module_list.get(), comp_unit_list.get(), regexp, symbol_language,
903 skip_prologue, internal, hardware);
904 }
905
906 if (log)
907 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
908 "=> SBBreakpoint(%p)",
909 static_cast<void *>(target_sp.get()), symbol_name_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +0000910 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911
912 return sb_bp;
913}
914
915SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
916 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
917
918 SBBreakpoint sb_bp;
919 TargetSP target_sp(GetSP());
920 if (target_sp) {
921 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
922 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000923 sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 }
925
926 if (log)
927 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
928 ") => SBBreakpoint(%p)",
929 static_cast<void *>(target_sp.get()),
930 static_cast<uint64_t>(address),
Pavel Labath6ac84032017-02-27 11:05:34 +0000931 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932
933 return sb_bp;
934}
935
936SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
937 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
938
939 SBBreakpoint sb_bp;
940 TargetSP target_sp(GetSP());
941 if (!sb_address.IsValid()) {
942 if (log)
943 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
944 "invalid address",
945 static_cast<void *>(target_sp.get()));
946 return sb_bp;
947 }
948
949 if (target_sp) {
950 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
951 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +0000952 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 }
954
955 if (log) {
956 SBStream s;
957 sb_address.GetDescription(s);
958 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
959 "SBBreakpoint(%p)",
960 static_cast<void *>(target_sp.get()), s.GetData(),
Pavel Labath6ac84032017-02-27 11:05:34 +0000961 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 }
963
964 return sb_bp;
965}
966
967lldb::SBBreakpoint
968SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
969 const lldb::SBFileSpec &source_file,
970 const char *module_name) {
971 SBFileSpecList module_spec_list;
972
973 if (module_name && module_name[0]) {
974 module_spec_list.Append(FileSpec(module_name, false));
975 }
976
977 SBFileSpecList source_file_list;
978 if (source_file.IsValid()) {
979 source_file_list.Append(source_file);
980 }
981
982 return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
983 source_file_list);
984}
985
986lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
987 const char *source_regex, const SBFileSpecList &module_list,
988 const lldb::SBFileSpecList &source_file_list) {
989 return BreakpointCreateBySourceRegex(source_regex, module_list,
990 source_file_list, SBStringList());
991}
992
993lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
994 const char *source_regex, const SBFileSpecList &module_list,
995 const lldb::SBFileSpecList &source_file_list,
996 const SBStringList &func_names) {
997 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
998
999 SBBreakpoint sb_bp;
1000 TargetSP target_sp(GetSP());
1001 if (target_sp && source_regex && source_regex[0]) {
1002 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1003 const bool hardware = false;
1004 const LazyBool move_to_nearest_code = eLazyBoolCalculate;
Zachary Turner95eae422016-09-21 16:01:28 +00001005 RegularExpression regexp((llvm::StringRef(source_regex)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 std::unordered_set<std::string> func_names_set;
1007 for (size_t i = 0; i < func_names.GetSize(); i++) {
1008 func_names_set.insert(func_names.GetStringAtIndex(i));
1009 }
1010
Pavel Labath6ac84032017-02-27 11:05:34 +00001011 sb_bp = target_sp->CreateSourceRegexBreakpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 module_list.get(), source_file_list.get(), func_names_set, regexp,
1013 false, hardware, move_to_nearest_code);
1014 }
1015
1016 if (log)
1017 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1018 "=> SBBreakpoint(%p)",
1019 static_cast<void *>(target_sp.get()), source_regex,
Pavel Labath6ac84032017-02-27 11:05:34 +00001020 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021
1022 return sb_bp;
1023}
1024
1025lldb::SBBreakpoint
1026SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1027 bool catch_bp, bool throw_bp) {
1028 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1029
1030 SBBreakpoint sb_bp;
1031 TargetSP target_sp(GetSP());
1032 if (target_sp) {
1033 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1034 const bool hardware = false;
Pavel Labath6ac84032017-02-27 11:05:34 +00001035 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 hardware);
1037 }
1038
1039 if (log)
1040 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: "
1041 "%s throw: %s) => SBBreakpoint(%p)",
1042 static_cast<void *>(target_sp.get()),
1043 Language::GetNameForLanguageType(language),
1044 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
Pavel Labath6ac84032017-02-27 11:05:34 +00001045 static_cast<void *>(sb_bp.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046
1047 return sb_bp;
1048}
1049
1050uint32_t SBTarget::GetNumBreakpoints() const {
1051 TargetSP target_sp(GetSP());
1052 if (target_sp) {
1053 // The breakpoint list is thread safe, no need to lock
1054 return target_sp->GetBreakpointList().GetSize();
1055 }
1056 return 0;
1057}
1058
1059SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1060 SBBreakpoint sb_breakpoint;
1061 TargetSP target_sp(GetSP());
1062 if (target_sp) {
1063 // The breakpoint list is thread safe, no need to lock
Pavel Labath6ac84032017-02-27 11:05:34 +00001064 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 }
1066 return sb_breakpoint;
1067}
1068
1069bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1070 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1071
1072 bool result = false;
1073 TargetSP target_sp(GetSP());
1074 if (target_sp) {
1075 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1076 result = target_sp->RemoveBreakpointByID(bp_id);
1077 }
1078
1079 if (log)
1080 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1081 static_cast<void *>(target_sp.get()),
1082 static_cast<uint32_t>(bp_id), result);
1083
1084 return result;
1085}
1086
1087SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1088 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1089
1090 SBBreakpoint sb_breakpoint;
1091 TargetSP target_sp(GetSP());
1092 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1093 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Pavel Labath6ac84032017-02-27 11:05:34 +00001094 sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 }
1096
1097 if (log)
1098 log->Printf(
1099 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1100 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
Pavel Labath6ac84032017-02-27 11:05:34 +00001101 static_cast<void *>(sb_breakpoint.GetSP().get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102
1103 return sb_breakpoint;
1104}
1105
Jim Inghamff9a91e2016-09-21 01:21:19 +00001106bool SBTarget::FindBreakpointsByName(const char *name,
1107 SBBreakpointList &bkpts) {
1108 TargetSP target_sp(GetSP());
1109 if (target_sp) {
1110 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1111 BreakpointList bkpt_list(false);
1112 bool is_valid =
1113 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1114 if (!is_valid)
1115 return false;
1116 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1117 bkpts.AppendByID(bkpt_sp->GetID());
1118 }
1119 }
1120 return true;
1121}
1122
Jim Inghamb842f2e2017-09-14 20:22:49 +00001123void SBTarget::GetBreakpointNames(SBStringList &names)
1124{
1125 names.Clear();
1126
1127 TargetSP target_sp(GetSP());
1128 if (target_sp) {
1129 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1130
1131 std::vector<std::string> name_vec;
1132 target_sp->GetBreakpointNames(name_vec);
1133 for (auto name : name_vec)
1134 names.AppendString(name.c_str());
1135 }
1136}
1137
1138void SBTarget::DeleteBreakpointName(const char *name)
1139{
1140 TargetSP target_sp(GetSP());
1141 if (target_sp) {
1142 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001143 target_sp->DeleteBreakpointName(ConstString(name));
Jim Inghamb842f2e2017-09-14 20:22:49 +00001144 }
1145}
1146
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147bool SBTarget::EnableAllBreakpoints() {
1148 TargetSP target_sp(GetSP());
1149 if (target_sp) {
1150 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001151 target_sp->EnableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 return true;
1153 }
1154 return false;
1155}
1156
1157bool SBTarget::DisableAllBreakpoints() {
1158 TargetSP target_sp(GetSP());
1159 if (target_sp) {
1160 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001161 target_sp->DisableAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162 return true;
1163 }
1164 return false;
1165}
1166
1167bool SBTarget::DeleteAllBreakpoints() {
1168 TargetSP target_sp(GetSP());
1169 if (target_sp) {
1170 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jim Inghamb842f2e2017-09-14 20:22:49 +00001171 target_sp->RemoveAllowedBreakpoints();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 return true;
1173 }
1174 return false;
1175}
1176
Jim Ingham01f16662016-09-14 19:07:35 +00001177lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1178 SBBreakpointList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001179 SBStringList empty_name_list;
1180 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
1181}
1182
1183lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1184 SBStringList &matching_names,
1185 SBBreakpointList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001186 SBError sberr;
1187 TargetSP target_sp(GetSP());
1188 if (!target_sp) {
1189 sberr.SetErrorString(
1190 "BreakpointCreateFromFile called with invalid target.");
1191 return sberr;
1192 }
1193 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1194
1195 BreakpointIDList bp_ids;
Jim Ingham3acdf382016-09-22 22:20:28 +00001196
1197 std::vector<std::string> name_vector;
1198 size_t num_names = matching_names.GetSize();
1199 for (size_t i = 0; i < num_names; i++)
1200 name_vector.push_back(matching_names.GetStringAtIndex(i));
1201
1202 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1203 name_vector, bp_ids);
Jim Ingham01f16662016-09-14 19:07:35 +00001204 if (sberr.Fail())
1205 return sberr;
1206
1207 size_t num_bkpts = bp_ids.GetSize();
1208 for (size_t i = 0; i < num_bkpts; i++) {
1209 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1210 new_bps.AppendByID(bp_id.GetBreakpointID());
1211 }
1212 return sberr;
1213}
1214
1215lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1216 SBError sberr;
1217 TargetSP target_sp(GetSP());
1218 if (!target_sp) {
1219 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1220 return sberr;
1221 }
1222 SBBreakpointList bkpt_list(*this);
1223 return BreakpointsWriteToFile(dest_file, bkpt_list);
1224}
1225
1226lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
Jim Ingham2d3628e2016-09-22 23:42:42 +00001227 SBBreakpointList &bkpt_list,
1228 bool append) {
Jim Ingham01f16662016-09-14 19:07:35 +00001229 SBError sberr;
1230 TargetSP target_sp(GetSP());
1231 if (!target_sp) {
1232 sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1233 return sberr;
1234 }
1235
1236 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1237 BreakpointIDList bp_id_list;
1238 bkpt_list.CopyToBreakpointIDList(bp_id_list);
Jim Ingham2d3628e2016-09-22 23:42:42 +00001239 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1240 bp_id_list, append);
Jim Ingham01f16662016-09-14 19:07:35 +00001241 return sberr;
1242}
1243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244uint32_t SBTarget::GetNumWatchpoints() const {
1245 TargetSP target_sp(GetSP());
1246 if (target_sp) {
1247 // The watchpoint list is thread safe, no need to lock
1248 return target_sp->GetWatchpointList().GetSize();
1249 }
1250 return 0;
1251}
1252
1253SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1254 SBWatchpoint sb_watchpoint;
1255 TargetSP target_sp(GetSP());
1256 if (target_sp) {
1257 // The watchpoint list is thread safe, no need to lock
1258 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1259 }
1260 return sb_watchpoint;
1261}
1262
1263bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1264 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1265
1266 bool result = false;
1267 TargetSP target_sp(GetSP());
1268 if (target_sp) {
1269 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1270 std::unique_lock<std::recursive_mutex> lock;
1271 target_sp->GetWatchpointList().GetListMutex(lock);
1272 result = target_sp->RemoveWatchpointByID(wp_id);
1273 }
1274
1275 if (log)
1276 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1277 static_cast<void *>(target_sp.get()),
1278 static_cast<uint32_t>(wp_id), result);
1279
1280 return result;
1281}
1282
1283SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1284 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1285
1286 SBWatchpoint sb_watchpoint;
1287 lldb::WatchpointSP watchpoint_sp;
1288 TargetSP target_sp(GetSP());
1289 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1290 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1291 std::unique_lock<std::recursive_mutex> lock;
1292 target_sp->GetWatchpointList().GetListMutex(lock);
1293 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1294 sb_watchpoint.SetSP(watchpoint_sp);
1295 }
1296
1297 if (log)
1298 log->Printf(
1299 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1300 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1301 static_cast<void *>(watchpoint_sp.get()));
1302
1303 return sb_watchpoint;
1304}
1305
1306lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1307 bool read, bool write,
1308 SBError &error) {
1309 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1310
1311 SBWatchpoint sb_watchpoint;
1312 lldb::WatchpointSP watchpoint_sp;
1313 TargetSP target_sp(GetSP());
1314 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1315 size > 0) {
1316 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1317 uint32_t watch_type = 0;
1318 if (read)
1319 watch_type |= LLDB_WATCH_TYPE_READ;
1320 if (write)
1321 watch_type |= LLDB_WATCH_TYPE_WRITE;
1322 if (watch_type == 0) {
1323 error.SetErrorString(
1324 "Can't create a watchpoint that is neither read nor write.");
1325 return sb_watchpoint;
1326 }
1327
1328 // Target::CreateWatchpoint() is thread safe.
Zachary Turner97206d52017-05-12 04:51:55 +00001329 Status cw_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 // This API doesn't take in a type, so we can't figure out what it is.
1331 CompilerType *type = NULL;
1332 watchpoint_sp =
1333 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1334 error.SetError(cw_error);
1335 sb_watchpoint.SetSP(watchpoint_sp);
1336 }
1337
1338 if (log)
1339 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1340 ", 0x%u) => SBWatchpoint(%p)",
1341 static_cast<void *>(target_sp.get()), addr,
1342 static_cast<uint32_t>(size),
1343 static_cast<void *>(watchpoint_sp.get()));
1344
1345 return sb_watchpoint;
1346}
1347
1348bool SBTarget::EnableAllWatchpoints() {
1349 TargetSP target_sp(GetSP());
1350 if (target_sp) {
1351 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1352 std::unique_lock<std::recursive_mutex> lock;
1353 target_sp->GetWatchpointList().GetListMutex(lock);
1354 target_sp->EnableAllWatchpoints();
1355 return true;
1356 }
1357 return false;
1358}
1359
1360bool SBTarget::DisableAllWatchpoints() {
1361 TargetSP target_sp(GetSP());
1362 if (target_sp) {
1363 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1364 std::unique_lock<std::recursive_mutex> lock;
1365 target_sp->GetWatchpointList().GetListMutex(lock);
1366 target_sp->DisableAllWatchpoints();
1367 return true;
1368 }
1369 return false;
1370}
1371
1372SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1373 SBType type) {
1374 SBValue sb_value;
1375 lldb::ValueObjectSP new_value_sp;
1376 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1377 lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1378 ExecutionContext exe_ctx(
1379 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1380 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1381 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1382 exe_ctx, ast_type);
1383 }
1384 sb_value.SetSP(new_value_sp);
1385 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1386 if (log) {
1387 if (new_value_sp)
1388 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1389 static_cast<void *>(m_opaque_sp.get()),
1390 new_value_sp->GetName().AsCString());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001391 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1393 static_cast<void *>(m_opaque_sp.get()));
1394 }
1395 return sb_value;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001396}
1397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1399 lldb::SBType type) {
1400 SBValue sb_value;
1401 lldb::ValueObjectSP new_value_sp;
1402 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1403 DataExtractorSP extractor(*data);
1404 ExecutionContext exe_ctx(
1405 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1406 CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1407 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1408 exe_ctx, ast_type);
1409 }
1410 sb_value.SetSP(new_value_sp);
1411 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1412 if (log) {
1413 if (new_value_sp)
1414 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1415 static_cast<void *>(m_opaque_sp.get()),
1416 new_value_sp->GetName().AsCString());
1417 else
1418 log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1419 static_cast<void *>(m_opaque_sp.get()));
1420 }
1421 return sb_value;
Greg Claytone14e1922012-12-04 02:22:16 +00001422}
1423
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1425 const char *expr) {
1426 SBValue sb_value;
1427 lldb::ValueObjectSP new_value_sp;
1428 if (IsValid() && name && *name && expr && *expr) {
1429 ExecutionContext exe_ctx(
1430 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1431 new_value_sp =
1432 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1433 }
1434 sb_value.SetSP(new_value_sp);
1435 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1436 if (log) {
1437 if (new_value_sp)
1438 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1439 static_cast<void *>(m_opaque_sp.get()),
1440 new_value_sp->GetName().AsCString());
1441 else
1442 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1443 static_cast<void *>(m_opaque_sp.get()));
1444 }
1445 return sb_value;
1446}
1447
1448bool SBTarget::DeleteAllWatchpoints() {
1449 TargetSP target_sp(GetSP());
1450 if (target_sp) {
1451 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1452 std::unique_lock<std::recursive_mutex> lock;
1453 target_sp->GetWatchpointList().GetListMutex(lock);
1454 target_sp->RemoveAllWatchpoints();
1455 return true;
1456 }
1457 return false;
1458}
1459
Alexander Polyakov3e7b9db2018-08-07 20:23:57 +00001460void SBTarget::AppendImageSearchPath(const char *from, const char *to,
1461 lldb::SBError &error) {
1462 TargetSP target_sp(GetSP());
1463 if (!target_sp)
1464 return error.SetErrorString("invalid target");
1465
1466 const ConstString csFrom(from), csTo(to);
1467 if (!csFrom)
1468 return error.SetErrorString("<from> path can't be empty");
1469 if (!csTo)
1470 return error.SetErrorString("<to> path can't be empty");
1471
1472 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1473 if (log)
1474 log->Printf("SBTarget(%p)::%s: '%s' -> '%s'",
1475 static_cast<void *>(target_sp.get()), __FUNCTION__,
1476 from, to);
1477 target_sp->GetImageSearchPathList().Append(csFrom, csTo, true);
1478}
1479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1481 const char *uuid_cstr) {
1482 return AddModule(path, triple, uuid_cstr, NULL);
1483}
1484
1485lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1486 const char *uuid_cstr, const char *symfile) {
1487 lldb::SBModule sb_module;
1488 TargetSP target_sp(GetSP());
1489 if (target_sp) {
1490 ModuleSpec module_spec;
1491 if (path)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001492 module_spec.GetFileSpec().SetFile(path, false, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493
1494 if (uuid_cstr)
Pavel Labatha174bcb2018-06-21 15:24:39 +00001495 module_spec.GetUUID().SetFromStringRef(uuid_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496
1497 if (triple)
Pavel Labath7263f1b2017-10-31 10:56:03 +00001498 module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1499 target_sp->GetPlatform().get(), triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 else
1501 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1502
1503 if (symfile)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001504 module_spec.GetSymbolFileSpec().SetFile(symfile, false,
1505 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506
1507 sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1508 }
1509 return sb_module;
1510}
1511
1512lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1513 lldb::SBModule sb_module;
1514 TargetSP target_sp(GetSP());
1515 if (target_sp)
1516 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap));
1517 return sb_module;
1518}
1519
1520bool SBTarget::AddModule(lldb::SBModule &module) {
1521 TargetSP target_sp(GetSP());
1522 if (target_sp) {
1523 target_sp->GetImages().AppendIfNeeded(module.GetSP());
1524 return true;
1525 }
1526 return false;
1527}
1528
1529uint32_t SBTarget::GetNumModules() const {
1530 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1531
1532 uint32_t num = 0;
1533 TargetSP target_sp(GetSP());
1534 if (target_sp) {
1535 // The module list is thread safe, no need to lock
1536 num = target_sp->GetImages().GetSize();
1537 }
1538
1539 if (log)
1540 log->Printf("SBTarget(%p)::GetNumModules () => %d",
1541 static_cast<void *>(target_sp.get()), num);
1542
1543 return num;
1544}
1545
1546void SBTarget::Clear() {
1547 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1548
1549 if (log)
1550 log->Printf("SBTarget(%p)::Clear ()",
1551 static_cast<void *>(m_opaque_sp.get()));
1552
1553 m_opaque_sp.reset();
1554}
1555
1556SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1557 SBModule sb_module;
1558 TargetSP target_sp(GetSP());
1559 if (target_sp && sb_file_spec.IsValid()) {
1560 ModuleSpec module_spec(*sb_file_spec);
1561 // The module list is thread safe, no need to lock
1562 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1563 }
1564 return sb_module;
1565}
1566
Alexander Polyakovda0c0812018-07-03 14:22:44 +00001567SBSymbolContextList
1568SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
1569 SBSymbolContextList sb_sc_list;
1570 const TargetSP target_sp(GetSP());
1571 if (target_sp && sb_file_spec.IsValid()) {
1572 const bool append = true;
1573 target_sp->GetImages().FindCompileUnits(*sb_file_spec,
1574 append, *sb_sc_list);
1575 }
1576 return sb_sc_list;
1577}
1578
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579lldb::ByteOrder SBTarget::GetByteOrder() {
1580 TargetSP target_sp(GetSP());
1581 if (target_sp)
1582 return target_sp->GetArchitecture().GetByteOrder();
1583 return eByteOrderInvalid;
1584}
1585
1586const char *SBTarget::GetTriple() {
1587 TargetSP target_sp(GetSP());
1588 if (target_sp) {
1589 std::string triple(target_sp->GetArchitecture().GetTriple().str());
Adrian Prantl05097242018-04-30 16:49:04 +00001590 // Unique the string so we don't run into ownership issues since the const
1591 // strings put the string into the string pool once and the strings never
1592 // comes out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 ConstString const_triple(triple.c_str());
1594 return const_triple.GetCString();
1595 }
1596 return NULL;
1597}
1598
1599uint32_t SBTarget::GetDataByteSize() {
1600 TargetSP target_sp(GetSP());
1601 if (target_sp) {
1602 return target_sp->GetArchitecture().GetDataByteSize();
1603 }
1604 return 0;
1605}
1606
1607uint32_t SBTarget::GetCodeByteSize() {
1608 TargetSP target_sp(GetSP());
1609 if (target_sp) {
1610 return target_sp->GetArchitecture().GetCodeByteSize();
1611 }
1612 return 0;
1613}
1614
1615uint32_t SBTarget::GetAddressByteSize() {
1616 TargetSP target_sp(GetSP());
1617 if (target_sp)
1618 return target_sp->GetArchitecture().GetAddressByteSize();
1619 return sizeof(void *);
1620}
1621
1622SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1623 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1624
1625 SBModule sb_module;
1626 ModuleSP module_sp;
1627 TargetSP target_sp(GetSP());
1628 if (target_sp) {
1629 // The module list is thread safe, no need to lock
1630 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1631 sb_module.SetSP(module_sp);
1632 }
1633
1634 if (log)
1635 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1636 static_cast<void *>(target_sp.get()), idx,
1637 static_cast<void *>(module_sp.get()));
1638
1639 return sb_module;
1640}
1641
1642bool SBTarget::RemoveModule(lldb::SBModule module) {
1643 TargetSP target_sp(GetSP());
1644 if (target_sp)
1645 return target_sp->GetImages().Remove(module.GetSP());
1646 return false;
1647}
1648
1649SBBroadcaster SBTarget::GetBroadcaster() const {
1650 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1651
1652 TargetSP target_sp(GetSP());
1653 SBBroadcaster broadcaster(target_sp.get(), false);
1654
1655 if (log)
1656 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1657 static_cast<void *>(target_sp.get()),
1658 static_cast<void *>(broadcaster.get()));
1659
1660 return broadcaster;
1661}
1662
1663bool SBTarget::GetDescription(SBStream &description,
1664 lldb::DescriptionLevel description_level) {
1665 Stream &strm = description.ref();
1666
1667 TargetSP target_sp(GetSP());
1668 if (target_sp) {
1669 target_sp->Dump(&strm, description_level);
1670 } else
1671 strm.PutCString("No value");
1672
1673 return true;
1674}
1675
1676lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
1677 uint32_t name_type_mask) {
1678 lldb::SBSymbolContextList sb_sc_list;
1679 if (name && name[0]) {
Jim Ingham763b2b22015-07-07 22:12:17 +00001680 TargetSP target_sp(GetSP());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681 if (target_sp) {
1682 const bool symbols_ok = true;
1683 const bool inlines_ok = true;
1684 const bool append = true;
1685 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask,
1686 symbols_ok, inlines_ok, append,
1687 *sb_sc_list);
1688 }
1689 }
1690 return sb_sc_list;
Jim Ingham763b2b22015-07-07 22:12:17 +00001691}
Greg Claytone14e1922012-12-04 02:22:16 +00001692
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
1694 uint32_t max_matches,
1695 MatchType matchtype) {
1696 lldb::SBSymbolContextList sb_sc_list;
1697 if (name && name[0]) {
Zachary Turner95eae422016-09-21 16:01:28 +00001698 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699 TargetSP target_sp(GetSP());
1700 if (target_sp) {
1701 std::string regexstr;
1702 switch (matchtype) {
1703 case eMatchTypeRegex:
Zachary Turner95eae422016-09-21 16:01:28 +00001704 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705 true, true, *sb_sc_list);
1706 break;
1707 case eMatchTypeStartsWith:
1708 regexstr = llvm::Regex::escape(name) + ".*";
Zachary Turner95eae422016-09-21 16:01:28 +00001709 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
1710 true, true, *sb_sc_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711 break;
1712 default:
1713 target_sp->GetImages().FindFunctions(ConstString(name),
1714 eFunctionNameTypeAny, true, true,
1715 true, *sb_sc_list);
1716 break;
1717 }
1718 }
1719 }
1720 return sb_sc_list;
1721}
1722
1723lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
1724 TargetSP target_sp(GetSP());
1725 if (typename_cstr && typename_cstr[0] && target_sp) {
1726 ConstString const_typename(typename_cstr);
1727 SymbolContext sc;
1728 const bool exact_match = false;
1729
1730 const ModuleList &module_list = target_sp->GetImages();
1731 size_t count = module_list.GetSize();
1732 for (size_t idx = 0; idx < count; idx++) {
1733 ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1734 if (module_sp) {
1735 TypeSP type_sp(
1736 module_sp->FindFirstType(sc, const_typename, exact_match));
1737 if (type_sp)
1738 return SBType(type_sp);
1739 }
1740 }
1741
Adrian Prantl05097242018-04-30 16:49:04 +00001742 // Didn't find the type in the symbols; try the Objective-C runtime if one
1743 // is installed
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744
1745 ProcessSP process_sp(target_sp->GetProcessSP());
1746
1747 if (process_sp) {
1748 ObjCLanguageRuntime *objc_language_runtime =
1749 process_sp->GetObjCLanguageRuntime();
1750
1751 if (objc_language_runtime) {
1752 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1753
1754 if (objc_decl_vendor) {
1755 std::vector<clang::NamedDecl *> decls;
1756
1757 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1758 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
1759 return SBType(type);
1760 }
1761 }
1762 }
1763 }
1764 }
1765
1766 // No matches, search for basic typename matches
1767 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1768 if (clang_ast)
1769 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(),
1770 const_typename));
1771 }
1772 return SBType();
1773}
1774
1775SBType SBTarget::GetBasicType(lldb::BasicType type) {
1776 TargetSP target_sp(GetSP());
1777 if (target_sp) {
1778 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1779 if (clang_ast)
1780 return SBType(
1781 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type));
1782 }
1783 return SBType();
1784}
1785
1786lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
1787 SBTypeList sb_type_list;
1788 TargetSP target_sp(GetSP());
1789 if (typename_cstr && typename_cstr[0] && target_sp) {
1790 ModuleList &images = target_sp->GetImages();
1791 ConstString const_typename(typename_cstr);
1792 bool exact_match = false;
1793 SymbolContext sc;
1794 TypeList type_list;
1795 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1796 uint32_t num_matches =
1797 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX,
1798 searched_symbol_files, type_list);
1799
1800 if (num_matches > 0) {
1801 for (size_t idx = 0; idx < num_matches; idx++) {
1802 TypeSP type_sp(type_list.GetTypeAtIndex(idx));
1803 if (type_sp)
1804 sb_type_list.Append(SBType(type_sp));
1805 }
1806 }
1807
1808 // Try the Objective-C runtime if one is installed
1809
1810 ProcessSP process_sp(target_sp->GetProcessSP());
1811
1812 if (process_sp) {
1813 ObjCLanguageRuntime *objc_language_runtime =
1814 process_sp->GetObjCLanguageRuntime();
1815
1816 if (objc_language_runtime) {
1817 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1818
1819 if (objc_decl_vendor) {
1820 std::vector<clang::NamedDecl *> decls;
1821
1822 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
1823 for (clang::NamedDecl *decl : decls) {
1824 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
1825 sb_type_list.Append(SBType(type));
1826 }
1827 }
1828 }
1829 }
1830 }
1831 }
1832
1833 if (sb_type_list.GetSize() == 0) {
1834 // No matches, search for basic typename matches
1835 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1836 if (clang_ast)
1837 sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
1838 clang_ast->getASTContext(), const_typename)));
1839 }
1840 }
1841 return sb_type_list;
1842}
1843
1844SBValueList SBTarget::FindGlobalVariables(const char *name,
1845 uint32_t max_matches) {
1846 SBValueList sb_value_list;
1847
1848 TargetSP target_sp(GetSP());
1849 if (name && target_sp) {
1850 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001852 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853
1854 if (match_count > 0) {
1855 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1856 if (exe_scope == NULL)
1857 exe_scope = target_sp.get();
1858 for (uint32_t i = 0; i < match_count; ++i) {
1859 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1860 exe_scope, variable_list.GetVariableAtIndex(i)));
1861 if (valobj_sp)
1862 sb_value_list.Append(SBValue(valobj_sp));
1863 }
1864 }
1865 }
1866
1867 return sb_value_list;
1868}
1869
1870SBValueList SBTarget::FindGlobalVariables(const char *name,
1871 uint32_t max_matches,
1872 MatchType matchtype) {
1873 SBValueList sb_value_list;
1874
1875 TargetSP target_sp(GetSP());
1876 if (name && target_sp) {
Zachary Turner95eae422016-09-21 16:01:28 +00001877 llvm::StringRef name_ref(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878 VariableList variable_list;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879
1880 std::string regexstr;
1881 uint32_t match_count;
1882 switch (matchtype) {
1883 case eMatchTypeNormal:
1884 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001885 ConstString(name), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886 break;
1887 case eMatchTypeRegex:
1888 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001889 RegularExpression(name_ref), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 break;
1891 case eMatchTypeStartsWith:
1892 regexstr = llvm::Regex::escape(name) + ".*";
1893 match_count = target_sp->GetImages().FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +00001894 RegularExpression(regexstr), max_matches, variable_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895 break;
1896 }
1897
1898 if (match_count > 0) {
1899 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1900 if (exe_scope == NULL)
1901 exe_scope = target_sp.get();
1902 for (uint32_t i = 0; i < match_count; ++i) {
1903 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
1904 exe_scope, variable_list.GetVariableAtIndex(i)));
1905 if (valobj_sp)
1906 sb_value_list.Append(SBValue(valobj_sp));
1907 }
1908 }
1909 }
1910
1911 return sb_value_list;
1912}
1913
1914lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
1915 SBValueList sb_value_list(FindGlobalVariables(name, 1));
1916 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1917 return sb_value_list.GetValueAtIndex(0);
1918 return SBValue();
1919}
1920
1921SBSourceManager SBTarget::GetSourceManager() {
1922 SBSourceManager source_manager(*this);
1923 return source_manager;
1924}
1925
1926lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1927 uint32_t count) {
1928 return ReadInstructions(base_addr, count, NULL);
1929}
1930
1931lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
1932 uint32_t count,
1933 const char *flavor_string) {
1934 SBInstructionList sb_instructions;
1935
1936 TargetSP target_sp(GetSP());
1937 if (target_sp) {
1938 Address *addr_ptr = base_addr.get();
1939
1940 if (addr_ptr) {
1941 DataBufferHeap data(
1942 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1943 bool prefer_file_cache = false;
Zachary Turner97206d52017-05-12 04:51:55 +00001944 lldb_private::Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001945 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1946 const size_t bytes_read =
1947 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
1948 data.GetByteSize(), error, &load_addr);
1949 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1950 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1951 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
1952 data.GetBytes(), bytes_read, count, data_from_file));
1953 }
1954 }
1955
1956 return sb_instructions;
1957}
1958
1959lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
1960 const void *buf,
1961 size_t size) {
1962 return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
1963}
1964
1965lldb::SBInstructionList
1966SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
1967 const char *flavor_string, const void *buf,
1968 size_t size) {
1969 SBInstructionList sb_instructions;
1970
1971 TargetSP target_sp(GetSP());
1972 if (target_sp) {
1973 Address addr;
1974
1975 if (base_addr.get())
1976 addr = *base_addr.get();
1977
1978 const bool data_from_file = true;
1979
1980 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1981 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
1982 UINT32_MAX, data_from_file));
1983 }
1984
1985 return sb_instructions;
1986}
1987
1988lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
1989 const void *buf,
1990 size_t size) {
1991 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
1992 size);
1993}
1994
1995lldb::SBInstructionList
1996SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
1997 const char *flavor_string, const void *buf,
1998 size_t size) {
1999 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
2000 buf, size);
2001}
2002
2003SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
2004 lldb::addr_t section_base_addr) {
2005 SBError sb_error;
2006 TargetSP target_sp(GetSP());
2007 if (target_sp) {
2008 if (!section.IsValid()) {
2009 sb_error.SetErrorStringWithFormat("invalid section");
2010 } else {
2011 SectionSP section_sp(section.GetSP());
2012 if (section_sp) {
2013 if (section_sp->IsThreadSpecific()) {
2014 sb_error.SetErrorString(
2015 "thread specific sections are not yet supported");
2016 } else {
2017 ProcessSP process_sp(target_sp->GetProcessSP());
2018 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
2019 ModuleSP module_sp(section_sp->GetModule());
2020 if (module_sp) {
2021 ModuleList module_list;
2022 module_list.Append(module_sp);
2023 target_sp->ModulesDidLoad(module_list);
2024 }
2025 // Flush info in the process (stack frames, etc)
2026 if (process_sp)
2027 process_sp->Flush();
2028 }
2029 }
2030 }
2031 }
2032 } else {
2033 sb_error.SetErrorString("invalid target");
2034 }
2035 return sb_error;
2036}
2037
2038SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
2039 SBError sb_error;
2040
2041 TargetSP target_sp(GetSP());
2042 if (target_sp) {
2043 if (!section.IsValid()) {
2044 sb_error.SetErrorStringWithFormat("invalid section");
2045 } else {
2046 SectionSP section_sp(section.GetSP());
2047 if (section_sp) {
2048 ProcessSP process_sp(target_sp->GetProcessSP());
2049 if (target_sp->SetSectionUnloaded(section_sp)) {
2050 ModuleSP module_sp(section_sp->GetModule());
2051 if (module_sp) {
2052 ModuleList module_list;
2053 module_list.Append(module_sp);
2054 target_sp->ModulesDidUnload(module_list, false);
2055 }
2056 // Flush info in the process (stack frames, etc)
2057 if (process_sp)
2058 process_sp->Flush();
2059 }
2060 } else {
2061 sb_error.SetErrorStringWithFormat("invalid section");
2062 }
2063 }
2064 } else {
2065 sb_error.SetErrorStringWithFormat("invalid target");
2066 }
2067 return sb_error;
2068}
2069
2070SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2071 int64_t slide_offset) {
2072 SBError sb_error;
2073
2074 TargetSP target_sp(GetSP());
2075 if (target_sp) {
2076 ModuleSP module_sp(module.GetSP());
2077 if (module_sp) {
2078 bool changed = false;
2079 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2080 // The load was successful, make sure that at least some sections
2081 // changed before we notify that our module was loaded.
2082 if (changed) {
2083 ModuleList module_list;
2084 module_list.Append(module_sp);
2085 target_sp->ModulesDidLoad(module_list);
2086 // Flush info in the process (stack frames, etc)
2087 ProcessSP process_sp(target_sp->GetProcessSP());
2088 if (process_sp)
2089 process_sp->Flush();
2090 }
2091 }
2092 } else {
2093 sb_error.SetErrorStringWithFormat("invalid module");
2094 }
2095
2096 } else {
2097 sb_error.SetErrorStringWithFormat("invalid target");
2098 }
2099 return sb_error;
2100}
2101
2102SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2103 SBError sb_error;
2104
2105 char path[PATH_MAX];
2106 TargetSP target_sp(GetSP());
2107 if (target_sp) {
2108 ModuleSP module_sp(module.GetSP());
2109 if (module_sp) {
2110 ObjectFile *objfile = module_sp->GetObjectFile();
2111 if (objfile) {
2112 SectionList *section_list = objfile->GetSectionList();
2113 if (section_list) {
2114 ProcessSP process_sp(target_sp->GetProcessSP());
2115
2116 bool changed = false;
2117 const size_t num_sections = section_list->GetSize();
2118 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2119 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2120 if (section_sp)
2121 changed |= target_sp->SetSectionUnloaded(section_sp);
2122 }
2123 if (changed) {
2124 ModuleList module_list;
2125 module_list.Append(module_sp);
2126 target_sp->ModulesDidUnload(module_list, false);
2127 // Flush info in the process (stack frames, etc)
2128 ProcessSP process_sp(target_sp->GetProcessSP());
2129 if (process_sp)
2130 process_sp->Flush();
2131 }
2132 } else {
2133 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2134 sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2135 path);
2136 }
2137 } else {
2138 module_sp->GetFileSpec().GetPath(path, sizeof(path));
2139 sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2140 path);
2141 }
2142 } else {
2143 sb_error.SetErrorStringWithFormat("invalid module");
2144 }
2145 } else {
2146 sb_error.SetErrorStringWithFormat("invalid target");
2147 }
2148 return sb_error;
2149}
2150
2151lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2152 lldb::SymbolType symbol_type) {
2153 SBSymbolContextList sb_sc_list;
2154 if (name && name[0]) {
2155 TargetSP target_sp(GetSP());
2156 if (target_sp) {
2157 bool append = true;
2158 target_sp->GetImages().FindSymbolsWithNameAndType(
2159 ConstString(name), symbol_type, *sb_sc_list, append);
2160 }
2161 }
2162 return sb_sc_list;
2163}
2164
2165lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2166 TargetSP target_sp(GetSP());
2167 if (!target_sp)
2168 return SBValue();
2169
2170 SBExpressionOptions options;
2171 lldb::DynamicValueType fetch_dynamic_value =
2172 target_sp->GetPreferDynamicValue();
2173 options.SetFetchDynamicValue(fetch_dynamic_value);
2174 options.SetUnwindOnError(true);
2175 return EvaluateExpression(expr, options);
2176}
2177
2178lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2179 const SBExpressionOptions &options) {
2180 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002181#if !defined(LLDB_DISABLE_PYTHON)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool6010f972016-04-22 23:08:34 +00002183#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002184 SBValue expr_result;
2185 ExpressionResults exe_results = eExpressionSetupError;
2186 ValueObjectSP expr_value_sp;
2187 TargetSP target_sp(GetSP());
2188 StackFrame *frame = NULL;
2189 if (target_sp) {
2190 if (expr == NULL || expr[0] == '\0') {
2191 if (log)
2192 log->Printf(
2193 "SBTarget::EvaluateExpression called with an empty expression");
2194 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002195 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002196
2197 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2198 ExecutionContext exe_ctx(m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002199
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002200 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2202
2203 frame = exe_ctx.GetFramePtr();
2204 Target *target = exe_ctx.GetTargetPtr();
2205
2206 if (target) {
2207#ifdef LLDB_CONFIGURATION_DEBUG
2208 StreamString frame_description;
2209 if (frame)
2210 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00002211 llvm::PrettyStackTraceFormat stack_trace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2213 "%u) %s",
2214 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00002215 frame_description.GetString().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216#endif
2217 exe_results =
2218 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2219
2220 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221 } else {
2222 if (log)
2223 log->Printf("SBTarget::EvaluateExpression () => error: could not "
2224 "reconstruct frame object for this SBTarget.");
2225 }
2226 }
2227#ifndef LLDB_DISABLE_PYTHON
2228 if (expr_log)
2229 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2230 "%s, summary %s **",
2231 expr_result.GetValue(), expr_result.GetSummary());
2232
2233 if (log)
2234 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2235 "(execution result=%d)",
2236 static_cast<void *>(frame), expr,
2237 static_cast<void *>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002238#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002239
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 return expr_result;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002241}
2242
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243lldb::addr_t SBTarget::GetStackRedZoneSize() {
2244 TargetSP target_sp(GetSP());
2245 if (target_sp) {
2246 ABISP abi_sp;
2247 ProcessSP process_sp(target_sp->GetProcessSP());
2248 if (process_sp)
2249 abi_sp = process_sp->GetABI();
2250 else
Jason Molenda43294c92017-06-29 02:57:03 +00002251 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 if (abi_sp)
2253 return abi_sp->GetRedZoneSize();
2254 }
2255 return 0;
Greg Clayton13fbb992013-02-01 00:47:49 +00002256}
Ilia K8f37ca52015-02-13 14:31:06 +00002257
Kate Stoneb9c1b512016-09-06 20:57:50 +00002258lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2259 lldb::SBLaunchInfo launch_info(NULL);
2260 TargetSP target_sp(GetSP());
2261 if (target_sp)
Pavel Labath62930e52018-01-10 11:57:31 +00002262 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002263 return launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00002264}
2265
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2267 TargetSP target_sp(GetSP());
2268 if (target_sp)
2269 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
Ilia K8f37ca52015-02-13 14:31:06 +00002270}