Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: chrome/app/mash/mash_crash_keys.cc

Issue 2718123003: mash: Store chrome --mash crash key metadata in shared memory
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "chrome/app/mash/mash_crash_keys.h"
6
7 #include <fcntl.h> // For O_ constants.
8 #include <stdlib.h>
9 #include <sys/mman.h> // For shm_mem().
10 #include <unistd.h>
11
12 #include <string>
13 #include <vector>
14
15 #include "base/debug/crash_logging.h"
16 #include "base/logging.h"
17 #include "base/strings/string_piece.h"
18 #include "base/strings/stringprintf.h"
19 #include "breakpad/src/common/simple_string_dictionary.h"
20 #include "components/crash/core/common/crash_keys.h"
21
22 using google_breakpad::SimpleStringDictionary;
23
24 namespace mash_crash_keys {
25 namespace {
26
27 // The size of the shared memory buffer for crash keys.
28 const size_t kSharedMemorySize = sizeof(SimpleStringDictionary);
29
30 // The shared memory name, including a trailing pid. The name is shared with
31 // crash_reporter, see src/platform2/crash-reporter/user_collector.cc.
32 const char kSharedMemoryNameFormat[] = "/chrome_crash_keys_%d";
33
34 // File descriptor for the shared memory buffer containing the crash key
35 // dictionary.
36 int g_shared_memory_fd = -1;
37
38 SimpleStringDictionary* g_crash_key_dictionary = nullptr;
39
40 void SetCrashKey(const base::StringPiece& key, const base::StringPiece& value) {
41 g_crash_key_dictionary->SetKeyValue(key.data(), value.data());
hashimoto 2017/03/02 07:42:22 base/strings/string_piece.h says StrinbPiece::data
42 }
43
44 void ClearCrashKey(const base::StringPiece& key) {
45 g_crash_key_dictionary->RemoveKey(key.data());
hashimoto 2017/03/02 07:42:22 ditto.
46 }
47
48 void CreateSharedMemoryDictionary() {
49 CHECK(!g_crash_key_dictionary);
50
51 // Create a shared memory buffer with a well-known name. Chrome unlinks
52 // the shared memory buffer below for normal exit. The OS crash_reporter
53 // unlinks the buffer for crashes.
54 const std::string shared_memory_name =
55 base::StringPrintf(kSharedMemoryNameFormat, getpid());
56 const int oflag = O_CREAT | O_RDWR;
vapier 2017/03/11 06:14:59 i don't think this exec's, so should use O_CLOEXEC
57 // Make user-only read-write (not group or world). crash_reporter runs as root
58 // and can read the buffer regardless.
59 const mode_t mode = S_IRUSR | S_IWUSR;
vapier 2017/03/11 06:14:59 S_xxx constants aren't readable. just use an octa
60 g_shared_memory_fd = shm_open(shared_memory_name.c_str(), oflag, mode);
hashimoto 2017/03/02 07:42:22 Can't we just use utilities in base/memory/shared_
61 PCHECK(g_shared_memory_fd != -1);
62
63 // Set the size of the buffer. The bytes are automatically initialized to 0.
64 int result = ftruncate(g_shared_memory_fd, kSharedMemorySize);
hashimoto 2017/03/02 07:42:22 Please use HANDLE_EINTR. ftruncate may return EINT
65 PCHECK(result != -1);
66
67 // Map the buffer as read-write memory.
68 void* buffer = mmap(nullptr, kSharedMemorySize, PROT_READ | PROT_WRITE,
69 MAP_SHARED, g_shared_memory_fd, 0 /* offset */);
70 PCHECK(buffer != MAP_FAILED);
71
72 // Memory filled with 0 is a valid empty SimpleStringDictionary.
hashimoto 2017/03/02 07:42:22 Please use placement new, so that you don't have t
73 g_crash_key_dictionary = reinterpret_cast<SimpleStringDictionary*>(buffer);
74 DCHECK_EQ(0u, g_crash_key_dictionary->GetCount());
75 }
76
77 void RegisterCrashKeys() {
hashimoto 2017/03/02 07:42:22 If you want to allow processes which cannot depend
78 // Register keys used by non-browser processes launched by the service manager
79 // (e.g. the GPU service). See chrome/common/crash_keys.cc.
80 std::vector<base::debug::CrashKey> keys = {
81 {"url-chunk", crash_keys::kLargeSize},
hashimoto 2017/03/02 07:42:22 Please use the constants defined in the appropriat
82 {"total-discardable-memory-allocated", crash_keys::kSmallSize},
83 {"discardable-memory-free", crash_keys::kSmallSize},
84 };
85 crash_keys::GetCrashKeysForCommandLineSwitches(&keys);
86 base::debug::InitCrashKeys(&keys.at(0), keys.size(),
87 crash_keys::kChunkMaxLength);
88 }
89
90 } // namespace
91
92 void Initialize() {
93 CHECK(!g_crash_key_dictionary);
94 CreateSharedMemoryDictionary();
95 RegisterCrashKeys();
96 base::debug::SetCrashKeyReportingFunctions(&SetCrashKey, &ClearCrashKey);
97 }
98
99 void Shutdown() {
100 CHECK(g_crash_key_dictionary);
101 int result = munmap(g_crash_key_dictionary, kSharedMemorySize);
102 PCHECK(result != -1);
103 g_crash_key_dictionary = nullptr;
104
105 result = close(g_shared_memory_fd);
106 PCHECK(result != -1);
107 g_shared_memory_fd = -1;
108
109 const std::string shared_memory_name =
110 base::StringPrintf(kSharedMemoryNameFormat, getpid());
111 result = shm_unlink(shared_memory_name.c_str());
112 PCHECK(result != -1);
113 }
114
115 int GetSharedMemoryFdForTesting() {
116 return g_shared_memory_fd;
117 }
118
119 size_t GetCrashKeyCountForTesting() {
120 return g_crash_key_dictionary->GetCount();
121 }
122
123 } // namespace mash_crash_keys
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698