ScriptInterpreterPython.h revision 14e71ecd9b05d3dff6e7dc3bf3148b4ad5e3989a
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#ifdef LLDB_DISABLE_PYTHON
15
16// Python is disabled in this build
17
18#else
19
20#if defined (__APPLE__)
21#include <Python/Python.h>
22#else
23#include <Python.h>
24#endif
25
26#include "lldb/lldb-private.h"
27#include "lldb/Interpreter/ScriptInterpreter.h"
28#include "lldb/Core/InputReader.h"
29#include "lldb/Host/Terminal.h"
30
31namespace lldb_private {
32
33class ScriptInterpreterPython : public ScriptInterpreter
34{
35public:
36
37    ScriptInterpreterPython (CommandInterpreter &interpreter);
38
39    ~ScriptInterpreterPython ();
40
41    bool
42    ExecuteOneLine (const char *command,
43                    CommandReturnObject *result,
44                    const ExecuteScriptOptions &options = ExecuteScriptOptions());
45
46    void
47    ExecuteInterpreterLoop ();
48
49    bool
50    ExecuteOneLineWithReturn (const char *in_string,
51                              ScriptInterpreter::ScriptReturnType return_type,
52                              void *ret_value,
53                              const ExecuteScriptOptions &options = ExecuteScriptOptions());
54
55    bool
56    ExecuteMultipleLines (const char *in_string,
57                          const ExecuteScriptOptions &options = ExecuteScriptOptions());
58
59    bool
60    ExportFunctionDefinitionToInterpreter (StringList &function_def);
61
62    bool
63    GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL);
64
65    bool
66    GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL);
67
68    bool
69    GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL);
70
71    // use this if the function code is just a one-liner script
72    bool
73    GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL);
74
75    virtual bool
76    GenerateScriptAliasFunction (StringList &input, std::string& output);
77
78    lldb::ScriptInterpreterObjectSP
79    CreateSyntheticScriptedProvider (std::string class_name,
80                                     lldb::ValueObjectSP valobj);
81
82    virtual lldb::ScriptInterpreterObjectSP
83    CreateOSPlugin (std::string class_name,
84                    lldb::ProcessSP process_sp);
85
86    virtual lldb::ScriptInterpreterObjectSP
87    OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object);
88
89    virtual lldb::ScriptInterpreterObjectSP
90    OSPlugin_QueryForThreadsInfo (lldb::ScriptInterpreterObjectSP object);
91
92    virtual lldb::ScriptInterpreterObjectSP
93    OSPlugin_QueryForRegisterContextData (lldb::ScriptInterpreterObjectSP object,
94                                          lldb::tid_t thread_id);
95
96    virtual uint32_t
97    CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
98
99    virtual lldb::ValueObjectSP
100    GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
101
102    virtual int
103    GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
104
105    virtual bool
106    UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
107
108    virtual bool
109    MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
110
111    virtual bool
112    RunScriptBasedCommand(const char* impl_function,
113                          const char* args,
114                          ScriptedCommandSynchronicity synchronicity,
115                          lldb_private::CommandReturnObject& cmd_retobj,
116                          Error& error);
117
118    bool
119    GenerateFunction(const char *signature, const StringList &input);
120
121    bool
122    GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
123
124    bool
125    GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
126
127    static size_t
128    GenerateBreakpointOptionsCommandCallback (void *baton,
129                                              InputReader &reader,
130                                              lldb::InputReaderAction notification,
131                                              const char *bytes,
132                                              size_t bytes_len);
133
134    static size_t
135    GenerateWatchpointOptionsCommandCallback (void *baton,
136                                              InputReader &reader,
137                                              lldb::InputReaderAction notification,
138                                              const char *bytes,
139                                              size_t bytes_len);
140
141    static bool
142    BreakpointCallbackFunction (void *baton,
143                                StoppointCallbackContext *context,
144                                lldb::user_id_t break_id,
145                                lldb::user_id_t break_loc_id);
146
147    static bool
148    WatchpointCallbackFunction (void *baton,
149                                StoppointCallbackContext *context,
150                                lldb::user_id_t watch_id);
151
152    virtual bool
153    GetScriptedSummary (const char *function_name,
154                        lldb::ValueObjectSP valobj,
155                        lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
156                        std::string& retval);
157
158    virtual bool
159    GetDocumentationForItem (const char* item, std::string& dest);
160
161    virtual bool
162    LoadScriptingModule (const char* filename,
163                         bool can_reload,
164                         bool init_session,
165                         lldb_private::Error& error);
166
167    virtual lldb::ScriptInterpreterObjectSP
168    MakeScriptObject (void* object);
169
170    void
171    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
172                                             CommandReturnObject &result);
173
174    void
175    CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
176                                             CommandReturnObject &result);
177
178    /// Set a Python one-liner as the callback for the breakpoint.
179    void
180    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
181                                  const char *oneliner);
182
183    /// Set a one-liner as the callback for the watchpoint.
184    void
185    SetWatchpointCommandCallback (WatchpointOptions *wp_options,
186                                  const char *oneliner);
187
188    StringList
189    ReadCommandInputFromUser (FILE *in_file);
190
191    virtual void
192    ResetOutputFileHandle (FILE *new_fh);
193
194    static lldb::thread_result_t
195    RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton);
196
197    static void
198    InitializePrivate ();
199
200    static void
201    InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
202
203protected:
204
205    void
206    EnterSession (bool init_lldb_globals);
207
208    void
209    LeaveSession ();
210
211    void
212    SaveTerminalState (int fd);
213
214    void
215    RestoreTerminalState ();
216
217private:
218
219    class SynchronicityHandler
220    {
221    private:
222        lldb::DebuggerSP             m_debugger_sp;
223        ScriptedCommandSynchronicity m_synch_wanted;
224        bool                         m_old_asynch;
225    public:
226        SynchronicityHandler(lldb::DebuggerSP,
227                             ScriptedCommandSynchronicity);
228        ~SynchronicityHandler();
229    };
230
231    class ScriptInterpreterPythonObject : public ScriptInterpreterObject
232    {
233    public:
234        ScriptInterpreterPythonObject() :
235        ScriptInterpreterObject()
236        {}
237
238        ScriptInterpreterPythonObject(void* obj) :
239        ScriptInterpreterObject(obj)
240        {
241            Py_XINCREF(m_object);
242        }
243
244        operator bool ()
245        {
246            return m_object && m_object != Py_None;
247        }
248
249
250        virtual
251        ~ScriptInterpreterPythonObject()
252        {
253            Py_XDECREF(m_object);
254            m_object = NULL;
255        }
256        private:
257            DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
258    };
259
260	class Locker
261	{
262	public:
263
264        enum OnEntry
265        {
266            AcquireLock         = 0x0001,
267            InitSession         = 0x0002,
268            InitGlobals         = 0x0004
269        };
270
271        enum OnLeave
272        {
273            FreeLock            = 0x0001,
274            FreeAcquiredLock    = 0x0002,    // do not free the lock if we already held it when calling constructor
275            TearDownSession     = 0x0004
276        };
277
278        Locker (ScriptInterpreterPython *py_interpreter = NULL,
279                uint16_t on_entry = AcquireLock | InitSession,
280                uint16_t on_leave = FreeLock | TearDownSession,
281                FILE* wait_msg_handle = NULL);
282
283    	~Locker ();
284
285	private:
286
287        bool
288        DoAcquireLock ();
289
290        bool
291        DoInitSession (bool init_lldb_globals);
292
293        bool
294        DoFreeLock ();
295
296        bool
297        DoTearDownSession ();
298
299        static void
300        ReleasePythonLock ();
301
302    	bool                     m_need_session;
303    	ScriptInterpreterPython *m_python_interpreter;
304    	FILE*                    m_tmp_fh;
305        PyGILState_STATE         m_GILState;
306	};
307
308    class PythonInputReaderManager
309    {
310    public:
311        PythonInputReaderManager (ScriptInterpreterPython *interpreter);
312
313        operator bool()
314        {
315            return m_error;
316        }
317
318        ~PythonInputReaderManager();
319
320    private:
321
322        static size_t
323        InputReaderCallback (void *baton,
324                                           InputReader &reader,
325                                           lldb::InputReaderAction notification,
326                                           const char *bytes,
327                                           size_t bytes_len);
328
329        static lldb::thread_result_t
330        RunPythonInputReader (lldb::thread_arg_t baton);
331
332        ScriptInterpreterPython *m_interpreter;
333        lldb::DebuggerSP m_debugger_sp;
334        lldb::InputReaderSP m_reader_sp;
335        bool m_error;
336    };
337
338    static size_t
339    InputReaderCallback (void *baton,
340                         InputReader &reader,
341                         lldb::InputReaderAction notification,
342                         const char *bytes,
343                         size_t bytes_len);
344
345
346    lldb_utility::PseudoTerminal m_embedded_thread_pty;
347    lldb_utility::PseudoTerminal m_embedded_python_pty;
348    lldb::InputReaderSP m_embedded_thread_input_reader_sp;
349    lldb::InputReaderSP m_embedded_python_input_reader_sp;
350    FILE *m_dbg_stdout;
351    PyObject *m_new_sysout;
352    PyObject *m_old_sysout;
353    PyObject *m_old_syserr;
354    PyObject *m_run_one_line;
355    std::string m_dictionary_name;
356    TerminalState m_terminal_state;
357    bool m_session_is_active;
358    bool m_pty_slave_is_open;
359    bool m_valid_session;
360};
361} // namespace lldb_private
362
363#endif // #ifdef LLDB_DISABLE_PYTHON
364
365#endif // #ifndef liblldb_ScriptInterpreterPython_h_
366