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#include "lldb/lldb-python.h" 21#include "lldb/lldb-private.h" 22#include "lldb/Core/IOHandler.h" 23#include "lldb/Interpreter/ScriptInterpreter.h" 24#include "lldb/Interpreter/PythonDataObjects.h" 25#include "lldb/Host/Terminal.h" 26 27namespace lldb_private { 28 29class ScriptInterpreterPython : 30 public ScriptInterpreter, 31 public IOHandlerDelegateMultiline 32{ 33public: 34 35 friend class IOHandlerPythonInterpreter; 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 lldb_private::Error 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 lldb::ScriptInterpreterObjectSP 102 LoadPluginModule (const FileSpec& file_spec, 103 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 void 247 InitializePrivate (); 248 249 static void 250 InitializeInterpreter (SWIGInitCallback python_swig_init_callback, 251 SWIGBreakpointCallbackFunction swig_breakpoint_callback, 252 SWIGWatchpointCallbackFunction swig_watchpoint_callback, 253 SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, 254 SWIGPythonCreateSyntheticProvider swig_synthetic_script, 255 SWIGPythonCalculateNumChildren swig_calc_children, 256 SWIGPythonGetChildAtIndex swig_get_child_index, 257 SWIGPythonGetIndexOfChildWithName swig_get_index_child, 258 SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , 259 SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, 260 SWIGPythonUpdateSynthProviderInstance swig_update_provider, 261 SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, 262 SWIGPythonCallCommand swig_call_command, 263 SWIGPythonCallModuleInit swig_call_module_init, 264 SWIGPythonCreateOSPlugin swig_create_os_plugin, 265 SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, 266 SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, 267 SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, 268 SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, 269 SWIGPython_GetDynamicSetting swig_plugin_get); 270 271 const char * 272 GetDictionaryName () 273 { 274 return m_dictionary_name.c_str(); 275 } 276 277 278 //---------------------------------------------------------------------- 279 // IOHandlerDelegate 280 //---------------------------------------------------------------------- 281 virtual void 282 IOHandlerActivated (IOHandler &io_handler); 283 284 virtual void 285 IOHandlerInputComplete (IOHandler &io_handler, std::string &data); 286 287protected: 288 289 bool 290 EnterSession (uint16_t on_entry_flags, 291 FILE *in, 292 FILE *out, 293 FILE *err); 294 295 void 296 LeaveSession (); 297 298 void 299 SaveTerminalState (int fd); 300 301 void 302 RestoreTerminalState (); 303 304 class SynchronicityHandler 305 { 306 private: 307 lldb::DebuggerSP m_debugger_sp; 308 ScriptedCommandSynchronicity m_synch_wanted; 309 bool m_old_asynch; 310 public: 311 SynchronicityHandler(lldb::DebuggerSP, 312 ScriptedCommandSynchronicity); 313 ~SynchronicityHandler(); 314 }; 315 316 class ScriptInterpreterPythonObject : public ScriptInterpreterObject 317 { 318 public: 319 ScriptInterpreterPythonObject() : 320 ScriptInterpreterObject() 321 {} 322 323 ScriptInterpreterPythonObject(void* obj) : 324 ScriptInterpreterObject(obj) 325 { 326 Py_XINCREF(m_object); 327 } 328 329 explicit operator bool () 330 { 331 return m_object && m_object != Py_None; 332 } 333 334 335 virtual 336 ~ScriptInterpreterPythonObject() 337 { 338 Py_XDECREF(m_object); 339 m_object = NULL; 340 } 341 private: 342 DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); 343 }; 344public: 345 class Locker : public ScriptInterpreterLocker 346 { 347 public: 348 349 enum OnEntry 350 { 351 AcquireLock = 0x0001, 352 InitSession = 0x0002, 353 InitGlobals = 0x0004, 354 NoSTDIN = 0x0008 355 }; 356 357 enum OnLeave 358 { 359 FreeLock = 0x0001, 360 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor 361 TearDownSession = 0x0004 362 }; 363 364 Locker (ScriptInterpreterPython *py_interpreter = NULL, 365 uint16_t on_entry = AcquireLock | InitSession, 366 uint16_t on_leave = FreeLock | TearDownSession, 367 FILE *in = NULL, 368 FILE *out = NULL, 369 FILE *err = NULL); 370 371 ~Locker (); 372 373 private: 374 375 bool 376 DoAcquireLock (); 377 378 bool 379 DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); 380 381 bool 382 DoFreeLock (); 383 384 bool 385 DoTearDownSession (); 386 387 static void 388 ReleasePythonLock (); 389 390 bool m_teardown_session; 391 ScriptInterpreterPython *m_python_interpreter; 392// FILE* m_tmp_fh; 393 PyGILState_STATE m_GILState; 394 }; 395private: 396 397 enum ActiveIOHandler { 398 eIOHandlerNone, 399 eIOHandlerBreakpoint, 400 eIOHandlerWatchpoint 401 }; 402 PythonObject & 403 GetMainModule (); 404 405 PythonDictionary & 406 GetSessionDictionary (); 407 408 PythonDictionary & 409 GetSysModuleDictionary (); 410 411 bool 412 GetEmbeddedInterpreterModuleObjects (); 413 414 PythonObject m_saved_stdin; 415 PythonObject m_saved_stdout; 416 PythonObject m_saved_stderr; 417 PythonObject m_main_module; 418 PythonObject m_lldb_module; 419 PythonDictionary m_session_dict; 420 PythonDictionary m_sys_module_dict; 421 PythonObject m_run_one_line_function; 422 PythonObject m_run_one_line_str_global; 423 std::string m_dictionary_name; 424 TerminalState m_terminal_state; 425 ActiveIOHandler m_active_io_handler; 426 bool m_session_is_active; 427 bool m_pty_slave_is_open; 428 bool m_valid_session; 429 PyThreadState *m_command_thread_state; 430}; 431} // namespace lldb_private 432 433#endif // #ifdef LLDB_DISABLE_PYTHON 434 435#endif // #ifndef liblldb_ScriptInterpreterPython_h_ 436