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 #if defined (__APPLE__)
21 #include <Python/Python.h>
26 #include "lldb/lldb-private.h"
27 #include "lldb/Interpreter/ScriptInterpreter.h"
28 #include "lldb/Core/InputReader.h"
29 #include "lldb/Host/Terminal.h"
31 namespace lldb_private {
33 class ScriptInterpreterPython : public ScriptInterpreter
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);
102 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor);
104 virtual lldb::ValueObjectSP
105 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx);
108 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name);
111 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
114 MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor);
117 RunScriptBasedCommand(const char* impl_function,
119 ScriptedCommandSynchronicity synchronicity,
120 lldb_private::CommandReturnObject& cmd_retobj,
124 GenerateFunction(const char *signature, const StringList &input);
127 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output);
130 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output);
133 GenerateBreakpointOptionsCommandCallback (void *baton,
135 lldb::InputReaderAction notification,
140 GenerateWatchpointOptionsCommandCallback (void *baton,
142 lldb::InputReaderAction notification,
147 BreakpointCallbackFunction (void *baton,
148 StoppointCallbackContext *context,
149 lldb::user_id_t break_id,
150 lldb::user_id_t break_loc_id);
153 WatchpointCallbackFunction (void *baton,
154 StoppointCallbackContext *context,
155 lldb::user_id_t watch_id);
158 GetScriptedSummary (const char *function_name,
159 lldb::ValueObjectSP valobj,
160 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
161 std::string& retval);
164 GetDocumentationForItem (const char* item, std::string& dest);
167 CheckObjectExists (const char* name)
169 if (!name || !name[0])
172 return GetDocumentationForItem (name,temp);
176 RunScriptFormatKeyword (const char* impl_function,
182 RunScriptFormatKeyword (const char* impl_function,
188 RunScriptFormatKeyword (const char* impl_function,
194 RunScriptFormatKeyword (const char* impl_function,
200 LoadScriptingModule (const char* filename,
203 lldb_private::Error& error);
205 virtual lldb::ScriptInterpreterObjectSP
206 MakeScriptObject (void* object);
208 virtual std::unique_ptr<ScriptInterpreterLocker>
209 AcquireInterpreterLock ();
212 CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
213 CommandReturnObject &result);
216 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
217 CommandReturnObject &result);
219 /// Set a Python one-liner as the callback for the breakpoint.
221 SetBreakpointCommandCallback (BreakpointOptions *bp_options,
222 const char *oneliner);
224 /// Set a one-liner as the callback for the watchpoint.
226 SetWatchpointCommandCallback (WatchpointOptions *wp_options,
227 const char *oneliner);
230 ReadCommandInputFromUser (FILE *in_file);
233 ResetOutputFileHandle (FILE *new_fh);
235 static lldb::thread_result_t
236 RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton);
239 InitializePrivate ();
242 InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
247 EnterSession (bool init_lldb_globals);
253 SaveTerminalState (int fd);
256 RestoreTerminalState ();
260 class SynchronicityHandler
263 lldb::DebuggerSP m_debugger_sp;
264 ScriptedCommandSynchronicity m_synch_wanted;
267 SynchronicityHandler(lldb::DebuggerSP,
268 ScriptedCommandSynchronicity);
269 ~SynchronicityHandler();
272 class ScriptInterpreterPythonObject : public ScriptInterpreterObject
275 ScriptInterpreterPythonObject() :
276 ScriptInterpreterObject()
279 ScriptInterpreterPythonObject(void* obj) :
280 ScriptInterpreterObject(obj)
282 Py_XINCREF(m_object);
287 return m_object && m_object != Py_None;
292 ~ScriptInterpreterPythonObject()
294 Py_XDECREF(m_object);
298 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject);
301 class Locker : public ScriptInterpreterLocker
307 AcquireLock = 0x0001,
308 InitSession = 0x0002,
315 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor
316 TearDownSession = 0x0004
319 Locker (ScriptInterpreterPython *py_interpreter = NULL,
320 uint16_t on_entry = AcquireLock | InitSession,
321 uint16_t on_leave = FreeLock | TearDownSession,
322 FILE* wait_msg_handle = NULL);
332 DoInitSession (bool init_lldb_globals);
338 DoTearDownSession ();
341 ReleasePythonLock ();
343 bool m_teardown_session;
344 ScriptInterpreterPython *m_python_interpreter;
346 PyGILState_STATE m_GILState;
349 class PythonInputReaderManager
352 PythonInputReaderManager (ScriptInterpreterPython *interpreter);
359 ~PythonInputReaderManager();
364 InputReaderCallback (void *baton,
366 lldb::InputReaderAction notification,
370 static lldb::thread_result_t
371 RunPythonInputReader (lldb::thread_arg_t baton);
373 ScriptInterpreterPython *m_interpreter;
374 lldb::DebuggerSP m_debugger_sp;
375 lldb::InputReaderSP m_reader_sp;
380 InputReaderCallback (void *baton,
382 lldb::InputReaderAction notification,
387 lldb_utility::PseudoTerminal m_embedded_thread_pty;
388 lldb_utility::PseudoTerminal m_embedded_python_pty;
389 lldb::InputReaderSP m_embedded_thread_input_reader_sp;
390 lldb::InputReaderSP m_embedded_python_input_reader_sp;
392 PyObject *m_new_sysout;
393 PyObject *m_old_sysout;
394 PyObject *m_old_syserr;
395 PyObject *m_run_one_line;
396 std::string m_dictionary_name;
397 TerminalState m_terminal_state;
398 bool m_session_is_active;
399 bool m_pty_slave_is_open;
400 bool m_valid_session;
401 PyThreadState *m_command_thread_state;
403 } // namespace lldb_private
405 #endif // #ifdef LLDB_DISABLE_PYTHON
407 #endif // #ifndef liblldb_ScriptInterpreterPython_h_