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-python.h"
11
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"
19
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"
28
29 using namespace lldb;
30 using namespace lldb_private;
31
32 class CommandPluginInterfaceImplementation : public CommandObjectParsed
33 {
34 public:
CommandPluginInterfaceImplementation(CommandInterpreter & interpreter,const char * name,lldb::SBCommandPluginInterface * backend,const char * help=NULL,const char * syntax=NULL,uint32_t flags=0)35 CommandPluginInterfaceImplementation (CommandInterpreter &interpreter,
36 const char *name,
37 lldb::SBCommandPluginInterface* backend,
38 const char *help = NULL,
39 const char *syntax = NULL,
40 uint32_t flags = 0) :
41 CommandObjectParsed (interpreter, name, help, syntax, flags),
42 m_backend(backend) {}
43
44 virtual bool
IsRemovable() const45 IsRemovable() const { return true; }
46
47 protected:
48 virtual bool
DoExecute(Args & command,CommandReturnObject & result)49 DoExecute (Args& command, CommandReturnObject &result)
50 {
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);
55 sb_return.Release();
56 return ret;
57 }
58 lldb::SBCommandPluginInterface* m_backend;
59 };
60
SBCommandInterpreter(CommandInterpreter * interpreter)61 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
62 m_opaque_ptr (interpreter)
63 {
64 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
65
66 if (log)
67 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
68 " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr);
69 }
70
SBCommandInterpreter(const SBCommandInterpreter & rhs)71 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
72 m_opaque_ptr (rhs.m_opaque_ptr)
73 {
74 }
75
76 const SBCommandInterpreter &
operator =(const SBCommandInterpreter & rhs)77 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
78 {
79 m_opaque_ptr = rhs.m_opaque_ptr;
80 return *this;
81 }
82
~SBCommandInterpreter()83 SBCommandInterpreter::~SBCommandInterpreter ()
84 {
85 }
86
87 bool
IsValid() const88 SBCommandInterpreter::IsValid() const
89 {
90 return m_opaque_ptr != NULL;
91 }
92
93
94 bool
CommandExists(const char * cmd)95 SBCommandInterpreter::CommandExists (const char *cmd)
96 {
97 if (cmd && m_opaque_ptr)
98 return m_opaque_ptr->CommandExists (cmd);
99 return false;
100 }
101
102 bool
AliasExists(const char * cmd)103 SBCommandInterpreter::AliasExists (const char *cmd)
104 {
105 if (cmd && m_opaque_ptr)
106 return m_opaque_ptr->AliasExists (cmd);
107 return false;
108 }
109
110 bool
IsActive()111 SBCommandInterpreter::IsActive ()
112 {
113 if (m_opaque_ptr)
114 return m_opaque_ptr->IsActive ();
115 return false;
116 }
117
118 const char *
GetIOHandlerControlSequence(char ch)119 SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
120 {
121 if (m_opaque_ptr)
122 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
123 return NULL;
124 }
125
126 lldb::ReturnStatus
HandleCommand(const char * command_line,SBCommandReturnObject & result,bool add_to_history)127 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
128 {
129 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
130
131 if (log)
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);
134
135 result.Clear();
136 if (command_line && m_opaque_ptr)
137 {
138 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
139 }
140 else
141 {
142 result->AppendError ("SBCommandInterpreter or the command line is not valid");
143 result->SetStatus (eReturnStatusFailed);
144 }
145
146 // We need to get the value again, in case the command disabled the log!
147 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
148 if (log)
149 {
150 SBStream sstr;
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());
154 }
155
156 return result.GetStatus();
157 }
158
159 int
HandleCompletion(const char * current_line,const char * cursor,const char * last_char,int match_start_point,int max_return_elements,SBStringList & matches)160 SBCommandInterpreter::HandleCompletion (const char *current_line,
161 const char *cursor,
162 const char *last_char,
163 int match_start_point,
164 int max_return_elements,
165 SBStringList &matches)
166 {
167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
168 int num_completions = 0;
169
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)
173 return 0;
174
175 if (cursor < current_line || last_char < current_line)
176 return 0;
177
178 size_t current_line_size = strlen (current_line);
179 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size)
180 return 0;
181
182 if (log)
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);
185
186 if (m_opaque_ptr)
187 {
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);
191
192 SBStringList temp_list (&lldb_matches);
193 matches.AppendList (temp_list);
194 }
195 if (log)
196 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", m_opaque_ptr, num_completions);
197
198 return num_completions;
199 }
200
201 int
HandleCompletion(const char * current_line,uint32_t cursor_pos,int match_start_point,int max_return_elements,lldb::SBStringList & matches)202 SBCommandInterpreter::HandleCompletion (const char *current_line,
203 uint32_t cursor_pos,
204 int match_start_point,
205 int max_return_elements,
206 lldb::SBStringList &matches)
207 {
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);
211 }
212
213 bool
HasCommands()214 SBCommandInterpreter::HasCommands ()
215 {
216 if (m_opaque_ptr)
217 return m_opaque_ptr->HasCommands();
218 return false;
219 }
220
221 bool
HasAliases()222 SBCommandInterpreter::HasAliases ()
223 {
224 if (m_opaque_ptr)
225 return m_opaque_ptr->HasAliases();
226 return false;
227 }
228
229 bool
HasAliasOptions()230 SBCommandInterpreter::HasAliasOptions ()
231 {
232 if (m_opaque_ptr)
233 return m_opaque_ptr->HasAliasOptions ();
234 return false;
235 }
236
237 SBProcess
GetProcess()238 SBCommandInterpreter::GetProcess ()
239 {
240 SBProcess sb_process;
241 ProcessSP process_sp;
242 if (m_opaque_ptr)
243 {
244 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
245 if (target_sp)
246 {
247 Mutex::Locker api_locker(target_sp->GetAPIMutex());
248 process_sp = target_sp->GetProcessSP();
249 sb_process.SetSP(process_sp);
250 }
251 }
252 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
253
254 if (log)
255 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
256 m_opaque_ptr, process_sp.get());
257
258
259 return sb_process;
260 }
261
262 SBDebugger
GetDebugger()263 SBCommandInterpreter::GetDebugger ()
264 {
265 SBDebugger sb_debugger;
266 if (m_opaque_ptr)
267 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
268 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
269
270 if (log)
271 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
272 m_opaque_ptr, sb_debugger.get());
273
274
275 return sb_debugger;
276 }
277
278 CommandInterpreter *
get()279 SBCommandInterpreter::get ()
280 {
281 return m_opaque_ptr;
282 }
283
284 CommandInterpreter &
ref()285 SBCommandInterpreter::ref ()
286 {
287 assert (m_opaque_ptr);
288 return *m_opaque_ptr;
289 }
290
291 void
reset(lldb_private::CommandInterpreter * interpreter)292 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
293 {
294 m_opaque_ptr = interpreter;
295 }
296
297 void
SourceInitFileInHomeDirectory(SBCommandReturnObject & result)298 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
299 {
300 result.Clear();
301 if (m_opaque_ptr)
302 {
303 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
304 Mutex::Locker api_locker;
305 if (target_sp)
306 api_locker.Lock(target_sp->GetAPIMutex());
307 m_opaque_ptr->SourceInitFile (false, result.ref());
308 }
309 else
310 {
311 result->AppendError ("SBCommandInterpreter is not valid");
312 result->SetStatus (eReturnStatusFailed);
313 }
314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
315
316 if (log)
317 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
318 m_opaque_ptr, result.get());
319
320 }
321
322 void
SourceInitFileInCurrentWorkingDirectory(SBCommandReturnObject & result)323 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
324 {
325 result.Clear();
326 if (m_opaque_ptr)
327 {
328 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
329 Mutex::Locker api_locker;
330 if (target_sp)
331 api_locker.Lock(target_sp->GetAPIMutex());
332 m_opaque_ptr->SourceInitFile (true, result.ref());
333 }
334 else
335 {
336 result->AppendError ("SBCommandInterpreter is not valid");
337 result->SetStatus (eReturnStatusFailed);
338 }
339 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
340
341 if (log)
342 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
343 m_opaque_ptr, result.get());
344 }
345
346 SBBroadcaster
GetBroadcaster()347 SBCommandInterpreter::GetBroadcaster ()
348 {
349 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
350
351 SBBroadcaster broadcaster (m_opaque_ptr, false);
352
353 if (log)
354 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
355 m_opaque_ptr, broadcaster.get());
356
357 return broadcaster;
358 }
359
360 const char *
GetBroadcasterClass()361 SBCommandInterpreter::GetBroadcasterClass ()
362 {
363 return Communication::GetStaticBroadcasterClass().AsCString();
364 }
365
366 const char *
GetArgumentTypeAsCString(const lldb::CommandArgumentType arg_type)367 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
368 {
369 return CommandObject::GetArgumentTypeAsCString (arg_type);
370 }
371
372 const char *
GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type)373 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
374 {
375 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
376 }
377
378 bool
SetCommandOverrideCallback(const char * command_name,lldb::CommandOverrideCallback callback,void * baton)379 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
380 lldb::CommandOverrideCallback callback,
381 void *baton)
382 {
383 if (command_name && command_name[0] && m_opaque_ptr)
384 {
385 std::string command_name_str (command_name);
386 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
387 if (cmd_obj)
388 {
389 assert(command_name_str.empty());
390 cmd_obj->SetOverrideCallback (callback, baton);
391 return true;
392 }
393 }
394 return false;
395 }
396
397 #ifndef LLDB_DISABLE_PYTHON
398
399 // Defined in the SWIG source file
400 extern "C" void
401 init_lldb(void);
402
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
408 extern "C" bool
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);
413
414 extern "C" bool
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);
419
420 extern "C" bool
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);
426
427 extern "C" void*
428 LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
429 const char *session_dictionary_name,
430 const lldb::ValueObjectSP& valobj_sp);
431
432
433 extern "C" uint32_t
434 LLDBSwigPython_CalculateNumChildren (void *implementor);
435
436 extern "C" void *
437 LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
438
439 extern "C" int
440 LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
441
442 extern "C" void *
443 LLDBSWIGPython_CastPyObjectToSBValue (void* data);
444
445 extern lldb::ValueObjectSP
446 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
447
448 extern "C" bool
449 LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
450
451 extern "C" bool
452 LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
453
454 extern "C" bool
455 LLDBSwigPythonCallCommand (const char *python_function_name,
456 const char *session_dictionary_name,
457 lldb::DebuggerSP& debugger,
458 const char* args,
459 lldb_private::CommandReturnObject &cmd_retobj);
460
461 extern "C" bool
462 LLDBSwigPythonCallModuleInit (const char *python_module_name,
463 const char *session_dictionary_name,
464 lldb::DebuggerSP& debugger);
465
466 extern "C" void*
467 LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
468 const char *session_dictionary_name,
469 const lldb::ProcessSP& process_sp);
470
471 extern "C" bool
472 LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
473 const char* session_dictionary_name,
474 lldb::ProcessSP& process,
475 std::string& output);
476
477 extern "C" bool
478 LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
479 const char* session_dictionary_name,
480 lldb::ThreadSP& thread,
481 std::string& output);
482
483 extern "C" bool
484 LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
485 const char* session_dictionary_name,
486 lldb::TargetSP& target,
487 std::string& output);
488
489 extern "C" bool
490 LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
491 const char* session_dictionary_name,
492 lldb::StackFrameSP& frame,
493 std::string& output);
494
495 extern "C" void*
496 LLDBSWIGPython_GetDynamicSetting (void* module,
497 const char* setting,
498 const lldb::TargetSP& target_sp);
499
500
501 #endif
502
503 void
InitializeSWIG()504 SBCommandInterpreter::InitializeSWIG ()
505 {
506 static bool g_initialized = false;
507 if (!g_initialized)
508 {
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);
531 #endif
532 }
533 }
534
535 lldb::SBCommand
AddMultiwordCommand(const char * name,const char * help)536 SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
537 {
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();
544 }
545
546 lldb::SBCommand
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help)547 SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
548 {
549 lldb::CommandObjectSP new_command_sp;
550 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
551
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();
555 }
556
SBCommand()557 SBCommand::SBCommand ()
558 {}
559
SBCommand(lldb::CommandObjectSP cmd_sp)560 SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
561 {}
562
563 bool
IsValid()564 SBCommand::IsValid ()
565 {
566 return (bool)m_opaque_sp;
567 }
568
569 const char*
GetName()570 SBCommand::GetName ()
571 {
572 if (IsValid ())
573 return m_opaque_sp->GetCommandName ();
574 return NULL;
575 }
576
577 const char*
GetHelp()578 SBCommand::GetHelp ()
579 {
580 if (IsValid ())
581 return m_opaque_sp->GetHelp ();
582 return NULL;
583 }
584
585 lldb::SBCommand
AddMultiwordCommand(const char * name,const char * help)586 SBCommand::AddMultiwordCommand (const char* name, const char* help)
587 {
588 if (!IsValid ())
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();
598 }
599
600 lldb::SBCommand
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help)601 SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
602 {
603 if (!IsValid ())
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();
612 }
613
614