Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3c6b82c4 authored by Ben Chan's avatar Ben Chan Committed by chrome-internal-fetch
Browse files

crash-reporter: Fix C++ style issues in KernelCollector.

BUG=None
TEST=`FEATURES=test emerge-$BOARD crash-reporter`

Change-Id: Ib33a23059878380425f6eb79385dc67141ad0f77
Reviewed-on: https://chromium-review.googlesource.com/209746


Reviewed-by: default avatarAlex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Ben Chan <benchan@chromium.org>
Tested-by: default avatarBen Chan <benchan@chromium.org>
parent 120c675d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ static int Initialize(KernelCollector *kernel_collector,
  bool was_kernel_crash = false;
  bool was_unclean_shutdown = false;
  kernel_collector->Enable();
  if (kernel_collector->IsEnabled()) {
  if (kernel_collector->is_enabled()) {
    was_kernel_crash = kernel_collector->Collect();
  }

+44 −46
Original line number Diff line number Diff line
@@ -11,23 +11,27 @@
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>

static const char kDefaultKernelStackSignature[] =
    "kernel-UnspecifiedStackSignature";
static const char kDumpPath[] = "/dev/pstore";
static const char kDumpFormat[] = "dmesg-ramoops-%zu";
static const char kKernelExecName[] = "kernel";
using base::FilePath;
using base::StringPrintf;

namespace {

const char kDefaultKernelStackSignature[] = "kernel-UnspecifiedStackSignature";
const char kDumpPath[] = "/dev/pstore";
const char kDumpFormat[] = "dmesg-ramoops-%zu";
const char kKernelExecName[] = "kernel";
// Maximum number of records to examine in the kDumpPath.
static const size_t kMaxDumpRecords = 100;
const size_t kMaxDumpRecords = 100;
const pid_t kKernelPid = 0;
static const char kKernelSignatureKey[] = "sig";
const char kKernelSignatureKey[] = "sig";
// Byte length of maximum human readable portion of a kernel crash signature.
static const int kMaxHumanStringLength = 40;
const int kMaxHumanStringLength = 40;
const uid_t kRootUid = 0;
// Time in seconds from the final kernel log message for a call stack
// to count towards the signature of the kcrash.
static const int kSignatureTimestampWindow = 2;
const int kSignatureTimestampWindow = 2;
// Kernel log timestamp regular expression.
static const std::string kTimestampRegex("^<.*>\\[\\s*(\\d+\\.\\d+)\\]");
const char kTimestampRegex[] = "^<.*>\\[\\s*(\\d+\\.\\d+)\\]";

//
// These regular expressions enable to us capture the PC in a backtrace.
@@ -42,7 +46,7 @@ static const std::string kTimestampRegex("^<.*>\\[\\s*(\\d+\\.\\d+)\\]");
//   "<0>[   37.474699] EIP: [<790ed488>] write_breakme+0x80/0x108
//    SS:ESP 0068:e9dd3efc"
//
static const char *s_pc_regex[] = {
const char* const kPCRegex[] = {
  0,
  " PC is at ([^\\+ ]+).*",
  " epc\\s+:\\s+\\S+\\s+([^\\+ ]+).*",  // MIPS has an exception program counter
@@ -50,18 +54,17 @@ static const char *s_pc_regex[] = {
  " RIP  \\[<.*>\\] ([^\\+ ]+).*",  // X86_64 uses RIP for the program counter
};

using base::FilePath;
using base::StringPrintf;

COMPILE_ASSERT(arraysize(s_pc_regex) == KernelCollector::archCount,
COMPILE_ASSERT(arraysize(kPCRegex) == KernelCollector::kArchCount,
               missing_arch_pc_regexp);

}  // namespace

KernelCollector::KernelCollector()
    : is_enabled_(false),
      ramoops_dump_path_(kDumpPath),
      records_(0) {
  // We expect crash dumps in the format of the architecture we are built for.
  arch_ = GetCompilerArch();
      records_(0),
      // We expect crash dumps in the format of architecture we are built for.
      arch_(GetCompilerArch()) {
}

KernelCollector::~KernelCollector() {
@@ -80,7 +83,8 @@ bool KernelCollector::ReadRecordToString(std::string *contents,

  // Ramoops appends a header to a crash which contains ==== followed by a
  // timestamp. Ignore the header.
  pcrecpp::RE record_re("====\\d+\\.\\d+\n(.*)",
  pcrecpp::RE record_re(
      "====\\d+\\.\\d+\n(.*)",
      pcrecpp::RE_Options().set_multiline(true).set_dotall(true));

  FilePath ramoops_record;
@@ -232,18 +236,17 @@ void KernelCollector::StripSensitiveData(std::string *kernel_dump) {
}

bool KernelCollector::Enable() {
  if (arch_ == archUnknown || arch_ >= archCount ||
      s_pc_regex[arch_] == NULL) {
  if (arch_ == kArchUnknown || arch_ >= kArchCount || kPCRegex[arch_] == NULL) {
    LOG(WARNING) << "KernelCollector does not understand this architecture";
    return false;
  } else {
  }

  FilePath ramoops_record;
  GetRamoopsRecordPath(&ramoops_record, 0);
  if (!base::PathExists(ramoops_record)) {
    LOG(WARNING) << "Kernel does not support crash dumping";
    return false;
  }
  }

  // To enable crashes, we will eventually need to set
  // the chnv bit in BIOS, but it does not yet work.
@@ -270,7 +273,7 @@ void KernelCollector::ProcessStackTrace(
    float *last_stack_timestamp,
    bool *is_watchdog_crash) {
  pcrecpp::RE line_re("(.+)", pcrecpp::MULTILINE());
  pcrecpp::RE stack_trace_start_re(kTimestampRegex +
  pcrecpp::RE stack_trace_start_re(std::string(kTimestampRegex) +
        " (Call Trace|Backtrace):$");

  // Match lines such as the following and grab out "function_name".
@@ -286,7 +289,7 @@ void KernelCollector::ProcessStackTrace(
  // For X86:
  // <4>[ 6066.849504]  [<7937bcee>] ? function_name+0x66/0x6c
  //
  pcrecpp::RE stack_entry_re(kTimestampRegex +
  pcrecpp::RE stack_entry_re(std::string(kTimestampRegex) +
    "\\s+\\[<[[:xdigit:]]+>\\]"      // Matches "  [<7937bcee>]"
    "([\\s\\?(]+)"                   // Matches " ? (" (ARM) or " ? " (X86)
    "([^\\+ )]+)");                  // Matches until delimiter reached
@@ -356,24 +359,21 @@ void KernelCollector::ProcessStackTrace(
  }
}

enum KernelCollector::ArchKind KernelCollector::GetCompilerArch(void) {
// static
KernelCollector::ArchKind KernelCollector::GetCompilerArch() {
#if defined(COMPILER_GCC) && defined(ARCH_CPU_ARM_FAMILY)
  return archArm;
  return kArchArm;
#elif defined(COMPILER_GCC) && defined(ARCH_CPU_MIPS_FAMILY)
  return archMips;
  return kArchMips;
#elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_64)
  return archX86_64;
  return kArchX86_64;
#elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_FAMILY)
  return archX86;
  return kArchX86;
#else
  return archUnknown;
  return kArchUnknown;
#endif
}

void KernelCollector::SetArch(enum ArchKind arch) {
  arch_ = arch;
}

bool KernelCollector::FindCrashingFunction(
  pcrecpp::StringPiece kernel_dump,
  bool print_diagnostics,
@@ -382,7 +382,7 @@ bool KernelCollector::FindCrashingFunction(
  float timestamp = 0;

  // Use the correct regex for this architecture.
  pcrecpp::RE eip_re(kTimestampRegex + s_pc_regex[arch_],
  pcrecpp::RE eip_re(std::string(kTimestampRegex) + kPCRegex[arch_],
                     pcrecpp::MULTILINE());

  while (eip_re.FindAndConsume(&kernel_dump, &timestamp, crashing_function)) {
@@ -417,7 +417,7 @@ bool KernelCollector::FindPanicMessage(pcrecpp::StringPiece kernel_dump,
                                       std::string *panic_message) {
  // Match lines such as the following and grab out "Fatal exception"
  // <0>[  342.841135] Kernel panic - not syncing: Fatal exception
  pcrecpp::RE kernel_panic_re(kTimestampRegex +
  pcrecpp::RE kernel_panic_re(std::string(kTimestampRegex) +
                              " Kernel panic[^\\:]*\\:\\s*(.*)",
                              pcrecpp::MULTILINE());
  float timestamp = 0;
@@ -524,9 +524,7 @@ bool KernelCollector::Collect() {
    }

    std::string dump_basename =
        FormatDumpBasename(kKernelExecName,
                           time(NULL),
                           kKernelPid);
        FormatDumpBasename(kKernelExecName, time(NULL), kKernelPid);
    FilePath kernel_crash_path = root_crash_directory.Append(
        StringPrintf("%s.kcrash", dump_basename.c_str()));

+14 −14
Original line number Diff line number Diff line
@@ -19,13 +19,13 @@ class KernelCollector : public CrashCollector {
 public:
  // Enumeration to specify architecture type.
  enum ArchKind {
    archUnknown,
    archArm,
    archMips,
    archX86,
    archX86_64,
    kArchUnknown,
    kArchArm,
    kArchMips,
    kArchX86,
    kArchX86_64,

    archCount  // Number of architectures.
    kArchCount  // Number of architectures.
  };

  KernelCollector();
@@ -38,9 +38,7 @@ class KernelCollector : public CrashCollector {
  bool Enable();

  // Returns true if the kernel collection currently enabled.
  bool IsEnabled() {
    return is_enabled_;
  }
  bool is_enabled() const { return is_enabled_; }

  // Collect any preserved kernel crash dump. Returns true if there was
  // a dump (even if there were problems storing the dump), false otherwise.
@@ -52,8 +50,8 @@ class KernelCollector : public CrashCollector {
                                   bool print_diagnostics);

  // Set the architecture of the crash dumps we are looking at.
  void SetArch(enum ArchKind arch);
  enum ArchKind GetArch() { return arch_; }
  void set_arch(ArchKind arch) { arch_ = arch; }
  ArchKind arch() const { return arch_; }

 private:
  friend class KernelCollectorTest;
@@ -94,15 +92,17 @@ class KernelCollector : public CrashCollector {
                        bool print_diagnostics,
                        std::string *panic_message);

  // Returns the architecture kind for which we are built - enum ArchKind.
  enum ArchKind GetCompilerArch(void);
  // Returns the architecture kind for which we are built.
  static ArchKind GetCompilerArch();

  bool is_enabled_;
  base::FilePath ramoops_dump_path_;
  size_t records_;

  // The architecture of kernel dump strings we are working with.
  enum ArchKind arch_;
  ArchKind arch_;

  DISALLOW_COPY_AND_ASSIGN(KernelCollector);
};

#endif  // CRASH_REPORTER_KERNEL_COLLECTOR_H_
+6 −6
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ class KernelCollectorTest : public ::testing::Test {

TEST_F(KernelCollectorTest, ComputeKernelStackSignatureBase) {
  // Make sure the normal build architecture is detected
  EXPECT_TRUE(collector_.GetArch() != KernelCollector::archUnknown);
  EXPECT_NE(KernelCollector::kArchUnknown, collector_.arch());
}

TEST_F(KernelCollectorTest, LoadPreservedDump) {
@@ -91,7 +91,7 @@ TEST_F(KernelCollectorTest, LoadPreservedDump) {

TEST_F(KernelCollectorTest, EnableMissingKernel) {
  ASSERT_FALSE(collector_.Enable());
  ASSERT_FALSE(collector_.IsEnabled());
  ASSERT_FALSE(collector_.is_enabled());
  ASSERT_TRUE(FindLog(
      "Kernel does not support crash dumping"));
  ASSERT_EQ(s_crashes, 0);
@@ -100,7 +100,7 @@ TEST_F(KernelCollectorTest, EnableMissingKernel) {
TEST_F(KernelCollectorTest, EnableOK) {
  WriteStringToFile(kcrash_file(), "");
  ASSERT_TRUE(collector_.Enable());
  ASSERT_TRUE(collector_.IsEnabled());
  ASSERT_TRUE(collector_.is_enabled());
  ASSERT_TRUE(FindLog("Enabling kernel crash handling"));
  ASSERT_EQ(s_crashes, 0);
}
@@ -359,7 +359,7 @@ TEST_F(KernelCollectorTest, ComputeKernelStackSignatureARM) {
          "[<c017bfe0>] (proc_reg_write+0x88/0x9c)\n";
  std::string signature;

  collector_.SetArch(KernelCollector::archArm);
  collector_.set_arch(KernelCollector::kArchArm);
  EXPECT_TRUE(
      collector_.ComputeKernelStackSignature(kBugToPanic, &signature, false));
  EXPECT_EQ("kernel-write_breakme-97D3E92F", signature);
@@ -418,7 +418,7 @@ TEST_F(KernelCollectorTest, ComputeKernelStackSignatureMIPS) {
      "<5>[ 3378.696000] ---[ end trace 75067432f24bbc93 ]---\n";
  std::string signature;

  collector_.SetArch(KernelCollector::archMips);
  collector_.set_arch(KernelCollector::kArchMips);
  EXPECT_TRUE(
      collector_.ComputeKernelStackSignature(kBugToPanic, &signature, false));
  EXPECT_EQ("kernel-lkdtm_do_action-5E600A6B", signature);
@@ -443,7 +443,7 @@ TEST_F(KernelCollectorTest, ComputeKernelStackSignatureX86) {
      "<4>[ 6066.950208]  [<7901b260>] no_context+0x10d/0x117\n";
  std::string signature;

  collector_.SetArch(KernelCollector::archX86);
  collector_.set_arch(KernelCollector::kArchX86);
  EXPECT_TRUE(
      collector_.ComputeKernelStackSignature(kBugToPanic, &signature, false));
  EXPECT_EQ("kernel-ieee80211_stop_tx_ba_session-DE253569", signature);