[Lldb-commits] [lldb] Add commands frequency to statistics dump (PR #80375)

via lldb-commits lldb-commits at lists.llvm.org
Mon Feb 5 12:27:06 PST 2024


https://github.com/jeffreytan81 updated https://github.com/llvm/llvm-project/pull/80375

>From 59e1499ec0afebb533c4952f079278341b957241 Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Thu, 1 Feb 2024 18:07:51 -0800
Subject: [PATCH 1/6] Add commands frequency to statistics dump

---
 lldb/include/lldb/API/SBCommandInterpreter.h  |  9 ++++---
 lldb/include/lldb/API/SBStructuredData.h      |  1 +
 .../lldb/Interpreter/CommandInterpreter.h     | 15 +++++++++--
 lldb/source/API/SBCommandInterpreter.cpp      | 15 ++++++++++-
 .../source/Commands/CommandObjectCommands.cpp | 10 ++++---
 .../source/Interpreter/CommandInterpreter.cpp |  9 ++++++-
 lldb/source/Interpreter/CommandObject.cpp     |  1 +
 lldb/source/Target/Statistics.cpp             |  4 +++
 .../commands/statistics/basic/TestStats.py    | 24 +++++++++++++++++
 .../stats_api/TestStatisticsAPI.py            | 26 +++++++++++++++++++
 10 files changed, 103 insertions(+), 11 deletions(-)

diff --git a/lldb/include/lldb/API/SBCommandInterpreter.h b/lldb/include/lldb/API/SBCommandInterpreter.h
index b7f5b3bf3396e..b4629a4c2f5a8 100644
--- a/lldb/include/lldb/API/SBCommandInterpreter.h
+++ b/lldb/include/lldb/API/SBCommandInterpreter.h
@@ -13,6 +13,7 @@
 
 #include "lldb/API/SBDebugger.h"
 #include "lldb/API/SBDefines.h"
+#include "lldb/API/SBStructuredData.h"
 
 namespace lldb_private {
 class CommandPluginInterfaceImplementation;
@@ -246,13 +247,13 @@ class SBCommandInterpreter {
                                        lldb::SBStringList &matches,
                                        lldb::SBStringList &descriptions);
 
-  /// Returns whether an interrupt flag was raised either by the SBDebugger - 
+  /// Returns whether an interrupt flag was raised either by the SBDebugger -
   /// when the function is not running on the RunCommandInterpreter thread, or
   /// by SBCommandInterpreter::InterruptCommand if it is.  If your code is doing
-  /// interruptible work, check this API periodically, and interrupt if it 
+  /// interruptible work, check this API periodically, and interrupt if it
   /// returns true.
   bool WasInterrupted() const;
-  
+
   /// Interrupts the command currently executing in the RunCommandInterpreter
   /// thread.
   ///
@@ -315,6 +316,8 @@ class SBCommandInterpreter {
   /// and aliases.  If successful, result->GetOutput has the full expansion.
   void ResolveCommand(const char *command_line, SBCommandReturnObject &result);
 
+  SBStructuredData GetStatistics();
+
 protected:
   friend class lldb_private::CommandPluginInterfaceImplementation;
 
diff --git a/lldb/include/lldb/API/SBStructuredData.h b/lldb/include/lldb/API/SBStructuredData.h
index 35d321eaa7b89..fc6e1ec95c7b8 100644
--- a/lldb/include/lldb/API/SBStructuredData.h
+++ b/lldb/include/lldb/API/SBStructuredData.h
@@ -122,6 +122,7 @@ class SBStructuredData {
   friend class SBTrace;
   friend class lldb_private::python::SWIGBridge;
   friend class lldb_private::lua::SWIGBridge;
+  friend class SBCommandInterpreter;
 
   SBStructuredData(const lldb_private::StructuredDataImpl &impl);
 
diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h
index 747188a15312f..c46cf0409bab6 100644
--- a/lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -28,6 +28,7 @@
 #include <mutex>
 #include <optional>
 #include <stack>
+#include <unordered_map>
 
 namespace lldb_private {
 class CommandInterpreter;
@@ -240,7 +241,7 @@ class CommandInterpreter : public Broadcaster,
     eCommandTypesAllThem = 0xFFFF  //< all commands
   };
 
-  // The CommandAlias and CommandInterpreter both have a hand in 
+  // The CommandAlias and CommandInterpreter both have a hand in
   // substituting for alias commands.  They work by writing special tokens
   // in the template form of the Alias command, and then detecting them when the
   // command is executed.  These are the special tokens:
@@ -575,7 +576,7 @@ class CommandInterpreter : public Broadcaster,
   void SetEchoCommentCommands(bool enable);
 
   bool GetRepeatPreviousCommand() const;
-  
+
   bool GetRequireCommandOverwrite() const;
 
   const CommandObject::CommandMap &GetUserCommands() const {
@@ -641,6 +642,12 @@ class CommandInterpreter : public Broadcaster,
   Status PreprocessCommand(std::string &command);
   Status PreprocessToken(std::string &token);
 
+  void IncreaseCommandUsage(const CommandObject &cmd_obj) {
+    ++m_command_usages[cmd_obj.GetCommandName().str()];
+  }
+
+  llvm::json::Value GetStatistics();
+
 protected:
   friend class Debugger;
 
@@ -754,6 +761,10 @@ class CommandInterpreter : public Broadcaster,
   // If the driver is accepts custom exit codes for the 'quit' command.
   bool m_allow_exit_code = false;
 
+  /// Command usage statistics.
+  typedef std::unordered_map<std::string, uint64_t> CommandUsageMap;
+  CommandUsageMap m_command_usages;
+
   StreamString m_transcript_stream;
 };
 
diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp
index c3cbb00145ed3..a8906686d1a11 100644
--- a/lldb/source/API/SBCommandInterpreter.cpp
+++ b/lldb/source/API/SBCommandInterpreter.cpp
@@ -150,7 +150,7 @@ bool SBCommandInterpreter::WasInterrupted() const {
 
 bool SBCommandInterpreter::InterruptCommand() {
   LLDB_INSTRUMENT_VA(this);
-  
+
   return (IsValid() ? m_opaque_ptr->InterruptCommand() : false);
 }
 
@@ -557,6 +557,19 @@ bool SBCommandInterpreter::SetCommandOverrideCallback(
   return false;
 }
 
+SBStructuredData SBCommandInterpreter::GetStatistics() {
+  LLDB_INSTRUMENT_VA(this);
+
+  SBStructuredData data;
+  if (!IsValid())
+    return data;
+
+  std::string json_str =
+      llvm::formatv("{0:2}", m_opaque_ptr->GetStatistics()).str();
+  data.m_impl_up->SetObjectSP(StructuredData::ParseJSON(json_str));
+  return data;
+}
+
 lldb::SBCommand SBCommandInterpreter::AddMultiwordCommand(const char *name,
                                                           const char *help) {
   LLDB_INSTRUMENT_VA(this, name, help);
diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index 5b9af4a3e1b88..c81f56b6f91bc 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -1123,6 +1123,8 @@ class CommandObjectPythonFunction : public CommandObjectRaw {
                  CommandReturnObject &result) override {
     ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
 
+    m_interpreter.IncreaseCommandUsage(*this);
+
     Status error;
 
     result.SetStatus(eReturnStatusInvalid);
@@ -1577,7 +1579,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
       case eLazyBoolNo:
         m_overwrite = false;
     }
-    
+
     Status path_error;
     m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
         command, true, path_error);
@@ -1631,7 +1633,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
           m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity,
           m_completion_type));
     }
-    
+
     // Assume we're going to succeed...
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
     if (!m_container) {
@@ -1644,7 +1646,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
       llvm::Error llvm_error =
           m_container->LoadUserSubcommand(m_cmd_name, new_cmd_sp, m_overwrite);
       if (llvm_error)
-        result.AppendErrorWithFormat("cannot add command: %s", 
+        result.AppendErrorWithFormat("cannot add command: %s",
                                      llvm::toString(std::move(llvm_error)).c_str());
     }
   }
@@ -1792,7 +1794,7 @@ class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
                                             /* multiword not okay */ false);
     if (llvm_error) {
       result.AppendErrorWithFormat("could not delete command '%s': %s",
-                                   leaf_cmd, 
+                                   leaf_cmd,
                                    llvm::toString(std::move(llvm_error)).c_str());
       return;
     }
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index 00651df48b622..86fbd48888a24 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -3055,7 +3055,7 @@ void CommandInterpreter::PrintCommandOutput(IOHandler &io_handler,
   }
 
   std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex());
-  if (had_output && INTERRUPT_REQUESTED(GetDebugger(), 
+  if (had_output && INTERRUPT_REQUESTED(GetDebugger(),
                                         "Interrupted dumping command output"))
     stream->Printf("\n... Interrupted.\n");
   stream->Flush();
@@ -3547,3 +3547,10 @@ CommandInterpreter::ResolveCommandImpl(std::string &command_line,
 
   return cmd_obj;
 }
+
+llvm::json::Value CommandInterpreter::GetStatistics() {
+  llvm::json::Object stats;
+  for (const auto& command_usage : m_command_usages)
+    stats.try_emplace(command_usage.first, command_usage.second);
+  return stats;
+}
diff --git a/lldb/source/Interpreter/CommandObject.cpp b/lldb/source/Interpreter/CommandObject.cpp
index 1ff9774a0da49..6324c7e701ed5 100644
--- a/lldb/source/Interpreter/CommandObject.cpp
+++ b/lldb/source/Interpreter/CommandObject.cpp
@@ -748,6 +748,7 @@ void CommandObjectParsed::Execute(const char *args_string,
           Cleanup();
           return;
         }
+        m_interpreter.IncreaseCommandUsage(*this);
         DoExecute(cmd_args, result);
       }
     }
diff --git a/lldb/source/Target/Statistics.cpp b/lldb/source/Target/Statistics.cpp
index c739ac7058cae..7cdee3414d8a3 100644
--- a/lldb/source/Target/Statistics.cpp
+++ b/lldb/source/Target/Statistics.cpp
@@ -10,6 +10,7 @@
 
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/Module.h"
+#include "lldb/Interpreter/CommandInterpreter.h"
 #include "lldb/Symbol/SymbolFile.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/Target.h"
@@ -291,10 +292,13 @@ llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger,
       {"strings", const_string_stats.ToJSON()},
   };
 
+  json::Value cmd_stats = debugger.GetCommandInterpreter().GetStatistics();
+
   json::Object global_stats{
       {"targets", std::move(json_targets)},
       {"modules", std::move(json_modules)},
       {"memory", std::move(json_memory)},
+      {"commands", std::move(cmd_stats)},
       {"totalSymbolTableParseTime", symtab_parse_time},
       {"totalSymbolTableIndexTime", symtab_index_time},
       {"totalSymbolTablesLoadedFromCache", symtabs_loaded},
diff --git a/lldb/test/API/commands/statistics/basic/TestStats.py b/lldb/test/API/commands/statistics/basic/TestStats.py
index 6fb0cc6c3be73..4ea5c6a74b317 100644
--- a/lldb/test/API/commands/statistics/basic/TestStats.py
+++ b/lldb/test/API/commands/statistics/basic/TestStats.py
@@ -76,6 +76,11 @@ def get_target_stats(self, debug_stats):
             return debug_stats["targets"][0]
         return None
 
+    def get_command_stats(self, debug_stats):
+        if "commands" in debug_stats:
+            return debug_stats["commands"]
+        return None
+
     def test_expressions_frame_var_counts(self):
         self.build()
         lldbutil.run_to_source_breakpoint(
@@ -355,6 +360,25 @@ def test_modules(self):
         self.assertNotEqual(exe_module, None)
         self.verify_keys(exe_module, 'module dict for "%s"' % (exe), module_keys)
 
+    def test_commands(self):
+        """
+            Test "statistics dump" and the command information.
+        """
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.createTestTarget(file_path=exe)
+
+        interp = self.dbg.GetCommandInterpreter()
+        result = lldb.SBCommandReturnObject()
+        interp.HandleCommand('target list', result)
+        interp.HandleCommand('target list', result)
+
+        debug_stats = self.get_stats()
+
+        command_stats = self.get_command_stats(debug_stats)
+        self.assertNotEqual(command_stats, None)
+        self.assertEqual(command_stats["target list"], 2)
+
     def test_breakpoints(self):
         """Test "statistics dump"
 
diff --git a/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py b/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py
index 33490c9c8f501..b6c972cbcf989 100644
--- a/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py
+++ b/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py
@@ -11,6 +11,9 @@ class TestStatsAPI(TestBase):
     NO_DEBUG_INFO_TESTCASE = True
 
     def test_stats_api(self):
+        """
+            Test SBTarget::GetStatistics() API.
+        """
         self.build()
         exe = self.getBuildArtifact("a.out")
         target = self.dbg.CreateTarget(exe)
@@ -70,3 +73,26 @@ def test_stats_api(self):
             True,
             'Make sure the "failures" key in in "frameVariable" dictionary"',
         )
+
+    def test_command_stats_api(self):
+            """
+                Test GetCommandInterpreter::GetStatistics() API.
+            """
+            self.build()
+            exe = self.getBuildArtifact("a.out")
+            lldbutil.run_to_name_breakpoint(self, 'main')
+
+            interp = self.dbg.GetCommandInterpreter()
+            result = lldb.SBCommandReturnObject()
+            interp.HandleCommand('bt', result)
+
+            stream = lldb.SBStream()
+            res = interp.GetStatistics().GetAsJSON(stream)
+            command_stats = json.loads(stream.GetData())
+
+            # Verify bt command is correctly parsed into final form.
+            self.assertEqual(command_stats["thread backtrace"], 1)
+            # Verify original raw command is not duplicatedly captured.
+            self.assertNotIn('bt', command_stats)
+            # Verify bt's regex command is not duplicatedly captured.
+            self.assertNotIn('_regexp-bt', command_stats)

>From b3c235a73bec8b5cd9d7ee94f56802b9d6334459 Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Thu, 1 Feb 2024 18:23:06 -0800
Subject: [PATCH 2/6] Fix linter

---
 .../source/Commands/CommandObjectCommands.cpp |  8 ++--
 .../source/Interpreter/CommandInterpreter.cpp |  6 +--
 lldb/source/Target/Statistics.cpp             |  3 +-
 .../commands/statistics/basic/TestStats.py    |  6 +--
 .../stats_api/TestStatisticsAPI.py            | 38 +++++++++----------
 5 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index c81f56b6f91bc..5ac9b4b8547d4 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -1625,7 +1625,8 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
           m_options.m_class_name.c_str());
       if (!cmd_obj_sp) {
         result.AppendErrorWithFormatv("cannot create helper object for: "
-                                      "'{0}'", m_options.m_class_name);
+                                      "'{0}'",
+                                      m_options.m_class_name);
         return;
       }
 
@@ -1646,8 +1647,9 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
       llvm::Error llvm_error =
           m_container->LoadUserSubcommand(m_cmd_name, new_cmd_sp, m_overwrite);
       if (llvm_error)
-        result.AppendErrorWithFormat("cannot add command: %s",
-                                     llvm::toString(std::move(llvm_error)).c_str());
+        result.AppendErrorWithFormat(
+            "cannot add command: %s",
+            llvm::toString(std::move(llvm_error)).c_str());
     }
   }
 
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index 86fbd48888a24..2ff8ae7b73cc2 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -3055,8 +3055,8 @@ void CommandInterpreter::PrintCommandOutput(IOHandler &io_handler,
   }
 
   std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex());
-  if (had_output && INTERRUPT_REQUESTED(GetDebugger(),
-                                        "Interrupted dumping command output"))
+  if (had_output &&
+      INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping command output"))
     stream->Printf("\n... Interrupted.\n");
   stream->Flush();
 }
@@ -3550,7 +3550,7 @@ CommandInterpreter::ResolveCommandImpl(std::string &command_line,
 
 llvm::json::Value CommandInterpreter::GetStatistics() {
   llvm::json::Object stats;
-  for (const auto& command_usage : m_command_usages)
+  for (const auto &command_usage : m_command_usages)
     stats.try_emplace(command_usage.first, command_usage.second);
   return stats;
 }
diff --git a/lldb/source/Target/Statistics.cpp b/lldb/source/Target/Statistics.cpp
index 7cdee3414d8a3..4699710035b2d 100644
--- a/lldb/source/Target/Statistics.cpp
+++ b/lldb/source/Target/Statistics.cpp
@@ -311,7 +311,8 @@ llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger,
       {"totalModuleCount", num_modules},
       {"totalModuleCountHasDebugInfo", num_modules_has_debug_info},
       {"totalModuleCountWithVariableErrors", num_modules_with_variable_errors},
-      {"totalModuleCountWithIncompleteTypes", num_modules_with_incomplete_types},
+      {"totalModuleCountWithIncompleteTypes",
+       num_modules_with_incomplete_types},
       {"totalDebugInfoEnabled", num_debug_info_enabled_modules},
       {"totalSymbolTableStripped", num_stripped_modules},
   };
diff --git a/lldb/test/API/commands/statistics/basic/TestStats.py b/lldb/test/API/commands/statistics/basic/TestStats.py
index 4ea5c6a74b317..0172ac536e979 100644
--- a/lldb/test/API/commands/statistics/basic/TestStats.py
+++ b/lldb/test/API/commands/statistics/basic/TestStats.py
@@ -362,7 +362,7 @@ def test_modules(self):
 
     def test_commands(self):
         """
-            Test "statistics dump" and the command information.
+        Test "statistics dump" and the command information.
         """
         self.build()
         exe = self.getBuildArtifact("a.out")
@@ -370,8 +370,8 @@ def test_commands(self):
 
         interp = self.dbg.GetCommandInterpreter()
         result = lldb.SBCommandReturnObject()
-        interp.HandleCommand('target list', result)
-        interp.HandleCommand('target list', result)
+        interp.HandleCommand("target list", result)
+        interp.HandleCommand("target list", result)
 
         debug_stats = self.get_stats()
 
diff --git a/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py b/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py
index b6c972cbcf989..fe55922fe4c31 100644
--- a/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py
+++ b/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py
@@ -12,7 +12,7 @@ class TestStatsAPI(TestBase):
 
     def test_stats_api(self):
         """
-            Test SBTarget::GetStatistics() API.
+        Test SBTarget::GetStatistics() API.
         """
         self.build()
         exe = self.getBuildArtifact("a.out")
@@ -75,24 +75,24 @@ def test_stats_api(self):
         )
 
     def test_command_stats_api(self):
-            """
-                Test GetCommandInterpreter::GetStatistics() API.
-            """
-            self.build()
-            exe = self.getBuildArtifact("a.out")
-            lldbutil.run_to_name_breakpoint(self, 'main')
+        """
+        Test GetCommandInterpreter::GetStatistics() API.
+        """
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        lldbutil.run_to_name_breakpoint(self, "main")
 
-            interp = self.dbg.GetCommandInterpreter()
-            result = lldb.SBCommandReturnObject()
-            interp.HandleCommand('bt', result)
+        interp = self.dbg.GetCommandInterpreter()
+        result = lldb.SBCommandReturnObject()
+        interp.HandleCommand("bt", result)
 
-            stream = lldb.SBStream()
-            res = interp.GetStatistics().GetAsJSON(stream)
-            command_stats = json.loads(stream.GetData())
+        stream = lldb.SBStream()
+        res = interp.GetStatistics().GetAsJSON(stream)
+        command_stats = json.loads(stream.GetData())
 
-            # Verify bt command is correctly parsed into final form.
-            self.assertEqual(command_stats["thread backtrace"], 1)
-            # Verify original raw command is not duplicatedly captured.
-            self.assertNotIn('bt', command_stats)
-            # Verify bt's regex command is not duplicatedly captured.
-            self.assertNotIn('_regexp-bt', command_stats)
+        # Verify bt command is correctly parsed into final form.
+        self.assertEqual(command_stats["thread backtrace"], 1)
+        # Verify original raw command is not duplicatedly captured.
+        self.assertNotIn("bt", command_stats)
+        # Verify bt's regex command is not duplicatedly captured.
+        self.assertNotIn("_regexp-bt", command_stats)

>From cba2ff39fa02a85a2400906930008b6aab9babee Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Thu, 1 Feb 2024 19:08:01 -0800
Subject: [PATCH 3/6] Fix more linter

---
 lldb/source/Commands/CommandObjectCommands.cpp | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index 5ac9b4b8547d4..eb1f0f48a9d3c 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -1792,12 +1792,13 @@ class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
       return;
     }
     const char *leaf_cmd = command[num_args - 1].c_str();
-    llvm::Error llvm_error = container->RemoveUserSubcommand(leaf_cmd,
-                                            /* multiword not okay */ false);
+    llvm::Error llvm_error =
+        container->RemoveUserSubcommand(leaf_cmd,
+                                        /* multiword not okay */ false);
     if (llvm_error) {
-      result.AppendErrorWithFormat("could not delete command '%s': %s",
-                                   leaf_cmd,
-                                   llvm::toString(std::move(llvm_error)).c_str());
+      result.AppendErrorWithFormat(
+          "could not delete command '%s': %s", leaf_cmd,
+          llvm::toString(std::move(llvm_error)).c_str());
       return;
     }
 

>From f1c94b6dd009b75d73d2d18b80ba7461f010fe72 Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Mon, 5 Feb 2024 10:19:42 -0800
Subject: [PATCH 4/6] Revert format and use StringMap

---
 lldb/include/lldb/API/SBCommandInterpreter.h   |  6 +++---
 .../lldb/Interpreter/CommandInterpreter.h      |  4 ++--
 lldb/source/API/SBCommandInterpreter.cpp       |  2 +-
 lldb/source/Commands/CommandObjectCommands.cpp | 18 ++++++++----------
 lldb/source/Interpreter/CommandInterpreter.cpp |  6 +++---
 lldb/source/Target/Statistics.cpp              |  3 +--
 6 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/lldb/include/lldb/API/SBCommandInterpreter.h b/lldb/include/lldb/API/SBCommandInterpreter.h
index b4629a4c2f5a8..ba2e049204b8e 100644
--- a/lldb/include/lldb/API/SBCommandInterpreter.h
+++ b/lldb/include/lldb/API/SBCommandInterpreter.h
@@ -247,13 +247,13 @@ class SBCommandInterpreter {
                                        lldb::SBStringList &matches,
                                        lldb::SBStringList &descriptions);
 
-  /// Returns whether an interrupt flag was raised either by the SBDebugger -
+  /// Returns whether an interrupt flag was raised either by the SBDebugger - 
   /// when the function is not running on the RunCommandInterpreter thread, or
   /// by SBCommandInterpreter::InterruptCommand if it is.  If your code is doing
-  /// interruptible work, check this API periodically, and interrupt if it
+  /// interruptible work, check this API periodically, and interrupt if it 
   /// returns true.
   bool WasInterrupted() const;
-
+  
   /// Interrupts the command currently executing in the RunCommandInterpreter
   /// thread.
   ///
diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h
index c46cf0409bab6..b01bab5fc4487 100644
--- a/lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -643,7 +643,7 @@ class CommandInterpreter : public Broadcaster,
   Status PreprocessToken(std::string &token);
 
   void IncreaseCommandUsage(const CommandObject &cmd_obj) {
-    ++m_command_usages[cmd_obj.GetCommandName().str()];
+    ++m_command_usages[cmd_obj.GetCommandName()];
   }
 
   llvm::json::Value GetStatistics();
@@ -762,7 +762,7 @@ class CommandInterpreter : public Broadcaster,
   bool m_allow_exit_code = false;
 
   /// Command usage statistics.
-  typedef std::unordered_map<std::string, uint64_t> CommandUsageMap;
+  typedef llvm::StringMap<uint64_t> CommandUsageMap;
   CommandUsageMap m_command_usages;
 
   StreamString m_transcript_stream;
diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp
index a8906686d1a11..7b87dc507e4be 100644
--- a/lldb/source/API/SBCommandInterpreter.cpp
+++ b/lldb/source/API/SBCommandInterpreter.cpp
@@ -150,7 +150,7 @@ bool SBCommandInterpreter::WasInterrupted() const {
 
 bool SBCommandInterpreter::InterruptCommand() {
   LLDB_INSTRUMENT_VA(this);
-
+  
   return (IsValid() ? m_opaque_ptr->InterruptCommand() : false);
 }
 
diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index eb1f0f48a9d3c..d1e20702bbac1 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -1579,7 +1579,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
       case eLazyBoolNo:
         m_overwrite = false;
     }
-
+    
     Status path_error;
     m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
         command, true, path_error);
@@ -1625,8 +1625,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
           m_options.m_class_name.c_str());
       if (!cmd_obj_sp) {
         result.AppendErrorWithFormatv("cannot create helper object for: "
-                                      "'{0}'",
-                                      m_options.m_class_name);
+                                      "'{0}'", m_options.m_class_name);
         return;
       }
 
@@ -1634,7 +1633,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
           m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity,
           m_completion_type));
     }
-
+    
     // Assume we're going to succeed...
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
     if (!m_container) {
@@ -1647,9 +1646,8 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
       llvm::Error llvm_error =
           m_container->LoadUserSubcommand(m_cmd_name, new_cmd_sp, m_overwrite);
       if (llvm_error)
-        result.AppendErrorWithFormat(
-            "cannot add command: %s",
-            llvm::toString(std::move(llvm_error)).c_str());
+        result.AppendErrorWithFormat("cannot add command: %s",
+                                     llvm::toString(std::move(llvm_error)).c_str());
     }
   }
 
@@ -1796,9 +1794,9 @@ class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
         container->RemoveUserSubcommand(leaf_cmd,
                                         /* multiword not okay */ false);
     if (llvm_error) {
-      result.AppendErrorWithFormat(
-          "could not delete command '%s': %s", leaf_cmd,
-          llvm::toString(std::move(llvm_error)).c_str());
+      result.AppendErrorWithFormat("could not delete command '%s': %s",
+                                   leaf_cmd,
+                                   llvm::toString(std::move(llvm_error)).c_str());
       return;
     }
 
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index 2ff8ae7b73cc2..508f517c61708 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -3055,8 +3055,8 @@ void CommandInterpreter::PrintCommandOutput(IOHandler &io_handler,
   }
 
   std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex());
-  if (had_output &&
-      INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping command output"))
+  if (had_output && INTERRUPT_REQUESTED(GetDebugger(),
+                    "Interrupted dumping command output"))
     stream->Printf("\n... Interrupted.\n");
   stream->Flush();
 }
@@ -3551,6 +3551,6 @@ CommandInterpreter::ResolveCommandImpl(std::string &command_line,
 llvm::json::Value CommandInterpreter::GetStatistics() {
   llvm::json::Object stats;
   for (const auto &command_usage : m_command_usages)
-    stats.try_emplace(command_usage.first, command_usage.second);
+    stats.try_emplace(command_usage.getKey(), command_usage.getValue());
   return stats;
 }
diff --git a/lldb/source/Target/Statistics.cpp b/lldb/source/Target/Statistics.cpp
index 4699710035b2d..7cdee3414d8a3 100644
--- a/lldb/source/Target/Statistics.cpp
+++ b/lldb/source/Target/Statistics.cpp
@@ -311,8 +311,7 @@ llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger,
       {"totalModuleCount", num_modules},
       {"totalModuleCountHasDebugInfo", num_modules_has_debug_info},
       {"totalModuleCountWithVariableErrors", num_modules_with_variable_errors},
-      {"totalModuleCountWithIncompleteTypes",
-       num_modules_with_incomplete_types},
+      {"totalModuleCountWithIncompleteTypes", num_modules_with_incomplete_types},
       {"totalDebugInfoEnabled", num_debug_info_enabled_modules},
       {"totalSymbolTableStripped", num_stripped_modules},
   };

>From 388e769244a330b3c1c67613c4c57dbd8d92af88 Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Mon, 5 Feb 2024 11:51:56 -0800
Subject: [PATCH 5/6] Make formatter happy again

---
 lldb/source/Commands/CommandObjectCommands.cpp | 11 ++++++-----
 lldb/source/Interpreter/CommandInterpreter.cpp |  4 ++--
 lldb/source/Target/Statistics.cpp              |  3 ++-
 3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index d1e20702bbac1..a51e5ab1af30c 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -1646,8 +1646,9 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
       llvm::Error llvm_error =
           m_container->LoadUserSubcommand(m_cmd_name, new_cmd_sp, m_overwrite);
       if (llvm_error)
-        result.AppendErrorWithFormat("cannot add command: %s",
-                                     llvm::toString(std::move(llvm_error)).c_str());
+        result.AppendErrorWithFormat(
+            "cannot add command: %s",
+            llvm::toString(std::move(llvm_error)).c_str());
     }
   }
 
@@ -1794,9 +1795,9 @@ class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
         container->RemoveUserSubcommand(leaf_cmd,
                                         /* multiword not okay */ false);
     if (llvm_error) {
-      result.AppendErrorWithFormat("could not delete command '%s': %s",
-                                   leaf_cmd,
-                                   llvm::toString(std::move(llvm_error)).c_str());
+      result.AppendErrorWithFormat(
+          "could not delete command '%s': %s", leaf_cmd,
+          llvm::toString(std::move(llvm_error)).c_str());
       return;
     }
 
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index 508f517c61708..8c3972a2ba4ce 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -3055,8 +3055,8 @@ void CommandInterpreter::PrintCommandOutput(IOHandler &io_handler,
   }
 
   std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex());
-  if (had_output && INTERRUPT_REQUESTED(GetDebugger(),
-                    "Interrupted dumping command output"))
+  if (had_output &&
+      INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping command output"))
     stream->Printf("\n... Interrupted.\n");
   stream->Flush();
 }
diff --git a/lldb/source/Target/Statistics.cpp b/lldb/source/Target/Statistics.cpp
index 7cdee3414d8a3..4699710035b2d 100644
--- a/lldb/source/Target/Statistics.cpp
+++ b/lldb/source/Target/Statistics.cpp
@@ -311,7 +311,8 @@ llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger,
       {"totalModuleCount", num_modules},
       {"totalModuleCountHasDebugInfo", num_modules_has_debug_info},
       {"totalModuleCountWithVariableErrors", num_modules_with_variable_errors},
-      {"totalModuleCountWithIncompleteTypes", num_modules_with_incomplete_types},
+      {"totalModuleCountWithIncompleteTypes",
+       num_modules_with_incomplete_types},
       {"totalDebugInfoEnabled", num_debug_info_enabled_modules},
       {"totalSymbolTableStripped", num_stripped_modules},
   };

>From 3615f46ddbbec5fbe44998c747b44cdd88aecf85 Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Mon, 5 Feb 2024 12:26:45 -0800
Subject: [PATCH 6/6] Revert unnecessary format change in one more file

---
 lldb/include/lldb/Interpreter/CommandInterpreter.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h
index b01bab5fc4487..d190bcdcab449 100644
--- a/lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -241,7 +241,7 @@ class CommandInterpreter : public Broadcaster,
     eCommandTypesAllThem = 0xFFFF  //< all commands
   };
 
-  // The CommandAlias and CommandInterpreter both have a hand in
+  // The CommandAlias and CommandInterpreter both have a hand in 
   // substituting for alias commands.  They work by writing special tokens
   // in the template form of the Alias command, and then detecting them when the
   // command is executed.  These are the special tokens:
@@ -576,7 +576,7 @@ class CommandInterpreter : public Broadcaster,
   void SetEchoCommentCommands(bool enable);
 
   bool GetRepeatPreviousCommand() const;
-
+  
   bool GetRequireCommandOverwrite() const;
 
   const CommandObject::CommandMap &GetUserCommands() const {



More information about the lldb-commits mailing list