1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/base/crash_keys.h"
18
19 #include <string.h>
20
21 #include <atomic>
22 #include <cinttypes>
23
24 #include "perfetto/ext/base/string_utils.h"
25
26 namespace perfetto {
27 namespace base {
28
29 namespace {
30
31 constexpr size_t kMaxKeys = 32;
32
33 std::atomic<CrashKey*> g_keys[kMaxKeys]{};
34 std::atomic<uint32_t> g_num_keys{};
35 } // namespace
36
Register()37 void CrashKey::Register() {
38 // If doesn't matter if we fail below. If there are no slots left, don't
39 // keep trying re-registering on every Set(), the outcome won't change.
40
41 // If two threads raced on the Register(), avoid registering the key twice.
42 if (registered_.exchange(true))
43 return;
44
45 uint32_t slot = g_num_keys.fetch_add(1);
46 if (slot >= kMaxKeys) {
47 PERFETTO_LOG("Too many crash keys registered");
48 return;
49 }
50 g_keys[slot].store(this);
51 }
52
53 // Returns the number of chars written, without counting the \0.
ToString(char * dst,size_t len)54 size_t CrashKey::ToString(char* dst, size_t len) {
55 if (len > 0)
56 *dst = '\0';
57 switch (type_.load(std::memory_order_relaxed)) {
58 case Type::kUnset:
59 break;
60 case Type::kInt:
61 return SprintfTrunc(dst, len, "%s: %" PRId64 "\n", name_,
62 int_value_.load(std::memory_order_relaxed));
63 case Type::kStr:
64 char buf[sizeof(str_value_)];
65 for (size_t i = 0; i < sizeof(str_value_); i++)
66 buf[i] = str_value_[i].load(std::memory_order_relaxed);
67
68 // Don't assume |str_value_| is properly null-terminated.
69 return SprintfTrunc(dst, len, "%s: %.*s\n", name_, int(sizeof(buf)), buf);
70 }
71 return 0;
72 }
73
UnregisterAllCrashKeysForTesting()74 void UnregisterAllCrashKeysForTesting() {
75 g_num_keys.store(0);
76 for (auto& key : g_keys)
77 key.store(nullptr);
78 }
79
SerializeCrashKeys(char * dst,size_t len)80 size_t SerializeCrashKeys(char* dst, size_t len) {
81 size_t written = 0;
82 uint32_t num_keys = g_num_keys.load();
83 if (len > 0)
84 *dst = '\0';
85 for (uint32_t i = 0; i < num_keys && written < len; i++) {
86 CrashKey* key = g_keys[i].load();
87 if (!key)
88 continue; // Can happen if we hit this between the add and the store.
89 written += key->ToString(dst + written, len - written);
90 }
91 PERFETTO_DCHECK(written <= len);
92 PERFETTO_DCHECK(len == 0 || dst[written] == '\0');
93 return written;
94 }
95
96 } // namespace base
97 } // namespace perfetto
98