Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBCommandInterpreter.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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/lldb-types.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/SourceManager.h" |
| 14 | #include "lldb/Core/Listener.h" |
| 15 | #include "lldb/Interpreter/CommandInterpreter.h" |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 18 | #include "lldb/Target/Target.h" |
| 19 | |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 20 | #include "lldb/API/SBBroadcaster.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 21 | #include "lldb/API/SBCommandReturnObject.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 22 | #include "lldb/API/SBCommandInterpreter.h" |
Ilia K | af10e1c | 2015-03-21 10:53:37 +0000 | [diff] [blame] | 23 | #include "lldb/API/SBEvent.h" |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 24 | #include "lldb/API/SBExecutionContext.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 25 | #include "lldb/API/SBProcess.h" |
| 26 | #include "lldb/API/SBTarget.h" |
| 27 | #include "lldb/API/SBListener.h" |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 28 | #include "lldb/API/SBStream.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 29 | #include "lldb/API/SBStringList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 34 | SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions() |
| 35 | { |
| 36 | m_opaque_up.reset(new CommandInterpreterRunOptions()); |
| 37 | } |
| 38 | |
| 39 | SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions() |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | bool |
| 45 | SBCommandInterpreterRunOptions::GetStopOnContinue () const |
| 46 | { |
| 47 | return m_opaque_up->GetStopOnContinue(); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | SBCommandInterpreterRunOptions::SetStopOnContinue (bool stop_on_continue) |
| 52 | { |
| 53 | m_opaque_up->SetStopOnContinue(stop_on_continue); |
| 54 | } |
| 55 | |
| 56 | bool |
| 57 | SBCommandInterpreterRunOptions::GetStopOnError () const |
| 58 | { |
| 59 | return m_opaque_up->GetStopOnError(); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | SBCommandInterpreterRunOptions::SetStopOnError (bool stop_on_error) |
| 64 | { |
| 65 | m_opaque_up->SetStopOnError(stop_on_error); |
| 66 | } |
| 67 | |
| 68 | bool |
| 69 | SBCommandInterpreterRunOptions::GetStopOnCrash () const |
| 70 | { |
| 71 | return m_opaque_up->GetStopOnCrash(); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | SBCommandInterpreterRunOptions::SetStopOnCrash (bool stop_on_crash) |
| 76 | { |
| 77 | m_opaque_up->SetStopOnCrash(stop_on_crash); |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | SBCommandInterpreterRunOptions::GetEchoCommands () const |
| 82 | { |
| 83 | return m_opaque_up->GetEchoCommands(); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | SBCommandInterpreterRunOptions::SetEchoCommands (bool echo_commands) |
| 88 | { |
| 89 | m_opaque_up->SetEchoCommands(echo_commands); |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | SBCommandInterpreterRunOptions::GetPrintResults () const |
| 94 | { |
| 95 | return m_opaque_up->GetPrintResults(); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | SBCommandInterpreterRunOptions::SetPrintResults (bool print_results) |
| 100 | { |
| 101 | m_opaque_up->SetPrintResults(print_results); |
| 102 | } |
| 103 | |
| 104 | bool |
| 105 | SBCommandInterpreterRunOptions::GetAddToHistory () const |
| 106 | { |
| 107 | return m_opaque_up->GetAddToHistory(); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | SBCommandInterpreterRunOptions::SetAddToHistory (bool add_to_history) |
| 112 | { |
| 113 | m_opaque_up->SetAddToHistory(add_to_history); |
| 114 | } |
| 115 | |
| 116 | lldb_private::CommandInterpreterRunOptions * |
| 117 | SBCommandInterpreterRunOptions::get () const |
| 118 | { |
| 119 | return m_opaque_up.get(); |
| 120 | } |
| 121 | |
| 122 | lldb_private::CommandInterpreterRunOptions & |
| 123 | SBCommandInterpreterRunOptions::ref () const |
| 124 | { |
| 125 | return *m_opaque_up.get(); |
| 126 | } |
| 127 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 128 | class CommandPluginInterfaceImplementation : public CommandObjectParsed |
| 129 | { |
| 130 | public: |
| 131 | CommandPluginInterfaceImplementation (CommandInterpreter &interpreter, |
| 132 | const char *name, |
| 133 | lldb::SBCommandPluginInterface* backend, |
| 134 | const char *help = NULL, |
| 135 | const char *syntax = NULL, |
| 136 | uint32_t flags = 0) : |
| 137 | CommandObjectParsed (interpreter, name, help, syntax, flags), |
| 138 | m_backend(backend) {} |
| 139 | |
| 140 | virtual bool |
Greg Clayton | 3a18e31 | 2012-10-08 22:41:53 +0000 | [diff] [blame] | 141 | IsRemovable() const { return true; } |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 142 | |
| 143 | protected: |
| 144 | virtual bool |
| 145 | DoExecute (Args& command, CommandReturnObject &result) |
| 146 | { |
| 147 | SBCommandReturnObject sb_return(&result); |
| 148 | SBCommandInterpreter sb_interpreter(&m_interpreter); |
| 149 | SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this()); |
| 150 | bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return); |
| 151 | sb_return.Release(); |
| 152 | return ret; |
| 153 | } |
| 154 | lldb::SBCommandPluginInterface* m_backend; |
| 155 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 157 | SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) : |
| 158 | m_opaque_ptr (interpreter) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 160 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 161 | |
| 162 | if (log) |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 163 | log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)" |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 164 | " => SBCommandInterpreter(%p)", |
| 165 | static_cast<void*>(interpreter), |
| 166 | static_cast<void*>(m_opaque_ptr)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 169 | SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) : |
| 170 | m_opaque_ptr (rhs.m_opaque_ptr) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | const SBCommandInterpreter & |
| 175 | SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs) |
| 176 | { |
| 177 | m_opaque_ptr = rhs.m_opaque_ptr; |
| 178 | return *this; |
| 179 | } |
| 180 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | SBCommandInterpreter::~SBCommandInterpreter () |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | bool |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 186 | SBCommandInterpreter::IsValid() const |
| 187 | { |
| 188 | return m_opaque_ptr != NULL; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | SBCommandInterpreter::CommandExists (const char *cmd) |
| 194 | { |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 195 | if (cmd && m_opaque_ptr) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 196 | return m_opaque_ptr->CommandExists (cmd); |
| 197 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool |
| 201 | SBCommandInterpreter::AliasExists (const char *cmd) |
| 202 | { |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 203 | if (cmd && m_opaque_ptr) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 204 | return m_opaque_ptr->AliasExists (cmd); |
| 205 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 208 | bool |
| 209 | SBCommandInterpreter::IsActive () |
| 210 | { |
| 211 | if (m_opaque_ptr) |
| 212 | return m_opaque_ptr->IsActive (); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | const char * |
| 217 | SBCommandInterpreter::GetIOHandlerControlSequence(char ch) |
| 218 | { |
| 219 | if (m_opaque_ptr) |
| 220 | return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString(); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | lldb::ReturnStatus |
| 225 | SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history) |
| 226 | { |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 227 | SBExecutionContext sb_exe_ctx; |
| 228 | return HandleCommand (command_line, sb_exe_ctx, result, add_to_history); |
| 229 | } |
| 230 | |
| 231 | lldb::ReturnStatus |
| 232 | SBCommandInterpreter::HandleCommand (const char *command_line, SBExecutionContext &override_context, SBCommandReturnObject &result, bool add_to_history) |
| 233 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 234 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 235 | |
| 236 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 237 | log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)", |
| 238 | static_cast<void*>(m_opaque_ptr), command_line, |
| 239 | static_cast<void*>(result.get()), add_to_history); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 240 | |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 241 | ExecutionContext ctx, *ctx_ptr; |
| 242 | if (override_context.get()) |
| 243 | { |
| 244 | ctx = override_context.get()->Lock(true); |
| 245 | ctx_ptr = &ctx; |
| 246 | } |
| 247 | else |
| 248 | ctx_ptr = nullptr; |
| 249 | |
| 250 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | result.Clear(); |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 252 | if (command_line && m_opaque_ptr) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 253 | { |
Greg Clayton | 45a44f3 | 2014-07-15 00:25:59 +0000 | [diff] [blame] | 254 | result.ref().SetInteractive(false); |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 255 | m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref(), ctx_ptr); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 259 | result->AppendError ("SBCommandInterpreter or the command line is not valid"); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 260 | result->SetStatus (eReturnStatusFailed); |
| 261 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 262 | |
Caroline Tice | 7b9da4a | 2010-10-27 21:23:37 +0000 | [diff] [blame] | 263 | // We need to get the value again, in case the command disabled the log! |
| 264 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 265 | if (log) |
| 266 | { |
| 267 | SBStream sstr; |
| 268 | result.GetDescription (sstr); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 269 | log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 270 | static_cast<void*>(m_opaque_ptr), command_line, |
| 271 | static_cast<void*>(result.get()), sstr.GetData(), |
| 272 | add_to_history, result.GetStatus()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | return result.GetStatus(); |
| 276 | } |
| 277 | |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 278 | void |
| 279 | SBCommandInterpreter::HandleCommandsFromFile (lldb::SBFileSpec &file, |
| 280 | lldb::SBExecutionContext &override_context, |
| 281 | lldb::SBCommandInterpreterRunOptions &options, |
| 282 | lldb::SBCommandReturnObject result) |
| 283 | { |
| 284 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 285 | |
| 286 | if (log) |
| 287 | { |
| 288 | SBStream s; |
| 289 | file.GetDescription (s); |
| 290 | log->Printf ("SBCommandInterpreter(%p)::HandleCommandsFromFile (file=\"%s\", SBCommandReturnObject(%p))", |
| 291 | static_cast<void*>(m_opaque_ptr), s.GetData(), |
| 292 | static_cast<void*>(result.get())); |
| 293 | } |
| 294 | |
| 295 | if (!m_opaque_ptr) |
| 296 | { |
| 297 | result->AppendError ("SBCommandInterpreter is not valid."); |
| 298 | result->SetStatus (eReturnStatusFailed); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (!file.IsValid()) |
| 303 | { |
| 304 | SBStream s; |
| 305 | file.GetDescription (s); |
| 306 | result->AppendErrorWithFormat ("File is not valid: %s.", s.GetData()); |
| 307 | result->SetStatus (eReturnStatusFailed); |
| 308 | } |
| 309 | |
| 310 | FileSpec tmp_spec = file.ref(); |
| 311 | ExecutionContext ctx, *ctx_ptr; |
| 312 | if (override_context.get()) |
| 313 | { |
| 314 | ctx = override_context.get()->Lock(true); |
| 315 | ctx_ptr = &ctx; |
| 316 | } |
| 317 | else |
| 318 | ctx_ptr = nullptr; |
| 319 | |
| 320 | |
| 321 | m_opaque_ptr->HandleCommandsFromFile (tmp_spec, ctx_ptr, options.ref(), result.ref()); |
| 322 | |
| 323 | } |
| 324 | |
| 325 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | int |
| 327 | SBCommandInterpreter::HandleCompletion (const char *current_line, |
| 328 | const char *cursor, |
| 329 | const char *last_char, |
| 330 | int match_start_point, |
| 331 | int max_return_elements, |
| 332 | SBStringList &matches) |
| 333 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 334 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 335 | int num_completions = 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 336 | |
Jim Ingham | ac01260 | 2011-12-05 19:24:15 +0000 | [diff] [blame] | 337 | // Sanity check the arguments that are passed in: |
| 338 | // cursor & last_char have to be within the current_line. |
| 339 | if (current_line == NULL || cursor == NULL || last_char == NULL) |
| 340 | return 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 341 | |
Jim Ingham | ac01260 | 2011-12-05 19:24:15 +0000 | [diff] [blame] | 342 | if (cursor < current_line || last_char < current_line) |
| 343 | return 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 344 | |
Jim Ingham | ac01260 | 2011-12-05 19:24:15 +0000 | [diff] [blame] | 345 | size_t current_line_size = strlen (current_line); |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 346 | if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) || |
| 347 | last_char - current_line > static_cast<ptrdiff_t>(current_line_size)) |
Jim Ingham | ac01260 | 2011-12-05 19:24:15 +0000 | [diff] [blame] | 348 | return 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 349 | |
Jim Ingham | 389512d | 2012-06-26 01:21:59 +0000 | [diff] [blame] | 350 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 351 | log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 352 | static_cast<void*>(m_opaque_ptr), current_line, |
| 353 | static_cast<uint64_t>(cursor - current_line), |
| 354 | static_cast<uint64_t>(last_char - current_line), |
| 355 | match_start_point, max_return_elements); |
| 356 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 357 | if (m_opaque_ptr) |
| 358 | { |
| 359 | lldb_private::StringList lldb_matches; |
| 360 | num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point, |
| 361 | max_return_elements, lldb_matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 362 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 363 | SBStringList temp_list (&lldb_matches); |
| 364 | matches.AppendList (temp_list); |
| 365 | } |
Jim Ingham | 389512d | 2012-06-26 01:21:59 +0000 | [diff] [blame] | 366 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 367 | log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", |
| 368 | static_cast<void*>(m_opaque_ptr), num_completions); |
| 369 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | return num_completions; |
| 371 | } |
| 372 | |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 373 | int |
| 374 | SBCommandInterpreter::HandleCompletion (const char *current_line, |
| 375 | uint32_t cursor_pos, |
| 376 | int match_start_point, |
| 377 | int max_return_elements, |
| 378 | lldb::SBStringList &matches) |
| 379 | { |
| 380 | const char *cursor = current_line + cursor_pos; |
| 381 | const char *last_char = current_line + strlen (current_line); |
| 382 | return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches); |
| 383 | } |
| 384 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | bool |
| 386 | SBCommandInterpreter::HasCommands () |
| 387 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 388 | if (m_opaque_ptr) |
| 389 | return m_opaque_ptr->HasCommands(); |
| 390 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | bool |
| 394 | SBCommandInterpreter::HasAliases () |
| 395 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 396 | if (m_opaque_ptr) |
| 397 | return m_opaque_ptr->HasAliases(); |
| 398 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 402 | SBCommandInterpreter::HasAliasOptions () |
| 403 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 404 | if (m_opaque_ptr) |
| 405 | return m_opaque_ptr->HasAliasOptions (); |
| 406 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | SBProcess |
| 410 | SBCommandInterpreter::GetProcess () |
| 411 | { |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 412 | SBProcess sb_process; |
| 413 | ProcessSP process_sp; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 414 | if (m_opaque_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 415 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 416 | TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); |
| 417 | if (target_sp) |
| 418 | { |
| 419 | Mutex::Locker api_locker(target_sp->GetAPIMutex()); |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 420 | process_sp = target_sp->GetProcessSP(); |
| 421 | sb_process.SetSP(process_sp); |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 422 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 423 | } |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 424 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 425 | |
| 426 | if (log) |
| 427 | log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 428 | static_cast<void*>(m_opaque_ptr), |
| 429 | static_cast<void*>(process_sp.get())); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 430 | |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 431 | return sb_process; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 434 | SBDebugger |
| 435 | SBCommandInterpreter::GetDebugger () |
| 436 | { |
| 437 | SBDebugger sb_debugger; |
| 438 | if (m_opaque_ptr) |
| 439 | sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this()); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 440 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 441 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 442 | if (log) |
| 443 | log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 444 | static_cast<void*>(m_opaque_ptr), |
| 445 | static_cast<void*>(sb_debugger.get())); |
| 446 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 447 | return sb_debugger; |
| 448 | } |
| 449 | |
Ilia K | acf28be | 2015-03-23 22:45:13 +0000 | [diff] [blame] | 450 | bool |
| 451 | SBCommandInterpreter::GetPromptOnQuit() |
| 452 | { |
| 453 | if (m_opaque_ptr) |
| 454 | return m_opaque_ptr->GetPromptOnQuit(); |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | void |
| 459 | SBCommandInterpreter::SetPromptOnQuit (bool b) |
| 460 | { |
| 461 | if (m_opaque_ptr) |
| 462 | m_opaque_ptr->SetPromptOnQuit(b); |
| 463 | } |
| 464 | |
Adrian McCarthy | 2304b6f | 2015-04-23 20:00:25 +0000 | [diff] [blame] | 465 | void |
| 466 | SBCommandInterpreter::ResolveCommand(const char *command_line, SBCommandReturnObject &result) |
| 467 | { |
| 468 | result.Clear(); |
| 469 | if (command_line && m_opaque_ptr) |
| 470 | { |
| 471 | m_opaque_ptr->ResolveCommand(command_line, result.ref()); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | result->AppendError("SBCommandInterpreter or the command line is not valid"); |
| 476 | result->SetStatus(eReturnStatusFailed); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 481 | CommandInterpreter * |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 482 | SBCommandInterpreter::get () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 483 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 484 | return m_opaque_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | CommandInterpreter & |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 488 | SBCommandInterpreter::ref () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 490 | assert (m_opaque_ptr); |
| 491 | return *m_opaque_ptr; |
| 492 | } |
| 493 | |
| 494 | void |
| 495 | SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter) |
| 496 | { |
| 497 | m_opaque_ptr = interpreter; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void |
| 501 | SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result) |
| 502 | { |
| 503 | result.Clear(); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 504 | if (m_opaque_ptr) |
| 505 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 506 | TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); |
| 507 | Mutex::Locker api_locker; |
| 508 | if (target_sp) |
Jim Ingham | 10ebffa | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 509 | api_locker.Lock(target_sp->GetAPIMutex()); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 510 | m_opaque_ptr->SourceInitFile (false, result.ref()); |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | result->AppendError ("SBCommandInterpreter is not valid"); |
| 515 | result->SetStatus (eReturnStatusFailed); |
| 516 | } |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 517 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 518 | |
| 519 | if (log) |
| 520 | log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 521 | static_cast<void*>(m_opaque_ptr), |
| 522 | static_cast<void*>(result.get())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void |
| 526 | SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result) |
| 527 | { |
| 528 | result.Clear(); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 529 | if (m_opaque_ptr) |
| 530 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 531 | TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); |
| 532 | Mutex::Locker api_locker; |
| 533 | if (target_sp) |
Jim Ingham | 10ebffa | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 534 | api_locker.Lock(target_sp->GetAPIMutex()); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 535 | m_opaque_ptr->SourceInitFile (true, result.ref()); |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | result->AppendError ("SBCommandInterpreter is not valid"); |
| 540 | result->SetStatus (eReturnStatusFailed); |
| 541 | } |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 542 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 543 | |
| 544 | if (log) |
| 545 | log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 546 | static_cast<void*>(m_opaque_ptr), |
| 547 | static_cast<void*>(result.get())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | SBBroadcaster |
| 551 | SBCommandInterpreter::GetBroadcaster () |
| 552 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 553 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 554 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 555 | SBBroadcaster broadcaster (m_opaque_ptr, false); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 556 | |
| 557 | if (log) |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 558 | log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 559 | static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get())); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 561 | return broadcaster; |
| 562 | } |
| 563 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 564 | const char * |
| 565 | SBCommandInterpreter::GetBroadcasterClass () |
| 566 | { |
Ilia K | 8a00a56 | 2015-03-17 16:54:52 +0000 | [diff] [blame] | 567 | return CommandInterpreter::GetStaticBroadcasterClass().AsCString(); |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 570 | const char * |
| 571 | SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type) |
| 572 | { |
| 573 | return CommandObject::GetArgumentTypeAsCString (arg_type); |
| 574 | } |
| 575 | |
| 576 | const char * |
| 577 | SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type) |
| 578 | { |
| 579 | return CommandObject::GetArgumentDescriptionAsCString (arg_type); |
| 580 | } |
| 581 | |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 582 | bool |
Ilia K | af10e1c | 2015-03-21 10:53:37 +0000 | [diff] [blame] | 583 | SBCommandInterpreter::EventIsCommandInterpreterEvent (const lldb::SBEvent &event) |
| 584 | { |
Ilia K | f9e5dc1 | 2015-03-21 11:11:07 +0000 | [diff] [blame] | 585 | return event.GetBroadcasterClass() == SBCommandInterpreter::GetBroadcasterClass(); |
Ilia K | af10e1c | 2015-03-21 10:53:37 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | bool |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 589 | SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name, |
| 590 | lldb::CommandOverrideCallback callback, |
| 591 | void *baton) |
| 592 | { |
| 593 | if (command_name && command_name[0] && m_opaque_ptr) |
| 594 | { |
Greg Clayton | af6f275 | 2012-05-08 04:29:20 +0000 | [diff] [blame] | 595 | std::string command_name_str (command_name); |
| 596 | CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str); |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 597 | if (cmd_obj) |
| 598 | { |
Greg Clayton | af6f275 | 2012-05-08 04:29:20 +0000 | [diff] [blame] | 599 | assert(command_name_str.empty()); |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 600 | cmd_obj->SetOverrideCallback (callback, baton); |
| 601 | return true; |
| 602 | } |
| 603 | } |
| 604 | return false; |
| 605 | } |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 606 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 607 | lldb::SBCommand |
| 608 | SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help) |
| 609 | { |
| 610 | CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help); |
| 611 | new_command->SetRemovable (true); |
| 612 | lldb::CommandObjectSP new_command_sp(new_command); |
| 613 | if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true)) |
| 614 | return lldb::SBCommand(new_command_sp); |
| 615 | return lldb::SBCommand(); |
| 616 | } |
| 617 | |
| 618 | lldb::SBCommand |
| 619 | SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help) |
| 620 | { |
| 621 | lldb::CommandObjectSP new_command_sp; |
| 622 | new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help)); |
| 623 | |
| 624 | if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true)) |
| 625 | return lldb::SBCommand(new_command_sp); |
| 626 | return lldb::SBCommand(); |
| 627 | } |
| 628 | |
| 629 | SBCommand::SBCommand () |
| 630 | {} |
| 631 | |
| 632 | SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp) |
| 633 | {} |
| 634 | |
| 635 | bool |
| 636 | SBCommand::IsValid () |
| 637 | { |
| 638 | return (bool)m_opaque_sp; |
| 639 | } |
| 640 | |
| 641 | const char* |
| 642 | SBCommand::GetName () |
| 643 | { |
| 644 | if (IsValid ()) |
| 645 | return m_opaque_sp->GetCommandName (); |
| 646 | return NULL; |
| 647 | } |
| 648 | |
| 649 | const char* |
| 650 | SBCommand::GetHelp () |
| 651 | { |
| 652 | if (IsValid ()) |
| 653 | return m_opaque_sp->GetHelp (); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
Enrico Granata | cc342da | 2015-03-13 22:32:11 +0000 | [diff] [blame] | 657 | const char* |
| 658 | SBCommand::GetHelpLong () |
| 659 | { |
| 660 | if (IsValid ()) |
| 661 | return m_opaque_sp->GetHelpLong (); |
| 662 | return NULL; |
| 663 | } |
| 664 | |
| 665 | void |
| 666 | SBCommand::SetHelp (const char* help) |
| 667 | { |
| 668 | if (IsValid()) |
| 669 | m_opaque_sp->SetHelp(help); |
| 670 | } |
| 671 | |
| 672 | void |
| 673 | SBCommand::SetHelpLong (const char* help) |
| 674 | { |
| 675 | if (IsValid()) |
| 676 | m_opaque_sp->SetHelpLong(help); |
| 677 | } |
| 678 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 679 | lldb::SBCommand |
| 680 | SBCommand::AddMultiwordCommand (const char* name, const char* help) |
| 681 | { |
| 682 | if (!IsValid ()) |
| 683 | return lldb::SBCommand(); |
| 684 | if (m_opaque_sp->IsMultiwordObject() == false) |
| 685 | return lldb::SBCommand(); |
| 686 | CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help); |
| 687 | new_command->SetRemovable (true); |
| 688 | lldb::CommandObjectSP new_command_sp(new_command); |
| 689 | if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp)) |
| 690 | return lldb::SBCommand(new_command_sp); |
| 691 | return lldb::SBCommand(); |
| 692 | } |
| 693 | |
| 694 | lldb::SBCommand |
| 695 | SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help) |
| 696 | { |
| 697 | if (!IsValid ()) |
| 698 | return lldb::SBCommand(); |
| 699 | if (m_opaque_sp->IsMultiwordObject() == false) |
| 700 | return lldb::SBCommand(); |
| 701 | lldb::CommandObjectSP new_command_sp; |
| 702 | new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help)); |
| 703 | if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp)) |
| 704 | return lldb::SBCommand(new_command_sp); |
| 705 | return lldb::SBCommand(); |
| 706 | } |
Enrico Granata | e87764f | 2015-05-27 05:04:35 +0000 | [diff] [blame^] | 707 | |
| 708 | uint32_t |
| 709 | SBCommand::GetFlags () |
| 710 | { |
| 711 | if (!IsValid()) |
| 712 | return 0; |
| 713 | return m_opaque_sp->GetFlags().Get(); |
| 714 | } |
| 715 | |
| 716 | void |
| 717 | SBCommand::SetFlags (uint32_t flags) |
| 718 | { |
| 719 | if (IsValid()) |
| 720 | m_opaque_sp->GetFlags().Set(flags); |
| 721 | } |