SBCommandInterpreter.cpp revision 3370f0ce9e2ae2fe1f2adf3628f443053602efa4
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-types.h" 11#include "lldb/Interpreter/Args.h" 12#include "lldb/Core/SourceManager.h" 13#include "lldb/Core/Listener.h" 14#include "lldb/Interpreter/CommandInterpreter.h" 15#include "lldb/Interpreter/CommandReturnObject.h" 16#include "lldb/Target/Target.h" 17 18#include "lldb/API/SBBroadcaster.h" 19#include "lldb/API/SBDebugger.h" 20#include "lldb/API/SBCommandReturnObject.h" 21#include "lldb/API/SBSourceManager.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 29using namespace lldb; 30using namespace lldb_private; 31 32 33SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) : 34 m_opaque_ptr (interpreter) 35{ 36 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 37 38 if (log) 39 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)" 40 " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr); 41} 42 43SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) : 44 m_opaque_ptr (rhs.m_opaque_ptr) 45{ 46} 47 48const SBCommandInterpreter & 49SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs) 50{ 51 m_opaque_ptr = rhs.m_opaque_ptr; 52 return *this; 53} 54 55SBCommandInterpreter::~SBCommandInterpreter () 56{ 57} 58 59bool 60SBCommandInterpreter::IsValid() const 61{ 62 return m_opaque_ptr != NULL; 63} 64 65 66bool 67SBCommandInterpreter::CommandExists (const char *cmd) 68{ 69 if (m_opaque_ptr) 70 return m_opaque_ptr->CommandExists (cmd); 71 return false; 72} 73 74bool 75SBCommandInterpreter::AliasExists (const char *cmd) 76{ 77 if (m_opaque_ptr) 78 return m_opaque_ptr->AliasExists (cmd); 79 return false; 80} 81 82lldb::ReturnStatus 83SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history) 84{ 85 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 86 87 if (log) 88 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)", 89 m_opaque_ptr, command_line, result.get(), add_to_history); 90 91 result.Clear(); 92 if (m_opaque_ptr) 93 { 94 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 95 Mutex::Locker api_locker; 96 if (target_sp) 97 api_locker.Reset(target_sp->GetAPIMutex().GetMutex()); 98 m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref()); 99 } 100 else 101 { 102 result->AppendError ("SBCommandInterpreter is not valid"); 103 result->SetStatus (eReturnStatusFailed); 104 } 105 106 // We need to get the value again, in case the command disabled the log! 107 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); 108 if (log) 109 { 110 SBStream sstr; 111 result.GetDescription (sstr); 112 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i", 113 m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus()); 114 } 115 116 return result.GetStatus(); 117} 118 119int 120SBCommandInterpreter::HandleCompletion (const char *current_line, 121 const char *cursor, 122 const char *last_char, 123 int match_start_point, 124 int max_return_elements, 125 SBStringList &matches) 126{ 127 int num_completions = 0; 128 if (m_opaque_ptr) 129 { 130 lldb_private::StringList lldb_matches; 131 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point, 132 max_return_elements, lldb_matches); 133 134 SBStringList temp_list (&lldb_matches); 135 matches.AppendList (temp_list); 136 } 137 return num_completions; 138} 139 140bool 141SBCommandInterpreter::HasCommands () 142{ 143 if (m_opaque_ptr) 144 return m_opaque_ptr->HasCommands(); 145 return false; 146} 147 148bool 149SBCommandInterpreter::HasAliases () 150{ 151 if (m_opaque_ptr) 152 return m_opaque_ptr->HasAliases(); 153 return false; 154} 155 156bool 157SBCommandInterpreter::HasAliasOptions () 158{ 159 if (m_opaque_ptr) 160 return m_opaque_ptr->HasAliasOptions (); 161 return false; 162} 163 164SBProcess 165SBCommandInterpreter::GetProcess () 166{ 167 SBProcess process; 168 if (m_opaque_ptr) 169 { 170 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 171 if (target_sp) 172 { 173 Mutex::Locker api_locker(target_sp->GetAPIMutex()); 174 process.SetProcess(target_sp->GetProcessSP()); 175 } 176 } 177 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 178 179 if (log) 180 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)", 181 m_opaque_ptr, process.get()); 182 183 184 return process; 185} 186 187ssize_t 188SBCommandInterpreter::WriteToScriptInterpreter (const char *src) 189{ 190 return WriteToScriptInterpreter (src, strlen(src)); 191} 192 193ssize_t 194SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len) 195{ 196 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 197 198 ssize_t bytes_written = 0; 199 if (m_opaque_ptr && src && src[0]) 200 { 201 ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter(); 202 if (script_interpreter) 203 bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len); 204 } 205 if (log) 206 log->Printf ("SBCommandInterpreter(%p)::WriteToScriptInterpreter (src=\"%s\", src_len=%zu) => %zi", 207 m_opaque_ptr, src, src_len, bytes_written); 208 209 return bytes_written; 210} 211 212 213CommandInterpreter * 214SBCommandInterpreter::get () 215{ 216 return m_opaque_ptr; 217} 218 219CommandInterpreter & 220SBCommandInterpreter::ref () 221{ 222 assert (m_opaque_ptr); 223 return *m_opaque_ptr; 224} 225 226void 227SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter) 228{ 229 m_opaque_ptr = interpreter; 230} 231 232void 233SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result) 234{ 235 result.Clear(); 236 if (m_opaque_ptr) 237 { 238 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 239 Mutex::Locker api_locker; 240 if (target_sp) 241 api_locker.Reset(target_sp->GetAPIMutex().GetMutex()); 242 m_opaque_ptr->SourceInitFile (false, result.ref()); 243 } 244 else 245 { 246 result->AppendError ("SBCommandInterpreter is not valid"); 247 result->SetStatus (eReturnStatusFailed); 248 } 249 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 250 251 if (log) 252 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))", 253 m_opaque_ptr, result.get()); 254 255} 256 257void 258SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result) 259{ 260 result.Clear(); 261 if (m_opaque_ptr) 262 { 263 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 264 Mutex::Locker api_locker; 265 if (target_sp) 266 api_locker.Reset(target_sp->GetAPIMutex().GetMutex()); 267 m_opaque_ptr->SourceInitFile (true, result.ref()); 268 } 269 else 270 { 271 result->AppendError ("SBCommandInterpreter is not valid"); 272 result->SetStatus (eReturnStatusFailed); 273 } 274 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 275 276 if (log) 277 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))", 278 m_opaque_ptr, result.get()); 279} 280 281SBBroadcaster 282SBCommandInterpreter::GetBroadcaster () 283{ 284 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 285 286 SBBroadcaster broadcaster (m_opaque_ptr, false); 287 288 if (log) 289 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)", 290 m_opaque_ptr, broadcaster.get()); 291 292 return broadcaster; 293} 294 295const char * 296SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type) 297{ 298 return CommandObject::GetArgumentTypeAsCString (arg_type); 299} 300 301const char * 302SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type) 303{ 304 return CommandObject::GetArgumentDescriptionAsCString (arg_type); 305} 306 307 308extern "C" bool 309LLDBSwigPythonBreakpointCallbackFunction 310( 311 const char *python_function_name, 312 const char *session_dictionary_name, 313 const lldb::StackFrameSP& sb_frame, 314 const lldb::BreakpointLocationSP& sb_bp_loc 315); 316 317extern "C" std::string 318LLDBSwigPythonCallTypeScript 319( 320 const char *python_function_name, 321 const char *session_dictionary_name, 322 const lldb::ValueObjectSP& valobj_sp 323); 324 325extern "C" void* 326LLDBSwigPythonCreateSyntheticProvider 327( 328 const std::string python_class_name, 329 const char *session_dictionary_name, 330 const lldb::ValueObjectSP& valobj_sp 331); 332 333 334extern "C" uint32_t LLDBSwigPython_CalculateNumChildren (void *implementor); 335extern "C" void* LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx); 336extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name); 337extern "C" lldb::SBValue* LLDBSWIGPython_CastPyObjectToSBValue (void* data); 338extern "C" void LLDBSwigPython_UpdateSynthProviderInstance (void* implementor); 339 340extern "C" bool LLDBSwigPythonCallCommand 341( 342 const char *python_function_name, 343 const char *session_dictionary_name, 344 lldb::DebuggerSP& debugger, 345 const char* args, 346 std::string& err_msg, 347 lldb_private::CommandReturnObject& cmd_retobj 348); 349 350 351extern "C" void init_lldb(void); 352 353void 354SBCommandInterpreter::InitializeSWIG () 355{ 356 static bool g_initialized = false; 357 if (!g_initialized) 358 { 359 g_initialized = true; 360 ScriptInterpreter::InitializeInterpreter (init_lldb, 361 LLDBSwigPythonBreakpointCallbackFunction, 362 LLDBSwigPythonCallTypeScript, 363 LLDBSwigPythonCreateSyntheticProvider, 364 LLDBSwigPython_CalculateNumChildren, 365 LLDBSwigPython_GetChildAtIndex, 366 LLDBSwigPython_GetIndexOfChildWithName, 367 LLDBSWIGPython_CastPyObjectToSBValue, 368 LLDBSwigPython_UpdateSynthProviderInstance, 369 LLDBSwigPythonCallCommand); 370 } 371} 372