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 lldb::ScriptInterpreterObjectSP
85 virtual CreateScriptedThreadPlan (const char *class_name,
86 lldb::ThreadPlanSP thread_plan);
89 ScriptedThreadPlanExplainsStop (lldb::ScriptInterpreterObjectSP implementor_sp,
93 ScriptedThreadPlanShouldStop (lldb::ScriptInterpreterObjectSP implementor_sp,
96 virtual lldb::StateType
97 ScriptedThreadPlanGetRunState (lldb::ScriptInterpreterObjectSP implementor_sp,
100 virtual lldb::ScriptInterpreterObjectSP
101 OSPlugin_CreatePluginObject (const char *class_name,
102 lldb::ProcessSP process_sp);
104 virtual lldb::ScriptInterpreterObjectSP
105 OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
107 virtual lldb::ScriptInterpreterObjectSP
108 OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp);
110 virtual lldb::ScriptInterpreterObjectSP
111 OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
112 lldb::tid_t thread_id);
114 virtual lldb::ScriptInterpreterObjectSP
115 OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
117 lldb::addr_t context);
119 virtual lldb::ScriptInterpreterObjectSP
120 LoadPluginModule (const FileSpec& file_spec,
121 lldb_private::Error& error);
123 virtual lldb::ScriptInterpreterObjectSP
124 GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp,
126 const char* setting_name,
127 lldb_private::Error& error);
130 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
132 virtual lldb::ValueObjectSP
133 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
136 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
139 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
142 MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
144 virtual lldb::ValueObjectSP
145 GetSyntheticValue (const lldb::ScriptInterpreterObjectSP& implementor);
148 RunScriptBasedCommand(const char* impl_function,
150 ScriptedCommandSynchronicity synchronicity,
151 lldb_private::CommandReturnObject& cmd_retobj,
153 const lldb_private::ExecutionContext& exe_ctx);
156 GenerateFunction(const char *signature, const StringList &input);
159 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
162 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
165 // GenerateBreakpointOptionsCommandCallback (void *baton,
166 // InputReader &reader,
167 // lldb::InputReaderAction notification,
168 // const char *bytes,
169 // size_t bytes_len);
172 // GenerateWatchpointOptionsCommandCallback (void *baton,
173 // InputReader &reader,
174 // lldb::InputReaderAction notification,
175 // const char *bytes,
176 // size_t bytes_len);
179 BreakpointCallbackFunction (void *baton,
180 StoppointCallbackContext *context,
181 lldb::user_id_t break_id,
182 lldb::user_id_t break_loc_id);
185 WatchpointCallbackFunction (void *baton,
186 StoppointCallbackContext *context,
187 lldb::user_id_t watch_id);
190 GetScriptedSummary (const char *function_name,
191 lldb::ValueObjectSP valobj,
192 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
193 const TypeSummaryOptions& options,
194 std::string& retval);
200 GetDocumentationForItem (const char* item, std::string& dest);
203 CheckObjectExists (const char* name)
205 if (!name || !name[0])
208 return GetDocumentationForItem (name,temp);
212 RunScriptFormatKeyword (const char* impl_function,
218 RunScriptFormatKeyword (const char* impl_function,
224 RunScriptFormatKeyword (const char* impl_function,
230 RunScriptFormatKeyword (const char* impl_function,
236 RunScriptFormatKeyword (const char* impl_function,
242 LoadScriptingModule (const char* filename,
245 lldb_private::Error& error,
246 lldb::ScriptInterpreterObjectSP* module_sp = nullptr);
248 virtual lldb::ScriptInterpreterObjectSP
249 MakeScriptObject (void* object);
251 virtual std::unique_ptr<ScriptInterpreterLocker>
252 AcquireInterpreterLock ();
255 CollectDataForBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec,
256 CommandReturnObject &result);
259 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
260 CommandReturnObject &result);
262 /// Set the callback body text into the callback for the breakpoint.
264 SetBreakpointCommandCallback (BreakpointOptions *bp_options,
265 const char *callback_body);
268 SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options,
269 const char *function_name);
271 /// Set a one-liner as the callback for the watchpoint.
273 SetWatchpointCommandCallback (WatchpointOptions *wp_options,
274 const char *oneliner);
277 ReadCommandInputFromUser (FILE *in_file);
280 ResetOutputFileHandle (FILE *new_fh);
283 InitializePrivate ();
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);
314 return m_dictionary_name.c_str();
321 return m_command_thread_state;
325 SetThreadState (PyThreadState *s)
328 m_command_thread_state = s;
331 //----------------------------------------------------------------------
333 //----------------------------------------------------------------------
335 IOHandlerActivated (IOHandler &io_handler);
338 IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
343 EnterSession (uint16_t on_entry_flags,
352 SaveTerminalState (int fd);
355 RestoreTerminalState ();
357 class SynchronicityHandler
360 lldb::DebuggerSP m_debugger_sp;
361 ScriptedCommandSynchronicity m_synch_wanted;
364 SynchronicityHandler(lldb::DebuggerSP,
365 ScriptedCommandSynchronicity);
366 ~SynchronicityHandler();
369 class ScriptInterpreterPythonObject : public ScriptInterpreterObject
372 ScriptInterpreterPythonObject() :
373 ScriptInterpreterObject()
376 ScriptInterpreterPythonObject(void* obj) :
377 ScriptInterpreterObject(obj)
379 Py_XINCREF(m_object);
382 explicit operator bool ()
384 return m_object && m_object != Py_None;
389 ~ScriptInterpreterPythonObject()
391 if (Py_IsInitialized())
392 Py_XDECREF(m_object);
396 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
399 class Locker : public ScriptInterpreterLocker
405 AcquireLock = 0x0001,
406 InitSession = 0x0002,
407 InitGlobals = 0x0004,
414 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor
415 TearDownSession = 0x0004
418 Locker (ScriptInterpreterPython *py_interpreter = NULL,
419 uint16_t on_entry = AcquireLock | InitSession,
420 uint16_t on_leave = FreeLock | TearDownSession,
433 DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
439 DoTearDownSession ();
442 ReleasePythonLock ();
444 bool m_teardown_session;
445 ScriptInterpreterPython *m_python_interpreter;
447 PyGILState_STATE m_GILState;
451 enum ActiveIOHandler {
453 eIOHandlerBreakpoint,
460 GetSessionDictionary ();
463 GetSysModuleDictionary ();
466 GetEmbeddedInterpreterModuleObjects ();
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;
485 } // namespace lldb_private
487 #endif // #ifdef LLDB_DISABLE_PYTHON
489 #endif // #ifndef liblldb_ScriptInterpreterPython_h_