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