1 //===-- IOHandler.h ---------------------------------------------*- 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 #ifndef liblldb_IOHandler_h_
11 #define liblldb_IOHandler_h_
12 
13 // C Includes
14 #include <string.h>
15 
16 // C++ Includes
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <vector>
21 
22 // Other libraries and framework includes
23 // Project includes
24 #include "lldb/Core/ConstString.h"
25 #include "lldb/Core/Error.h"
26 #include "lldb/Core/Flags.h"
27 #include "lldb/Core/Stream.h"
28 #include "lldb/Core/StringList.h"
29 #include "lldb/Core/ValueObjectList.h"
30 #include "lldb/Host/Predicate.h"
31 #include "lldb/lldb-enumerations.h"
32 #include "lldb/lldb-public.h"
33 
34 namespace curses {
35 class Application;
36 typedef std::unique_ptr<Application> ApplicationAP;
37 } // namespace curses
38 
39 namespace lldb_private {
40 
41 class IOHandler {
42 public:
43   enum class Type {
44     CommandInterpreter,
45     CommandList,
46     Confirm,
47     Curses,
48     Expression,
49     REPL,
50     ProcessIO,
51     PythonInterpreter,
52     PythonCode,
53     Other
54   };
55 
56   IOHandler(Debugger &debugger, IOHandler::Type type);
57 
58   IOHandler(Debugger &debugger, IOHandler::Type type,
59             const lldb::StreamFileSP &input_sp,
60             const lldb::StreamFileSP &output_sp,
61             const lldb::StreamFileSP &error_sp, uint32_t flags);
62 
63   virtual ~IOHandler();
64 
65   // Each IOHandler gets to run until it is done. It should read data
66   // from the "in" and place output into "out" and "err and return
67   // when done.
68   virtual void Run() = 0;
69 
70   // Called when an input reader should relinquish its control so another
71   // can be pushed onto the IO handler stack, or so the current IO
72   // handler can pop itself off the stack
73 
74   virtual void Cancel() = 0;
75 
76   // Called when CTRL+C is pressed which usually causes
77   // Debugger::DispatchInputInterrupt to be called.
78 
79   virtual bool Interrupt() = 0;
80 
81   virtual void GotEOF() = 0;
82 
83   virtual bool IsActive() { return m_active && !m_done; }
84 
85   virtual void SetIsDone(bool b) { m_done = b; }
86 
87   virtual bool GetIsDone() { return m_done; }
88 
89   Type GetType() const { return m_type; }
90 
91   virtual void Activate() { m_active = true; }
92 
93   virtual void Deactivate() { m_active = false; }
94 
95   virtual const char *GetPrompt() {
96     // Prompt support isn't mandatory
97     return nullptr;
98   }
99 
100   virtual bool SetPrompt(llvm::StringRef prompt) {
101     // Prompt support isn't mandatory
102     return false;
103   }
104   bool SetPrompt(const char *) = delete;
105 
106   virtual ConstString GetControlSequence(char ch) { return ConstString(); }
107 
108   virtual const char *GetCommandPrefix() { return nullptr; }
109 
110   virtual const char *GetHelpPrologue() { return nullptr; }
111 
112   int GetInputFD();
113 
114   int GetOutputFD();
115 
116   int GetErrorFD();
117 
118   FILE *GetInputFILE();
119 
120   FILE *GetOutputFILE();
121 
122   FILE *GetErrorFILE();
123 
124   lldb::StreamFileSP &GetInputStreamFile();
125 
126   lldb::StreamFileSP &GetOutputStreamFile();
127 
128   lldb::StreamFileSP &GetErrorStreamFile();
129 
130   Debugger &GetDebugger() { return m_debugger; }
131 
132   void *GetUserData() { return m_user_data; }
133 
134   void SetUserData(void *user_data) { m_user_data = user_data; }
135 
136   Flags &GetFlags() { return m_flags; }
137 
138   const Flags &GetFlags() const { return m_flags; }
139 
140   //------------------------------------------------------------------
141   /// Check if the input is being supplied interactively by a user
142   ///
143   /// This will return true if the input stream is a terminal (tty or
144   /// pty) and can cause IO handlers to do different things (like
145   /// for a confirmation when deleting all breakpoints).
146   //------------------------------------------------------------------
147   bool GetIsInteractive();
148 
149   //------------------------------------------------------------------
150   /// Check if the input is coming from a real terminal.
151   ///
152   /// A real terminal has a valid size with a certain number of rows
153   /// and columns. If this function returns true, then terminal escape
154   /// sequences are expected to work (cursor movement escape sequences,
155   /// clearing lines, etc).
156   //------------------------------------------------------------------
157   bool GetIsRealTerminal();
158 
159   void SetPopped(bool b);
160 
161   void WaitForPop();
162 
163   virtual void PrintAsync(Stream *stream, const char *s, size_t len) {
164     stream->Write(s, len);
165     stream->Flush();
166   }
167 
168 protected:
169   Debugger &m_debugger;
170   lldb::StreamFileSP m_input_sp;
171   lldb::StreamFileSP m_output_sp;
172   lldb::StreamFileSP m_error_sp;
173   Predicate<bool> m_popped;
174   Flags m_flags;
175   Type m_type;
176   void *m_user_data;
177   bool m_done;
178   bool m_active;
179 
180 private:
181   DISALLOW_COPY_AND_ASSIGN(IOHandler);
182 };
183 
184 //------------------------------------------------------------------
185 /// A delegate class for use with IOHandler subclasses.
186 ///
187 /// The IOHandler delegate is designed to be mixed into classes so
188 /// they can use an IOHandler subclass to fetch input and notify the
189 /// object that inherits from this delegate class when a token is
190 /// received.
191 //------------------------------------------------------------------
192 class IOHandlerDelegate {
193 public:
194   enum class Completion { None, LLDBCommand, Expression };
195 
196   IOHandlerDelegate(Completion completion = Completion::None)
197       : m_completion(completion), m_io_handler_done(false) {}
198 
199   virtual ~IOHandlerDelegate() = default;
200 
201   virtual void IOHandlerActivated(IOHandler &io_handler) {}
202 
203   virtual void IOHandlerDeactivated(IOHandler &io_handler) {}
204 
205   virtual int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
206                                 const char *cursor, const char *last_char,
207                                 int skip_first_n_matches, int max_matches,
208                                 StringList &matches);
209 
210   virtual const char *IOHandlerGetFixIndentationCharacters() { return nullptr; }
211 
212   //------------------------------------------------------------------
213   /// Called when a new line is created or one of an identified set of
214   /// indentation characters is typed.
215   ///
216   /// This function determines how much indentation should be added
217   /// or removed to match the recommended amount for the final line.
218   ///
219   /// @param[in] io_handler
220   ///     The IOHandler that responsible for input.
221   ///
222   /// @param[in] lines
223   ///     The current input up to the line to be corrected.  Lines
224   ///     following the line containing the cursor are not included.
225   ///
226   /// @param[in] cursor_position
227   ///     The number of characters preceding the cursor on the final
228   ///     line at the time.
229   ///
230   /// @return
231   ///     Returns an integer describing the number of spaces needed
232   ///     to correct the indentation level.  Positive values indicate
233   ///     that spaces should be added, while negative values represent
234   ///     spaces that should be removed.
235   //------------------------------------------------------------------
236   virtual int IOHandlerFixIndentation(IOHandler &io_handler,
237                                       const StringList &lines,
238                                       int cursor_position) {
239     return 0;
240   }
241 
242   //------------------------------------------------------------------
243   /// Called when a line or lines have been retrieved.
244   ///
245   /// This function can handle the current line and possibly call
246   /// IOHandler::SetIsDone(true) when the IO handler is done like when
247   /// "quit" is entered as a command, of when an empty line is
248   /// received. It is up to the delegate to determine when a line
249   /// should cause a IOHandler to exit.
250   //------------------------------------------------------------------
251   virtual void IOHandlerInputComplete(IOHandler &io_handler,
252                                       std::string &data) = 0;
253 
254   virtual void IOHandlerInputInterrupted(IOHandler &io_handler,
255                                          std::string &data) {}
256 
257   //------------------------------------------------------------------
258   /// Called to determine whether typing enter after the last line in
259   /// \a lines should end input.  This function will not be called on
260   /// IOHandler objects that are getting single lines.
261   /// @param[in] io_handler
262   ///     The IOHandler that responsible for updating the lines.
263   ///
264   /// @param[in] lines
265   ///     The current multi-line content.  May be altered to provide
266   ///     alternative input when complete.
267   ///
268   /// @return
269   ///     Return an boolean to indicate whether input is complete,
270   ///     true indicates that no additional input is necessary, while
271   ///     false indicates that more input is required.
272   //------------------------------------------------------------------
273   virtual bool IOHandlerIsInputComplete(IOHandler &io_handler,
274                                         StringList &lines) {
275     // Impose no requirements for input to be considered
276     // complete.  subclasses should do something more intelligent.
277     return true;
278   }
279 
280   virtual ConstString IOHandlerGetControlSequence(char ch) {
281     return ConstString();
282   }
283 
284   virtual const char *IOHandlerGetCommandPrefix() { return nullptr; }
285 
286   virtual const char *IOHandlerGetHelpPrologue() { return nullptr; }
287 
288   //------------------------------------------------------------------
289   // Intercept the IOHandler::Interrupt() calls and do something.
290   //
291   // Return true if the interrupt was handled, false if the IOHandler
292   // should continue to try handle the interrupt itself.
293   //------------------------------------------------------------------
294   virtual bool IOHandlerInterrupt(IOHandler &io_handler) { return false; }
295 
296 protected:
297   Completion m_completion; // Support for common builtin completions
298   bool m_io_handler_done;
299 };
300 
301 //----------------------------------------------------------------------
302 // IOHandlerDelegateMultiline
303 //
304 // A IOHandlerDelegate that handles terminating multi-line input when
305 // the last line is equal to "end_line" which is specified in the
306 // constructor.
307 //----------------------------------------------------------------------
308 class IOHandlerDelegateMultiline : public IOHandlerDelegate {
309 public:
310   IOHandlerDelegateMultiline(const char *end_line,
311                              Completion completion = Completion::None)
312       : IOHandlerDelegate(completion),
313         m_end_line((end_line && end_line[0]) ? end_line : "") {}
314 
315   ~IOHandlerDelegateMultiline() override = default;
316 
317   ConstString IOHandlerGetControlSequence(char ch) override {
318     if (ch == 'd')
319       return ConstString(m_end_line + "\n");
320     return ConstString();
321   }
322 
323   bool IOHandlerIsInputComplete(IOHandler &io_handler,
324                                 StringList &lines) override {
325     // Determine whether the end of input signal has been entered
326     const size_t num_lines = lines.GetSize();
327     if (num_lines > 0 && lines[num_lines - 1] == m_end_line) {
328       // Remove the terminal line from "lines" so it doesn't appear in
329       // the resulting input and return true to indicate we are done
330       // getting lines
331       lines.PopBack();
332       return true;
333     }
334     return false;
335   }
336 
337 protected:
338   const std::string m_end_line;
339 };
340 
341 class IOHandlerEditline : public IOHandler {
342 public:
343   IOHandlerEditline(Debugger &debugger, IOHandler::Type type,
344                     const char *editline_name, // Used for saving history files
345                     llvm::StringRef prompt, llvm::StringRef continuation_prompt,
346                     bool multi_line, bool color_prompts,
347                     uint32_t line_number_start, // If non-zero show line numbers
348                                                 // starting at
349                                                 // 'line_number_start'
350                     IOHandlerDelegate &delegate);
351 
352   IOHandlerEditline(Debugger &debugger, IOHandler::Type type,
353                     const lldb::StreamFileSP &input_sp,
354                     const lldb::StreamFileSP &output_sp,
355                     const lldb::StreamFileSP &error_sp, uint32_t flags,
356                     const char *editline_name, // Used for saving history files
357                     llvm::StringRef prompt, llvm::StringRef continuation_prompt,
358                     bool multi_line, bool color_prompts,
359                     uint32_t line_number_start, // If non-zero show line numbers
360                                                 // starting at
361                                                 // 'line_number_start'
362                     IOHandlerDelegate &delegate);
363 
364   IOHandlerEditline(Debugger &, IOHandler::Type, const char *, const char *,
365                     const char *, bool, bool, uint32_t,
366                     IOHandlerDelegate &) = delete;
367 
368   IOHandlerEditline(Debugger &, IOHandler::Type, const lldb::StreamFileSP &,
369                     const lldb::StreamFileSP &, const lldb::StreamFileSP &,
370                     uint32_t, const char *, const char *, const char *, bool,
371                     bool, uint32_t, IOHandlerDelegate &) = delete;
372 
373   ~IOHandlerEditline() override;
374 
375   void Run() override;
376 
377   void Cancel() override;
378 
379   bool Interrupt() override;
380 
381   void GotEOF() override;
382 
383   void Activate() override;
384 
385   void Deactivate() override;
386 
387   ConstString GetControlSequence(char ch) override {
388     return m_delegate.IOHandlerGetControlSequence(ch);
389   }
390 
391   const char *GetCommandPrefix() override {
392     return m_delegate.IOHandlerGetCommandPrefix();
393   }
394 
395   const char *GetHelpPrologue() override {
396     return m_delegate.IOHandlerGetHelpPrologue();
397   }
398 
399   const char *GetPrompt() override;
400 
401   bool SetPrompt(llvm::StringRef prompt) override;
402   bool SetPrompt(const char *prompt) = delete;
403 
404   const char *GetContinuationPrompt();
405 
406   void SetContinuationPrompt(llvm::StringRef prompt);
407   void SetContinuationPrompt(const char *) = delete;
408 
409   bool GetLine(std::string &line, bool &interrupted);
410 
411   bool GetLines(StringList &lines, bool &interrupted);
412 
413   void SetBaseLineNumber(uint32_t line);
414 
415   bool GetInterruptExits() { return m_interrupt_exits; }
416 
417   void SetInterruptExits(bool b) { m_interrupt_exits = b; }
418 
419   const StringList *GetCurrentLines() const { return m_current_lines_ptr; }
420 
421   uint32_t GetCurrentLineIndex() const;
422 
423   void PrintAsync(Stream *stream, const char *s, size_t len) override;
424 
425 private:
426 #ifndef LLDB_DISABLE_LIBEDIT
427   static bool IsInputCompleteCallback(Editline *editline, StringList &lines,
428                                       void *baton);
429 
430   static int FixIndentationCallback(Editline *editline, const StringList &lines,
431                                     int cursor_position, void *baton);
432 
433   static int AutoCompleteCallback(const char *current_line, const char *cursor,
434                                   const char *last_char,
435                                   int skip_first_n_matches, int max_matches,
436                                   StringList &matches, void *baton);
437 #endif
438 
439 protected:
440 #ifndef LLDB_DISABLE_LIBEDIT
441   std::unique_ptr<Editline> m_editline_ap;
442 #endif
443   IOHandlerDelegate &m_delegate;
444   std::string m_prompt;
445   std::string m_continuation_prompt;
446   StringList *m_current_lines_ptr;
447   uint32_t m_base_line_number; // If non-zero, then show line numbers in prompt
448   uint32_t m_curr_line_idx;
449   bool m_multi_line;
450   bool m_color_prompts;
451   bool m_interrupt_exits;
452   bool m_editing; // Set to true when fetching a line manually (not using
453                   // libedit)
454 };
455 
456 // The order of base classes is important. Look at the constructor of
457 // IOHandlerConfirm
458 // to see how.
459 class IOHandlerConfirm : public IOHandlerDelegate, public IOHandlerEditline {
460 public:
461   IOHandlerConfirm(Debugger &debugger, llvm::StringRef prompt,
462                    bool default_response);
463 
464   ~IOHandlerConfirm() override;
465 
466   bool GetResponse() const { return m_user_response; }
467 
468   int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
469                         const char *cursor, const char *last_char,
470                         int skip_first_n_matches, int max_matches,
471                         StringList &matches) override;
472 
473   void IOHandlerInputComplete(IOHandler &io_handler,
474                               std::string &data) override;
475 
476 protected:
477   const bool m_default_response;
478   bool m_user_response;
479 };
480 
481 class IOHandlerCursesGUI : public IOHandler {
482 public:
483   IOHandlerCursesGUI(Debugger &debugger);
484 
485   ~IOHandlerCursesGUI() override;
486 
487   void Run() override;
488 
489   void Cancel() override;
490 
491   bool Interrupt() override;
492 
493   void GotEOF() override;
494 
495   void Activate() override;
496 
497   void Deactivate() override;
498 
499 protected:
500   curses::ApplicationAP m_app_ap;
501 };
502 
503 class IOHandlerCursesValueObjectList : public IOHandler {
504 public:
505   IOHandlerCursesValueObjectList(Debugger &debugger,
506                                  ValueObjectList &valobj_list);
507 
508   ~IOHandlerCursesValueObjectList() override;
509 
510   void Run() override;
511 
512   void GotEOF() override;
513 
514 protected:
515   ValueObjectList m_valobj_list;
516 };
517 
518 class IOHandlerStack {
519 public:
520   IOHandlerStack() : m_stack(), m_mutex(), m_top(nullptr) {}
521 
522   ~IOHandlerStack() = default;
523 
524   size_t GetSize() const {
525     std::lock_guard<std::recursive_mutex> guard(m_mutex);
526     return m_stack.size();
527   }
528 
529   void Push(const lldb::IOHandlerSP &sp) {
530     if (sp) {
531       std::lock_guard<std::recursive_mutex> guard(m_mutex);
532       sp->SetPopped(false);
533       m_stack.push_back(sp);
534       // Set m_top the non-locking IsTop() call
535       m_top = sp.get();
536     }
537   }
538 
539   bool IsEmpty() const {
540     std::lock_guard<std::recursive_mutex> guard(m_mutex);
541     return m_stack.empty();
542   }
543 
544   lldb::IOHandlerSP Top() {
545     lldb::IOHandlerSP sp;
546     {
547       std::lock_guard<std::recursive_mutex> guard(m_mutex);
548       if (!m_stack.empty())
549         sp = m_stack.back();
550     }
551     return sp;
552   }
553 
554   void Pop() {
555     std::lock_guard<std::recursive_mutex> guard(m_mutex);
556     if (!m_stack.empty()) {
557       lldb::IOHandlerSP sp(m_stack.back());
558       m_stack.pop_back();
559       sp->SetPopped(true);
560     }
561     // Set m_top the non-locking IsTop() call
562 
563     m_top = (m_stack.empty() ? nullptr : m_stack.back().get());
564   }
565 
566   std::recursive_mutex &GetMutex() { return m_mutex; }
567 
568   bool IsTop(const lldb::IOHandlerSP &io_handler_sp) const {
569     return m_top == io_handler_sp.get();
570   }
571 
572   bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
573                               IOHandler::Type second_top_type) {
574     std::lock_guard<std::recursive_mutex> guard(m_mutex);
575     const size_t num_io_handlers = m_stack.size();
576     return (num_io_handlers >= 2 &&
577             m_stack[num_io_handlers - 1]->GetType() == top_type &&
578             m_stack[num_io_handlers - 2]->GetType() == second_top_type);
579   }
580 
581   ConstString GetTopIOHandlerControlSequence(char ch) {
582     return ((m_top != nullptr) ? m_top->GetControlSequence(ch) : ConstString());
583   }
584 
585   const char *GetTopIOHandlerCommandPrefix() {
586     return ((m_top != nullptr) ? m_top->GetCommandPrefix() : nullptr);
587   }
588 
589   const char *GetTopIOHandlerHelpPrologue() {
590     return ((m_top != nullptr) ? m_top->GetHelpPrologue() : nullptr);
591   }
592 
593   void PrintAsync(Stream *stream, const char *s, size_t len);
594 
595 protected:
596   typedef std::vector<lldb::IOHandlerSP> collection;
597   collection m_stack;
598   mutable std::recursive_mutex m_mutex;
599   IOHandler *m_top;
600 
601 private:
602   DISALLOW_COPY_AND_ASSIGN(IOHandlerStack);
603 };
604 
605 } // namespace lldb_private
606 
607 #endif // liblldb_IOHandler_h_
608