crash_logging.cc 5.83 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "butil/debug/crash_logging.h"
gejun's avatar
gejun committed
6 7 8 9

#include <cmath>
#include <map>

10 11 12 13 14
#include "butil/debug/stack_trace.h"
#include "butil/format_macros.h"
#include "butil/logging.h"
#include "butil/strings/string_util.h"
#include "butil/strings/stringprintf.h"
gejun's avatar
gejun committed
15

16
namespace butil {
gejun's avatar
gejun committed
17 18 19 20 21
namespace debug {

namespace {

// Global map of crash key names to registration entries.
22
typedef std::map<butil::StringPiece, CrashKey> CrashKeyMap;
gejun's avatar
gejun committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
CrashKeyMap* g_crash_keys_ = NULL;

// The maximum length of a single chunk.
size_t g_chunk_max_length_ = 0;

// String used to format chunked key names.
const char kChunkFormatString[] = "%s-%" PRIuS;

// The functions that are called to actually set the key-value pairs in the
// crash reportng system.
SetCrashKeyValueFuncT g_set_key_func_ = NULL;
ClearCrashKeyValueFuncT g_clear_key_func_ = NULL;

// For a given |length|, computes the number of chunks a value of that size
// will occupy.
size_t NumChunksForLength(size_t length) {
    return (size_t)std::ceil(length / static_cast<double>(g_chunk_max_length_));
}

// The longest max_length allowed by the system.
const size_t kLargestValueAllowed = 1024;

}  // namespace

47 48
void SetCrashKeyValue(const butil::StringPiece& key,
                      const butil::StringPiece& value) {
gejun's avatar
gejun committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  if (!g_set_key_func_ || !g_crash_keys_)
    return;

  const CrashKey* crash_key = LookupCrashKey(key);

  DCHECK(crash_key) << "All crash keys must be registered before use "
                    << "(key = " << key << ")";

  // Handle the un-chunked case.
  if (!crash_key || crash_key->max_length <= g_chunk_max_length_) {
    g_set_key_func_(key, value);
    return;
  }

  // Unset the unused chunks.
  std::vector<std::string> chunks =
      ChunkCrashKeyValue(*crash_key, value, g_chunk_max_length_);
  for (size_t i = chunks.size();
       i < NumChunksForLength(crash_key->max_length);
       ++i) {
69
    g_clear_key_func_(butil::StringPrintf(kChunkFormatString, key.data(), i+1));
gejun's avatar
gejun committed
70 71 72 73
  }

  // Set the chunked keys.
  for (size_t i = 0; i < chunks.size(); ++i) {
74
    g_set_key_func_(butil::StringPrintf(kChunkFormatString, key.data(), i+1),
gejun's avatar
gejun committed
75 76 77 78
                    chunks[i]);
  }
}

79
void ClearCrashKey(const butil::StringPiece& key) {
gejun's avatar
gejun committed
80 81 82 83 84 85 86 87 88 89 90 91
  if (!g_clear_key_func_ || !g_crash_keys_)
    return;

  const CrashKey* crash_key = LookupCrashKey(key);

  // Handle the un-chunked case.
  if (!crash_key || crash_key->max_length <= g_chunk_max_length_) {
    g_clear_key_func_(key);
    return;
  }

  for (size_t i = 0; i < NumChunksForLength(crash_key->max_length); ++i) {
92
    g_clear_key_func_(butil::StringPrintf(kChunkFormatString, key.data(), i+1));
gejun's avatar
gejun committed
93 94 95
  }
}

96
void SetCrashKeyToStackTrace(const butil::StringPiece& key,
gejun's avatar
gejun committed
97 98 99 100 101 102
                             const StackTrace& trace) {
  size_t count = 0;
  const void* const* addresses = trace.Addresses(&count);
  SetCrashKeyFromAddresses(key, addresses, count);
}

103
void SetCrashKeyFromAddresses(const butil::StringPiece& key,
gejun's avatar
gejun committed
104 105 106 107 108 109 110 111 112 113
                              const void* const* addresses,
                              size_t count) {
  std::string value = "<null>";
  if (addresses && count) {
    const size_t kBreakpadValueMax = 255;

    std::vector<std::string> hex_backtrace;
    size_t length = 0;

    for (size_t i = 0; i < count; ++i) {
114
      std::string s = butil::StringPrintf("%p", addresses[i]);
gejun's avatar
gejun committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      length += s.length() + 1;
      if (length > kBreakpadValueMax)
        break;
      hex_backtrace.push_back(s);
    }

    value = JoinString(hex_backtrace, ' ');

    // Warn if this exceeds the breakpad limits.
    DCHECK_LE(value.length(), kBreakpadValueMax);
  }

  SetCrashKeyValue(key, value);
}

130 131
ScopedCrashKey::ScopedCrashKey(const butil::StringPiece& key,
                               const butil::StringPiece& value)
gejun's avatar
gejun committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    : key_(key.as_string()) {
  SetCrashKeyValue(key, value);
}

ScopedCrashKey::~ScopedCrashKey() {
  ClearCrashKey(key_);
}

size_t InitCrashKeys(const CrashKey* const keys, size_t count,
                     size_t chunk_max_length) {
  DCHECK(!g_crash_keys_) << "Crash logging may only be initialized once";
  if (!keys) {
    delete g_crash_keys_;
    g_crash_keys_ = NULL;
    return 0;
  }

  g_crash_keys_ = new CrashKeyMap;
  g_chunk_max_length_ = chunk_max_length;

  size_t total_keys = 0;
  for (size_t i = 0; i < count; ++i) {
    g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i]));
    total_keys += NumChunksForLength(keys[i].max_length);
    DCHECK_LT(keys[i].max_length, kLargestValueAllowed);
  }
  DCHECK_EQ(count, g_crash_keys_->size())
      << "Duplicate crash keys were registered";

  return total_keys;
}

164
const CrashKey* LookupCrashKey(const butil::StringPiece& key) {
gejun's avatar
gejun committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  if (!g_crash_keys_)
    return NULL;
  CrashKeyMap::const_iterator it = g_crash_keys_->find(key.as_string());
  if (it == g_crash_keys_->end())
    return NULL;
  return &(it->second);
}

void SetCrashKeyReportingFunctions(
    SetCrashKeyValueFuncT set_key_func,
    ClearCrashKeyValueFuncT clear_key_func) {
  g_set_key_func_ = set_key_func;
  g_clear_key_func_ = clear_key_func;
}

std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key,
181
                                            const butil::StringPiece& value,
gejun's avatar
gejun committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                                            size_t chunk_max_length) {
  std::string value_string = value.substr(0, crash_key.max_length).as_string();
  std::vector<std::string> chunks;
  for (size_t offset = 0; offset < value_string.length(); ) {
    std::string chunk = value_string.substr(offset, chunk_max_length);
    chunks.push_back(chunk);
    offset += chunk.length();
  }
  return chunks;
}

void ResetCrashLoggingForTesting() {
  delete g_crash_keys_;
  g_crash_keys_ = NULL;
  g_chunk_max_length_ = 0;
  g_set_key_func_ = NULL;
  g_clear_key_func_ = NULL;
}

}  // namespace debug
202
}  // namespace butil