blob: cb83b8327932b44cc746c963348b20c41d68d2cf [file] [log] [blame]
Yabin Cui19bec5b2015-09-22 15:52:57 -07001/*
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 Hughesf55ead92015-12-04 22:00:26 -080024#include <android-base/logging.h>
25#include <android-base/strings.h>
Yabin Cui19bec5b2015-09-22 15:52:57 -070026
27#include "adb.h"
28
29#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070030#include <android-base/properties.h>
Yabin Cui19bec5b2015-09-22 15:52:57 -070031#endif
32
33#if !ADB_HOST
34const char* adb_device_banner = "device";
Josh Gao0560feb2019-01-22 19:36:15 -080035#if defined(__ANDROID__)
Yabin Cui19bec5b2015-09-22 15:52:57 -070036static android::base::LogdLogger gLogdLogger;
Josh Gao0560feb2019-01-22 19:36:15 -080037#endif
Yabin Cui19bec5b2015-09-22 15:52:57 -070038#else
39const char* adb_device_banner = "host";
40#endif
41
42void 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 Gaoe4026002018-11-08 13:33:18 -080046#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 Gao0560feb2019-01-22 19:36:15 -080051#if !ADB_HOST && defined(__ANDROID__)
Josh Gaoda857132018-02-06 15:49:26 -080052 // 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 Cui19bec5b2015-09-22 15:52:57 -070057#endif
58}
59
60
61#if !ADB_HOST
62static 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
Elliott Hughesa8ab5ce2024-06-28 12:10:09 +000076void start_device_log() {
Josh Gaof0c44032018-12-12 16:12:28 -080077 int fd = unix_open(get_log_file_name(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
Yabin Cui19bec5b2015-09-22 15:52:57 -070078 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
90int adb_trace_mask;
91
Josh Gaod7c49ec2020-04-20 19:22:05 -070092std::string get_trace_setting() {
Josh Gaoadb09262020-04-22 18:40:00 -070093#if ADB_HOST || !defined(__ANDROID__)
Yabin Cui19bec5b2015-09-22 15:52:57 -070094 const char* setting = getenv("ADB_TRACE");
95 if (setting == nullptr) {
96 setting = "";
97 }
Josh Gaod7c49ec2020-04-20 19:22:05 -070098 return setting;
Yabin Cui19bec5b2015-09-22 15:52:57 -070099#else
Elliott Hughes8b249d22016-09-23 15:40:03 -0700100 return android::base::GetProperty("persist.adb.trace_mask", "");
Yabin Cui19bec5b2015-09-22 15:52:57 -0700101#endif
102}
103
104// Split the space separated list of tags from the trace setting and build the
105// trace mask from it. note that '1' and 'all' are special cases to enable all
106// tracing.
107//
108// adb's trace setting comes from the ADB_TRACE environment variable, whereas
109// adbd's comes from the system property persist.adb.trace_mask.
110static void setup_trace_mask() {
111 const std::string trace_setting = get_trace_setting();
112 if (trace_setting.empty()) {
113 return;
114 }
115
Fabien Sanglardb94502a2024-06-17 17:37:50 -0700116 std::unordered_map<std::string, int> trace_flags = {
117 {"1", -1},
118 {"all", -1},
119 {"adb", ADB},
120 {"sockets", SOCKETS},
121 {"packets", PACKETS},
122 {"rwx", RWX},
123 {"usb", USB},
124 {"sync", SYNC},
125 {"sysdeps", SYSDEPS},
126 {"transport", TRANSPORT},
127 {"jdwp", JDWP},
128 {"services", SERVICES},
129 {"auth", AUTH},
130 {"fdevent", FDEVENT},
131 {"shell", SHELL},
132 {"incremental", INCREMENTAL},
133 {"mdns", MDNS},
134 };
135
136 // Make sure we check for ALL enum in AdbTrace.
137 size_t num_flags = trace_flags.size() - 2;
138 size_t num_traces = AdbTrace::NUM_TRACES;
139 if (num_flags != num_traces) {
140 LOG(FATAL) << "Mismatched #AdbTrace=" << num_traces
141 << " and trace_flags.size=" << num_flags;
142 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700143
Fabien Sanglarda3544292023-07-21 20:24:20 +0000144 std::vector<std::string> elements = android::base::Split(trace_setting, ", ");
Yabin Cui19bec5b2015-09-22 15:52:57 -0700145 for (const auto& elem : elements) {
146 const auto& flag = trace_flags.find(elem);
147 if (flag == trace_flags.end()) {
148 LOG(ERROR) << "Unknown trace flag: " << elem;
149 continue;
150 }
151
Daniel Friederich9926f2f2016-12-14 11:28:28 -0600152 if (flag->second == -1) {
153 // -1 is used for the special values "1" and "all" that enable all
Yabin Cui19bec5b2015-09-22 15:52:57 -0700154 // tracing.
155 adb_trace_mask = ~0;
Josh Gaoda857132018-02-06 15:49:26 -0800156 break;
Yabin Cui19bec5b2015-09-22 15:52:57 -0700157 } else {
158 adb_trace_mask |= 1 << flag->second;
159 }
160 }
Josh Gaoda857132018-02-06 15:49:26 -0800161
162 if (adb_trace_mask != 0) {
163 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
164 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700165}
166
167void adb_trace_init(char** argv) {
168#if !ADB_HOST
169 // Don't open log file if no tracing, since this will block
170 // the crypto unmount of /data
171 if (!get_trace_setting().empty()) {
David Pursell58805362015-10-28 14:29:51 -0700172 if (unix_isatty(STDOUT_FILENO) == 0) {
Yabin Cui19bec5b2015-09-22 15:52:57 -0700173 start_device_log();
174 }
175 }
176#endif
177
Yabin Cui3cf1b362017-03-10 16:01:01 -0800178#if ADB_HOST && !defined(_WIN32)
Elliott Hughes679285a2016-10-26 15:12:14 -0700179 // adb historically ignored $ANDROID_LOG_TAGS but passed it through to logcat.
180 // If set, move it out of the way so that libbase logging doesn't try to parse it.
181 std::string log_tags;
182 char* ANDROID_LOG_TAGS = getenv("ANDROID_LOG_TAGS");
183 if (ANDROID_LOG_TAGS) {
184 log_tags = ANDROID_LOG_TAGS;
185 unsetenv("ANDROID_LOG_TAGS");
186 }
187#endif
188
Spencer Low28bc2cb2015-11-07 18:51:54 -0800189 android::base::InitLogging(argv, &AdbLogger);
Elliott Hughes679285a2016-10-26 15:12:14 -0700190
Yabin Cui3cf1b362017-03-10 16:01:01 -0800191#if ADB_HOST && !defined(_WIN32)
Elliott Hughes679285a2016-10-26 15:12:14 -0700192 // Put $ANDROID_LOG_TAGS back so we can pass it to logcat.
193 if (!log_tags.empty()) setenv("ANDROID_LOG_TAGS", log_tags.c_str(), 1);
194#endif
195
Yabin Cui19bec5b2015-09-22 15:52:57 -0700196 setup_trace_mask();
197
198 VLOG(ADB) << adb_version();
199}
200
201void adb_trace_enable(AdbTrace trace_tag) {
202 adb_trace_mask |= (1 << trace_tag);
203}