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