Yabin Cui | 19bec5b | 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 | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | #include <android-base/strings.h> |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 26 | |
| 27 | #include "adb.h" |
| 28 | |
| 29 | #if !ADB_HOST |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 30 | #include <android-base/properties.h> |
Yabin Cui | 19bec5b | 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 | e402600 | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 49 | #if !ADB_HOST |
Josh Gao | da85713 | 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 | 19bec5b | 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) { |
Josh Gao | f0c4403 | 2018-12-12 16:12:28 -0800 | [diff] [blame^] | 75 | int fd = unix_open(get_log_file_name(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 76 | if (fd == -1) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Redirect stdout and stderr to the log file. |
| 81 | dup2(fd, STDOUT_FILENO); |
| 82 | dup2(fd, STDERR_FILENO); |
| 83 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| 84 | unix_close(fd); |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | int adb_trace_mask; |
| 89 | |
| 90 | std::string get_trace_setting_from_env() { |
| 91 | const char* setting = getenv("ADB_TRACE"); |
| 92 | if (setting == nullptr) { |
| 93 | setting = ""; |
| 94 | } |
| 95 | |
| 96 | return std::string(setting); |
| 97 | } |
| 98 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 99 | std::string get_trace_setting() { |
| 100 | #if ADB_HOST |
| 101 | return get_trace_setting_from_env(); |
| 102 | #else |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 103 | return android::base::GetProperty("persist.adb.trace_mask", ""); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 104 | #endif |
| 105 | } |
| 106 | |
| 107 | // Split the space separated list of tags from the trace setting and build the |
| 108 | // trace mask from it. note that '1' and 'all' are special cases to enable all |
| 109 | // tracing. |
| 110 | // |
| 111 | // adb's trace setting comes from the ADB_TRACE environment variable, whereas |
| 112 | // adbd's comes from the system property persist.adb.trace_mask. |
| 113 | static void setup_trace_mask() { |
| 114 | const std::string trace_setting = get_trace_setting(); |
| 115 | if (trace_setting.empty()) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | std::unordered_map<std::string, int> trace_flags = { |
Daniel Friederich | 9926f2f | 2016-12-14 11:28:28 -0600 | [diff] [blame] | 120 | {"1", -1}, |
| 121 | {"all", -1}, |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 122 | {"adb", ADB}, |
| 123 | {"sockets", SOCKETS}, |
| 124 | {"packets", PACKETS}, |
| 125 | {"rwx", RWX}, |
| 126 | {"usb", USB}, |
| 127 | {"sync", SYNC}, |
| 128 | {"sysdeps", SYSDEPS}, |
| 129 | {"transport", TRANSPORT}, |
| 130 | {"jdwp", JDWP}, |
| 131 | {"services", SERVICES}, |
| 132 | {"auth", AUTH}, |
| 133 | {"fdevent", FDEVENT}, |
| 134 | {"shell", SHELL}}; |
| 135 | |
| 136 | std::vector<std::string> elements = android::base::Split(trace_setting, " "); |
| 137 | for (const auto& elem : elements) { |
| 138 | const auto& flag = trace_flags.find(elem); |
| 139 | if (flag == trace_flags.end()) { |
| 140 | LOG(ERROR) << "Unknown trace flag: " << elem; |
| 141 | continue; |
| 142 | } |
| 143 | |
Daniel Friederich | 9926f2f | 2016-12-14 11:28:28 -0600 | [diff] [blame] | 144 | if (flag->second == -1) { |
| 145 | // -1 is used for the special values "1" and "all" that enable all |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 146 | // tracing. |
| 147 | adb_trace_mask = ~0; |
Josh Gao | da85713 | 2018-02-06 15:49:26 -0800 | [diff] [blame] | 148 | break; |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 149 | } else { |
| 150 | adb_trace_mask |= 1 << flag->second; |
| 151 | } |
| 152 | } |
Josh Gao | da85713 | 2018-02-06 15:49:26 -0800 | [diff] [blame] | 153 | |
| 154 | if (adb_trace_mask != 0) { |
| 155 | android::base::SetMinimumLogSeverity(android::base::VERBOSE); |
| 156 | } |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void adb_trace_init(char** argv) { |
| 160 | #if !ADB_HOST |
| 161 | // Don't open log file if no tracing, since this will block |
| 162 | // the crypto unmount of /data |
| 163 | if (!get_trace_setting().empty()) { |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 164 | if (unix_isatty(STDOUT_FILENO) == 0) { |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 165 | start_device_log(); |
| 166 | } |
| 167 | } |
| 168 | #endif |
| 169 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 170 | #if ADB_HOST && !defined(_WIN32) |
Elliott Hughes | 679285a | 2016-10-26 15:12:14 -0700 | [diff] [blame] | 171 | // adb historically ignored $ANDROID_LOG_TAGS but passed it through to logcat. |
| 172 | // If set, move it out of the way so that libbase logging doesn't try to parse it. |
| 173 | std::string log_tags; |
| 174 | char* ANDROID_LOG_TAGS = getenv("ANDROID_LOG_TAGS"); |
| 175 | if (ANDROID_LOG_TAGS) { |
| 176 | log_tags = ANDROID_LOG_TAGS; |
| 177 | unsetenv("ANDROID_LOG_TAGS"); |
| 178 | } |
| 179 | #endif |
| 180 | |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 181 | android::base::InitLogging(argv, &AdbLogger); |
Elliott Hughes | 679285a | 2016-10-26 15:12:14 -0700 | [diff] [blame] | 182 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 183 | #if ADB_HOST && !defined(_WIN32) |
Elliott Hughes | 679285a | 2016-10-26 15:12:14 -0700 | [diff] [blame] | 184 | // Put $ANDROID_LOG_TAGS back so we can pass it to logcat. |
| 185 | if (!log_tags.empty()) setenv("ANDROID_LOG_TAGS", log_tags.c_str(), 1); |
| 186 | #endif |
| 187 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 188 | setup_trace_mask(); |
| 189 | |
| 190 | VLOG(ADB) << adb_version(); |
| 191 | } |
| 192 | |
| 193 | void adb_trace_enable(AdbTrace trace_tag) { |
| 194 | adb_trace_mask |= (1 << trace_tag); |
| 195 | } |