[lldb/Reproducers] Add test-specific API to set the test CWD
authorJonas Devlieghere <jonas@devlieghere.com>
Wed, 13 May 2020 15:54:57 +0000 (08:54 -0700)
committerJonas Devlieghere <jonas@devlieghere.com>
Wed, 13 May 2020 16:00:07 +0000 (09:00 -0700)
The reproducers' working directory is set to the current working
directory when they are initialized. While this is not optimal, as the
cwd can change during a debug session, it has been sufficient so far.

The current approach doesn't work for the API test suite however because
dotest temporarily changes the directory to where the test's Python file
lives.

This patch adds an API to tell the reproducers what to set the CWD to.
This is a NO-OP in every mode but capture.

Differential revision: https://reviews.llvm.org/D79825

lldb/bindings/interface/SBReproducer.i
lldb/include/lldb/API/SBReproducer.h
lldb/include/lldb/Utility/Reproducer.h
lldb/packages/Python/lldbsuite/test/lldbtest.py
lldb/source/API/SBReproducer.cpp

index e976863..7c9b007 100644 (file)
@@ -13,5 +13,6 @@ class SBReproducer
         static const char *Capture(const char *path);
         static const char *PassiveReplay(const char *path);
         static bool SetAutoGenerate(bool b);
+        static void SetWorkingDirectory(const char *path);
 };
 }
index 5a298d1..78044e9 100644 (file)
@@ -26,6 +26,13 @@ public:
   static const char *GetPath();
   static bool SetAutoGenerate(bool b);
   static bool Generate();
+
+  /// The working directory is set to the current working directory when the
+  /// reproducers are initialized. This method allows setting a different
+  /// working directory. This is used by the API test suite  which temporarily
+  /// changes the directory to where the test lives. This is a NO-OP in every
+  /// mode but capture.
+  static void SetWorkingDirectory(const char *path);
 };
 
 } // namespace lldb
index 499243b..5d81046 100644 (file)
@@ -147,6 +147,9 @@ public:
       return;
     m_cwd = std::string(cwd.str());
   }
+
+  void Update(llvm::StringRef path) { m_cwd = std::string(path); }
+
   struct Info {
     static const char *name;
     static const char *file;
index 3cea39d..9d119e0 100644 (file)
@@ -526,6 +526,7 @@ class Base(unittest2.TestCase):
             if traceAlways:
                 print("Change dir to:", full_dir, file=sys.stderr)
             os.chdir(full_dir)
+            lldb.SBReproducer.SetWorkingDirectory(full_dir)
 
         # Set platform context.
         cls.platformContext = lldbplatformutil.createPlatformContext()
index 329c1b5..703d613 100644 (file)
@@ -231,6 +231,12 @@ const char *SBReproducer::GetPath() {
   return path.c_str();
 }
 
+void SBReproducer::SetWorkingDirectory(const char *path) {
+  if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
+    g->GetOrCreate<WorkingDirectoryProvider>().Update(path);
+  }
+}
+
 char lldb_private::repro::SBProvider::ID = 0;
 const char *SBProvider::Info::name = "sbapi";
 const char *SBProvider::Info::file = "sbapi.bin";