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