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