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 LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H 12 #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H 13 14 #ifdef LLDB_DISABLE_PYTHON 15 16 // Python is disabled in this build 17 18 #else 19 20 #include "lldb/lldb-private.h" 21 #include "PythonDataObjects.h" 22 #include "lldb/Core/IOHandler.h" 23 #include "lldb/Interpreter/ScriptInterpreter.h" 24 #include "lldb/Host/Terminal.h" 25 26 class IOHandlerPythonInterpreter; 27 28 namespace lldb_private { 29 30 class ScriptInterpreterPython : 31 public ScriptInterpreter, 32 public IOHandlerDelegateMultiline 33 { 34 public: 35 typedef void (*SWIGInitCallback) (void); 36 37 typedef bool (*SWIGBreakpointCallbackFunction) (const char *python_function_name, 38 const char *session_dictionary_name, 39 const lldb::StackFrameSP& frame_sp, 40 const lldb::BreakpointLocationSP &bp_loc_sp); 41 42 typedef bool (*SWIGWatchpointCallbackFunction) (const char *python_function_name, 43 const char *session_dictionary_name, 44 const lldb::StackFrameSP& frame_sp, 45 const lldb::WatchpointSP &wp_sp); 46 47 typedef bool (*SWIGPythonTypeScriptCallbackFunction) (const char *python_function_name, 48 void *session_dictionary, 49 const lldb::ValueObjectSP& valobj_sp, 50 void** pyfunct_wrapper, 51 const lldb::TypeSummaryOptionsSP& options, 52 std::string& retval); 53 54 typedef void* (*SWIGPythonCreateSyntheticProvider) (const char *python_class_name, 55 const char *session_dictionary_name, 56 const lldb::ValueObjectSP& valobj_sp); 57 58 typedef void* (*SWIGPythonCreateCommandObject) (const char *python_class_name, 59 const char *session_dictionary_name, 60 const lldb::DebuggerSP debugger_sp); 61 62 typedef void* (*SWIGPythonCreateScriptedThreadPlan) (const char *python_class_name, 63 const char *session_dictionary_name, 64 const lldb::ThreadPlanSP& thread_plan_sp); 65 66 typedef bool (*SWIGPythonCallThreadPlan) (void *implementor, const char *method_name, Event *event_sp, bool &got_error); 67 68 typedef void* (*SWIGPythonCreateOSPlugin) (const char *python_class_name, 69 const char *session_dictionary_name, 70 const lldb::ProcessSP& process_sp); 71 72 typedef size_t (*SWIGPythonCalculateNumChildren) (void *implementor); 73 typedef void* (*SWIGPythonGetChildAtIndex) (void *implementor, uint32_t idx); 74 typedef int (*SWIGPythonGetIndexOfChildWithName) (void *implementor, const char* child_name); 75 typedef void* (*SWIGPythonCastPyObjectToSBValue) (void* data); 76 typedef lldb::ValueObjectSP (*SWIGPythonGetValueObjectSPFromSBValue) (void* data); 77 typedef bool (*SWIGPythonUpdateSynthProviderInstance) (void* data); 78 typedef bool (*SWIGPythonMightHaveChildrenSynthProviderInstance) (void* data); 79 typedef void* (*SWIGPythonGetValueSynthProviderInstance) (void *implementor); 80 81 typedef bool (*SWIGPythonCallCommand) (const char *python_function_name, 82 const char *session_dictionary_name, 83 lldb::DebuggerSP& debugger, 84 const char* args, 85 lldb_private::CommandReturnObject& cmd_retobj, 86 lldb::ExecutionContextRefSP exe_ctx_ref_sp); 87 88 typedef bool (*SWIGPythonCallCommandObject) (void *implementor, 89 lldb::DebuggerSP& debugger, 90 const char* args, 91 lldb_private::CommandReturnObject& cmd_retobj, 92 lldb::ExecutionContextRefSP exe_ctx_ref_sp); 93 94 95 typedef bool (*SWIGPythonCallModuleInit) (const char *python_module_name, 96 const char *session_dictionary_name, 97 lldb::DebuggerSP& debugger); 98 99 typedef bool (*SWIGPythonScriptKeyword_Process) (const char* python_function_name, 100 const char* session_dictionary_name, 101 lldb::ProcessSP& process, 102 std::string& output); 103 typedef bool (*SWIGPythonScriptKeyword_Thread) (const char* python_function_name, 104 const char* session_dictionary_name, 105 lldb::ThreadSP& thread, 106 std::string& output); 107 108 typedef bool (*SWIGPythonScriptKeyword_Target) (const char* python_function_name, 109 const char* session_dictionary_name, 110 lldb::TargetSP& target, 111 std::string& output); 112 113 typedef bool (*SWIGPythonScriptKeyword_Frame) (const char* python_function_name, 114 const char* session_dictionary_name, 115 lldb::StackFrameSP& frame, 116 std::string& output); 117 118 typedef bool (*SWIGPythonScriptKeyword_Value) (const char* python_function_name, 119 const char* session_dictionary_name, 120 lldb::ValueObjectSP& value, 121 std::string& output); 122 123 typedef void* (*SWIGPython_GetDynamicSetting) (void* module, 124 const char* setting, 125 const lldb::TargetSP& target_sp); 126 127 friend class ::IOHandlerPythonInterpreter; 128 129 ScriptInterpreterPython (CommandInterpreter &interpreter); 130 131 ~ScriptInterpreterPython() override; 132 133 bool 134 Interrupt() override; 135 136 bool 137 ExecuteOneLine (const char *command, 138 CommandReturnObject *result, 139 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 140 141 void 142 ExecuteInterpreterLoop () override; 143 144 bool 145 ExecuteOneLineWithReturn (const char *in_string, 146 ScriptInterpreter::ScriptReturnType return_type, 147 void *ret_value, 148 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 149 150 lldb_private::Error 151 ExecuteMultipleLines (const char *in_string, 152 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 153 154 Error 155 ExportFunctionDefinitionToInterpreter (StringList &function_def) override; 156 157 bool 158 GenerateTypeScriptFunction (StringList &input, std::string& output, const void* name_token = NULL) override; 159 160 bool 161 GenerateTypeSynthClass (StringList &input, std::string& output, const void* name_token = NULL) override; 162 163 bool 164 GenerateTypeSynthClass (const char* oneliner, std::string& output, const void* name_token = NULL) override; 165 166 // use this if the function code is just a one-liner script 167 bool 168 GenerateTypeScriptFunction (const char* oneliner, std::string& output, const void* name_token = NULL) override; 169 170 bool 171 GenerateScriptAliasFunction (StringList &input, std::string& output) override; 172 173 StructuredData::ObjectSP CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj) override; 174 175 StructuredData::GenericSP CreateScriptCommandObject (const char *class_name) override; 176 177 StructuredData::ObjectSP CreateScriptedThreadPlan(const char *class_name, lldb::ThreadPlanSP thread_plan) override; 178 179 bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error) override; 180 bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error) override; 181 lldb::StateType ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, bool &script_error) override; 182 183 StructuredData::GenericSP OSPlugin_CreatePluginObject(const char *class_name, lldb::ProcessSP process_sp) override; 184 185 StructuredData::DictionarySP OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 186 187 StructuredData::ArraySP OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 188 189 StructuredData::StringSP OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t thread_id) override; 190 191 StructuredData::DictionarySP OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid, 192 lldb::addr_t context) override; 193 194 StructuredData::ObjectSP LoadPluginModule(const FileSpec &file_spec, lldb_private::Error &error) override; 195 196 StructuredData::DictionarySP GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, const char *setting_name, 197 lldb_private::Error &error) override; 198 199 size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor) override; 200 201 lldb::ValueObjectSP GetChildAtIndex(const StructuredData::ObjectSP &implementor, uint32_t idx) override; 202 203 int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, const char *child_name) override; 204 205 bool UpdateSynthProviderInstance(const StructuredData::ObjectSP &implementor) override; 206 207 bool MightHaveChildrenSynthProviderInstance(const StructuredData::ObjectSP &implementor) override; 208 209 lldb::ValueObjectSP GetSyntheticValue(const StructuredData::ObjectSP &implementor) override; 210 211 bool 212 RunScriptBasedCommand(const char* impl_function, 213 const char* args, 214 ScriptedCommandSynchronicity synchronicity, 215 lldb_private::CommandReturnObject& cmd_retobj, 216 Error& error, 217 const lldb_private::ExecutionContext& exe_ctx) override; 218 219 bool 220 RunScriptBasedCommand (StructuredData::GenericSP impl_obj_sp, 221 const char* args, 222 ScriptedCommandSynchronicity synchronicity, 223 lldb_private::CommandReturnObject& cmd_retobj, 224 Error& error, 225 const lldb_private::ExecutionContext& exe_ctx) override; 226 227 Error 228 GenerateFunction(const char *signature, const StringList &input) override; 229 230 Error 231 GenerateBreakpointCommandCallbackData (StringList &input, std::string& output) override; 232 233 bool 234 GenerateWatchpointCommandCallbackData (StringList &input, std::string& output) override; 235 236 // static size_t 237 // GenerateBreakpointOptionsCommandCallback (void *baton, 238 // InputReader &reader, 239 // lldb::InputReaderAction notification, 240 // const char *bytes, 241 // size_t bytes_len); 242 // 243 // static size_t 244 // GenerateWatchpointOptionsCommandCallback (void *baton, 245 // InputReader &reader, 246 // lldb::InputReaderAction notification, 247 // const char *bytes, 248 // size_t bytes_len); 249 250 static bool 251 BreakpointCallbackFunction (void *baton, 252 StoppointCallbackContext *context, 253 lldb::user_id_t break_id, 254 lldb::user_id_t break_loc_id); 255 256 static bool 257 WatchpointCallbackFunction (void *baton, 258 StoppointCallbackContext *context, 259 lldb::user_id_t watch_id); 260 261 bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, 262 const TypeSummaryOptions &options, std::string &retval) override; 263 264 void 265 Clear () override; 266 267 bool 268 GetDocumentationForItem (const char* item, std::string& dest) override; 269 270 bool 271 GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string& dest) override; 272 273 uint32_t 274 GetFlagsForCommandObject (StructuredData::GenericSP cmd_obj_sp) override; 275 276 bool 277 GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string& dest) override; 278 279 bool 280 CheckObjectExists (const char* name) override 281 { 282 if (!name || !name[0]) 283 return false; 284 std::string temp; 285 return GetDocumentationForItem (name,temp); 286 } 287 288 bool 289 RunScriptFormatKeyword (const char* impl_function, 290 Process* process, 291 std::string& output, 292 Error& error) override; 293 294 bool 295 RunScriptFormatKeyword (const char* impl_function, 296 Thread* thread, 297 std::string& output, 298 Error& error) override; 299 300 bool 301 RunScriptFormatKeyword (const char* impl_function, 302 Target* target, 303 std::string& output, 304 Error& error) override; 305 306 bool 307 RunScriptFormatKeyword (const char* impl_function, 308 StackFrame* frame, 309 std::string& output, 310 Error& error) override; 311 312 bool 313 RunScriptFormatKeyword (const char* impl_function, 314 ValueObject* value, 315 std::string& output, 316 Error& error) override; 317 318 bool LoadScriptingModule(const char *filename, bool can_reload, bool init_session, lldb_private::Error &error, 319 StructuredData::ObjectSP *module_sp = nullptr) override; 320 321 bool 322 IsReservedWord (const char* word) override; 323 324 std::unique_ptr<ScriptInterpreterLocker> 325 AcquireInterpreterLock () override; 326 327 void 328 CollectDataForBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec, 329 CommandReturnObject &result) override; 330 331 void 332 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, 333 CommandReturnObject &result) override; 334 335 /// Set the callback body text into the callback for the breakpoint. 336 Error 337 SetBreakpointCommandCallback (BreakpointOptions *bp_options, 338 const char *callback_body) override; 339 340 void 341 SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options, 342 const char *function_name) override; 343 344 /// Set a one-liner as the callback for the watchpoint. 345 void 346 SetWatchpointCommandCallback (WatchpointOptions *wp_options, 347 const char *oneliner) override; 348 349 StringList 350 ReadCommandInputFromUser (FILE *in_file); 351 352 void ResetOutputFileHandle(FILE *new_fh) override; 353 354 static void 355 InitializePrivate (); 356 357 static void 358 InitializeInterpreter (SWIGInitCallback python_swig_init_callback, 359 SWIGBreakpointCallbackFunction swig_breakpoint_callback, 360 SWIGWatchpointCallbackFunction swig_watchpoint_callback, 361 SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, 362 SWIGPythonCreateSyntheticProvider swig_synthetic_script, 363 SWIGPythonCreateCommandObject swig_create_cmd, 364 SWIGPythonCalculateNumChildren swig_calc_children, 365 SWIGPythonGetChildAtIndex swig_get_child_index, 366 SWIGPythonGetIndexOfChildWithName swig_get_index_child, 367 SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , 368 SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, 369 SWIGPythonUpdateSynthProviderInstance swig_update_provider, 370 SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, 371 SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider, 372 SWIGPythonCallCommand swig_call_command, 373 SWIGPythonCallCommandObject swig_call_command_object, 374 SWIGPythonCallModuleInit swig_call_module_init, 375 SWIGPythonCreateOSPlugin swig_create_os_plugin, 376 SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, 377 SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, 378 SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, 379 SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, 380 SWIGPythonScriptKeyword_Value swig_run_script_keyword_value, 381 SWIGPython_GetDynamicSetting swig_plugin_get, 382 SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script, 383 SWIGPythonCallThreadPlan swig_call_thread_plan); 384 385 const char * 386 GetDictionaryName () 387 { 388 return m_dictionary_name.c_str(); 389 } 390 391 392 PyThreadState * 393 GetThreadState() 394 { 395 return m_command_thread_state; 396 } 397 398 void 399 SetThreadState (PyThreadState *s) 400 { 401 if (s) 402 m_command_thread_state = s; 403 } 404 405 //---------------------------------------------------------------------- 406 // IOHandlerDelegate 407 //---------------------------------------------------------------------- 408 void 409 IOHandlerActivated (IOHandler &io_handler) override; 410 411 void 412 IOHandlerInputComplete (IOHandler &io_handler, std::string &data) override; 413 414 415 //------------------------------------------------------------------ 416 // Static Functions 417 //------------------------------------------------------------------ 418 static void 419 Initialize(); 420 421 static void 422 Terminate(); 423 424 static lldb::ScriptInterpreterSP 425 CreateInstance(CommandInterpreter &interpreter); 426 427 static lldb_private::ConstString 428 GetPluginNameStatic(); 429 430 static const char * 431 GetPluginDescriptionStatic(); 432 433 //------------------------------------------------------------------ 434 // PluginInterface protocol 435 //------------------------------------------------------------------ 436 virtual lldb_private::ConstString 437 GetPluginName() override; 438 439 virtual uint32_t 440 GetPluginVersion() override; 441 442 protected: 443 444 bool 445 EnterSession (uint16_t on_entry_flags, 446 FILE *in, 447 FILE *out, 448 FILE *err); 449 450 void 451 LeaveSession (); 452 453 void 454 SaveTerminalState (int fd); 455 456 void 457 RestoreTerminalState (); 458 459 class SynchronicityHandler 460 { 461 private: 462 lldb::DebuggerSP m_debugger_sp; 463 ScriptedCommandSynchronicity m_synch_wanted; 464 bool m_old_asynch; 465 public: 466 SynchronicityHandler(lldb::DebuggerSP, 467 ScriptedCommandSynchronicity); 468 ~SynchronicityHandler(); 469 }; 470 471 public: 472 class Locker : public ScriptInterpreterLocker 473 { 474 public: 475 476 enum OnEntry 477 { 478 AcquireLock = 0x0001, 479 InitSession = 0x0002, 480 InitGlobals = 0x0004, 481 NoSTDIN = 0x0008 482 }; 483 484 enum OnLeave 485 { 486 FreeLock = 0x0001, 487 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor 488 TearDownSession = 0x0004 489 }; 490 491 Locker (ScriptInterpreterPython *py_interpreter = NULL, 492 uint16_t on_entry = AcquireLock | InitSession, 493 uint16_t on_leave = FreeLock | TearDownSession, 494 FILE *in = NULL, 495 FILE *out = NULL, 496 FILE *err = NULL); 497 498 ~Locker () override; 499 500 private: 501 502 bool 503 DoAcquireLock (); 504 505 bool 506 DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); 507 508 bool 509 DoFreeLock (); 510 511 bool 512 DoTearDownSession (); 513 514 static void 515 ReleasePythonLock (); 516 517 bool m_teardown_session; 518 ScriptInterpreterPython *m_python_interpreter; 519 // FILE* m_tmp_fh; 520 PyGILState_STATE m_GILState; 521 }; 522 protected: 523 enum class AddLocation 524 { 525 Beginning, 526 End 527 }; 528 529 static void AddToSysPath(AddLocation location, std::string path); 530 531 uint32_t 532 IsExecutingPython () const 533 { 534 return m_lock_count > 0; 535 } 536 537 uint32_t 538 IncrementLockCount() 539 { 540 return ++m_lock_count; 541 } 542 543 uint32_t 544 DecrementLockCount() 545 { 546 if (m_lock_count > 0) 547 --m_lock_count; 548 return m_lock_count; 549 } 550 551 enum ActiveIOHandler { 552 eIOHandlerNone, 553 eIOHandlerBreakpoint, 554 eIOHandlerWatchpoint 555 }; 556 557 PythonObject &GetMainModule(); 558 559 PythonDictionary & 560 GetSessionDictionary (); 561 562 PythonDictionary & 563 GetSysModuleDictionary (); 564 565 bool 566 GetEmbeddedInterpreterModuleObjects (); 567 568 PythonObject m_saved_stdin; 569 PythonObject m_saved_stdout; 570 PythonObject m_saved_stderr; 571 PythonObject m_main_module; 572 PythonObject m_lldb_module; 573 PythonDictionary m_session_dict; 574 PythonDictionary m_sys_module_dict; 575 PythonObject m_run_one_line_function; 576 PythonObject m_run_one_line_str_global; 577 std::string m_dictionary_name; 578 TerminalState m_terminal_state; 579 ActiveIOHandler m_active_io_handler; 580 bool m_session_is_active; 581 bool m_pty_slave_is_open; 582 bool m_valid_session; 583 uint32_t m_lock_count; 584 PyThreadState *m_command_thread_state; 585 }; 586 } // namespace lldb_private 587 588 #endif // LLDB_DISABLE_PYTHON 589 590 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H 591