Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Alex Langford | d17cd90 | 2018-05-25 20:28:16 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SystemInitializerFull.h" |
| 11 | |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBDebugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 14 | #include "lldb/lldb-private.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 16 | #include "lldb/API/SBBroadcaster.h" |
| 17 | #include "lldb/API/SBCommandInterpreter.h" |
| 18 | #include "lldb/API/SBCommandReturnObject.h" |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 19 | #include "lldb/API/SBError.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 20 | #include "lldb/API/SBEvent.h" |
| 21 | #include "lldb/API/SBFrame.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | #include "lldb/API/SBListener.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 23 | #include "lldb/API/SBProcess.h" |
| 24 | #include "lldb/API/SBSourceManager.h" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 25 | #include "lldb/API/SBStream.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 26 | #include "lldb/API/SBStringList.h" |
Vadim Macagon | c10e34d | 2017-08-09 09:20:40 +0000 | [diff] [blame] | 27 | #include "lldb/API/SBStructuredData.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 28 | #include "lldb/API/SBTarget.h" |
| 29 | #include "lldb/API/SBThread.h" |
Enrico Granata | 061858c | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 30 | #include "lldb/API/SBTypeCategory.h" |
Enrico Granata | 061858c | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 31 | #include "lldb/API/SBTypeFilter.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | #include "lldb/API/SBTypeFormat.h" |
Enrico Granata | 061858c | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 33 | #include "lldb/API/SBTypeNameSpecifier.h" |
| 34 | #include "lldb/API/SBTypeSummary.h" |
| 35 | #include "lldb/API/SBTypeSynthetic.h" |
| 36 | |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 37 | #include "lldb/Core/Debugger.h" |
Vadim Macagon | c10e34d | 2017-08-09 09:20:40 +0000 | [diff] [blame] | 38 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 39 | #include "lldb/Core/StreamFile.h" |
Vadim Macagon | c10e34d | 2017-08-09 09:20:40 +0000 | [diff] [blame] | 40 | #include "lldb/Core/StructuredDataImpl.h" |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 41 | #include "lldb/DataFormatters/DataVisualization.h" |
Pavel Labath | f1389e9 | 2018-02-19 15:06:28 +0000 | [diff] [blame] | 42 | #include "lldb/Host/XML.h" |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 43 | #include "lldb/Initialization/SystemLifetimeManager.h" |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 44 | #include "lldb/Interpreter/CommandInterpreter.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 45 | #include "lldb/Interpreter/OptionArgParser.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 46 | #include "lldb/Interpreter/OptionGroupPlatform.h" |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 47 | #include "lldb/Target/Process.h" |
| 48 | #include "lldb/Target/TargetList.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 49 | #include "lldb/Utility/Args.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 50 | #include "lldb/Utility/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | |
Enrico Granata | d717cc9 | 2015-10-20 04:50:09 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 7b2e5a3 | 2016-09-16 19:09:12 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 58a559c | 2014-08-27 20:15:09 +0000 | [diff] [blame] | 54 | #include "llvm/Support/DynamicLibrary.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ManagedStatic.h" |
Zachary Turner | 58a559c | 2014-08-27 20:15:09 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | using namespace lldb; |
| 58 | using namespace lldb_private; |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp, |
| 61 | const FileSpec &spec, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 62 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | llvm::sys::DynamicLibrary dynlib = |
| 64 | llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str()); |
| 65 | if (dynlib.isValid()) { |
| 66 | typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger); |
| 67 | |
| 68 | lldb::SBDebugger debugger_sb(debugger_sp); |
| 69 | // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) |
| 70 | // function. |
| 71 | // TODO: mangle this differently for your system - on OSX, the first |
| 72 | // underscore needs to be removed and the second one stays |
| 73 | LLDBCommandPluginInit init_func = |
Stephane Sezer | 22701b2 | 2017-10-24 23:46:00 +0000 | [diff] [blame] | 74 | (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | "_ZN4lldb16PluginInitializeENS_10SBDebuggerE"); |
| 76 | if (init_func) { |
| 77 | if (init_func(debugger_sb)) |
| 78 | return dynlib; |
| 79 | else |
| 80 | error.SetErrorString("plug-in refused to load " |
| 81 | "(lldb::PluginInitialize(lldb::SBDebugger) " |
| 82 | "returned false)"); |
| 83 | } else { |
| 84 | error.SetErrorString("plug-in is missing the required initialization: " |
| 85 | "lldb::PluginInitialize(lldb::SBDebugger)"); |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 86 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | } else { |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 88 | if (FileSystem::Instance().Exists(spec)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | error.SetErrorString("this file does not represent a loadable dylib"); |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 90 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | error.SetErrorString("no such file"); |
| 92 | } |
| 93 | return llvm::sys::DynamicLibrary(); |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 96 | static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime; |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | SBError SBInputReader::Initialize( |
| 99 | lldb::SBDebugger &sb_debugger, |
| 100 | unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction, |
| 101 | char const *, unsigned long), |
| 102 | void *, lldb::InputReaderGranularity, char const *, char const *, bool) { |
| 103 | return SBError(); |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | void SBInputReader::SetIsDone(bool) {} |
Eugene Zelenko | dbb0abb | 2015-10-31 01:22:59 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | bool SBInputReader::IsActive() const { return false; } |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 109 | |
Eugene Zelenko | dbb0abb | 2015-10-31 01:22:59 +0000 | [diff] [blame] | 110 | SBDebugger::SBDebugger() = default; |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) |
| 113 | : m_opaque_sp(debugger_sp) {} |
Eugene Zelenko | dbb0abb | 2015-10-31 01:22:59 +0000 | [diff] [blame] | 114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {} |
Eugene Zelenko | dbb0abb | 2015-10-31 01:22:59 +0000 | [diff] [blame] | 116 | |
| 117 | SBDebugger::~SBDebugger() = default; |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) { |
| 120 | if (this != &rhs) { |
| 121 | m_opaque_sp = rhs.m_opaque_sp; |
| 122 | } |
| 123 | return *this; |
Eugene Zelenko | dbb0abb | 2015-10-31 01:22:59 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | void SBDebugger::Initialize() { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 127 | SBInitializerOptions options; |
| 128 | SBDebugger::Initialize(options); |
| 129 | } |
| 130 | |
| 131 | lldb::SBError SBDebugger::Initialize(SBInitializerOptions &options) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | if (log) |
| 135 | log->Printf("SBDebugger::Initialize ()"); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 136 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 137 | SBError error; |
| 138 | if (auto e = g_debugger_lifetime->Initialize( |
| 139 | llvm::make_unique<SystemInitializerFull>(), *options.m_opaque_up, |
| 140 | LoadPlugin)) { |
| 141 | error.SetError(Status(std::move(e))); |
| 142 | } |
| 143 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); } |
| 147 | |
| 148 | void SBDebugger::Clear() { |
| 149 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 150 | |
| 151 | if (log) |
| 152 | log->Printf("SBDebugger(%p)::Clear ()", |
| 153 | static_cast<void *>(m_opaque_sp.get())); |
| 154 | |
| 155 | if (m_opaque_sp) |
| 156 | m_opaque_sp->ClearIOHandlers(); |
| 157 | |
| 158 | m_opaque_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | SBDebugger SBDebugger::Create() { |
| 162 | return SBDebugger::Create(false, nullptr, nullptr); |
Greg Clayton | 48e4254 | 2010-07-30 20:12:55 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | SBDebugger SBDebugger::Create(bool source_init_files) { |
| 166 | return SBDebugger::Create(source_init_files, nullptr, nullptr); |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | SBDebugger SBDebugger::Create(bool source_init_files, |
| 170 | lldb::LogOutputCallback callback, void *baton) |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 171 | |
| 172 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | SBDebugger debugger; |
Greg Clayton | cb172b1 | 2014-05-19 20:42:14 +0000 | [diff] [blame] | 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | // Currently we have issues if this function is called simultaneously on two |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 178 | // different threads. The issues mainly revolve around the fact that the |
| 179 | // lldb_private::FormatManager uses global collections and having two threads |
| 180 | // parsing the .lldbinit files can cause mayhem. So to get around this for |
| 181 | // now we need to use a mutex to prevent bad things from happening. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | static std::recursive_mutex g_mutex; |
| 183 | std::lock_guard<std::recursive_mutex> guard(g_mutex); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | debugger.reset(Debugger::CreateInstance(callback, baton)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | if (log) { |
| 188 | SBStream sstr; |
| 189 | debugger.GetDescription(sstr); |
| 190 | log->Printf("SBDebugger::Create () => SBDebugger(%p): %s", |
| 191 | static_cast<void *>(debugger.m_opaque_sp.get()), |
| 192 | sstr.GetData()); |
| 193 | } |
| 194 | |
| 195 | SBCommandInterpreter interp = debugger.GetCommandInterpreter(); |
| 196 | if (source_init_files) { |
| 197 | interp.get()->SkipLLDBInitFiles(false); |
| 198 | interp.get()->SkipAppInitFiles(false); |
| 199 | SBCommandReturnObject result; |
| 200 | interp.SourceInitFileInHomeDirectory(result); |
| 201 | } else { |
| 202 | interp.get()->SkipLLDBInitFiles(true); |
| 203 | interp.get()->SkipAppInitFiles(true); |
| 204 | } |
| 205 | return debugger; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | void SBDebugger::Destroy(SBDebugger &debugger) { |
| 209 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 210 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 211 | if (log) { |
| 212 | SBStream sstr; |
| 213 | debugger.GetDescription(sstr); |
| 214 | log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s", |
| 215 | static_cast<void *>(debugger.m_opaque_sp.get()), |
| 216 | sstr.GetData()); |
| 217 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | Debugger::Destroy(debugger.m_opaque_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | if (debugger.m_opaque_sp.get() != nullptr) |
| 222 | debugger.m_opaque_sp.reset(); |
Caroline Tice | e02657b | 2011-01-22 01:02:07 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | void SBDebugger::MemoryPressureDetected() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 226 | // Since this function can be call asynchronously, we allow it to be non- |
| 227 | // mandatory. We have seen deadlocks with this function when called so we |
| 228 | // need to safeguard against this until we can determine what is causing the |
| 229 | // deadlocks. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 231 | |
| 232 | const bool mandatory = false; |
| 233 | if (log) { |
| 234 | log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d", |
| 235 | mandatory); |
| 236 | } |
| 237 | |
| 238 | ModuleList::RemoveOrphanSharedModules(mandatory); |
Greg Clayton | f932241 | 2011-12-15 04:38:41 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; } |
| 242 | |
| 243 | void SBDebugger::SetAsync(bool b) { |
| 244 | if (m_opaque_sp) |
| 245 | m_opaque_sp->SetAsyncExecution(b); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | bool SBDebugger::GetAsync() { |
| 249 | return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | void SBDebugger::SkipLLDBInitFiles(bool b) { |
| 253 | if (m_opaque_sp) |
| 254 | m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b); |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | void SBDebugger::SkipAppInitFiles(bool b) { |
| 258 | if (m_opaque_sp) |
| 259 | m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b); |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 262 | // Shouldn't really be settable after initialization as this could cause lots |
| 263 | // of problems; don't want users trying to switch modes in the middle of a |
| 264 | // debugging session. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) { |
| 266 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | if (log) |
| 269 | log->Printf( |
| 270 | "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)", |
| 271 | static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), |
| 272 | transfer_ownership); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | if (m_opaque_sp) |
| 275 | m_opaque_sp->SetInputFileHandle(fh, transfer_ownership); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) { |
| 279 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | if (log) |
| 282 | log->Printf( |
| 283 | "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)", |
| 284 | static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), |
| 285 | transfer_ownership); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | if (m_opaque_sp) |
| 288 | m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) { |
| 292 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 293 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | if (log) |
| 295 | log->Printf( |
| 296 | "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)", |
| 297 | static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), |
| 298 | transfer_ownership); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 299 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | if (m_opaque_sp) |
| 301 | m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 304 | FILE *SBDebugger::GetInputFileHandle() { |
| 305 | if (m_opaque_sp) { |
| 306 | StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile()); |
| 307 | if (stream_file_sp) |
| 308 | return stream_file_sp->GetFile().GetStream(); |
| 309 | } |
| 310 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | FILE *SBDebugger::GetOutputFileHandle() { |
| 314 | if (m_opaque_sp) { |
| 315 | StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile()); |
| 316 | if (stream_file_sp) |
| 317 | return stream_file_sp->GetFile().GetStream(); |
| 318 | } |
| 319 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | FILE *SBDebugger::GetErrorFileHandle() { |
| 323 | if (m_opaque_sp) { |
| 324 | StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile()); |
| 325 | if (stream_file_sp) |
| 326 | return stream_file_sp->GetFile().GetStream(); |
| 327 | } |
| 328 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | void SBDebugger::SaveInputTerminalState() { |
| 332 | if (m_opaque_sp) |
| 333 | m_opaque_sp->SaveInputTerminalState(); |
Jim Ingham | c5917d9 | 2012-11-30 20:23:19 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | void SBDebugger::RestoreInputTerminalState() { |
| 337 | if (m_opaque_sp) |
| 338 | m_opaque_sp->RestoreInputTerminalState(); |
Jim Ingham | c5917d9 | 2012-11-30 20:23:19 +0000 | [diff] [blame] | 339 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 340 | SBCommandInterpreter SBDebugger::GetCommandInterpreter() { |
| 341 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 342 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | SBCommandInterpreter sb_interpreter; |
| 344 | if (m_opaque_sp) |
| 345 | sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 346 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | if (log) |
| 348 | log->Printf( |
| 349 | "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)", |
| 350 | static_cast<void *>(m_opaque_sp.get()), |
| 351 | static_cast<void *>(sb_interpreter.get())); |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 352 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | return sb_interpreter; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | void SBDebugger::HandleCommand(const char *command) { |
| 357 | if (m_opaque_sp) { |
| 358 | TargetSP target_sp(m_opaque_sp->GetSelectedTarget()); |
| 359 | std::unique_lock<std::recursive_mutex> lock; |
| 360 | if (target_sp) |
| 361 | lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | SBCommandInterpreter sb_interpreter(GetCommandInterpreter()); |
| 364 | SBCommandReturnObject result; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | sb_interpreter.HandleCommand(command, result, false); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | if (GetErrorFileHandle() != nullptr) |
| 369 | result.PutError(GetErrorFileHandle()); |
| 370 | if (GetOutputFileHandle() != nullptr) |
| 371 | result.PutOutput(GetOutputFileHandle()); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 372 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 373 | if (!m_opaque_sp->GetAsyncExecution()) { |
| 374 | SBProcess process(GetCommandInterpreter().GetProcess()); |
| 375 | ProcessSP process_sp(process.GetSP()); |
| 376 | if (process_sp) { |
| 377 | EventSP event_sp; |
| 378 | ListenerSP lldb_listener_sp = m_opaque_sp->GetListener(); |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 379 | while (lldb_listener_sp->GetEventForBroadcaster( |
| 380 | process_sp.get(), event_sp, std::chrono::seconds(0))) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | SBEvent event(event_sp); |
| 382 | HandleProcessEvent(process, event, GetOutputFileHandle(), |
| 383 | GetErrorFileHandle()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 384 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | SBListener SBDebugger::GetListener() { |
| 391 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | SBListener sb_listener; |
| 394 | if (m_opaque_sp) |
| 395 | sb_listener.reset(m_opaque_sp->GetListener()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | if (log) |
| 398 | log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)", |
| 399 | static_cast<void *>(m_opaque_sp.get()), |
| 400 | static_cast<void *>(sb_listener.get())); |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | return sb_listener; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | void SBDebugger::HandleProcessEvent(const SBProcess &process, |
| 406 | const SBEvent &event, FILE *out, |
| 407 | FILE *err) { |
| 408 | if (!process.IsValid()) |
| 409 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | TargetSP target_sp(process.GetTarget().GetSP()); |
| 412 | if (!target_sp) |
| 413 | return; |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 414 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | const uint32_t event_type = event.GetType(); |
| 416 | char stdio_buffer[1024]; |
| 417 | size_t len; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 419 | std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | if (event_type & |
| 422 | (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) { |
| 423 | // Drain stdout when we stop just in case we have any bytes |
| 424 | while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0) |
| 425 | if (out != nullptr) |
| 426 | ::fwrite(stdio_buffer, 1, len, out); |
| 427 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 428 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 429 | if (event_type & |
| 430 | (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) { |
| 431 | // Drain stderr when we stop just in case we have any bytes |
| 432 | while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0) |
| 433 | if (err != nullptr) |
| 434 | ::fwrite(stdio_buffer, 1, len, err); |
| 435 | } |
| 436 | |
| 437 | if (event_type & Process::eBroadcastBitStateChanged) { |
| 438 | StateType event_state = SBProcess::GetStateFromEvent(event); |
| 439 | |
| 440 | if (event_state == eStateInvalid) |
| 441 | return; |
| 442 | |
| 443 | bool is_stopped = StateIsStoppedState(event_state); |
| 444 | if (!is_stopped) |
| 445 | process.ReportEventState(event, out); |
| 446 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | SBSourceManager SBDebugger::GetSourceManager() { |
| 450 | SBSourceManager sb_source_manager(*this); |
| 451 | return sb_source_manager; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) { |
| 455 | if (arch_name && arch_name_len) { |
| 456 | ArchSpec default_arch = Target::GetDefaultArchitecture(); |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 457 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | if (default_arch.IsValid()) { |
| 459 | const std::string &triple_str = default_arch.GetTriple().str(); |
| 460 | if (!triple_str.empty()) |
| 461 | ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str()); |
| 462 | else |
| 463 | ::snprintf(arch_name, arch_name_len, "%s", |
| 464 | default_arch.GetArchitectureName()); |
| 465 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | } |
| 468 | if (arch_name && arch_name_len) |
| 469 | arch_name[0] = '\0'; |
| 470 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | bool SBDebugger::SetDefaultArchitecture(const char *arch_name) { |
| 474 | if (arch_name) { |
| 475 | ArchSpec arch(arch_name); |
| 476 | if (arch.IsValid()) { |
| 477 | Target::SetDefaultArchitecture(arch); |
| 478 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 480 | } |
| 481 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | ScriptLanguage |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 485 | SBDebugger::GetScriptingLanguage(const char *script_language_name) { |
Zachary Turner | 7b2e5a3 | 2016-09-16 19:09:12 +0000 | [diff] [blame] | 486 | if (!script_language_name) return eScriptLanguageDefault; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 487 | return OptionArgParser::ToScriptLanguage( |
| 488 | llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 491 | const char *SBDebugger::GetVersionString() { |
| 492 | return lldb_private::GetVersion(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | const char *SBDebugger::StateAsCString(StateType state) { |
| 496 | return lldb_private::StateAsCString(state); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Pavel Labath | f1389e9 | 2018-02-19 15:06:28 +0000 | [diff] [blame] | 499 | static void AddBoolConfigEntry(StructuredData::Dictionary &dict, |
| 500 | llvm::StringRef name, bool value, |
| 501 | llvm::StringRef description) { |
| 502 | auto entry_up = llvm::make_unique<StructuredData::Dictionary>(); |
| 503 | entry_up->AddBooleanItem("value", value); |
| 504 | entry_up->AddStringItem("description", description); |
| 505 | dict.AddItem(name, std::move(entry_up)); |
| 506 | } |
| 507 | |
Pavel Labath | abc0c6a | 2018-06-28 14:23:04 +0000 | [diff] [blame] | 508 | static void AddLLVMTargets(StructuredData::Dictionary &dict) { |
| 509 | auto array_up = llvm::make_unique<StructuredData::Array>(); |
| 510 | #define LLVM_TARGET(target) \ |
| 511 | array_up->AddItem(llvm::make_unique<StructuredData::String>(#target)); |
| 512 | #include "llvm/Config/Targets.def" |
| 513 | auto entry_up = llvm::make_unique<StructuredData::Dictionary>(); |
| 514 | entry_up->AddItem("value", std::move(array_up)); |
| 515 | entry_up->AddStringItem("description", "A list of configured LLVM targets."); |
| 516 | dict.AddItem("targets", std::move(entry_up)); |
| 517 | } |
| 518 | |
Pavel Labath | f1389e9 | 2018-02-19 15:06:28 +0000 | [diff] [blame] | 519 | SBStructuredData SBDebugger::GetBuildConfiguration() { |
| 520 | auto config_up = llvm::make_unique<StructuredData::Dictionary>(); |
| 521 | AddBoolConfigEntry( |
| 522 | *config_up, "xml", XMLDocument::XMLEnabled(), |
| 523 | "A boolean value that indicates if XML support is enabled in LLDB"); |
Pavel Labath | abc0c6a | 2018-06-28 14:23:04 +0000 | [diff] [blame] | 524 | AddLLVMTargets(*config_up); |
Pavel Labath | f1389e9 | 2018-02-19 15:06:28 +0000 | [diff] [blame] | 525 | |
| 526 | SBStructuredData data; |
| 527 | data.m_impl_up->SetObjectSP(std::move(config_up)); |
| 528 | return data; |
| 529 | } |
| 530 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 531 | bool SBDebugger::StateIsRunningState(StateType state) { |
| 532 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 533 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 534 | const bool result = lldb_private::StateIsRunningState(state); |
| 535 | if (log) |
| 536 | log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i", |
| 537 | StateAsCString(state), result); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 538 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 542 | bool SBDebugger::StateIsStoppedState(StateType state) { |
| 543 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | const bool result = lldb_private::StateIsStoppedState(state, false); |
| 546 | if (log) |
| 547 | log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i", |
| 548 | StateAsCString(state), result); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 549 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 553 | lldb::SBTarget SBDebugger::CreateTarget(const char *filename, |
| 554 | const char *target_triple, |
| 555 | const char *platform_name, |
| 556 | bool add_dependent_modules, |
| 557 | lldb::SBError &sb_error) { |
| 558 | SBTarget sb_target; |
| 559 | TargetSP target_sp; |
| 560 | if (m_opaque_sp) { |
| 561 | sb_error.Clear(); |
| 562 | OptionGroupPlatform platform_options(false); |
| 563 | platform_options.SetPlatformName(platform_name); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget( |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 566 | *m_opaque_sp, filename, target_triple, |
| 567 | add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 568 | &platform_options, target_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 569 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | if (sb_error.Success()) |
| 571 | sb_target.SetSP(target_sp); |
| 572 | } else { |
| 573 | sb_error.SetErrorString("invalid debugger"); |
| 574 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 575 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 576 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 577 | if (log) |
| 578 | log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, " |
| 579 | "platform_name=%s, add_dependent_modules=%u, error=%s) => " |
| 580 | "SBTarget(%p)", |
| 581 | static_cast<void *>(m_opaque_sp.get()), filename, target_triple, |
| 582 | platform_name, add_dependent_modules, sb_error.GetCString(), |
| 583 | static_cast<void *>(target_sp.get())); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 584 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 585 | return sb_target; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 586 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 587 | |
| 588 | SBTarget |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename, |
| 590 | const char *target_triple) { |
| 591 | SBTarget sb_target; |
| 592 | TargetSP target_sp; |
| 593 | if (m_opaque_sp) { |
| 594 | const bool add_dependent_modules = true; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 595 | Status error(m_opaque_sp->GetTargetList().CreateTarget( |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 596 | *m_opaque_sp, filename, target_triple, |
| 597 | add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 598 | target_sp)); |
| 599 | sb_target.SetSP(target_sp); |
| 600 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 601 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 603 | if (log) |
| 604 | log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple " |
| 605 | "(filename=\"%s\", triple=%s) => SBTarget(%p)", |
| 606 | static_cast<void *>(m_opaque_sp.get()), filename, target_triple, |
| 607 | static_cast<void *>(target_sp.get())); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 608 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 609 | return sb_target; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 612 | SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename, |
| 613 | const char *arch_cstr) { |
| 614 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 615 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 616 | SBTarget sb_target; |
| 617 | TargetSP target_sp; |
| 618 | if (m_opaque_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 619 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 620 | const bool add_dependent_modules = true; |
| 621 | |
| 622 | error = m_opaque_sp->GetTargetList().CreateTarget( |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 623 | *m_opaque_sp, filename, arch_cstr, |
| 624 | add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | target_sp); |
| 626 | |
| 627 | if (error.Success()) { |
| 628 | m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); |
| 629 | sb_target.SetSP(target_sp); |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 630 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | if (log) |
| 634 | log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", " |
| 635 | "arch=%s) => SBTarget(%p)", |
| 636 | static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr, |
| 637 | static_cast<void *>(target_sp.get())); |
| 638 | |
| 639 | return sb_target; |
| 640 | } |
| 641 | |
| 642 | SBTarget SBDebugger::CreateTarget(const char *filename) { |
| 643 | SBTarget sb_target; |
| 644 | TargetSP target_sp; |
| 645 | if (m_opaque_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 646 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | const bool add_dependent_modules = true; |
| 648 | error = m_opaque_sp->GetTargetList().CreateTarget( |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 649 | *m_opaque_sp, filename, "", |
| 650 | add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr, |
| 651 | target_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 652 | |
| 653 | if (error.Success()) { |
| 654 | m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); |
| 655 | sb_target.SetSP(target_sp); |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 656 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 657 | } |
| 658 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 659 | if (log) |
| 660 | log->Printf( |
| 661 | "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)", |
| 662 | static_cast<void *>(m_opaque_sp.get()), filename, |
| 663 | static_cast<void *>(target_sp.get())); |
| 664 | return sb_target; |
| 665 | } |
| 666 | |
Jim Ingham | b842f2e | 2017-09-14 20:22:49 +0000 | [diff] [blame] | 667 | SBTarget SBDebugger::GetDummyTarget() { |
| 668 | SBTarget sb_target; |
| 669 | if (m_opaque_sp) { |
| 670 | sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this()); |
| 671 | } |
| 672 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 673 | if (log) |
| 674 | log->Printf( |
| 675 | "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)", |
| 676 | static_cast<void *>(m_opaque_sp.get()), |
| 677 | static_cast<void *>(sb_target.GetSP().get())); |
| 678 | return sb_target; |
| 679 | } |
| 680 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | bool SBDebugger::DeleteTarget(lldb::SBTarget &target) { |
| 682 | bool result = false; |
| 683 | if (m_opaque_sp) { |
| 684 | TargetSP target_sp(target.GetSP()); |
| 685 | if (target_sp) { |
| 686 | // No need to lock, the target list is thread safe |
| 687 | result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp); |
| 688 | target_sp->Destroy(); |
| 689 | target.Clear(); |
| 690 | const bool mandatory = true; |
| 691 | ModuleList::RemoveOrphanSharedModules(mandatory); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 696 | if (log) |
| 697 | log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i", |
| 698 | static_cast<void *>(m_opaque_sp.get()), |
| 699 | static_cast<void *>(target.m_opaque_sp.get()), result); |
| 700 | |
| 701 | return result; |
| 702 | } |
| 703 | |
| 704 | SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) { |
| 705 | SBTarget sb_target; |
| 706 | if (m_opaque_sp) { |
| 707 | // No need to lock, the target list is thread safe |
| 708 | sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx)); |
| 709 | } |
| 710 | return sb_target; |
| 711 | } |
| 712 | |
| 713 | uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) { |
| 714 | |
| 715 | lldb::TargetSP target_sp = target.GetSP(); |
| 716 | if (!target_sp) |
| 717 | return UINT32_MAX; |
| 718 | |
| 719 | if (!m_opaque_sp) |
| 720 | return UINT32_MAX; |
| 721 | |
| 722 | return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP()); |
| 723 | } |
| 724 | |
| 725 | SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) { |
| 726 | SBTarget sb_target; |
| 727 | if (m_opaque_sp) { |
| 728 | // No need to lock, the target list is thread safe |
| 729 | sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid)); |
| 730 | } |
| 731 | return sb_target; |
| 732 | } |
| 733 | |
| 734 | SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename, |
| 735 | const char *arch_name) { |
| 736 | SBTarget sb_target; |
| 737 | if (m_opaque_sp && filename && filename[0]) { |
| 738 | // No need to lock, the target list is thread safe |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 739 | ArchSpec arch = Platform::GetAugmentedArchSpec( |
| 740 | m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 741 | TargetSP target_sp( |
| 742 | m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture( |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 743 | FileSpec(filename), arch_name ? &arch : nullptr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 744 | sb_target.SetSP(target_sp); |
| 745 | } |
| 746 | return sb_target; |
| 747 | } |
| 748 | |
| 749 | SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) { |
| 750 | SBTarget sb_target; |
| 751 | if (m_opaque_sp) { |
| 752 | // No need to lock, the target list is thread safe |
| 753 | sb_target.SetSP( |
| 754 | m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get())); |
| 755 | } |
| 756 | return sb_target; |
| 757 | } |
| 758 | |
| 759 | uint32_t SBDebugger::GetNumTargets() { |
| 760 | if (m_opaque_sp) { |
| 761 | // No need to lock, the target list is thread safe |
| 762 | return m_opaque_sp->GetTargetList().GetNumTargets(); |
| 763 | } |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | SBTarget SBDebugger::GetSelectedTarget() { |
| 768 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 769 | |
| 770 | SBTarget sb_target; |
| 771 | TargetSP target_sp; |
| 772 | if (m_opaque_sp) { |
| 773 | // No need to lock, the target list is thread safe |
| 774 | target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget(); |
| 775 | sb_target.SetSP(target_sp); |
| 776 | } |
| 777 | |
| 778 | if (log) { |
| 779 | SBStream sstr; |
| 780 | sb_target.GetDescription(sstr, eDescriptionLevelBrief); |
| 781 | log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s", |
| 782 | static_cast<void *>(m_opaque_sp.get()), |
| 783 | static_cast<void *>(target_sp.get()), sstr.GetData()); |
| 784 | } |
| 785 | |
| 786 | return sb_target; |
| 787 | } |
| 788 | |
| 789 | void SBDebugger::SetSelectedTarget(SBTarget &sb_target) { |
| 790 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 791 | |
| 792 | TargetSP target_sp(sb_target.GetSP()); |
| 793 | if (m_opaque_sp) { |
| 794 | m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); |
| 795 | } |
| 796 | if (log) { |
| 797 | SBStream sstr; |
| 798 | sb_target.GetDescription(sstr, eDescriptionLevelBrief); |
| 799 | log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s", |
| 800 | static_cast<void *>(m_opaque_sp.get()), |
| 801 | static_cast<void *>(target_sp.get()), sstr.GetData()); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | SBPlatform SBDebugger::GetSelectedPlatform() { |
| 806 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 807 | |
| 808 | SBPlatform sb_platform; |
| 809 | DebuggerSP debugger_sp(m_opaque_sp); |
| 810 | if (debugger_sp) { |
| 811 | sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform()); |
| 812 | } |
| 813 | if (log) |
| 814 | log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s", |
| 815 | static_cast<void *>(m_opaque_sp.get()), |
| 816 | static_cast<void *>(sb_platform.GetSP().get()), |
| 817 | sb_platform.GetName()); |
| 818 | return sb_platform; |
| 819 | } |
| 820 | |
| 821 | void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) { |
| 822 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 823 | |
| 824 | DebuggerSP debugger_sp(m_opaque_sp); |
| 825 | if (debugger_sp) { |
| 826 | debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP()); |
| 827 | } |
| 828 | |
| 829 | if (log) |
| 830 | log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)", |
| 831 | static_cast<void *>(m_opaque_sp.get()), |
| 832 | static_cast<void *>(sb_platform.GetSP().get()), |
| 833 | sb_platform.GetName()); |
| 834 | } |
| 835 | |
Vadim Macagon | c10e34d | 2017-08-09 09:20:40 +0000 | [diff] [blame] | 836 | uint32_t SBDebugger::GetNumPlatforms() { |
| 837 | if (m_opaque_sp) { |
| 838 | // No need to lock, the platform list is thread safe |
| 839 | return m_opaque_sp->GetPlatformList().GetSize(); |
| 840 | } |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) { |
| 845 | SBPlatform sb_platform; |
| 846 | if (m_opaque_sp) { |
| 847 | // No need to lock, the platform list is thread safe |
| 848 | sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx)); |
| 849 | } |
| 850 | return sb_platform; |
| 851 | } |
| 852 | |
| 853 | uint32_t SBDebugger::GetNumAvailablePlatforms() { |
| 854 | uint32_t idx = 0; |
| 855 | while (true) { |
| 856 | if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) { |
| 857 | break; |
| 858 | } |
| 859 | ++idx; |
| 860 | } |
| 861 | // +1 for the host platform, which should always appear first in the list. |
| 862 | return idx + 1; |
| 863 | } |
| 864 | |
| 865 | SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) { |
| 866 | SBStructuredData data; |
| 867 | auto platform_dict = llvm::make_unique<StructuredData::Dictionary>(); |
| 868 | llvm::StringRef name_str("name"), desc_str("description"); |
| 869 | |
| 870 | if (idx == 0) { |
| 871 | PlatformSP host_platform_sp(Platform::GetHostPlatform()); |
| 872 | platform_dict->AddStringItem( |
| 873 | name_str, host_platform_sp->GetPluginName().GetStringRef()); |
| 874 | platform_dict->AddStringItem( |
| 875 | desc_str, llvm::StringRef(host_platform_sp->GetDescription())); |
| 876 | } else if (idx > 0) { |
| 877 | const char *plugin_name = |
| 878 | PluginManager::GetPlatformPluginNameAtIndex(idx - 1); |
| 879 | if (!plugin_name) { |
| 880 | return data; |
| 881 | } |
| 882 | platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name)); |
| 883 | |
| 884 | const char *plugin_desc = |
| 885 | PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1); |
| 886 | if (!plugin_desc) { |
| 887 | return data; |
| 888 | } |
| 889 | platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc)); |
| 890 | } |
| 891 | |
| 892 | data.m_impl_up->SetObjectSP( |
| 893 | StructuredData::ObjectSP(platform_dict.release())); |
| 894 | return data; |
| 895 | } |
| 896 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 897 | void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) { |
| 898 | DispatchInput(data, data_len); |
| 899 | } |
| 900 | |
| 901 | void SBDebugger::DispatchInput(const void *data, size_t data_len) { |
| 902 | // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 903 | // |
| 904 | // if (log) |
| 905 | // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", |
| 906 | // size_t=%" PRIu64 ")", |
| 907 | // m_opaque_sp.get(), |
| 908 | // (int) data_len, |
| 909 | // (const char *) data, |
| 910 | // (uint64_t)data_len); |
| 911 | // |
| 912 | // if (m_opaque_sp) |
| 913 | // m_opaque_sp->DispatchInput ((const char *) data, data_len); |
| 914 | } |
| 915 | |
| 916 | void SBDebugger::DispatchInputInterrupt() { |
| 917 | if (m_opaque_sp) |
| 918 | m_opaque_sp->DispatchInputInterrupt(); |
| 919 | } |
| 920 | |
| 921 | void SBDebugger::DispatchInputEndOfFile() { |
| 922 | if (m_opaque_sp) |
| 923 | m_opaque_sp->DispatchInputEndOfFile(); |
| 924 | } |
| 925 | |
| 926 | void SBDebugger::PushInputReader(SBInputReader &reader) {} |
| 927 | |
| 928 | void SBDebugger::RunCommandInterpreter(bool auto_handle_events, |
| 929 | bool spawn_thread) { |
| 930 | if (m_opaque_sp) { |
| 931 | CommandInterpreterRunOptions options; |
| 932 | |
| 933 | m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter( |
| 934 | auto_handle_events, spawn_thread, options); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | void SBDebugger::RunCommandInterpreter(bool auto_handle_events, |
| 939 | bool spawn_thread, |
| 940 | SBCommandInterpreterRunOptions &options, |
| 941 | int &num_errors, bool &quit_requested, |
| 942 | bool &stopped_for_crash) |
| 943 | |
| 944 | { |
| 945 | if (m_opaque_sp) { |
| 946 | CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter(); |
| 947 | interp.RunCommandInterpreter(auto_handle_events, spawn_thread, |
| 948 | options.ref()); |
| 949 | num_errors = interp.GetNumErrors(); |
| 950 | quit_requested = interp.GetQuitRequested(); |
| 951 | stopped_for_crash = interp.GetStoppedForCrash(); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | SBError SBDebugger::RunREPL(lldb::LanguageType language, |
| 956 | const char *repl_options) { |
| 957 | SBError error; |
| 958 | if (m_opaque_sp) |
| 959 | error.ref() = m_opaque_sp->RunREPL(language, repl_options); |
| 960 | else |
| 961 | error.SetErrorString("invalid debugger"); |
| 962 | return error; |
| 963 | } |
| 964 | |
| 965 | void SBDebugger::reset(const DebuggerSP &debugger_sp) { |
| 966 | m_opaque_sp = debugger_sp; |
| 967 | } |
| 968 | |
| 969 | Debugger *SBDebugger::get() const { return m_opaque_sp.get(); } |
| 970 | |
| 971 | Debugger &SBDebugger::ref() const { |
| 972 | assert(m_opaque_sp.get()); |
| 973 | return *m_opaque_sp; |
| 974 | } |
| 975 | |
| 976 | const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; } |
| 977 | |
| 978 | SBDebugger SBDebugger::FindDebuggerWithID(int id) { |
| 979 | // No need to lock, the debugger list is thread safe |
| 980 | SBDebugger sb_debugger; |
| 981 | DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id); |
| 982 | if (debugger_sp) |
| 983 | sb_debugger.reset(debugger_sp); |
| 984 | return sb_debugger; |
| 985 | } |
| 986 | |
| 987 | const char *SBDebugger::GetInstanceName() { |
| 988 | return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr); |
| 989 | } |
| 990 | |
| 991 | SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value, |
| 992 | const char *debugger_instance_name) { |
| 993 | SBError sb_error; |
| 994 | DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName( |
| 995 | ConstString(debugger_instance_name))); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 996 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 997 | if (debugger_sp) { |
| 998 | ExecutionContext exe_ctx( |
| 999 | debugger_sp->GetCommandInterpreter().GetExecutionContext()); |
| 1000 | error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign, |
| 1001 | var_name, value); |
| 1002 | } else { |
| 1003 | error.SetErrorStringWithFormat("invalid debugger instance name '%s'", |
| 1004 | debugger_instance_name); |
| 1005 | } |
| 1006 | if (error.Fail()) |
| 1007 | sb_error.SetError(error); |
| 1008 | return sb_error; |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Greg Clayton | 431ce67 | 2011-04-18 23:15:17 +0000 | [diff] [blame] | 1011 | SBStringList |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | SBDebugger::GetInternalVariableValue(const char *var_name, |
| 1013 | const char *debugger_instance_name) { |
| 1014 | SBStringList ret_value; |
| 1015 | DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName( |
| 1016 | ConstString(debugger_instance_name))); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1017 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1018 | if (debugger_sp) { |
| 1019 | ExecutionContext exe_ctx( |
| 1020 | debugger_sp->GetCommandInterpreter().GetExecutionContext()); |
| 1021 | lldb::OptionValueSP value_sp( |
| 1022 | debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error)); |
| 1023 | if (value_sp) { |
| 1024 | StreamString value_strm; |
| 1025 | value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue); |
| 1026 | const std::string &value_str = value_strm.GetString(); |
| 1027 | if (!value_str.empty()) { |
| 1028 | StringList string_list; |
| 1029 | string_list.SplitIntoLines(value_str); |
| 1030 | return SBStringList(&string_list); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | return SBStringList(); |
| 1035 | } |
| 1036 | |
| 1037 | uint32_t SBDebugger::GetTerminalWidth() const { |
| 1038 | return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0); |
| 1039 | } |
| 1040 | |
| 1041 | void SBDebugger::SetTerminalWidth(uint32_t term_width) { |
| 1042 | if (m_opaque_sp) |
| 1043 | m_opaque_sp->SetTerminalWidth(term_width); |
| 1044 | } |
| 1045 | |
| 1046 | const char *SBDebugger::GetPrompt() const { |
| 1047 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 1048 | |
| 1049 | if (log) |
| 1050 | log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"", |
| 1051 | static_cast<void *>(m_opaque_sp.get()), |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 1052 | (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : "")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1053 | |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 1054 | return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString() |
| 1055 | : nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | void SBDebugger::SetPrompt(const char *prompt) { |
| 1059 | if (m_opaque_sp) |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 1060 | m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 1063 | const char *SBDebugger::GetReproducerPath() const { |
| 1064 | return (m_opaque_sp |
| 1065 | ? ConstString(m_opaque_sp->GetReproducerPath()).GetCString() |
| 1066 | : nullptr); |
| 1067 | } |
| 1068 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | ScriptLanguage SBDebugger::GetScriptLanguage() const { |
| 1070 | return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone); |
| 1071 | } |
| 1072 | |
| 1073 | void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) { |
| 1074 | if (m_opaque_sp) { |
| 1075 | m_opaque_sp->SetScriptLanguage(script_lang); |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | bool SBDebugger::SetUseExternalEditor(bool value) { |
| 1080 | return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false); |
| 1081 | } |
| 1082 | |
| 1083 | bool SBDebugger::GetUseExternalEditor() { |
| 1084 | return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false); |
| 1085 | } |
| 1086 | |
| 1087 | bool SBDebugger::SetUseColor(bool value) { |
| 1088 | return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false); |
| 1089 | } |
| 1090 | |
| 1091 | bool SBDebugger::GetUseColor() const { |
| 1092 | return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false); |
| 1093 | } |
| 1094 | |
| 1095 | bool SBDebugger::GetDescription(SBStream &description) { |
| 1096 | Stream &strm = description.ref(); |
| 1097 | |
| 1098 | if (m_opaque_sp) { |
| 1099 | const char *name = m_opaque_sp->GetInstanceName().AsCString(); |
| 1100 | user_id_t id = m_opaque_sp->GetID(); |
| 1101 | strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id); |
| 1102 | } else |
| 1103 | strm.PutCString("No value"); |
| 1104 | |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
| 1108 | user_id_t SBDebugger::GetID() { |
| 1109 | return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID); |
| 1110 | } |
| 1111 | |
| 1112 | SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) { |
| 1113 | SBError sb_error; |
| 1114 | if (m_opaque_sp) { |
| 1115 | if (platform_name_cstr && platform_name_cstr[0]) { |
| 1116 | ConstString platform_name(platform_name_cstr); |
| 1117 | PlatformSP platform_sp(Platform::Find(platform_name)); |
| 1118 | |
| 1119 | if (platform_sp) { |
| 1120 | // Already have a platform with this name, just select it |
| 1121 | m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp); |
| 1122 | } else { |
| 1123 | // We don't have a platform by this name yet, create one |
| 1124 | platform_sp = Platform::Create(platform_name, sb_error.ref()); |
| 1125 | if (platform_sp) { |
| 1126 | // We created the platform, now append and select it |
| 1127 | bool make_selected = true; |
| 1128 | m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected); |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 1129 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } else { |
| 1132 | sb_error.ref().SetErrorString("invalid platform name"); |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 1133 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1134 | } else { |
| 1135 | sb_error.ref().SetErrorString("invalid debugger"); |
| 1136 | } |
| 1137 | return sb_error; |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1140 | bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) { |
| 1141 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 1142 | if (m_opaque_sp) { |
| 1143 | PlatformSP platform_sp( |
| 1144 | m_opaque_sp->GetPlatformList().GetSelectedPlatform()); |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1146 | if (platform_sp) { |
| 1147 | if (log && sysroot) |
| 1148 | log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot); |
| 1149 | platform_sp->SetSDKRootDirectory(ConstString(sysroot)); |
| 1150 | return true; |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 1151 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1152 | } |
| 1153 | return false; |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1156 | bool SBDebugger::GetCloseInputOnEOF() const { |
| 1157 | return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false); |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1160 | void SBDebugger::SetCloseInputOnEOF(bool b) { |
| 1161 | if (m_opaque_sp) |
| 1162 | m_opaque_sp->SetCloseInputOnEOF(b); |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1165 | SBTypeCategory SBDebugger::GetCategory(const char *category_name) { |
| 1166 | if (!category_name || *category_name == 0) |
| 1167 | return SBTypeCategory(); |
| 1168 | |
| 1169 | TypeCategoryImplSP category_sp; |
| 1170 | |
| 1171 | if (DataVisualization::Categories::GetCategory(ConstString(category_name), |
| 1172 | category_sp, false)) |
| 1173 | return SBTypeCategory(category_sp); |
| 1174 | else |
| 1175 | return SBTypeCategory(); |
Michael Sartain | c3ce7f27 | 2013-05-23 20:47:45 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1178 | SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) { |
| 1179 | TypeCategoryImplSP category_sp; |
| 1180 | if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) |
| 1181 | return SBTypeCategory(category_sp); |
| 1182 | else |
| 1183 | return SBTypeCategory(); |
Michael Sartain | c3ce7f27 | 2013-05-23 20:47:45 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1186 | SBTypeCategory SBDebugger::CreateCategory(const char *category_name) { |
| 1187 | if (!category_name || *category_name == 0) |
| 1188 | return SBTypeCategory(); |
Greg Clayton | da7bc7d | 2011-11-13 06:57:31 +0000 | [diff] [blame] | 1189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1190 | TypeCategoryImplSP category_sp; |
| 1191 | |
| 1192 | if (DataVisualization::Categories::GetCategory(ConstString(category_name), |
| 1193 | category_sp, true)) |
| 1194 | return SBTypeCategory(category_sp); |
| 1195 | else |
| 1196 | return SBTypeCategory(); |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 1197 | } |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1199 | bool SBDebugger::DeleteCategory(const char *category_name) { |
| 1200 | if (!category_name || *category_name == 0) |
| 1201 | return false; |
| 1202 | |
| 1203 | return DataVisualization::Categories::Delete(ConstString(category_name)); |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1204 | } |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 1205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1206 | uint32_t SBDebugger::GetNumCategories() { |
| 1207 | return DataVisualization::Categories::GetCount(); |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1210 | SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) { |
| 1211 | return SBTypeCategory( |
| 1212 | DataVisualization::Categories::GetCategoryAtIndex(index)); |
| 1213 | } |
| 1214 | |
| 1215 | SBTypeCategory SBDebugger::GetDefaultCategory() { |
| 1216 | return GetCategory("default"); |
| 1217 | } |
| 1218 | |
| 1219 | SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) { |
| 1220 | SBTypeCategory default_category_sb = GetDefaultCategory(); |
| 1221 | if (default_category_sb.GetEnabled()) |
| 1222 | return default_category_sb.GetFormatForType(type_name); |
| 1223 | return SBTypeFormat(); |
| 1224 | } |
| 1225 | |
| 1226 | #ifndef LLDB_DISABLE_PYTHON |
| 1227 | SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) { |
| 1228 | if (!type_name.IsValid()) |
| 1229 | return SBTypeSummary(); |
| 1230 | return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())); |
| 1231 | } |
| 1232 | #endif // LLDB_DISABLE_PYTHON |
| 1233 | |
| 1234 | SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) { |
| 1235 | if (!type_name.IsValid()) |
| 1236 | return SBTypeFilter(); |
| 1237 | return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())); |
| 1238 | } |
| 1239 | |
| 1240 | #ifndef LLDB_DISABLE_PYTHON |
| 1241 | SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) { |
| 1242 | if (!type_name.IsValid()) |
| 1243 | return SBTypeSynthetic(); |
| 1244 | return SBTypeSynthetic( |
| 1245 | DataVisualization::GetSyntheticForType(type_name.GetSP())); |
| 1246 | } |
| 1247 | #endif // LLDB_DISABLE_PYTHON |
| 1248 | |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 1249 | static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) { |
| 1250 | if (categories == nullptr) |
| 1251 | return {}; |
| 1252 | size_t len = 0; |
| 1253 | while (categories[len] != nullptr) |
| 1254 | ++len; |
| 1255 | return llvm::makeArrayRef(categories, len); |
| 1256 | } |
| 1257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1258 | bool SBDebugger::EnableLog(const char *channel, const char **categories) { |
| 1259 | if (m_opaque_sp) { |
| 1260 | uint32_t log_options = |
| 1261 | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 1262 | std::string error; |
| 1263 | llvm::raw_string_ostream error_stream(error); |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 1264 | return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "", |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 1265 | log_options, error_stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1266 | } else |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
| 1269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1270 | void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback, |
| 1271 | void *baton) { |
| 1272 | if (m_opaque_sp) { |
| 1273 | return m_opaque_sp->SetLoggingCallback(log_callback, baton); |
| 1274 | } |
Jim Ingham | 4f02b22 | 2012-02-22 22:49:20 +0000 | [diff] [blame] | 1275 | } |