1 //===-- CommandObjectStats.cpp ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "CommandObjectStats.h" 11 #include "lldb/Host/Host.h" 12 #include "lldb/Interpreter/CommandInterpreter.h" 13 #include "lldb/Interpreter/CommandReturnObject.h" 14 #include "lldb/Target/Target.h" 15 16 using namespace lldb; 17 using namespace lldb_private; 18 19 class CommandObjectStatsEnable : public CommandObjectParsed { 20 public: 21 CommandObjectStatsEnable(CommandInterpreter &interpreter) 22 : CommandObjectParsed(interpreter, "enable", 23 "Enable statistics collection", nullptr, 24 eCommandProcessMustBePaused) {} 25 26 ~CommandObjectStatsEnable() override = default; 27 28 protected: 29 bool DoExecute(Args &command, CommandReturnObject &result) override { 30 Target *target = GetSelectedOrDummyTarget(); 31 32 if (target->GetCollectingStats()) { 33 result.AppendError("statistics already enabled"); 34 result.SetStatus(eReturnStatusFailed); 35 return false; 36 } 37 38 target->SetCollectingStats(true); 39 result.SetStatus(eReturnStatusSuccessFinishResult); 40 return true; 41 } 42 }; 43 44 class CommandObjectStatsDisable : public CommandObjectParsed { 45 public: 46 CommandObjectStatsDisable(CommandInterpreter &interpreter) 47 : CommandObjectParsed(interpreter, "disable", 48 "Disable statistics collection", nullptr, 49 eCommandProcessMustBePaused) {} 50 51 ~CommandObjectStatsDisable() override = default; 52 53 protected: 54 bool DoExecute(Args &command, CommandReturnObject &result) override { 55 Target *target = GetSelectedOrDummyTarget(); 56 57 if (!target->GetCollectingStats()) { 58 result.AppendError("need to enable statistics before disabling them"); 59 result.SetStatus(eReturnStatusFailed); 60 return false; 61 } 62 63 target->SetCollectingStats(false); 64 result.SetStatus(eReturnStatusSuccessFinishResult); 65 return true; 66 } 67 }; 68 69 class CommandObjectStatsDump : public CommandObjectParsed { 70 public: 71 CommandObjectStatsDump(CommandInterpreter &interpreter) 72 : CommandObjectParsed(interpreter, "dump", "Dump statistics results", 73 nullptr, eCommandProcessMustBePaused) {} 74 75 ~CommandObjectStatsDump() override = default; 76 77 protected: 78 bool DoExecute(Args &command, CommandReturnObject &result) override { 79 Target *target = GetSelectedOrDummyTarget(); 80 81 uint32_t i = 0; 82 for (auto &stat : target->GetStatistics()) { 83 result.AppendMessageWithFormat( 84 "%s : %u\n", 85 lldb_private::GetStatDescription(static_cast<lldb_private::StatisticKind>(i)) 86 .c_str(), 87 stat); 88 i += 1; 89 } 90 result.SetStatus(eReturnStatusSuccessFinishResult); 91 return true; 92 } 93 }; 94 95 CommandObjectStats::CommandObjectStats(CommandInterpreter &interpreter) 96 : CommandObjectMultiword(interpreter, "statistics", 97 "Print statistics about a debugging session", 98 "statistics <subcommand> [<subcommand-options>]") { 99 LoadSubCommand("enable", 100 CommandObjectSP(new CommandObjectStatsEnable(interpreter))); 101 LoadSubCommand("disable", 102 CommandObjectSP(new CommandObjectStatsDisable(interpreter))); 103 LoadSubCommand("dump", 104 CommandObjectSP(new CommandObjectStatsDump(interpreter))); 105 } 106 107 CommandObjectStats::~CommandObjectStats() = default; 108