1 // Copyright 2018 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 #ifndef BASE_TRACE_EVENT_BUILTIN_CATEGORIES_H_
6 #define BASE_TRACE_EVENT_BUILTIN_CATEGORIES_H_
7 
8 #include "base/base_export.h"
9 #include "base/macros.h"
10 #include "base/stl_util.h"
11 #include "base/trace_event/common/trace_event_common.h"
12 #include "build/build_config.h"
13 
14 // List of builtin category names. If you want to use a new category name in
15 // your code and you get a static assert, this is the right place to register
16 // the name. If the name is going to be used only for testing, please add it to
17 // |kIgnoredCategoriesForTesting| instead.
18 //
19 // Prefer to use '_' to separate word of category name, like content_capture.
20 //
21 // Parameter |X| must be a *macro* that takes a single |name| string argument,
22 // denoting a category name.
23 #define INTERNAL_TRACE_LIST_BUILTIN_CATEGORIES(X)                        \
24   /* These entries must go first to be consistent with the               \
25    * CategoryRegistry::kCategory* consts.*/                              \
26   X("tracing categories exhausted; must increase kMaxCategories")        \
27   X("tracing already shutdown")                                          \
28   X("__metadata")                                                        \
29   /* The rest of the list is in alphabetical order */                    \
30   X("accessibility")                                                     \
31   X("AccountFetcherService")                                             \
32   X("android_webview")                                                   \
33   X("audio")                                                             \
34   X("base")                                                              \
35   X("benchmark")                                                         \
36   X("blink")                                                             \
37   X("blink.bindings")                                                    \
38   X("blink.animations")                                                  \
39   X("blink.console")                                                     \
40   X("blink_gc")                                                          \
41   X("blink.net")                                                         \
42   X("blink_style")                                                       \
43   X("blink.user_timing")                                                 \
44   X("blink.worker")                                                      \
45   X("Blob")                                                              \
46   X("browser")                                                           \
47   X("browsing_data")                                                     \
48   X("CacheStorage")                                                      \
49   X("camera")                                                            \
50   X("cast_perf_test")                                                    \
51   X("cast.stream")                                                       \
52   X("cc")                                                                \
53   X("cc.debug")                                                          \
54   X("cdp.perf")                                                          \
55   X("chromeos")                                                          \
56   X("cma")                                                               \
57   X("compositor")                                                        \
58   X("content")                                                           \
59   X("content_capture")                                                   \
60   X("devtools")                                                          \
61   X("devtools.timeline")                                                 \
62   X("devtools.timeline.async")                                           \
63   X("disk_cache")                                                        \
64   X("download")                                                          \
65   X("download_service")                                                  \
66   X("drm")                                                               \
67   X("drmcursor")                                                         \
68   X("dwrite")                                                            \
69   X("DXVA Decoding")                                                     \
70   X("EarlyJava")                                                         \
71   X("evdev")                                                             \
72   X("event")                                                             \
73   X("exo")                                                               \
74   X("explore_sites")                                                     \
75   X("FileSystem")                                                        \
76   X("file_system_provider")                                              \
77   X("fonts")                                                             \
78   X("GAMEPAD")                                                           \
79   X("gpu")                                                               \
80   X("gpu.capture")                                                       \
81   X("headless")                                                          \
82   X("hwoverlays")                                                        \
83   X("identity")                                                          \
84   X("IndexedDB")                                                         \
85   X("input")                                                             \
86   X("io")                                                                \
87   X("ipc")                                                               \
88   X("Java")                                                              \
89   X("jni")                                                               \
90   X("jpeg")                                                              \
91   X("latency")                                                           \
92   X("latencyInfo")                                                       \
93   X("leveldb")                                                           \
94   X("loading")                                                           \
95   X("log")                                                               \
96   X("login")                                                             \
97   X("media")                                                             \
98   X("media_router")                                                      \
99   X("memory")                                                            \
100   X("midi")                                                              \
101   X("mojom")                                                             \
102   X("mus")                                                               \
103   X("native")                                                            \
104   X("navigation")                                                        \
105   X("net")                                                               \
106   X("netlog")                                                            \
107   X("offline_pages")                                                     \
108   X("omnibox")                                                           \
109   X("oobe")                                                              \
110   X("ozone")                                                             \
111   X("passwords")                                                         \
112   X("p2p")                                                               \
113   X("page-serialization")                                                \
114   X("pepper")                                                            \
115   X("ppapi")                                                             \
116   X("ppapi proxy")                                                       \
117   X("rail")                                                              \
118   X("renderer")                                                          \
119   X("renderer_host")                                                     \
120   X("renderer.scheduler")                                                \
121   X("RLZ")                                                               \
122   X("safe_browsing")                                                     \
123   X("screenlock_monitor")                                                \
124   X("sequence_manager")                                                  \
125   X("service_manager")                                                   \
126   X("ServiceWorker")                                                     \
127   X("sharing")                                                           \
128   X("shell")                                                             \
129   X("shortcut_viewer")                                                   \
130   X("shutdown")                                                          \
131   X("SiteEngagement")                                                    \
132   X("skia")                                                              \
133   X("sql")                                                               \
134   X("startup")                                                           \
135   X("sync")                                                              \
136   X("sync_lock_contention")                                              \
137   X("thread_pool")                                                       \
138   X("test_gpu")                                                          \
139   X("test_tracing")                                                      \
140   X("toplevel")                                                          \
141   X("ui")                                                                \
142   X("v8")                                                                \
143   X("v8.execute")                                                        \
144   X("ValueStoreFrontend::Backend")                                       \
145   X("views")                                                             \
146   X("views.frame")                                                       \
147   X("viz")                                                               \
148   X("vk")                                                                \
149   X("wayland")                                                           \
150   X("webaudio")                                                          \
151   X("weblayer")                                                          \
152   X("WebCore")                                                           \
153   X("webrtc")                                                            \
154   X("xr")                                                                \
155   X(TRACE_DISABLED_BY_DEFAULT("animation-worklet"))                      \
156   X(TRACE_DISABLED_BY_DEFAULT("audio-worklet"))                          \
157   X(TRACE_DISABLED_BY_DEFAULT("blink.debug"))                            \
158   X(TRACE_DISABLED_BY_DEFAULT("blink.debug.display_lock"))               \
159   X(TRACE_DISABLED_BY_DEFAULT("blink.debug.layout"))                     \
160   X(TRACE_DISABLED_BY_DEFAULT("blink.debug.layout.trees"))               \
161   X(TRACE_DISABLED_BY_DEFAULT("blink.feature_usage"))                    \
162   X(TRACE_DISABLED_BY_DEFAULT("blink_gc"))                               \
163   X(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"))                   \
164   X(TRACE_DISABLED_BY_DEFAULT("blink.invalidation"))                     \
165   X(TRACE_DISABLED_BY_DEFAULT("cc"))                                     \
166   X(TRACE_DISABLED_BY_DEFAULT("cc.debug"))                               \
167   X(TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"))                      \
168   X(TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"))                 \
169   X(TRACE_DISABLED_BY_DEFAULT("cc.debug.picture"))                       \
170   X(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"))                     \
171   X(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"))              \
172   X(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"))                 \
173   X(TRACE_DISABLED_BY_DEFAULT("cpu_profiler"))                           \
174   X(TRACE_DISABLED_BY_DEFAULT("cpu_profiler.debug"))                     \
175   X(TRACE_DISABLED_BY_DEFAULT("devtools.screenshot"))                    \
176   X(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"))                      \
177   X(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.frame"))                \
178   X(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.inputs"))               \
179   X(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.invalidationTracking")) \
180   X(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.layers"))               \
181   X(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"))              \
182   X(TRACE_DISABLED_BY_DEFAULT("file"))                                   \
183   X(TRACE_DISABLED_BY_DEFAULT("fonts"))                                  \
184   X(TRACE_DISABLED_BY_DEFAULT("gpu_cmd_queue"))                          \
185   X(TRACE_DISABLED_BY_DEFAULT("gpu.dawn"))                               \
186   X(TRACE_DISABLED_BY_DEFAULT("gpu.debug"))                              \
187   X(TRACE_DISABLED_BY_DEFAULT("gpu.decoder"))                            \
188   X(TRACE_DISABLED_BY_DEFAULT("gpu.device"))                             \
189   X(TRACE_DISABLED_BY_DEFAULT("gpu.service"))                            \
190   X(TRACE_DISABLED_BY_DEFAULT("histogram_samples"))                      \
191   X(TRACE_DISABLED_BY_DEFAULT("java-heap-profiler"))                     \
192   X(TRACE_DISABLED_BY_DEFAULT("layer-element"))                          \
193   X(TRACE_DISABLED_BY_DEFAULT("layout_shift.debug"))                     \
194   X(TRACE_DISABLED_BY_DEFAULT("lifecycles"))                             \
195   X(TRACE_DISABLED_BY_DEFAULT("loading"))                                \
196   X(TRACE_DISABLED_BY_DEFAULT("memory-infra"))                           \
197   X(TRACE_DISABLED_BY_DEFAULT("memory-infra.v8.code_stats"))             \
198   X(TRACE_DISABLED_BY_DEFAULT("net"))                                    \
199   X(TRACE_DISABLED_BY_DEFAULT("network"))                                \
200   X(TRACE_DISABLED_BY_DEFAULT("paint-worklet"))                          \
201   X(TRACE_DISABLED_BY_DEFAULT("power"))                                  \
202   X(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"))                     \
203   X(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler.debug"))               \
204   X(TRACE_DISABLED_BY_DEFAULT("sequence_manager"))                       \
205   X(TRACE_DISABLED_BY_DEFAULT("sequence_manager.debug"))                 \
206   X(TRACE_DISABLED_BY_DEFAULT("sequence_manager.verbose_snapshots"))     \
207   X(TRACE_DISABLED_BY_DEFAULT("skia"))                                   \
208   X(TRACE_DISABLED_BY_DEFAULT("skia.gpu"))                               \
209   X(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"))                         \
210   X(TRACE_DISABLED_BY_DEFAULT("SyncFileSystem"))                         \
211   X(TRACE_DISABLED_BY_DEFAULT("system_stats"))                           \
212   X(TRACE_DISABLED_BY_DEFAULT("thread_pool_diagnostics"))                \
213   X(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"))                          \
214   X(TRACE_DISABLED_BY_DEFAULT("toplevel.ipc"))                           \
215   X(TRACE_DISABLED_BY_DEFAULT("user_action_samples"))                    \
216   X(TRACE_DISABLED_BY_DEFAULT("v8.compile"))                             \
217   X(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"))                        \
218   X(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler.hires"))                  \
219   X(TRACE_DISABLED_BY_DEFAULT("v8.gc"))                                  \
220   X(TRACE_DISABLED_BY_DEFAULT("v8.gc_stats"))                            \
221   X(TRACE_DISABLED_BY_DEFAULT("v8.ic_stats"))                            \
222   X(TRACE_DISABLED_BY_DEFAULT("v8.runtime"))                             \
223   X(TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats"))                       \
224   X(TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats_sampling"))              \
225   X(TRACE_DISABLED_BY_DEFAULT("v8.turbofan"))                            \
226   X(TRACE_DISABLED_BY_DEFAULT("v8.wasm"))                                \
227   X(TRACE_DISABLED_BY_DEFAULT("video_and_image_capture"))                \
228   X(TRACE_DISABLED_BY_DEFAULT("viz.debug.overlay_planes"))               \
229   X(TRACE_DISABLED_BY_DEFAULT("viz.hit_testing_flow"))                   \
230   X(TRACE_DISABLED_BY_DEFAULT("viz.overdraw"))                           \
231   X(TRACE_DISABLED_BY_DEFAULT("viz.quads"))                              \
232   X(TRACE_DISABLED_BY_DEFAULT("viz.surface_id_flow"))                    \
233   X(TRACE_DISABLED_BY_DEFAULT("viz.surface_lifetime"))                   \
234   X(TRACE_DISABLED_BY_DEFAULT("viz.triangles"))                          \
235   X(TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"))                     \
236   X(TRACE_DISABLED_BY_DEFAULT("worker.scheduler"))
237 
238 #define INTERNAL_TRACE_INIT_CATEGORY_NAME(name) name,
239 
240 #define INTERNAL_TRACE_INIT_CATEGORY(name) {0, 0, name},
241 
242 namespace base {
243 namespace trace_event {
244 
245 // Constexpr version of string comparison operator. |a| and |b| must be valid
246 // C-style strings known at compile-time.
StrEqConstexpr(const char * a,const char * b)247 constexpr bool StrEqConstexpr(const char* a, const char* b) {
248   for (; *a != '\0' && *b != '\0'; ++a, ++b) {
249     if (*a != *b)
250       return false;
251   }
252   return *a == *b;
253 }
254 
255 // Tests for |StrEqConstexpr()|.
256 static_assert(StrEqConstexpr("foo", "foo"), "strings should be equal");
257 static_assert(!StrEqConstexpr("foo", "Foo"), "strings should not be equal");
258 static_assert(!StrEqConstexpr("foo", "foo1"), "strings should not be equal");
259 static_assert(!StrEqConstexpr("foo2", "foo"), "strings should not be equal");
260 static_assert(StrEqConstexpr("", ""), "strings should be equal");
261 static_assert(!StrEqConstexpr("foo", ""), "strings should not be equal");
262 static_assert(!StrEqConstexpr("", "foo"), "strings should not be equal");
263 static_assert(!StrEqConstexpr("ab", "abc"), "strings should not be equal");
264 static_assert(!StrEqConstexpr("abc", "ab"), "strings should not be equal");
265 
266 // Static-only class providing access to the compile-time registry of trace
267 // categories.
268 class BASE_EXPORT BuiltinCategories {
269  public:
270   // Returns a built-in category name at |index| in the registry.
At(size_t index)271   static constexpr const char* At(size_t index) {
272     return kBuiltinCategories[index];
273   }
274 
275   // Returns the amount of built-in categories in the registry.
Size()276   static constexpr size_t Size() { return base::size(kBuiltinCategories); }
277 
278   // Where in the builtin category list to start when populating the
279   // about://tracing UI.
280   static constexpr size_t kVisibleCategoryStart = 3;
281 
282   // Returns whether the category is either:
283   // - Properly registered in the builtin list.
284   // - Constists of several categories separated by commas.
285   // - Used only in tests.
286   // All trace categories are checked against this. A static_assert is triggered
287   // if at least one category fails this check.
IsAllowedCategory(const char * category)288   static constexpr bool IsAllowedCategory(const char* category) {
289 #if defined(OS_WIN) && defined(COMPONENT_BUILD)
290     return true;
291 #else
292     return IsBuiltinCategory(category) ||
293            IsCommaSeparatedCategoryGroup(category) ||
294            IsCategoryForTesting(category);
295 #endif
296   }
297 
298  private:
299   // The array of built-in category names used for compile-time lookup.
300   static constexpr const char* kBuiltinCategories[] = {
301       INTERNAL_TRACE_LIST_BUILTIN_CATEGORIES(
302           INTERNAL_TRACE_INIT_CATEGORY_NAME)};
303 
304   // The array of category names used only for testing. It's kept separately
305   // from the main list to avoid allocating the space for them in the binary.
306   static constexpr const char* kCategoriesForTesting[] = {
307       "\001\002\003\n\r",
308       "a",
309       "all",
310       "b",
311       "b1",
312       "c",
313       "c0",
314       "c1",
315       "c2",
316       "c3",
317       "c4",
318       "cat",
319       "cat1",
320       "cat2",
321       "cat3",
322       "cat4",
323       "cat5",
324       "cat6",
325       "category",
326       "drink",
327       "excluded_cat",
328       "filtered_cat",
329       "foo",
330       "inc",
331       "inc2",
332       "included",
333       "inc_wildcard_",
334       "inc_wildcard_abc",
335       "inc_wildchar_bla_end",
336       "inc_wildchar_x_end",
337       "kTestCategory",
338       "log",
339       "noise",
340       "other_included",
341       "test",
342       "test_category",
343       "Testing",
344       "TraceEventAgentTestCategory",
345       "unfiltered_cat",
346       "whitewashed",
347       "x",
348       TRACE_DISABLED_BY_DEFAULT("c9"),
349       TRACE_DISABLED_BY_DEFAULT("cat"),
350       TRACE_DISABLED_BY_DEFAULT("filtered_cat"),
351       TRACE_DISABLED_BY_DEFAULT("NotTesting"),
352       TRACE_DISABLED_BY_DEFAULT("Testing"),
353       TRACE_DISABLED_BY_DEFAULT("unfiltered_cat")};
354 
355   // Returns whether |str| is in |array| of |array_len|.
IsStringInArray(const char * str,const char * const array[],size_t array_len)356   static constexpr bool IsStringInArray(const char* str,
357                                         const char* const array[],
358                                         size_t array_len) {
359     for (size_t i = 0; i < array_len; ++i) {
360       if (StrEqConstexpr(str, array[i]))
361         return true;
362     }
363     return false;
364   }
365 
366   // Returns whether |category_group| contains a ',' symbol, denoting that an
367   // event belongs to several categories. We don't add such strings in the
368   // builtin list but allow them to pass the static assert.
IsCommaSeparatedCategoryGroup(const char * category_group)369   static constexpr bool IsCommaSeparatedCategoryGroup(
370       const char* category_group) {
371     for (; *category_group != '\0'; ++category_group) {
372       if (*category_group == ',')
373         return true;
374     }
375     return false;
376   }
377 
378   // Returns whether |category| is used only for testing.
IsCategoryForTesting(const char * category)379   static constexpr bool IsCategoryForTesting(const char* category) {
380     return IsStringInArray(category, kCategoriesForTesting,
381                            base::size(kCategoriesForTesting));
382   }
383 
384   // Returns whether |category| is registered in the builtin list.
IsBuiltinCategory(const char * category)385   static constexpr bool IsBuiltinCategory(const char* category) {
386     return IsStringInArray(category, kBuiltinCategories,
387                            base::size(kBuiltinCategories));
388   }
389 
390   DISALLOW_IMPLICIT_CONSTRUCTORS(BuiltinCategories);
391 };
392 
393 }  // namespace trace_event
394 }  // namespace base
395 
396 #endif  // BASE_TRACE_EVENT_BUILTIN_CATEGORIES_H_
397