| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2015 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 "sysdeps.h" | 
 | 18 | #include "adb_trace.h" | 
 | 19 |  | 
 | 20 | #include <string> | 
 | 21 | #include <unordered_map> | 
 | 22 | #include <vector> | 
 | 23 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> | 
 | 25 | #include <android-base/strings.h> | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 26 |  | 
 | 27 | #include "adb.h" | 
 | 28 |  | 
 | 29 | #if !ADB_HOST | 
| Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 30 | #include <android-base/properties.h> | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 31 | #endif | 
 | 32 |  | 
 | 33 | #if !ADB_HOST | 
 | 34 | const char* adb_device_banner = "device"; | 
 | 35 | static android::base::LogdLogger gLogdLogger; | 
 | 36 | #else | 
 | 37 | const char* adb_device_banner = "host"; | 
 | 38 | #endif | 
 | 39 |  | 
 | 40 | void AdbLogger(android::base::LogId id, android::base::LogSeverity severity, | 
 | 41 |                const char* tag, const char* file, unsigned int line, | 
 | 42 |                const char* message) { | 
 | 43 |     android::base::StderrLogger(id, severity, tag, file, line, message); | 
| Josh Gao | 68b5d0c | 2018-11-08 13:33:18 -0800 | [diff] [blame] | 44 | #if defined(_WIN32) | 
 | 45 |     // stderr can be buffered on Windows (and setvbuf doesn't seem to work), so explicitly flush. | 
 | 46 |     fflush(stderr); | 
 | 47 | #endif | 
 | 48 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 49 | #if !ADB_HOST | 
| Josh Gao | 95c4497 | 2018-02-06 15:49:26 -0800 | [diff] [blame] | 50 |     // Only print logs of INFO or higher to logcat, so that `adb logcat` with adbd tracing on | 
 | 51 |     // doesn't result in exponential logging. | 
 | 52 |     if (severity >= android::base::INFO) { | 
 | 53 |         gLogdLogger(id, severity, tag, file, line, message); | 
 | 54 |     } | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 55 | #endif | 
 | 56 | } | 
 | 57 |  | 
 | 58 |  | 
 | 59 | #if !ADB_HOST | 
 | 60 | static std::string get_log_file_name() { | 
 | 61 |     struct tm now; | 
 | 62 |     time_t t; | 
 | 63 |     tzset(); | 
 | 64 |     time(&t); | 
 | 65 |     localtime_r(&t, &now); | 
 | 66 |  | 
 | 67 |     char timestamp[PATH_MAX]; | 
 | 68 |     strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now); | 
 | 69 |  | 
 | 70 |     return android::base::StringPrintf("/data/adb/adb-%s-%d", timestamp, | 
 | 71 |                                        getpid()); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | void start_device_log(void) { | 
 | 75 |     int fd = unix_open(get_log_file_name().c_str(), | 
 | 76 |                        O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640); | 
 | 77 |     if (fd == -1) { | 
 | 78 |         return; | 
 | 79 |     } | 
 | 80 |  | 
 | 81 |     // Redirect stdout and stderr to the log file. | 
 | 82 |     dup2(fd, STDOUT_FILENO); | 
 | 83 |     dup2(fd, STDERR_FILENO); | 
 | 84 |     fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); | 
 | 85 |     unix_close(fd); | 
 | 86 | } | 
 | 87 | #endif | 
 | 88 |  | 
 | 89 | int adb_trace_mask; | 
 | 90 |  | 
 | 91 | std::string get_trace_setting_from_env() { | 
 | 92 |     const char* setting = getenv("ADB_TRACE"); | 
 | 93 |     if (setting == nullptr) { | 
 | 94 |         setting = ""; | 
 | 95 |     } | 
 | 96 |  | 
 | 97 |     return std::string(setting); | 
 | 98 | } | 
 | 99 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 100 | std::string get_trace_setting() { | 
 | 101 | #if ADB_HOST | 
 | 102 |     return get_trace_setting_from_env(); | 
 | 103 | #else | 
| Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 104 |     return android::base::GetProperty("persist.adb.trace_mask", ""); | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 105 | #endif | 
 | 106 | } | 
 | 107 |  | 
 | 108 | // Split the space separated list of tags from the trace setting and build the | 
 | 109 | // trace mask from it. note that '1' and 'all' are special cases to enable all | 
 | 110 | // tracing. | 
 | 111 | // | 
 | 112 | // adb's trace setting comes from the ADB_TRACE environment variable, whereas | 
 | 113 | // adbd's comes from the system property persist.adb.trace_mask. | 
 | 114 | static void setup_trace_mask() { | 
 | 115 |     const std::string trace_setting = get_trace_setting(); | 
 | 116 |     if (trace_setting.empty()) { | 
 | 117 |         return; | 
 | 118 |     } | 
 | 119 |  | 
 | 120 |     std::unordered_map<std::string, int> trace_flags = { | 
| Daniel Friederich | b644134 | 2016-12-14 11:28:28 -0600 | [diff] [blame] | 121 |         {"1", -1}, | 
 | 122 |         {"all", -1}, | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 123 |         {"adb", ADB}, | 
 | 124 |         {"sockets", SOCKETS}, | 
 | 125 |         {"packets", PACKETS}, | 
 | 126 |         {"rwx", RWX}, | 
 | 127 |         {"usb", USB}, | 
 | 128 |         {"sync", SYNC}, | 
 | 129 |         {"sysdeps", SYSDEPS}, | 
 | 130 |         {"transport", TRANSPORT}, | 
 | 131 |         {"jdwp", JDWP}, | 
 | 132 |         {"services", SERVICES}, | 
 | 133 |         {"auth", AUTH}, | 
 | 134 |         {"fdevent", FDEVENT}, | 
 | 135 |         {"shell", SHELL}}; | 
 | 136 |  | 
 | 137 |     std::vector<std::string> elements = android::base::Split(trace_setting, " "); | 
 | 138 |     for (const auto& elem : elements) { | 
 | 139 |         const auto& flag = trace_flags.find(elem); | 
 | 140 |         if (flag == trace_flags.end()) { | 
 | 141 |             LOG(ERROR) << "Unknown trace flag: " << elem; | 
 | 142 |             continue; | 
 | 143 |         } | 
 | 144 |  | 
| Daniel Friederich | b644134 | 2016-12-14 11:28:28 -0600 | [diff] [blame] | 145 |         if (flag->second == -1) { | 
 | 146 |             // -1 is used for the special values "1" and "all" that enable all | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 147 |             // tracing. | 
 | 148 |             adb_trace_mask = ~0; | 
| Josh Gao | 95c4497 | 2018-02-06 15:49:26 -0800 | [diff] [blame] | 149 |             break; | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 150 |         } else { | 
 | 151 |             adb_trace_mask |= 1 << flag->second; | 
 | 152 |         } | 
 | 153 |     } | 
| Josh Gao | 95c4497 | 2018-02-06 15:49:26 -0800 | [diff] [blame] | 154 |  | 
 | 155 |     if (adb_trace_mask != 0) { | 
 | 156 |         android::base::SetMinimumLogSeverity(android::base::VERBOSE); | 
 | 157 |     } | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 158 | } | 
 | 159 |  | 
 | 160 | void adb_trace_init(char** argv) { | 
 | 161 | #if !ADB_HOST | 
 | 162 |     // Don't open log file if no tracing, since this will block | 
 | 163 |     // the crypto unmount of /data | 
 | 164 |     if (!get_trace_setting().empty()) { | 
| David Pursell | c5b8ad8 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 165 |         if (unix_isatty(STDOUT_FILENO) == 0) { | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 166 |             start_device_log(); | 
 | 167 |         } | 
 | 168 |     } | 
 | 169 | #endif | 
 | 170 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 171 | #if ADB_HOST && !defined(_WIN32) | 
| Elliott Hughes | 9039030 | 2016-10-26 15:12:14 -0700 | [diff] [blame] | 172 |     // adb historically ignored $ANDROID_LOG_TAGS but passed it through to logcat. | 
 | 173 |     // If set, move it out of the way so that libbase logging doesn't try to parse it. | 
 | 174 |     std::string log_tags; | 
 | 175 |     char* ANDROID_LOG_TAGS = getenv("ANDROID_LOG_TAGS"); | 
 | 176 |     if (ANDROID_LOG_TAGS) { | 
 | 177 |         log_tags = ANDROID_LOG_TAGS; | 
 | 178 |         unsetenv("ANDROID_LOG_TAGS"); | 
 | 179 |     } | 
 | 180 | #endif | 
 | 181 |  | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 182 |     android::base::InitLogging(argv, &AdbLogger); | 
| Elliott Hughes | 9039030 | 2016-10-26 15:12:14 -0700 | [diff] [blame] | 183 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 184 | #if ADB_HOST && !defined(_WIN32) | 
| Elliott Hughes | 9039030 | 2016-10-26 15:12:14 -0700 | [diff] [blame] | 185 |     // Put $ANDROID_LOG_TAGS back so we can pass it to logcat. | 
 | 186 |     if (!log_tags.empty()) setenv("ANDROID_LOG_TAGS", log_tags.c_str(), 1); | 
 | 187 | #endif | 
 | 188 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 189 |     setup_trace_mask(); | 
 | 190 |  | 
 | 191 |     VLOG(ADB) << adb_version(); | 
 | 192 | } | 
 | 193 |  | 
 | 194 | void adb_trace_enable(AdbTrace trace_tag) { | 
 | 195 |     adb_trace_mask |= (1 << trace_tag); | 
 | 196 | } |