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