]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Interpreter/ScriptInterpreterPython.h
Merge latest (commit c8c1b3a77934768c7f7a4a9c10140c8bec529059) files
[FreeBSD/FreeBSD.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 #include "lldb/lldb-python.h"
21 #include "lldb/lldb-private.h"
22 #include "lldb/Core/IOHandler.h"
23 #include "lldb/Interpreter/ScriptInterpreter.h"
24 #include "lldb/Interpreter/PythonDataObjects.h"
25 #include "lldb/Host/Terminal.h"
26
27 class IOHandlerPythonInterpreter;
28
29 namespace lldb_private {
30     
31 class ScriptInterpreterPython :
32     public ScriptInterpreter,
33     public IOHandlerDelegateMultiline
34 {
35 public:
36
37     friend class IOHandlerPythonInterpreter;
38
39     ScriptInterpreterPython (CommandInterpreter &interpreter);
40
41     ~ScriptInterpreterPython ();
42
43     bool
44     ExecuteOneLine (const char *command,
45                     CommandReturnObject *result,
46                     const ExecuteScriptOptions &options = ExecuteScriptOptions());
47
48     void
49     ExecuteInterpreterLoop ();
50
51     bool
52     ExecuteOneLineWithReturn (const char *in_string, 
53                               ScriptInterpreter::ScriptReturnType return_type,
54                               void *ret_value,
55                               const ExecuteScriptOptions &options = ExecuteScriptOptions());
56
57     lldb_private::Error
58     ExecuteMultipleLines (const char *in_string,
59                           const ExecuteScriptOptions &options = ExecuteScriptOptions());
60
61     Error
62     ExportFunctionDefinitionToInterpreter (StringList &function_def);
63
64     bool
65     GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL);
66     
67     bool
68     GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL);
69     
70     bool
71     GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL);
72     
73     // use this if the function code is just a one-liner script
74     bool
75     GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL);
76     
77     virtual bool
78     GenerateScriptAliasFunction (StringList &input, std::string& output);
79     
80     lldb::ScriptInterpreterObjectSP
81     CreateSyntheticScriptedProvider (const char *class_name,
82                                      lldb::ValueObjectSP valobj);
83     
84     virtual lldb::ScriptInterpreterObjectSP
85     OSPlugin_CreatePluginObject (const char *class_name,
86                                  lldb::ProcessSP process_sp);
87     
88     virtual lldb::ScriptInterpreterObjectSP
89     OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
90     
91     virtual lldb::ScriptInterpreterObjectSP
92     OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
93     
94     virtual lldb::ScriptInterpreterObjectSP
95     OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
96                                   lldb::tid_t thread_id);
97     
98     virtual lldb::ScriptInterpreterObjectSP
99     OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
100                            lldb::tid_t tid,
101                            lldb::addr_t context);
102     
103     virtual lldb::ScriptInterpreterObjectSP
104     LoadPluginModule (const FileSpec& file_spec,
105                       lldb_private::Error& error);
106     
107     virtual lldb::ScriptInterpreterObjectSP
108     GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp,
109                         Target* target,
110                         const char* setting_name,
111                         lldb_private::Error& error);
112     
113     virtual size_t
114     CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
115     
116     virtual lldb::ValueObjectSP
117     GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
118     
119     virtual int
120     GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
121     
122     virtual bool
123     UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
124     
125     virtual bool
126     MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
127     
128     virtual bool
129     RunScriptBasedCommand(const char* impl_function,
130                           const char* args,
131                           ScriptedCommandSynchronicity synchronicity,
132                           lldb_private::CommandReturnObject& cmd_retobj,
133                           Error& error);
134     
135     Error
136     GenerateFunction(const char *signature, const StringList &input);
137     
138     Error
139     GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
140
141     bool
142     GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
143
144 //    static size_t
145 //    GenerateBreakpointOptionsCommandCallback (void *baton, 
146 //                                              InputReader &reader, 
147 //                                              lldb::InputReaderAction notification,
148 //                                              const char *bytes, 
149 //                                              size_t bytes_len);
150 //    
151 //    static size_t
152 //    GenerateWatchpointOptionsCommandCallback (void *baton, 
153 //                                              InputReader &reader, 
154 //                                              lldb::InputReaderAction notification,
155 //                                              const char *bytes, 
156 //                                              size_t bytes_len);
157     
158     static bool
159     BreakpointCallbackFunction (void *baton, 
160                                 StoppointCallbackContext *context, 
161                                 lldb::user_id_t break_id,
162                                 lldb::user_id_t break_loc_id);
163     
164     static bool
165     WatchpointCallbackFunction (void *baton, 
166                                 StoppointCallbackContext *context, 
167                                 lldb::user_id_t watch_id);
168     
169     virtual bool
170     GetScriptedSummary (const char *function_name,
171                         lldb::ValueObjectSP valobj,
172                         lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
173                         std::string& retval);
174     
175     virtual void
176     Clear ();
177
178     virtual bool
179     GetDocumentationForItem (const char* item, std::string& dest);
180     
181     virtual bool
182     CheckObjectExists (const char* name)
183     {
184         if (!name || !name[0])
185             return false;
186         std::string temp;
187         return GetDocumentationForItem (name,temp);
188     }
189     
190     virtual bool
191     RunScriptFormatKeyword (const char* impl_function,
192                             Process* process,
193                             std::string& output,
194                             Error& error);
195
196     virtual bool
197     RunScriptFormatKeyword (const char* impl_function,
198                             Thread* thread,
199                             std::string& output,
200                             Error& error);
201     
202     virtual bool
203     RunScriptFormatKeyword (const char* impl_function,
204                             Target* target,
205                             std::string& output,
206                             Error& error);
207     
208     virtual bool
209     RunScriptFormatKeyword (const char* impl_function,
210                             StackFrame* frame,
211                             std::string& output,
212                             Error& error);
213     
214     virtual bool
215     LoadScriptingModule (const char* filename,
216                          bool can_reload,
217                          bool init_session,
218                          lldb_private::Error& error,
219                          lldb::ScriptInterpreterObjectSP* module_sp = nullptr);
220     
221     virtual lldb::ScriptInterpreterObjectSP
222     MakeScriptObject (void* object);
223     
224     virtual std::unique_ptr<ScriptInterpreterLocker>
225     AcquireInterpreterLock ();
226     
227     void
228     CollectDataForBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec,
229                                              CommandReturnObject &result);
230
231     void 
232     CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
233                                              CommandReturnObject &result);
234
235     /// Set the callback body text into the callback for the breakpoint.
236     Error
237     SetBreakpointCommandCallback (BreakpointOptions *bp_options,
238                                   const char *callback_body);
239
240     void 
241     SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options,
242                                           const char *function_name);
243
244     /// Set a one-liner as the callback for the watchpoint.
245     void 
246     SetWatchpointCommandCallback (WatchpointOptions *wp_options,
247                                   const char *oneliner);
248
249     StringList
250     ReadCommandInputFromUser (FILE *in_file);
251     
252     virtual void
253     ResetOutputFileHandle (FILE *new_fh);
254     
255     static void
256     InitializePrivate ();
257
258     static void
259     InitializeInterpreter (SWIGInitCallback python_swig_init_callback,
260                            SWIGBreakpointCallbackFunction swig_breakpoint_callback,
261                            SWIGWatchpointCallbackFunction swig_watchpoint_callback,
262                            SWIGPythonTypeScriptCallbackFunction swig_typescript_callback,
263                            SWIGPythonCreateSyntheticProvider swig_synthetic_script,
264                            SWIGPythonCalculateNumChildren swig_calc_children,
265                            SWIGPythonGetChildAtIndex swig_get_child_index,
266                            SWIGPythonGetIndexOfChildWithName swig_get_index_child,
267                            SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue ,
268                            SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue,
269                            SWIGPythonUpdateSynthProviderInstance swig_update_provider,
270                            SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider,
271                            SWIGPythonCallCommand swig_call_command,
272                            SWIGPythonCallModuleInit swig_call_module_init,
273                            SWIGPythonCreateOSPlugin swig_create_os_plugin,
274                            SWIGPythonScriptKeyword_Process swig_run_script_keyword_process,
275                            SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread,
276                            SWIGPythonScriptKeyword_Target swig_run_script_keyword_target,
277                            SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame,
278                            SWIGPython_GetDynamicSetting swig_plugin_get);
279
280     const char *
281     GetDictionaryName ()
282     {
283         return m_dictionary_name.c_str();
284     }
285
286     
287     PyThreadState *
288     GetThreadState()
289     {
290         return m_command_thread_state;
291     }
292
293     void
294     SetThreadState (PyThreadState *s)
295     {
296         if (s)
297             m_command_thread_state = s;
298     }
299
300     //----------------------------------------------------------------------
301     // IOHandlerDelegate
302     //----------------------------------------------------------------------
303     virtual void
304     IOHandlerActivated (IOHandler &io_handler);
305
306     virtual void
307     IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
308
309 protected:
310
311     bool
312     EnterSession (uint16_t on_entry_flags,
313                   FILE *in,
314                   FILE *out,
315                   FILE *err);
316     
317     void
318     LeaveSession ();
319     
320     void
321     SaveTerminalState (int fd);
322
323     void
324     RestoreTerminalState ();
325     
326     class SynchronicityHandler
327     {
328     private:
329         lldb::DebuggerSP             m_debugger_sp;
330         ScriptedCommandSynchronicity m_synch_wanted;
331         bool                         m_old_asynch;
332     public:
333         SynchronicityHandler(lldb::DebuggerSP,
334                              ScriptedCommandSynchronicity);
335         ~SynchronicityHandler();
336     };
337     
338     class ScriptInterpreterPythonObject : public ScriptInterpreterObject
339     {
340     public:
341         ScriptInterpreterPythonObject() :
342         ScriptInterpreterObject()
343         {}
344         
345         ScriptInterpreterPythonObject(void* obj) :
346         ScriptInterpreterObject(obj)
347         {
348             Py_XINCREF(m_object);
349         }
350         
351         explicit operator bool ()
352         {
353             return m_object && m_object != Py_None;
354         }
355         
356         
357         virtual
358         ~ScriptInterpreterPythonObject()
359         {
360             if (Py_IsInitialized())
361                 Py_XDECREF(m_object);
362             m_object = NULL;
363         }
364         private:
365             DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
366     };
367 public:
368         class Locker : public ScriptInterpreterLocker
369         {
370         public:
371         
372         enum OnEntry
373         {
374             AcquireLock         = 0x0001,
375             InitSession         = 0x0002,
376             InitGlobals         = 0x0004,
377             NoSTDIN             = 0x0008
378         };
379         
380         enum OnLeave
381         {
382             FreeLock            = 0x0001,
383             FreeAcquiredLock    = 0x0002,    // do not free the lock if we already held it when calling constructor
384             TearDownSession     = 0x0004
385         };
386         
387         Locker (ScriptInterpreterPython *py_interpreter = NULL,
388                 uint16_t on_entry = AcquireLock | InitSession,
389                 uint16_t on_leave = FreeLock | TearDownSession,
390                 FILE *in = NULL,
391                 FILE *out = NULL,
392                 FILE *err = NULL);
393         
394         ~Locker ();
395
396         private:
397         
398         bool
399         DoAcquireLock ();
400         
401         bool
402         DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
403         
404         bool
405         DoFreeLock ();
406         
407         bool
408         DoTearDownSession ();
409
410         static void
411         ReleasePythonLock ();
412         
413         bool                     m_teardown_session;
414         ScriptInterpreterPython *m_python_interpreter;
415 //      FILE*                    m_tmp_fh;
416         PyGILState_STATE         m_GILState;
417         };
418 protected:
419
420     enum ActiveIOHandler {
421         eIOHandlerNone,
422         eIOHandlerBreakpoint,
423         eIOHandlerWatchpoint
424     };
425     PythonObject &
426     GetMainModule ();
427     
428     PythonDictionary &
429     GetSessionDictionary ();
430     
431     PythonDictionary &
432     GetSysModuleDictionary ();
433
434     bool
435     GetEmbeddedInterpreterModuleObjects ();
436     
437     PythonObject m_saved_stdin;
438     PythonObject m_saved_stdout;
439     PythonObject m_saved_stderr;
440     PythonObject m_main_module;
441     PythonObject m_lldb_module;
442     PythonDictionary m_session_dict;
443     PythonDictionary m_sys_module_dict;
444     PythonObject m_run_one_line_function;
445     PythonObject m_run_one_line_str_global;
446     std::string m_dictionary_name;
447     TerminalState m_terminal_state;
448     ActiveIOHandler m_active_io_handler;
449     bool m_session_is_active;
450     bool m_pty_slave_is_open;
451     bool m_valid_session;
452     PyThreadState *m_command_thread_state;
453 };
454 } // namespace lldb_private
455
456 #endif // #ifdef LLDB_DISABLE_PYTHON
457
458 #endif // #ifndef liblldb_ScriptInterpreterPython_h_