ScriptInterpreterPython.h revision 400105dd92731a0c8968d6296b0b7279b483ebf0
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#ifdef LLDB_DISABLE_PYTHON 15 16// Python is disabled in this build 17 18#else 19 20#if defined (__APPLE__) 21#include <Python/Python.h> 22#else 23#include <Python.h> 24#endif 25 26#include "lldb/lldb-private.h" 27#include "lldb/Interpreter/ScriptInterpreter.h" 28#include "lldb/Core/InputReader.h" 29#include "lldb/Host/Terminal.h" 30 31namespace lldb_private { 32 33class ScriptInterpreterPython : public ScriptInterpreter 34{ 35public: 36 37 ScriptInterpreterPython (CommandInterpreter &interpreter); 38 39 ~ScriptInterpreterPython (); 40 41 bool 42 ExecuteOneLine (const char *command, CommandReturnObject *result); 43 44 void 45 ExecuteInterpreterLoop (); 46 47 bool 48 ExecuteOneLineWithReturn (const char *in_string, 49 ScriptInterpreter::ScriptReturnType return_type, 50 void *ret_value); 51 52 bool 53 ExecuteMultipleLines (const char *in_string); 54 55 bool 56 ExportFunctionDefinitionToInterpreter (StringList &function_def); 57 58 bool 59 GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL); 60 61 bool 62 GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL); 63 64 bool 65 GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL); 66 67 // use this if the function code is just a one-liner script 68 bool 69 GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL); 70 71 virtual bool 72 GenerateScriptAliasFunction (StringList &input, std::string& output); 73 74 lldb::ScriptInterpreterObjectSP 75 CreateSyntheticScriptedProvider (std::string class_name, 76 lldb::ValueObjectSP valobj); 77 78 virtual uint32_t 79 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor); 80 81 virtual lldb::ValueObjectSP 82 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx); 83 84 virtual int 85 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name); 86 87 virtual void 88 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor); 89 90 virtual bool 91 RunScriptBasedCommand(const char* impl_function, 92 const char* args, 93 ScriptedCommandSynchronicity synchronicity, 94 lldb_private::CommandReturnObject& cmd_retobj, 95 Error& error); 96 97 bool 98 GenerateFunction(const char *signature, const StringList &input); 99 100 bool 101 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output); 102 103 static size_t 104 GenerateBreakpointOptionsCommandCallback (void *baton, 105 InputReader &reader, 106 lldb::InputReaderAction notification, 107 const char *bytes, 108 size_t bytes_len); 109 110 static bool 111 BreakpointCallbackFunction (void *baton, 112 StoppointCallbackContext *context, 113 lldb::user_id_t break_id, 114 lldb::user_id_t break_loc_id); 115 116 virtual bool 117 GetScriptedSummary (const char *function_name, 118 lldb::ValueObjectSP valobj, 119 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, 120 std::string& retval); 121 122 virtual std::string 123 GetDocumentationForItem (const char* item); 124 125 virtual bool 126 LoadScriptingModule (const char* filename, 127 bool can_reload, 128 lldb_private::Error& error); 129 130 virtual lldb::ScriptInterpreterObjectSP 131 MakeScriptObject (void* object); 132 133 void 134 CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, 135 CommandReturnObject &result); 136 137 /// Set a Python one-liner as the callback for the breakpoint. 138 void 139 SetBreakpointCommandCallback (BreakpointOptions *bp_options, 140 const char *oneliner); 141 142 StringList 143 ReadCommandInputFromUser (FILE *in_file); 144 145 virtual void 146 ResetOutputFileHandle (FILE *new_fh); 147 148 static lldb::thread_result_t 149 RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton); 150 151 static void 152 InitializePrivate (); 153 154 static void 155 InitializeInterpreter (SWIGInitCallback python_swig_init_callback); 156 157protected: 158 159 void 160 EnterSession (); 161 162 void 163 LeaveSession (); 164 165 void 166 SaveTerminalState (int fd); 167 168 void 169 RestoreTerminalState (); 170 171private: 172 173 class SynchronicityHandler 174 { 175 private: 176 lldb::DebuggerSP m_debugger_sp; 177 ScriptedCommandSynchronicity m_synch_wanted; 178 bool m_old_asynch; 179 public: 180 SynchronicityHandler(lldb::DebuggerSP, 181 ScriptedCommandSynchronicity); 182 ~SynchronicityHandler(); 183 }; 184 185 class ScriptInterpreterPythonObject : public ScriptInterpreterObject 186 { 187 public: 188 ScriptInterpreterPythonObject() : 189 ScriptInterpreterObject() 190 {} 191 192 ScriptInterpreterPythonObject(void* obj) : 193 ScriptInterpreterObject(obj) 194 { 195 Py_XINCREF(m_object); 196 } 197 198 virtual 199 ~ScriptInterpreterPythonObject() 200 { 201 Py_XDECREF(m_object); 202 m_object = NULL; 203 } 204 private: 205 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); 206 }; 207 208 class Locker 209 { 210 public: 211 212 enum OnEntry 213 { 214 AcquireLock = 0x0001, 215 InitSession = 0x0002 216 }; 217 218 enum OnLeave 219 { 220 FreeLock = 0x0001, 221 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor 222 TearDownSession = 0x0004 223 }; 224 225 Locker (ScriptInterpreterPython *py_interpreter = NULL, 226 uint16_t on_entry = AcquireLock | InitSession, 227 uint16_t on_leave = FreeLock | TearDownSession, 228 FILE* wait_msg_handle = NULL); 229 230 ~Locker (); 231 232 static bool 233 CurrentThreadHasPythonLock (); 234 235 private: 236 237 bool 238 DoAcquireLock (); 239 240 bool 241 DoInitSession (); 242 243 bool 244 DoFreeLock (); 245 246 bool 247 DoTearDownSession (); 248 249 static bool 250 TryGetPythonLock (uint32_t seconds_to_wait); 251 252 static void 253 ReleasePythonLock (); 254 255 bool m_need_session; 256 bool m_release_lock; 257 ScriptInterpreterPython *m_python_interpreter; 258 FILE* m_tmp_fh; 259 }; 260 261 static size_t 262 InputReaderCallback (void *baton, 263 InputReader &reader, 264 lldb::InputReaderAction notification, 265 const char *bytes, 266 size_t bytes_len); 267 268 269 lldb_utility::PseudoTerminal m_embedded_python_pty; 270 lldb::InputReaderSP m_embedded_thread_input_reader_sp; 271 FILE *m_dbg_stdout; 272 PyObject *m_new_sysout; 273 PyObject *m_old_sysout; 274 PyObject *m_old_syserr; 275 PyObject *m_run_one_line; 276 std::string m_dictionary_name; 277 TerminalState m_terminal_state; 278 bool m_session_is_active; 279 bool m_pty_slave_is_open; 280 bool m_valid_session; 281 282}; 283} // namespace lldb_private 284 285#endif // #ifdef LLDB_DISABLE_PYTHON 286 287#endif // #ifndef liblldb_ScriptInterpreterPython_h_ 288