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 #ifndef INCLUDE_PERFETTO_TRACING_STRING_HELPERS_H_
18 #define INCLUDE_PERFETTO_TRACING_STRING_HELPERS_H_
19
20 #include "perfetto/base/export.h"
21
22 #include <cstddef>
23 #include <string>
24
25 namespace perfetto {
26
27 // A wrapper for marking strings that can't be determined to be static at build
28 // time, but are in fact static.
29 class PERFETTO_EXPORT StaticString {
30 public:
31 // Implicit constructor for string literals.
32 template <size_t N>
StaticString(const char (& str)[N])33 constexpr StaticString(const char (&str)[N]) : value(str) {}
34
35 // Implicit constructor for null strings.
StaticString(std::nullptr_t)36 constexpr StaticString(std::nullptr_t) : value(nullptr) {}
37
StaticString(const char * str)38 constexpr explicit StaticString(const char* str) : value(str) {}
39
40 const char* value;
41 };
42
43 namespace internal {
44
45 // Ensure that |string| is a static constant string.
46 //
47 // If you get a compiler failure here, you are most likely trying to use
48 // TRACE_EVENT with a dynamic event name. There are two ways to fix this:
49 //
50 // 1) If the event name is actually dynamic (e.g., std::string), write it into
51 // the event manually:
52 //
53 // TRACE_EVENT("category", nullptr, [&](perfetto::EventContext ctx) {
54 // ctx.event()->set_name(dynamic_name);
55 // });
56 //
57 // 2) If the name is static, but the pointer is computed at runtime, wrap it
58 // with perfetto::StaticString:
59 //
60 // TRACE_EVENT("category", perfetto::StaticString{name});
61 //
62 // DANGER: Using perfetto::StaticString with strings whose contents change
63 // dynamically can cause silent trace data corruption.
64 //
GetStaticString(StaticString string)65 constexpr const char* GetStaticString(StaticString string) {
66 return string.value;
67 }
68
69 } // namespace internal
70
71 // A explicit wrapper for marking strings as dynamic to ensure that perfetto
72 // doesn't try to cache the pointer value.
73 class PERFETTO_EXPORT DynamicString {
74 public:
DynamicString(const std::string & str)75 explicit DynamicString(const std::string& str)
76 : value(str.data()), length(str.length()) {}
DynamicString(const char * str)77 explicit DynamicString(const char* str) : value(str), length(strlen(str)) {}
DynamicString(const char * str,size_t len)78 DynamicString(const char* str, size_t len) : value(str), length(len) {}
79
80 const char* value;
81 size_t length;
82 };
83
84 } // namespace perfetto
85
86 #endif // INCLUDE_PERFETTO_TRACING_STRING_HELPERS_H_
87