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/SBCommandInterpreter.h" 22 #include "lldb/API/SBProcess.h" 23 #include "lldb/API/SBTarget.h" 24 #include "lldb/API/SBListener.h" 25 #include "lldb/API/SBStream.h" 26 #include "lldb/API/SBStringList.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 32 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) : 33 m_opaque_ptr (interpreter) 34 { 35 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 36 37 if (log) 38 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)" 39 " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr); 40 } 41 42 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) : 43 m_opaque_ptr (rhs.m_opaque_ptr) 44 { 45 } 46 47 const SBCommandInterpreter & 48 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs) 49 { 50 m_opaque_ptr = rhs.m_opaque_ptr; 51 return *this; 52 } 53 54 SBCommandInterpreter::~SBCommandInterpreter () 55 { 56 } 57 58 bool 59 SBCommandInterpreter::IsValid() const 60 { 61 return m_opaque_ptr != NULL; 62 } 63 64 65 bool 66 SBCommandInterpreter::CommandExists (const char *cmd) 67 { 68 if (cmd && m_opaque_ptr) 69 return m_opaque_ptr->CommandExists (cmd); 70 return false; 71 } 72 73 bool 74 SBCommandInterpreter::AliasExists (const char *cmd) 75 { 76 if (cmd && m_opaque_ptr) 77 return m_opaque_ptr->AliasExists (cmd); 78 return false; 79 } 80 81 lldb::ReturnStatus 82 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history) 83 { 84 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 85 86 if (log) 87 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)", 88 m_opaque_ptr, command_line, result.get(), add_to_history); 89 90 result.Clear(); 91 if (command_line && m_opaque_ptr) 92 { 93 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 94 Mutex::Locker api_locker; 95 if (target_sp) 96 api_locker.Reset(target_sp->GetAPIMutex().GetMutex()); 97 m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref()); 98 } 99 else 100 { 101 result->AppendError ("SBCommandInterpreter or the command line is not valid"); 102 result->SetStatus (eReturnStatusFailed); 103 } 104 105 // We need to get the value again, in case the command disabled the log! 106 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); 107 if (log) 108 { 109 SBStream sstr; 110 result.GetDescription (sstr); 111 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i", 112 m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus()); 113 } 114 115 return result.GetStatus(); 116 } 117 118 int 119 SBCommandInterpreter::HandleCompletion (const char *current_line, 120 const char *cursor, 121 const char *last_char, 122 int match_start_point, 123 int max_return_elements, 124 SBStringList &matches) 125 { 126 int num_completions = 0; 127 128 // Sanity check the arguments that are passed in: 129 // cursor & last_char have to be within the current_line. 130 if (current_line == NULL || cursor == NULL || last_char == NULL) 131 return 0; 132 133 if (cursor < current_line || last_char < current_line) 134 return 0; 135 136 size_t current_line_size = strlen (current_line); 137 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size) 138 return 0; 139 140 if (m_opaque_ptr) 141 { 142 lldb_private::StringList lldb_matches; 143 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point, 144 max_return_elements, lldb_matches); 145 146 SBStringList temp_list (&lldb_matches); 147 matches.AppendList (temp_list); 148 } 149 return num_completions; 150 } 151 152 int 153 SBCommandInterpreter::HandleCompletion (const char *current_line, 154 uint32_t cursor_pos, 155 int match_start_point, 156 int max_return_elements, 157 lldb::SBStringList &matches) 158 { 159 const char *cursor = current_line + cursor_pos; 160 const char *last_char = current_line + strlen (current_line); 161 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches); 162 } 163 164 bool 165 SBCommandInterpreter::HasCommands () 166 { 167 if (m_opaque_ptr) 168 return m_opaque_ptr->HasCommands(); 169 return false; 170 } 171 172 bool 173 SBCommandInterpreter::HasAliases () 174 { 175 if (m_opaque_ptr) 176 return m_opaque_ptr->HasAliases(); 177 return false; 178 } 179 180 bool 181 SBCommandInterpreter::HasAliasOptions () 182 { 183 if (m_opaque_ptr) 184 return m_opaque_ptr->HasAliasOptions (); 185 return false; 186 } 187 188 SBProcess 189 SBCommandInterpreter::GetProcess () 190 { 191 SBProcess sb_process; 192 ProcessSP process_sp; 193 if (m_opaque_ptr) 194 { 195 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); 196 if (target_sp) 197 { 198 Mutex::Locker api_locker(target_sp->GetAPIMutex()); 199 process_sp = target_sp->GetProcessSP(); 200 sb_process.SetSP(process_sp); 201 } 202 } 203 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 204 205 if (log) 206 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)", 207 m_opaque_ptr, process_sp.get()); 208 209 210 return sb_process; 211 } 212 213 CommandInterpreter * 214 SBCommandInterpreter::get () 215 { 216 return m_opaque_ptr; 217 } 218 219 CommandInterpreter & 220 SBCommandInterpreter::ref () 221 { 222 assert (m_opaque_ptr); 223 return *m_opaque_ptr; 224 } 225 226 void 227 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter) 228 { 229 m_opaque_ptr = interpreter; 230 } 231 232 void 233 SBCommandInterpreter::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 257 void 258 SBCommandInterpreter::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 281 SBBroadcaster 282 SBCommandInterpreter::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 295 const char * 296 SBCommandInterpreter::GetBroadcasterClass () 297 { 298 return Communication::GetStaticBroadcasterClass().AsCString(); 299 } 300 301 const char * 302 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type) 303 { 304 return CommandObject::GetArgumentTypeAsCString (arg_type); 305 } 306 307 const char * 308 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type) 309 { 310 return CommandObject::GetArgumentDescriptionAsCString (arg_type); 311 } 312 313 bool 314 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name, 315 lldb::CommandOverrideCallback callback, 316 void *baton) 317 { 318 if (command_name && command_name[0] && m_opaque_ptr) 319 { 320 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObject(command_name); 321 if (cmd_obj) 322 { 323 cmd_obj->SetOverrideCallback (callback, baton); 324 return true; 325 } 326 } 327 return false; 328 } 329 330 #ifndef LLDB_DISABLE_PYTHON 331 332 // Defined in the SWIG source file 333 extern "C" void 334 init_lldb(void); 335 336 #else 337 338 extern "C" void init_lldb(void); 339 340 // Usually defined in the SWIG source file, but we have sripting disabled 341 extern "C" void 342 init_lldb(void) 343 { 344 } 345 346 #endif 347 348 void 349 SBCommandInterpreter::InitializeSWIG () 350 { 351 static bool g_initialized = false; 352 if (!g_initialized) 353 { 354 g_initialized = true; 355 #ifndef LLDB_DISABLE_PYTHON 356 ScriptInterpreter::InitializeInterpreter (init_lldb); 357 #endif 358 } 359 } 360 361