[Lldb-commits] [lldb] fe1874d - [lldb/Interpreter] Add setting to set session transcript save directory

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Tue Jun 29 01:54:47 PDT 2021


Author: Med Ismail Bennani
Date: 2021-06-29T10:54:29+02:00
New Revision: fe1874dd2dd99c9811db515a2957e2a42f9f6868

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

LOG: [lldb/Interpreter] Add setting to set session transcript save directory

This patch introduces a new interpreter setting
`interpreter.save-session-directory` so the user can specify a directory
where the session transcripts will be saved.

If not set, the session transcript are saved on a temporary file.

rdar://72902842

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

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    

Modified: 
    lldb/include/lldb/Interpreter/CommandInterpreter.h
    lldb/source/Interpreter/CommandInterpreter.cpp
    lldb/source/Interpreter/InterpreterProperties.td
    lldb/test/API/commands/session/save/TestSessionSave.py

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h
index a8475ca610463..6430773de1b64 100644
--- a/lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -493,6 +493,9 @@ class CommandInterpreter : public Broadcaster,
   bool GetSaveSessionOnQuit() const;
   void SetSaveSessionOnQuit(bool enable);
 
+  FileSpec GetSaveSessionDirectory() const;
+  void SetSaveSessionDirectory(llvm::StringRef path);
+
   bool GetEchoCommands() const;
   void SetEchoCommands(bool enable);
 

diff  --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index 2e07ff5703ff2..68e8edfc90583 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -160,6 +160,16 @@ void CommandInterpreter::SetSaveSessionOnQuit(bool enable) {
   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, enable);
 }
 
+FileSpec CommandInterpreter::GetSaveSessionDirectory() const {
+  const uint32_t idx = ePropertySaveSessionDirectory;
+  return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
+}
+
+void CommandInterpreter::SetSaveSessionDirectory(llvm::StringRef path) {
+  const uint32_t idx = ePropertySaveSessionDirectory;
+  m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
+}
+
 bool CommandInterpreter::GetEchoCommands() const {
   const uint32_t idx = ePropertyEchoCommands;
   return m_collection_sp->GetPropertyAtIndexAsBoolean(
@@ -2925,9 +2935,15 @@ bool CommandInterpreter::SaveTranscript(
     std::string now = llvm::to_string(std::chrono::system_clock::now());
     std::replace(now.begin(), now.end(), ' ', '_');
     const std::string file_name = "lldb_session_" + now + ".log";
-    FileSpec tmp = HostInfo::GetGlobalTempDir();
-    tmp.AppendPathComponent(file_name);
-    output_file = tmp.GetPath();
+
+    FileSpec save_location = GetSaveSessionDirectory();
+
+    if (!save_location)
+      save_location = HostInfo::GetGlobalTempDir();
+
+    FileSystem::Instance().Resolve(save_location);
+    save_location.AppendPathComponent(file_name);
+    output_file = save_location.GetPath();
   }
 
   auto error_out = [&](llvm::StringRef error_message, std::string description) {

diff  --git a/lldb/source/Interpreter/InterpreterProperties.td b/lldb/source/Interpreter/InterpreterProperties.td
index 1148c1b01def5..1c6f0206c489d 100644
--- a/lldb/source/Interpreter/InterpreterProperties.td
+++ b/lldb/source/Interpreter/InterpreterProperties.td
@@ -13,6 +13,9 @@ let Definition = "interpreter" in {
     Global,
     DefaultFalse,
     Desc<"If true, LLDB will save the session's transcripts before quitting.">;
+  def SaveSessionDirectory: Property<"save-session-directory", "FileSpec">,
+    DefaultStringValue<"">,
+    Desc<"A path where LLDB will save the session's transcripts. This is particularly useful when you can't set the session file, for example when using `save-session-on-quit`.">;
   def StopCmdSourceOnError: Property<"stop-command-source-on-error", "Boolean">,
     Global,
     DefaultTrue,

diff  --git a/lldb/test/API/commands/session/save/TestSessionSave.py b/lldb/test/API/commands/session/save/TestSessionSave.py
index ec244a42efcac..e144ed19d1c50 100644
--- a/lldb/test/API/commands/session/save/TestSessionSave.py
+++ b/lldb/test/API/commands/session/save/TestSessionSave.py
@@ -1,7 +1,7 @@
 """
 Test the session save feature
 """
-
+import os
 import lldb
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
@@ -72,3 +72,20 @@ def test_session_save(self):
           lines = raw.splitlines()[:-1]
           for line in lines:
             self.assertIn(line, content)
+
+        td = tempfile.TemporaryDirectory()
+        res = lldb.SBCommandReturnObject()
+        interpreter.HandleCommand('settings set interpreter.save-session-directory ' + td.name, res)
+        self.assertTrue(res.Succeeded())
+
+        res = lldb.SBCommandReturnObject()
+        interpreter.HandleCommand('session save', res)
+        self.assertTrue(res.Succeeded())
+        raw += self.raw_transcript_builder(cmd, res)
+
+        with open(os.path.join(td.name, os.listdir(td.name)[0]), "r") as file:
+          content = file.read()
+          # Exclude last line, since session won't record it's own output
+          lines = raw.splitlines()[:-1]
+          for line in lines:
+            self.assertIn(line, content)


        


More information about the lldb-commits mailing list