OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/breakpad/app/crash_keys_win.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/base_switches.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/breakpad/app/breakpad_client.h" |
| 16 |
| 17 |
| 18 namespace breakpad { |
| 19 |
| 20 namespace { |
| 21 |
| 22 const size_t kMaxPluginPathLength = 256; |
| 23 const size_t kMaxDynamicEntries = 256; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 CrashKeysWin* CrashKeysWin::keeper_; |
| 28 |
| 29 CrashKeysWin::CrashKeysWin() : dynamic_keys_offset_(0) { |
| 30 DCHECK_EQ(static_cast<CrashKeysWin*>(NULL), keeper_); |
| 31 keeper_ = this; |
| 32 } |
| 33 |
| 34 CrashKeysWin::~CrashKeysWin() { |
| 35 DCHECK_EQ(this, keeper_); |
| 36 keeper_ = NULL; |
| 37 } |
| 38 |
| 39 // Appends the plugin path to |g_custom_entries|. |
| 40 void CrashKeysWin::SetPluginPath(const std::wstring& path) { |
| 41 if (path.size() > kMaxPluginPathLength) { |
| 42 // If the path is too long, truncate from the start rather than the end, |
| 43 // since we want to be able to recover the DLL name. |
| 44 SetPluginPath(path.substr(path.size() - kMaxPluginPathLength)); |
| 45 return; |
| 46 } |
| 47 |
| 48 // The chunk size without terminator. |
| 49 const size_t kChunkSize = static_cast<size_t>( |
| 50 google_breakpad::CustomInfoEntry::kValueMaxLength - 1); |
| 51 |
| 52 int chunk_index = 0; |
| 53 size_t chunk_start = 0; // Current position inside |path| |
| 54 |
| 55 for (chunk_start = 0; chunk_start < path.size(); chunk_index++) { |
| 56 size_t chunk_length = std::min(kChunkSize, path.size() - chunk_start); |
| 57 |
| 58 custom_entries_.push_back(google_breakpad::CustomInfoEntry( |
| 59 base::StringPrintf(L"plugin-path-chunk-%i", chunk_index + 1).c_str(), |
| 60 path.substr(chunk_start, chunk_length).c_str())); |
| 61 |
| 62 chunk_start += chunk_length; |
| 63 } |
| 64 } |
| 65 |
| 66 // Appends the breakpad dump path to |g_custom_entries|. |
| 67 void CrashKeysWin::SetBreakpadDumpPath(BreakpadClient* breakpad_client) { |
| 68 |
| 69 base::FilePath crash_dumps_dir_path; |
| 70 if (breakpad_client->GetAlternativeCrashDumpLocation( |
| 71 &crash_dumps_dir_path)) { |
| 72 custom_entries_.push_back(google_breakpad::CustomInfoEntry( |
| 73 L"breakpad-dump-location", crash_dumps_dir_path.value().c_str())); |
| 74 } |
| 75 } |
| 76 |
| 77 // Returns the custom info structure based on the dll in parameter and the |
| 78 // process type. |
| 79 google_breakpad::CustomClientInfo* |
| 80 CrashKeysWin::GetCustomInfo(const std::wstring& exe_path, |
| 81 const std::wstring& type, |
| 82 const std::wstring& profile_type, |
| 83 base::CommandLine* cmd_line, |
| 84 BreakpadClient* breakpad_client) { |
| 85 base::string16 version, product; |
| 86 base::string16 special_build; |
| 87 base::string16 channel_name; |
| 88 |
| 89 breakpad_client->GetProductNameAndVersion( |
| 90 base::FilePath(exe_path), |
| 91 &product, |
| 92 &version, |
| 93 &special_build, |
| 94 &channel_name); |
| 95 |
| 96 // We only expect this method to be called once per process. |
| 97 // Common enties |
| 98 custom_entries_.push_back( |
| 99 google_breakpad::CustomInfoEntry(L"ver", |
| 100 base::UTF16ToWide(version).c_str())); |
| 101 custom_entries_.push_back( |
| 102 google_breakpad::CustomInfoEntry(L"prod", |
| 103 base::UTF16ToWide(product).c_str())); |
| 104 custom_entries_.push_back( |
| 105 google_breakpad::CustomInfoEntry(L"plat", L"Win32")); |
| 106 custom_entries_.push_back( |
| 107 google_breakpad::CustomInfoEntry(L"ptype", type.c_str())); |
| 108 custom_entries_.push_back(google_breakpad::CustomInfoEntry( |
| 109 L"pid", base::StringPrintf(L"%d", ::GetCurrentProcessId()).c_str())); |
| 110 custom_entries_.push_back(google_breakpad::CustomInfoEntry( |
| 111 L"channel", base::UTF16ToWide(channel_name).c_str())); |
| 112 custom_entries_.push_back(google_breakpad::CustomInfoEntry( |
| 113 L"profile-type", profile_type.c_str())); |
| 114 |
| 115 if (!special_build.empty()) |
| 116 custom_entries_.push_back(google_breakpad::CustomInfoEntry( |
| 117 L"special", base::UTF16ToWide(special_build).c_str())); |
| 118 |
| 119 if (type == L"plugin" || type == L"ppapi") { |
| 120 std::wstring plugin_path = cmd_line->GetSwitchValueNative("plugin-path"); |
| 121 if (!plugin_path.empty()) |
| 122 SetPluginPath(plugin_path); |
| 123 } |
| 124 |
| 125 |
| 126 // Check whether configuration management controls crash reporting. |
| 127 bool crash_reporting_enabled = true; |
| 128 bool controlled_by_policy = breakpad_client->ReportingIsEnforcedByPolicy( |
| 129 &crash_reporting_enabled); |
| 130 bool use_crash_service = !controlled_by_policy && |
| 131 (cmd_line->HasSwitch(switches::kNoErrorDialogs) || |
| 132 breakpad_client->IsRunningUnattended()); |
| 133 if (use_crash_service) |
| 134 SetBreakpadDumpPath(breakpad_client); |
| 135 |
| 136 // Create space for dynamic ad-hoc keys. The names and values are set using |
| 137 // the API defined in base/debug/crash_logging.h. |
| 138 dynamic_keys_offset_ = custom_entries_.size(); |
| 139 for (size_t i = 0; i < kMaxDynamicEntries; ++i) { |
| 140 // The names will be mutated as they are set. Un-numbered since these are |
| 141 // merely placeholders. The name cannot be empty because Breakpad's |
| 142 // HTTPUpload will interpret that as an invalid parameter. |
| 143 custom_entries_.push_back( |
| 144 google_breakpad::CustomInfoEntry(L"unspecified-crash-key", L"")); |
| 145 } |
| 146 |
| 147 static google_breakpad::CustomClientInfo custom_client_info; |
| 148 custom_client_info.entries = &custom_entries_.front(); |
| 149 custom_client_info.count = custom_entries_.size(); |
| 150 |
| 151 return &custom_client_info; |
| 152 } |
| 153 |
| 154 void CrashKeysWin::SetCrashKeyValue( |
| 155 const std::wstring& key, const std::wstring& value) { |
| 156 // CustomInfoEntry limits the length of key and value. If they exceed |
| 157 // their maximum length the underlying string handling functions raise |
| 158 // an exception and prematurely trigger a crash. Truncate here. |
| 159 std::wstring safe_key(std::wstring(key).substr( |
| 160 0, google_breakpad::CustomInfoEntry::kNameMaxLength - 1)); |
| 161 std::wstring safe_value(std::wstring(value).substr( |
| 162 0, google_breakpad::CustomInfoEntry::kValueMaxLength - 1)); |
| 163 |
| 164 // If we already have a value for this key, update it; otherwise, insert |
| 165 // the new value if we have not exhausted the pre-allocated slots for dynamic |
| 166 // entries. |
| 167 base::AutoLock lock(lock_); |
| 168 |
| 169 DynamicEntriesMap::iterator it = dynamic_entries_.find(safe_key); |
| 170 google_breakpad::CustomInfoEntry* entry = NULL; |
| 171 if (it == dynamic_entries_.end()) { |
| 172 if (dynamic_entries_.size() >= kMaxDynamicEntries) |
| 173 return; |
| 174 entry = &custom_entries_[dynamic_keys_offset_++]; |
| 175 dynamic_entries_.insert(std::make_pair(safe_key, entry)); |
| 176 } else { |
| 177 entry = it->second; |
| 178 } |
| 179 |
| 180 entry->set(safe_key.data(), safe_value.data()); |
| 181 } |
| 182 |
| 183 void CrashKeysWin::ClearCrashKeyValue(const std::wstring& key) { |
| 184 base::AutoLock lock(lock_); |
| 185 |
| 186 std::wstring key_string(key); |
| 187 DynamicEntriesMap::iterator it = dynamic_entries_.find(key_string); |
| 188 if (it == dynamic_entries_.end()) |
| 189 return; |
| 190 |
| 191 it->second->set_value(NULL); |
| 192 } |
| 193 |
| 194 } // namespace breakpad |
OLD | NEW |