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)",
69 static_cast<void*>(interpreter),
70 static_cast<void*>(m_opaque_ptr));
73 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
74 m_opaque_ptr (rhs.m_opaque_ptr)
78 const SBCommandInterpreter &
79 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
81 m_opaque_ptr = rhs.m_opaque_ptr;
85 SBCommandInterpreter::~SBCommandInterpreter ()
90 SBCommandInterpreter::IsValid() const
92 return m_opaque_ptr != NULL;
97 SBCommandInterpreter::CommandExists (const char *cmd)
99 if (cmd && m_opaque_ptr)
100 return m_opaque_ptr->CommandExists (cmd);
105 SBCommandInterpreter::AliasExists (const char *cmd)
107 if (cmd && m_opaque_ptr)
108 return m_opaque_ptr->AliasExists (cmd);
113 SBCommandInterpreter::IsActive ()
116 return m_opaque_ptr->IsActive ();
121 SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
124 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
129 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
131 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
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);
139 if (command_line && m_opaque_ptr)
141 result.ref().SetInteractive(false);
142 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
146 result->AppendError ("SBCommandInterpreter or the command line is not valid");
147 result->SetStatus (eReturnStatusFailed);
150 // We need to get the value again, in case the command disabled the log!
151 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
155 result.GetDescription (sstr);
156 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
157 static_cast<void*>(m_opaque_ptr), command_line,
158 static_cast<void*>(result.get()), sstr.GetData(),
159 add_to_history, result.GetStatus());
162 return result.GetStatus();
166 SBCommandInterpreter::HandleCompletion (const char *current_line,
168 const char *last_char,
169 int match_start_point,
170 int max_return_elements,
171 SBStringList &matches)
173 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
174 int num_completions = 0;
176 // Sanity check the arguments that are passed in:
177 // cursor & last_char have to be within the current_line.
178 if (current_line == NULL || cursor == NULL || last_char == NULL)
181 if (cursor < current_line || last_char < current_line)
184 size_t current_line_size = strlen (current_line);
185 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
186 last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
190 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)",
191 static_cast<void*>(m_opaque_ptr), current_line,
192 static_cast<uint64_t>(cursor - current_line),
193 static_cast<uint64_t>(last_char - current_line),
194 match_start_point, max_return_elements);
198 lldb_private::StringList lldb_matches;
199 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
200 max_return_elements, lldb_matches);
202 SBStringList temp_list (&lldb_matches);
203 matches.AppendList (temp_list);
206 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
207 static_cast<void*>(m_opaque_ptr), num_completions);
209 return num_completions;
213 SBCommandInterpreter::HandleCompletion (const char *current_line,
215 int match_start_point,
216 int max_return_elements,
217 lldb::SBStringList &matches)
219 const char *cursor = current_line + cursor_pos;
220 const char *last_char = current_line + strlen (current_line);
221 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
225 SBCommandInterpreter::HasCommands ()
228 return m_opaque_ptr->HasCommands();
233 SBCommandInterpreter::HasAliases ()
236 return m_opaque_ptr->HasAliases();
241 SBCommandInterpreter::HasAliasOptions ()
244 return m_opaque_ptr->HasAliasOptions ();
249 SBCommandInterpreter::GetProcess ()
251 SBProcess sb_process;
252 ProcessSP process_sp;
255 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
258 Mutex::Locker api_locker(target_sp->GetAPIMutex());
259 process_sp = target_sp->GetProcessSP();
260 sb_process.SetSP(process_sp);
263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
266 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
267 static_cast<void*>(m_opaque_ptr),
268 static_cast<void*>(process_sp.get()));
274 SBCommandInterpreter::GetDebugger ()
276 SBDebugger sb_debugger;
278 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
279 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
282 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
283 static_cast<void*>(m_opaque_ptr),
284 static_cast<void*>(sb_debugger.get()));
290 SBCommandInterpreter::get ()
296 SBCommandInterpreter::ref ()
298 assert (m_opaque_ptr);
299 return *m_opaque_ptr;
303 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
305 m_opaque_ptr = interpreter;
309 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
314 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
315 Mutex::Locker api_locker;
317 api_locker.Lock(target_sp->GetAPIMutex());
318 m_opaque_ptr->SourceInitFile (false, result.ref());
322 result->AppendError ("SBCommandInterpreter is not valid");
323 result->SetStatus (eReturnStatusFailed);
325 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
328 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
329 static_cast<void*>(m_opaque_ptr),
330 static_cast<void*>(result.get()));
334 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
339 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
340 Mutex::Locker api_locker;
342 api_locker.Lock(target_sp->GetAPIMutex());
343 m_opaque_ptr->SourceInitFile (true, result.ref());
347 result->AppendError ("SBCommandInterpreter is not valid");
348 result->SetStatus (eReturnStatusFailed);
350 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
353 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
354 static_cast<void*>(m_opaque_ptr),
355 static_cast<void*>(result.get()));
359 SBCommandInterpreter::GetBroadcaster ()
361 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
363 SBBroadcaster broadcaster (m_opaque_ptr, false);
366 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
367 static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get()));
373 SBCommandInterpreter::GetBroadcasterClass ()
375 return Communication::GetStaticBroadcasterClass().AsCString();
379 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
381 return CommandObject::GetArgumentTypeAsCString (arg_type);
385 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
387 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
391 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
392 lldb::CommandOverrideCallback callback,
395 if (command_name && command_name[0] && m_opaque_ptr)
397 std::string command_name_str (command_name);
398 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
401 assert(command_name_str.empty());
402 cmd_obj->SetOverrideCallback (callback, baton);
409 #ifndef LLDB_DISABLE_PYTHON
411 // Defined in the SWIG source file
415 // these are the Pythonic implementations of the required callbacks
416 // these are scripting-language specific, which is why they belong here
417 // we still need to use function pointers to them instead of relying
418 // on linkage-time resolution because the SWIG stuff and this file
419 // get built at different times
421 LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
422 const char *session_dictionary_name,
423 const lldb::StackFrameSP& sb_frame,
424 const lldb::BreakpointLocationSP& sb_bp_loc);
427 LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
428 const char *session_dictionary_name,
429 const lldb::StackFrameSP& sb_frame,
430 const lldb::WatchpointSP& sb_wp);
433 LLDBSwigPythonCallTypeScript (const char *python_function_name,
434 void *session_dictionary,
435 const lldb::ValueObjectSP& valobj_sp,
436 void** pyfunct_wrapper,
437 std::string& retval);
440 LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
441 const char *session_dictionary_name,
442 const lldb::ValueObjectSP& valobj_sp);
446 LLDBSwigPython_CalculateNumChildren (void *implementor);
449 LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
452 LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
455 LLDBSWIGPython_CastPyObjectToSBValue (void* data);
457 extern lldb::ValueObjectSP
458 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
461 LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
464 LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
467 LLDBSwigPythonCallCommand (const char *python_function_name,
468 const char *session_dictionary_name,
469 lldb::DebuggerSP& debugger,
471 lldb_private::CommandReturnObject &cmd_retobj);
474 LLDBSwigPythonCallModuleInit (const char *python_module_name,
475 const char *session_dictionary_name,
476 lldb::DebuggerSP& debugger);
479 LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
480 const char *session_dictionary_name,
481 const lldb::ProcessSP& process_sp);
484 LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
485 const char* session_dictionary_name,
486 lldb::ProcessSP& process,
487 std::string& output);
490 LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
491 const char* session_dictionary_name,
492 lldb::ThreadSP& thread,
493 std::string& output);
496 LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
497 const char* session_dictionary_name,
498 lldb::TargetSP& target,
499 std::string& output);
502 LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
503 const char* session_dictionary_name,
504 lldb::StackFrameSP& frame,
505 std::string& output);
508 LLDBSWIGPython_GetDynamicSetting (void* module,
510 const lldb::TargetSP& target_sp);
516 SBCommandInterpreter::InitializeSWIG ()
518 static bool g_initialized = false;
521 g_initialized = true;
522 #ifndef LLDB_DISABLE_PYTHON
523 ScriptInterpreter::InitializeInterpreter (init_lldb,
524 LLDBSwigPythonBreakpointCallbackFunction,
525 LLDBSwigPythonWatchpointCallbackFunction,
526 LLDBSwigPythonCallTypeScript,
527 LLDBSwigPythonCreateSyntheticProvider,
528 LLDBSwigPython_CalculateNumChildren,
529 LLDBSwigPython_GetChildAtIndex,
530 LLDBSwigPython_GetIndexOfChildWithName,
531 LLDBSWIGPython_CastPyObjectToSBValue,
532 LLDBSWIGPython_GetValueObjectSPFromSBValue,
533 LLDBSwigPython_UpdateSynthProviderInstance,
534 LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
535 LLDBSwigPythonCallCommand,
536 LLDBSwigPythonCallModuleInit,
537 LLDBSWIGPythonCreateOSPlugin,
538 LLDBSWIGPythonRunScriptKeywordProcess,
539 LLDBSWIGPythonRunScriptKeywordThread,
540 LLDBSWIGPythonRunScriptKeywordTarget,
541 LLDBSWIGPythonRunScriptKeywordFrame,
542 LLDBSWIGPython_GetDynamicSetting);
548 SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
550 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
551 new_command->SetRemovable (true);
552 lldb::CommandObjectSP new_command_sp(new_command);
553 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
554 return lldb::SBCommand(new_command_sp);
555 return lldb::SBCommand();
559 SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
561 lldb::CommandObjectSP new_command_sp;
562 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
564 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
565 return lldb::SBCommand(new_command_sp);
566 return lldb::SBCommand();
569 SBCommand::SBCommand ()
572 SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
576 SBCommand::IsValid ()
578 return (bool)m_opaque_sp;
582 SBCommand::GetName ()
585 return m_opaque_sp->GetCommandName ();
590 SBCommand::GetHelp ()
593 return m_opaque_sp->GetHelp ();
598 SBCommand::AddMultiwordCommand (const char* name, const char* help)
601 return lldb::SBCommand();
602 if (m_opaque_sp->IsMultiwordObject() == false)
603 return lldb::SBCommand();
604 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
605 new_command->SetRemovable (true);
606 lldb::CommandObjectSP new_command_sp(new_command);
607 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
608 return lldb::SBCommand(new_command_sp);
609 return lldb::SBCommand();
613 SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
616 return lldb::SBCommand();
617 if (m_opaque_sp->IsMultiwordObject() == false)
618 return lldb::SBCommand();
619 lldb::CommandObjectSP new_command_sp;
620 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
621 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
622 return lldb::SBCommand(new_command_sp);
623 return lldb::SBCommand();