1 //===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #ifndef liblldb_ScriptInterpreterPython_h_
12 #define liblldb_ScriptInterpreterPython_h_
14 #ifdef LLDB_DISABLE_PYTHON
16 // Python is disabled in this build
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"
27 namespace lldb_private {
29 class ScriptInterpreterPython :
30 public ScriptInterpreter,
31 public IOHandlerDelegateMultiline
35 friend class IOHandlerPythonInterpreter;
37 ScriptInterpreterPython (CommandInterpreter &interpreter);
39 ~ScriptInterpreterPython ();
42 ExecuteOneLine (const char *command,
43 CommandReturnObject *result,
44 const ExecuteScriptOptions &options = ExecuteScriptOptions());
47 ExecuteInterpreterLoop ();
50 ExecuteOneLineWithReturn (const char *in_string,
51 ScriptInterpreter::ScriptReturnType return_type,
53 const ExecuteScriptOptions &options = ExecuteScriptOptions());
56 ExecuteMultipleLines (const char *in_string,
57 const ExecuteScriptOptions &options = ExecuteScriptOptions());
60 ExportFunctionDefinitionToInterpreter (StringList &function_def);
63 GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL);
66 GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL);
69 GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL);
71 // use this if the function code is just a one-liner script
73 GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL);
76 GenerateScriptAliasFunction (StringList &input, std::string& output);
78 lldb::ScriptInterpreterObjectSP
79 CreateSyntheticScriptedProvider (const char *class_name,
80 lldb::ValueObjectSP valobj);
82 virtual lldb::ScriptInterpreterObjectSP
83 OSPlugin_CreatePluginObject (const char *class_name,
84 lldb::ProcessSP process_sp);
86 virtual lldb::ScriptInterpreterObjectSP
87 OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
89 virtual lldb::ScriptInterpreterObjectSP
90 OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
92 virtual lldb::ScriptInterpreterObjectSP
93 OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
94 lldb::tid_t thread_id);
96 virtual lldb::ScriptInterpreterObjectSP
97 OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
99 lldb::addr_t context);
101 virtual lldb::ScriptInterpreterObjectSP
102 LoadPluginModule (const FileSpec& file_spec,
103 lldb_private::Error& error);
105 virtual lldb::ScriptInterpreterObjectSP
106 GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp,
108 const char* setting_name,
109 lldb_private::Error& error);
112 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
114 virtual lldb::ValueObjectSP
115 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
118 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
121 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
124 MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
127 RunScriptBasedCommand(const char* impl_function,
129 ScriptedCommandSynchronicity synchronicity,
130 lldb_private::CommandReturnObject& cmd_retobj,
134 GenerateFunction(const char *signature, const StringList &input);
137 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
140 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
143 // GenerateBreakpointOptionsCommandCallback (void *baton,
144 // InputReader &reader,
145 // lldb::InputReaderAction notification,
146 // const char *bytes,
147 // size_t bytes_len);
150 // GenerateWatchpointOptionsCommandCallback (void *baton,
151 // InputReader &reader,
152 // lldb::InputReaderAction notification,
153 // const char *bytes,
154 // size_t bytes_len);
157 BreakpointCallbackFunction (void *baton,
158 StoppointCallbackContext *context,
159 lldb::user_id_t break_id,
160 lldb::user_id_t break_loc_id);
163 WatchpointCallbackFunction (void *baton,
164 StoppointCallbackContext *context,
165 lldb::user_id_t watch_id);
168 GetScriptedSummary (const char *function_name,
169 lldb::ValueObjectSP valobj,
170 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
171 std::string& retval);
174 GetDocumentationForItem (const char* item, std::string& dest);
177 CheckObjectExists (const char* name)
179 if (!name || !name[0])
182 return GetDocumentationForItem (name,temp);
186 RunScriptFormatKeyword (const char* impl_function,
192 RunScriptFormatKeyword (const char* impl_function,
198 RunScriptFormatKeyword (const char* impl_function,
204 RunScriptFormatKeyword (const char* impl_function,
210 LoadScriptingModule (const char* filename,
213 lldb_private::Error& error,
214 lldb::ScriptInterpreterObjectSP* module_sp = nullptr);
216 virtual lldb::ScriptInterpreterObjectSP
217 MakeScriptObject (void* object);
219 virtual std::unique_ptr<ScriptInterpreterLocker>
220 AcquireInterpreterLock ();
223 CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
224 CommandReturnObject &result);
227 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
228 CommandReturnObject &result);
230 /// Set a Python one-liner as the callback for the breakpoint.
232 SetBreakpointCommandCallback (BreakpointOptions *bp_options,
233 const char *oneliner);
235 /// Set a one-liner as the callback for the watchpoint.
237 SetWatchpointCommandCallback (WatchpointOptions *wp_options,
238 const char *oneliner);
241 ReadCommandInputFromUser (FILE *in_file);
244 ResetOutputFileHandle (FILE *new_fh);
247 InitializePrivate ();
250 InitializeInterpreter (SWIGInitCallback python_swig_init_callback,
251 SWIGBreakpointCallbackFunction swig_breakpoint_callback,
252 SWIGWatchpointCallbackFunction swig_watchpoint_callback,
253 SWIGPythonTypeScriptCallbackFunction swig_typescript_callback,
254 SWIGPythonCreateSyntheticProvider swig_synthetic_script,
255 SWIGPythonCalculateNumChildren swig_calc_children,
256 SWIGPythonGetChildAtIndex swig_get_child_index,
257 SWIGPythonGetIndexOfChildWithName swig_get_index_child,
258 SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue ,
259 SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue,
260 SWIGPythonUpdateSynthProviderInstance swig_update_provider,
261 SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider,
262 SWIGPythonCallCommand swig_call_command,
263 SWIGPythonCallModuleInit swig_call_module_init,
264 SWIGPythonCreateOSPlugin swig_create_os_plugin,
265 SWIGPythonScriptKeyword_Process swig_run_script_keyword_process,
266 SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread,
267 SWIGPythonScriptKeyword_Target swig_run_script_keyword_target,
268 SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame,
269 SWIGPython_GetDynamicSetting swig_plugin_get);
274 return m_dictionary_name.c_str();
278 //----------------------------------------------------------------------
280 //----------------------------------------------------------------------
282 IOHandlerActivated (IOHandler &io_handler);
285 IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
290 EnterSession (uint16_t on_entry_flags,
299 SaveTerminalState (int fd);
302 RestoreTerminalState ();
304 class SynchronicityHandler
307 lldb::DebuggerSP m_debugger_sp;
308 ScriptedCommandSynchronicity m_synch_wanted;
311 SynchronicityHandler(lldb::DebuggerSP,
312 ScriptedCommandSynchronicity);
313 ~SynchronicityHandler();
316 class ScriptInterpreterPythonObject : public ScriptInterpreterObject
319 ScriptInterpreterPythonObject() :
320 ScriptInterpreterObject()
323 ScriptInterpreterPythonObject(void* obj) :
324 ScriptInterpreterObject(obj)
326 Py_XINCREF(m_object);
329 explicit operator bool ()
331 return m_object && m_object != Py_None;
336 ~ScriptInterpreterPythonObject()
338 Py_XDECREF(m_object);
342 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
345 class Locker : public ScriptInterpreterLocker
351 AcquireLock = 0x0001,
352 InitSession = 0x0002,
353 InitGlobals = 0x0004,
360 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor
361 TearDownSession = 0x0004
364 Locker (ScriptInterpreterPython *py_interpreter = NULL,
365 uint16_t on_entry = AcquireLock | InitSession,
366 uint16_t on_leave = FreeLock | TearDownSession,
379 DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
385 DoTearDownSession ();
388 ReleasePythonLock ();
390 bool m_teardown_session;
391 ScriptInterpreterPython *m_python_interpreter;
393 PyGILState_STATE m_GILState;
397 enum ActiveIOHandler {
399 eIOHandlerBreakpoint,
406 GetSessionDictionary ();
409 GetSysModuleDictionary ();
412 GetEmbeddedInterpreterModuleObjects ();
414 PythonObject m_saved_stdin;
415 PythonObject m_saved_stdout;
416 PythonObject m_saved_stderr;
417 PythonObject m_main_module;
418 PythonObject m_lldb_module;
419 PythonDictionary m_session_dict;
420 PythonDictionary m_sys_module_dict;
421 PythonObject m_run_one_line_function;
422 PythonObject m_run_one_line_str_global;
423 std::string m_dictionary_name;
424 TerminalState m_terminal_state;
425 ActiveIOHandler m_active_io_handler;
426 bool m_session_is_active;
427 bool m_pty_slave_is_open;
428 bool m_valid_session;
429 PyThreadState *m_command_thread_state;
431 } // namespace lldb_private
433 #endif // #ifdef LLDB_DISABLE_PYTHON
435 #endif // #ifndef liblldb_ScriptInterpreterPython_h_