ScriptInterpreterPython.h revision 59df36f99b76e33852e6848a162f5c2851074ea2
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#if defined (__APPLE__) 15#include <Python/Python.h> 16#else 17#include <Python.h> 18#endif 19 20#include "lldb/lldb-private.h" 21#include "lldb/Interpreter/ScriptInterpreter.h" 22#include "lldb/Core/InputReader.h" 23#include "lldb/Host/Terminal.h" 24 25namespace lldb_private { 26 27class ScriptInterpreterPython : public ScriptInterpreter 28{ 29public: 30 31 ScriptInterpreterPython (CommandInterpreter &interpreter); 32 33 ~ScriptInterpreterPython (); 34 35 bool 36 ExecuteOneLine (const char *command, CommandReturnObject *result); 37 38 void 39 ExecuteInterpreterLoop (); 40 41 bool 42 ExecuteOneLineWithReturn (const char *in_string, 43 ScriptInterpreter::ScriptReturnType return_type, 44 void *ret_value); 45 46 bool 47 ExecuteMultipleLines (const char *in_string); 48 49 bool 50 ExportFunctionDefinitionToInterpreter (StringList &function_def); 51 52 bool 53 GenerateTypeScriptFunction (StringList &input, StringList &output); 54 55 bool 56 GenerateTypeSynthClass (StringList &input, StringList &output); 57 58 // use this if the function code is just a one-liner script 59 bool 60 GenerateTypeScriptFunction (const char* oneliner, StringList &output); 61 62 virtual bool 63 GenerateScriptAliasFunction (StringList &input, StringList &output); 64 65 void* 66 CreateSyntheticScriptedProvider (std::string class_name, 67 lldb::ValueObjectSP valobj); 68 69 virtual uint32_t 70 CalculateNumChildren (void *implementor); 71 72 virtual lldb::ValueObjectSP 73 GetChildAtIndex (void *implementor, uint32_t idx); 74 75 virtual int 76 GetIndexOfChildWithName (void *implementor, const char* child_name); 77 78 virtual void 79 UpdateSynthProviderInstance (void* implementor); 80 81 virtual bool 82 RunScriptBasedCommand(const char* impl_function, 83 const char* args, 84 lldb_private::CommandReturnObject& cmd_retobj, 85 Error& error); 86 87 bool 88 GenerateFunction(std::string& signature, StringList &input, StringList &output); 89 90 bool 91 GenerateBreakpointCommandCallbackData (StringList &input, StringList &output); 92 93 static size_t 94 GenerateBreakpointOptionsCommandCallback (void *baton, 95 InputReader &reader, 96 lldb::InputReaderAction notification, 97 const char *bytes, 98 size_t bytes_len); 99 100 static bool 101 BreakpointCallbackFunction (void *baton, 102 StoppointCallbackContext *context, 103 lldb::user_id_t break_id, 104 lldb::user_id_t break_loc_id); 105 106 static std::string 107 CallPythonScriptFunction (const char *python_function_name, 108 lldb::ValueObjectSP valobj); 109 110 virtual std::string 111 GetDocumentationForItem (const char* item); 112 113 virtual bool 114 LoadScriptingModule (const char* filename, 115 lldb_private::Error& error); 116 117 void 118 CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, 119 CommandReturnObject &result); 120 121 /// Set a Python one-liner as the callback for the breakpoint. 122 void 123 SetBreakpointCommandCallback (BreakpointOptions *bp_options, 124 const char *oneliner); 125 126 StringList 127 ReadCommandInputFromUser (FILE *in_file); 128 129 virtual void 130 ResetOutputFileHandle (FILE *new_fh); 131 132 static lldb::thread_result_t 133 RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton); 134 135 static void 136 InitializePrivate (); 137 138 static void 139 InitializeInterpreter (SWIGInitCallback python_swig_init_callback, 140 SWIGBreakpointCallbackFunction python_swig_breakpoint_callback, 141 SWIGPythonTypeScriptCallbackFunction python_swig_typescript_callback, 142 SWIGPythonCreateSyntheticProvider python_swig_synthetic_script, 143 SWIGPythonCalculateNumChildren python_swig_calc_children, 144 SWIGPythonGetChildAtIndex python_swig_get_child_index, 145 SWIGPythonGetIndexOfChildWithName python_swig_get_index_child, 146 SWIGPythonCastPyObjectToSBValue python_swig_cast_to_sbvalue, 147 SWIGPythonUpdateSynthProviderInstance python_swig_update_provider, 148 SWIGPythonCallCommand python_swig_call_command, 149 SWIGPythonCallModuleInit python_swig_call_mod_init); 150 151protected: 152 153 void 154 EnterSession (); 155 156 void 157 LeaveSession (); 158 159 void 160 SaveTerminalState (int fd); 161 162 void 163 RestoreTerminalState (); 164 165private: 166 167 class Locker 168 { 169 public: 170 Locker (ScriptInterpreterPython *py_interpreter, 171 FILE* wait_msg_handle = NULL, 172 bool need_session = true); 173 174 bool 175 HasAcquiredLock () 176 { 177 return m_release_lock; 178 } 179 180 ~Locker (); 181 182 private: 183 bool m_need_session; 184 bool m_release_lock; 185 ScriptInterpreterPython *m_python_interpreter; 186 FILE* m_tmp_fh; 187 }; 188 189 static size_t 190 InputReaderCallback (void *baton, 191 InputReader &reader, 192 lldb::InputReaderAction notification, 193 const char *bytes, 194 size_t bytes_len); 195 196 197 lldb_utility::PseudoTerminal m_embedded_python_pty; 198 lldb::InputReaderSP m_embedded_thread_input_reader_sp; 199 FILE *m_dbg_stdout; 200 PyObject *m_new_sysout; 201 std::string m_dictionary_name; 202 TerminalState m_terminal_state; 203 bool m_session_is_active; 204 bool m_pty_slave_is_open; 205 bool m_valid_session; 206 207}; 208} // namespace lldb_private 209 210 211#endif // #ifndef liblldb_ScriptInterpreterPython_h_ 212