blob: 15c8a98a26ae52789b39261784964c92f6dd4654 [file] [log] [blame]
Alex Ray0a346432012-11-14 17:25:28 -08001/*
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 <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/types.h>
24#include <cutils/atomic.h>
25#include <cutils/compiler.h>
26#include <cutils/properties.h>
27#include <cutils/trace.h>
28
29#define LOG_TAG "cutils-trace"
30#include <cutils/log.h>
31
32int32_t atrace_is_ready = 0;
33int atrace_marker_fd = -1;
34uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
35static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
36
37// Read the sysprop and return the value tags should be set to
38static uint64_t atrace_get_property()
39{
40 char value[PROPERTY_VALUE_MAX];
41 char *endptr;
42 uint64_t tags;
43
44 property_get("debug.atrace.tags.enableflags", value, "0");
45 errno = 0;
46 tags = strtoull(value, &endptr, 0);
47 if (value[0] == '\0' || *endptr != '\0') {
48 ALOGE("Error parsing trace property: Not a number: %s", value);
49 return 0;
50 } else if (errno == ERANGE || tags == ULLONG_MAX) {
51 ALOGE("Error parsing trace property: Number too large: %s", value);
52 return 0;
53 }
54 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
55}
56
57static void atrace_init_once()
58{
59 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY);
60 if (atrace_marker_fd == -1) {
61 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
62 atrace_enabled_tags = 0;
63 goto done;
64 }
65
66 atrace_enabled_tags = atrace_get_property();
67
68done:
69 android_atomic_release_store(1, &atrace_is_ready);
70}
71
72void atrace_setup()
73{
74 pthread_once(&atrace_once_control, atrace_init_once);
75}