ScriptInterpreterPython.h revision 155ee91cc315888c26de1bfebd876bf35b857329
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, bool enable_io); 43 44 void 45 ExecuteInterpreterLoop (); 46 47 bool 48 ExecuteOneLineWithReturn (const char *in_string, 49 ScriptInterpreter::ScriptReturnType return_type, 50 void *ret_value, 51 bool enable_io); 52 53 bool 54 ExecuteMultipleLines (const char *in_string, bool enable_io); 55 56 bool 57 ExportFunctionDefinitionToInterpreter (StringList &function_def); 58 59 bool 60 GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL); 61 62 bool 63 GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL); 64 65 bool 66 GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL); 67 68 // use this if the function code is just a one-liner script 69 bool 70 GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL); 71 72 virtual bool 73 GenerateScriptAliasFunction (StringList &input, std::string& output); 74 75 lldb::ScriptInterpreterObjectSP 76 CreateSyntheticScriptedProvider (std::string class_name, 77 lldb::ValueObjectSP valobj); 78 79 virtual lldb::ScriptInterpreterObjectSP 80 CreateOSPlugin (std::string class_name, 81 lldb::ProcessSP process_sp); 82 83 virtual lldb::ScriptInterpreterObjectSP 84 OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object); 85 86 virtual uint32_t 87 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor); 88 89 virtual lldb::ValueObjectSP 90 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx); 91 92 virtual int 93 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name); 94 95 virtual bool 96 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor); 97 98 virtual bool 99 RunScriptBasedCommand(const char* impl_function, 100 const char* args, 101 ScriptedCommandSynchronicity synchronicity, 102 lldb_private::CommandReturnObject& cmd_retobj, 103 Error& error); 104 105 bool 106 GenerateFunction(const char *signature, const StringList &input); 107 108 bool 109 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output); 110 111 bool 112 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output); 113 114 static size_t 115 GenerateBreakpointOptionsCommandCallback (void *baton, 116 InputReader &reader, 117 lldb::InputReaderAction notification, 118 const char *bytes, 119 size_t bytes_len); 120 121 static size_t 122 GenerateWatchpointOptionsCommandCallback (void *baton, 123 InputReader &reader, 124 lldb::InputReaderAction notification, 125 const char *bytes, 126 size_t bytes_len); 127 128 static bool 129 BreakpointCallbackFunction (void *baton, 130 StoppointCallbackContext *context, 131 lldb::user_id_t break_id, 132 lldb::user_id_t break_loc_id); 133 134 static bool 135 WatchpointCallbackFunction (void *baton, 136 StoppointCallbackContext *context, 137 lldb::user_id_t watch_id); 138 139 virtual bool 140 GetScriptedSummary (const char *function_name, 141 lldb::ValueObjectSP valobj, 142 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, 143 std::string& retval); 144 145 virtual std::string 146 GetDocumentationForItem (const char* item); 147 148 virtual bool 149 LoadScriptingModule (const char* filename, 150 bool can_reload, 151 lldb_private::Error& error); 152 153 virtual lldb::ScriptInterpreterObjectSP 154 MakeScriptObject (void* object); 155 156 void 157 CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, 158 CommandReturnObject &result); 159 160 void 161 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, 162 CommandReturnObject &result); 163 164 /// Set a Python one-liner as the callback for the breakpoint. 165 void 166 SetBreakpointCommandCallback (BreakpointOptions *bp_options, 167 const char *oneliner); 168 169 /// Set a one-liner as the callback for the watchpoint. 170 void 171 SetWatchpointCommandCallback (WatchpointOptions *wp_options, 172 const char *oneliner); 173 174 StringList 175 ReadCommandInputFromUser (FILE *in_file); 176 177 virtual void 178 ResetOutputFileHandle (FILE *new_fh); 179 180 static lldb::thread_result_t 181 RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton); 182 183 static void 184 InitializePrivate (); 185 186 static void 187 InitializeInterpreter (SWIGInitCallback python_swig_init_callback); 188 189protected: 190 191 void 192 EnterSession (); 193 194 void 195 LeaveSession (); 196 197 void 198 SaveTerminalState (int fd); 199 200 void 201 RestoreTerminalState (); 202 203private: 204 205 class SynchronicityHandler 206 { 207 private: 208 lldb::DebuggerSP m_debugger_sp; 209 ScriptedCommandSynchronicity m_synch_wanted; 210 bool m_old_asynch; 211 public: 212 SynchronicityHandler(lldb::DebuggerSP, 213 ScriptedCommandSynchronicity); 214 ~SynchronicityHandler(); 215 }; 216 217 class ScriptInterpreterPythonObject : public ScriptInterpreterObject 218 { 219 public: 220 ScriptInterpreterPythonObject() : 221 ScriptInterpreterObject() 222 {} 223 224 ScriptInterpreterPythonObject(void* obj) : 225 ScriptInterpreterObject(obj) 226 { 227 Py_XINCREF(m_object); 228 } 229 230 virtual 231 ~ScriptInterpreterPythonObject() 232 { 233 Py_XDECREF(m_object); 234 m_object = NULL; 235 } 236 private: 237 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); 238 }; 239 240 class Locker 241 { 242 public: 243 244 enum OnEntry 245 { 246 AcquireLock = 0x0001, 247 InitSession = 0x0002 248 }; 249 250 enum OnLeave 251 { 252 FreeLock = 0x0001, 253 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor 254 TearDownSession = 0x0004 255 }; 256 257 Locker (ScriptInterpreterPython *py_interpreter = NULL, 258 uint16_t on_entry = AcquireLock | InitSession, 259 uint16_t on_leave = FreeLock | TearDownSession, 260 FILE* wait_msg_handle = NULL); 261 262 ~Locker (); 263 264 private: 265 266 bool 267 DoAcquireLock (); 268 269 bool 270 DoInitSession (); 271 272 bool 273 DoFreeLock (); 274 275 bool 276 DoTearDownSession (); 277 278 static void 279 ReleasePythonLock (); 280 281 bool m_need_session; 282 ScriptInterpreterPython *m_python_interpreter; 283 FILE* m_tmp_fh; 284 PyGILState_STATE m_GILState; 285 }; 286 287 class PythonInputReaderManager 288 { 289 public: 290 PythonInputReaderManager (ScriptInterpreterPython *interpreter); 291 292 operator bool() 293 { 294 return m_error; 295 } 296 297 ~PythonInputReaderManager(); 298 299 private: 300 301 static size_t 302 InputReaderCallback (void *baton, 303 InputReader &reader, 304 lldb::InputReaderAction notification, 305 const char *bytes, 306 size_t bytes_len); 307 308 static lldb::thread_result_t 309 RunPythonInputReader (lldb::thread_arg_t baton); 310 311 ScriptInterpreterPython *m_interpreter; 312 lldb::DebuggerSP m_debugger_sp; 313 lldb::InputReaderSP m_reader_sp; 314 bool m_error; 315 }; 316 317 static size_t 318 InputReaderCallback (void *baton, 319 InputReader &reader, 320 lldb::InputReaderAction notification, 321 const char *bytes, 322 size_t bytes_len); 323 324 325 lldb_utility::PseudoTerminal m_embedded_thread_pty; 326 lldb_utility::PseudoTerminal m_embedded_python_pty; 327 lldb::InputReaderSP m_embedded_thread_input_reader_sp; 328 lldb::InputReaderSP m_embedded_python_input_reader_sp; 329 FILE *m_dbg_stdout; 330 PyObject *m_new_sysout; 331 PyObject *m_old_sysout; 332 PyObject *m_old_syserr; 333 PyObject *m_run_one_line; 334 std::string m_dictionary_name; 335 TerminalState m_terminal_state; 336 bool m_session_is_active; 337 bool m_pty_slave_is_open; 338 bool m_valid_session; 339}; 340} // namespace lldb_private 341 342#endif // #ifdef LLDB_DISABLE_PYTHON 343 344#endif // #ifndef liblldb_ScriptInterpreterPython_h_ 345