ScriptInterpreterPython.h revision ec1e823a9eb7ce4205a254210f633aa894fd612a
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 (const char *class_name,
80                                     lldb::ValueObjectSP valobj);
81
82    virtual lldb::ScriptInterpreterObjectSP
83    CreateOSPlugin (const char *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    CheckObjectExists (const char* name)
163    {
164        if (!name || !name[0])
165            return false;
166        std::string temp;
167        return GetDocumentationForItem (name,temp);
168    }
169
170    virtual bool
171    LoadScriptingModule (const char* filename,
172                         bool can_reload,
173                         bool init_session,
174                         lldb_private::Error& error);
175
176    virtual lldb::ScriptInterpreterObjectSP
177    MakeScriptObject (void* object);
178
179    void
180    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
181                                             CommandReturnObject &result);
182
183    void
184    CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
185                                             CommandReturnObject &result);
186
187    /// Set a Python one-liner as the callback for the breakpoint.
188    void
189    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
190                                  const char *oneliner);
191
192    /// Set a one-liner as the callback for the watchpoint.
193    void
194    SetWatchpointCommandCallback (WatchpointOptions *wp_options,
195                                  const char *oneliner);
196
197    StringList
198    ReadCommandInputFromUser (FILE *in_file);
199
200    virtual void
201    ResetOutputFileHandle (FILE *new_fh);
202
203    static lldb::thread_result_t
204    RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton);
205
206    static void
207    InitializePrivate ();
208
209    static void
210    InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
211
212protected:
213
214    void
215    EnterSession (bool init_lldb_globals);
216
217    void
218    LeaveSession ();
219
220    void
221    SaveTerminalState (int fd);
222
223    void
224    RestoreTerminalState ();
225
226private:
227
228    class SynchronicityHandler
229    {
230    private:
231        lldb::DebuggerSP             m_debugger_sp;
232        ScriptedCommandSynchronicity m_synch_wanted;
233        bool                         m_old_asynch;
234    public:
235        SynchronicityHandler(lldb::DebuggerSP,
236                             ScriptedCommandSynchronicity);
237        ~SynchronicityHandler();
238    };
239
240    class ScriptInterpreterPythonObject : public ScriptInterpreterObject
241    {
242    public:
243        ScriptInterpreterPythonObject() :
244        ScriptInterpreterObject()
245        {}
246
247        ScriptInterpreterPythonObject(void* obj) :
248        ScriptInterpreterObject(obj)
249        {
250            Py_XINCREF(m_object);
251        }
252
253        operator bool ()
254        {
255            return m_object && m_object != Py_None;
256        }
257
258
259        virtual
260        ~ScriptInterpreterPythonObject()
261        {
262            Py_XDECREF(m_object);
263            m_object = NULL;
264        }
265        private:
266            DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
267    };
268
269	class Locker
270	{
271	public:
272
273        enum OnEntry
274        {
275            AcquireLock         = 0x0001,
276            InitSession         = 0x0002,
277            InitGlobals         = 0x0004
278        };
279
280        enum OnLeave
281        {
282            FreeLock            = 0x0001,
283            FreeAcquiredLock    = 0x0002,    // do not free the lock if we already held it when calling constructor
284            TearDownSession     = 0x0004
285        };
286
287        Locker (ScriptInterpreterPython *py_interpreter = NULL,
288                uint16_t on_entry = AcquireLock | InitSession,
289                uint16_t on_leave = FreeLock | TearDownSession,
290                FILE* wait_msg_handle = NULL);
291
292    	~Locker ();
293
294	private:
295
296        bool
297        DoAcquireLock ();
298
299        bool
300        DoInitSession (bool init_lldb_globals);
301
302        bool
303        DoFreeLock ();
304
305        bool
306        DoTearDownSession ();
307
308        static void
309        ReleasePythonLock ();
310
311    	bool                     m_need_session;
312    	ScriptInterpreterPython *m_python_interpreter;
313    	FILE*                    m_tmp_fh;
314        PyGILState_STATE         m_GILState;
315	};
316
317    class PythonInputReaderManager
318    {
319    public:
320        PythonInputReaderManager (ScriptInterpreterPython *interpreter);
321
322        operator bool()
323        {
324            return m_error;
325        }
326
327        ~PythonInputReaderManager();
328
329    private:
330
331        static size_t
332        InputReaderCallback (void *baton,
333                                           InputReader &reader,
334                                           lldb::InputReaderAction notification,
335                                           const char *bytes,
336                                           size_t bytes_len);
337
338        static lldb::thread_result_t
339        RunPythonInputReader (lldb::thread_arg_t baton);
340
341        ScriptInterpreterPython *m_interpreter;
342        lldb::DebuggerSP m_debugger_sp;
343        lldb::InputReaderSP m_reader_sp;
344        bool m_error;
345    };
346
347    static size_t
348    InputReaderCallback (void *baton,
349                         InputReader &reader,
350                         lldb::InputReaderAction notification,
351                         const char *bytes,
352                         size_t bytes_len);
353
354
355    lldb_utility::PseudoTerminal m_embedded_thread_pty;
356    lldb_utility::PseudoTerminal m_embedded_python_pty;
357    lldb::InputReaderSP m_embedded_thread_input_reader_sp;
358    lldb::InputReaderSP m_embedded_python_input_reader_sp;
359    FILE *m_dbg_stdout;
360    PyObject *m_new_sysout;
361    PyObject *m_old_sysout;
362    PyObject *m_old_syserr;
363    PyObject *m_run_one_line;
364    std::string m_dictionary_name;
365    TerminalState m_terminal_state;
366    bool m_session_is_active;
367    bool m_pty_slave_is_open;
368    bool m_valid_session;
369    PyThreadState *m_command_thread_state;
370};
371} // namespace lldb_private
372
373#endif // #ifdef LLDB_DISABLE_PYTHON
374
375#endif // #ifndef liblldb_ScriptInterpreterPython_h_
376