
It is surprisingly difficult to write a simple python script that can reliably `import lldb` without failing, or crashing. I'm currently resorting to convolutions like this: def find_lldb(may_reexec=False): if prefix := os.environ.get('LLDB_PYTHON_PREFIX'): if os.path.realpath(prefix) != os.path.realpath(sys.prefix): raise Exception("cannot import lldb.\n" f" sys.prefix should be: {prefix}\n" f" but it is: {sys.prefix}") else: line1, line2 = subprocess.run( ['lldb', '-x', '-b', '-o', 'script print(sys.prefix)'], encoding='utf8', stdout=subprocess.PIPE, check=True).stdout.strip().splitlines() assert line1.strip() == '(lldb) script print(sys.prefix)' prefix = line2.strip() os.environ['LLDB_PYTHON_PREFIX'] = prefix if sys.prefix != prefix: if not may_reexec: raise Exception( "cannot import lldb.\n" + f" This python, at {sys.prefix}\n" f" does not math LLDB's python at {prefix}") os.environ['LLDB_PYTHON_PREFIX'] = prefix python_exe = os.path.join(prefix, 'bin', 'python3') os.execl(python_exe, python_exe, *sys.argv) lldb_path = subprocess.run(['lldb', '-P'], check=True, stdout=subprocess.PIPE, encoding='utf8').stdout.strip() sys.path = [lldb_path] + sys.path This patch aims to replace all that with: #!/usr/bin/env lldb-python import lldb ... ... by adding the following features: * new command line option: --print-script-interpreter-info. This prints language-specific information about the script interpreter in JSON format. * new tool (unix only): lldb-python which finds python and exec's it. Reviewed By: JDevlieghere Differential Revision: https://reviews.llvm.org/D112973
65 lines
2.3 KiB
C++
65 lines
2.3 KiB
C++
//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H
|
|
#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H
|
|
|
|
#include "lldb/Host/Config.h"
|
|
|
|
#if LLDB_ENABLE_PYTHON
|
|
|
|
#include "lldb/Breakpoint/BreakpointOptions.h"
|
|
#include "lldb/Core/IOHandler.h"
|
|
#include "lldb/Core/StructuredDataImpl.h"
|
|
#include "lldb/Interpreter/ScriptInterpreter.h"
|
|
#include "lldb/lldb-private.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace lldb_private {
|
|
/// Abstract interface for the Python script interpreter.
|
|
class ScriptInterpreterPython : public ScriptInterpreter,
|
|
public IOHandlerDelegateMultiline {
|
|
public:
|
|
class CommandDataPython : public BreakpointOptions::CommandData {
|
|
public:
|
|
CommandDataPython() : BreakpointOptions::CommandData() {
|
|
interpreter = lldb::eScriptLanguagePython;
|
|
}
|
|
CommandDataPython(StructuredData::ObjectSP extra_args_sp) :
|
|
BreakpointOptions::CommandData(),
|
|
m_extra_args_up(new StructuredDataImpl()) {
|
|
interpreter = lldb::eScriptLanguagePython;
|
|
m_extra_args_up->SetObjectSP(extra_args_sp);
|
|
}
|
|
lldb::StructuredDataImplUP m_extra_args_up;
|
|
};
|
|
|
|
ScriptInterpreterPython(Debugger &debugger)
|
|
: ScriptInterpreter(debugger, lldb::eScriptLanguagePython),
|
|
IOHandlerDelegateMultiline("DONE") {}
|
|
|
|
StructuredData::DictionarySP GetInterpreterInfo() override;
|
|
static void Initialize();
|
|
static void Terminate();
|
|
static llvm::StringRef GetPluginNameStatic() { return "script-python"; }
|
|
static llvm::StringRef GetPluginDescriptionStatic();
|
|
static FileSpec GetPythonDir();
|
|
static void SharedLibraryDirectoryHelper(FileSpec &this_file);
|
|
|
|
protected:
|
|
static void ComputePythonDirForApple(llvm::SmallVectorImpl<char> &path);
|
|
static void ComputePythonDir(llvm::SmallVectorImpl<char> &path);
|
|
};
|
|
} // namespace lldb_private
|
|
|
|
#endif // LLDB_ENABLE_PYTHON
|
|
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H
|