LLDB mainline
ScriptInterpreterPythonImpl.h
Go to the documentation of this file.
1//===-- ScriptInterpreterPythonImpl.h ---------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
10#define LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
11
12#include "lldb-python.h"
13
14#include "PythonDataObjects.h"
16
17#include "lldb/Host/Terminal.h"
19
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22
23namespace lldb_private {
26public:
28
30
32
33 bool Interrupt() override;
34
35 bool ExecuteOneLine(
36 llvm::StringRef command, CommandReturnObject *result,
37 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
38
39 void ExecuteInterpreterLoop() override;
40
42 llvm::StringRef in_string,
43 ScriptInterpreter::ScriptReturnType return_type, void *ret_value,
44 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
45
47 const char *in_string,
48 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
49
50 Status
52
53 bool GenerateTypeScriptFunction(StringList &input, std::string &output,
54 const void *name_token = nullptr) override;
55
56 bool GenerateTypeSynthClass(StringList &input, std::string &output,
57 const void *name_token = nullptr) override;
58
59 bool GenerateTypeSynthClass(const char *oneliner, std::string &output,
60 const void *name_token = nullptr) override;
61
62 // use this if the function code is just a one-liner script
63 bool GenerateTypeScriptFunction(const char *oneliner, std::string &output,
64 const void *name_token = nullptr) override;
65
67 std::string &output) override;
68
70 CreateSyntheticScriptedProvider(const char *class_name,
71 lldb::ValueObjectSP valobj) override;
72
74 CreateScriptCommandObject(const char *class_name) override;
75
78
80 CreateFrameRecognizer(const char *class_name) override;
81
84 lldb::StackFrameSP frame_sp) override;
85
86 bool ShouldHide(const StructuredData::ObjectSP &implementor,
87 lldb::StackFrameSP frame_sp) override;
88
90
92
95
97
99
102
105
107
109 LoadPluginModule(const FileSpec &file_spec,
110 lldb_private::Status &error) override;
111
113 GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
114 const char *setting_name,
115 lldb_private::Status &error) override;
116
117 size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor,
118 uint32_t max) override;
119
121 GetChildAtIndex(const StructuredData::ObjectSP &implementor,
122 uint32_t idx) override;
123
124 llvm::Expected<uint32_t>
126 const char *child_name) override;
127
129 const StructuredData::ObjectSP &implementor) override;
130
132 const StructuredData::ObjectSP &implementor) override;
133
135 GetSyntheticValue(const StructuredData::ObjectSP &implementor) override;
136
138 GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override;
139
140 bool
141 RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
142 ScriptedCommandSynchronicity synchronicity,
144 Status &error,
145 const lldb_private::ExecutionContext &exe_ctx) override;
146
148 StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
149 ScriptedCommandSynchronicity synchronicity,
151 const lldb_private::ExecutionContext &exe_ctx) override;
152
154 StructuredData::GenericSP impl_obj_sp, Args &args,
155 ScriptedCommandSynchronicity synchronicity,
157 const lldb_private::ExecutionContext &exe_ctx) override;
158
159 std::optional<std::string>
161 Args &args) override;
162
164 StructuredData::GenericSP impl_obj_sp, std::vector<llvm::StringRef> &args,
165 size_t args_pos, size_t char_in_arg) override;
166
168 StructuredData::GenericSP impl_obj_sp, llvm::StringRef &long_options,
169 size_t char_in_arg) override;
170
171 Status GenerateFunction(const char *signature, const StringList &input,
172 bool is_callback) override;
173
175 std::string &output,
176 bool has_extra_args,
177 bool is_callback) override;
178
180 std::string &output,
181 bool is_callback) override;
182
183 bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj,
184 StructuredData::ObjectSP &callee_wrapper_sp,
185 const TypeSummaryOptions &options,
186 std::string &retval) override;
187
188 bool FormatterCallbackFunction(const char *function_name,
189 lldb::TypeImplSP type_impl_sp) override;
190
191 bool GetDocumentationForItem(const char *item, std::string &dest) override;
192
194 std::string &dest) override;
195
196 uint32_t
198
200 std::string &dest) override;
201
204
207
209 ExecutionContext *exe_ctx,
210 llvm::StringRef long_option,
211 llvm::StringRef value) override;
212
214 StructuredData::GenericSP cmd_obj_sp) override;
215
216 bool CheckObjectExists(const char *name) override {
217 if (!name || !name[0])
218 return false;
219 std::string temp;
220 return GetDocumentationForItem(name, temp);
221 }
222
223 bool RunScriptFormatKeyword(const char *impl_function, Process *process,
224 std::string &output, Status &error) override;
225
226 bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
227 std::string &output, Status &error) override;
228
229 bool RunScriptFormatKeyword(const char *impl_function, Target *target,
230 std::string &output, Status &error) override;
231
232 bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame,
233 std::string &output, Status &error) override;
234
235 bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value,
236 std::string &output, Status &error) override;
237
238 bool LoadScriptingModule(const char *filename,
239 const LoadScriptOptions &options,
241 StructuredData::ObjectSP *module_sp = nullptr,
242 FileSpec extra_search_dir = {},
243 lldb::TargetSP loaded_into_target_sp = {}) override;
244
245 bool IsReservedWord(const char *word) override;
246
247 std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override;
248
250 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
251 CommandReturnObject &result) override;
252
253 void
254 CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
255 CommandReturnObject &result) override;
256
257 /// Set the callback body text into the callback for the breakpoint.
258 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
259 const char *callback_body,
260 bool is_callback) override;
261
263 BreakpointOptions &bp_options, const char *function_name,
264 StructuredData::ObjectSP extra_args_sp) override;
265
266 /// This one is for deserialization:
268 BreakpointOptions &bp_options,
269 std::unique_ptr<BreakpointOptions::CommandData> &data_up) override;
270
271 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
272 const char *command_body_text,
273 StructuredData::ObjectSP extra_args_sp,
274 bool uses_extra_args,
275 bool is_callback);
276
277 /// Set a one-liner as the callback for the watchpoint.
278 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
279 const char *user_input,
280 bool is_callback) override;
281
282 const char *GetDictionaryName() { return m_dictionary_name.c_str(); }
283
284 PyThreadState *GetThreadState() { return m_command_thread_state; }
285
286 void SetThreadState(PyThreadState *s) {
287 if (s)
289 }
290
291 // IOHandlerDelegate
292 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
293
294 void IOHandlerInputComplete(IOHandler &io_handler,
295 std::string &data) override;
296
298
299 // PluginInterface protocol
300 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
301
303 public:
304 enum OnEntry {
305 AcquireLock = 0x0001,
306 InitSession = 0x0002,
307 InitGlobals = 0x0004,
308 NoSTDIN = 0x0008
309 };
310
311 enum OnLeave {
312 FreeLock = 0x0001,
313 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it
314 // when calling constructor
316 };
317
318 Locker(ScriptInterpreterPythonImpl *py_interpreter,
319 uint16_t on_entry = AcquireLock | InitSession,
320 uint16_t on_leave = FreeLock | TearDownSession,
321 lldb::FileSP in = nullptr, lldb::FileSP out = nullptr,
322 lldb::FileSP err = nullptr);
323
324 ~Locker() override;
325
326 private:
327 bool DoAcquireLock();
328
329 bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in,
330 lldb::FileSP out, lldb::FileSP err);
331
332 bool DoFreeLock();
333
334 bool DoTearDownSession();
335
338 PyGILState_STATE m_GILState;
339 };
340
341 static bool BreakpointCallbackFunction(void *baton,
343 lldb::user_id_t break_id,
344 lldb::user_id_t break_loc_id);
345 static bool WatchpointCallbackFunction(void *baton,
347 lldb::user_id_t watch_id);
348 static void Initialize();
349
361
362 enum class AddLocation { Beginning, End };
363
364 static void AddToSysPath(AddLocation location, std::string path);
365
366 bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out,
367 lldb::FileSP err);
368
369 void LeaveSession();
370
371 uint32_t IsExecutingPython() {
372 std::lock_guard<std::mutex> guard(m_mutex);
373 return m_lock_count > 0;
374 }
375
377 std::lock_guard<std::mutex> guard(m_mutex);
378 return ++m_lock_count;
379 }
380
382 std::lock_guard<std::mutex> guard(m_mutex);
383 if (m_lock_count > 0)
384 --m_lock_count;
385 return m_lock_count;
386 }
387
393
395
397
399
400 llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable(
401 const llvm::StringRef &callable_name) override;
402
404
405 bool SetStdHandle(lldb::FileSP file, const char *py_name,
406 python::PythonObject &save_file, const char *mode);
407
416 std::string m_dictionary_name;
421 uint32_t m_lock_count;
422 std::mutex m_mutex;
424};
425
427public:
432
433 ~IOHandlerPythonInterpreter() override = default;
434
435 llvm::StringRef GetControlSequence(char ch) override {
436 static constexpr llvm::StringLiteral control_sequence("quit()\n");
437 if (ch == 'd')
438 return control_sequence;
439 return {};
440 }
441
442 void Run() override {
443 if (m_python) {
444 int stdin_fd = GetInputFD();
445 if (stdin_fd >= 0) {
446 Terminal terminal(stdin_fd);
447 TerminalState terminal_state(terminal);
448
449 if (terminal.IsATerminal()) {
450 // FIXME: error handling?
451 llvm::consumeError(terminal.SetCanonical(false));
452 llvm::consumeError(terminal.SetEcho(true));
453 }
454
456 m_python,
462
463 // The following call drops into the embedded interpreter loop and
464 // stays there until the user chooses to exit from the Python
465 // interpreter. This embedded interpreter will, as any Python code that
466 // performs I/O, unlock the GIL before a system call that can hang, and
467 // lock it when the syscall has returned.
468
469 // We need to surround the call to the embedded interpreter with calls
470 // to PyGILState_Ensure and PyGILState_Release (using the Locker
471 // above). This is because Python has a global lock which must be held
472 // whenever we want to touch any Python objects. Otherwise, if the user
473 // calls Python code, the interpreter state will be off, and things
474 // could hang (it's happened before).
475
476 StreamString run_string;
477 run_string.Printf("run_python_interpreter (%s)",
478 m_python->GetDictionaryName());
479 python::RunSimpleString(run_string.GetData());
480 }
481 }
482 SetIsDone(true);
483 }
484
485 void Cancel() override {}
486
487 bool Interrupt() override { return m_python->Interrupt(); }
488
489 void GotEOF() override {}
490
491protected:
493};
494
495} // namespace lldb_private
496
497#endif // LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
static llvm::raw_ostream & error(Stream &strm)
ScriptInterpreterPythonImpl::Locker Locker
A command line argument class.
Definition Args.h:33
A uniqued constant string class.
Definition ConstString.h:40
A class to manage flag bits.
Definition Debugger.h:87
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
~IOHandlerPythonInterpreter() override=default
llvm::StringRef GetControlSequence(char ch) override
IOHandlerPythonInterpreter(Debugger &debugger, ScriptInterpreterPythonImpl *python)
IOHandler(Debugger &debugger, IOHandler::Type type)
Definition IOHandler.cpp:55
void SetIsDone(bool b)
Definition IOHandler.h:81
A plug-in interface definition class for debugging a process.
Definition Process.h:354
bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, lldb::FileSP err)
Locker(ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry=AcquireLock|InitSession, uint16_t on_leave=FreeLock|TearDownSession, lldb::FileSP in=nullptr, lldb::FileSP out=nullptr, lldb::FileSP err=nullptr)
SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity)
bool GenerateTypeScriptFunction(StringList &input, std::string &output, const void *name_token=nullptr) override
Status GenerateFunction(const char *signature, const StringList &input, bool is_callback) override
bool GenerateScriptAliasFunction(StringList &input, std::string &output) override
lldb_private::Status ExecuteMultipleLines(const char *in_string, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
bool ShouldHide(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp) override
bool GenerateWatchpointCommandCallbackData(StringList &input, std::string &output, bool is_callback) override
StructuredData::DictionarySP HandleOptionArgumentCompletionForScriptedCommand(StructuredData::GenericSP impl_obj_sp, llvm::StringRef &long_options, size_t char_in_arg) override
bool RunScriptBasedParsedCommand(StructuredData::GenericSP impl_obj_sp, Args &args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx) override
void OptionParsingStartedForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string &dest) override
lldb::ScriptedStopHookInterfaceSP CreateScriptedStopHookInterface() override
Status SetBreakpointCommandCallbackFunction(BreakpointOptions &bp_options, const char *function_name, StructuredData::ObjectSP extra_args_sp) override
Set a script function as the callback for the breakpoint.
lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface() override
static bool BreakpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
StructuredData::DictionarySP GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, const char *setting_name, lldb_private::Status &error) override
void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result) override
StructuredData::DictionarySP HandleArgumentCompletionForScriptedCommand(StructuredData::GenericSP impl_obj_sp, std::vector< llvm::StringRef > &args, size_t args_pos, size_t char_in_arg) override
bool RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx) override
lldb::ScriptedFrameProviderInterfaceSP CreateScriptedFrameProviderInterface() override
std::optional< std::string > GetRepeatCommandForScriptedCommand(StructuredData::GenericSP impl_obj_sp, Args &args) override
Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, const char *callback_body, bool is_callback) override
Set the callback body text into the callback for the breakpoint.
StructuredData::ObjectSP GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, lldb::FileSP err)
void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *user_input, bool is_callback) override
Set a one-liner as the callback for the watchpoint.
lldb::ValueObjectSP GetSyntheticValue(const StructuredData::ObjectSP &implementor) override
std::unique_ptr< ScriptInterpreterLocker > AcquireInterpreterLock() override
void CollectDataForBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, CommandReturnObject &result) override
bool UpdateSynthProviderInstance(const StructuredData::ObjectSP &implementor) override
static void AddToSysPath(AddLocation location, std::string path)
bool MightHaveChildrenSynthProviderInstance(const StructuredData::ObjectSP &implementor) override
bool LoadScriptingModule(const char *filename, const LoadScriptOptions &options, lldb_private::Status &error, StructuredData::ObjectSP *module_sp=nullptr, FileSpec extra_search_dir={}, lldb::TargetSP loaded_into_target_sp={}) override
StructuredData::GenericSP CreateFrameRecognizer(const char *class_name) override
ConstString GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override
Status GenerateBreakpointCommandCallbackData(StringList &input, std::string &output, bool has_extra_args, bool is_callback) override
lldb::OperatingSystemInterfaceSP CreateOperatingSystemInterface() override
bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp, ExecutionContext *exe_ctx, llvm::StringRef long_option, llvm::StringRef value) override
llvm::Expected< uint32_t > GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, const char *child_name) override
bool FormatterCallbackFunction(const char *function_name, lldb::TypeImplSP type_impl_sp) override
Status ExportFunctionDefinitionToInterpreter(StringList &function_def) override
StructuredData::ObjectSP CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj) override
bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
bool GetDocumentationForItem(const char *item, std::string &dest) override
In Python, a special attribute doc contains the docstring for an object (function,...
uint32_t GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override
Called when a line or lines have been retrieved.
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string &retval) override
lldb::ScriptedFrameInterfaceSP CreateScriptedFrameInterface() override
lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface() override
StructuredData::GenericSP CreateScriptCommandObject(const char *class_name) override
size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, uint32_t max) override
bool ExecuteOneLineWithReturn(llvm::StringRef in_string, ScriptInterpreter::ScriptReturnType return_type, void *ret_value, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
lldb::ScriptedBreakpointInterfaceSP CreateScriptedBreakpointInterface() override
bool RunScriptFormatKeyword(const char *impl_function, Process *process, std::string &output, Status &error) override
bool SetStdHandle(lldb::FileSP file, const char *py_name, python::PythonObject &save_file, const char *mode)
lldb::ValueObjectSP GetChildAtIndex(const StructuredData::ObjectSP &implementor, uint32_t idx) override
bool GenerateTypeSynthClass(StringList &input, std::string &output, const void *name_token=nullptr) override
StructuredData::ObjectSP CreateStructuredDataFromScriptObject(ScriptObject obj) override
StructuredData::ObjectSP GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
StructuredData::ObjectSP LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error) override
bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string &dest) override
llvm::Expected< unsigned > GetMaxPositionalArgumentsForCallable(const llvm::StringRef &callable_name) override
static bool WatchpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger)
lldb::ValueObjectListSP GetRecognizedArguments(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp) override
lldb::ScriptedThreadPlanInterfaceSP CreateScriptedThreadPlanInterface() override
This base class provides an interface to stack frames.
Definition StackFrame.h:44
An error handling class.
Definition Status.h:118
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
const char * GetData() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
A RAII-friendly terminal state saving/restoring class.
Definition Terminal.h:99
llvm::Error SetEcho(bool enabled)
Definition Terminal.cpp:76
llvm::Error SetCanonical(bool enabled)
Definition Terminal.cpp:92
bool IsATerminal() const
Definition Terminal.cpp:31
int RunSimpleString(const char *str)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ScriptedStopHookInterface > ScriptedStopHookInterfaceSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ScriptInterpreter > ScriptInterpreterSP
std::shared_ptr< lldb_private::ScriptedThreadPlanInterface > ScriptedThreadPlanInterfaceSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::OperatingSystemInterface > OperatingSystemInterfaceSP
std::shared_ptr< lldb_private::ScriptedBreakpointInterface > ScriptedBreakpointInterfaceSP
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
std::shared_ptr< lldb_private::ValueObjectList > ValueObjectListSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::ScriptedFrameProviderInterface > ScriptedFrameProviderInterfaceSP
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::TypeImpl > TypeImplSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::File > FileSP
std::unique_ptr< lldb_private::ScriptedProcessInterface > ScriptedProcessInterfaceUP
std::shared_ptr< lldb_private::ScriptedFrameInterface > ScriptedFrameInterfaceSP