]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/llvm/tools/lldb/include/lldb/Interpreter/ScriptInterpreterPython.h
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / llvm / tools / lldb / include / lldb / Interpreter / ScriptInterpreterPython.h
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
31 namespace lldb_private {
32     
33 class ScriptInterpreterPython : public ScriptInterpreter
34 {
35 public:
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     OSPlugin_CreatePluginObject (const char *class_name,
84                                  lldb::ProcessSP process_sp);
85     
86     virtual lldb::ScriptInterpreterObjectSP
87     OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
88     
89     virtual lldb::ScriptInterpreterObjectSP
90     OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
91     
92     virtual lldb::ScriptInterpreterObjectSP
93     OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
94                                   lldb::tid_t thread_id);
95     
96     virtual lldb::ScriptInterpreterObjectSP
97     OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
98                            lldb::tid_t tid,
99                            lldb::addr_t context);
100     
101     virtual size_t
102     CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
103     
104     virtual lldb::ValueObjectSP
105     GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
106     
107     virtual int
108     GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
109     
110     virtual bool
111     UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
112     
113     virtual bool
114     MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
115     
116     virtual bool
117     RunScriptBasedCommand(const char* impl_function,
118                           const char* args,
119                           ScriptedCommandSynchronicity synchronicity,
120                           lldb_private::CommandReturnObject& cmd_retobj,
121                           Error& error);
122     
123     bool
124     GenerateFunction(const char *signature, const StringList &input);
125     
126     bool
127     GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
128
129     bool
130     GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
131
132     static size_t
133     GenerateBreakpointOptionsCommandCallback (void *baton, 
134                                               InputReader &reader, 
135                                               lldb::InputReaderAction notification,
136                                               const char *bytes, 
137                                               size_t bytes_len);
138         
139     static size_t
140     GenerateWatchpointOptionsCommandCallback (void *baton, 
141                                               InputReader &reader, 
142                                               lldb::InputReaderAction notification,
143                                               const char *bytes, 
144                                               size_t bytes_len);
145         
146     static bool
147     BreakpointCallbackFunction (void *baton, 
148                                 StoppointCallbackContext *context, 
149                                 lldb::user_id_t break_id,
150                                 lldb::user_id_t break_loc_id);
151     
152     static bool
153     WatchpointCallbackFunction (void *baton, 
154                                 StoppointCallbackContext *context, 
155                                 lldb::user_id_t watch_id);
156     
157     virtual bool
158     GetScriptedSummary (const char *function_name,
159                         lldb::ValueObjectSP valobj,
160                         lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
161                         std::string& retval);
162     
163     virtual bool
164     GetDocumentationForItem (const char* item, std::string& dest);
165     
166     virtual bool
167     CheckObjectExists (const char* name)
168     {
169         if (!name || !name[0])
170             return false;
171         std::string temp;
172         return GetDocumentationForItem (name,temp);
173     }
174     
175     virtual bool
176     RunScriptFormatKeyword (const char* impl_function,
177                             Process* process,
178                             std::string& output,
179                             Error& error);
180
181     virtual bool
182     RunScriptFormatKeyword (const char* impl_function,
183                             Thread* thread,
184                             std::string& output,
185                             Error& error);
186     
187     virtual bool
188     RunScriptFormatKeyword (const char* impl_function,
189                             Target* target,
190                             std::string& output,
191                             Error& error);
192     
193     virtual bool
194     RunScriptFormatKeyword (const char* impl_function,
195                             StackFrame* frame,
196                             std::string& output,
197                             Error& error);
198     
199     virtual bool
200     LoadScriptingModule (const char* filename,
201                          bool can_reload,
202                          bool init_session,
203                          lldb_private::Error& error);
204     
205     virtual lldb::ScriptInterpreterObjectSP
206     MakeScriptObject (void* object);
207     
208     virtual std::unique_ptr<ScriptInterpreterLocker>
209     AcquireInterpreterLock ();
210     
211     void
212     CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
213                                              CommandReturnObject &result);
214
215     void 
216     CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
217                                              CommandReturnObject &result);
218
219     /// Set a Python one-liner as the callback for the breakpoint.
220     void 
221     SetBreakpointCommandCallback (BreakpointOptions *bp_options,
222                                   const char *oneliner);
223
224     /// Set a one-liner as the callback for the watchpoint.
225     void 
226     SetWatchpointCommandCallback (WatchpointOptions *wp_options,
227                                   const char *oneliner);
228
229     StringList
230     ReadCommandInputFromUser (FILE *in_file);
231     
232     virtual void
233     ResetOutputFileHandle (FILE *new_fh);
234     
235     static lldb::thread_result_t
236     RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton);
237
238     static void
239     InitializePrivate ();
240
241     static void
242     InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
243
244 protected:
245
246     bool
247     EnterSession (bool init_lldb_globals);
248     
249     void
250     LeaveSession ();
251     
252     void
253     SaveTerminalState (int fd);
254
255     void
256     RestoreTerminalState ();
257     
258 private:
259     
260     class SynchronicityHandler
261     {
262     private:
263         lldb::DebuggerSP             m_debugger_sp;
264         ScriptedCommandSynchronicity m_synch_wanted;
265         bool                         m_old_asynch;
266     public:
267         SynchronicityHandler(lldb::DebuggerSP,
268                              ScriptedCommandSynchronicity);
269         ~SynchronicityHandler();
270     };
271     
272     class ScriptInterpreterPythonObject : public ScriptInterpreterObject
273     {
274     public:
275         ScriptInterpreterPythonObject() :
276         ScriptInterpreterObject()
277         {}
278         
279         ScriptInterpreterPythonObject(void* obj) :
280         ScriptInterpreterObject(obj)
281         {
282             Py_XINCREF(m_object);
283         }
284         
285         operator bool ()
286         {
287             return m_object && m_object != Py_None;
288         }
289         
290         
291         virtual
292         ~ScriptInterpreterPythonObject()
293         {
294             Py_XDECREF(m_object);
295             m_object = NULL;
296         }
297         private:
298             DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
299     };
300     
301         class Locker : public ScriptInterpreterLocker
302         {
303         public:
304         
305         enum OnEntry
306         {
307             AcquireLock         = 0x0001,
308             InitSession         = 0x0002,
309             InitGlobals         = 0x0004
310         };
311         
312         enum OnLeave
313         {
314             FreeLock            = 0x0001,
315             FreeAcquiredLock    = 0x0002,    // do not free the lock if we already held it when calling constructor
316             TearDownSession     = 0x0004
317         };
318         
319         Locker (ScriptInterpreterPython *py_interpreter = NULL,
320                 uint16_t on_entry = AcquireLock | InitSession,
321                 uint16_t on_leave = FreeLock | TearDownSession,
322                 FILE* wait_msg_handle = NULL);
323         
324         ~Locker ();
325
326         private:
327         
328         bool
329         DoAcquireLock ();
330         
331         bool
332         DoInitSession (bool init_lldb_globals);
333         
334         bool
335         DoFreeLock ();
336         
337         bool
338         DoTearDownSession ();
339
340         static void
341         ReleasePythonLock ();
342         
343         bool                     m_teardown_session;
344         ScriptInterpreterPython *m_python_interpreter;
345         FILE*                    m_tmp_fh;
346         PyGILState_STATE         m_GILState;
347         };
348     
349     class PythonInputReaderManager
350     {
351     public:
352         PythonInputReaderManager (ScriptInterpreterPython *interpreter);
353         
354         operator bool()
355         {
356             return m_error;
357         }
358         
359         ~PythonInputReaderManager();
360         
361     private:
362         
363         static size_t
364         InputReaderCallback (void *baton,
365                                            InputReader &reader,
366                                            lldb::InputReaderAction notification,
367                                            const char *bytes,
368                                            size_t bytes_len);
369         
370         static lldb::thread_result_t
371         RunPythonInputReader (lldb::thread_arg_t baton);
372         
373         ScriptInterpreterPython *m_interpreter;
374         lldb::DebuggerSP m_debugger_sp;
375         lldb::InputReaderSP m_reader_sp;
376         bool m_error;
377     };
378
379     static size_t
380     InputReaderCallback (void *baton, 
381                          InputReader &reader, 
382                          lldb::InputReaderAction notification,
383                          const char *bytes, 
384                          size_t bytes_len);
385
386
387     lldb_utility::PseudoTerminal m_embedded_thread_pty;
388     lldb_utility::PseudoTerminal m_embedded_python_pty;
389     lldb::InputReaderSP m_embedded_thread_input_reader_sp;
390     lldb::InputReaderSP m_embedded_python_input_reader_sp;
391     FILE *m_dbg_stdout;
392     PyObject *m_new_sysout;
393     PyObject *m_old_sysout;
394     PyObject *m_old_syserr;
395     PyObject *m_run_one_line;
396     std::string m_dictionary_name;
397     TerminalState m_terminal_state;
398     bool m_session_is_active;
399     bool m_pty_slave_is_open;
400     bool m_valid_session;
401     PyThreadState *m_command_thread_state;
402 };
403 } // namespace lldb_private
404
405 #endif // #ifdef LLDB_DISABLE_PYTHON
406
407 #endif // #ifndef liblldb_ScriptInterpreterPython_h_