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 10 #include "lldb/lldb-python.h" 11 12 #include "lldb/lldb-types.h" 13 #include "lldb/Core/SourceManager.h" 14 #include "lldb/Core/Listener.h" 15 #include "lldb/Interpreter/CommandInterpreter.h" 16 #include "lldb/Interpreter/CommandObjectMultiword.h" 17 #include "lldb/Interpreter/CommandReturnObject.h" 18 #include "lldb/Target/Target.h" 19 20 #include "lldb/API/SBBroadcaster.h" 21 #include "lldb/API/SBCommandReturnObject.h" 22 #include "lldb/API/SBCommandInterpreter.h" 23 #include "lldb/API/SBProcess.h" 24 #include "lldb/API/SBTarget.h" 25 #include "lldb/API/SBListener.h" 26 #include "lldb/API/SBStream.h" 27 #include "lldb/API/SBStringList.h" 28 29 using namespace lldb; 30 using namespace lldb_private; 31 32 class CommandPluginInterfaceImplementation : public CommandObjectParsed 33 { 34 public: 35 CommandPluginInterfaceImplementation (CommandInterpreter &interpreter, 36 const char *name, 37 lldb::SBCommandPluginInterface* backend, 38 const char *help = NULL, 39 const char *syntax = NULL, 40 uint32_t flags = 0) : 41 CommandObjectParsed (interpreter, name, help, syntax, flags), 42 m_backend(backend) {} 43 44 virtual bool 45 IsRemovable() const { return true; } 46 47 protected: 48 virtual bool 49 DoExecute (Args& command, CommandReturnObject &result) 50 { 51 SBCommandReturnObject sb_return(&result); 52 SBCommandInterpreter sb_interpreter(&m_interpreter); 53 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this()); 54 bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return); 55 sb_return.Release(); 56 return ret; 57 } 58 lldb::SBCommandPluginInterface* m_backend; 59 }; 60 61 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) : 62 m_opaque_ptr (interpreter) 63 { 64 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 65 66 if (log) 67 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)" 68 " => SBCommandInterpreter(%p)", 69 static_cast<void*>(interpreter), 70 static_cast<void*>(m_opaque_ptr)); 71 } 72 73 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) : 74 m_opaque_ptr (rhs.m_opaque_ptr) 75 { 76 } 77 78 const SBCommandInterpreter & 79 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs) 80 { 81 m_opaque_ptr = rhs.m_opaque_ptr; 82 return *this; 83 } 84 85 SBCommandInterpreter::~SBCommandInterpreter () 86 { 87 } 88 89 bool 90 SBCommandInterpreter::IsValid() const 91 { 92 return m_opaque_ptr != NULL; 93 } 94 95 96 bool 97 SBCommandInterpreter::CommandExists (const char *cmd) 98 { 99 if (cmd && m_opaque_ptr) 100 return m_opaque_ptr->CommandExists (cmd); 101 return false; 102 } 103 104 bool 105 SBCommandInterpreter::AliasExists (const char *cmd) 106 { 107 if (cmd && m_opaque_ptr) 108 return m_opaque_ptr->AliasExists (cmd); 109 return false; 110 } 111 112 bool 113 SBCommandInterpreter::IsActive () 114 { 115 if (m_opaque_ptr) 116 return m_opaque_ptr->IsActive (); 117 return false; 118 } 119 120 const char * 121 SBCommandInterpreter::GetIOHandlerControlSequence(char ch) 122 { 123 if (m_opaque_ptr) 124 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString(); 125 return NULL; 126 } 127 128 lldb::ReturnStatus 129 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history) 130 { 131 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 132 133 if (log) 134 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)", 135 static_cast<void*>(m_opaque_ptr), command_line, 136 static_cast<void*>(result.get()), add_to_history); 137 138 result.Clear(); 139 if (command_line && m_opaque_ptr) 140 { 141 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref()); 142 } 143 else 144 { 145 result->AppendError ("SBCommandInterpreter or the command line is not valid"); 146 result->SetStatus (eReturnStatusFailed); 147 } 148 149 // We need to get the value again, in case the command disabled the log! 150 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); 151 if (log) 152 { 153 SBStream sstr; 154 result.GetDescription (sstr); 155 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i", 156 static_cast<void*>(m_opaque_ptr), command_line, 157 static_cast<void*>(result.get()), sstr.GetData(), 158 add_to_history, result.GetStatus()); 159 } 160 161 return result.GetStatus(); 162 } 163 164 int 165 SBCommandInterpreter::HandleCompletion (const char *current_line, 166 const char *cursor, 167 const char *last_char, 168 int match_start_point, 169 int max_return_elements, 170 SBStringList &matches) 171 { 172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 173 int num_completions = 0; 174 175 // Sanity check the arguments that are passed in: 176 // cursor & last_char have to be within the current_line. 177 if (current_line == NULL || cursor == NULL || last_char == NULL) 178 return 0; 179 180 if (cursor < current_line || last_char < current_line) 181 return 0; 182 183 size_t current_line_size = strlen (current_line); 184 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) || 185 last_char - current_line > static_cast<ptrdiff_t>(current_line_size)) 186 return 0; 187 188 if (log) 189 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)", 190 static_cast<void*>(m_opaque_ptr), current_line, 191 static_cast<uint64_t>(cursor - current_line), 192 static_cast<uint64_t>(last_char - current_line), 193 match_start_point, max_return_elements); 194 195 if (m_opaque_ptr) 196 { 197 lldb_private::StringList lldb_matches; 198 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point, 199 max_return_elements, lldb_matches); 200 201 SBStringList temp_list (&lldb_matches); 202 matches.AppendList (temp_list); 203 } 204 if (log) 205 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", 206 static_cast<void*>(m_opaque_ptr), num_completions); 207 208 return num_completions; 209 } 210 211 int 212 SBCommandInterpreter::HandleCompletion (const char *current_line, 213 uint32_t cursor_pos, 214 int match_start_point, 215 int max_return_elements, 216 lldb::SBStringList &matches) 217 { 218 const char *cursor = current_line + cursor_pos; 219 const char *last_char = current_line + strlen (current_line); 220 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches); 221 } 222 223 bool 224 SBCommandInterpreter::HasCommands () 225 { 226 if (m_opaque_ptr) 227 return m_opaque_ptr->HasCommands(); 228 return false; 229 } 230 231 bool 232 SBCommandInterpreter::HasAliases () 233 { 234 if (m_opaque_ptr) 235 return m_opaque_ptr->HasAliases(); 236 return false; 237 } 238 239 bool 240 SBCommandInterpreter::HasAliasOptions () 241 { 242 if (m_opaque_ptr) 243 return m_opaque_ptr->HasAliasOptions (); 244 return false; 245 } 246 247 SBProcess 248 SBCommandInterpreter::GetProcess () 249 { 250 SBProcess sb_process; 251 ProcessSP process_sp; 252 if (m_opaque_ptr) 253 { 254 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 255 if (target_sp) 256 { 257 Mutex::Locker api_locker(target_sp->GetAPIMutex()); 258 process_sp = target_sp->GetProcessSP(); 259 sb_process.SetSP(process_sp); 260 } 261 } 262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 263 264 if (log) 265 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)", 266 static_cast<void*>(m_opaque_ptr), 267 static_cast<void*>(process_sp.get())); 268 269 return sb_process; 270 } 271 272 SBDebugger 273 SBCommandInterpreter::GetDebugger () 274 { 275 SBDebugger sb_debugger; 276 if (m_opaque_ptr) 277 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this()); 278 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 279 280 if (log) 281 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)", 282 static_cast<void*>(m_opaque_ptr), 283 static_cast<void*>(sb_debugger.get())); 284 285 return sb_debugger; 286 } 287 288 CommandInterpreter * 289 SBCommandInterpreter::get () 290 { 291 return m_opaque_ptr; 292 } 293 294 CommandInterpreter & 295 SBCommandInterpreter::ref () 296 { 297 assert (m_opaque_ptr); 298 return *m_opaque_ptr; 299 } 300 301 void 302 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter) 303 { 304 m_opaque_ptr = interpreter; 305 } 306 307 void 308 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result) 309 { 310 result.Clear(); 311 if (m_opaque_ptr) 312 { 313 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 314 Mutex::Locker api_locker; 315 if (target_sp) 316 api_locker.Lock(target_sp->GetAPIMutex()); 317 m_opaque_ptr->SourceInitFile (false, result.ref()); 318 } 319 else 320 { 321 result->AppendError ("SBCommandInterpreter is not valid"); 322 result->SetStatus (eReturnStatusFailed); 323 } 324 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 325 326 if (log) 327 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))", 328 static_cast<void*>(m_opaque_ptr), 329 static_cast<void*>(result.get())); 330 } 331 332 void 333 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result) 334 { 335 result.Clear(); 336 if (m_opaque_ptr) 337 { 338 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 339 Mutex::Locker api_locker; 340 if (target_sp) 341 api_locker.Lock(target_sp->GetAPIMutex()); 342 m_opaque_ptr->SourceInitFile (true, result.ref()); 343 } 344 else 345 { 346 result->AppendError ("SBCommandInterpreter is not valid"); 347 result->SetStatus (eReturnStatusFailed); 348 } 349 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 350 351 if (log) 352 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))", 353 static_cast<void*>(m_opaque_ptr), 354 static_cast<void*>(result.get())); 355 } 356 357 SBBroadcaster 358 SBCommandInterpreter::GetBroadcaster () 359 { 360 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 361 362 SBBroadcaster broadcaster (m_opaque_ptr, false); 363 364 if (log) 365 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)", 366 static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get())); 367 368 return broadcaster; 369 } 370 371 const char * 372 SBCommandInterpreter::GetBroadcasterClass () 373 { 374 return Communication::GetStaticBroadcasterClass().AsCString(); 375 } 376 377 const char * 378 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type) 379 { 380 return CommandObject::GetArgumentTypeAsCString (arg_type); 381 } 382 383 const char * 384 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type) 385 { 386 return CommandObject::GetArgumentDescriptionAsCString (arg_type); 387 } 388 389 bool 390 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name, 391 lldb::CommandOverrideCallback callback, 392 void *baton) 393 { 394 if (command_name && command_name[0] && m_opaque_ptr) 395 { 396 std::string command_name_str (command_name); 397 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str); 398 if (cmd_obj) 399 { 400 assert(command_name_str.empty()); 401 cmd_obj->SetOverrideCallback (callback, baton); 402 return true; 403 } 404 } 405 return false; 406 } 407 408 #ifndef LLDB_DISABLE_PYTHON 409 410 // Defined in the SWIG source file 411 extern "C" void 412 init_lldb(void); 413 414 // these are the Pythonic implementations of the required callbacks 415 // these are scripting-language specific, which is why they belong here 416 // we still need to use function pointers to them instead of relying 417 // on linkage-time resolution because the SWIG stuff and this file 418 // get built at different times 419 extern "C" bool 420 LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name, 421 const char *session_dictionary_name, 422 const lldb::StackFrameSP& sb_frame, 423 const lldb::BreakpointLocationSP& sb_bp_loc); 424 425 extern "C" bool 426 LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name, 427 const char *session_dictionary_name, 428 const lldb::StackFrameSP& sb_frame, 429 const lldb::WatchpointSP& sb_wp); 430 431 extern "C" bool 432 LLDBSwigPythonCallTypeScript (const char *python_function_name, 433 void *session_dictionary, 434 const lldb::ValueObjectSP& valobj_sp, 435 void** pyfunct_wrapper, 436 std::string& retval); 437 438 extern "C" void* 439 LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name, 440 const char *session_dictionary_name, 441 const lldb::ValueObjectSP& valobj_sp); 442 443 444 extern "C" uint32_t 445 LLDBSwigPython_CalculateNumChildren (void *implementor); 446 447 extern "C" void * 448 LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx); 449 450 extern "C" int 451 LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name); 452 453 extern "C" void * 454 LLDBSWIGPython_CastPyObjectToSBValue (void* data); 455 456 extern lldb::ValueObjectSP 457 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data); 458 459 extern "C" bool 460 LLDBSwigPython_UpdateSynthProviderInstance (void* implementor); 461 462 extern "C" bool 463 LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor); 464 465 extern "C" bool 466 LLDBSwigPythonCallCommand (const char *python_function_name, 467 const char *session_dictionary_name, 468 lldb::DebuggerSP& debugger, 469 const char* args, 470 lldb_private::CommandReturnObject &cmd_retobj); 471 472 extern "C" bool 473 LLDBSwigPythonCallModuleInit (const char *python_module_name, 474 const char *session_dictionary_name, 475 lldb::DebuggerSP& debugger); 476 477 extern "C" void* 478 LLDBSWIGPythonCreateOSPlugin (const char *python_class_name, 479 const char *session_dictionary_name, 480 const lldb::ProcessSP& process_sp); 481 482 extern "C" bool 483 LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name, 484 const char* session_dictionary_name, 485 lldb::ProcessSP& process, 486 std::string& output); 487 488 extern "C" bool 489 LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name, 490 const char* session_dictionary_name, 491 lldb::ThreadSP& thread, 492 std::string& output); 493 494 extern "C" bool 495 LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name, 496 const char* session_dictionary_name, 497 lldb::TargetSP& target, 498 std::string& output); 499 500 extern "C" bool 501 LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name, 502 const char* session_dictionary_name, 503 lldb::StackFrameSP& frame, 504 std::string& output); 505 506 extern "C" void* 507 LLDBSWIGPython_GetDynamicSetting (void* module, 508 const char* setting, 509 const lldb::TargetSP& target_sp); 510 511 512 #endif 513 514 void 515 SBCommandInterpreter::InitializeSWIG () 516 { 517 static bool g_initialized = false; 518 if (!g_initialized) 519 { 520 g_initialized = true; 521 #ifndef LLDB_DISABLE_PYTHON 522 ScriptInterpreter::InitializeInterpreter (init_lldb, 523 LLDBSwigPythonBreakpointCallbackFunction, 524 LLDBSwigPythonWatchpointCallbackFunction, 525 LLDBSwigPythonCallTypeScript, 526 LLDBSwigPythonCreateSyntheticProvider, 527 LLDBSwigPython_CalculateNumChildren, 528 LLDBSwigPython_GetChildAtIndex, 529 LLDBSwigPython_GetIndexOfChildWithName, 530 LLDBSWIGPython_CastPyObjectToSBValue, 531 LLDBSWIGPython_GetValueObjectSPFromSBValue, 532 LLDBSwigPython_UpdateSynthProviderInstance, 533 LLDBSwigPython_MightHaveChildrenSynthProviderInstance, 534 LLDBSwigPythonCallCommand, 535 LLDBSwigPythonCallModuleInit, 536 LLDBSWIGPythonCreateOSPlugin, 537 LLDBSWIGPythonRunScriptKeywordProcess, 538 LLDBSWIGPythonRunScriptKeywordThread, 539 LLDBSWIGPythonRunScriptKeywordTarget, 540 LLDBSWIGPythonRunScriptKeywordFrame, 541 LLDBSWIGPython_GetDynamicSetting); 542 #endif 543 } 544 } 545 546 lldb::SBCommand 547 SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help) 548 { 549 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help); 550 new_command->SetRemovable (true); 551 lldb::CommandObjectSP new_command_sp(new_command); 552 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true)) 553 return lldb::SBCommand(new_command_sp); 554 return lldb::SBCommand(); 555 } 556 557 lldb::SBCommand 558 SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help) 559 { 560 lldb::CommandObjectSP new_command_sp; 561 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help)); 562 563 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true)) 564 return lldb::SBCommand(new_command_sp); 565 return lldb::SBCommand(); 566 } 567 568 SBCommand::SBCommand () 569 {} 570 571 SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp) 572 {} 573 574 bool 575 SBCommand::IsValid () 576 { 577 return (bool)m_opaque_sp; 578 } 579 580 const char* 581 SBCommand::GetName () 582 { 583 if (IsValid ()) 584 return m_opaque_sp->GetCommandName (); 585 return NULL; 586 } 587 588 const char* 589 SBCommand::GetHelp () 590 { 591 if (IsValid ()) 592 return m_opaque_sp->GetHelp (); 593 return NULL; 594 } 595 596 lldb::SBCommand 597 SBCommand::AddMultiwordCommand (const char* name, const char* help) 598 { 599 if (!IsValid ()) 600 return lldb::SBCommand(); 601 if (m_opaque_sp->IsMultiwordObject() == false) 602 return lldb::SBCommand(); 603 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help); 604 new_command->SetRemovable (true); 605 lldb::CommandObjectSP new_command_sp(new_command); 606 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp)) 607 return lldb::SBCommand(new_command_sp); 608 return lldb::SBCommand(); 609 } 610 611 lldb::SBCommand 612 SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help) 613 { 614 if (!IsValid ()) 615 return lldb::SBCommand(); 616 if (m_opaque_sp->IsMultiwordObject() == false) 617 return lldb::SBCommand(); 618 lldb::CommandObjectSP new_command_sp; 619 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help)); 620 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp)) 621 return lldb::SBCommand(new_command_sp); 622 return lldb::SBCommand(); 623 } 624 625