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