blob: a024a89bf7b4e19873d6abbbff1e0ac549a36a1c [file] [log] [blame]
Yabin Cuiaed3c612015-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 Hughes4f713192015-12-04 22:00:26 -080024#include <android-base/logging.h>
25#include <android-base/strings.h>
Yabin Cuiaed3c612015-09-22 15:52:57 -070026
27#include "adb.h"
28
29#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070030#include <android-base/properties.h>
Yabin Cuiaed3c612015-09-22 15:52:57 -070031#endif
32
33#if !ADB_HOST
34const char* adb_device_banner = "device";
35static android::base::LogdLogger gLogdLogger;
36#else
37const char* adb_device_banner = "host";
38#endif
39
40void 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 Gao68b5d0c2018-11-08 13:33:18 -080044#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 Cuiaed3c612015-09-22 15:52:57 -070049#if !ADB_HOST
Josh Gao95c44972018-02-06 15:49:26 -080050 // 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 Cuiaed3c612015-09-22 15:52:57 -070055#endif
56}
57
58
59#if !ADB_HOST
60static 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
74void 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
89int adb_trace_mask;
90
91std::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 Cuiaed3c612015-09-22 15:52:57 -0700100std::string get_trace_setting() {
101#if ADB_HOST
102 return get_trace_setting_from_env();
103#else
Elliott Hughesffdec182016-09-23 15:40:03 -0700104 return android::base::GetProperty("persist.adb.trace_mask", "");
Yabin Cuiaed3c612015-09-22 15:52:57 -0700105#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.
114static 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 Friederichb6441342016-12-14 11:28:28 -0600121 {"1", -1},
122 {"all", -1},
Yabin Cuiaed3c612015-09-22 15:52:57 -0700123 {"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 Friederichb6441342016-12-14 11:28:28 -0600145 if (flag->second == -1) {
146 // -1 is used for the special values "1" and "all" that enable all
Yabin Cuiaed3c612015-09-22 15:52:57 -0700147 // tracing.
148 adb_trace_mask = ~0;
Josh Gao95c44972018-02-06 15:49:26 -0800149 break;
Yabin Cuiaed3c612015-09-22 15:52:57 -0700150 } else {
151 adb_trace_mask |= 1 << flag->second;
152 }
153 }
Josh Gao95c44972018-02-06 15:49:26 -0800154
155 if (adb_trace_mask != 0) {
156 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
157 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700158}
159
160void 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 Pursellc5b8ad82015-10-28 14:29:51 -0700165 if (unix_isatty(STDOUT_FILENO) == 0) {
Yabin Cuiaed3c612015-09-22 15:52:57 -0700166 start_device_log();
167 }
168 }
169#endif
170
Yabin Cuib5e11412017-03-10 16:01:01 -0800171#if ADB_HOST && !defined(_WIN32)
Elliott Hughes90390302016-10-26 15:12:14 -0700172 // 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 Low363af562015-11-07 18:51:54 -0800182 android::base::InitLogging(argv, &AdbLogger);
Elliott Hughes90390302016-10-26 15:12:14 -0700183
Yabin Cuib5e11412017-03-10 16:01:01 -0800184#if ADB_HOST && !defined(_WIN32)
Elliott Hughes90390302016-10-26 15:12:14 -0700185 // 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 Cuiaed3c612015-09-22 15:52:57 -0700189 setup_trace_mask();
190
191 VLOG(ADB) << adb_version();
192}
193
194void adb_trace_enable(AdbTrace trace_tag) {
195 adb_trace_mask |= (1 << trace_tag);
196}