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