blob: 2bd6a3ea1bc2e016ac25b629f8777c5f4a186a72 [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";
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 Gaoe4026002018-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 Cui19bec5b2015-09-22 15:52:57 -070049#if !ADB_HOST
Josh Gaoda857132018-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 Cui19bec5b2015-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) {
Josh Gaof0c44032018-12-12 16:12:28 -080075 int fd = unix_open(get_log_file_name(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
Yabin Cui19bec5b2015-09-22 15:52:57 -070076 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
88int adb_trace_mask;
89
90std::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 Cui19bec5b2015-09-22 15:52:57 -070099std::string get_trace_setting() {
100#if ADB_HOST
101 return get_trace_setting_from_env();
102#else
Elliott Hughes8b249d22016-09-23 15:40:03 -0700103 return android::base::GetProperty("persist.adb.trace_mask", "");
Yabin Cui19bec5b2015-09-22 15:52:57 -0700104#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.
113static 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 Friederich9926f2f2016-12-14 11:28:28 -0600120 {"1", -1},
121 {"all", -1},
Yabin Cui19bec5b2015-09-22 15:52:57 -0700122 {"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 Friederich9926f2f2016-12-14 11:28:28 -0600144 if (flag->second == -1) {
145 // -1 is used for the special values "1" and "all" that enable all
Yabin Cui19bec5b2015-09-22 15:52:57 -0700146 // tracing.
147 adb_trace_mask = ~0;
Josh Gaoda857132018-02-06 15:49:26 -0800148 break;
Yabin Cui19bec5b2015-09-22 15:52:57 -0700149 } else {
150 adb_trace_mask |= 1 << flag->second;
151 }
152 }
Josh Gaoda857132018-02-06 15:49:26 -0800153
154 if (adb_trace_mask != 0) {
155 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
156 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700157}
158
159void 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 Pursell58805362015-10-28 14:29:51 -0700164 if (unix_isatty(STDOUT_FILENO) == 0) {
Yabin Cui19bec5b2015-09-22 15:52:57 -0700165 start_device_log();
166 }
167 }
168#endif
169
Yabin Cui3cf1b362017-03-10 16:01:01 -0800170#if ADB_HOST && !defined(_WIN32)
Elliott Hughes679285a2016-10-26 15:12:14 -0700171 // 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 Low28bc2cb2015-11-07 18:51:54 -0800181 android::base::InitLogging(argv, &AdbLogger);
Elliott Hughes679285a2016-10-26 15:12:14 -0700182
Yabin Cui3cf1b362017-03-10 16:01:01 -0800183#if ADB_HOST && !defined(_WIN32)
Elliott Hughes679285a2016-10-26 15:12:14 -0700184 // 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 Cui19bec5b2015-09-22 15:52:57 -0700188 setup_trace_mask();
189
190 VLOG(ADB) << adb_version();
191}
192
193void adb_trace_enable(AdbTrace trace_tag) {
194 adb_trace_mask |= (1 << trace_tag);
195}