1 //===-- SBCommandInterpreter.cpp --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
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"
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"
30 using namespace lldb_private;
32 class CommandPluginInterfaceImplementation : public CommandObjectParsed
35 CommandPluginInterfaceImplementation (CommandInterpreter &interpreter,
37 lldb::SBCommandPluginInterface* backend,
38 const char *help = NULL,
39 const char *syntax = NULL,
41 CommandObjectParsed (interpreter, name, help, syntax, flags),
45 IsRemovable() const { return true; }
49 DoExecute (Args& command, CommandReturnObject &result)
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);
58 lldb::SBCommandPluginInterface* m_backend;
61 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
62 m_opaque_ptr (interpreter)
64 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
67 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
68 " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr);
71 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
72 m_opaque_ptr (rhs.m_opaque_ptr)
76 const SBCommandInterpreter &
77 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
79 m_opaque_ptr = rhs.m_opaque_ptr;
83 SBCommandInterpreter::~SBCommandInterpreter ()
88 SBCommandInterpreter::IsValid() const
90 return m_opaque_ptr != NULL;
95 SBCommandInterpreter::CommandExists (const char *cmd)
97 if (cmd && m_opaque_ptr)
98 return m_opaque_ptr->CommandExists (cmd);
103 SBCommandInterpreter::AliasExists (const char *cmd)
105 if (cmd && m_opaque_ptr)
106 return m_opaque_ptr->AliasExists (cmd);
111 SBCommandInterpreter::IsActive ()
114 return m_opaque_ptr->IsActive ();
119 SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
122 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
127 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
129 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
132 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
133 m_opaque_ptr, command_line, result.get(), add_to_history);
136 if (command_line && m_opaque_ptr)
138 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
142 result->AppendError ("SBCommandInterpreter or the command line is not valid");
143 result->SetStatus (eReturnStatusFailed);
146 // We need to get the value again, in case the command disabled the log!
147 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
151 result.GetDescription (sstr);
152 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
153 m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus());
156 return result.GetStatus();
160 SBCommandInterpreter::HandleCompletion (const char *current_line,
162 const char *last_char,
163 int match_start_point,
164 int max_return_elements,
165 SBStringList &matches)
167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
168 int num_completions = 0;
170 // Sanity check the arguments that are passed in:
171 // cursor & last_char have to be within the current_line.
172 if (current_line == NULL || cursor == NULL || last_char == NULL)
175 if (cursor < current_line || last_char < current_line)
178 size_t current_line_size = strlen (current_line);
179 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size)
183 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)",
184 m_opaque_ptr, current_line, (uint64_t) (cursor - current_line), (uint64_t) (last_char - current_line), match_start_point, max_return_elements);
188 lldb_private::StringList lldb_matches;
189 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
190 max_return_elements, lldb_matches);
192 SBStringList temp_list (&lldb_matches);
193 matches.AppendList (temp_list);
196 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", m_opaque_ptr, num_completions);
198 return num_completions;
202 SBCommandInterpreter::HandleCompletion (const char *current_line,
204 int match_start_point,
205 int max_return_elements,
206 lldb::SBStringList &matches)
208 const char *cursor = current_line + cursor_pos;
209 const char *last_char = current_line + strlen (current_line);
210 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
214 SBCommandInterpreter::HasCommands ()
217 return m_opaque_ptr->HasCommands();
222 SBCommandInterpreter::HasAliases ()
225 return m_opaque_ptr->HasAliases();
230 SBCommandInterpreter::HasAliasOptions ()
233 return m_opaque_ptr->HasAliasOptions ();
238 SBCommandInterpreter::GetProcess ()
240 SBProcess sb_process;
241 ProcessSP process_sp;
244 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
247 Mutex::Locker api_locker(target_sp->GetAPIMutex());
248 process_sp = target_sp->GetProcessSP();
249 sb_process.SetSP(process_sp);
252 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
255 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
256 m_opaque_ptr, process_sp.get());
263 SBCommandInterpreter::GetDebugger ()
265 SBDebugger sb_debugger;
267 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
268 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
271 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
272 m_opaque_ptr, sb_debugger.get());
279 SBCommandInterpreter::get ()
285 SBCommandInterpreter::ref ()
287 assert (m_opaque_ptr);
288 return *m_opaque_ptr;
292 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
294 m_opaque_ptr = interpreter;
298 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
303 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
304 Mutex::Locker api_locker;
306 api_locker.Lock(target_sp->GetAPIMutex());
307 m_opaque_ptr->SourceInitFile (false, result.ref());
311 result->AppendError ("SBCommandInterpreter is not valid");
312 result->SetStatus (eReturnStatusFailed);
314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
317 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
318 m_opaque_ptr, result.get());
323 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
328 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
329 Mutex::Locker api_locker;
331 api_locker.Lock(target_sp->GetAPIMutex());
332 m_opaque_ptr->SourceInitFile (true, result.ref());
336 result->AppendError ("SBCommandInterpreter is not valid");
337 result->SetStatus (eReturnStatusFailed);
339 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
342 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
343 m_opaque_ptr, result.get());
347 SBCommandInterpreter::GetBroadcaster ()
349 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
351 SBBroadcaster broadcaster (m_opaque_ptr, false);
354 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
355 m_opaque_ptr, broadcaster.get());
361 SBCommandInterpreter::GetBroadcasterClass ()
363 return Communication::GetStaticBroadcasterClass().AsCString();
367 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
369 return CommandObject::GetArgumentTypeAsCString (arg_type);
373 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
375 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
379 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
380 lldb::CommandOverrideCallback callback,
383 if (command_name && command_name[0] && m_opaque_ptr)
385 std::string command_name_str (command_name);
386 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
389 assert(command_name_str.empty());
390 cmd_obj->SetOverrideCallback (callback, baton);
397 #ifndef LLDB_DISABLE_PYTHON
399 // Defined in the SWIG source file
403 // these are the Pythonic implementations of the required callbacks
404 // these are scripting-language specific, which is why they belong here
405 // we still need to use function pointers to them instead of relying
406 // on linkage-time resolution because the SWIG stuff and this file
407 // get built at different times
409 LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
410 const char *session_dictionary_name,
411 const lldb::StackFrameSP& sb_frame,
412 const lldb::BreakpointLocationSP& sb_bp_loc);
415 LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
416 const char *session_dictionary_name,
417 const lldb::StackFrameSP& sb_frame,
418 const lldb::WatchpointSP& sb_wp);
421 LLDBSwigPythonCallTypeScript (const char *python_function_name,
422 void *session_dictionary,
423 const lldb::ValueObjectSP& valobj_sp,
424 void** pyfunct_wrapper,
425 std::string& retval);
428 LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
429 const char *session_dictionary_name,
430 const lldb::ValueObjectSP& valobj_sp);
434 LLDBSwigPython_CalculateNumChildren (void *implementor);
437 LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
440 LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
443 LLDBSWIGPython_CastPyObjectToSBValue (void* data);
445 extern lldb::ValueObjectSP
446 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
449 LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
452 LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
455 LLDBSwigPythonCallCommand (const char *python_function_name,
456 const char *session_dictionary_name,
457 lldb::DebuggerSP& debugger,
459 lldb_private::CommandReturnObject &cmd_retobj);
462 LLDBSwigPythonCallModuleInit (const char *python_module_name,
463 const char *session_dictionary_name,
464 lldb::DebuggerSP& debugger);
467 LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
468 const char *session_dictionary_name,
469 const lldb::ProcessSP& process_sp);
472 LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
473 const char* session_dictionary_name,
474 lldb::ProcessSP& process,
475 std::string& output);
478 LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
479 const char* session_dictionary_name,
480 lldb::ThreadSP& thread,
481 std::string& output);
484 LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
485 const char* session_dictionary_name,
486 lldb::TargetSP& target,
487 std::string& output);
490 LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
491 const char* session_dictionary_name,
492 lldb::StackFrameSP& frame,
493 std::string& output);
496 LLDBSWIGPython_GetDynamicSetting (void* module,
498 const lldb::TargetSP& target_sp);
504 SBCommandInterpreter::InitializeSWIG ()
506 static bool g_initialized = false;
509 g_initialized = true;
510 #ifndef LLDB_DISABLE_PYTHON
511 ScriptInterpreter::InitializeInterpreter (init_lldb,
512 LLDBSwigPythonBreakpointCallbackFunction,
513 LLDBSwigPythonWatchpointCallbackFunction,
514 LLDBSwigPythonCallTypeScript,
515 LLDBSwigPythonCreateSyntheticProvider,
516 LLDBSwigPython_CalculateNumChildren,
517 LLDBSwigPython_GetChildAtIndex,
518 LLDBSwigPython_GetIndexOfChildWithName,
519 LLDBSWIGPython_CastPyObjectToSBValue,
520 LLDBSWIGPython_GetValueObjectSPFromSBValue,
521 LLDBSwigPython_UpdateSynthProviderInstance,
522 LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
523 LLDBSwigPythonCallCommand,
524 LLDBSwigPythonCallModuleInit,
525 LLDBSWIGPythonCreateOSPlugin,
526 LLDBSWIGPythonRunScriptKeywordProcess,
527 LLDBSWIGPythonRunScriptKeywordThread,
528 LLDBSWIGPythonRunScriptKeywordTarget,
529 LLDBSWIGPythonRunScriptKeywordFrame,
530 LLDBSWIGPython_GetDynamicSetting);
536 SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
538 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
539 new_command->SetRemovable (true);
540 lldb::CommandObjectSP new_command_sp(new_command);
541 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
542 return lldb::SBCommand(new_command_sp);
543 return lldb::SBCommand();
547 SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
549 lldb::CommandObjectSP new_command_sp;
550 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
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();
557 SBCommand::SBCommand ()
560 SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
564 SBCommand::IsValid ()
566 return (bool)m_opaque_sp;
570 SBCommand::GetName ()
573 return m_opaque_sp->GetCommandName ();
578 SBCommand::GetHelp ()
581 return m_opaque_sp->GetHelp ();
586 SBCommand::AddMultiwordCommand (const char* name, const char* help)
589 return lldb::SBCommand();
590 if (m_opaque_sp->IsMultiwordObject() == false)
591 return lldb::SBCommand();
592 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
593 new_command->SetRemovable (true);
594 lldb::CommandObjectSP new_command_sp(new_command);
595 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
596 return lldb::SBCommand(new_command_sp);
597 return lldb::SBCommand();
601 SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
604 return lldb::SBCommand();
605 if (m_opaque_sp->IsMultiwordObject() == false)
606 return lldb::SBCommand();
607 lldb::CommandObjectSP new_command_sp;
608 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
609 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
610 return lldb::SBCommand(new_command_sp);
611 return lldb::SBCommand();