2015-09-23 00:52:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "adb_trace.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/strings.h>
|
2015-09-23 00:52:57 +02:00
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
2016-09-24 00:40:03 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-09-23 00:52:57 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
const char* adb_device_banner = "device";
|
2019-01-23 04:36:15 +01:00
|
|
|
#if defined(__ANDROID__)
|
2015-09-23 00:52:57 +02:00
|
|
|
static android::base::LogdLogger gLogdLogger;
|
2019-01-23 04:36:15 +01:00
|
|
|
#endif
|
2015-09-23 00:52:57 +02:00
|
|
|
#else
|
|
|
|
const char* adb_device_banner = "host";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void AdbLogger(android::base::LogId id, android::base::LogSeverity severity,
|
|
|
|
const char* tag, const char* file, unsigned int line,
|
|
|
|
const char* message) {
|
|
|
|
android::base::StderrLogger(id, severity, tag, file, line, message);
|
2018-11-08 22:33:18 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// stderr can be buffered on Windows (and setvbuf doesn't seem to work), so explicitly flush.
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
|
2019-01-23 04:36:15 +01:00
|
|
|
#if !ADB_HOST && defined(__ANDROID__)
|
2018-02-07 00:49:26 +01:00
|
|
|
// Only print logs of INFO or higher to logcat, so that `adb logcat` with adbd tracing on
|
|
|
|
// doesn't result in exponential logging.
|
|
|
|
if (severity >= android::base::INFO) {
|
|
|
|
gLogdLogger(id, severity, tag, file, line, message);
|
|
|
|
}
|
2015-09-23 00:52:57 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
static std::string get_log_file_name() {
|
|
|
|
struct tm now;
|
|
|
|
time_t t;
|
|
|
|
tzset();
|
|
|
|
time(&t);
|
|
|
|
localtime_r(&t, &now);
|
|
|
|
|
|
|
|
char timestamp[PATH_MAX];
|
|
|
|
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);
|
|
|
|
|
|
|
|
return android::base::StringPrintf("/data/adb/adb-%s-%d", timestamp,
|
|
|
|
getpid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_device_log(void) {
|
2018-12-13 01:12:28 +01:00
|
|
|
int fd = unix_open(get_log_file_name(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
|
2015-09-23 00:52:57 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect stdout and stderr to the log file.
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
|
|
|
|
unix_close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int adb_trace_mask;
|
|
|
|
|
2020-04-21 04:22:05 +02:00
|
|
|
std::string get_trace_setting() {
|
|
|
|
#if ADB_HOST
|
2015-09-23 00:52:57 +02:00
|
|
|
const char* setting = getenv("ADB_TRACE");
|
|
|
|
if (setting == nullptr) {
|
|
|
|
setting = "";
|
|
|
|
}
|
2020-04-21 04:22:05 +02:00
|
|
|
return setting;
|
2015-09-23 00:52:57 +02:00
|
|
|
#else
|
2016-09-24 00:40:03 +02:00
|
|
|
return android::base::GetProperty("persist.adb.trace_mask", "");
|
2015-09-23 00:52:57 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the space separated list of tags from the trace setting and build the
|
|
|
|
// trace mask from it. note that '1' and 'all' are special cases to enable all
|
|
|
|
// tracing.
|
|
|
|
//
|
|
|
|
// adb's trace setting comes from the ADB_TRACE environment variable, whereas
|
|
|
|
// adbd's comes from the system property persist.adb.trace_mask.
|
|
|
|
static void setup_trace_mask() {
|
|
|
|
const std::string trace_setting = get_trace_setting();
|
|
|
|
if (trace_setting.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-13 15:52:04 +01:00
|
|
|
std::unordered_map<std::string, int> trace_flags = {{"1", -1},
|
|
|
|
{"all", -1},
|
|
|
|
{"adb", ADB},
|
|
|
|
{"sockets", SOCKETS},
|
|
|
|
{"packets", PACKETS},
|
|
|
|
{"rwx", RWX},
|
|
|
|
{"usb", USB},
|
|
|
|
{"sync", SYNC},
|
|
|
|
{"sysdeps", SYSDEPS},
|
|
|
|
{"transport", TRANSPORT},
|
|
|
|
{"jdwp", JDWP},
|
|
|
|
{"services", SERVICES},
|
|
|
|
{"auth", AUTH},
|
|
|
|
{"fdevent", FDEVENT},
|
|
|
|
{"shell", SHELL},
|
|
|
|
{"incremental", INCREMENTAL}};
|
2015-09-23 00:52:57 +02:00
|
|
|
|
|
|
|
std::vector<std::string> elements = android::base::Split(trace_setting, " ");
|
|
|
|
for (const auto& elem : elements) {
|
|
|
|
const auto& flag = trace_flags.find(elem);
|
|
|
|
if (flag == trace_flags.end()) {
|
|
|
|
LOG(ERROR) << "Unknown trace flag: " << elem;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:28:28 +01:00
|
|
|
if (flag->second == -1) {
|
|
|
|
// -1 is used for the special values "1" and "all" that enable all
|
2015-09-23 00:52:57 +02:00
|
|
|
// tracing.
|
|
|
|
adb_trace_mask = ~0;
|
2018-02-07 00:49:26 +01:00
|
|
|
break;
|
2015-09-23 00:52:57 +02:00
|
|
|
} else {
|
|
|
|
adb_trace_mask |= 1 << flag->second;
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 00:49:26 +01:00
|
|
|
|
|
|
|
if (adb_trace_mask != 0) {
|
|
|
|
android::base::SetMinimumLogSeverity(android::base::VERBOSE);
|
|
|
|
}
|
2015-09-23 00:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void adb_trace_init(char** argv) {
|
|
|
|
#if !ADB_HOST
|
|
|
|
// Don't open log file if no tracing, since this will block
|
|
|
|
// the crypto unmount of /data
|
|
|
|
if (!get_trace_setting().empty()) {
|
2015-10-28 22:29:51 +01:00
|
|
|
if (unix_isatty(STDOUT_FILENO) == 0) {
|
2015-09-23 00:52:57 +02:00
|
|
|
start_device_log();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
#if ADB_HOST && !defined(_WIN32)
|
2016-10-27 00:12:14 +02:00
|
|
|
// adb historically ignored $ANDROID_LOG_TAGS but passed it through to logcat.
|
|
|
|
// If set, move it out of the way so that libbase logging doesn't try to parse it.
|
|
|
|
std::string log_tags;
|
|
|
|
char* ANDROID_LOG_TAGS = getenv("ANDROID_LOG_TAGS");
|
|
|
|
if (ANDROID_LOG_TAGS) {
|
|
|
|
log_tags = ANDROID_LOG_TAGS;
|
|
|
|
unsetenv("ANDROID_LOG_TAGS");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-08 03:51:54 +01:00
|
|
|
android::base::InitLogging(argv, &AdbLogger);
|
2016-10-27 00:12:14 +02:00
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
#if ADB_HOST && !defined(_WIN32)
|
2016-10-27 00:12:14 +02:00
|
|
|
// Put $ANDROID_LOG_TAGS back so we can pass it to logcat.
|
|
|
|
if (!log_tags.empty()) setenv("ANDROID_LOG_TAGS", log_tags.c_str(), 1);
|
|
|
|
#endif
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
setup_trace_mask();
|
|
|
|
|
|
|
|
VLOG(ADB) << adb_version();
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_trace_enable(AdbTrace trace_tag) {
|
|
|
|
adb_trace_mask |= (1 << trace_tag);
|
|
|
|
}
|