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::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
113 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
116 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
117 m_opaque_ptr, command_line, result.get(), add_to_history);
120 if (command_line && m_opaque_ptr)
122 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
126 result->AppendError ("SBCommandInterpreter or the command line is not valid");
127 result->SetStatus (eReturnStatusFailed);
130 // We need to get the value again, in case the command disabled the log!
131 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
135 result.GetDescription (sstr);
136 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
137 m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus());
140 return result.GetStatus();
144 SBCommandInterpreter::HandleCompletion (const char *current_line,
146 const char *last_char,
147 int match_start_point,
148 int max_return_elements,
149 SBStringList &matches)
151 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
152 int num_completions = 0;
154 // Sanity check the arguments that are passed in:
155 // cursor & last_char have to be within the current_line.
156 if (current_line == NULL || cursor == NULL || last_char == NULL)
159 if (cursor < current_line || last_char < current_line)
162 size_t current_line_size = strlen (current_line);
163 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size)
167 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)",
168 m_opaque_ptr, current_line, (uint64_t) (cursor - current_line), (uint64_t) (last_char - current_line), match_start_point, max_return_elements);
172 lldb_private::StringList lldb_matches;
173 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
174 max_return_elements, lldb_matches);
176 SBStringList temp_list (&lldb_matches);
177 matches.AppendList (temp_list);
180 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", m_opaque_ptr, num_completions);
182 return num_completions;
186 SBCommandInterpreter::HandleCompletion (const char *current_line,
188 int match_start_point,
189 int max_return_elements,
190 lldb::SBStringList &matches)
192 const char *cursor = current_line + cursor_pos;
193 const char *last_char = current_line + strlen (current_line);
194 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
198 SBCommandInterpreter::HasCommands ()
201 return m_opaque_ptr->HasCommands();
206 SBCommandInterpreter::HasAliases ()
209 return m_opaque_ptr->HasAliases();
214 SBCommandInterpreter::HasAliasOptions ()
217 return m_opaque_ptr->HasAliasOptions ();
222 SBCommandInterpreter::GetProcess ()
224 SBProcess sb_process;
225 ProcessSP process_sp;
228 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
231 Mutex::Locker api_locker(target_sp->GetAPIMutex());
232 process_sp = target_sp->GetProcessSP();
233 sb_process.SetSP(process_sp);
236 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
239 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
240 m_opaque_ptr, process_sp.get());
247 SBCommandInterpreter::GetDebugger ()
249 SBDebugger sb_debugger;
251 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
252 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
255 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
256 m_opaque_ptr, sb_debugger.get());
263 SBCommandInterpreter::get ()
269 SBCommandInterpreter::ref ()
271 assert (m_opaque_ptr);
272 return *m_opaque_ptr;
276 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
278 m_opaque_ptr = interpreter;
282 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
287 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
288 Mutex::Locker api_locker;
290 api_locker.Lock(target_sp->GetAPIMutex());
291 m_opaque_ptr->SourceInitFile (false, result.ref());
295 result->AppendError ("SBCommandInterpreter is not valid");
296 result->SetStatus (eReturnStatusFailed);
298 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
301 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
302 m_opaque_ptr, result.get());
307 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
312 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
313 Mutex::Locker api_locker;
315 api_locker.Lock(target_sp->GetAPIMutex());
316 m_opaque_ptr->SourceInitFile (true, result.ref());
320 result->AppendError ("SBCommandInterpreter is not valid");
321 result->SetStatus (eReturnStatusFailed);
323 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
326 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
327 m_opaque_ptr, result.get());
331 SBCommandInterpreter::GetBroadcaster ()
333 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
335 SBBroadcaster broadcaster (m_opaque_ptr, false);
338 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
339 m_opaque_ptr, broadcaster.get());
345 SBCommandInterpreter::GetBroadcasterClass ()
347 return Communication::GetStaticBroadcasterClass().AsCString();
351 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
353 return CommandObject::GetArgumentTypeAsCString (arg_type);
357 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
359 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
363 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
364 lldb::CommandOverrideCallback callback,
367 if (command_name && command_name[0] && m_opaque_ptr)
369 std::string command_name_str (command_name);
370 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
373 assert(command_name_str.empty());
374 cmd_obj->SetOverrideCallback (callback, baton);
381 #ifndef LLDB_DISABLE_PYTHON
383 // Defined in the SWIG source file
389 extern "C" void init_lldb(void);
391 // Usually defined in the SWIG source file, but we have sripting disabled
400 SBCommandInterpreter::InitializeSWIG ()
402 static bool g_initialized = false;
405 g_initialized = true;
406 #ifndef LLDB_DISABLE_PYTHON
407 ScriptInterpreter::InitializeInterpreter (init_lldb);
413 SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
415 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
416 new_command->SetRemovable (true);
417 lldb::CommandObjectSP new_command_sp(new_command);
418 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
419 return lldb::SBCommand(new_command_sp);
420 return lldb::SBCommand();
424 SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
426 lldb::CommandObjectSP new_command_sp;
427 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
429 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
430 return lldb::SBCommand(new_command_sp);
431 return lldb::SBCommand();
434 SBCommand::SBCommand ()
437 SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
441 SBCommand::IsValid ()
443 return (bool)m_opaque_sp;
447 SBCommand::GetName ()
450 return m_opaque_sp->GetCommandName ();
455 SBCommand::GetHelp ()
458 return m_opaque_sp->GetHelp ();
463 SBCommand::AddMultiwordCommand (const char* name, const char* help)
466 return lldb::SBCommand();
467 if (m_opaque_sp->IsMultiwordObject() == false)
468 return lldb::SBCommand();
469 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
470 new_command->SetRemovable (true);
471 lldb::CommandObjectSP new_command_sp(new_command);
472 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
473 return lldb::SBCommand(new_command_sp);
474 return lldb::SBCommand();
478 SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
481 return lldb::SBCommand();
482 if (m_opaque_sp->IsMultiwordObject() == false)
483 return lldb::SBCommand();
484 lldb::CommandObjectSP new_command_sp;
485 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
486 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
487 return lldb::SBCommand(new_command_sp);
488 return lldb::SBCommand();