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