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 class IOHandlerPythonInterpreter;
29 namespace lldb_private {
31 class ScriptInterpreterPython :
32 public ScriptInterpreter,
33 public IOHandlerDelegateMultiline
37 friend class IOHandlerPythonInterpreter;
39 ScriptInterpreterPython (CommandInterpreter &interpreter);
41 ~ScriptInterpreterPython ();
44 ExecuteOneLine (const char *command,
45 CommandReturnObject *result,
46 const ExecuteScriptOptions &options = ExecuteScriptOptions());
49 ExecuteInterpreterLoop ();
52 ExecuteOneLineWithReturn (const char *in_string,
53 ScriptInterpreter::ScriptReturnType return_type,
55 const ExecuteScriptOptions &options = ExecuteScriptOptions());
58 ExecuteMultipleLines (const char *in_string,
59 const ExecuteScriptOptions &options = ExecuteScriptOptions());
62 ExportFunctionDefinitionToInterpreter (StringList &function_def);
65 GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL);
68 GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL);
71 GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL);
73 // use this if the function code is just a one-liner script
75 GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL);
78 GenerateScriptAliasFunction (StringList &input, std::string& output);
80 lldb::ScriptInterpreterObjectSP
81 CreateSyntheticScriptedProvider (const char *class_name,
82 lldb::ValueObjectSP valobj);
84 virtual lldb::ScriptInterpreterObjectSP
85 OSPlugin_CreatePluginObject (const char *class_name,
86 lldb::ProcessSP process_sp);
88 virtual lldb::ScriptInterpreterObjectSP
89 OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
91 virtual lldb::ScriptInterpreterObjectSP
92 OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
94 virtual lldb::ScriptInterpreterObjectSP
95 OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
96 lldb::tid_t thread_id);
98 virtual lldb::ScriptInterpreterObjectSP
99 OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
101 lldb::addr_t context);
103 virtual lldb::ScriptInterpreterObjectSP
104 LoadPluginModule (const FileSpec& file_spec,
105 lldb_private::Error& error);
107 virtual lldb::ScriptInterpreterObjectSP
108 GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp,
110 const char* setting_name,
111 lldb_private::Error& error);
114 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
116 virtual lldb::ValueObjectSP
117 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
120 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
123 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
126 MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
129 RunScriptBasedCommand(const char* impl_function,
131 ScriptedCommandSynchronicity synchronicity,
132 lldb_private::CommandReturnObject& cmd_retobj,
136 GenerateFunction(const char *signature, const StringList &input);
139 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
142 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
145 // GenerateBreakpointOptionsCommandCallback (void *baton,
146 // InputReader &reader,
147 // lldb::InputReaderAction notification,
148 // const char *bytes,
149 // size_t bytes_len);
152 // GenerateWatchpointOptionsCommandCallback (void *baton,
153 // InputReader &reader,
154 // lldb::InputReaderAction notification,
155 // const char *bytes,
156 // size_t bytes_len);
159 BreakpointCallbackFunction (void *baton,
160 StoppointCallbackContext *context,
161 lldb::user_id_t break_id,
162 lldb::user_id_t break_loc_id);
165 WatchpointCallbackFunction (void *baton,
166 StoppointCallbackContext *context,
167 lldb::user_id_t watch_id);
170 GetScriptedSummary (const char *function_name,
171 lldb::ValueObjectSP valobj,
172 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
173 std::string& retval);
179 GetDocumentationForItem (const char* item, std::string& dest);
182 CheckObjectExists (const char* name)
184 if (!name || !name[0])
187 return GetDocumentationForItem (name,temp);
191 RunScriptFormatKeyword (const char* impl_function,
197 RunScriptFormatKeyword (const char* impl_function,
203 RunScriptFormatKeyword (const char* impl_function,
209 RunScriptFormatKeyword (const char* impl_function,
215 LoadScriptingModule (const char* filename,
218 lldb_private::Error& error,
219 lldb::ScriptInterpreterObjectSP* module_sp = nullptr);
221 virtual lldb::ScriptInterpreterObjectSP
222 MakeScriptObject (void* object);
224 virtual std::unique_ptr<ScriptInterpreterLocker>
225 AcquireInterpreterLock ();
228 CollectDataForBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec,
229 CommandReturnObject &result);
232 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
233 CommandReturnObject &result);
235 /// Set the callback body text into the callback for the breakpoint.
237 SetBreakpointCommandCallback (BreakpointOptions *bp_options,
238 const char *callback_body);
241 SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options,
242 const char *function_name);
244 /// Set a one-liner as the callback for the watchpoint.
246 SetWatchpointCommandCallback (WatchpointOptions *wp_options,
247 const char *oneliner);
250 ReadCommandInputFromUser (FILE *in_file);
253 ResetOutputFileHandle (FILE *new_fh);
256 InitializePrivate ();
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);
283 return m_dictionary_name.c_str();
290 return m_command_thread_state;
294 SetThreadState (PyThreadState *s)
297 m_command_thread_state = s;
300 //----------------------------------------------------------------------
302 //----------------------------------------------------------------------
304 IOHandlerActivated (IOHandler &io_handler);
307 IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
312 EnterSession (uint16_t on_entry_flags,
321 SaveTerminalState (int fd);
324 RestoreTerminalState ();
326 class SynchronicityHandler
329 lldb::DebuggerSP m_debugger_sp;
330 ScriptedCommandSynchronicity m_synch_wanted;
333 SynchronicityHandler(lldb::DebuggerSP,
334 ScriptedCommandSynchronicity);
335 ~SynchronicityHandler();
338 class ScriptInterpreterPythonObject : public ScriptInterpreterObject
341 ScriptInterpreterPythonObject() :
342 ScriptInterpreterObject()
345 ScriptInterpreterPythonObject(void* obj) :
346 ScriptInterpreterObject(obj)
348 Py_XINCREF(m_object);
351 explicit operator bool ()
353 return m_object && m_object != Py_None;
358 ~ScriptInterpreterPythonObject()
360 if (Py_IsInitialized())
361 Py_XDECREF(m_object);
365 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
368 class Locker : public ScriptInterpreterLocker
374 AcquireLock = 0x0001,
375 InitSession = 0x0002,
376 InitGlobals = 0x0004,
383 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor
384 TearDownSession = 0x0004
387 Locker (ScriptInterpreterPython *py_interpreter = NULL,
388 uint16_t on_entry = AcquireLock | InitSession,
389 uint16_t on_leave = FreeLock | TearDownSession,
402 DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
408 DoTearDownSession ();
411 ReleasePythonLock ();
413 bool m_teardown_session;
414 ScriptInterpreterPython *m_python_interpreter;
416 PyGILState_STATE m_GILState;
420 enum ActiveIOHandler {
422 eIOHandlerBreakpoint,
429 GetSessionDictionary ();
432 GetSysModuleDictionary ();
435 GetEmbeddedInterpreterModuleObjects ();
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;
454 } // namespace lldb_private
456 #endif // #ifdef LLDB_DISABLE_PYTHON
458 #endif // #ifndef liblldb_ScriptInterpreterPython_h_