1 /*
2  * Copyright (C) 2012 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 <cutils/trace.h>
18 
19 #include "trace-dev.inc"
20 
21 static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
22 
23 // Set whether tracing is enabled in this process.  This is used to prevent
24 // the Zygote process from tracing.
atrace_set_tracing_enabled(bool enabled)25 void atrace_set_tracing_enabled(bool enabled)
26 {
27     atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release);
28     atrace_update_tags();
29 }
30 
atrace_init_once()31 static void atrace_init_once()
32 {
33     atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
34     if (atrace_marker_fd == -1) {
35         ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
36         atrace_enabled_tags = 0;
37         goto done;
38     }
39 
40     atrace_enabled_tags = atrace_get_property();
41 
42 done:
43     atomic_store_explicit(&atrace_is_ready, true, memory_order_release);
44 }
45 
atrace_setup()46 void atrace_setup()
47 {
48     pthread_once(&atrace_once_control, atrace_init_once);
49 }
50 
atrace_begin_body(const char * name)51 void atrace_begin_body(const char* name)
52 {
53     WRITE_MSG("B|%d|", "%s", name, "");
54 }
55 
atrace_end_body()56 void atrace_end_body()
57 {
58     WRITE_MSG("E|%d", "%s", "", "");
59 }
60 
atrace_async_begin_body(const char * name,int32_t cookie)61 void atrace_async_begin_body(const char* name, int32_t cookie)
62 {
63     WRITE_MSG("S|%d|", "|%" PRId32, name, cookie);
64 }
65 
atrace_async_end_body(const char * name,int32_t cookie)66 void atrace_async_end_body(const char* name, int32_t cookie)
67 {
68     WRITE_MSG("F|%d|", "|%" PRId32, name, cookie);
69 }
70 
atrace_int_body(const char * name,int32_t value)71 void atrace_int_body(const char* name, int32_t value)
72 {
73     WRITE_MSG("C|%d|", "|%" PRId32, name, value);
74 }
75 
atrace_int64_body(const char * name,int64_t value)76 void atrace_int64_body(const char* name, int64_t value)
77 {
78     WRITE_MSG("C|%d|", "|%" PRId64, name, value);
79 }
80