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