ScriptInterpreterPython.h revision fa1f617779902cabfcce11abf07a857b85880f41
1//===-- ScriptInterpreterPython.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
11#ifndef liblldb_ScriptInterpreterPython_h_
12#define liblldb_ScriptInterpreterPython_h_
13
14#include "lldb/lldb-private.h"
15#include "lldb/Interpreter/ScriptInterpreter.h"
16#include "lldb/Core/InputReader.h"
17#include "lldb/Host/Terminal.h"
18
19namespace lldb_private {
20
21class ScriptInterpreterPython : public ScriptInterpreter
22{
23public:
24
25    ScriptInterpreterPython (CommandInterpreter &interpreter);
26
27    ~ScriptInterpreterPython ();
28
29    bool
30    ExecuteOneLine (const char *command, CommandReturnObject *result);
31
32    void
33    ExecuteInterpreterLoop ();
34
35    bool
36    ExecuteOneLineWithReturn (const char *in_string,
37                              ScriptInterpreter::ScriptReturnType return_type,
38                              void *ret_value);
39
40    bool
41    ExecuteMultipleLines (const char *in_string);
42
43    bool
44    ExportFunctionDefinitionToInterpreter (StringList &function_def);
45
46    bool
47    GenerateTypeScriptFunction (StringList &input, StringList &output);
48
49    bool
50    GenerateTypeSynthClass (StringList &input, StringList &output);
51
52    // use this if the function code is just a one-liner script
53    bool
54    GenerateTypeScriptFunction (const char* oneliner, StringList &output);
55
56    virtual bool
57    GenerateScriptAliasFunction (StringList &input, StringList &output);
58
59    void*
60    CreateSyntheticScriptedProvider (std::string class_name,
61                                     lldb::ValueObjectSP valobj);
62
63    virtual uint32_t
64    CalculateNumChildren (void *implementor);
65
66    virtual lldb::ValueObjectSP
67    GetChildAtIndex (void *implementor, uint32_t idx);
68
69    virtual int
70    GetIndexOfChildWithName (void *implementor, const char* child_name);
71
72    virtual void
73    UpdateSynthProviderInstance (void* implementor);
74
75    virtual bool
76    RunScriptBasedCommand(const char* impl_function,
77                          const char* args,
78                          lldb_private::CommandReturnObject& cmd_retobj,
79                          Error& error);
80
81    bool
82    GenerateFunction(std::string& signature, StringList &input, StringList &output);
83
84    bool
85    GenerateBreakpointCommandCallbackData (StringList &input, StringList &output);
86
87    static size_t
88    GenerateBreakpointOptionsCommandCallback (void *baton,
89                                              InputReader &reader,
90                                              lldb::InputReaderAction notification,
91                                              const char *bytes,
92                                              size_t bytes_len);
93
94    static bool
95    BreakpointCallbackFunction (void *baton,
96                                StoppointCallbackContext *context,
97                                lldb::user_id_t break_id,
98                                lldb::user_id_t break_loc_id);
99
100    static std::string
101    CallPythonScriptFunction (const char *python_function_name,
102                              lldb::ValueObjectSP valobj);
103
104    virtual std::string
105    GetDocumentationForItem (const char* item);
106
107    virtual bool
108    LoadScriptingModule (const char* filename,
109                         lldb_private::Error& error);
110
111    void
112    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
113                                             CommandReturnObject &result);
114
115    /// Set a Python one-liner as the callback for the breakpoint.
116    void
117    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
118                                  const char *oneliner);
119
120    StringList
121    ReadCommandInputFromUser (FILE *in_file);
122
123    virtual void
124    ResetOutputFileHandle (FILE *new_fh);
125
126    static lldb::thread_result_t
127    RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton);
128
129    static void
130    InitializePrivate ();
131
132    static void
133    InitializeInterpreter (SWIGInitCallback python_swig_init_callback,
134                           SWIGBreakpointCallbackFunction python_swig_breakpoint_callback,
135                           SWIGPythonTypeScriptCallbackFunction python_swig_typescript_callback,
136                           SWIGPythonCreateSyntheticProvider python_swig_synthetic_script,
137                           SWIGPythonCalculateNumChildren python_swig_calc_children,
138                           SWIGPythonGetChildAtIndex python_swig_get_child_index,
139                           SWIGPythonGetIndexOfChildWithName python_swig_get_index_child,
140                           SWIGPythonCastPyObjectToSBValue python_swig_cast_to_sbvalue,
141                           SWIGPythonUpdateSynthProviderInstance python_swig_update_provider,
142                           SWIGPythonCallCommand python_swig_call_command,
143                           SWIGPythonCallModuleInit python_swig_call_mod_init);
144
145protected:
146
147    void
148    EnterSession ();
149
150    void
151    LeaveSession ();
152
153    void
154    SaveTerminalState (int fd);
155
156    void
157    RestoreTerminalState ();
158
159private:
160
161	class Locker
162	{
163	public:
164
165        enum OnEntry
166        {
167            AcquireLock         = 0x0001,
168            InitSession         = 0x0002
169        };
170
171        enum OnLeave
172        {
173            FreeLock            = 0x0001,
174            FreeAcquiredLock    = 0x0002,    // do not free the lock if we already held it when calling constructor
175            TearDownSession     = 0x0004
176        };
177
178        Locker (ScriptInterpreterPython *py_interpreter = NULL,
179                uint16_t on_entry = AcquireLock | InitSession,
180                uint16_t on_leave = FreeLock | TearDownSession,
181                FILE* wait_msg_handle = NULL);
182
183    	~Locker ();
184
185        static bool
186        CurrentThreadHasPythonLock ();
187
188	private:
189
190        bool
191        DoAcquireLock ();
192
193        bool
194        DoInitSession ();
195
196        bool
197        DoFreeLock ();
198
199        bool
200        DoTearDownSession ();
201
202        static bool
203        TryGetPythonLock (uint32_t seconds_to_wait);
204
205        static void
206        ReleasePythonLock ();
207
208    	bool                     m_need_session;
209    	bool                     m_release_lock;
210    	ScriptInterpreterPython *m_python_interpreter;
211    	FILE*                    m_tmp_fh;
212	};
213
214    static size_t
215    InputReaderCallback (void *baton,
216                         InputReader &reader,
217                         lldb::InputReaderAction notification,
218                         const char *bytes,
219                         size_t bytes_len);
220
221
222    lldb_utility::PseudoTerminal m_embedded_python_pty;
223    lldb::InputReaderSP m_embedded_thread_input_reader_sp;
224    FILE *m_dbg_stdout;
225    void *m_new_sysout; // This is a PyObject.
226    std::string m_dictionary_name;
227    TerminalState m_terminal_state;
228    bool m_session_is_active;
229    bool m_pty_slave_is_open;
230    bool m_valid_session;
231
232};
233} // namespace lldb_private
234
235
236#endif // #ifndef liblldb_ScriptInterpreterPython_h_
237