[Lldb-commits] [lldb] 8d33437 - [LLDB/API] Expose args and env from SBProcessInfo.

Bruce Mitchener via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 4 23:43:17 PDT 2021


Author: Bruce Mitchener
Date: 2021-06-05T13:42:18+07:00
New Revision: 8d33437d030af27fff21dd3fd0e66893b0148217

URL: https://github.com/llvm/llvm-project/commit/8d33437d030af27fff21dd3fd0e66893b0148217
DIFF: https://github.com/llvm/llvm-project/commit/8d33437d030af27fff21dd3fd0e66893b0148217.diff

LOG: [LLDB/API] Expose args and env from SBProcessInfo.

This is another step towards implementing the equivalent of
`platform process list` and related functionality.

`uint32_t` is used for the argument count and index despite the
underlying value being `size_t` to be consistent with other
index-based access to arguments.

Differential Revision: https://reviews.llvm.org/D103675

Added: 
    

Modified: 
    lldb/bindings/interface/SBProcessInfo.i
    lldb/include/lldb/API/SBEnvironment.h
    lldb/include/lldb/API/SBProcessInfo.h
    lldb/source/API/SBProcessInfo.cpp
    lldb/test/API/python_api/process/TestProcessAPI.py

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBProcessInfo.i b/lldb/bindings/interface/SBProcessInfo.i
index 17b2761a344e..361975a571ad 100644
--- a/lldb/bindings/interface/SBProcessInfo.i
+++ b/lldb/bindings/interface/SBProcessInfo.i
@@ -68,6 +68,25 @@ public:
     ) GetTriple;
     const char *
     GetTriple ();
+
+    %feature("docstring",
+    "Return the number of arguments given to the described process."
+    ) GetNumArguments;
+    uint32_t
+    GetNumArguments ();
+
+    %feature("autodoc", "
+    GetArgumentAtIndex(int index) -> string
+    Return the specified argument given to the described process."
+    ) GetArgumentAtIndex;
+    const char *
+    GetArgumentAtIndex (uint32_t index);
+
+    %feature("docstring",
+    "Return the environment variables for the described process."
+    ) GetEnvironment;
+    SBEnvironment
+    GetEnvironment ();
 };
 
 } // namespace lldb

diff  --git a/lldb/include/lldb/API/SBEnvironment.h b/lldb/include/lldb/API/SBEnvironment.h
index f40ee01a42ab..fcf41684d0be 100644
--- a/lldb/include/lldb/API/SBEnvironment.h
+++ b/lldb/include/lldb/API/SBEnvironment.h
@@ -122,6 +122,7 @@ class LLDB_API SBEnvironment {
 protected:
   friend class SBPlatform;
   friend class SBTarget;
+  friend class SBProcessInfo;
   friend class SBLaunchInfo;
 
   SBEnvironment(lldb_private::Environment rhs);

diff  --git a/lldb/include/lldb/API/SBProcessInfo.h b/lldb/include/lldb/API/SBProcessInfo.h
index 36fae9e842a6..ae5e6072aa74 100644
--- a/lldb/include/lldb/API/SBProcessInfo.h
+++ b/lldb/include/lldb/API/SBProcessInfo.h
@@ -53,6 +53,19 @@ class LLDB_API SBProcessInfo {
   /// Return the target triple (arch-vendor-os) for the described process.
   const char *GetTriple();
 
+  // Return the number of arguments given to the described process.
+  uint32_t GetNumArguments();
+
+  // Return the specified argument given to the described process.
+  const char *GetArgumentAtIndex(uint32_t index);
+
+  /// Return the environment variables for the described process.
+  ///
+  /// \return
+  ///     An lldb::SBEnvironment object which is a copy of the process
+  ///     environment.
+  SBEnvironment GetEnvironment();
+
 private:
   friend class SBProcess;
 

diff  --git a/lldb/source/API/SBProcessInfo.cpp b/lldb/source/API/SBProcessInfo.cpp
index cba3bdc179f3..6f6381367398 100644
--- a/lldb/source/API/SBProcessInfo.cpp
+++ b/lldb/source/API/SBProcessInfo.cpp
@@ -9,6 +9,7 @@
 #include "lldb/API/SBProcessInfo.h"
 #include "SBReproducerPrivate.h"
 #include "Utils.h"
+#include "lldb/API/SBEnvironment.h"
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/Utility/ProcessInfo.h"
 
@@ -194,6 +195,38 @@ const char *SBProcessInfo::GetTriple() {
   return triple;
 }
 
+uint32_t SBProcessInfo::GetNumArguments() {
+  LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBProcessInfo, GetNumArguments);
+
+  uint32_t num = 0;
+  if (m_opaque_up) {
+    num = m_opaque_up->GetArguments().size();
+  }
+  return num;
+}
+
+const char *SBProcessInfo::GetArgumentAtIndex(uint32_t index) {
+  LLDB_RECORD_METHOD(const char *, SBProcessInfo, GetArgumentAtIndex,
+                     (uint32_t), index);
+
+  const char *argument = nullptr;
+  if (m_opaque_up) {
+    argument = m_opaque_up->GetArguments().GetArgumentAtIndex(index);
+  }
+  return argument;
+}
+
+SBEnvironment SBProcessInfo::GetEnvironment() {
+  LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBProcessInfo,
+                             GetEnvironment);
+
+  if (m_opaque_up) {
+    return LLDB_RECORD_RESULT(SBEnvironment(m_opaque_up->GetEnvironment()));
+  }
+
+  return LLDB_RECORD_RESULT(SBEnvironment());
+}
+
 namespace lldb_private {
 namespace repro {
 
@@ -220,6 +253,10 @@ void RegisterMethods<SBProcessInfo>(Registry &R) {
   LLDB_REGISTER_METHOD(bool, SBProcessInfo, EffectiveGroupIDIsValid, ());
   LLDB_REGISTER_METHOD(lldb::pid_t, SBProcessInfo, GetParentProcessID, ());
   LLDB_REGISTER_METHOD(const char *, SBProcessInfo, GetTriple, ());
+  LLDB_REGISTER_METHOD(uint32_t, SBProcessInfo, GetNumArguments, ());
+  LLDB_REGISTER_METHOD(const char *, SBProcessInfo, GetArgumentAtIndex,
+                       (uint32_t));
+  LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBProcessInfo, GetEnvironment, ());
 }
 
 }

diff  --git a/lldb/test/API/python_api/process/TestProcessAPI.py b/lldb/test/API/python_api/process/TestProcessAPI.py
index b7efc7e4affa..5df9eb9d5edb 100644
--- a/lldb/test/API/python_api/process/TestProcessAPI.py
+++ b/lldb/test/API/python_api/process/TestProcessAPI.py
@@ -335,6 +335,8 @@ def test_get_process_info(self):
         # Launch the process and stop at the entry point.
         launch_info = target.GetLaunchInfo()
         launch_info.SetWorkingDirectory(self.get_process_working_directory())
+        launch_info.SetEnvironmentEntries(["FOO=BAR"], False)
+        launch_info.SetArguments(["--abc"], False)
         launch_flags = launch_info.GetLaunchFlags()
         launch_flags |= lldb.eLaunchFlagStopAtEntry
         launch_info.SetLaunchFlags(launch_flags)
@@ -358,6 +360,11 @@ def test_get_process_info(self):
             "Process ID is valid")
         triple = process_info.GetTriple()
         self.assertIsNotNone(triple, "Process has a triple")
+        env = process_info.GetEnvironment()
+        self.assertGreater(env.GetNumValues(), 0)
+        self.assertEqual("BAR", env.Get("FOO"))
+        self.assertEqual(process_info.GetNumArguments(), 1)
+        self.assertEqual("--abc", process_info.GetArgumentAtIndex(0))
 
         # Additional process info varies by platform, so just check that
         # whatever info was retrieved is consistent and nothing blows up.


        


More information about the lldb-commits mailing list