diff --git a/lldb/bindings/headers.swig b/lldb/bindings/headers.swig --- a/lldb/bindings/headers.swig +++ b/lldb/bindings/headers.swig @@ -21,6 +21,7 @@ #include "lldb/API/SBData.h" #include "lldb/API/SBDebugger.h" #include "lldb/API/SBDeclaration.h" +#include "lldb/API/SBEnvironment.h" #include "lldb/API/SBError.h" #include "lldb/API/SBEvent.h" #include "lldb/API/SBExecutionContext.h" diff --git a/lldb/bindings/interface/SBEnvironment.i b/lldb/bindings/interface/SBEnvironment.i new file mode 100644 --- /dev/null +++ b/lldb/bindings/interface/SBEnvironment.i @@ -0,0 +1,34 @@ +//===-- SWIG Interface for SBEnvironment-------------------------*- 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 +// +//===----------------------------------------------------------------------===// + +namespace lldb { + +%feature("docstring", +"Represents the environment of a certain process or platform. + +Example: + lldb.debugger.GetSelectedPlatform().GetEnvironment() +") SBEnvironment; +class SBEnvironment { +public: + SBEnvironment (); + + SBEnvironment (const lldb::SBEnvironment &rhs); + + ~SBEnvironment(); + + size_t GetNumEntries(); + + const char *GetEntry(const char *name); + + SBStringList GetEntries(); + + void PutEntry(const char *nameAndValue); +}; + +} // namespace lldb diff --git a/lldb/bindings/interface/SBPlatform.i b/lldb/bindings/interface/SBPlatform.i --- a/lldb/bindings/interface/SBPlatform.i +++ b/lldb/bindings/interface/SBPlatform.i @@ -194,6 +194,9 @@ lldb::SBUnixSignals GetUnixSignals(); + lldb::SBEnvironment + GetEnvironment(); + }; } // namespace lldb diff --git a/lldb/bindings/interface/SBTarget.i b/lldb/bindings/interface/SBTarget.i --- a/lldb/bindings/interface/SBTarget.i +++ b/lldb/bindings/interface/SBTarget.i @@ -677,6 +677,9 @@ lldb::SBBreakpoint BreakpointCreateByAddress (addr_t address); + lldb::SBEnvironment + GetEnvironment(); + lldb::SBBreakpoint BreakpointCreateBySBAddress (SBAddress &sb_address); diff --git a/lldb/bindings/interfaces.swig b/lldb/bindings/interfaces.swig --- a/lldb/bindings/interfaces.swig +++ b/lldb/bindings/interfaces.swig @@ -29,6 +29,7 @@ %include "./interface/SBDebugger.i" %include "./interface/SBDeclaration.i" %include "./interface/SBError.i" +%include "./interface/SBEnvironment.i" %include "./interface/SBEvent.i" %include "./interface/SBExecutionContext.i" %include "./interface/SBExpressionOptions.i" diff --git a/lldb/include/lldb/API/LLDB.h b/lldb/include/lldb/API/LLDB.h --- a/lldb/include/lldb/API/LLDB.h +++ b/lldb/include/lldb/API/LLDB.h @@ -24,6 +24,7 @@ #include "lldb/API/SBDebugger.h" #include "lldb/API/SBDeclaration.h" #include "lldb/API/SBDefines.h" +#include "lldb/API/SBEnvironment.h" #include "lldb/API/SBError.h" #include "lldb/API/SBEvent.h" #include "lldb/API/SBExecutionContext.h" diff --git a/lldb/include/lldb/API/SBDefines.h b/lldb/include/lldb/API/SBDefines.h --- a/lldb/include/lldb/API/SBDefines.h +++ b/lldb/include/lldb/API/SBDefines.h @@ -35,6 +35,7 @@ class LLDB_API SBData; class LLDB_API SBDebugger; class LLDB_API SBDeclaration; +class LLDB_API SBEnvironment; class LLDB_API SBError; class LLDB_API SBEvent; class LLDB_API SBEventList; diff --git a/lldb/include/lldb/API/SBEnvironment.h b/lldb/include/lldb/API/SBEnvironment.h new file mode 100644 --- /dev/null +++ b/lldb/include/lldb/API/SBEnvironment.h @@ -0,0 +1,60 @@ +//===-- SBEnvironment.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_API_SBENVIRONMENT_H +#define LLDB_API_SBENVIRONMENT_H + +#include "lldb/API/SBDefines.h" + +namespace lldb { + +class LLDB_API SBEnvironment { +public: + SBEnvironment(); + + SBEnvironment(const lldb::SBEnvironment &rhs); + + ~SBEnvironment(); + + const lldb::SBEnvironment &operator=(const lldb::SBEnvironment &rhs); + + size_t GetNumEntries(); + + /// Return the value of a given environment variable. + /// + /// \return + /// The value of the enviroment variable or null if not present. + const char *GetEntry(const char *name); + + /// Return the list of enviroment variables with the format + /// VARIABLE=value + /// + /// \return + /// The value of the environment variable or null if not present. + lldb::SBStringList GetEntries(); + + /// Adds an environment variable to this object. The input must be a string + /// with the format + /// VARIABLE=value + /// + /// \return + void PutEntry(const char *nameAndValue); + +protected: + friend class SBPlatform; + friend class SBTarget; + + SBEnvironment(const lldb_private::Environment &rhs); + +private: + std::unique_ptr m_opaque_up; +}; + +} // namespace lldb + +#endif // LLDB_API_SBENVIRONMENT_H diff --git a/lldb/include/lldb/API/SBPlatform.h b/lldb/include/lldb/API/SBPlatform.h --- a/lldb/include/lldb/API/SBPlatform.h +++ b/lldb/include/lldb/API/SBPlatform.h @@ -154,6 +154,14 @@ SBUnixSignals GetUnixSignals() const; + /// Return the environment variables contained in the remote platform + /// connection process. + /// + /// \return + /// An lldb::SBEnvironment object with the platform's environment + /// variables. + SBEnvironment GetEnvironment(); + protected: friend class SBDebugger; friend class SBTarget; diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h --- a/lldb/include/lldb/API/SBTarget.h +++ b/lldb/include/lldb/API/SBTarget.h @@ -94,6 +94,13 @@ /// A platform object. lldb::SBPlatform GetPlatform(); + /// Return the environment variables or the target. + /// + /// \return + /// An lldb::SBEnvironment object with the taget's environment variables. + + SBEnvironment GetEnvironment(); + /// Install any binaries that need to be installed. /// /// This function does nothing when debugging on the host system. diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h --- a/lldb/include/lldb/lldb-forward.h +++ b/lldb/include/lldb/lldb-forward.h @@ -76,6 +76,7 @@ class DynamicLoader; class Editline; class EmulateInstruction; +class Environment; class EvaluateExpressionOptions; class Event; class EventData; diff --git a/lldb/source/API/CMakeLists.txt b/lldb/source/API/CMakeLists.txt --- a/lldb/source/API/CMakeLists.txt +++ b/lldb/source/API/CMakeLists.txt @@ -35,6 +35,7 @@ SBData.cpp SBDebugger.cpp SBDeclaration.cpp + SBEnvironment.cpp SBError.cpp SBEvent.cpp SBExecutionContext.cpp diff --git a/lldb/source/API/SBEnvironment.cpp b/lldb/source/API/SBEnvironment.cpp new file mode 100644 --- /dev/null +++ b/lldb/source/API/SBEnvironment.cpp @@ -0,0 +1,85 @@ +//===-- SBEnvironment.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBEnvironment.h" +#include "SBReproducerPrivate.h" +#include "Utils.h" +#include "lldb/API/SBStringList.h" +#include "lldb/Utility/Environment.h" + +using namespace lldb; +using namespace lldb_private; + +SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) { + LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEnvironment); +} + +SBEnvironment::SBEnvironment(const SBEnvironment &rhs) + : m_opaque_up(new Environment()) { + LLDB_RECORD_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &), rhs); + + m_opaque_up = clone(rhs.m_opaque_up); +} + +SBEnvironment::SBEnvironment(const Environment &rhs) + : m_opaque_up(new Environment(rhs)) {} + +SBEnvironment::~SBEnvironment() = default; + +const SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) { + LLDB_RECORD_METHOD(const lldb::SBEnvironment &, + SBEnvironment, operator=,(const lldb::SBEnvironment &), + rhs); + + if (this != &rhs) + m_opaque_up = clone(rhs.m_opaque_up); + return LLDB_RECORD_RESULT(*this); +} + +size_t SBEnvironment::GetNumEntries() { + LLDB_RECORD_METHOD_NO_ARGS(size_t, SBEnvironment, GetNumEntries); + return LLDB_RECORD_RESULT(m_opaque_up->size()); +} + +const char *SBEnvironment::GetEntry(const char *name) { + LLDB_RECORD_METHOD(const char *, SBEnvironment, GetEntry, (const char *), + name); + return LLDB_RECORD_RESULT(ConstString(m_opaque_up->lookup(name)).AsCString()); +} + +SBStringList SBEnvironment::GetEntries() { + LLDB_RECORD_METHOD_NO_ARGS(SBStringList, SBEnvironment, GetEntries); + auto entries = SBStringList(); + for (const auto &KV : *m_opaque_up) { + entries.AppendString(Environment::compose(KV).c_str()); + } + return LLDB_RECORD_RESULT(entries); +} + +void SBEnvironment::PutEntry(const char *nameAndValue) { + LLDB_RECORD_METHOD(void, SBEnvironment, PutEntry, (const char *), + nameAndValue); + m_opaque_up->insert(nameAndValue); +} + +namespace lldb_private { +namespace repro { + +template <> void RegisterMethods(Registry &R) { + LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, ()); + LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &)); + LLDB_REGISTER_METHOD(const lldb::SBEnvironment &, + SBEnvironment, operator=,(const lldb::SBEnvironment &)); + LLDB_REGISTER_METHOD(size_t, SBEnvironment, GetNumEntries, ()); + LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetEntry, (const char *)); + LLDB_REGISTER_METHOD(lldb::SBStringList, SBEnvironment, GetEntries, ()); + LLDB_REGISTER_METHOD(void, SBEnvironment, PutEntry, (const char *)); +} + +} // namespace repro +} // namespace lldb_private diff --git a/lldb/source/API/SBPlatform.cpp b/lldb/source/API/SBPlatform.cpp --- a/lldb/source/API/SBPlatform.cpp +++ b/lldb/source/API/SBPlatform.cpp @@ -8,9 +8,11 @@ #include "lldb/API/SBPlatform.h" #include "SBReproducerPrivate.h" +#include "lldb/API/SBEnvironment.h" #include "lldb/API/SBError.h" #include "lldb/API/SBFileSpec.h" #include "lldb/API/SBLaunchInfo.h" +#include "lldb/API/SBPlatform.h" #include "lldb/API/SBUnixSignals.h" #include "lldb/Host/File.h" #include "lldb/Target/Platform.h" @@ -649,6 +651,17 @@ return LLDB_RECORD_RESULT(SBUnixSignals()); } +SBEnvironment SBPlatform::GetEnvironment() { + LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBPlatform, GetEnvironment); + PlatformSP platform_sp(GetSP()); + + if (platform_sp) { + return LLDB_RECORD_RESULT(SBEnvironment(platform_sp->GetEnvironment())); + } + + return LLDB_RECORD_RESULT(SBEnvironment()); +} + namespace lldb_private { namespace repro { @@ -740,6 +753,7 @@ (const char *)); LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, SetFilePermissions, (const char *, uint32_t)); + LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBPlatform, GetEnvironment, ()); LLDB_REGISTER_METHOD_CONST(lldb::SBUnixSignals, SBPlatform, GetUnixSignals, ()); } diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp --- a/lldb/source/API/SBTarget.cpp +++ b/lldb/source/API/SBTarget.cpp @@ -13,6 +13,7 @@ #include "lldb/API/SBBreakpoint.h" #include "lldb/API/SBDebugger.h" +#include "lldb/API/SBEnvironment.h" #include "lldb/API/SBEvent.h" #include "lldb/API/SBExpressionOptions.h" #include "lldb/API/SBFileSpec.h" @@ -2388,6 +2389,17 @@ m_opaque_sp->SetProcessLaunchInfo(launch_info.ref()); } +SBEnvironment SBTarget::GetEnvironment() { + LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBTarget, GetEnvironment); + TargetSP target_sp(GetSP()); + + if (target_sp) { + return LLDB_RECORD_RESULT(SBEnvironment(target_sp->GetEnvironment())); + } + + return LLDB_RECORD_RESULT(SBEnvironment()); +} + namespace lldb_private { namespace repro { @@ -2643,6 +2655,7 @@ LLDB_REGISTER_METHOD(lldb::SBInstructionList, SBTarget, GetInstructionsWithFlavor, (lldb::addr_t, const char *, const void *, size_t)); + LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBTarget, GetEnvironment, ()); } } diff --git a/lldb/test/API/python_api/sbenvironment/Makefile b/lldb/test/API/python_api/sbenvironment/Makefile new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/sbenvironment/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py b/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py @@ -0,0 +1,54 @@ +"""Test the SBEnvironment APIs.""" + + + +from math import fabs +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class SBEnvironmentAPICase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + NO_DEBUG_INFO_TESTCASE = True + + + @add_test_categories(['pyapi']) + def test_reading_platform_environment(self): + env = self.dbg.GetSelectedPlatform().GetEnvironment() + # We assume at least PATH is set + self.assertTrue(env.GetEntry("PATH") is not None) + + + @add_test_categories(['pyapi']) + def test_reading_target_environment(self): + env = self.dbg.GetSelectedTarget().GetEnvironment() + # There is no target, so env should be empty + self.assertEqual(len(env.GetEntries()), 0) + + self.build() + self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) + env = self.dbg.GetSelectedTarget().GetEnvironment() + # Now there's a target, so at least PATH should exist + self.assertTrue(env.GetEntry("PATH") is not None) + + @add_test_categories(['pyapi']) + def test_creating_and_modifying_environment(self): + env = lldb.SBEnvironment() + + self.assertEqual(env.GetEntry("FOO"), None) + self.assertEqual(env.GetEntry("BAR"), None) + + env.PutEntry("FOO=bar") + env.PutEntry("BAR=foo") + + self.assertEqual(env.GetEntry("FOO"), "bar") + self.assertEqual(env.GetEntry("BAR"), "foo") + + entries = env.GetEntries() + self.assertEqual(len(entries), 2) + + for entry in env.GetEntries(): + self.assertTrue(entry in ["FOO=bar", "BAR=foo"]) diff --git a/lldb/test/API/python_api/sbenvironment/main.cpp b/lldb/test/API/python_api/sbenvironment/main.cpp new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/sbenvironment/main.cpp @@ -0,0 +1,4 @@ +int main (int argc, char const *argv[]) +{ + return 0; +}