1 /*
2  * Copyright 2014 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 <stdlib.h>
19 #include <ctype.h>
20 
21 #define LOG_TAG "ADebug"
22 #include <cutils/atomic.h>
23 #include <utils/Log.h>
24 #include <utils/misc.h>
25 
26 #include <cutils/properties.h>
27 
28 #include <ADebug.h>
29 #include <AStringUtils.h>
30 #include <AUtils.h>
31 
32 #define UNUSED(x) ((void)(x))
33 
34 namespace android {
35 
36 //static
GetLevelFromSettingsString(const char * name,const char * value,long def)37 long ADebug::GetLevelFromSettingsString(
38         const char *name, const char *value, long def) {
39     // split on ,
40     const char *next = value, *current;
41     while (next != NULL) {
42         current = next;
43         next = strchr(current, ',');
44         if (next != NULL) {
45             ++next;  // pass ,
46         }
47 
48         while (isspace(*current)) {
49             ++current;
50         }
51         // check for :
52         const char *colon = strchr(current, ':');
53 
54         // get level
55         char *end;
56         errno = 0;  // strtol does not clear errno, but it can be set for any return value
57         long level = strtol(current, &end, 10);
58         while (isspace(*end)) {
59             ++end;
60         }
61         if (errno != 0 || end == current || (end != colon && *end != '\0' && end != next)) {
62             // invalid level - skip
63             continue;
64         }
65         if (colon != NULL) {
66             // check if pattern matches
67             do {  // skip colon and spaces
68                 ++colon;
69             } while (isspace(*colon));
70             size_t globLen = (next == NULL ? strlen(colon) : (next - 1 - colon));
71             while (globLen > 0 && isspace(colon[globLen - 1])) {
72                 --globLen;  // trim glob
73             }
74 
75             if (!AStringUtils::MatchesGlob(
76                     colon, globLen, name, strlen(name), true /* ignoreCase */)) {
77                 continue;
78             }
79         }
80 
81         // update value
82         def = level;
83     }
84     return def;
85 }
86 
87 //static
GetLevelFromProperty(const char * name,const char * propertyName,long def)88 long ADebug::GetLevelFromProperty(
89         const char *name, const char *propertyName, long def) {
90     char value[PROPERTY_VALUE_MAX];
91     if (property_get(propertyName, value, NULL)) {
92         def = GetLevelFromSettingsString(name, value, def);
93     }
94     return def;
95 }
96 
97 //static
GetDebugLevelFromProperty(const char * name,const char * propertyName,ADebug::Level def)98 ADebug::Level ADebug::GetDebugLevelFromProperty(
99         const char *name, const char *propertyName, ADebug::Level def) {
100     long level = GetLevelFromProperty(name, propertyName, (long)def);
101     return (Level)min(max(level, (long)kDebugNone), (long)kDebugMax);
102 }
103 
104 //static
GetDebugName(const char * name)105 char *ADebug::GetDebugName(const char *name) {
106     char *debugName = strdup(name);
107     const char *terms[] = { "omx", "video", "audio" };
108     for (size_t i = 0; i < NELEM(terms) && debugName != NULL; i++) {
109         const char *term = terms[i];
110         const size_t len = strlen(term);
111         char *match = strcasestr(debugName, term);
112         if (match != NULL && (match == debugName || match[-1] == '.'
113                 || match[len] == '.' || match[len] == '\0')) {
114             char *src = match + len;
115             if (match == debugName || match[-1] == '.') {
116                 src += (*src == '.');  // remove trailing or double .
117             }
118             memmove(match, src, debugName + strlen(debugName) - src + 1);
119         }
120     }
121 
122     return debugName;
123 }
124 
125 //static
getExperimentFlag(bool allow,const char * name,uint64_t modulo,uint64_t limit,uint64_t plus,uint64_t timeDivisor)126 bool ADebug::getExperimentFlag(
127         bool allow, const char *name, uint64_t modulo,
128         uint64_t limit, uint64_t plus, uint64_t timeDivisor) {
129     // see if this experiment should be disabled/enabled based on properties.
130     // default to 2 to allow 0/1 specification
131     const int undefined = 2;
132     long level = GetLevelFromProperty(name, "debug.stagefright.experiments", undefined);
133     if (level != undefined) {
134         ALOGI("experiment '%s': %s from property", name, level ? "ENABLED" : "disabled");
135         return allow && (level != 0);
136     }
137 
138 #ifndef ENABLE_STAGEFRIGHT_AUTO_EXPERIMENTS
139     UNUSED(modulo);
140     UNUSED(limit);
141     UNUSED(plus);
142     UNUSED(timeDivisor);
143     return false;
144 #else
145     // Disable automatic experiments in "non-experimental" builds (that is, _all_ builds
146     // as there is no "experimental" build).
147     // TODO: change build type to enable automatic experiments in the future for some builds
148     char value[PROPERTY_VALUE_MAX];
149     if (property_get("ro.build.type", value, NULL)) {
150         if (strcmp(value, "experimental")) {
151             return false;
152         }
153     }
154 
155     static volatile int32_t haveSerial = 0;
156     static uint64_t serialNum;
157     if (!android_atomic_acquire_load(&haveSerial)) {
158         // calculate initial counter value based on serial number
159         static char serial[PROPERTY_VALUE_MAX];
160         property_get("ro.serialno", serial, "0");
161         uint64_t num = 0; // it is okay for this number to overflow
162         for (size_t i = 0; i < NELEM(serial) && serial[i] != '\0'; ++i) {
163             const char &c = serial[i];
164             // try to use most letters of serialno
165             if (isdigit(c)) {
166                 num = num * 10 + (c - '0');
167             } else if (islower(c)) {
168                 num = num * 26 + (c - 'a');
169             } else if (isupper(c)) {
170                 num = num * 26 + (c - 'A');
171             } else {
172                 num = num * 256 + c;
173             }
174         }
175         serialNum = num;
176         android_atomic_release_store(1, &haveSerial);
177     }
178     ALOGD("serial: %llu, time: %lld", (long long unsigned)serialNum, (long long)time(NULL));
179     // MINOR: use modulo for counter and time, so that their sum does not
180     // roll over, and mess up the correlation between related experiments.
181     // e.g. keep (a mod 2N) = 0 impl (a mod N) = 0
182     time_t counter = (time(NULL) / timeDivisor) % modulo + plus + serialNum % modulo;
183     bool enable = allow && (counter % modulo < limit);
184     ALOGI("experiment '%s': %s", name, enable ? "ENABLED" : "disabled");
185     return enable;
186 #endif
187 }
188 
189 }  // namespace android
190 
191