]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/IOHandler.h
Add ELF Tool Chain's ar(1) and elfdump(1) to contrib
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / IOHandler.h
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 #include <string.h>
14
15 #include <stack>
16
17 #include "lldb/lldb-public.h"
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/Flags.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Core/StringList.h"
24 #include "lldb/Core/ValueObjectList.h"
25 #include "lldb/Host/Mutex.h"
26 #include "lldb/Host/Predicate.h"
27
28 namespace curses
29 {
30     class Application;
31     typedef std::unique_ptr<Application> ApplicationAP;
32 }
33
34 namespace lldb_private {
35
36     class IOHandler
37     {
38     public:
39         enum class Type {
40             CommandInterpreter,
41             CommandList,
42             Confirm,
43             Curses,
44             Expression,
45             ProcessIO,
46             PythonInterpreter,
47             PythonCode,
48             Other
49         };
50
51         IOHandler (Debugger &debugger,
52                    IOHandler::Type type);
53
54         IOHandler (Debugger &debugger,
55                    IOHandler::Type type,
56                    const lldb::StreamFileSP &input_sp,
57                    const lldb::StreamFileSP &output_sp,
58                    const lldb::StreamFileSP &error_sp,
59                    uint32_t flags);
60
61         virtual
62         ~IOHandler ();
63
64         // Each IOHandler gets to run until it is done. It should read data
65         // from the "in" and place output into "out" and "err and return
66         // when done.
67         virtual void
68         Run () = 0;
69
70         // Hide any characters that have been displayed so far so async
71         // output can be displayed. Refresh() will be called after the
72         // output has been displayed.
73         virtual void
74         Hide () = 0;
75         
76         // Called when the async output has been received in order to update
77         // the input reader (refresh the prompt and redisplay any current
78         // line(s) that are being edited
79         virtual void
80         Refresh () = 0;
81
82         // Called when an input reader should relinquish its control so another
83         // can be pushed onto the IO handler stack, or so the current IO
84         // handler can pop itself off the stack
85
86         virtual void
87         Cancel () = 0;
88
89         // Called when CTRL+C is pressed which usually causes
90         // Debugger::DispatchInputInterrupt to be called.
91         
92         virtual bool
93         Interrupt () = 0;
94         
95         virtual void
96         GotEOF() = 0;
97         
98         virtual bool
99         IsActive ()
100         {
101             return m_active && !m_done;
102         }
103
104         virtual void
105         SetIsDone (bool b)
106         {
107             m_done = b;
108         }
109
110         virtual bool
111         GetIsDone ()
112         {
113             return m_done;
114         }
115
116         Type
117         GetType () const
118         {
119             return m_type;
120         }
121
122         virtual void
123         Activate ()
124         {
125             m_active = true;
126         }
127         
128         virtual void
129         Deactivate ()
130         {
131             m_active = false;
132         }
133
134         virtual const char *
135         GetPrompt ()
136         {
137             // Prompt support isn't mandatory
138             return NULL;
139         }
140         
141         virtual bool
142         SetPrompt (const char *prompt)
143         {
144             // Prompt support isn't mandatory
145             return false;
146         }
147         
148         virtual ConstString
149         GetControlSequence (char ch)
150         {
151             return ConstString();
152         }
153
154         virtual const char *
155         GetCommandPrefix ()
156         {
157             return NULL;
158         }
159         
160         virtual const char *
161         GetHelpPrologue()
162         {
163             return NULL;
164         }
165
166         int
167         GetInputFD();
168         
169         int
170         GetOutputFD();
171         
172         int
173         GetErrorFD();
174
175         FILE *
176         GetInputFILE();
177         
178         FILE *
179         GetOutputFILE();
180         
181         FILE *
182         GetErrorFILE();
183
184         lldb::StreamFileSP &
185         GetInputStreamFile();
186         
187         lldb::StreamFileSP &
188         GetOutputStreamFile();
189         
190         lldb::StreamFileSP &
191         GetErrorStreamFile();
192
193         Debugger &
194         GetDebugger()
195         {
196             return m_debugger;
197         }
198
199         void *
200         GetUserData ()
201         {
202             return m_user_data;
203         }
204
205         void
206         SetUserData (void *user_data)
207         {
208             m_user_data = user_data;
209         }
210
211         Flags &
212         GetFlags ()
213         {
214             return m_flags;
215         }
216
217         const Flags &
218         GetFlags () const
219         {
220             return m_flags;
221         }
222
223         //------------------------------------------------------------------
224         /// Check if the input is being supplied interactively by a user
225         ///
226         /// This will return true if the input stream is a terminal (tty or
227         /// pty) and can cause IO handlers to do different things (like
228         /// for a confirmation when deleting all breakpoints).
229         //------------------------------------------------------------------
230         bool
231         GetIsInteractive ();
232
233         //------------------------------------------------------------------
234         /// Check if the input is coming from a real terminal.
235         ///
236         /// A real terminal has a valid size with a certain number of rows
237         /// and columns. If this function returns true, then terminal escape
238         /// sequences are expected to work (cursor movement escape sequences,
239         /// clearing lines, etc).
240         //------------------------------------------------------------------
241         bool
242         GetIsRealTerminal ();
243         
244         void
245         SetPopped (bool b);
246         
247         void
248         WaitForPop ();
249         
250     protected:
251         Debugger &m_debugger;
252         lldb::StreamFileSP m_input_sp;
253         lldb::StreamFileSP m_output_sp;
254         lldb::StreamFileSP m_error_sp;
255         Predicate<bool> m_popped;
256         Flags m_flags;
257         Type m_type;
258         void *m_user_data;
259         bool m_done;
260         bool m_active;
261
262     private:
263         DISALLOW_COPY_AND_ASSIGN (IOHandler);
264     };
265
266     
267     //------------------------------------------------------------------
268     /// A delegate class for use with IOHandler subclasses.
269     ///
270     /// The IOHandler delegate is designed to be mixed into classes so
271     /// they can use an IOHandler subclass to fetch input and notify the
272     /// object that inherits from this delegate class when a token is
273     /// received.
274     //------------------------------------------------------------------
275     class IOHandlerDelegate
276     {
277     public:
278         enum class Completion {
279             None,
280             LLDBCommand,
281             Expression
282         };
283         
284         IOHandlerDelegate (Completion completion = Completion::None) :
285             m_completion(completion),
286             m_io_handler_done (false)
287         {
288         }
289         
290         virtual
291         ~IOHandlerDelegate()
292         {
293         }
294         
295         virtual void
296         IOHandlerActivated (IOHandler &io_handler)
297         {
298         }
299
300         virtual void
301         IOHandlerDeactivated (IOHandler &io_handler)
302         {
303         }
304
305         virtual int
306         IOHandlerComplete (IOHandler &io_handler,
307                            const char *current_line,
308                            const char *cursor,
309                            const char *last_char,
310                            int skip_first_n_matches,
311                            int max_matches,
312                            StringList &matches);
313         
314         virtual const char *
315         IOHandlerGetFixIndentationCharacters ()
316         {
317             return NULL;
318         }
319         
320         //------------------------------------------------------------------
321         /// Called when a new line is created or one of an identifed set of
322         /// indentation characters is typed.
323         ///
324         /// This function determines how much indentation should be added
325         /// or removed to match the recommended amount for the final line.
326         ///
327         /// @param[in] io_handler
328         ///     The IOHandler that responsible for input.
329         ///
330         /// @param[in] lines
331         ///     The current input up to the line to be corrected.  Lines
332         ///     following the line containing the cursor are not included.
333         ///
334         /// @param[in] cursor_position
335         ///     The number of characters preceeding the cursor on the final
336         ///     line at the time.
337         ///
338         /// @return
339         ///     Returns an integer describing the number of spaces needed
340         ///     to correct the indentation level.  Positive values indicate
341         ///     that spaces should be added, while negative values represent
342         ///     spaces that should be removed.
343         //------------------------------------------------------------------
344         virtual int
345         IOHandlerFixIndentation (IOHandler &io_handler,
346                                  const StringList &lines,
347                                  int cursor_position)
348         {
349             return 0;
350         }
351                         
352         //------------------------------------------------------------------
353         /// Called when a line or lines have been retrieved.
354         ///
355         /// This function can handle the current line and possibly call
356         /// IOHandler::SetIsDone(true) when the IO handler is done like when
357         /// "quit" is entered as a command, of when an empty line is
358         /// received. It is up to the delegate to determine when a line
359         /// should cause a IOHandler to exit.
360         //------------------------------------------------------------------
361         virtual void
362         IOHandlerInputComplete (IOHandler &io_handler, std::string &data) = 0;
363
364         virtual void
365         IOHandlerInputInterrupted (IOHandler &io_handler, std::string &data)
366         {
367         }
368
369         //------------------------------------------------------------------
370         /// Called to determine whether typing enter after the last line in
371         /// \a lines should end input.  This function will not be called on
372         /// IOHandler objects that are getting single lines.
373         /// @param[in] io_handler
374         ///     The IOHandler that responsible for updating the lines.
375         ///
376         /// @param[in] lines
377         ///     The current multi-line content.  May be altered to provide
378         ///     alternative input when complete.
379         ///
380         /// @return
381         ///     Return an boolean to indicate whether input is complete,
382         ///     true indicates that no additional input is necessary, while
383         ///     false indicates that more input is required.
384         //------------------------------------------------------------------
385         virtual bool
386         IOHandlerIsInputComplete (IOHandler &io_handler,
387                                   StringList &lines)
388         {
389             // Impose no requirements for input to be considered
390             // complete.  subclasses should do something more intelligent.
391             return true;
392         }
393         
394         virtual ConstString
395         IOHandlerGetControlSequence (char ch)
396         {
397             return ConstString();
398         }
399         
400         virtual const char *
401         IOHandlerGetCommandPrefix ()
402         {
403             return NULL;
404         }
405
406         virtual const char *
407         IOHandlerGetHelpPrologue ()
408         {
409             return NULL;
410         }
411
412         //------------------------------------------------------------------
413         // Intercept the IOHandler::Interrupt() calls and do something.
414         //
415         // Return true if the interrupt was handled, false if the IOHandler
416         // should continue to try handle the interrupt itself.
417         //------------------------------------------------------------------
418         virtual bool
419         IOHandlerInterrupt (IOHandler &io_handler)
420         {
421             return false;
422         }
423     protected:
424         Completion m_completion; // Support for common builtin completions
425         bool m_io_handler_done;
426     };
427
428     //----------------------------------------------------------------------
429     // IOHandlerDelegateMultiline
430     //
431     // A IOHandlerDelegate that handles terminating multi-line input when
432     // the last line is equal to "end_line" which is specified in the
433     // constructor.
434     //----------------------------------------------------------------------
435     class IOHandlerDelegateMultiline :
436         public IOHandlerDelegate
437     {
438     public:
439         IOHandlerDelegateMultiline (const char *end_line,
440                                     Completion completion = Completion::None) :
441             IOHandlerDelegate (completion),
442             m_end_line((end_line && end_line[0]) ? end_line : "")
443         {
444         }
445         
446         virtual
447         ~IOHandlerDelegateMultiline ()
448         {
449         }
450         
451         virtual ConstString
452         IOHandlerGetControlSequence (char ch)
453         {
454             if (ch == 'd')
455                 return ConstString (m_end_line + "\n");
456             return ConstString();
457         }
458
459         virtual bool
460         IOHandlerIsInputComplete (IOHandler &io_handler,
461                                   StringList &lines)
462         {
463             // Determine whether the end of input signal has been entered
464             const size_t num_lines = lines.GetSize();
465             if (num_lines > 0 && lines[num_lines - 1] == m_end_line)
466             {
467                 // Remove the terminal line from "lines" so it doesn't appear in
468                 // the resulting input and return true to indicate we are done
469                 // getting lines
470                 lines.PopBack();
471                 return true;
472             }
473             return false;
474         }
475     protected:
476         const std::string m_end_line;
477     };
478     
479     
480     class IOHandlerEditline : public IOHandler
481     {
482     public:
483         IOHandlerEditline (Debugger &debugger,
484                            IOHandler::Type type,
485                            const char *editline_name, // Used for saving history files
486                            const char *prompt,
487                            const char *continuation_prompt,
488                            bool multi_line,
489                            bool color_prompts,
490                            uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
491                            IOHandlerDelegate &delegate);
492
493         IOHandlerEditline (Debugger &debugger,
494                            IOHandler::Type type,
495                            const lldb::StreamFileSP &input_sp,
496                            const lldb::StreamFileSP &output_sp,
497                            const lldb::StreamFileSP &error_sp,
498                            uint32_t flags,
499                            const char *editline_name, // Used for saving history files
500                            const char *prompt,
501                            const char *continuation_prompt,
502                            bool multi_line,
503                            bool color_prompts,
504                            uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
505                            IOHandlerDelegate &delegate);
506         
507         virtual
508         ~IOHandlerEditline ();
509         
510         virtual void
511         Run ();
512         
513         virtual void
514         Hide ();
515
516         virtual void
517         Refresh ();
518
519         virtual void
520         Cancel ();
521
522         virtual bool
523         Interrupt ();
524         
525         virtual void
526         GotEOF();
527         
528         virtual void
529         Activate ();
530
531         virtual void
532         Deactivate ();
533
534         virtual ConstString
535         GetControlSequence (char ch)
536         {
537             return m_delegate.IOHandlerGetControlSequence (ch);
538         }
539
540         virtual const char *
541         GetCommandPrefix ()
542         {
543             return m_delegate.IOHandlerGetCommandPrefix ();
544         }
545
546         virtual const char *
547         GetHelpPrologue ()
548         {
549             return m_delegate.IOHandlerGetHelpPrologue ();
550         }
551
552         virtual const char *
553         GetPrompt ();
554         
555         virtual bool
556         SetPrompt (const char *prompt);
557         
558         const char *
559         GetContinuationPrompt ();
560         
561         void
562         SetContinuationPrompt (const char *prompt);
563         
564         bool
565         GetLine (std::string &line, bool &interrupted);
566         
567         bool
568         GetLines (StringList &lines, bool &interrupted);
569         
570         void
571         SetBaseLineNumber (uint32_t line);
572         
573         bool
574         GetInterruptExits ()
575         {
576             return m_interrupt_exits;
577         }
578
579         void
580         SetInterruptExits (bool b)
581         {
582             m_interrupt_exits = b;
583         }
584         
585         const StringList *
586         GetCurrentLines () const
587         {
588             return m_current_lines_ptr;
589         }
590         
591         uint32_t
592         GetCurrentLineIndex () const;
593
594     private:
595 #ifndef LLDB_DISABLE_LIBEDIT
596         static bool
597         IsInputCompleteCallback (Editline *editline,
598                                  StringList &lines,
599                                  void *baton);
600         
601         static int
602         FixIndentationCallback (Editline *editline,
603                                 const StringList &lines,
604                                 int cursor_position,
605                                 void *baton);
606         
607         static int AutoCompleteCallback (const char *current_line,
608                                          const char *cursor,
609                                          const char *last_char,
610                                          int skip_first_n_matches,
611                                          int max_matches,
612                                          StringList &matches,
613                                          void *baton);
614 #endif
615
616     protected:
617 #ifndef LLDB_DISABLE_LIBEDIT
618         std::unique_ptr<Editline> m_editline_ap;
619 #endif
620         IOHandlerDelegate &m_delegate;
621         std::string m_prompt;
622         std::string m_continuation_prompt;
623         StringList *m_current_lines_ptr;
624         uint32_t m_base_line_number; // If non-zero, then show line numbers in prompt
625         uint32_t m_curr_line_idx;
626         bool m_multi_line;
627         bool m_color_prompts;
628         bool m_interrupt_exits;
629     };
630     
631     // The order of base classes is important. Look at the constructor of IOHandlerConfirm
632     // to see how.
633     class IOHandlerConfirm :
634         public IOHandlerDelegate,
635         public IOHandlerEditline
636     {
637     public:
638         IOHandlerConfirm (Debugger &debugger,
639                           const char *prompt,
640                           bool default_response);
641         
642         virtual
643         ~IOHandlerConfirm ();
644                 
645         bool
646         GetResponse () const
647         {
648             return m_user_response;
649         }
650         
651         virtual int
652         IOHandlerComplete (IOHandler &io_handler,
653                            const char *current_line,
654                            const char *cursor,
655                            const char *last_char,
656                            int skip_first_n_matches,
657                            int max_matches,
658                            StringList &matches);
659         
660         virtual void
661         IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
662
663     protected:
664         const bool m_default_response;
665         bool m_user_response;
666     };
667
668     class IOHandlerCursesGUI :
669         public IOHandler
670     {
671     public:
672         IOHandlerCursesGUI (Debugger &debugger);
673         
674         virtual
675         ~IOHandlerCursesGUI ();
676         
677         virtual void
678         Run ();
679         
680         virtual void
681         Hide ();
682         
683         virtual void
684         Refresh ();
685
686         virtual void
687         Cancel ();
688
689         virtual bool
690         Interrupt ();
691         
692         virtual void
693         GotEOF();
694         
695         virtual void
696         Activate ();
697         
698         virtual void
699         Deactivate ();
700
701     protected:
702         curses::ApplicationAP m_app_ap;
703     };
704
705     class IOHandlerCursesValueObjectList :
706         public IOHandler
707     {
708     public:
709         IOHandlerCursesValueObjectList (Debugger &debugger, ValueObjectList &valobj_list);
710         
711         virtual
712         ~IOHandlerCursesValueObjectList ();
713         
714         virtual void
715         Run ();
716         
717         virtual void
718         Hide ();
719         
720         virtual void
721         Refresh ();
722         
723         virtual bool
724         HandleInterrupt ();
725         
726         virtual void
727         GotEOF();
728     protected:
729         ValueObjectList m_valobj_list;
730     };
731
732     class IOHandlerStack
733     {
734     public:
735         
736         IOHandlerStack () :
737             m_stack(),
738             m_mutex(Mutex::eMutexTypeRecursive),
739             m_top (NULL)
740         {
741         }
742         
743         ~IOHandlerStack ()
744         {
745         }
746         
747         size_t
748         GetSize () const
749         {
750             Mutex::Locker locker (m_mutex);
751             return m_stack.size();
752         }
753         
754         void
755         Push (const lldb::IOHandlerSP& sp)
756         {
757             if (sp)
758             {
759                 Mutex::Locker locker (m_mutex);
760                 sp->SetPopped (false);
761                 m_stack.push_back (sp);
762                 // Set m_top the non-locking IsTop() call
763                 m_top = sp.get();
764             }
765         }
766         
767         bool
768         IsEmpty () const
769         {
770             Mutex::Locker locker (m_mutex);
771             return m_stack.empty();
772         }
773         
774         lldb::IOHandlerSP
775         Top ()
776         {
777             lldb::IOHandlerSP sp;
778             {
779                 Mutex::Locker locker (m_mutex);
780                 if (!m_stack.empty())
781                     sp = m_stack.back();
782             }
783             return sp;
784         }
785         
786         void
787         Pop ()
788         {
789             Mutex::Locker locker (m_mutex);
790             if (!m_stack.empty())
791             {
792                 lldb::IOHandlerSP sp (m_stack.back());
793                 m_stack.pop_back();
794                 sp->SetPopped (true);
795             }
796             // Set m_top the non-locking IsTop() call
797             if (m_stack.empty())
798                 m_top = NULL;
799             else
800                 m_top = m_stack.back().get();
801         }
802
803         Mutex &
804         GetMutex()
805         {
806             return m_mutex;
807         }
808       
809         bool
810         IsTop (const lldb::IOHandlerSP &io_handler_sp) const
811         {
812             return m_top == io_handler_sp.get();
813         }
814
815         bool
816         CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
817         {
818             Mutex::Locker locker (m_mutex);
819             const size_t num_io_handlers = m_stack.size();
820             if (num_io_handlers >= 2 &&
821                 m_stack[num_io_handlers-1]->GetType() == top_type &&
822                 m_stack[num_io_handlers-2]->GetType() == second_top_type)
823             {
824                 return true;
825             }
826             return false;
827         }
828         ConstString
829         GetTopIOHandlerControlSequence (char ch)
830         {
831             if (m_top)
832                 return m_top->GetControlSequence(ch);
833             return ConstString();
834         }
835
836         const char *
837         GetTopIOHandlerCommandPrefix()
838         {
839             if (m_top)
840                 return m_top->GetCommandPrefix();
841             return NULL;
842         }
843         
844         const char *
845         GetTopIOHandlerHelpPrologue()
846         {
847             if (m_top)
848                 return m_top->GetHelpPrologue();
849             return NULL;
850         }
851
852     protected:        
853         
854         typedef std::vector<lldb::IOHandlerSP> collection;
855         collection m_stack;
856         mutable Mutex m_mutex;
857         IOHandler *m_top;
858         
859     private:
860         
861         DISALLOW_COPY_AND_ASSIGN (IOHandlerStack);
862     };
863
864 } // namespace lldb_private
865
866 #endif // #ifndef liblldb_IOHandler_h_