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