ScriptInterpreterPython.h revision 9ac7ce312ef8b5327a74d38edf10af86c07586b9
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 const ExecuteScriptOptions &options = ExecuteScriptOptions()); 45 46 void 47 ExecuteInterpreterLoop (); 48 49 bool 50 ExecuteOneLineWithReturn (const char *in_string, 51 ScriptInterpreter::ScriptReturnType return_type, 52 void *ret_value, 53 const ExecuteScriptOptions &options = ExecuteScriptOptions()); 54 55 bool 56 ExecuteMultipleLines (const char *in_string, 57 const ExecuteScriptOptions &options = ExecuteScriptOptions()); 58 59 bool 60 ExportFunctionDefinitionToInterpreter (StringList &function_def); 61 62 bool 63 GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL); 64 65 bool 66 GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL); 67 68 bool 69 GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL); 70 71 // use this if the function code is just a one-liner script 72 bool 73 GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL); 74 75 virtual bool 76 GenerateScriptAliasFunction (StringList &input, std::string& output); 77 78 lldb::ScriptInterpreterObjectSP 79 CreateSyntheticScriptedProvider (std::string class_name, 80 lldb::ValueObjectSP valobj); 81 82 virtual lldb::ScriptInterpreterObjectSP 83 CreateOSPlugin (std::string class_name, 84 lldb::ProcessSP process_sp); 85 86 virtual lldb::ScriptInterpreterObjectSP 87 OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object); 88 89 virtual lldb::ScriptInterpreterObjectSP 90 OSPlugin_QueryForThreadsInfo (lldb::ScriptInterpreterObjectSP object); 91 92 virtual lldb::ScriptInterpreterObjectSP 93 OSPlugin_QueryForRegisterContextData (lldb::ScriptInterpreterObjectSP object, 94 lldb::tid_t thread_id); 95 96 virtual uint32_t 97 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor); 98 99 virtual lldb::ValueObjectSP 100 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx); 101 102 virtual int 103 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name); 104 105 virtual bool 106 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor); 107 108 virtual bool 109 MightHaveChildrenSynthProviderInstance (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 operator bool () 245 { 246 return m_object && m_object != Py_None; 247 } 248 249 250 virtual 251 ~ScriptInterpreterPythonObject() 252 { 253 Py_XDECREF(m_object); 254 m_object = NULL; 255 } 256 private: 257 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); 258 }; 259 260 class Locker 261 { 262 public: 263 264 enum OnEntry 265 { 266 AcquireLock = 0x0001, 267 InitSession = 0x0002 268 }; 269 270 enum OnLeave 271 { 272 FreeLock = 0x0001, 273 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor 274 TearDownSession = 0x0004 275 }; 276 277 Locker (ScriptInterpreterPython *py_interpreter = NULL, 278 uint16_t on_entry = AcquireLock | InitSession, 279 uint16_t on_leave = FreeLock | TearDownSession, 280 FILE* wait_msg_handle = NULL); 281 282 ~Locker (); 283 284 private: 285 286 bool 287 DoAcquireLock (); 288 289 bool 290 DoInitSession (); 291 292 bool 293 DoFreeLock (); 294 295 bool 296 DoTearDownSession (); 297 298 static void 299 ReleasePythonLock (); 300 301 bool m_need_session; 302 ScriptInterpreterPython *m_python_interpreter; 303 FILE* m_tmp_fh; 304 PyGILState_STATE m_GILState; 305 }; 306 307 class PythonInputReaderManager 308 { 309 public: 310 PythonInputReaderManager (ScriptInterpreterPython *interpreter); 311 312 operator bool() 313 { 314 return m_error; 315 } 316 317 ~PythonInputReaderManager(); 318 319 private: 320 321 static size_t 322 InputReaderCallback (void *baton, 323 InputReader &reader, 324 lldb::InputReaderAction notification, 325 const char *bytes, 326 size_t bytes_len); 327 328 static lldb::thread_result_t 329 RunPythonInputReader (lldb::thread_arg_t baton); 330 331 ScriptInterpreterPython *m_interpreter; 332 lldb::DebuggerSP m_debugger_sp; 333 lldb::InputReaderSP m_reader_sp; 334 bool m_error; 335 }; 336 337 static size_t 338 InputReaderCallback (void *baton, 339 InputReader &reader, 340 lldb::InputReaderAction notification, 341 const char *bytes, 342 size_t bytes_len); 343 344 345 lldb_utility::PseudoTerminal m_embedded_thread_pty; 346 lldb_utility::PseudoTerminal m_embedded_python_pty; 347 lldb::InputReaderSP m_embedded_thread_input_reader_sp; 348 lldb::InputReaderSP m_embedded_python_input_reader_sp; 349 FILE *m_dbg_stdout; 350 PyObject *m_new_sysout; 351 PyObject *m_old_sysout; 352 PyObject *m_old_syserr; 353 PyObject *m_run_one_line; 354 std::string m_dictionary_name; 355 TerminalState m_terminal_state; 356 bool m_session_is_active; 357 bool m_pty_slave_is_open; 358 bool m_valid_session; 359}; 360} // namespace lldb_private 361 362#endif // #ifdef LLDB_DISABLE_PYTHON 363 364#endif // #ifndef liblldb_ScriptInterpreterPython_h_ 365