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/SBEvent.h"
24 #include "lldb/API/SBExecutionContext.h"
25 #include "lldb/API/SBProcess.h"
26 #include "lldb/API/SBTarget.h"
27 #include "lldb/API/SBListener.h"
28 #include "lldb/API/SBStream.h"
29 #include "lldb/API/SBStringList.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions()
35 {
36     m_opaque_up.reset(new CommandInterpreterRunOptions());
37 }
38 
39 SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions()
40 {
41 
42 }
43 
44 bool
45 SBCommandInterpreterRunOptions::GetStopOnContinue () const
46 {
47     return m_opaque_up->GetStopOnContinue();
48 }
49 
50 void
51 SBCommandInterpreterRunOptions::SetStopOnContinue (bool stop_on_continue)
52 {
53     m_opaque_up->SetStopOnContinue(stop_on_continue);
54 }
55 
56 bool
57 SBCommandInterpreterRunOptions::GetStopOnError () const
58 {
59     return m_opaque_up->GetStopOnError();
60 }
61 
62 void
63 SBCommandInterpreterRunOptions::SetStopOnError (bool stop_on_error)
64 {
65     m_opaque_up->SetStopOnError(stop_on_error);
66 }
67 
68 bool
69 SBCommandInterpreterRunOptions::GetStopOnCrash () const
70 {
71     return m_opaque_up->GetStopOnCrash();
72 }
73 
74 void
75 SBCommandInterpreterRunOptions::SetStopOnCrash (bool stop_on_crash)
76 {
77     m_opaque_up->SetStopOnCrash(stop_on_crash);
78 }
79 
80 bool
81 SBCommandInterpreterRunOptions::GetEchoCommands () const
82 {
83     return m_opaque_up->GetEchoCommands();
84 }
85 
86 void
87 SBCommandInterpreterRunOptions::SetEchoCommands (bool echo_commands)
88 {
89     m_opaque_up->SetEchoCommands(echo_commands);
90 }
91 
92 bool
93 SBCommandInterpreterRunOptions::GetPrintResults () const
94 {
95     return m_opaque_up->GetPrintResults();
96 }
97 
98 void
99 SBCommandInterpreterRunOptions::SetPrintResults (bool print_results)
100 {
101     m_opaque_up->SetPrintResults(print_results);
102 }
103 
104 bool
105 SBCommandInterpreterRunOptions::GetAddToHistory () const
106 {
107     return m_opaque_up->GetAddToHistory();
108 }
109 
110 void
111 SBCommandInterpreterRunOptions::SetAddToHistory (bool add_to_history)
112 {
113     m_opaque_up->SetAddToHistory(add_to_history);
114 }
115 
116 lldb_private::CommandInterpreterRunOptions *
117 SBCommandInterpreterRunOptions::get () const
118 {
119     return m_opaque_up.get();
120 }
121 
122 lldb_private::CommandInterpreterRunOptions &
123 SBCommandInterpreterRunOptions::ref () const
124 {
125     return *m_opaque_up.get();
126 }
127 
128 class CommandPluginInterfaceImplementation : public CommandObjectParsed
129 {
130 public:
131     CommandPluginInterfaceImplementation (CommandInterpreter &interpreter,
132                                           const char *name,
133                                           lldb::SBCommandPluginInterface* backend,
134                                           const char *help = NULL,
135                                           const char *syntax = NULL,
136                                           uint32_t flags = 0) :
137     CommandObjectParsed (interpreter, name, help, syntax, flags),
138     m_backend(backend) {}
139 
140     virtual bool
141     IsRemovable() const { return true; }
142 
143 protected:
144     virtual bool
145     DoExecute (Args& command, CommandReturnObject &result)
146     {
147         SBCommandReturnObject sb_return(&result);
148         SBCommandInterpreter sb_interpreter(&m_interpreter);
149         SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
150         bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return);
151         sb_return.Release();
152         return ret;
153     }
154     lldb::SBCommandPluginInterface* m_backend;
155 };
156 
157 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
158     m_opaque_ptr (interpreter)
159 {
160     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
161 
162     if (log)
163         log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
164                      " => SBCommandInterpreter(%p)",
165                      static_cast<void*>(interpreter),
166                      static_cast<void*>(m_opaque_ptr));
167 }
168 
169 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
170     m_opaque_ptr (rhs.m_opaque_ptr)
171 {
172 }
173 
174 const SBCommandInterpreter &
175 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
176 {
177     m_opaque_ptr = rhs.m_opaque_ptr;
178     return *this;
179 }
180 
181 SBCommandInterpreter::~SBCommandInterpreter ()
182 {
183 }
184 
185 bool
186 SBCommandInterpreter::IsValid() const
187 {
188     return m_opaque_ptr != NULL;
189 }
190 
191 
192 bool
193 SBCommandInterpreter::CommandExists (const char *cmd)
194 {
195     if (cmd && m_opaque_ptr)
196         return m_opaque_ptr->CommandExists (cmd);
197     return false;
198 }
199 
200 bool
201 SBCommandInterpreter::AliasExists (const char *cmd)
202 {
203     if (cmd && m_opaque_ptr)
204         return m_opaque_ptr->AliasExists (cmd);
205     return false;
206 }
207 
208 bool
209 SBCommandInterpreter::IsActive ()
210 {
211     if (m_opaque_ptr)
212         return m_opaque_ptr->IsActive ();
213     return false;
214 }
215 
216 const char *
217 SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
218 {
219     if (m_opaque_ptr)
220         return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
221     return NULL;
222 }
223 
224 lldb::ReturnStatus
225 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
226 {
227     SBExecutionContext sb_exe_ctx;
228     return HandleCommand (command_line, sb_exe_ctx, result, add_to_history);
229 }
230 
231 lldb::ReturnStatus
232 SBCommandInterpreter::HandleCommand (const char *command_line, SBExecutionContext &override_context, SBCommandReturnObject &result, bool add_to_history)
233 {
234     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
235 
236     if (log)
237         log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
238                      static_cast<void*>(m_opaque_ptr), command_line,
239                      static_cast<void*>(result.get()), add_to_history);
240 
241     ExecutionContext ctx, *ctx_ptr;
242     if (override_context.get())
243     {
244         ctx = override_context.get()->Lock(true);
245         ctx_ptr = &ctx;
246     }
247     else
248        ctx_ptr = nullptr;
249 
250 
251     result.Clear();
252     if (command_line && m_opaque_ptr)
253     {
254         result.ref().SetInteractive(false);
255         m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref(), ctx_ptr);
256     }
257     else
258     {
259         result->AppendError ("SBCommandInterpreter or the command line is not valid");
260         result->SetStatus (eReturnStatusFailed);
261     }
262 
263     // We need to get the value again, in case the command disabled the log!
264     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
265     if (log)
266     {
267         SBStream sstr;
268         result.GetDescription (sstr);
269         log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
270                      static_cast<void*>(m_opaque_ptr), command_line,
271                      static_cast<void*>(result.get()), sstr.GetData(),
272                      add_to_history, result.GetStatus());
273     }
274 
275     return result.GetStatus();
276 }
277 
278 void
279 SBCommandInterpreter::HandleCommandsFromFile (lldb::SBFileSpec &file,
280                                               lldb::SBExecutionContext &override_context,
281                                               lldb::SBCommandInterpreterRunOptions &options,
282                                               lldb::SBCommandReturnObject result)
283 {
284     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
285 
286     if (log)
287     {
288         SBStream s;
289         file.GetDescription (s);
290         log->Printf ("SBCommandInterpreter(%p)::HandleCommandsFromFile (file=\"%s\", SBCommandReturnObject(%p))",
291                      static_cast<void*>(m_opaque_ptr), s.GetData(),
292                      static_cast<void*>(result.get()));
293     }
294 
295     if (!m_opaque_ptr)
296     {
297         result->AppendError ("SBCommandInterpreter is not valid.");
298         result->SetStatus (eReturnStatusFailed);
299         return;
300     }
301 
302     if (!file.IsValid())
303     {
304         SBStream s;
305         file.GetDescription (s);
306         result->AppendErrorWithFormat ("File is not valid: %s.", s.GetData());
307         result->SetStatus (eReturnStatusFailed);
308     }
309 
310     FileSpec tmp_spec = file.ref();
311     ExecutionContext ctx, *ctx_ptr;
312     if (override_context.get())
313     {
314         ctx = override_context.get()->Lock(true);
315         ctx_ptr = &ctx;
316     }
317     else
318        ctx_ptr = nullptr;
319 
320 
321     m_opaque_ptr->HandleCommandsFromFile (tmp_spec, ctx_ptr, options.ref(), result.ref());
322 
323 }
324 
325 
326 int
327 SBCommandInterpreter::HandleCompletion (const char *current_line,
328                                         const char *cursor,
329                                         const char *last_char,
330                                         int match_start_point,
331                                         int max_return_elements,
332                                         SBStringList &matches)
333 {
334     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
335     int num_completions = 0;
336 
337     // Sanity check the arguments that are passed in:
338     // cursor & last_char have to be within the current_line.
339     if (current_line == NULL || cursor == NULL || last_char == NULL)
340         return 0;
341 
342     if (cursor < current_line || last_char < current_line)
343         return 0;
344 
345     size_t current_line_size = strlen (current_line);
346     if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
347         last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
348         return 0;
349 
350     if (log)
351         log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)",
352                      static_cast<void*>(m_opaque_ptr), current_line,
353                      static_cast<uint64_t>(cursor - current_line),
354                      static_cast<uint64_t>(last_char - current_line),
355                      match_start_point, max_return_elements);
356 
357     if (m_opaque_ptr)
358     {
359         lldb_private::StringList lldb_matches;
360         num_completions =  m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
361                                                            max_return_elements, lldb_matches);
362 
363         SBStringList temp_list (&lldb_matches);
364         matches.AppendList (temp_list);
365     }
366     if (log)
367         log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
368                      static_cast<void*>(m_opaque_ptr), num_completions);
369 
370     return num_completions;
371 }
372 
373 int
374 SBCommandInterpreter::HandleCompletion (const char *current_line,
375                   uint32_t cursor_pos,
376                   int match_start_point,
377                   int max_return_elements,
378                   lldb::SBStringList &matches)
379 {
380     const char *cursor = current_line + cursor_pos;
381     const char *last_char = current_line + strlen (current_line);
382     return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
383 }
384 
385 bool
386 SBCommandInterpreter::HasCommands ()
387 {
388     if (m_opaque_ptr)
389         return m_opaque_ptr->HasCommands();
390     return false;
391 }
392 
393 bool
394 SBCommandInterpreter::HasAliases ()
395 {
396     if (m_opaque_ptr)
397         return m_opaque_ptr->HasAliases();
398     return false;
399 }
400 
401 bool
402 SBCommandInterpreter::HasAliasOptions ()
403 {
404     if (m_opaque_ptr)
405         return m_opaque_ptr->HasAliasOptions ();
406     return false;
407 }
408 
409 SBProcess
410 SBCommandInterpreter::GetProcess ()
411 {
412     SBProcess sb_process;
413     ProcessSP process_sp;
414     if (m_opaque_ptr)
415     {
416         TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
417         if (target_sp)
418         {
419             Mutex::Locker api_locker(target_sp->GetAPIMutex());
420             process_sp = target_sp->GetProcessSP();
421             sb_process.SetSP(process_sp);
422         }
423     }
424     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
425 
426     if (log)
427         log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
428                      static_cast<void*>(m_opaque_ptr),
429                      static_cast<void*>(process_sp.get()));
430 
431     return sb_process;
432 }
433 
434 SBDebugger
435 SBCommandInterpreter::GetDebugger ()
436 {
437     SBDebugger sb_debugger;
438     if (m_opaque_ptr)
439         sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
440     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
441 
442     if (log)
443         log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
444                      static_cast<void*>(m_opaque_ptr),
445                      static_cast<void*>(sb_debugger.get()));
446 
447     return sb_debugger;
448 }
449 
450 bool
451 SBCommandInterpreter::GetPromptOnQuit()
452 {
453     if (m_opaque_ptr)
454         return m_opaque_ptr->GetPromptOnQuit();
455     return false;
456 }
457 
458 void
459 SBCommandInterpreter::SetPromptOnQuit (bool b)
460 {
461     if (m_opaque_ptr)
462         m_opaque_ptr->SetPromptOnQuit(b);
463 }
464 
465 CommandInterpreter *
466 SBCommandInterpreter::get ()
467 {
468     return m_opaque_ptr;
469 }
470 
471 CommandInterpreter &
472 SBCommandInterpreter::ref ()
473 {
474     assert (m_opaque_ptr);
475     return *m_opaque_ptr;
476 }
477 
478 void
479 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
480 {
481     m_opaque_ptr = interpreter;
482 }
483 
484 void
485 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
486 {
487     result.Clear();
488     if (m_opaque_ptr)
489     {
490         TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
491         Mutex::Locker api_locker;
492         if (target_sp)
493             api_locker.Lock(target_sp->GetAPIMutex());
494         m_opaque_ptr->SourceInitFile (false, result.ref());
495     }
496     else
497     {
498         result->AppendError ("SBCommandInterpreter is not valid");
499         result->SetStatus (eReturnStatusFailed);
500     }
501     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
502 
503     if (log)
504         log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
505                      static_cast<void*>(m_opaque_ptr),
506                      static_cast<void*>(result.get()));
507 }
508 
509 void
510 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
511 {
512     result.Clear();
513     if (m_opaque_ptr)
514     {
515         TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
516         Mutex::Locker api_locker;
517         if (target_sp)
518             api_locker.Lock(target_sp->GetAPIMutex());
519         m_opaque_ptr->SourceInitFile (true, result.ref());
520     }
521     else
522     {
523         result->AppendError ("SBCommandInterpreter is not valid");
524         result->SetStatus (eReturnStatusFailed);
525     }
526     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
527 
528     if (log)
529         log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
530                      static_cast<void*>(m_opaque_ptr),
531                      static_cast<void*>(result.get()));
532 }
533 
534 SBBroadcaster
535 SBCommandInterpreter::GetBroadcaster ()
536 {
537     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
538 
539     SBBroadcaster broadcaster (m_opaque_ptr, false);
540 
541     if (log)
542         log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
543                      static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get()));
544 
545     return broadcaster;
546 }
547 
548 const char *
549 SBCommandInterpreter::GetBroadcasterClass ()
550 {
551     return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
552 }
553 
554 const char *
555 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
556 {
557     return CommandObject::GetArgumentTypeAsCString (arg_type);
558 }
559 
560 const char *
561 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
562 {
563     return CommandObject::GetArgumentDescriptionAsCString (arg_type);
564 }
565 
566 bool
567 SBCommandInterpreter::EventIsCommandInterpreterEvent (const lldb::SBEvent &event)
568 {
569     return event.GetBroadcasterClass() == SBCommandInterpreter::GetBroadcasterClass();
570 }
571 
572 bool
573 SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
574                                                   lldb::CommandOverrideCallback callback,
575                                                   void *baton)
576 {
577     if (command_name && command_name[0] && m_opaque_ptr)
578     {
579         std::string command_name_str (command_name);
580         CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
581         if (cmd_obj)
582         {
583             assert(command_name_str.empty());
584             cmd_obj->SetOverrideCallback (callback, baton);
585             return true;
586         }
587     }
588     return false;
589 }
590 
591 lldb::SBCommand
592 SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
593 {
594     CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
595     new_command->SetRemovable (true);
596     lldb::CommandObjectSP new_command_sp(new_command);
597     if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
598         return lldb::SBCommand(new_command_sp);
599     return lldb::SBCommand();
600 }
601 
602 lldb::SBCommand
603 SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
604 {
605     lldb::CommandObjectSP new_command_sp;
606     new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
607 
608     if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
609         return lldb::SBCommand(new_command_sp);
610     return lldb::SBCommand();
611 }
612 
613 SBCommand::SBCommand ()
614 {}
615 
616 SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
617 {}
618 
619 bool
620 SBCommand::IsValid ()
621 {
622     return (bool)m_opaque_sp;
623 }
624 
625 const char*
626 SBCommand::GetName ()
627 {
628     if (IsValid ())
629         return m_opaque_sp->GetCommandName ();
630     return NULL;
631 }
632 
633 const char*
634 SBCommand::GetHelp ()
635 {
636     if (IsValid ())
637         return m_opaque_sp->GetHelp ();
638     return NULL;
639 }
640 
641 const char*
642 SBCommand::GetHelpLong ()
643 {
644     if (IsValid ())
645         return m_opaque_sp->GetHelpLong ();
646     return NULL;
647 }
648 
649 void
650 SBCommand::SetHelp (const char* help)
651 {
652     if (IsValid())
653         m_opaque_sp->SetHelp(help);
654 }
655 
656 void
657 SBCommand::SetHelpLong (const char* help)
658 {
659     if (IsValid())
660         m_opaque_sp->SetHelpLong(help);
661 }
662 
663 lldb::SBCommand
664 SBCommand::AddMultiwordCommand (const char* name, const char* help)
665 {
666     if (!IsValid ())
667         return lldb::SBCommand();
668     if (m_opaque_sp->IsMultiwordObject() == false)
669         return lldb::SBCommand();
670     CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
671     new_command->SetRemovable (true);
672     lldb::CommandObjectSP new_command_sp(new_command);
673     if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
674         return lldb::SBCommand(new_command_sp);
675     return lldb::SBCommand();
676 }
677 
678 lldb::SBCommand
679 SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
680 {
681     if (!IsValid ())
682         return lldb::SBCommand();
683     if (m_opaque_sp->IsMultiwordObject() == false)
684         return lldb::SBCommand();
685     lldb::CommandObjectSP new_command_sp;
686     new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
687     if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
688         return lldb::SBCommand(new_command_sp);
689     return lldb::SBCommand();
690 }
691