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