ScriptInterpreterPython.h revision 263363
1127474Stjr//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===// 2127474Stjr// 3127474Stjr// The LLVM Compiler Infrastructure 4127474Stjr// 5127474Stjr// This file is distributed under the University of Illinois Open Source 6127474Stjr// License. See LICENSE.TXT for details. 7174990Sache// 8127474Stjr//===----------------------------------------------------------------------===// 9127474Stjr 10127474Stjr 11127474Stjr#ifndef liblldb_ScriptInterpreterPython_h_ 12127474Stjr#define liblldb_ScriptInterpreterPython_h_ 13127474Stjr 14127474Stjr#ifdef LLDB_DISABLE_PYTHON 15127474Stjr 16127474Stjr// Python is disabled in this build 17127474Stjr 18127474Stjr#else 19127474Stjr 20127474Stjr#if defined (__APPLE__) 21127474Stjr#include <Python/Python.h> 22174990Sache#else 23127474Stjr#include <Python.h> 24127474Stjr#endif 25127474Stjr 26127474Stjr#include "lldb/lldb-private.h" 27127474Stjr#include "lldb/Interpreter/ScriptInterpreter.h" 28127474Stjr#include "lldb/Core/InputReader.h" 29127474Stjr#include "lldb/Host/Terminal.h" 30127474Stjr 31127474Stjrnamespace lldb_private { 32127474Stjr 33127474Stjrclass ScriptInterpreterPython : public ScriptInterpreter 34127474Stjr{ 35127474Stjrpublic: 36127474Stjr 37174990Sache ScriptInterpreterPython (CommandInterpreter &interpreter); 38127474Stjr 39193498Sedwin ~ScriptInterpreterPython (); 40193498Sedwin 41193498Sedwin bool 42193498Sedwin ExecuteOneLine (const char *command, 43193498Sedwin CommandReturnObject *result, 44193498Sedwin const ExecuteScriptOptions &options = ExecuteScriptOptions()); 45193498Sedwin 46127474Stjr void 47174990Sache ExecuteInterpreterLoop (); 48127474Stjr 49193498Sedwin bool 50193498Sedwin ExecuteOneLineWithReturn (const char *in_string, 51193498Sedwin ScriptInterpreter::ScriptReturnType return_type, 52193498Sedwin void *ret_value, 53193498Sedwin const ExecuteScriptOptions &options = ExecuteScriptOptions()); 54193498Sedwin 55193498Sedwin bool 56127474Stjr ExecuteMultipleLines (const char *in_string, 57127474Stjr const ExecuteScriptOptions &options = ExecuteScriptOptions()); 58127474Stjr 59127474Stjr bool 60127474Stjr ExportFunctionDefinitionToInterpreter (StringList &function_def); 61127474Stjr 62127474Stjr bool 63193498Sedwin GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL); 64127474Stjr 65127474Stjr bool 66127474Stjr GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL); 67127474Stjr 68127474Stjr bool 69127474Stjr GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL); 70127474Stjr 71127474Stjr // use this if the function code is just a one-liner script 72127474Stjr bool 73127474Stjr GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL); 74127474Stjr 75127474Stjr virtual bool 76127474Stjr GenerateScriptAliasFunction (StringList &input, std::string& output); 77127474Stjr 78127474Stjr lldb::ScriptInterpreterObjectSP 79127474Stjr CreateSyntheticScriptedProvider (const char *class_name, 80127474Stjr lldb::ValueObjectSP valobj); 81174990Sache 82127474Stjr virtual lldb::ScriptInterpreterObjectSP 83127474Stjr OSPlugin_CreatePluginObject (const char *class_name, 84127474Stjr lldb::ProcessSP process_sp); 85127474Stjr 86127474Stjr virtual lldb::ScriptInterpreterObjectSP 87127474Stjr OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp); 88127474Stjr 89127474Stjr virtual lldb::ScriptInterpreterObjectSP 90127474Stjr OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp); 91127474Stjr 92127474Stjr virtual lldb::ScriptInterpreterObjectSP 93127474Stjr OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, 94127474Stjr lldb::tid_t thread_id); 95127474Stjr 96127474Stjr virtual lldb::ScriptInterpreterObjectSP 97127474Stjr OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, 98127474Stjr lldb::tid_t tid, 99127474Stjr lldb::addr_t context); 100127474Stjr 101127474Stjr virtual lldb::ScriptInterpreterObjectSP 102127474Stjr LoadPluginModule (const FileSpec& file_spec, 103127474Stjr lldb_private::Error& error); 104 105 virtual lldb::ScriptInterpreterObjectSP 106 GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp, 107 Target* target, 108 const char* setting_name, 109 lldb_private::Error& error); 110 111 virtual size_t 112 CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor); 113 114 virtual lldb::ValueObjectSP 115 GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx); 116 117 virtual int 118 GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name); 119 120 virtual bool 121 UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor); 122 123 virtual bool 124 MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor); 125 126 virtual bool 127 RunScriptBasedCommand(const char* impl_function, 128 const char* args, 129 ScriptedCommandSynchronicity synchronicity, 130 lldb_private::CommandReturnObject& cmd_retobj, 131 Error& error); 132 133 bool 134 GenerateFunction(const char *signature, const StringList &input); 135 136 bool 137 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output); 138 139 bool 140 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output); 141 142 static size_t 143 GenerateBreakpointOptionsCommandCallback (void *baton, 144 InputReader &reader, 145 lldb::InputReaderAction notification, 146 const char *bytes, 147 size_t bytes_len); 148 149 static size_t 150 GenerateWatchpointOptionsCommandCallback (void *baton, 151 InputReader &reader, 152 lldb::InputReaderAction notification, 153 const char *bytes, 154 size_t bytes_len); 155 156 static bool 157 BreakpointCallbackFunction (void *baton, 158 StoppointCallbackContext *context, 159 lldb::user_id_t break_id, 160 lldb::user_id_t break_loc_id); 161 162 static bool 163 WatchpointCallbackFunction (void *baton, 164 StoppointCallbackContext *context, 165 lldb::user_id_t watch_id); 166 167 virtual bool 168 GetScriptedSummary (const char *function_name, 169 lldb::ValueObjectSP valobj, 170 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, 171 std::string& retval); 172 173 virtual bool 174 GetDocumentationForItem (const char* item, std::string& dest); 175 176 virtual bool 177 CheckObjectExists (const char* name) 178 { 179 if (!name || !name[0]) 180 return false; 181 std::string temp; 182 return GetDocumentationForItem (name,temp); 183 } 184 185 virtual bool 186 RunScriptFormatKeyword (const char* impl_function, 187 Process* process, 188 std::string& output, 189 Error& error); 190 191 virtual bool 192 RunScriptFormatKeyword (const char* impl_function, 193 Thread* thread, 194 std::string& output, 195 Error& error); 196 197 virtual bool 198 RunScriptFormatKeyword (const char* impl_function, 199 Target* target, 200 std::string& output, 201 Error& error); 202 203 virtual bool 204 RunScriptFormatKeyword (const char* impl_function, 205 StackFrame* frame, 206 std::string& output, 207 Error& error); 208 209 virtual bool 210 LoadScriptingModule (const char* filename, 211 bool can_reload, 212 bool init_session, 213 lldb_private::Error& error, 214 lldb::ScriptInterpreterObjectSP* module_sp = nullptr); 215 216 virtual lldb::ScriptInterpreterObjectSP 217 MakeScriptObject (void* object); 218 219 virtual std::unique_ptr<ScriptInterpreterLocker> 220 AcquireInterpreterLock (); 221 222 void 223 CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, 224 CommandReturnObject &result); 225 226 void 227 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, 228 CommandReturnObject &result); 229 230 /// Set a Python one-liner as the callback for the breakpoint. 231 void 232 SetBreakpointCommandCallback (BreakpointOptions *bp_options, 233 const char *oneliner); 234 235 /// Set a one-liner as the callback for the watchpoint. 236 void 237 SetWatchpointCommandCallback (WatchpointOptions *wp_options, 238 const char *oneliner); 239 240 StringList 241 ReadCommandInputFromUser (FILE *in_file); 242 243 virtual void 244 ResetOutputFileHandle (FILE *new_fh); 245 246 static lldb::thread_result_t 247 RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton); 248 249 static void 250 InitializePrivate (); 251 252 static void 253 InitializeInterpreter (SWIGInitCallback python_swig_init_callback, 254 SWIGBreakpointCallbackFunction swig_breakpoint_callback, 255 SWIGWatchpointCallbackFunction swig_watchpoint_callback, 256 SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, 257 SWIGPythonCreateSyntheticProvider swig_synthetic_script, 258 SWIGPythonCalculateNumChildren swig_calc_children, 259 SWIGPythonGetChildAtIndex swig_get_child_index, 260 SWIGPythonGetIndexOfChildWithName swig_get_index_child, 261 SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , 262 SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, 263 SWIGPythonUpdateSynthProviderInstance swig_update_provider, 264 SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, 265 SWIGPythonCallCommand swig_call_command, 266 SWIGPythonCallModuleInit swig_call_module_init, 267 SWIGPythonCreateOSPlugin swig_create_os_plugin, 268 SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, 269 SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, 270 SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, 271 SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, 272 SWIGPython_GetDynamicSetting swig_plugin_get); 273 274protected: 275 276 bool 277 EnterSession (bool init_lldb_globals); 278 279 void 280 LeaveSession (); 281 282 void 283 SaveTerminalState (int fd); 284 285 void 286 RestoreTerminalState (); 287 288private: 289 290 class SynchronicityHandler 291 { 292 private: 293 lldb::DebuggerSP m_debugger_sp; 294 ScriptedCommandSynchronicity m_synch_wanted; 295 bool m_old_asynch; 296 public: 297 SynchronicityHandler(lldb::DebuggerSP, 298 ScriptedCommandSynchronicity); 299 ~SynchronicityHandler(); 300 }; 301 302 class ScriptInterpreterPythonObject : public ScriptInterpreterObject 303 { 304 public: 305 ScriptInterpreterPythonObject() : 306 ScriptInterpreterObject() 307 {} 308 309 ScriptInterpreterPythonObject(void* obj) : 310 ScriptInterpreterObject(obj) 311 { 312 Py_XINCREF(m_object); 313 } 314 315 explicit operator bool () 316 { 317 return m_object && m_object != Py_None; 318 } 319 320 321 virtual 322 ~ScriptInterpreterPythonObject() 323 { 324 Py_XDECREF(m_object); 325 m_object = NULL; 326 } 327 private: 328 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); 329 }; 330 331 class Locker : public ScriptInterpreterLocker 332 { 333 public: 334 335 enum OnEntry 336 { 337 AcquireLock = 0x0001, 338 InitSession = 0x0002, 339 InitGlobals = 0x0004 340 }; 341 342 enum OnLeave 343 { 344 FreeLock = 0x0001, 345 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor 346 TearDownSession = 0x0004 347 }; 348 349 Locker (ScriptInterpreterPython *py_interpreter = NULL, 350 uint16_t on_entry = AcquireLock | InitSession, 351 uint16_t on_leave = FreeLock | TearDownSession, 352 FILE* wait_msg_handle = NULL); 353 354 ~Locker (); 355 356 private: 357 358 bool 359 DoAcquireLock (); 360 361 bool 362 DoInitSession (bool init_lldb_globals); 363 364 bool 365 DoFreeLock (); 366 367 bool 368 DoTearDownSession (); 369 370 static void 371 ReleasePythonLock (); 372 373 bool m_teardown_session; 374 ScriptInterpreterPython *m_python_interpreter; 375 FILE* m_tmp_fh; 376 PyGILState_STATE m_GILState; 377 }; 378 379 class PythonInputReaderManager 380 { 381 public: 382 PythonInputReaderManager (ScriptInterpreterPython *interpreter); 383 384 explicit operator bool() 385 { 386 return m_error; 387 } 388 389 ~PythonInputReaderManager(); 390 391 private: 392 393 static size_t 394 InputReaderCallback (void *baton, 395 InputReader &reader, 396 lldb::InputReaderAction notification, 397 const char *bytes, 398 size_t bytes_len); 399 400 static lldb::thread_result_t 401 RunPythonInputReader (lldb::thread_arg_t baton); 402 403 ScriptInterpreterPython *m_interpreter; 404 lldb::DebuggerSP m_debugger_sp; 405 lldb::InputReaderSP m_reader_sp; 406 bool m_error; 407 }; 408 409 static size_t 410 InputReaderCallback (void *baton, 411 InputReader &reader, 412 lldb::InputReaderAction notification, 413 const char *bytes, 414 size_t bytes_len); 415 416 417 lldb_utility::PseudoTerminal m_embedded_thread_pty; 418 lldb_utility::PseudoTerminal m_embedded_python_pty; 419 lldb::InputReaderSP m_embedded_thread_input_reader_sp; 420 lldb::InputReaderSP m_embedded_python_input_reader_sp; 421 FILE *m_dbg_stdout; 422 PyObject *m_new_sysout; 423 PyObject *m_old_sysout; 424 PyObject *m_old_syserr; 425 PyObject *m_run_one_line; 426 std::string m_dictionary_name; 427 TerminalState m_terminal_state; 428 bool m_session_is_active; 429 bool m_pty_slave_is_open; 430 bool m_valid_session; 431 PyThreadState *m_command_thread_state; 432}; 433} // namespace lldb_private 434 435#endif // #ifdef LLDB_DISABLE_PYTHON 436 437#endif // #ifndef liblldb_ScriptInterpreterPython_h_ 438